-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,21 @@ extension BijectiveDictionary { | |
self._rtl = Dictionary(uniqueKeysWithValues: reversePairs) | ||
} | ||
|
||
/// Creates a new dictionary from the left-right pairs in the given sequence, discarding any conflicting pairs. | ||
This comment has been minimized.
Sorry, something went wrong. |
||
/// | ||
/// - Parameter leftRightPairs: A sequence of left-right pairs to use for the new dictionary. | ||
This comment has been minimized.
Sorry, something went wrong.
DandyLyons
Collaborator
|
||
/// - Complexity: O(*n*), where *n* is the number of key-value pairs in the given sequence. | ||
@inlinable public init<S>( | ||
discardConflicting leftRightPairs: S | ||
This comment has been minimized.
Sorry, something went wrong.
DandyLyons
Collaborator
|
||
) where S: Sequence, S.Element == Element { | ||
self.init() | ||
for pair in leftRightPairs where conflict(with: pair) == nil { | ||
_ltr[pair.left] = pair.right | ||
_rtl[pair.right] = pair.left | ||
} | ||
_invariantCheck() | ||
} | ||
|
||
/// Creates a bijective dictionary from a standard dictionary. | ||
/// | ||
/// Unlike a standard dictionary which only requires its keys be unique, a bijective | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -299,4 +299,11 @@ func conflict() { | |
#expect(dict.conflict(with: ("A", 1)) == .pair) | ||
#expect(dict.conflict(with: ("A", 3)) == .both(otherLeft: "C", otherRight: 1)) | ||
} | ||
|
||
@Test | ||
func discardConflicting() { | ||
let pairs = [("A", 1), ("B", 1), ("A", 10), ("C", 4), ("C", 4), ("E", 5), ("A", 5)] | ||
This comment has been minimized.
Sorry, something went wrong.
DandyLyons
Collaborator
|
||
let dict = BijectiveDictionary(discardConflicting: pairs) | ||
#expect(dict == ["A": 1, "C": 4, "E": 5]) | ||
} | ||
#endif |
I recommend writing
discarding conflicting pairs (if any)
.