Skip to content
This repository has been archived by the owner on Feb 6, 2022. It is now read-only.

Swift bidirectional mapping providing O(1) lookup between values

License

Notifications You must be signed in to change notification settings

JadenGeller/BidirectionalMap

Repository files navigation

BidirectionalMap

BidirectionalMap<Left, Right> provides O(1) lookup of a Left value given a Right value and vice-versa. Essentially, BidirectionalMap is a bidirectional dictionary that creates a 1:1 mapping between elements.

let map: BidirectionalMap = ["seven" : 7, "three" : 3, "twenty" : 20]
print(map[7])       // -> seven
print(map["three"]) // -> 3

In addition to being initializable from a dictionary literal, BidirectionalMap can be initialized from any SequenceType whose element is (Left, Right) so hashable Left and Right.

let bleh = BidirectionalMap([("Jaden", 20), ("Brandon", 21), ("Alex", 19)])
let blah = BidirectionalMap([1 : 9, 2 : 8, 3 : 7, 4 : 6, 5 : 5])

BidirectionalMap includes many similiar functions and properties to Dictionary. For example, it includes a leftValues and rightValues property to obtain a sequence of the values for each type.

print(map.rightValues) // -> [7, 3, 20]

BidirectionalMap also includes mutating functions that allow adding and removing associations from the mapping.

var copy = map
copy.associateValues("five", 5)
copy.disassociateValues(right: 20)

Since BidirectionalMap conforms to CollectionType, it can be indexed as well.

let index = map.indexForValue(left: "seven")
print(map[index]) // -> ("seven", 7)

About

Swift bidirectional mapping providing O(1) lookup between values

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages