causal_hub/types/
states.rs

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