Skip to content

Commit dd2ad89

Browse files
committed
Add missing documentation to rotate and reverse
1 parent 1df76cf commit dd2ad89

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

Sources/Algorithms/Rotate.swift

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,18 @@ extension MutableCollection where Self: BidirectionalCollection {
4343
return (f, l)
4444
}
4545

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`.
4658
public mutating func reverse(subrange: Range<Index>) {
4759
if subrange.isEmpty { return }
4860
var lo = subrange.lowerBound
@@ -98,6 +110,27 @@ extension MutableCollection {
98110
return (p, q)
99111
}
100112

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`.
101134
@discardableResult
102135
public mutating func rotate(
103136
subrange: Range<Index>,
@@ -159,13 +192,52 @@ extension MutableCollection {
159192
return ret
160193
}
161194

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*)
162213
@discardableResult
163214
public mutating func rotate(toStartAt newStart: Index) -> Index {
164215
rotate(subrange: startIndex..<endIndex, toStartAt: newStart)
165216
}
166217
}
167218

168219
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`.
169241
@discardableResult
170242
public mutating func rotate(
171243
subrange: Range<Index>,

0 commit comments

Comments
 (0)