Skip to content

Commit c48d6aa

Browse files
committed
stdlib: change underestimateCount() into a method
rdar://19895265 Swift SVN r27346
1 parent c7c25ad commit c48d6aa

File tree

10 files changed

+222
-107
lines changed

10 files changed

+222
-107
lines changed

stdlib/private/StdlibUnittest/StdlibUnittest.swift.gyb

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2187,17 +2187,15 @@ public struct MinimalSequence<T> : SequenceType {
21872187
return MinimalGenerator(_sharedState)
21882188
}
21892189

2190+
public func _prext_underestimateCount() -> Int {
2191+
return underestimatedCount
2192+
}
2193+
21902194
public var underestimatedCount: Int
21912195

21922196
internal let _sharedState: _MinimalGeneratorSharedState<T>
21932197
}
21942198

2195-
public func ~> <T> (
2196-
s: MinimalSequence<T>, _: (_UnderestimateCount, ())
2197-
) -> Int {
2198-
return s.underestimatedCount
2199-
}
2200-
22012199
//===----------------------------------------------------------------------===//
22022200
// MinimalForwardIndex
22032201
//===----------------------------------------------------------------------===//
@@ -2359,17 +2357,15 @@ public struct ${Self}<T> : CollectionType {
23592357
return _elements[i.position]
23602358
}
23612359

2360+
public func _prext_underestimateCount() -> Int {
2361+
return underestimatedCount
2362+
}
2363+
23622364
public var underestimatedCount: Int
23632365

23642366
internal let _elements: [T]
23652367
}
23662368

2367-
public func ~> <T> (
2368-
c: ${Self}<T>, _: (_UnderestimateCount, ())
2369-
) -> Int {
2370-
return c.underestimatedCount
2371-
}
2372-
23732369
%end
23742370

23752371
//===----------------------------------------------------------------------===//
@@ -2411,6 +2407,10 @@ public struct ${Self}<T> : RangeReplaceableCollectionType {
24112407
return MinimalGenerator(elements)
24122408
}
24132409

