#fixed-size #map #structure #set-key #data

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

#1161 in Data structures

Download history 600/week @ 2024-07-23 553/week @ 2024-07-30 338/week @ 2024-08-06 540/week @ 2024-08-13 408/week @ 2024-08-20 355/week @ 2024-08-27 352/week @ 2024-09-03 416/week @ 2024-09-10 509/week @ 2024-09-17 554/week @ 2024-09-24 504/week @ 2024-10-01 341/week @ 2024-10-08 472/week @ 2024-10-15 435/week @ 2024-10-22 373/week @ 2024-10-29 303/week @ 2024-11-05

1,640 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

~0–295KB