Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conflict detection #20

Merged
merged 4 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions Sources/BijectiveDictionary/BijectiveDictionary+Conflict.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// =============================================================
// File: BijectiveDictionary+Conflict.swift
// Project: BijectiveDictionary
// -------------------------------------------------------------
// Created by Jacob Gelman on 09/11/2024
// Copyright © 2024 Jacob Gelman. All rights reserved.
// =============================================================

extension BijectiveDictionary {

/// The result of a conflict check.
@frozen
public enum Conflict: Hashable {

/// The left value is already present.
case left

/// The right value is already present.
case right

/// Both the left and right values are already present in the same pair.
case pair

/// Both the left and right values are already present across two different pairs.
case both(otherLeft: Left, otherRight: Right)
}

/// Check if a conflict exists between the given pair and the contents of the dictionary that, if inserted,
/// would override an existing pair or break the bijective property.
///
/// - Parameter pair: The left-right pair to perform the conflict check against.
/// - Returns: The conflict, if one exists, or `nil`, indicating neither the left nor right value already
/// exist in the dictionary.
/// - Complexity: O(1)
///
/// The following example demonstrates creating a dictionary mapping element symbols
/// to their atomic numbers and checking to see if a conflict exists:
/// ```swift
/// let dict: BijectiveDictionary = ["Ti": 22, "Si": 14, "He": 2]
///
/// guard let conflict = dict.conflict(for: ("Ti", 2)) else {
/// print("No conflict")
/// return
/// }
/// switch conflict {
/// case .left:
/// print("Symbol already present")
/// case .right:
/// print("Atomic number already present")
/// case .pair:
/// print("Pair already exists")
/// case .both(let otherSymbol, let otherNumber):
/// print("Other symbol: \(otherSymbol), other number: \(otherNumber)")
/// }
/// // prints "Other symbol: He, other number: 22"
/// ```
@inlinable
public func conflict(with pair: Element) -> Conflict? {
let existing = (findPairByLeft(pair.left), findPairByRight(pair.right))
return switch existing {
case (nil, nil): nil
case (nil, .some): .right
case (.some, nil): .left
case (.some(let byLeft), .some(let byRight)):
if byLeft.left != byRight.left || byLeft.right != byRight.right {
.both(otherLeft: byRight.left, otherRight: byLeft.right)
} else {
.pair
}
}
}
}
12 changes: 12 additions & 0 deletions Sources/BijectiveDictionary/BijectiveDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,18 @@ public struct BijectiveDictionary<Left: Hashable, Right: Hashable> {
}
}
}

/// Find a left-right pair in the dictionary by left value.
@inlinable public func findPairByLeft(_ leftValue: Left) -> Element? {
guard let rightValue = self[left: leftValue] else { return nil }
return (leftValue, rightValue)
}

/// Find a left-right pair in the dictionary by right value.
@inlinable public func findPairByRight(_ rightValue: Right) -> Element? {
guard let leftValue = self[right: rightValue] else { return nil }
return (leftValue, rightValue)
}
}

extension BijectiveDictionary: Encodable where Left: Encodable, Right: Encodable {
Expand Down
26 changes: 26 additions & 0 deletions Tests/BijectiveDictionaryTests/BijectiveDictionaryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -273,4 +273,30 @@ func testAThousand() {
#expect(bijectiveDict[right: rightV] == leftV)
}
}

@Test
func findPairByLeft() {
let dict: BijectiveDictionary = ["A": 1, "B": 2, "C": 3]
#expect(dict.findPairByLeft("A")?.left == "A")
#expect(dict.findPairByLeft("A")?.right == 1)
#expect(dict.findPairByLeft("D") == nil)
}

@Test
func findPairByRight() {
let dict: BijectiveDictionary = ["A": 1, "B": 2, "C": 3]
#expect(dict.findPairByRight(1)?.left == "A")
#expect(dict.findPairByRight(1)?.right == 1)
#expect(dict.findPairByRight(4) == nil)
}

@Test
func conflict() {
let dict: BijectiveDictionary = ["A": 1, "B": 2, "C": 3]
#expect(dict.conflict(with: ("D", 4)) == nil)
#expect(dict.conflict(with: ("A", 0)) == .left)
#expect(dict.conflict(with: ("E", 1)) == .right)
#expect(dict.conflict(with: ("A", 1)) == .pair)
#expect(dict.conflict(with: ("A", 3)) == .both(otherLeft: "C", otherRight: 1))
}
#endif