Skip to content

Commit

Permalink
Fixing review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
LucianoPAlmeida committed Jan 8, 2021
1 parent 3092eb1 commit 741d559
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions Sources/Algorithms/Chunked.swift
Original file line number Diff line number Diff line change
Expand Up @@ -313,11 +313,13 @@ extension ChunkedByCount: Collection {

/// - Complexity: O(n)
public subscript(i: Index) -> Element {
base[i.baseRange]
precondition(i < endIndex, "Index out of range")
return base[i.baseRange]
}

@inlinable
public func index(after i: Index) -> Index {
precondition(i < endIndex, "Advancing past end index")
let baseIdx = base.index(
i.baseRange.upperBound, offsetBy: chunkCount,
limitedBy: base.endIndex
Expand All @@ -339,6 +341,8 @@ extension ChunkedByCount:
where Base: RandomAccessCollection {
@inlinable
public func index(before i: Index) -> Index {
precondition(i > startIndex, "Advancing past start index")

var offset = chunkCount
if i.baseRange.lowerBound == base.endIndex {
let remainder = base.count%chunkCount
Expand All @@ -354,25 +358,25 @@ where Base: RandomAccessCollection {
return Index(_baseRange: baseIdx..<i.baseRange.lowerBound)
}

// TODO: index(_:offsetBy:) and index(_:offsetBy:limitedBy:)
}

extension ChunkedByCount {
@inlinable
public func distance(from start: Index, to end: Index) -> Int {
let distance =
base.distance(from: start.baseRange.lowerBound,
to: end.baseRange.lowerBound)
let (quotient, remainder) =
distance.quotientAndRemainder(dividingBy: chunkCount)
// Increment should account for negative distances.
if remainder < 0 {
return quotient - 1
}
return quotient + (remainder == 0 ? 0 : 1)
return quotient + remainder.signum()
}

@inlinable
public var count: Int {
let (quotient, remainder) =
base.count.quotientAndRemainder(dividingBy: chunkCount)
return quotient + (remainder == 0 ? 0 : 1)
return quotient + remainder.signum()
}
}

Expand Down Expand Up @@ -404,10 +408,10 @@ extension Collection {
extension ChunkedByCount: Equatable where Base: Equatable {}

// Since we have another stored property of type `Index` on the
// collection, synthetization of hashble conformace would require
// collection, synthesis of `Hashble` conformace would require
// a `Base.Index: Hashable` constraint, so we implement the hasher
// only in terms of base. Since the computed index is based on it,
// it should make a difference here.
// only in terms of `base`. Since the computed index is based on it,
// it should not make a difference here.
extension ChunkedByCount: Hashable where Base: Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(base)
Expand Down

0 comments on commit 741d559

Please sign in to comment.