2410+
public func _prext_underestimateCount() -> Int {
2411+
return underestimatedCount
2412+
}
2413+
24142414
public var startIndex: ${Index} {
24152415
return ${Index}(
24162416
position: 0,
@@ -2485,12 +2485,6 @@ public struct ${Self}<T> : RangeReplaceableCollectionType {
24852485
public var elements: [T]
24862486
}
24872487

2488-
public func ~> <T> (
2489-
c: ${Self}<T>, _: (_UnderestimateCount, ())
2490-
) -> Int {
2491-
return c.underestimatedCount
2492-
}
2493-
24942488
%end
24952489

24962490
// ${'Local Variables'}:

stdlib/public/core/Collection.swift

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,17 @@ public protocol _CollectionType : _SequenceType {
6666
subscript(_i: Index) -> _Element {get}
6767
}
6868

69-
public protocol _CollectionDefaultsType : _CollectionType {
70-
}
69+
public protocol _CollectionDefaultsType
70+
: _CollectionType, _SequenceElementInvariantDefaultsType {}
7171

7272
extension _CollectionDefaultsType {
73+
/// Return a value less than or equal to the number of elements in
74+
/// `self`, **nondestructively**.
75+
///
76+
/// Complexity: O(N)
77+
final public func _prext_underestimateCount() -> Int {
78+
return numericCast(count(self))
79+
}
7380
}
7481

7582
/// A multi-pass *sequence* with addressable positions.
@@ -99,12 +106,6 @@ public protocol CollectionType
99106
func ~> (_:Self, _:(_Count, ())) -> Index.Distance
100107
}
101108

102-
// Default implementation of underestimateCount for *collections*. Do not
103-
// use this operator directly; call `underestimateCount(s)` instead
104-
public func ~> <T : _CollectionType>(x:T,_:(_UnderestimateCount,())) -> Int {
105-
return numericCast(x~>_count())
106-
}
107-
108109
// A fast implementation for when you are backed by a contiguous array.
109110
public func ~> <T : protocol<_Sequence_Type, _ArrayType>>(
110111
source: T, ptr: (_InitializeTo, UnsafeMutablePointer<T.Generator.Element>)) {

stdlib/public/core/CompilerProtocols.swift

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,9 @@ public protocol GeneratorType {
6161
public protocol _SequenceType {
6262
}
6363

64-
/// This protocol is an implementation detail of `SequenceType`; do
65-
/// not use it directly.
66-
///
67-
/// Its requirements are inherited by `SequenceType` and thus must
68-
/// be satisfied by types conforming to that protocol.
69-
public protocol _Sequence_Type : _SequenceType, _SequenceDefaultsType {
70-
/// A type whose instances can produce the elements of this
71-
/// sequence, in order.
64+
public protocol _SequenceDefaultsType {
65+
/// A type that provides the *sequence*\ 's iteration interface and
66+
/// encapsulates its iteration state.
7267
typealias Generator : GeneratorType
7368

7469
/// Return a *generator* over the elements of this *sequence*. The
@@ -79,9 +74,34 @@ public protocol _Sequence_Type : _SequenceType, _SequenceDefaultsType {
7974
func generate() -> Generator
8075
}
8176

82-
public protocol _SequenceDefaultsType {
83-
/// A type that provides the *sequence*\ 's iteration interface and
84-
/// encapsulates its iteration state.
77+
extension _SequenceDefaultsType {
78+
}
79+
80+
public protocol _SequenceElementInvariantDefaultsType {
81+
// This protocol is an anchor point for default implementations for
82+
// SequenceType that don't depend on sequence contents.
83+
}
84+
85+
extension _SequenceElementInvariantDefaultsType {
86+
/// Return a value less than or equal to the number of elements in
87+
/// `self`, **nondestructively**.
88+
///
89+
/// Complexity: O(N)
90+
final public func _prext_underestimateCount() -> Int {
91+
return 0
92+
}
93+
}
94+
95+
/// This protocol is an implementation detail of `SequenceType`; do
96+
/// not use it directly.
97+
///
98+
/// Its requirements are inherited by `SequenceType` and thus must
99+
/// be satisfied by types conforming to that protocol.
100+
public protocol _Sequence_Type
101+
: _SequenceType, _SequenceDefaultsType, _SequenceElementInvariantDefaultsType {
102+
103+
/// A type whose instances can produce the elements of this
104+
/// sequence, in order.
85105
typealias Generator : GeneratorType
86106

87107
/// Return a *generator* over the elements of this *sequence*. The
@@ -90,12 +110,12 @@ public protocol _SequenceDefaultsType {
90110
///
91111
/// Complexity: O(1)
92112
func generate() -> Generator
93-
}
94-
95-
extension _SequenceDefaultsType {
96-
}
97113

98-
extension _SequenceDefaultsType {
114+
/// Return a value less than or equal to the number of elements in
115+
/// `self`, **nondestructively**.
116+
///
117+
/// Complexity: O(N)
118+
func _prext_underestimateCount() -> Int
99119
}
100120

101121
/// A type that can be iterated with a `for`\ ...\ `in` loop.
@@ -114,12 +134,6 @@ public protocol SequenceType : _Sequence_Type {
114134
/// Complexity: O(1)
115135
func generate() -> Generator
116136

117-
/// Return a value less than or equal to the number of elements in
118-
/// self, **nondestructively**.
119-
///
120-
/// Complexity: O(N)
121-
func ~> (_:Self,_:(_UnderestimateCount,())) -> Int
122-
123137
/// If `self` is multi-pass (i.e., a `CollectionType`), invoke the function
124138
/// on `self` and return its result. Otherwise, return `nil`.
125139
func ~> <R>(_: Self, _: (_PreprocessingPass, ((Self)->R))) -> R?
@@ -141,26 +155,12 @@ public func _copyToNativeArrayBuffer<Args>(args: Args)
141155
return (_CopyToNativeArrayBuffer(), args)
142156
}
143157

144-
// Operation tags for underestimateCount. See Index.swift for an
145-
// explanation of operation tags.
146-
public struct _UnderestimateCount {}
147-
internal func _underestimateCount<Args>(args: Args)
148-
-> (_UnderestimateCount, Args)
149-
{
150-
return (_UnderestimateCount(), args)
151-
}
152-
153-
// Default implementation of underestimateCount for Sequences. Do not
154-
// use this operator directly; call underestimateCount(s) instead
155-
public func ~> <T : _SequenceType>(s: T,_:(_UnderestimateCount, ())) -> Int {
156-
return 0
157-
}
158-
159158
/// Return an underestimate of the number of elements in the given
160159
/// sequence, without consuming the sequence. For Sequences that are
161160
/// actually Collections, this will return count(x)
162161
public func underestimateCount<T : SequenceType>(x: T) -> Int {
163-
return x~>_underestimateCount()
162+
// FIXME(prext): remove this function when protocol extensions land.
163+
return x._prext_underestimateCount()
164164
}
165165

166166
public struct _InitializeTo {}

stdlib/public/core/ContiguousArrayBuffer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ public func ~> <
533533
source: S, _: (_CopyToNativeArrayBuffer,())
534534
) -> _ContiguousArrayBuffer<S.Generator.Element>
535535
{
536-
let initialCapacity = source~>_underestimateCount()
536+
let initialCapacity = source._prext_underestimateCount()
537537
var result = _ContiguousArrayBuffer<S.Generator.Element>(
538538
count: 0, minimumCapacity: initialCapacity)
539539

stdlib/public/core/ExistentialCollection.swift.gyb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,10 @@ public struct AnySequence<T> : SequenceType {
214214
}
215215

216216
% for Kind in ['Sequence'] + [t + 'Collection' for t in traversals]:
217-
public func ~> <Element>(
218-
source: Any${Kind}<Element>, _: (_UnderestimateCount, ())
219-
) -> Int {
220-
return source._box._underestimateCount()
217+
extension Any${Kind} {
218+
public func _prext_underestimateCount() -> Int {
219+
return _box._underestimateCount()
220+
}
221221
}
222222

223223
public func ~> <Element>(

stdlib/public/core/LazyCollection.swift.gyb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,11 @@ public struct ${Self}<S : CollectionType ${whereClause}> : CollectionType {
8080
return Array(_base)
8181
}
8282

83-
var _base: S
84-
}
83+
public func _prext_underestimateCount() -> Int {
84+
return underestimateCount(_base)
85+
}
8586

86-
public func ~> <S : CollectionType ${whereClause}> (
87-
s: ${Self}<S>, _: (_UnderestimateCount, ())
88-
) -> Int {
89-
return underestimateCount(s._base)
87+
var _base: S
9088
}
9189

9290
/// Augment `s` with lazy methods such as `map`, `filter`, etc.

stdlib/public/core/LazySequence.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,11 @@ public struct LazySequence<S : SequenceType> : SequenceType {
3232
return Array(_base)
3333
}
3434

35-
var _base: S
36-
}
35+
public func _prext_underestimateCount() -> Int {
36+
return underestimateCount(_base)
37+
}
3738

38-
public func ~> <S : SequenceType> (
39-
s: LazySequence<S>, _: (_UnderestimateCount, ())
40-
) -> Int {
41-
return underestimateCount(s._base)
39+
var _base: S
4240
}
4341

4442
/// Augment `s` with lazy methods such as `map`, `filter`, etc.

stdlib/public/core/Map.swift.gyb

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,14 @@ public struct MapSequenceView<Base : SequenceType, T> : SequenceType {
5555
_base: _base.generate(), _transform: _transform)
5656
}
5757

58+
public func _prext_underestimateCount() -> Int {
59+
return underestimateCount(_base)
60+
}
61+
5862
var _base: Base
5963
var _transform: (Base.Generator.Element)->T
6064
}
6165

62-
public func ~> <Base : SequenceType, T> (
63-
s: MapSequenceView<Base, T>, _: (_UnderestimateCount, ())
64-
) -> Int {
65-
return underestimateCount(s._base)
66-
}
67-
6866
/// Return an `Array` containing the results of mapping `transform`
6967
/// over `source`.
7068
public func map<S : SequenceType, T>(
@@ -121,16 +119,14 @@ public struct MapCollectionView<Base : CollectionType, T> : CollectionType {
121119
return MapSequenceGenerator(_base: _base.generate(), _transform: _transform)
122120
}
123121

122+
public func _prext_underestimateCount() -> Int {
123+
return underestimateCount(_base)
124+
}
125+
124126
var _base: Base
125127
var _transform: (Base.Generator.Element)->T
126128
}
127129

128-
public func ~> <Base: CollectionType, T> (
129-
s: MapCollectionView<Base, T>, _: (_UnderestimateCount, ())
130-
) -> Int {
131-
return underestimateCount(s._base)
132-
}
133-
134130
/// Return an `Array` containing the results of mapping `transform`
135131
/// over `source`.
136132
public func map<C : CollectionType, T>(

stdlib/public/core/StringCore.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ extension _StringCore : ExtensibleCollectionType {
598598
}
599599
}
600600

601-
let growth = s~>_underestimateCount()
601+
let growth = s._prext_underestimateCount()
602602
var g = s.generate()
603603

604604
if _fastPath(growth > 0) {

0 commit comments

Comments
 (0)