#map #fixed-size #structure #data #no-std

no-std array_map

Map backed array for fixed size keys with O(1) performance

10 unstable releases (3 breaking)

0.4.0 Mar 24, 2022
0.3.4 Sep 27, 2021
0.3.2 Aug 31, 2021
0.3.0 Jul 7, 2021
0.1.0 May 19, 2021

#782 in Data structures

Download history 20/week @ 2023-12-07 27/week @ 2023-12-14 10/week @ 2023-12-21 40/week @ 2023-12-28 233/week @ 2024-01-04 60/week @ 2024-01-11 53/week @ 2024-01-18 87/week @ 2024-01-25 231/week @ 2024-02-01 176/week @ 2024-02-08 142/week @ 2024-02-15 195/week @ 2024-02-22 171/week @ 2024-02-29 103/week @ 2024-03-07 202/week @ 2024-03-14 422/week @ 2024-03-21

912 downloads per month

MIT/Apache

60KB
1.5K SLoC

array_map

no_std compatible Map and Set backed by arrays.

This crate will evolve as more const-generic features become available.

This is especially useful if you have a bare enum where you want to treat each key as a field

use array_map::*;
#[repr(u8)]
#[derive(Indexable)]
enum DetectionType {
  Person,
  Vehicle,
  Bicycle,
}
let thresholds = ArrayMap::<DetectionType, f32, {DetectionType::count()}>::from_closure(|dt| match dt {
    DetectionType::Person => 0.8,
    DetectionType::Vehicle => 0.9,
    DetectionType::Bicycle => 0.7,
  });
let person_threshold = thresholds[DetectionType::Person];

This can also be used to memoize some common computations. (this is 2x as fast as doing the computation on aarch64)

use array_map::*;
let u8_to_f32_cache = ArrayMap::<u8, f32, {u8::SIZE}>::from_closure(|u|f32::from(*u) / 255.0);
let bytes = vec![0_u8; 1024];
// take some bytes and convert them to f32
let floats = bytes.iter().copied().map(|b|u8_to_f32_cache[b]).collect::<Vec<_>>();

Dependencies

~180KB