diff --git a/Package.swift b/Package.swift index 2608009..d18ce1c 100644 --- a/Package.swift +++ b/Package.swift @@ -1,11 +1,14 @@ -// swift-tools-version:5.7.3 +// swift-tools-version:5.9 // The swift-tools-version declares the minimum version of Swift required to build this package. import PackageDescription let package = Package( name: "Abacus", - platforms: [.macOS(.v13)], + platforms: [ + .iOS(.v16), + .macOS(.v14) + ], products: [ // Products define the executables and libraries produced by a package, and make them visible to other packages. .library( diff --git a/Sources/Abacus/Bijection.swift b/Sources/Abacus/Bijection.swift index f5a0aa2..ba0557d 100644 --- a/Sources/Abacus/Bijection.swift +++ b/Sources/Abacus/Bijection.swift @@ -12,6 +12,16 @@ public class Bijection where S: Hashable, T: Hashable var leftward: [T: S] = [:] var rightward: [S: T] = [:] + public init() + { + } + + public init(leftward: [T : S], rightward: [S : T]) + { + self.leftward = leftward + self.rightward = rightward + } + public func set(_ s: S, _ t: T) throws { guard !rightward.keys.contains(s) else diff --git a/Sources/Abacus/OrderedDictionary.swift b/Sources/Abacus/OrderedDictionary.swift index 2da1795..020ceee 100644 --- a/Sources/Abacus/OrderedDictionary.swift +++ b/Sources/Abacus/OrderedDictionary.swift @@ -7,7 +7,7 @@ import Foundation -public class OrderedDictionary where Key: Hashable +public class OrderedDictionary where Key: Equatable, Key: Hashable, Value: Equatable, Value: Hashable { public typealias Key = Key public typealias Value = Value @@ -32,30 +32,30 @@ public class OrderedDictionary where Key: Hashable } } - func set(key: Key, value: Value) + public func set(key: Key, value: Value) { self.dictionary[key] = value self.orderedKeys.add(key) } - func get(key: Key) -> Value? + public func get(key: Key) -> Value? { return self.dictionary[key] } - func remove(key: Key) + public func remove(key: Key) { guard self.orderedKeys.contains(key) else {return} self.dictionary.removeValue(forKey: key) self.orderedKeys.remove(key) } - func keys() -> [Key] + public func keys() -> [Key] { return self.orderedKeys.array } - func set(index: Int, value: Value) -> Bool + public func set(index: Int, value: Value) -> Bool { guard let key = self.orderedKeys.get(index) else {return false} self.dictionary[key] = value @@ -63,13 +63,13 @@ public class OrderedDictionary where Key: Hashable return true } - func get(index: Int) -> Value? + public func get(index: Int) -> Value? { guard let key = self.orderedKeys.get(index) else {return nil} return self.dictionary[key] } - func remove(index: Int) -> Bool + public func remove(index: Int) -> Bool { guard let key = self.orderedKeys.get(index) else {return false} self.dictionary.removeValue(forKey: key) @@ -78,7 +78,7 @@ public class OrderedDictionary where Key: Hashable return true } - func values() -> [Value] + public func values() -> [Value] { return self.orderedKeys.array.compactMap { @@ -88,3 +88,36 @@ public class OrderedDictionary where Key: Hashable } } } + +extension OrderedDictionary: Equatable +{ + public static func ==(lhs: OrderedDictionary, rhs: OrderedDictionary) -> Bool + { + guard lhs.orderedKeys == rhs.orderedKeys else + { + return false + } + + for key in lhs.orderedKeys.array + { + let lvalue = lhs.dictionary[key] + let rvalue = rhs.dictionary[key] + + guard lvalue == rvalue else + { + return false + } + } + + return true + } +} + +extension OrderedDictionary: Hashable +{ + public func hash(into hasher: inout Hasher) + { + hasher.combine(self.dictionary) + hasher.combine(self.orderedKeys) + } +} diff --git a/Sources/Abacus/OrderedSet.swift b/Sources/Abacus/OrderedSet.swift index b5830a6..5018409 100644 --- a/Sources/Abacus/OrderedSet.swift +++ b/Sources/Abacus/OrderedSet.swift @@ -57,3 +57,11 @@ extension OrderedSet: Equatable return lhs.array == rhs.array } } + +extension OrderedSet: Hashable +{ + public func hash(into hasher: inout Hasher) + { + hasher.combine(self.array) + } +}