Skip to content

Commit 702e1e3

Browse files
authored
Merge branch 'master' into remove
2 parents 8302dea + e93a766 commit 702e1e3

2 files changed

Lines changed: 7 additions & 3 deletions

File tree

Sources/SortedArray.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,11 @@ extension SortedArray: RandomAccessCollection {
106106
return _elements[position]
107107
}
108108

109+
/// Like `Sequence.filter(_:)`, but returns a `SortedArray` instead of an `Array`.
110+
/// We can do this efficiently because filtering doesn't change the sort order.
109111
public func filter(_ isIncluded: (Element) throws -> Bool) rethrows -> SortedArray<Element> {
110-
return SortedArray(sorted: try filter(isIncluded), areInIncreasingOrder: areInIncreasingOrder)
112+
let newElements = try _elements.filter(isIncluded)
113+
return SortedArray(sorted: newElements, areInIncreasingOrder: areInIncreasingOrder)
111114
}
112115
}
113116

@@ -145,15 +148,15 @@ extension SortedArray {
145148
/// - Complexity: O(1).
146149
@warn_unqualified_access
147150
public func min() -> Element? {
148-
return _elements.first
151+
return first
149152
}
150153

151154
/// Returns the maximum element in the sequence.
152155
///
153156
/// - Complexity: O(1).
154157
@warn_unqualified_access
155158
public func max() -> Element? {
156-
return _elements.last
159+
return last
157160
}
158161
}
159162

Tests/SortedArrayTests/SortedArrayTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ extension SortedArrayTests {
202202
("testMax", testMax),
203203
("testCustomStringConvertible", testCustomStringConvertible),
204204
("testCustomDebugStringConvertible", testCustomDebugStringConvertible),
205+
("testFilter", testFilter),
205206
("testRemoveAtBeginningPreservesSortOrder", testRemoveAtBeginningPreservesSortOrder),
206207
("testRemoveInMiddlePreservesSortOrder", testRemoveInMiddlePreservesSortOrder),
207208
("testRemoveAtEndPreservesSortOrder", testRemoveAtEndPreservesSortOrder),

0 commit comments

Comments
 (0)