// // Collection.swift // LoopKit // // Created by Michael Pangburn on 2/14/19. // Copyright © 2019 LoopKit Authors. All rights reserved. // /// Returns the cartesian product of a sequence and a collection. /// /// O(1), but O(_n_*_m_) on iteration. /// - Note: Don't mind the scary return type; it's just a lazy sequence. func product(_ s: S, _ c: C) -> LazySequence>>> { return s.lazy.flatMap { first in c.lazy.map { second in (first, second) } } } extension Collection { /// Returns a sequence containing adjacent pairs of elements in the ordered collection. func adjacentPairs() -> Zip2Sequence { return zip(self, dropFirst()) } } extension RandomAccessCollection { /// Returns all unique pair combinations of elements in the collection. /// /// O(1), but O(*n*²) on iteration. /// - Note: Don't mind the scary return type; it's just a lazy sequence. func allPairs() -> LazyMapSequence>>>, (Element, Element)> { return product(indices, indices).filter(<).map { (self[$0], self[$1]) } } } extension RangeReplaceableCollection where Index: Hashable { /// Removes the elements at all of the given indices. /// /// O(_n_*_m_) mutating func removeAll(at indices: S) where S.Element == Index { let arranged = Set(indices).sorted(by: >) for index in arranged { remove(at: index) } } }