Skip to content

Commit 0d68b3a

Browse files
author
Dave Abrahams
committed
[stdlib] Generate RandomAccessCollection defaults
The defaults we were generating for Collection and BidirectionalCollection didn't make any sense, because if you could do that strideable arithmetic then you essentially had random access. Instead we constrain the defaults to apply to RandomAccessCollection where the Indices are a CountableRange.
1 parent c594b1d commit 0d68b3a

22 files changed

+148
-171
lines changed

stdlib/public/SDK/CoreAudio/CoreAudio.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,5 @@ extension UnsafeMutableAudioBufferListPointer
166166
}
167167

168168
public typealias Indices = CountableRange<Int>
169-
170-
public var indices: Indices {
171-
return startIndex..<endIndex
172-
}
173169
}
174170

stdlib/public/core/ArrayBuffer.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -475,10 +475,6 @@ extension _ArrayBuffer {
475475

476476
public typealias Indices = CountableRange<Int>
477477

478-
public var indices: CountableRange<Int> {
479-
return startIndex..<endIndex
480-
}
481-
482478
//===--- private --------------------------------------------------------===//
483479
typealias Storage = _ContiguousArrayStorage<Element>
484480
public typealias NativeBuffer = _ContiguousArrayBuffer<Element>

stdlib/public/core/ArrayBufferProtocol.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212

1313
/// The underlying buffer for an ArrayType conforms to
1414
/// `_ArrayBufferProtocol`. This buffer does not provide value semantics.
15-
public protocol _ArrayBufferProtocol : MutableCollection {
15+
public protocol _ArrayBufferProtocol
16+
: MutableCollection, RandomAccessCollection {
17+
1618
/// The type of elements stored in the buffer.
1719
associatedtype Element
1820

@@ -124,6 +126,8 @@ public protocol _ArrayBufferProtocol : MutableCollection {
124126
}
125127

126128
extension _ArrayBufferProtocol where Index == Int {
129+
public typealias Indices = CountableRange<Int>
130+
127131
public var subscriptBaseAddress: UnsafeMutablePointer<Element> {
128132
return firstElementAddress
129133
}

stdlib/public/core/Arrays.swift.gyb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -455,8 +455,8 @@ ${SelfDocComment}
455455
%end
456456
public struct ${Self}<Element>
457457
: RandomAccessCollection,
458-
MutableCollection,
459-
_DestructorSafeContainer
458+
MutableCollection,
459+
_DestructorSafeContainer
460460
{
461461

462462
public typealias Index = Int
@@ -505,10 +505,6 @@ public struct ${Self}<Element>
505505

506506
public typealias Indices = CountableRange<Int>
507507

508-
public var indices: CountableRange<Int> {
509-
return startIndex..<endIndex
510-
}
511-
512508
%{
513509
SubscriptDocComment="""\
514510
/// Accesses the element at the specified position.

stdlib/public/core/BidirectionalCollection.swift

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -136,47 +136,6 @@ extension BidirectionalIndexable {
136136
}
137137
}
138138

139-
/// Supply optimized defaults for `BidirectionalCollection` models that use
140-
/// some model of `Strideable` as their `Index`.
141-
extension BidirectionalIndexable
142-
where
143-
Index : Strideable,
144-
Index.Stride == IndexDistance,
145-
Index.Stride : SignedInteger {
146-
147-
@warn_unused_result
148-
public func index(before i: Index) -> Index {
149-
// FIXME: swift-3-indexing-model: range check i: should allow `endIndex`.
150-
//_failEarlyRangeCheck(i, bounds: startIndex..<endIndex)
151-
152-
return i.advanced(by: -1)
153-
}
154-
155-
@warn_unused_result
156-
public func index(_ i: Index, offsetBy n: IndexDistance) -> Index {
157-
// FIXME: swift-3-indexing-model: range check i
158-
return i.advanced(by: n)
159-
}
160-
161-
@warn_unused_result
162-
public func index(
163-
_ i: Index, offsetBy n: IndexDistance, limitedBy limit: Index
164-
) -> Index? {
165-
// FIXME: swift-3-indexing-model: range check i
166-
let l = i.distance(to: limit)
167-
if n > 0 ? l >= 0 && l < n : l <= 0 && l > n {
168-
return nil
169-
}
170-
return index(i, offsetBy: n)
171-
}
172-
173-
@warn_unused_result
174-
public func distance(from start: Index, to end: Index) -> IndexDistance {
175-
// FIXME: swift-3-indexing-model: range check supplies start and end?
176-
return start.distance(to: end)
177-
}
178-
}
179-
180139
/// Supply the default "slicing" `subscript` for `BidirectionalCollection`
181140
/// models that accept the default associated `SubSequence`,
182141
/// `BidirectionalSlice<Self>`.

stdlib/public/core/Character.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ public struct Character :
137137
}
138138

139139
internal struct _SmallUTF8 : RandomAccessCollection {
140+
typealias Indices = CountableRange<Int>
141+
140142
var indices: CountableRange<Int> {
141143
return startIndex..<endIndex
142144
}
@@ -203,9 +205,7 @@ public struct Character :
203205
}
204206

205207
struct _SmallUTF16 : RandomAccessCollection {
206-
var indices: CountableRange<Int> {
207-
return startIndex..<endIndex
208-
}
208+
typealias Indices = CountableRange<Int>
209209

210210
init(_ u8: UInt64) {
211211
let count = UTF16.transcodedLength(

stdlib/public/core/CocoaArray.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ import SwiftShims
2626
/// `_NSArrayCore` conform directly? It's a class, and I
2727
/// don't want to pay for the dynamic dispatch overhead.
2828
internal struct _CocoaArrayWrapper : RandomAccessCollection {
29-
var indices: CountableRange<Int> {
30-
return startIndex..<endIndex
31-
}
29+
typealias Indices = CountableRange<Int>
3230

3331
var startIndex: Int {
3432
return 0

stdlib/public/core/Collection.swift

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -857,52 +857,6 @@ extension Indexable {
857857
}
858858
}
859859

860-
/// Supply optimized defaults for `Collection` models that use some model
861-
/// of `Strideable` as their `Index`.
862-
extension Indexable
863-
where
864-
Index : Strideable,
865-
Index.Stride == IndexDistance,
866-
Index.Stride : SignedInteger {
867-
868-
@warn_unused_result
869-
public func index(after i: Index) -> Index {
870-
// FIXME: swift-3-indexing-model: tests.
871-
_failEarlyRangeCheck(i, bounds: startIndex..<endIndex)
872-
873-
return i.advanced(by: 1)
874-
}
875-
876-
@warn_unused_result
877-
public func index(_ i: Index, offsetBy n: IndexDistance) -> Index {
878-
_precondition(n >= 0,
879-
"Only BidirectionalCollections can be advanced by a negative amount")
880-
// FIXME: swift-3-indexing-model: range check i
881-
return i.advanced(by: n)
882-
}
883-
884-
@warn_unused_result
885-
public func index(
886-
_ i: Index, offsetBy n: IndexDistance, limitedBy limit: Index
887-
) -> Index? {
888-
_precondition(n >= 0,
889-
"Only BidirectionalCollections can be advanced by a negative amount")
890-
let l = i.distance(to: limit)
891-
if l >= 0 && l < n {
892-
return nil
893-
}
894-
return index(i, offsetBy: n)
895-
}
896-
897-
@warn_unused_result
898-
public func distance(from start: Index, to end: Index) -> IndexDistance {
899-
_precondition(start <= end,
900-
"Only BidirectionalCollections can have end come before start")
901-
// FIXME: swift-3-indexing-model: range check supplied start and end?
902-
return start.distance(to: end)
903-
}
904-
}
905-
906860
/// Supply the default `makeIterator()` method for `Collection` models
907861
/// that accept the default associated `Iterator`,
908862
/// `IndexingIterator<Self>`.

stdlib/public/core/CollectionOfOne.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ public struct IteratorOverOne<Element> : IteratorProtocol, Sequence {
3737
/// A collection containing a single element of type `Element`.
3838
public struct CollectionOfOne<Element> : RandomAccessCollection {
3939

40+
// FIXME: swift-3-indexing-model - compiler bug prevents this
41+
// typealias from being sufficient. Instead we need to define the
42+
// var below.
43+
//
44+
// public typealias indices = CountableRange<Int>
4045
public var indices: CountableRange<Int> {
4146
return startIndex..<endIndex
4247
}

stdlib/public/core/ContiguousArrayBuffer.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -549,10 +549,6 @@ extension _ContiguousArrayBuffer : RandomAccessCollection {
549549
}
550550

551551
public typealias Indices = CountableRange<Int>
552-
553-
public var indices: CountableRange<Int> {
554-
return startIndex..<endIndex
555-
}
556552
}
557553

558554
extension Sequence {

0 commit comments

Comments
 (0)