From 6142869b83c412864e4251d67902d3110dff124b Mon Sep 17 00:00:00 2001 From: Konrad `ktoso` Malawski Date: Tue, 16 Aug 2022 20:37:44 +0900 Subject: [PATCH 1/5] +OrderedSet add filter #158 --- .../OrderedSet/OrderedSet.swift | 23 +++++++++++++++++++ .../OrderedSet/OrderedSetTests.swift | 18 +++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/Sources/OrderedCollections/OrderedSet/OrderedSet.swift b/Sources/OrderedCollections/OrderedSet/OrderedSet.swift index 9e3965bbb..c5f7c6053 100644 --- a/Sources/OrderedCollections/OrderedSet/OrderedSet.swift +++ b/Sources/OrderedCollections/OrderedSet/OrderedSet.swift @@ -494,3 +494,26 @@ extension OrderedSet { return _elements.remove(at: index) } } + +extension OrderedSet { + /// Returns a new ordered set containing the values pairs of the ordered set + /// that satisfy the given predicate. + /// + /// - Parameter isIncluded: A closure that takes a value as its + /// argument and returns a Boolean value indicating whether the value + /// should be included in the returned dictionary. + /// + /// - Returns: An ordered set of the values that `isIncluded` allows. + /// + /// - Complexity: O(`count`) + @inlinable + public func filter( + _ isIncluded: (Element) throws -> Bool + ) rethrows -> Self { + var result: OrderedSet = Self(minimumCapacity: _minimumCapacity) + for element in self where try isIncluded(element) { + result._appendNew(element) + } + return result + } +} diff --git a/Tests/OrderedCollectionsTests/OrderedSet/OrderedSetTests.swift b/Tests/OrderedCollectionsTests/OrderedSet/OrderedSetTests.swift index f2161dc3d..2ed48c8c1 100644 --- a/Tests/OrderedCollectionsTests/OrderedSet/OrderedSetTests.swift +++ b/Tests/OrderedCollectionsTests/OrderedSet/OrderedSetTests.swift @@ -1360,4 +1360,22 @@ class OrderedSetTests: CollectionTestCase { } } } + + + func test_filter() { + let items = (0 ..< 100) + let s = OrderedSet(items) + + var c = 0 + let s2 = s.filter { item in + c += 1 + return item.isMultiple(of: 2) + } + expectEqual(c, 100) + expectEqualElements(s, items) + + expectEqualElements(s2, (0 ..< 50).compactMap { key in + return (2 * key) + }) + } } From f8726ce33f9872597675c0e09c878a7b952c3502 Mon Sep 17 00:00:00 2001 From: Konrad `ktoso` Malawski Date: Wed, 17 Aug 2022 11:01:03 +0900 Subject: [PATCH 2/5] checkInvariants --- Sources/OrderedCollections/OrderedSet/OrderedSet.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Sources/OrderedCollections/OrderedSet/OrderedSet.swift b/Sources/OrderedCollections/OrderedSet/OrderedSet.swift index c5f7c6053..25cc22f42 100644 --- a/Sources/OrderedCollections/OrderedSet/OrderedSet.swift +++ b/Sources/OrderedCollections/OrderedSet/OrderedSet.swift @@ -514,6 +514,7 @@ extension OrderedSet { for element in self where try isIncluded(element) { result._appendNew(element) } + result._checkInvariants() return result } } From 4138ccd68efeb8505d7f03555584fb7953531315 Mon Sep 17 00:00:00 2001 From: Konrad `ktoso` Malawski Date: Wed, 17 Aug 2022 11:01:07 +0900 Subject: [PATCH 3/5] improve test to cover more cases --- .../OrderedSet/OrderedSetTests.swift | 21 +++++++------------ 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/Tests/OrderedCollectionsTests/OrderedSet/OrderedSetTests.swift b/Tests/OrderedCollectionsTests/OrderedSet/OrderedSetTests.swift index 2ed48c8c1..797b2ee41 100644 --- a/Tests/OrderedCollectionsTests/OrderedSet/OrderedSetTests.swift +++ b/Tests/OrderedCollectionsTests/OrderedSet/OrderedSetTests.swift @@ -1361,21 +1361,16 @@ class OrderedSetTests: CollectionTestCase { } } - func test_filter() { - let items = (0 ..< 100) - let s = OrderedSet(items) + withOrderedSetLayouts(scales: [0, 5, 6]) { layout in + withEvery("factor", in: [1, 2, 3, 5, 10]) { factor in + let count = layout.count + let input = OrderedSet(layout: layout, contents: 0 ..< count) + let expected = (0 ..< count).filter { $0 % factor == 0 } + let actual = input.filter { $0 % factor == 0 } - var c = 0 - let s2 = s.filter { item in - c += 1 - return item.isMultiple(of: 2) + expectEqualElements(actual, expected) + } } - expectEqual(c, 100) - expectEqualElements(s, items) - - expectEqualElements(s2, (0 ..< 50).compactMap { key in - return (2 * key) - }) } } From 876586f9249173f3ea6fd73291fc5510118463e3 Mon Sep 17 00:00:00 2001 From: Konrad `ktoso` Malawski Date: Wed, 17 Aug 2022 11:06:07 +0900 Subject: [PATCH 4/5] expect type is correct --- .../AssertionContexts/Assertions.swift | 4 ++++ .../OrderedSet/OrderedSetTests.swift | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/Sources/_CollectionsTestSupport/AssertionContexts/Assertions.swift b/Sources/_CollectionsTestSupport/AssertionContexts/Assertions.swift index ff2e6e4f7..76db23779 100644 --- a/Sources/_CollectionsTestSupport/AssertionContexts/Assertions.swift +++ b/Sources/_CollectionsTestSupport/AssertionContexts/Assertions.swift @@ -402,3 +402,7 @@ public func expectThrows( errorHandler(error) } } + +public func expectType(_ actual: T, _ expectedType: Any.Type) { + expectTrue(type(of: actual) == expectedType) +} diff --git a/Tests/OrderedCollectionsTests/OrderedSet/OrderedSetTests.swift b/Tests/OrderedCollectionsTests/OrderedSet/OrderedSetTests.swift index 797b2ee41..5389730b1 100644 --- a/Tests/OrderedCollectionsTests/OrderedSet/OrderedSetTests.swift +++ b/Tests/OrderedCollectionsTests/OrderedSet/OrderedSetTests.swift @@ -1373,4 +1373,11 @@ class OrderedSetTests: CollectionTestCase { } } } + func test_filter_type() { + let s = Set([1, 2, 3, 4]).filter { $0.isMultiple(of: 2) } + expectType(s, Set.self) + + let os = OrderedSet([1, 2, 3, 4]).filter { $0.isMultiple(of: 2) } + expectType(os, OrderedSet.self) + } } From 74a800ac534877875d5d1187ac04f35b31a0b98c Mon Sep 17 00:00:00 2001 From: Konrad `ktoso` Malawski Date: Wed, 17 Aug 2022 12:21:02 +0900 Subject: [PATCH 5/5] Apply suggestions from code review Co-authored-by: Xiaodi Wu <13952+xwu@users.noreply.github.com> --- Sources/OrderedCollections/OrderedSet/OrderedSet.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/OrderedCollections/OrderedSet/OrderedSet.swift b/Sources/OrderedCollections/OrderedSet/OrderedSet.swift index 25cc22f42..3a65abc4d 100644 --- a/Sources/OrderedCollections/OrderedSet/OrderedSet.swift +++ b/Sources/OrderedCollections/OrderedSet/OrderedSet.swift @@ -508,7 +508,7 @@ extension OrderedSet { /// - Complexity: O(`count`) @inlinable public func filter( - _ isIncluded: (Element) throws -> Bool + _ isIncluded: (Element) throws -> Bool ) rethrows -> Self { var result: OrderedSet = Self(minimumCapacity: _minimumCapacity) for element in self where try isIncluded(element) {