File tree Expand file tree Collapse file tree 6 files changed +86
-0
lines changed Expand file tree Collapse file tree 6 files changed +86
-0
lines changed Original file line number Diff line number Diff line change @@ -85,6 +85,17 @@ extension ClosedRangeIndex : Comparable {
85
85
}
86
86
}
87
87
88
+ extension ClosedRangeIndex : Hashable where Bound : Hashable {
89
+ public var hashValue : Int {
90
+ switch _value {
91
+ case . inRange( let value) :
92
+ return value. hashValue
93
+ case . pastEnd:
94
+ return . max
95
+ }
96
+ }
97
+ }
98
+
88
99
/// A closed range that forms a collection of consecutive values.
89
100
///
90
101
/// You create a `CountableClosedRange` instance by using the closed range
Original file line number Diff line number Diff line change @@ -140,6 +140,12 @@ public struct LazyDropWhileIndex<Base : Collection> : Comparable {
140
140
}
141
141
}
142
142
143
+ extension LazyDropWhileIndex : Hashable where Base. Index : Hashable {
144
+ public var hashValue : Int {
145
+ return base. hashValue
146
+ }
147
+ }
148
+
143
149
% for Traversal in [ 'Forward', 'Bidirectional'] :
144
150
% Collection = collectionForTraversal ( Traversal)
145
151
% Self = " LazyDropWhile " + Collection
Original file line number Diff line number Diff line change @@ -220,6 +220,14 @@ extension ${Index} : Comparable {
220
220
}
221
221
}
222
222
223
+ extension ${ Index} : Hashable
224
+ where BaseElements. Index : Hashable, BaseElements. Element. Index : Hashable
225
+ {
226
+ public var hashValue: Int {
227
+ return _mixInt ( _inner? . hashValue ?? 0 ) ^ _outer. hashValue
228
+ }
229
+ }
230
+
223
231
/// A flattened view of a base collection of collections.
224
232
///
225
233
/// The elements of this view are a concatenation of the elements of
Original file line number Diff line number Diff line change @@ -161,6 +161,17 @@ public struct LazyPrefixWhileIndex<Base : Collection> : Comparable {
161
161
}
162
162
}
163
163
164
+ extension LazyPrefixWhileIndex : Hashable where Base. Index : Hashable {
165
+ public var hashValue : Int {
166
+ switch _value {
167
+ case . index( let value) :
168
+ return value. hashValue
169
+ case . pastEnd:
170
+ return . max
171
+ }
172
+ }
173
+ }
174
+
164
175
% for Traversal in [ 'Forward', 'Bidirectional'] :
165
176
% Collection = collectionForTraversal ( Traversal)
166
177
% Self = " LazyPrefixWhile " + Collection
Original file line number Diff line number Diff line change @@ -149,6 +149,12 @@ public struct ReversedIndex<Base : Collection> : Comparable {
149
149
}
150
150
}
151
151
152
+ extension ReversedIndex : Hashable where Base. Index : Hashable {
153
+ public var hashValue : Int {
154
+ return base. hashValue
155
+ }
156
+ }
157
+
152
158
/// A collection that presents the elements of its base collection
153
159
/// in reverse order.
154
160
///
@@ -349,6 +355,12 @@ public struct ReversedRandomAccessIndex<
349
355
}
350
356
}
351
357
358
+ extension ReversedRandomAccessIndex : Hashable where Base. Index : Hashable {
359
+ public var hashValue : Int {
360
+ return base. hashValue
361
+ }
362
+ }
363
+
352
364
/// A collection that presents the elements of its base collection
353
365
/// in reverse order.
354
366
///
Original file line number Diff line number Diff line change
1
+ // RUN: %target-run-simple-swift
2
+ // REQUIRES: executable_test
3
+
4
+ import StdlibUnittest
5
+
6
+ var HashTests = TestSuite ( " HashableIndices " )
7
+
8
+ HashTests . test ( " ClosedRangeIndex " ) {
9
+ let a = 1 ... 10
10
+ checkHashable ( a. indices, equalityOracle: { $0 == $1 } )
11
+ }
12
+
13
+ HashTests . test ( " FlattenIndex " ) {
14
+ let a = [ 1 ... 10 , 11 ... 20 , 21 ... 30 ] . joined ( )
15
+ checkHashable ( a. indices, equalityOracle: { $0 == $1 } )
16
+ }
17
+
18
+ HashTests . test ( " LazyDropWhileIndex " ) {
19
+ let a = ( 1 ... 10 ) . lazy. drop ( while: { $0 < 5 } )
20
+ checkHashable ( a. indices, equalityOracle: { $0 == $1 } )
21
+ }
22
+
23
+ HashTests . test ( " LazyPrefixWhileIndex " ) {
24
+ let a = ( 1 ... 10 ) . lazy. prefix ( while: { $0 < 5 } )
25
+ checkHashable ( a. indices, equalityOracle: { $0 == $1 } )
26
+ }
27
+
28
+ HashTests . test ( " ReversedIndex " ) {
29
+ let a = ( 1 ... 10 ) . lazy. filter ( { $0 > 3 } ) . reversed ( )
30
+ checkHashable ( a. indices, equalityOracle: { $0 == $1 } )
31
+ }
32
+
33
+ HashTests . test ( " ReversedRandomAccessIndex " ) {
34
+ let a = ( 1 ... 10 ) . reversed ( )
35
+ checkHashable ( a. indices, equalityOracle: { $0 == $1 } )
36
+ }
37
+
38
+ runAllTests ( )
You can’t perform that action at this time.
0 commit comments