Skip to content

Commit 79aeb24

Browse files
Fixing review comments
1 parent 3092eb1 commit 79aeb24

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
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)

Tests/SwiftAlgorithmsTests/ChunkedTests.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,14 @@ final class ChunkedTests: XCTestCase {
113113
XCTAssertEqual([Int]().chunks(ofCount: 1).count, 0)
114114
XCTAssertEqual([Int]().chunks(ofCount: 5).count, 0)
115115

116-
let collection = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
117-
XCTAssertEqual(collection.chunks(ofCount: 1).count, 10)
118-
XCTAssertEqual(collection.chunks(ofCount: 3).count, 4)
119-
XCTAssertEqual(collection.chunks(ofCount: 5).count, 2)
120-
XCTAssertEqual(collection.chunks(ofCount: 11).count, 1)
116+
let collection1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
117+
XCTAssertEqual(collection1.chunks(ofCount: 1).count, 10)
118+
XCTAssertEqual(collection1.chunks(ofCount: 3).count, 4)
119+
XCTAssertEqual(collection1.chunks(ofCount: 5).count, 2)
120+
XCTAssertEqual(collection1.chunks(ofCount: 11).count, 1)
121+
122+
let collection2 = (1...50).map { $0 }
123+
XCTAssertEqual(collection2.chunks(ofCount: 9).count, 6)
121124
}
122125

123126
func testEmptyChunksTraversal() {

0 commit comments

Comments
 (0)