Skip to content

Commit

Permalink
changes to count, distance and offsetBy
Browse files Browse the repository at this point in the history
  • Loading branch information
ollieatkinson committed Oct 21, 2020
1 parent 3c0e501 commit afbb684
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 14 deletions.
17 changes: 9 additions & 8 deletions Sources/Algorithms/Stride.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,22 @@ extension Stride: Collection {
base.index(i.base, offsetBy: distance * stride, limitedBy: limit.base)
.map(Index.init)
}

public var count: Int {
(base.count - 1) / stride + 1
let limit = base.count - 1
return limit / stride + (limit < 0 ? 0 : 1)
}

public func distance(from start: Index, to end: Index) -> Int {
base.distance(from: start.base, to: end.base) / stride
let distance = base.distance(from: start.base, to: end.base)
return distance / stride + (distance % stride > 0 ? 1 : 0)
}

public func index(_ i: Index, offsetBy distance: Int) -> Index {
Index(base.index(i.base, offsetBy: distance * stride))
precondition(i.base < base.endIndex, "Advancing past end index")
let limit = distance > 0 ? endIndex : startIndex
return index(i, offsetBy: distance, limitedBy: limit) ?? limit
}

}

extension Stride: BidirectionalCollection
Expand Down Expand Up @@ -137,8 +140,7 @@ extension Stride: Equatable
where Base.Element: Equatable {

public static func == (lhs: Stride, rhs: Stride) -> Bool {
guard lhs.count == rhs.count else { return false }
return lhs.elementsEqual(rhs, by: ==)
lhs.elementsEqual(rhs, by: ==)
}

}
Expand All @@ -147,7 +149,6 @@ extension Stride: Hashable
where Base.Element: Hashable {

public func hash(into hasher: inout Hasher) {
hasher.combine(count)
hasher.combine(stride)
for element in self {
hasher.combine(element)
Expand Down
59 changes: 53 additions & 6 deletions Tests/SwiftAlgorithmsTests/StrideTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ final class StridingTests: XCTestCase {
}

func testCount() {
let empty = [Int]().striding(by: 2)
XCTAssertEqual(empty.count, 0)
let a = (0...10)
XCTAssertEqual(a.striding(by: 1).count, (0...10).count)
XCTAssertEqual(a.striding(by: 2).count, [0, 2, 4, 6, 8, 10].count)
Expand All @@ -87,12 +89,50 @@ final class StridingTests: XCTestCase {
}

func testDistance() {
let a = (0...100).striding(by: 12)
var i = a.startIndex
a.formIndex(&i, offsetBy: 3)
XCTAssertEqual(a.distance(from: a.startIndex, to: i), 3)
XCTAssertEqual(a[i], 36)
XCTAssertEqual(a.distance(from: a.startIndex, to: a.endIndex), a.count - 1)

do {
let a = (0...100).striding(by: 11)
XCTAssertEqual(a.distance(from: a.startIndex, to: a.endIndex), a.count)
for (i, index) in a.indices.enumerated() {
XCTAssertEqual(a.distance(from: a.startIndex, to: index), i)
}

var i = a.startIndex
a.formIndex(&i, offsetBy: 3)
XCTAssertEqual(a.distance(from: a.startIndex, to: i), 3)
XCTAssertEqual(a[i], 33)
}

do {

let a = (0...100).striding(by: 10)
XCTAssertEqual(a.distance(from: a.startIndex, to: a.endIndex), a.count)

for (i, index) in a.indices.enumerated() {
XCTAssertEqual(a.distance(from: a.startIndex, to: index), i)
}

var i = a.startIndex
a.formIndex(&i, offsetBy: 3)
XCTAssertEqual(a.distance(from: a.startIndex, to: i), 3)
XCTAssertEqual(a[i], 30)
}

do {

let a = (0...100).striding(by: 101)
XCTAssertEqual(a.distance(from: a.startIndex, to: a.endIndex), a.count)

for (i, index) in a.indices.enumerated() {
XCTAssertEqual(a.distance(from: a.startIndex, to: index), i)
}

var i = a.startIndex
a.formIndex(&i, offsetBy: 3)
XCTAssertEqual(a.distance(from: a.startIndex, to: i), a.count)
XCTAssertEqual(i, a.endIndex)
// a[i] // == Fatal error: Index out of range
}
}

func testOffsetBy() {
Expand All @@ -102,4 +142,11 @@ final class StridingTests: XCTestCase {
XCTAssertEqual(a[a.index(a.startIndex, offsetBy: i)], b[i])
}
}

func testOffsetByEndIndex() {
let a = 1...5
let b = a.striding(by: 3) // [1, 4]
let i = b.index(b.startIndex, offsetBy: 2)
XCTAssertEqual(i, b.endIndex)
}
}

0 comments on commit afbb684

Please sign in to comment.