Created
November 2, 2014 14:54
-
-
Save aspitz/6e1b7e7251a1dab282a9 to your computer and use it in GitHub Desktop.
Swift implementation of a bidirectional dictionary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct BidirectionalDictionary<KeyType:Hashable, ValueType:Hashable>{ | |
var keyToValue = [KeyType:ValueType]() | |
var valueToKey = [ValueType:KeyType]() | |
subscript(key:KeyType) -> ValueType? { | |
get { | |
return self.keyToValue[key] | |
} | |
set { | |
self.keyToValue[key] = newValue | |
if let aValue = newValue { | |
self.valueToKey[aValue] = key | |
} | |
} | |
} | |
subscript(value:ValueType) -> KeyType? { | |
get { | |
return self.valueToKey[value] | |
} | |
set(key) { | |
self.valueToKey[value] = key | |
if let aKey = key { | |
self.keyToValue[aKey] = value | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment