Skip to content

Commit 91f1e73

Browse files
Adjustments on distance logic and remove count because it is already on terms of distance and distance is O(1).
1 parent 76e6d2f commit 91f1e73

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

Guides/Chunked.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Break a collection into subsequences where consecutive elements pass a binary
77
predicate, or where all elements in each chunk project to the same value.
88

99
Also, includes a `chunks(ofCount:)` that breaks a collection into subsequences
10-
of a given size.
10+
of a given `count`.
1111

1212
There are two variations of the `chunked` method: `chunked(by:)` and
1313
`chunked(on:)`. `chunked(by:)` uses a binary predicate to test consecutive
@@ -29,10 +29,11 @@ let chunks = names.chunked(on: \.first!)
2929
// [["David"], ["Kyle", "Karoy"], ["Nate"]]
3030
```
3131

32-
The `chunks(ofCount:)` takes a `size` parameter (required to be > 0) and separates
33-
the collection into `n` chunks of this given size. If the size parameter is
32+
The `chunks(ofCount:)` takes a `count` parameter (required to be > 0) and separates
33+
the collection into `n` chunks of this given count. If the `count` parameter is
3434
evenly divided by the count of the base `Collection` all the chunks will have
35-
the count equals to size. Otherwise, the last chunk will contain the remaining elements.
35+
the count equals to the parameter. Otherwise, the last chunk will contain the
36+
remaining elements.
3637

3738
```swift
3839
let names = ["David", "Kyle", "Karoy", "Nate"]

Sources/Algorithms/Chunked.swift

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,11 @@ where Base: RandomAccessCollection {
331331
let distance = base.distance(from: start.base, to: end.base)
332332
let (quotient, remainder) =
333333
distance.quotientAndRemainder(dividingBy: _chunkCount)
334-
return quotient + remainder
334+
// Increment should account for negative distances.
335+
if remainder < 0 {
336+
return quotient - 1
337+
}
338+
return quotient + (remainder == 0 ? 0 : 1)
335339
}
336340

337341
@inlinable
@@ -343,13 +347,6 @@ where Base: RandomAccessCollection {
343347
base.index(i.base, offsetBy: n * _chunkCount, limitedBy: bound) ?? bound
344348
)
345349
}
346-
347-
@inlinable
348-
public var count: Int {
349-
return base.count
350-
.isMultiple(of: _chunkCount) ? base.count/_chunkCount
351-
: base.count/_chunkCount + 1
352-
}
353350
}
354351

355352
extension Collection {
@@ -371,7 +368,7 @@ extension Collection {
371368
/// - Complexity: O(1)
372369
@inlinable
373370
public func chunks(ofCount count: Int) -> ChunkedCollection<Self> {
374-
assert(count > 0, " Cannot chunk with size <= 0!")
371+
precondition(count > 0, " Cannot chunk with count <= 0!")
375372
return ChunkedCollection(_base: self, _chunkCount: count)
376373
}
377374
}

0 commit comments

Comments
 (0)