Skip to main content

causal_hub/types/
states.rs

1use indexmap::{IndexMap, IndexSet};
2
3/// A type alias for a hash map with a fast hash function.
4pub type Map<K, V> = IndexMap<K, V, FxBuildHasher>;
5/// A type alias for a hash set with a fast hash function.
6pub type Set<T> = IndexSet<T, FxBuildHasher>;
7/// A type alias for a set of labels, which are strings.
8pub type Labels = Set<String>;
9
10pub use fxhash::FxBuildHasher;
11
12/// Create a `Set` from a list of values.
13#[macro_export]
14macro_rules! set {
15    [] => { $crate::types::Set::default() };
16    [$($value:expr,)+] => { $crate::set!($($value),+) };
17    [$($value:expr),*] => {
18        {
19            const CAPACITY: usize = <[()]>::len(&[$({ stringify!($value); }),*]);
20            let mut set = $crate::types::Set::with_capacity_and_hasher(
21                CAPACITY,
22                fxhash::FxBuildHasher::default(),
23            );
24            $(
25                set.insert($value);
26            )*
27            set
28        }
29    };
30}
31
32/// Create a `Labels` set from a list of string-like values.
33#[macro_export]
34macro_rules! labels {
35    [] => { $crate::types::Labels::default() };
36    [$($label:expr),+ $(,)?] => {
37        {
38            const CAPACITY: usize = <[()]>::len(&[$({ stringify!($label); }),*]);
39            let mut labels = $crate::types::Labels::with_capacity_and_hasher(
40                CAPACITY,
41                fxhash::FxBuildHasher::default(),
42            );
43            $(
44                labels.insert(::std::string::ToString::to_string(&$label));
45            )*
46            labels
47        }
48    };
49}
50
51/// Create a `Map` from a list of key-value pairs.
52#[macro_export]
53macro_rules! map {
54    [] => { $crate::types::Map::default() };
55    [$(($key:expr, $value:expr),)+] => { $crate::map!($(($key, $value)),+) };
56    [$(($key:expr, $value:expr)),*] => {
57        {
58            const CAPACITY: usize = <[()]>::len(&[$({ stringify!($key); stringify!($value); }),*]);
59            let mut map = $crate::types::Map::with_capacity_and_hasher(
60                CAPACITY,
61                fxhash::FxBuildHasher::default(),
62            );
63            $(
64                map.insert($key, $value);
65            )*
66            map
67        }
68    };
69}
70
71/// Create a `CatSupport` map from a list of variable-value pairs.
72#[macro_export]
73macro_rules! support {
74    [] => { $crate::models::CatSupport::default() };
75    [$(($label:expr, [$($value:expr),* $(,)?]),)+] => { $crate::support!($(($label, [$($value),*])),+) };
76    [$(($label:expr, [$($value:expr),* $(,)?])),*] => {
77        {
78            const CAPACITY: usize = <[()]>::len(&[$({ stringify!($label); $( stringify!($value); )* }),*]);
79            let mut support: $crate::models::CatSupport = $crate::types::Map::with_capacity_and_hasher(
80                CAPACITY,
81                $crate::types::FxBuildHasher::default(),
82            );
83            $(
84                // Convert the label to a string.
85                let label = ::std::string::ToString::to_string(&$label);
86                // Create a set of values for the current label.
87                let value = $crate::set![$(
88                    ::std::string::ToString::to_string(&$value)
89                ),*];
90                // Insert the label and the corresponding set of values into the map.
91                support.insert(label, value);
92            )*
93            support
94        }
95    };
96}