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

macro no-std array_map_derive

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

4 releases (breaking)

0.4.0 Mar 24, 2022
0.3.0 Jul 7, 2021
0.2.0 May 25, 2021
0.1.0 May 19, 2021

#133 in #fixed-size

Download history 420/week @ 2024-10-18 337/week @ 2024-10-25 374/week @ 2024-11-01 493/week @ 2024-11-08 478/week @ 2024-11-15 281/week @ 2024-11-22 355/week @ 2024-11-29 518/week @ 2024-12-06 582/week @ 2024-12-13 203/week @ 2024-12-20 31/week @ 2024-12-27 360/week @ 2025-01-03 551/week @ 2025-01-10 435/week @ 2025-01-17 618/week @ 2025-01-24 470/week @ 2025-01-31

2,153 downloads per month
Used in 3 crates (via array_map)

MIT/Apache

14KB
258 lines

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

~1.5MB
~37K SLoC