#ordinal #map #set-key #enums #set #integer #traits-structs

ordinal-map

Ordinal trait to map values to integers and efficient maps and sets for such types

6 releases

0.1.6 Jun 24, 2024
0.1.5 Jun 24, 2024

#708 in Data structures

MIT license

98KB
3K SLoC

Ordinal types and collections

The library provides Ordinal trait to map types to usize values, proc-macro to derive Ordinal trait for structs and enums, and map and set implementations that use these types as keys efficiently.

Example

use ordinal_map::map::total::OrdinalTotalMap;
#[derive(ordinal_map::Ordinal)]
enum ErrorCategory {
    Network,
    Disk,
    Logic,
}

fn classify_error(error: &str) -> ErrorCategory {
    // ...
}

let mut error_counts: OrdinalTotalMap<ErrorCategory, u64> = OrdinalTotalMap::default();

for error in &errors {
    let category = classify_error(error);
    error_counts[category] += 1;
}

lib.rs:

The library provides Ordinal trait to map types to usize values, proc-macro to derive Ordinal trait for structs and enums, and map and set implementations that use these types as keys efficiently.

Example

use ordinal_map::map::total::OrdinalTotalMap;
#[derive(ordinal_map::Ordinal)]
enum ErrorCategory {
    Network,
    Disk,
    Logic,
}

fn classify_error(error: &str) -> ErrorCategory {
    // ...
}

let mut error_counts: OrdinalTotalMap<ErrorCategory, u64> = OrdinalTotalMap::default();

for error in &errors {
    let category = classify_error(error);
    error_counts[&category] += 1;
}

Dependencies

~275–730KB
~17K SLoC