@@ -43,6 +43,18 @@ extension MutableCollection where Self: BidirectionalCollection {
43
43
return ( f, l)
44
44
}
45
45
46
+ /// Reverses the elements within the given subrange.
47
+ ///
48
+ /// This example reverses the numbers within the subrange at the start of the
49
+ /// `numbers` array:
50
+ ///
51
+ /// var numbers = [10, 20, 30, 40, 50, 60, 70, 80]
52
+ /// numbers.reverse(subrange: 0..<4)
53
+ /// // numbers == [40, 30, 20, 10, 50, 60, 70, 80]
54
+ ///
55
+ /// - Parameter subrange: The subrange of this collection to reverse.
56
+ ///
57
+ /// - Complexity: O(*n*), where *n* is the length of `subrange`.
46
58
public mutating func reverse( subrange: Range < Index > ) {
47
59
if subrange. isEmpty { return }
48
60
var lo = subrange. lowerBound
@@ -98,6 +110,27 @@ extension MutableCollection {
98
110
return ( p, q)
99
111
}
100
112
113
+ /// Rotates the elements within the given subrange so that the element
114
+ /// at the specified index becomes the start of the subrange.
115
+ ///
116
+ /// Rotating a collection is equivalent to breaking the collection into two
117
+ /// sections at the index `newStart`, and then swapping those two sections.
118
+ /// In this example, the `numbers` array is rotated so that the element at
119
+ /// index `3` (`40`) is first:
120
+ ///
121
+ /// var numbers = [10, 20, 30, 40, 50, 60, 70, 80]
122
+ /// let oldStart = numbers.rotate(subrange: 0..<4, toStartAt: 2)
123
+ /// // numbers == [30, 40, 10, 20, 50, 60, 70, 80]
124
+ /// // numbers[oldStart] == 10
125
+ ///
126
+ /// - Parameters:
127
+ /// - subrange: The subrange of this collection to rotate.
128
+ /// - newStart: The index of the element that should be at the start of
129
+ /// `subrange` after rotating.
130
+ /// - Returns: The new index of the element that was at the start of
131
+ /// `subrange` pre-rotation.
132
+ ///
133
+ /// - Complexity: O(*n*), where *n* is the length of `subrange`.
101
134
@discardableResult
102
135
public mutating func rotate(
103
136
subrange: Range < Index > ,
@@ -159,13 +192,52 @@ extension MutableCollection {
159
192
return ret
160
193
}
161
194
195
+ /// Rotates the elements of this collection so that the element
196
+ /// at the specified index becomes the start of the collection.
197
+ ///
198
+ /// Rotating a collection is equivalent to breaking the collection into two
199
+ /// sections at the index `newStart`, and then swapping those two sections.
200
+ /// In this example, the `numbers` array is rotated so that the element at
201
+ /// index `3` (`40`) is first:
202
+ ///
203
+ /// var numbers = [10, 20, 30, 40, 50, 60, 70, 80]
204
+ /// let oldStart = numbers.rotate(toStartAt: 3)
205
+ /// // numbers == [40, 50, 60, 70, 80, 10, 20, 30]
206
+ /// // numbers[oldStart] == 10
207
+ ///
208
+ /// - Parameter newStart: The index of the element that should be first after
209
+ /// rotating.
210
+ /// - Returns: The new index of the element that was first pre-rotation.
211
+ ///
212
+ /// - Complexity: O(*n*)
162
213
@discardableResult
163
214
public mutating func rotate( toStartAt newStart: Index ) -> Index {
164
215
rotate ( subrange: startIndex..< endIndex, toStartAt: newStart)
165
216
}
166
217
}
167
218
168
219
extension MutableCollection where Self: BidirectionalCollection {
220
+ /// Rotates the elements within the given subrange so that the element
221
+ /// at the specified index becomes the start of the subrange.
222
+ ///
223
+ /// Rotating a collection is equivalent to breaking the collection into two
224
+ /// sections at the index `newStart`, and then swapping those two sections.
225
+ /// In this example, the `numbers` array is rotated so that the element at
226
+ /// index `3` (`40`) is first:
227
+ ///
228
+ /// var numbers = [10, 20, 30, 40, 50, 60, 70, 80]
229
+ /// let oldStart = numbers.rotate(subrange: 0..<4, toStartAt: 2)
230
+ /// // numbers == [30, 40, 10, 20, 50, 60, 70, 80]
231
+ /// // numbers[oldStart] == 10
232
+ ///
233
+ /// - Parameters:
234
+ /// - subrange: The subrange of this collection to rotate.
235
+ /// - newStart: The index of the element that should be at the start of
236
+ /// `subrange` after rotating.
237
+ /// - Returns: The new index of the element that was at the start of
238
+ /// `subrange` pre-rotation.
239
+ ///
240
+ /// - Complexity: O(*n*), where *n* is the length of `subrange`.
169
241
@discardableResult
170
242
public mutating func rotate(
171
243
subrange: Range < Index > ,
0 commit comments