9
9
//
10
10
//===----------------------------------------------------------------------===//
11
11
12
- extension LazyCollection {
12
+ /// A namespace for methods which overlay a collection of elements
13
+ /// over a region of a base collection.
14
+ ///
15
+ /// Access the namespace via the `.overlay` member, available on all collections:
16
+ ///
17
+ /// ```swift
18
+ /// let base = 0..<5
19
+ /// for n in base.overlay.inserting(42, at: 2) {
20
+ /// print(n)
21
+ /// }
22
+ /// // Prints: 0, 1, 42, 2, 3, 4
23
+ /// ```
24
+ ///
25
+ public struct OverlayCollectionNamespace < Elements: Collection > {
26
+
27
+ @usableFromInline
28
+ internal var elements : Elements
29
+
30
+ @inlinable
31
+ internal init ( elements: Elements ) {
32
+ self . elements = elements
33
+ }
34
+ }
35
+
36
+ extension Collection {
37
+
38
+ /// A namespace for methods which overlay another collection of elements
39
+ /// over a region of this collection.
40
+ ///
41
+ @inlinable
42
+ public var overlay : OverlayCollectionNamespace < Self > {
43
+ OverlayCollectionNamespace ( elements: self )
44
+ }
45
+ }
46
+
47
+ extension OverlayCollectionNamespace {
48
+
49
+ @inlinable
50
+ public func replacingSubrange< Overlay> (
51
+ _ subrange: Range < Elements . Index > , with newElements: Overlay
52
+ ) -> OverlayCollection < Elements , Overlay > {
53
+ OverlayCollection ( base: elements, overlay: newElements, replacedRange: subrange)
54
+ }
55
+
56
+ @inlinable
57
+ public func appending< Overlay> (
58
+ contentsOf newElements: Overlay
59
+ ) -> OverlayCollection < Elements , Overlay > {
60
+ replacingSubrange ( elements. endIndex..< elements. endIndex, with: newElements)
61
+ }
62
+
63
+ @inlinable
64
+ public func inserting< Overlay> (
65
+ contentsOf newElements: Overlay , at position: Elements . Index
66
+ ) -> OverlayCollection < Elements , Overlay > {
67
+ replacingSubrange ( position..< position, with: newElements)
68
+ }
69
+
70
+ @inlinable
71
+ public func removingSubrange(
72
+ _ subrange: Range < Elements . Index >
73
+ ) -> OverlayCollection < Elements , EmptyCollection < Elements . Element > > {
74
+ replacingSubrange ( subrange, with: EmptyCollection ( ) )
75
+ }
76
+
77
+ @inlinable
78
+ public func appending(
79
+ _ element: Elements . Element
80
+ ) -> OverlayCollection < Elements , CollectionOfOne < Elements . Element > > {
81
+ appending ( contentsOf: CollectionOfOne ( element) )
82
+ }
83
+
84
+ @inlinable
85
+ public func inserting(
86
+ _ element: Elements . Element , at position: Elements . Index
87
+ ) -> OverlayCollection < Elements , CollectionOfOne < Elements . Element > > {
88
+ inserting ( contentsOf: CollectionOfOne ( element) , at: position)
89
+ }
13
90
14
91
@inlinable
15
- public func replacingSubrange < Replacements > (
16
- _ subrange : Range < Index > , with newElements : Replacements
17
- ) -> ReplacingSubrangeCollection < Base , Replacements > {
18
- ReplacingSubrangeCollection ( base : elements , replacements : newElements , replacedRange : subrange )
92
+ public func removing (
93
+ at position : Elements . Index
94
+ ) -> OverlayCollection < Elements , EmptyCollection < Elements . Element > > {
95
+ removingSubrange ( position ..< position )
19
96
}
20
97
}
21
98
22
- public struct ReplacingSubrangeCollection < Base, Replacements >
23
- where Base: Collection , Replacements : Collection , Base. Element == Replacements . Element {
99
+ public struct OverlayCollection < Base, Overlay >
100
+ where Base: Collection , Overlay : Collection , Base. Element == Overlay . Element {
24
101
25
102
@usableFromInline
26
103
internal var base : Base
27
104
28
105
@usableFromInline
29
- internal var replacements : Replacements
106
+ internal var overlay : Overlay
30
107
31
108
@usableFromInline
32
109
internal var replacedRange : Range < Base . Index >
33
110
34
111
@inlinable
35
- internal init ( base: Base , replacements : Replacements , replacedRange: Range < Base . Index > ) {
112
+ internal init ( base: Base , overlay : Overlay , replacedRange: Range < Base . Index > ) {
36
113
self . base = base
37
- self . replacements = replacements
114
+ self . overlay = overlay
38
115
self . replacedRange = replacedRange
39
116
}
40
117
}
41
118
42
- extension ReplacingSubrangeCollection : Collection {
119
+ extension OverlayCollection : Collection {
43
120
44
121
public typealias Element = Base . Element
45
122
@@ -48,10 +125,10 @@ extension ReplacingSubrangeCollection: Collection {
48
125
@usableFromInline
49
126
internal enum Wrapped {
50
127
case base( Base . Index )
51
- case replacement ( Replacements . Index )
128
+ case overlay ( Overlay . Index )
52
129
}
53
130
54
- /// The underlying base/replacements index.
131
+ /// The underlying base/overlay index.
55
132
///
56
133
@usableFromInline
57
134
internal var wrapped : Wrapped
@@ -72,11 +149,11 @@ extension ReplacingSubrangeCollection: Collection {
72
149
switch ( lhs. wrapped, rhs. wrapped) {
73
150
case ( . base( let unwrappedLeft) , . base( let unwrappedRight) ) :
74
151
return unwrappedLeft < unwrappedRight
75
- case ( . replacement ( let unwrappedLeft) , . replacement ( let unwrappedRight) ) :
152
+ case ( . overlay ( let unwrappedLeft) , . overlay ( let unwrappedRight) ) :
76
153
return unwrappedLeft < unwrappedRight
77
- case ( . base( let unwrappedLeft) , . replacement ( _) ) :
154
+ case ( . base( let unwrappedLeft) , . overlay ( _) ) :
78
155
return unwrappedLeft < lhs. replacedRange. lowerBound
79
- case ( . replacement ( _) , . base( let unwrappedRight) ) :
156
+ case ( . overlay ( _) , . base( let unwrappedRight) ) :
80
157
return !( unwrappedRight < lhs. replacedRange. lowerBound)
81
158
}
82
159
}
@@ -87,7 +164,7 @@ extension ReplacingSubrangeCollection: Collection {
87
164
switch ( lhs. wrapped, rhs. wrapped) {
88
165
case ( . base( let unwrappedLeft) , . base( let unwrappedRight) ) :
89
166
return unwrappedLeft == unwrappedRight
90
- case ( . replacement ( let unwrappedLeft) , . replacement ( let unwrappedRight) ) :
167
+ case ( . overlay ( let unwrappedLeft) , . overlay ( let unwrappedRight) ) :
91
168
return unwrappedLeft == unwrappedRight
92
169
default :
93
170
return false
@@ -96,41 +173,41 @@ extension ReplacingSubrangeCollection: Collection {
96
173
}
97
174
}
98
175
99
- extension ReplacingSubrangeCollection {
176
+ extension OverlayCollection {
100
177
101
178
@inlinable
102
179
internal func makeIndex( _ position: Base . Index ) -> Index {
103
180
Index ( wrapped: . base( position) , replacedRange: replacedRange)
104
181
}
105
182
106
183
@inlinable
107
- internal func makeIndex( _ position: Replacements . Index ) -> Index {
108
- Index ( wrapped: . replacement ( position) , replacedRange: replacedRange)
184
+ internal func makeIndex( _ position: Overlay . Index ) -> Index {
185
+ Index ( wrapped: . overlay ( position) , replacedRange: replacedRange)
109
186
}
110
187
111
188
@inlinable
112
189
public var startIndex : Index {
113
190
if base. startIndex == replacedRange. lowerBound {
114
- if replacements . isEmpty {
191
+ if overlay . isEmpty {
115
192
return makeIndex ( replacedRange. upperBound)
116
193
}
117
- return makeIndex ( replacements . startIndex)
194
+ return makeIndex ( overlay . startIndex)
118
195
}
119
196
return makeIndex ( base. startIndex)
120
197
}
121
198
122
199
@inlinable
123
200
public var endIndex : Index {
124
- if replacedRange. lowerBound != base. endIndex || replacements . isEmpty {
201
+ if replacedRange. lowerBound != base. endIndex || overlay . isEmpty {
125
202
return makeIndex ( base. endIndex)
126
203
}
127
- return makeIndex ( replacements . endIndex)
204
+ return makeIndex ( overlay . endIndex)
128
205
}
129
206
130
207
@inlinable
131
208
public var count : Int {
132
209
base. distance ( from: base. startIndex, to: replacedRange. lowerBound)
133
- + replacements . count
210
+ + overlay . count
134
211
+ base. distance ( from: replacedRange. upperBound, to: base. endIndex)
135
212
}
136
213
@@ -140,19 +217,19 @@ extension ReplacingSubrangeCollection {
140
217
case . base( var baseIndex) :
141
218
base. formIndex ( after: & baseIndex)
142
219
if baseIndex == replacedRange. lowerBound {
143
- if replacements . isEmpty {
220
+ if overlay . isEmpty {
144
221
return makeIndex ( replacedRange. upperBound)
145
222
}
146
- return makeIndex ( replacements . startIndex)
223
+ return makeIndex ( overlay . startIndex)
147
224
}
148
225
return makeIndex ( baseIndex)
149
226
150
- case . replacement ( var replacementIndex ) :
151
- replacements . formIndex ( after: & replacementIndex )
152
- if replacedRange. lowerBound != base. endIndex, replacementIndex == replacements . endIndex {
227
+ case . overlay ( var overlayIndex ) :
228
+ overlay . formIndex ( after: & overlayIndex )
229
+ if replacedRange. lowerBound != base. endIndex, overlayIndex == overlay . endIndex {
153
230
return makeIndex ( replacedRange. upperBound)
154
231
}
155
- return makeIndex ( replacementIndex )
232
+ return makeIndex ( overlayIndex )
156
233
}
157
234
}
158
235
@@ -161,34 +238,34 @@ extension ReplacingSubrangeCollection {
161
238
switch position. wrapped {
162
239
case . base( let baseIndex) :
163
240
return base [ baseIndex]
164
- case . replacement ( let replacementIndex ) :
165
- return replacements [ replacementIndex ]
241
+ case . overlay ( let overlayIndex ) :
242
+ return overlay [ overlayIndex ]
166
243
}
167
244
}
168
245
}
169
246
170
- extension ReplacingSubrangeCollection : BidirectionalCollection
171
- where Base: BidirectionalCollection , Replacements : BidirectionalCollection {
247
+ extension OverlayCollection : BidirectionalCollection
248
+ where Base: BidirectionalCollection , Overlay : BidirectionalCollection {
172
249
173
250
@inlinable
174
251
public func index( before i: Index ) -> Index {
175
252
switch i. wrapped {
176
253
case . base( var baseIndex) :
177
254
if baseIndex == replacedRange. upperBound {
178
- if replacements . isEmpty {
255
+ if overlay . isEmpty {
179
256
return makeIndex ( base. index ( before: replacedRange. lowerBound) )
180
257
}
181
- return makeIndex ( replacements . index ( before: replacements . endIndex) )
258
+ return makeIndex ( overlay . index ( before: overlay . endIndex) )
182
259
}
183
260
base. formIndex ( before: & baseIndex)
184
261
return makeIndex ( baseIndex)
185
262
186
- case . replacement ( var replacementIndex ) :
187
- if replacementIndex == replacements . startIndex {
263
+ case . overlay ( var overlayIndex ) :
264
+ if overlayIndex == overlay . startIndex {
188
265
return makeIndex ( base. index ( before: replacedRange. lowerBound) )
189
266
}
190
- replacements . formIndex ( before: & replacementIndex )
191
- return makeIndex ( replacementIndex )
267
+ overlay . formIndex ( before: & overlayIndex )
268
+ return makeIndex ( overlayIndex )
192
269
}
193
270
}
194
271
}
0 commit comments