lattices/ght/
macros.rs

1//! Macros for GHT
2#[macro_export]
3/// Internal macro for constructing a Ght struct with the given schema and storage type
4///
5/// Should not be used directly, use `GhtType!` instead
6macro_rules! GhtTypeWithSchema {
7    // Empty key & Val (Leaf)
8    (() => () => $( $schema:ty ),+ : $storage:ident) => (
9        $crate::ght::GhtLeaf::<$( $schema ),*,  ()  >
10    );
11
12    // Empty key (Leaf)
13    (() => $( $z:ty ),* => $schema:ty : $storage:ident) => (
14        $crate::ght::GhtLeaf::<$schema,  $crate::variadics::var_type!($( $z ),*), $crate::variadics::variadic_collections::$storage<$schema> >
15    );
16
17    // Singleton key & Empty val (Inner over Leaf)
18    ($a:ty => () => $schema:ty : $storage:ident) => (
19        $crate::ght::GhtInner::<$a, $crate::ght::GhtLeaf::<$schema, (), $crate::variadics::variadic_collections::$storage<$schema> >>
20    );
21
22    // Singleton key (Inner over Leaf)
23    ($a:ty => $( $z:ty ),* => $schema:ty : $storage:ident) => (
24        $crate::ght::GhtInner::<$a, $crate::ght::GhtLeaf::<$schema, $crate::variadics::var_type!($( $z ),*), $crate::variadics::variadic_collections::$storage<$schema> >>
25    );
26
27    // Recursive case with empty val
28    ($a:ty, $( $b:ty ),* => () => $schema:ty : $storage:ident) => (
29        $crate::ght::GhtInner::<$a, $crate::GhtTypeWithSchema!($( $b ),* => () => $schema : $storage)>
30    );
31
32    // Recursive case
33    ($a:ty, $( $b:ty ),* => $( $z:ty ),* => $schema:ty : $storage:ident) => (
34        $crate::ght::GhtInner::<$a, $crate::GhtTypeWithSchema!($( $b ),* => $( $z ),* => $schema : $storage)>
35    );
36}
37
38#[macro_export]
39/// Public macro for constructing a Ght struct with the given schema and storage type
40///
41/// # Example
42/// ```
43/// use lattices::GhtType;
44/// use variadics::variadic_collections::VariadicHashSet;
45///
46/// // This generates a Ght struct with (u16, u32) as key, (u64) as val, and VariadicHashSet as storage
47/// type MyHashGht = GhtType!(u16, u32 => u64: VariadicHashSet);
48/// let my_ght = MyHashGht::default();
49///
50/// /// // This generates a Ght struct with (u16, u32) as key, () as val, and VariadicCountedHashSet as storage
51/// type MyMultisetGht = GhtType!(u16, u32 => (): VariadicCountedHashSet);
52/// let my_ght = MyMultisetGht::default();
53///
54/// // This generates a Ght struct with (u16, u32) as key, () as val, and VariadicColumnSet as storage
55/// type MyColumnarMultisetGht = GhtType!(u16, u32 => (): VariadicColumnMultiset);
56/// let my_ght = MyColumnarMultisetGht::default();
57/// ```
58macro_rules! GhtType {
59    // Empty key
60    (() => $( $z:ty ),*: $storage:ident) => (
61        $crate::GhtTypeWithSchema!(() => $( $z ),* => $crate::variadics::var_type!($( $z ),*): $storage)
62    );
63
64    // Recursive case empty val
65    ($( $b:ty ),* => (): $storage:ident) => (
66        $crate::GhtTypeWithSchema!($( $b ),* => () => $crate::variadics::var_type!($( $b ),*): $storage)
67    );
68
69    // Recursive case
70    ($( $b:ty ),* => $( $z:ty ),*: $storage:ident) => (
71        $crate::GhtTypeWithSchema!($( $b ),* => $( $z ),* => $crate::variadics::var_type!($( $b ),*, $( $z ),*): $storage)
72    );
73}
74
75#[macro_export]
76/// Construct a forest of Ghts (i.e. a ColtForest) with the given schema and storage type.
77///
78/// # Example
79/// ```
80/// use lattices::ColtType;
81///
82/// type MyColt = ColtType!(u16, u32, u64);
83/// ```
84macro_rules! ColtType {
85    // Base case: single type to empty
86    ($a:ty => ()) => {
87        $crate::variadics::var_type!($crate::GhtType!($a => (): VariadicColumnMultiset))
88    };
89    // Base case: single type to single type
90    ($a:ty => $c:ty) => {
91        ($crate::GhtType!($a => $c: VariadicColumnMultiset), $crate::ColtType!($a, $c => ()))
92    };
93    // Recursive case: single type to multiple types
94    ($a:ty => $c:ty, $( $d:ty ),*) => {
95        ($crate::GhtType!($a => $c, $( $d ),*: VariadicColumnMultiset), $crate::ColtType!($a, $c => $( $d ),*))
96    };
97    // Base case: multiple types to empty
98    ($a:ty, $( $b:ty ),* => ()) => {
99        $crate::variadics::var_type!($crate::GhtType!($a, $( $b ),* => (): VariadicColumnMultiset))
100    };
101    // Base case: multiple types to single type
102    ($a:ty, $( $b:ty ),* => $c:ty) => {
103        ($crate::GhtType!($a, $( $b ),* => $c: VariadicColumnMultiset), $crate::ColtType!($a, $( $b ),*, $c => ()))
104    };
105    // Recursive case: multiple types to multiple types
106    ($a:ty, $( $b:ty ),* => $c:ty, $( $d:ty ),*) => {
107        ($crate::GhtType!($a, $( $b ),* => $c, $( $d ),*: VariadicColumnMultiset), $crate::ColtType!($a, $( $b ),*, $c => $( $d ),*))
108    };
109    // General case: single type
110    ($a:ty) => {
111        ($crate::GhtType!(() => $a: VariadicColumnMultiset), $crate::ColtType!($a => ()))
112    };
113    // General case: multiple types
114    ($a:ty, $( $b:ty ),*) => {
115        ($crate::GhtType!(() => $a, $( $b ),*: VariadicColumnMultiset), $crate::ColtType!($a => $( $b ),*))
116    };
117}