#hash-map #map #set #items #lookup #maps #cycle

cycle_map

Implementations of bi-directionally accessible hashmaps

3 unstable releases

0.2.0 Sep 27, 2023
0.1.1 May 6, 2022
0.1.0 Apr 24, 2022

#1748 in Data structures

Download history 6/week @ 2023-12-28 9/week @ 2024-02-01 1/week @ 2024-02-08 40/week @ 2024-02-15 30/week @ 2024-02-22 8/week @ 2024-02-29 10/week @ 2024-03-07 5/week @ 2024-03-14 23/week @ 2024-03-28 29/week @ 2024-04-04

52 downloads per month

LGPL-3.0

205KB
3.5K SLoC

Crates.io Documentation GitHub Workflows Coverage Status Maintenance

About

CycleMap provides several "double-sided" hash maps that associate items between two sets. It does this without keep duplicate data and while maintaining lookup speeds on par with the standard HashMap.

There are many ways that you might want to map items between sets. CycleMap supports three.

  • Bijective maps: every item in both sets is paired, and paired with exactly one other item
  • Partial bijective maps: items in either set don't have to be paired, but any paired item is paired with exactly one other item
  • (Weakly) Surjective maps: every item in the left set is paired with exactly one right item, and items in the right set can be paired with multiple (or no) items from the left set

How it Works

Each map contains two sets, a left set and a right set. When two items are paired, they form a cycle. That is, an item from either set can be used to uniquely lookup the item(s) it is paired with. This forms a cyclic lookup and is the core algorithm that underlies all cycle maps.

Instead of holding self-referential pointers, all cycle maps pair an item with the hash of its partner item (and an id). This prevents possible unforeseen memory bugs and makes map resizing faster since an item doesn't need to "know" where its partner is but rather how to get there.

Why use

Cycle maps aren't meant to replace the standard hashmap. In fact, they are built on top of it. Rather, they provide a clean solution to fast, bi-direction lookups.

If you find yourself creating workarounds to these sorts of problems, give a cycle map a try.

Contribution

If you want to contribute to or improve upon this library, please do so. Fork this project or submit an issue or a pull request for a feature/fix/change/etc. All that I ask is for derived/iterative libraries to be open and free to use and ideally with the same license (LGPL v3). Any other application or library that uses this library can use any license.

Future Plans

The CycleMap struct has a counterpart, PartialCycleMap. This struct looses the requirements of the CycleMap by allowing unpaired items in either set. A similar PartialGroupMap struct is planned to mirror the GroupMap struct.

Dependencies

~0.8–1.2MB
~19K SLoC