Skip to content

Commit 741d559

Browse files
Fixing review comments
1 parent 3092eb1 commit 741d559

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

Sources/Algorithms/Chunked.swift

Lines changed: 14 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, "Advancing past end index")
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, "Advancing past start index")
345+
342346
var offset = chunkCount
343347
if i.baseRange.lowerBound == base.endIndex {
344348
let remainder = base.count%chunkCount
@@ -354,25 +358,25 @@ where Base: RandomAccessCollection {
354358
return Index(_baseRange: baseIdx..<i.baseRange.lowerBound)
355359
}
356360

361+
// TODO: index(_:offsetBy:) and index(_:offsetBy:limitedBy:)
362+
}
363+
364+
extension ChunkedByCount {
357365
@inlinable
358366
public func distance(from start: Index, to end: Index) -> Int {
359367
let distance =
360368
base.distance(from: start.baseRange.lowerBound,
361369
to: end.baseRange.lowerBound)
362370
let (quotient, remainder) =
363371
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()
369373
}
370374

371375
@inlinable
372376
public var count: Int {
373377
let (quotient, remainder) =
374378
base.count.quotientAndRemainder(dividingBy: chunkCount)
375-
return quotient + (remainder == 0 ? 0 : 1)
379+
return quotient + remainder.signum()
376380
}
377381
}
378382

@@ -404,10 +408,10 @@ extension Collection {
404408
extension ChunkedByCount: Equatable where Base: Equatable {}
405409

406410
// 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
408412
// 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.
411415
extension ChunkedByCount: Hashable where Base: Hashable {
412416
public func hash(into hasher: inout Hasher) {
413417
hasher.combine(base)

0 commit comments

Comments
 (0)