Skip to content

Commit 2d18684

Browse files
Fixing review comments
1 parent 3092eb1 commit 2d18684

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

Sources/Algorithms/Chunked.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -313,11 +313,13 @@ extension ChunkedByCount: Collection {
313313

314314
/// - Complexity: O(n)
315315
public subscript(i: Index) -> Element {
316-
base[i.baseRange]
316+
precondition(i != endIndex, "Index out of range")
317+
return base[i.baseRange]
317318
}
318319

319320
@inlinable
320321
public func index(after i: Index) -> Index {
322+
precondition(i != endIndex, "Index out of range")
321323
let baseIdx = base.index(
322324
i.baseRange.upperBound, offsetBy: chunkCount,
323325
limitedBy: base.endIndex
@@ -339,6 +341,8 @@ extension ChunkedByCount:
339341
where Base: RandomAccessCollection {
340342
@inlinable
341343
public func index(before i: Index) -> Index {
344+
precondition(i != startIndex, "Index out of range")
345+
342346
var offset = chunkCount
343347
if i.baseRange.lowerBound == base.endIndex {
344348
let remainder = base.count%chunkCount
@@ -361,18 +365,14 @@ where Base: RandomAccessCollection {
361365
to: end.baseRange.lowerBound)
362366
let (quotient, remainder) =
363367
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()
369369
}
370370

371371
@inlinable
372372
public var count: Int {
373373
let (quotient, remainder) =
374374
base.count.quotientAndRemainder(dividingBy: chunkCount)
375-
return quotient + (remainder == 0 ? 0 : 1)
375+
return quotient + remainder.signum()
376376
}
377377
}
378378

@@ -404,10 +404,10 @@ extension Collection {
404404
extension ChunkedByCount: Equatable where Base: Equatable {}
405405

406406
// 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
408408
// 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.
411411
extension ChunkedByCount: Hashable where Base: Hashable {
412412
public func hash(into hasher: inout Hasher) {
413413
hasher.combine(base)

0 commit comments

Comments
 (0)