@@ -313,11 +313,13 @@ extension ChunkedByCount: Collection {
313
313
314
314
/// - Complexity: O(n)
315
315
public subscript( i: Index ) -> Element {
316
- base [ i. baseRange]
316
+ precondition ( i < endIndex, " Index out of range " )
317
+ return base [ i. baseRange]
317
318
}
318
319
319
320
@inlinable
320
321
public func index( after i: Index ) -> Index {
322
+ precondition ( i < endIndex, " Advancing past end index " )
321
323
let baseIdx = base. index (
322
324
i. baseRange. upperBound, offsetBy: chunkCount,
323
325
limitedBy: base. endIndex
@@ -339,6 +341,8 @@ extension ChunkedByCount:
339
341
where Base: RandomAccessCollection {
340
342
@inlinable
341
343
public func index( before i: Index ) -> Index {
344
+ precondition ( i > startIndex, " Advancing past start index " )
345
+
342
346
var offset = chunkCount
343
347
if i. baseRange. lowerBound == base. endIndex {
344
348
let remainder = base. count% chunkCount
@@ -354,25 +358,25 @@ where Base: RandomAccessCollection {
354
358
return Index ( _baseRange: baseIdx..< i. baseRange. lowerBound)
355
359
}
356
360
361
+ // TODO: index(_:offsetBy:) and index(_:offsetBy:limitedBy:)
362
+ }
363
+
364
+ extension ChunkedByCount {
357
365
@inlinable
358
366
public func distance( from start: Index , to end: Index ) -> Int {
359
367
let distance =
360
368
base. distance ( from: start. baseRange. lowerBound,
361
369
to: end. baseRange. lowerBound)
362
370
let ( quotient, remainder) =
363
371
distance. quotientAndRemainder ( dividingBy: chunkCount)
364
- // Increment should account for negative distances.
365
- if remainder < 0 {
366
- return quotient - 1
367
- }
368
- return quotient + ( remainder == 0 ? 0 : 1 )
372
+ return quotient + remainder. signum ( )
369
373
}
370
374
371
375
@inlinable
372
376
public var count : Int {
373
377
let ( quotient, remainder) =
374
378
base. count. quotientAndRemainder ( dividingBy: chunkCount)
375
- return quotient + ( remainder == 0 ? 0 : 1 )
379
+ return quotient + remainder. signum ( )
376
380
}
377
381
}
378
382
@@ -404,10 +408,10 @@ extension Collection {
404
408
extension ChunkedByCount : Equatable where Base: Equatable { }
405
409
406
410
// Since we have another stored property of type `Index` on the
407
- // collection, synthetization of hashble conformace would require
411
+ // collection, synthesis of `Hashble` conformace would require
408
412
// a `Base.Index: Hashable` constraint, so we implement the hasher
409
- // only in terms of base. Since the computed index is based on it,
410
- // it should make a difference here.
413
+ // only in terms of ` base` . Since the computed index is based on it,
414
+ // it should not make a difference here.
411
415
extension ChunkedByCount : Hashable where Base: Hashable {
412
416
public func hash( into hasher: inout Hasher ) {
413
417
hasher. combine ( base)
0 commit comments