1 unstable release

new 0.1.0 Dec 16, 2024

#650 in Data structures

MIT/Apache

28KB
598 lines

MuleMap<🫏,🗺>

ci

MuleMap is a hybrid between a HashMap and a lookup table. Use MuleMap when the majority of your map accesses use keys that are near a fixed point n (by default we assume for keys near 0). MuleMap tries to match the API of the standard library HashMap (where possible) - making it mostly a drop in replacement for HashMap.

Example

use MuleMap;

type Hash = fnv_rs::FnvBuildHasher;  // Use whatever hash function you prefer
let mut mule_map = MuleMap::<u32, usize, Hash>::new();

assert_eq!(mule_map.get(5), None);
let entry = mule_map.entry(5);
entry.or_insert(10);
assert_eq!(mule_map.get(5), Some(&10));

Limitations

  • Only supports keys that are primitive integer types (u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, and isize). Non-primitive keys could be added if demand is seen, otherwise not likely.
  • Does not support automatically converting enum's with primitive representations. Likely to be added in the future.
  • Currently the type of a const generic can't depend on another generic type argument, so TABLE_MIN_VALUE and TABLE_MAX_VALUE can't use the same type as the key. Because of this, I am using i128, but that means we can't represent values near u128::MAX. Hopefully having frequent keys near u128::MAX is extremely rare.
  • Currently requires std, but if there is demand it would not be hard to make no_std.

Micro Benchmarks

violin lines

Dependencies

~0.3–0.8MB
~19K SLoC