Skip to content

Commit a3b6200

Browse files
committed
feat: add toggle func for Set
1 parent d94882b commit a3b6200

File tree

6 files changed

+86
-0
lines changed

6 files changed

+86
-0
lines changed

ReerKit.xcodeproj/project.pbxproj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@
2626
D019B8F7285BA3CB00257A7D /* UnfairLockTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D019B8F6285BA3CB00257A7D /* UnfairLockTests.swift */; };
2727
D01DABC429B4FE1300ABD25E /* ThrottlerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D01DABC329B4FE1300ABD25E /* ThrottlerTests.swift */; };
2828
D022BED729DD48E200245550 /* URLRequestExtensionsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D022BED629DD48E200245550 /* URLRequestExtensionsTests.swift */; };
29+
D02B86032D3F6EEF007FDAE1 /* Set+REExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D02B86022D3F6EE9007FDAE1 /* Set+REExtensions.swift */; };
30+
D02B86042D3F6EEF007FDAE1 /* Set+REExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D02B86022D3F6EE9007FDAE1 /* Set+REExtensions.swift */; };
31+
D02B86052D3F6EEF007FDAE1 /* Set+REExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D02B86022D3F6EE9007FDAE1 /* Set+REExtensions.swift */; };
32+
D02B86062D3F6EEF007FDAE1 /* Set+REExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D02B86022D3F6EE9007FDAE1 /* Set+REExtensions.swift */; };
33+
D02B86072D3F6EEF007FDAE1 /* Set+REExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D02B86022D3F6EE9007FDAE1 /* Set+REExtensions.swift */; };
34+
D02B86092D3F7039007FDAE1 /* SetExtensionsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D02B86082D3F7033007FDAE1 /* SetExtensionsTests.swift */; };
2935
D03510692D362F4C00F4B5F2 /* Associatable.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0350FF82D362F4C00F4B5F2 /* Associatable.swift */; };
3036
D035106A2D362F4C00F4B5F2 /* Queue.swift in Sources */ = {isa = PBXBuildFile; fileRef = D035103D2D362F4C00F4B5F2 /* Queue.swift */; };
3137
D035106B2D362F4C00F4B5F2 /* UINavigationController+REExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D03510242D362F4C00F4B5F2 /* UINavigationController+REExtensions.swift */; };
@@ -863,6 +869,8 @@
863869
D019B8F6285BA3CB00257A7D /* UnfairLockTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UnfairLockTests.swift; sourceTree = "<group>"; };
864870
D01DABC329B4FE1300ABD25E /* ThrottlerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ThrottlerTests.swift; sourceTree = "<group>"; };
865871
D022BED629DD48E200245550 /* URLRequestExtensionsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = URLRequestExtensionsTests.swift; sourceTree = "<group>"; };
872+
D02B86022D3F6EE9007FDAE1 /* Set+REExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Set+REExtensions.swift"; sourceTree = "<group>"; };
873+
D02B86082D3F7033007FDAE1 /* SetExtensionsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SetExtensionsTests.swift; sourceTree = "<group>"; };
866874
D0350FCD2D362F4C00F4B5F2 /* CAGradientLayer+REExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CAGradientLayer+REExtensions.swift"; sourceTree = "<group>"; };
867875
D0350FCE2D362F4C00F4B5F2 /* CALayer+REExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CALayer+REExtensions.swift"; sourceTree = "<group>"; };
868876
D0350FCF2D362F4C00F4B5F2 /* CATransform3D+REExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CATransform3D+REExtensions.swift"; sourceTree = "<group>"; };
@@ -1319,6 +1327,7 @@
13191327
D03510152D362F4C00F4B5F2 /* StandardLibrary */ = {
13201328
isa = PBXGroup;
13211329
children = (
1330+
D02B86022D3F6EE9007FDAE1 /* Set+REExtensions.swift */,
13221331
D0350FFC2D362F4C00F4B5F2 /* AnyObjectExtensions */,
13231332
D0350FFD2D362F4C00F4B5F2 /* Array+REExtensions.swift */,
13241333
D0350FFE2D362F4C00F4B5F2 /* BinaryFloatingPoint+REExtensions.swift */,
@@ -1678,6 +1687,7 @@
16781687
D0C95FD62857937F00D0522D /* StandardLibraryTests */ = {
16791688
isa = PBXGroup;
16801689
children = (
1690+
D02B86082D3F7033007FDAE1 /* SetExtensionsTests.swift */,
16811691
D0C95FD7285793A000D0522D /* BoolExtensionsTests.swift */,
16821692
D0C95FDD2857A24000D0522D /* SignedIntegerExtensionsTests.swift */,
16831693
D0C95FE12857AD5D00D0522D /* SignedNumericExtensionsTests.swift */,
@@ -2221,6 +2231,7 @@
22212231
D03511EE2D362F4C00F4B5F2 /* NanoID.swift in Sources */,
22222232
D03511EF2D362F4C00F4B5F2 /* UIApplication+REExtensions.swift in Sources */,
22232233
D03511F02D362F4C00F4B5F2 /* ReerKit.swift in Sources */,
2234+
D02B86052D3F6EEF007FDAE1 /* Set+REExtensions.swift in Sources */,
22242235
D03511F12D362F4C00F4B5F2 /* Timer+REExtensions.swift in Sources */,
22252236
D03511F22D362F4C00F4B5F2 /* Collection+REExtensions.swift in Sources */,
22262237
D03511F32D362F4C00F4B5F2 /* Reachability.swift in Sources */,
@@ -2371,6 +2382,7 @@
23712382
D03512762D362F4C00F4B5F2 /* NanoID.swift in Sources */,
23722383
D03512772D362F4C00F4B5F2 /* UIApplication+REExtensions.swift in Sources */,
23732384
D03512782D362F4C00F4B5F2 /* ReerKit.swift in Sources */,
2385+
D02B86042D3F6EEF007FDAE1 /* Set+REExtensions.swift in Sources */,
23742386
D03512792D362F4C00F4B5F2 /* Timer+REExtensions.swift in Sources */,
23752387
D035127A2D362F4C00F4B5F2 /* Collection+REExtensions.swift in Sources */,
23762388
D035127B2D362F4C00F4B5F2 /* Reachability.swift in Sources */,
@@ -2520,6 +2532,7 @@
25202532
D03511652D362F4C00F4B5F2 /* NanoID.swift in Sources */,
25212533
D03511662D362F4C00F4B5F2 /* UIApplication+REExtensions.swift in Sources */,
25222534
D03511672D362F4C00F4B5F2 /* ReerKit.swift in Sources */,
2535+
D02B86062D3F6EEF007FDAE1 /* Set+REExtensions.swift in Sources */,
25232536
D03511682D362F4C00F4B5F2 /* Timer+REExtensions.swift in Sources */,
25242537
D03511692D362F4C00F4B5F2 /* Collection+REExtensions.swift in Sources */,
25252538
D035116A2D362F4C00F4B5F2 /* Reachability.swift in Sources */,
@@ -2669,6 +2682,7 @@
26692682
D03510DD2D362F4C00F4B5F2 /* NanoID.swift in Sources */,
26702683
D03510DE2D362F4C00F4B5F2 /* UIApplication+REExtensions.swift in Sources */,
26712684
D03510DF2D362F4C00F4B5F2 /* ReerKit.swift in Sources */,
2685+
D02B86072D3F6EEF007FDAE1 /* Set+REExtensions.swift in Sources */,
26722686
D03510E02D362F4C00F4B5F2 /* Timer+REExtensions.swift in Sources */,
26732687
D03510E12D362F4C00F4B5F2 /* Collection+REExtensions.swift in Sources */,
26742688
D03510E22D362F4C00F4B5F2 /* Reachability.swift in Sources */,
@@ -2761,6 +2775,7 @@
27612775
D03512C32D362F4C00F4B5F2 /* UITabBar+REExtensions.swift in Sources */,
27622776
D03512C42D362F4C00F4B5F2 /* UIFont+REExtensions.swift in Sources */,
27632777
D03512C52D362F4C00F4B5F2 /* FloatingPoint+REExtensions.swift in Sources */,
2778+
D02B86032D3F6EEF007FDAE1 /* Set+REExtensions.swift in Sources */,
27642779
D03512C62D362F4C00F4B5F2 /* SignedInteger+REExtensions.swift in Sources */,
27652780
D03512C72D362F4C00F4B5F2 /* Bool+REExtensions.swift in Sources */,
27662781
D03512C82D362F4C00F4B5F2 /* Weak.swift in Sources */,
@@ -2940,6 +2955,7 @@
29402955
D05C8674294B4F610077E744 /* CAGradientLayerExtensionsTests.swift in Sources */,
29412956
D090D96829DC5A6300245538 /* LocalExtensionsTests.swift in Sources */,
29422957
D076D4BD29F3C24C0008ABE0 /* UITextViewExtensionsTests.swift in Sources */,
2958+
D02B86092D3F7039007FDAE1 /* SetExtensionsTests.swift in Sources */,
29432959
D094C84F29F456A400491CD8 /* OptionSetExtensionsTests.swift in Sources */,
29442960
);
29452961
runOnlyForDeploymentPostprocessing = 0;
Binary file not shown.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Set+REExtensions
2+
3+
## Topics
4+
5+
- ``ReerReferenceGeneric/toggle(_:)``

Sources/ReerKit/Documentation.docc/Catalog/StandardLibrary/StandardLibrary_doc.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
- <doc:/RangeExt_doc>
2323
- <doc:/RangeReplaceableCollectionExt_doc>
2424
- <doc:/SequenceExt_doc>
25+
- <doc:/SetExt_doc>
2526
- <doc:/SignedIntegerExt_doc>
2627
- <doc:/SignedNumericExt_doc>
2728
- <doc:/StringExt_doc>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//
2+
// Copyright © 2022 reers.
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in
12+
// all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
// THE SOFTWARE.
21+
22+
// MARK: ----------- Mutating -----------
23+
24+
extension Set: ReerReferenceGenericCompatible {
25+
public typealias T = Element
26+
}
27+
28+
// MARK: - Methods
29+
30+
public extension ReerReferenceGeneric where Base == Set<T> {
31+
/// ReerKit: Toggle the presence of an element in the set
32+
///
33+
/// - Parameter element: The element to toggle
34+
/// - Returns: True if the element was inserted, false if it was removed
35+
@discardableResult
36+
mutating func toggle(_ element: T) -> Bool {
37+
if base.pointee.insert(element).inserted {
38+
return true
39+
} else {
40+
base.pointee.remove(element)
41+
return false
42+
}
43+
}
44+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// SetExtensionsTests.swift
3+
// ReerKit
4+
//
5+
// Created by phoenix on 2025/1/21.
6+
// Copyright © 2025 reers. All rights reserved.
7+
//
8+
9+
import XCTest
10+
import ReerKit
11+
12+
class SetExtensionsTests: XCTestCase {
13+
14+
func testToogle() {
15+
var set: Set = [1, 2, 3, 4, 5]
16+
XCTAssertFalse(set.re.toggle(1))
17+
XCTAssertTrue(set.re.toggle(6))
18+
XCTAssertEqual(set, [2, 3, 4, 5, 6])
19+
}
20+
}

0 commit comments

Comments
 (0)