Skip to content

[stdlib] Eradicate IndexDistance associated type #12641

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 7 commits into from
Dec 8, 2017
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
1 change: 0 additions & 1 deletion benchmark/single-source/StaticArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ struct StaticArray<
func count() -> Int { return values.count() }

typealias Index = Int
typealias IndexDistance = Int
let startIndex: Int = 0
var endIndex: Int { return count()}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ public func check${inc.capitalize()}rementable<Instances, BaseCollection>(
_ instances: Instances,
of baseCollection: BaseCollection,
equalityOracle: (Instances.Index, Instances.Index) -> Bool,
${end}Index: Instances.Iterator.Element, ${TRACE}
${end}Index: Instances.Element, ${TRACE}
) where
Instances : Collection,
BaseCollection : ${protocol},
Instances.Iterator.Element == BaseCollection.Index {
Instances.Element == BaseCollection.Index {

checkEquatable(instances, oracle: equalityOracle, ${trace})
for i in instances {
Expand All @@ -82,20 +82,20 @@ internal func _checkIncrementalAdvance<Instances, BaseCollection>(
_ instances: Instances,
of baseCollection : BaseCollection,
equalityOracle: (Instances.Index, Instances.Index) -> Bool,
limit: Instances.Iterator.Element,
sign: BaseCollection.IndexDistance, // 1 or -1
next: (Instances.Iterator.Element) -> Instances.Iterator.Element,
limit: Instances.Element,
sign: Int, // 1 or -1
next: (Instances.Element) -> Instances.Element,
${TRACE}
) where
Instances : Collection,
BaseCollection : Collection,
Instances.Iterator.Element == BaseCollection.Index {
Instances.Element == BaseCollection.Index {
for i in instances {
let d: BaseCollection.IndexDistance = sign > 0 ?
let d: Int = sign > 0 ?
baseCollection.distance(from: i, to: limit) :
-baseCollection.distance(from: limit, to: i)

var offset: BaseCollection.IndexDistance = 0
var offset: Int = 0
for _ in 0...Int64(d * sign) {
let j = baseCollection.index(i, offsetBy: offset)
let k = baseCollection.index(i, offsetBy: offset + sign, limitedBy: limit) ?? limit
Expand All @@ -119,11 +119,11 @@ public func checkForwardIndex<Instances, BaseCollection>(
_ instances: Instances,
of baseCollection: BaseCollection,
equalityOracle: (Instances.Index, Instances.Index) -> Bool,
endIndex: Instances.Iterator.Element, ${TRACE}
endIndex: Instances.Element, ${TRACE}
) where
Instances : Collection,
BaseCollection : Collection,
Instances.Iterator.Element == BaseCollection.Index {
Instances.Element == BaseCollection.Index {

checkIncrementable(instances, of: baseCollection,
equalityOracle: equalityOracle, endIndex: endIndex, ${trace})
Expand All @@ -144,13 +144,13 @@ public func checkBidirectionalIndex<Instances, BaseCollection>(
_ instances: Instances,
of baseCollection: BaseCollection,
equalityOracle: (Instances.Index, Instances.Index) -> Bool,
startIndex: Instances.Iterator.Element,
endIndex: Instances.Iterator.Element,
startIndex: Instances.Element,
endIndex: Instances.Element,
${TRACE}
) where
Instances: Collection,
BaseCollection : BidirectionalCollection,
Instances.Iterator.Element == BaseCollection.Index {
Instances.Element == BaseCollection.Index {

checkForwardIndex(instances, of: baseCollection,
equalityOracle: equalityOracle, endIndex: endIndex)
Expand All @@ -176,18 +176,18 @@ public func checkRandomAccessIndex<Instances, Distances, BaseCollection>(
_ instances: Instances, distances: Distances,
of baseCollection: BaseCollection,
distanceOracle:
(Instances.Index, Instances.Index) -> Distances.Iterator.Element,
(Instances.Index, Instances.Index) -> Distances.Element,
advanceOracle:
(Instances.Index, Distances.Index) -> Instances.Iterator.Element,
(Instances.Index, Distances.Index) -> Instances.Element,
startIndex: Instances.Iterator.Element,
endIndex: Instances.Iterator.Element,
${TRACE}
) where
Instances : Collection,
Distances : Collection,
BaseCollection : RandomAccessCollection,
Instances.Iterator.Element == BaseCollection.Index,
Distances.Iterator.Element == BaseCollection.IndexDistance {
Instances.Element == BaseCollection.Index,
Distances.Element == Int {

checkBidirectionalIndex(instances, of: baseCollection,
equalityOracle: { distanceOracle($0, $1) == 0 },
Expand All @@ -207,16 +207,16 @@ public func checkAdvancesAndDistances<Instances, Distances, BaseCollection>(
_ instances: Instances, distances: Distances,
of baseCollection: BaseCollection,
distanceOracle:
(Instances.Index, Instances.Index) -> Distances.Iterator.Element,
(Instances.Index, Instances.Index) -> Distances.Element,
advanceOracle:
(Instances.Index, Distances.Index) -> Instances.Iterator.Element,
(Instances.Index, Distances.Index) -> Instances.Element,
${TRACE}
) where
Instances : Collection,
Distances : Collection,
BaseCollection : Collection,
Instances.Iterator.Element == BaseCollection.Index,
Distances.Iterator.Element == BaseCollection.IndexDistance {
Instances.Element == BaseCollection.Index,
Distances.Element == Int {

checkComparable(
instances,
Expand Down Expand Up @@ -246,7 +246,7 @@ public func checkAdvancesAndDistances<Instances, Distances, BaseCollection>(
// picked up when the caller passes a literal), and another that
// accepts any appropriate Collection type.
% for genericParam, Element, Expected in [
% ('Expected: Collection', 'Expected.Iterator.Element', 'Expected'),
% ('Expected: Collection', 'Expected.Element', 'Expected'),
% ('Element' , 'Element' , 'Array<Element>')]:

// Top-level check for Collection instances. Alias for checkForwardCollection.
Expand All @@ -257,7 +257,7 @@ public func checkCollection<${genericParam}, C : Collection>(
${TRACE},
resiliencyChecks: CollectionMisuseResiliencyChecks = .all,
sameValue: (${Element}, ${Element}) -> Bool
) where C.Iterator.Element == ${Element} {
) where C.Element == ${Element} {

checkForwardCollection(expected, collection, message(),
stackTrace: stackTrace, showFrame: showFrame, file: file, line: line,
Expand All @@ -276,7 +276,7 @@ public func check${Traversal}Collection<
${TRACE},
resiliencyChecks: CollectionMisuseResiliencyChecks = .all
) where
C.Iterator.Element == ${Element},
C.Element == ${Element},
${Element} : Equatable {

check${Traversal}Collection(
Expand All @@ -296,7 +296,7 @@ public func check${Traversal}Collection<
resiliencyChecks: CollectionMisuseResiliencyChecks = .all,
sameValue: (${Element}, ${Element}) -> Bool
) where
C.Iterator.Element == ${Element} {
C.Element == ${Element} {

checkOneLevelOf${Traversal}Collection(expected, collection, ${trace},
resiliencyChecks: resiliencyChecks, sameValue: sameValue)
Expand Down Expand Up @@ -324,7 +324,7 @@ public func checkOneLevelOf${Traversal}Collection<
${TRACE},
resiliencyChecks: CollectionMisuseResiliencyChecks = .all,
sameValue: (${Element}, ${Element}) -> Bool
) where C.Iterator.Element == ${Element} {
) where C.Element == ${Element} {

// A `Collection` is a multi-pass `Sequence`.
for _ in 0..<3 {
Expand Down Expand Up @@ -370,7 +370,7 @@ public func checkOneLevelOf${Traversal}Collection<

% else:
% assert(Traversal == 'RandomAccess')
typealias Distance = C.IndexDistance
typealias Distance = Int

let count: Distance = collection.count
let offset0 = min(5, count)
Expand Down Expand Up @@ -501,7 +501,7 @@ ${genericParam}, S : ${TraversalCollection}
resiliencyChecks: CollectionMisuseResiliencyChecks = .all,
sameValue: (${Element}, ${Element}) -> Bool
) where
S.Iterator.Element == ${Element} {
S.Element == ${Element} {

let expectedArray = Array(expected)

Expand Down Expand Up @@ -564,14 +564,14 @@ public func checkRangeReplaceable<C, N>(
) where
C : RangeReplaceableCollection,
N : Collection,
C.Iterator.Element : Equatable,
C.Iterator.Element == N.Iterator.Element {
C.Element : Equatable,
C.Element == N.Element {

typealias A = C

// First make an independent copy of the array that we can use for
// comparison later.
let source = Array<A.Iterator.Element>(makeCollection())
let source = Array<A.Element>(makeCollection())

for (ix, i) in source.indices.enumerated() {
for (jx_, j) in (i..<source.endIndex).enumerated() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1198,10 +1198,10 @@ extension TestSuite {

// FIXME: swift-3-indexing-model -
// enhance the following for negative direction?
// advance(i: Index, by n: IndexDistance) -> Index
// advance(i: Index, by n: Int) -> Index
// advance(
// i: Index, by n: IndexDistance, limitedBy: Index) -> Index
// distance(from start: Index, to end: Index) -> IndexDistance
// i: Index, by n: Int, limitedBy: Index) -> Index
// distance(from start: Index, to end: Index) -> Int

//===------------------------------------------------------------------===//
// last
Expand Down
27 changes: 15 additions & 12 deletions stdlib/private/StdlibCollectionUnittest/LoggingWrappers.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -414,9 +414,7 @@ public struct ${Self}<
return base.isEmpty
}

public typealias IndexDistance = Base.IndexDistance

public var count: IndexDistance {
public var count: Int {
Log.count[selfType] += 1
return base.count
}
Expand All @@ -433,19 +431,19 @@ public struct ${Self}<
return base.first
}

public func index(_ i: Index, offsetBy n: IndexDistance) -> Index {
public func index(_ i: Index, offsetBy n: Int) -> Index {
Log.advance[selfType] += 1
return base.index(i, offsetBy: n)
}

public func index(
_ i: Index, offsetBy n: IndexDistance, limitedBy limit: Index
_ i: Index, offsetBy n: Int, limitedBy limit: Index
) -> Index? {
Log.advanceLimit[selfType] += 1
return base.index(i, offsetBy: n, limitedBy: limit)
}

public func distance(from start: Index, to end: Index) -> IndexDistance {
public func distance(from start: Index, to end: Index) -> Int {
Log.distance[selfType] += 1
return base.distance(from: start, to: end)
}
Expand Down Expand Up @@ -558,7 +556,7 @@ public struct ${Self}<
base.replaceSubrange(bounds, with: newElements)
}

public mutating func reserveCapacity(_ n: IndexDistance) {
public mutating func reserveCapacity(_ n: Int) {
Log.reserveCapacity[selfType] += 1
base.reserveCapacity(n)
}
Expand Down Expand Up @@ -601,8 +599,8 @@ public struct ${Self}<
public typealias Log = MutableCollectionLog

public typealias SubSequence = Base.SubSequence

public typealias Iterator = Base.Iterator
public typealias Element = Base.Element

public init(wrapping base: Base) {
self.base = base
Expand All @@ -613,6 +611,7 @@ public struct ${Self}<
}

public typealias Index = Base.Index
public typealias Indices = Base.Indices

public var startIndex: Index {
return base.startIndex
Expand All @@ -621,8 +620,12 @@ public struct ${Self}<
public var endIndex: Index {
return base.endIndex
}

public subscript(position: Index) -> Base.Iterator.Element {

public var indices: Indices {
return base.indices
}

public subscript(position: Index) -> Element {
get {
return base[position]
}
Expand Down Expand Up @@ -650,11 +653,11 @@ public struct ${Self}<
}
% end

public func index(_ i: Index, offsetBy n: Base.IndexDistance) -> Index {
public func index(_ i: Index, offsetBy n: Int) -> Index {
return base.index(i, offsetBy: n)
}

public func distance(from start: Index, to end: Index) -> Base.IndexDistance {
public func distance(from start: Index, to end: Index) -> Int {
return base.distance(from: start, to: end)
}

Expand Down
Loading