Skip to content

Instantly share code, notes, and snippets.

@CMCDragonkai
Last active December 13, 2017 06:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CMCDragonkai/cf0c16d7f2bae2f78a168e0c97b85d54 to your computer and use it in GitHub Desktop.
Save CMCDragonkai/cf0c16d7f2bae2f78a168e0c97b85d54 to your computer and use it in GitHub Desktop.
Bidirectional Maps and their Variants

Bidirectional maps https://en.wikipedia.org/wiki/Bidirectional_map has extra variants:

  1. Bimap (key to single value, value to single key)
  2. Multivalued Bimap (key to multiple values, value to single key)
  3. Multikeyed Bimap (key to single value, value to multiple keys)
  4. Multikeyed Multivalued Bimap (key to multiple values, value to multiple keys)

Note that making array keys or array values are totally orthogonal to this system. Any "claimed" multimap that uses array keys or array values has nothing to do with this idea.

Although you can definitely make the value an array, it just means that the array is considered the value, not the individual elements.

Example implementation: https://codereview.stackexchange.com/questions/85842/a-dictionary-that-allows-multiple-keys-for-one-value this does Multikeyed Multivalued Bimap and https://stackoverflow.com/a/21894086/582917 which extends the bidict with multikeyed capability.

Note that the whole idea is that with multikeyed multivalued bimap, you can create a simple relational database table in-memory.

Ordering of the bimap variants depends on the underlying datastructure that is used to implement them. Remember you may want insertion order of keys or insertion order of values, or underlying order of the key, or underlying order of the value or some combination of orders.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment