Skip to content

Commit 8d80999

Browse files
author
Dave Abrahams
committed
[stdlib] Use CountableRange for Indices more
This will be much more efficient than the default, when it applies, because it doesn't need to carry a reference to underlying collection storage.
1 parent 2ea3db0 commit 8d80999

File tree

7 files changed

+41
-0
lines changed

7 files changed

+41
-0
lines changed

stdlib/public/core/Character.swift

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

139139
internal struct _SmallUTF8 : RandomAccessCollection {
140+
var indices: CountableRange<Int> {
141+
return startIndex..<endIndex
142+
}
143+
140144
init(_ u8: UInt64) {
141145
let utf8Count = Character._smallSize(u8)
142146
_sanityCheck(utf8Count <= 8, "Character with more than 8 UTF-8 code units")
@@ -199,6 +203,10 @@ public struct Character :
199203
}
200204

201205
struct _SmallUTF16 : RandomAccessCollection {
206+
var indices: CountableRange<Int> {
207+
return startIndex..<endIndex
208+
}
209+
202210
init(_ u8: UInt64) {
203211
let count = UTF16.transcodedLength(
204212
of: _SmallUTF8(u8).makeIterator(),

stdlib/public/core/CocoaArray.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ 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+
public var indices: CountableRange<Int> {
30+
return startIndex..<endIndex
31+
}
32+
2933
var startIndex: Int {
3034
return 0
3135
}

stdlib/public/core/CollectionOfOne.swift

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

40+
public var indices: CountableRange<Int> {
41+
return startIndex..<endIndex
42+
}
43+
4044
/// Construct an instance containing just `element`.
4145
public init(_ element: Element) {
4246
self._element = element

stdlib/public/core/Mirror.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,11 @@ internal extension Mirror {
526526
/// mirrors to use the new style, which only present forward
527527
/// traversal in general.
528528
internal struct LegacyChildren : RandomAccessCollection {
529+
530+
public var indices: CountableRange<Int> {
531+
return startIndex..<endIndex
532+
}
533+
529534
init(_ oldMirror: _Mirror) {
530535
self._oldMirror = oldMirror
531536
}
@@ -754,6 +759,11 @@ public struct DictionaryLiteral<Key, Value> : DictionaryLiteralConvertible {
754759
/// `Collection` conformance that allows `DictionaryLiteral` to
755760
/// interoperate with the rest of the standard library.
756761
extension DictionaryLiteral : RandomAccessCollection {
762+
763+
public var indices: CountableRange<Int> {
764+
return startIndex..<endIndex
765+
}
766+
757767
/// The position of the first element in a non-empty `DictionaryLiteral`.
758768
///
759769
/// Identical to `endIndex` in an empty `DictionaryLiteral`.

stdlib/public/core/Repeat.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212

1313
/// A collection whose elements are all identical `Element`s.
1414
public struct Repeated<Element> : RandomAccessCollection {
15+
16+
public var indices: CountableRange<Int> {
17+
return startIndex..<endIndex
18+
}
19+
1520
/// A type that represents a valid position in the collection.
1621
///
1722
/// Valid indices consist of the position of every element and a

stdlib/public/core/StringCore.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,11 @@ public struct _StringCore {
572572
}
573573

574574
extension _StringCore : RandomAccessCollection {
575+
576+
public var indices: CountableRange<Int> {
577+
return startIndex..<endIndex
578+
}
579+
575580
public // @testable
576581
var startIndex: Int {
577582
return 0

stdlib/public/core/UnicodeScalar.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,11 @@ extension UnicodeScalar {
239239
}
240240

241241
extension UnicodeScalar.UTF16View : RandomAccessCollection {
242+
243+
public var indices: CountableRange<Int> {
244+
return startIndex..<endIndex
245+
}
246+
242247
/// The position of the first code unit.
243248
var startIndex: Int {
244249
return 0

0 commit comments

Comments
 (0)