From 99152ecc6836379baa41b74969ff806885c22f63 Mon Sep 17 00:00:00 2001 From: Tim Vermeulen Date: Wed, 2 Dec 2020 22:25:50 +0100 Subject: [PATCH] Unify chunked(by:) and chunked(on:) (#44) * Unify the eager `chunked(on:)` and `chunked(by:)` * Unify the lazy `chunked(on:)` and `chunked(by:)` * Trap when going past the end * Add preconditions in `index(before:)`/`index(after:)` --- Sources/Algorithms/Chunked.swift | 112 ++++++++++++++++++++----------- 1 file changed, 71 insertions(+), 41 deletions(-) diff --git a/Sources/Algorithms/Chunked.swift b/Sources/Algorithms/Chunked.swift index a3dc0d3..e3dbba5 100644 --- a/Sources/Algorithms/Chunked.swift +++ b/Sources/Algorithms/Chunked.swift @@ -9,17 +9,26 @@ // //===----------------------------------------------------------------------===// -public struct LazyChunked { +public struct LazyChunked { /// The collection that this instance provides a view onto. public let base: Base /// The projection function. @usableFromInline - internal var belongInSameGroup: (Base.Element, Base.Element) -> Bool + internal let projection: (Base.Element) -> Subject + + /// The predicate. + @usableFromInline + internal let belongInSameGroup: (Subject, Subject) -> Bool @usableFromInline - internal init(base: Base, belongInSameGroup: @escaping (Base.Element, Base.Element) -> Bool) { + internal init( + base: Base, + projection: @escaping (Base.Element) -> Subject, + belongInSameGroup: @escaping (Subject, Subject) -> Bool + ) { self.base = base + self.projection = projection self.belongInSameGroup = belongInSameGroup } } @@ -64,13 +73,14 @@ extension LazyChunked: LazyCollectionProtocol { } } - /// Returns the index in the base collection for the first element that - /// doesn't match the current chunk. + /// Returns the index in the base collection of the end of the chunk starting + /// at the given index. @usableFromInline - internal func endOfChunk(from i: Base.Index) -> Base.Index { - guard i != base.endIndex else { return base.endIndex } - return base[base.index(after: i)...] - .firstIndex(where: { !belongInSameGroup($0, base[i]) }) ?? base.endIndex + internal func endOfChunk(startingAt start: Base.Index) -> Base.Index { + let subject = projection(base[start]) + return base[base.index(after: start)...] + .firstIndex(where: { !belongInSameGroup(subject, projection($0)) }) + ?? base.endIndex } @inlinable @@ -80,20 +90,22 @@ extension LazyChunked: LazyCollectionProtocol { @inlinable public var endIndex: Index { - Index(lowerBound: base.endIndex, upperBound: base.endIndex) + Index(lowerBound: base.endIndex) } @inlinable public func index(after i: Index) -> Index { - let upperBound = i.upperBound ?? endOfChunk(from: i.lowerBound) - let end = endOfChunk(from: upperBound) + precondition(i != endIndex, "Can't advance past endIndex") + let upperBound = i.upperBound ?? endOfChunk(startingAt: i.lowerBound) + guard upperBound != base.endIndex else { return endIndex } + let end = endOfChunk(startingAt: upperBound) return Index(lowerBound: upperBound, upperBound: end) } @inlinable public subscript(position: Index) -> Base.SubSequence { let upperBound = position.upperBound - ?? endOfChunk(from: position.lowerBound) + ?? endOfChunk(startingAt: position.lowerBound) return base[position.lowerBound.. Base.Index { + let indexBeforeEnd = base.index(before: end) + // Get the projected value of the last element in the range ending at `end`. - let lastOfPreviousChunk = base[base.index(before: end)] + let subject = projection(base[indexBeforeEnd]) // Search backward from `end` for the first element whose projection isn't - // equal to `lastOfPreviousChunk`. - if let firstMismatch = base[.. Index { + precondition(i != startIndex, "Can't advance before startIndex") let start = startOfChunk(endingAt: i.lowerBound) return Index(lowerBound: start, upperBound: i.lowerBound) } @@ -146,8 +161,11 @@ extension LazyCollectionProtocol { @inlinable public func chunked( by belongInSameGroup: @escaping (Element, Element) -> Bool - ) -> LazyChunked { - LazyChunked(base: elements, belongInSameGroup: belongInSameGroup) + ) -> LazyChunked { + LazyChunked( + base: elements, + projection: { $0 }, + belongInSameGroup: belongInSameGroup) } /// Returns a lazy collection of subsequences of this collection, chunked by @@ -159,10 +177,11 @@ extension LazyCollectionProtocol { @inlinable public func chunked( on projection: @escaping (Element) -> Subject - ) -> LazyChunked { + ) -> LazyChunked { LazyChunked( base: elements, - belongInSameGroup: { projection($0) == projection($1) }) + projection: projection, + belongInSameGroup: ==) } } @@ -172,27 +191,28 @@ extension LazyCollectionProtocol { extension Collection { /// Returns a collection of subsequences of this collection, chunked by - /// the given predicate. + /// grouping elements that project to the same value according to the given + /// predicate. /// /// - Complexity: O(*n*), where *n* is the length of this collection. - @inlinable - public func chunked( - by belongInSameGroup: (Element, Element) -> Bool - ) -> [SubSequence] { + @usableFromInline + internal func chunked( + on projection: (Element) throws -> Subject, + by belongInSameGroup: (Subject, Subject) throws -> Bool + ) rethrows -> [SubSequence] { guard !isEmpty else { return [] } var result: [SubSequence] = [] var start = startIndex - var current = startIndex - while true { - let next = index(after: current) - if next == endIndex { break } - - if !belongInSameGroup(self[current], self[next]) { - result.append(self[start.. Bool + ) rethrows -> [SubSequence] { + try chunked(on: { $0 }, by: belongInSameGroup) + } /// Returns a collection of subsequences of this collection, chunked by /// grouping elements that project to the same value. @@ -208,9 +239,8 @@ extension Collection { /// - Complexity: O(*n*), where *n* is the length of this collection. @inlinable public func chunked( - on projection: (Element) -> Subject - ) -> [SubSequence] { - chunked(by: { projection($0) == projection($1) }) + on projection: (Element) throws -> Subject + ) rethrows -> [SubSequence] { + try chunked(on: projection, by: ==) } } -