@@ -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, " Index out of range " )
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, " Index out of range " )
345
+
342
346
var offset = chunkCount
343
347
if i. baseRange. lowerBound == base. endIndex {
344
348
let remainder = base. count% chunkCount
@@ -361,18 +365,14 @@ where Base: RandomAccessCollection {
361
365
to: end. baseRange. lowerBound)
362
366
let ( quotient, remainder) =
363
367
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 )
368
+ return quotient + remainder. signum ( )
369
369
}
370
370
371
371
@inlinable
372
372
public var count : Int {
373
373
let ( quotient, remainder) =
374
374
base. count. quotientAndRemainder ( dividingBy: chunkCount)
375
- return quotient + ( remainder == 0 ? 0 : 1 )
375
+ return quotient + remainder. signum ( )
376
376
}
377
377
}
378
378
@@ -404,10 +404,10 @@ extension Collection {
404
404
extension ChunkedByCount : Equatable where Base: Equatable { }
405
405
406
406
// Since we have another stored property of type `Index` on the
407
- // collection, synthetization of hashble conformace would require
407
+ // collection, synthesis of `Hashble` conformace would require
408
408
// 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.
409
+ // only in terms of ` base` . Since the computed index is based on it,
410
+ // it should not make a difference here.
411
411
extension ChunkedByCount : Hashable where Base: Hashable {
412
412
public func hash( into hasher: inout Hasher ) {
413
413
hasher. combine ( base)
0 commit comments