Skip to content

[SR-12881] DefaultIndices dynamic dispatch #32019

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions stdlib/public/core/Indices.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,23 @@ extension DefaultIndices: Collection {
public var indices: Indices {
return self
}

@_alwaysEmitIntoClient
public func index(_ i: Index, offsetBy distance: Int) -> Index {
return _elements.index(i, offsetBy: distance)
}

@_alwaysEmitIntoClient
public func index(
_ i: Index, offsetBy distance: Int, limitedBy limit: Index
) -> Index? {
return _elements.index(i, offsetBy: distance, limitedBy: limit)
}

@_alwaysEmitIntoClient
public func distance(from start: Index, to end: Index) -> Int {
return _elements.distance(from: start, to: end)
}
}

extension DefaultIndices: BidirectionalCollection
Expand Down
40 changes: 40 additions & 0 deletions test/stdlib/DefaultIndices.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// RUN: %target-run-stdlib-swift
// REQUIRES: executable_test

import StdlibUnittest

var suite = TestSuite("DefaultIndices")
defer { runAllTests() }

extension Collection {
func genericDistance(from start: Index, to end: Index) -> Int {
return distance(from: start, to: end)
}

func genericIndex(_ i: Index, offsetBy distance: Int) -> Index {
return index(i, offsetBy: distance)
}

func genericIndex(
_ i: Index, offsetBy distance: Int, limitedBy limit: Index
) -> Index? {
return index(i, offsetBy: distance, limitedBy: limit)
}
}

suite.test("Bidirectional dispatch") {
var r = (0...10).indices
expectType(DefaultIndices<ClosedRange<Int>>.self, &r)

let d = r.distance(from: r.endIndex, to: r.startIndex)
let d2 = r.genericDistance(from: r.endIndex, to: r.startIndex)
expectEqual(d, d2)

let i = r.index(r.endIndex, offsetBy: -1)
let i2 = r.genericIndex(r.endIndex, offsetBy: -1)
expectEqual(i, i2)

let j = r.index(r.startIndex, offsetBy: -1, limitedBy: r.startIndex)
let j2 = r.genericIndex(r.startIndex, offsetBy: -1, limitedBy: r.startIndex)
expectEqual(j, j2)
}