Skip to content

Commit 1f71978

Browse files
Fixing review comments
1 parent 3092eb1 commit 1f71978

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

Sources/Algorithms/Chunked.swift

Lines changed: 13 additions & 11 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
@@ -353,26 +357,24 @@ where Base: RandomAccessCollection {
353357
) ?? base.startIndex
354358
return Index(_baseRange: baseIdx..<i.baseRange.lowerBound)
355359
}
356-
360+
}
361+
362+
extension ChunkedByCount {
357363
@inlinable
358364
public func distance(from start: Index, to end: Index) -> Int {
359365
let distance =
360366
base.distance(from: start.baseRange.lowerBound,
361367
to: end.baseRange.lowerBound)
362368
let (quotient, remainder) =
363369
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)
370+
return quotient + remainder.signum()
369371
}
370372

371373
@inlinable
372374
public var count: Int {
373375
let (quotient, remainder) =
374376
base.count.quotientAndRemainder(dividingBy: chunkCount)
375-
return quotient + (remainder == 0 ? 0 : 1)
377+
return quotient + remainder.signum()
376378
}
377379
}
378380

@@ -404,10 +406,10 @@ extension Collection {
404406
extension ChunkedByCount: Equatable where Base: Equatable {}
405407

406408
// Since we have another stored property of type `Index` on the
407-
// collection, synthetization of hashble conformace would require
409+
// collection, synthesis of `Hashble` conformace would require
408410
// 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.
411+
// only in terms of `base`. Since the computed index is based on it,
412+
// it should not make a difference here.
411413
extension ChunkedByCount: Hashable where Base: Hashable {
412414
public func hash(into hasher: inout Hasher) {
413415
hasher.combine(base)

0 commit comments

Comments
 (0)