1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
use core::marker::PhantomData;

use super::cast::Cast;

/// A bit subset of `u64`.
///
/// This structure represents a subset of bits within a 64-bit unsigned integer,
/// defined by a specific pattern where each bit is either `0`, `1`, or `S`.
///
/// - `0` means the bit is always `0`. These bits contribute to both the tag and the mask.
/// - `1` means the bit is always `1`. These bits also contribute to both the tag and the mask.
/// - `S` represents a superposition state, meaning the bit can be either `0` or `1`.
///
/// The cardinality of the set is calculated as `2^(superposition.count_ones())`, representing
/// the number of unique combinations possible within the superposition bits.
///
/// The following table summarizes how each field is derived:
///
/// |Property      | 0 | 1 | S | Description        |
/// |--------------|---|---|---|--------------------|
/// | union        | 0 | 1 | 1 | items.reduce(or)   |
/// | tag          | 0 | 1 | 0 | items.reduce(and)  |
/// | superposition| 0 | 0 | 1 | union ^ tag        |
/// | mask         | 1 | 1 | 0 | !superposition     |
pub struct BitSubset64<T: Cast<u64> = u64>
where
    u64: Cast<T>,
{
    /// Represents the intersection of all items in the subset. A pattern of bits
    /// where a `1` in each position indicates that the corresponding bit is consistently `1`
    /// across all items, and a `0` indicates that it is not consistently `1`.
    pub tag: u64,
    /// Identifies the bits that are constant (either `0` or `1`). A `1` in a position
    /// indicates a fixed bit (as per the `tag`), and a `0` indicates a superposition bit.
    pub mask: u64,
    _0: PhantomData<T>,
}

impl<T: Cast<u64>> Clone for BitSubset64<T>
where
    u64: Cast<T>,
{
    #[inline(always)]
    fn clone(&self) -> Self {
        *self
    }
}

impl<T: Cast<u64>> Copy for BitSubset64<T> where u64: Cast<T> {}

impl BitSubset64<u64> {
    #[inline(always)]
    pub const fn cast<U: Cast<u64>>(self) -> BitSubset64<U>
    where
        u64: Cast<U>,
    {
        BitSubset64 {
            tag: self.tag,
            mask: self.mask,
            _0: PhantomData,
        }
    }
}

impl<T: Cast<u64>> BitSubset64<T>
where
    u64: Cast<T>,
{
    #[inline(always)]
    pub const fn from_tag_and_mask(tag: u64, mask: u64) -> Self {
        assert!(mask & tag == tag);
        Self {
            tag,
            mask,
            _0: PhantomData,
        }
    }
    #[inline(always)]
    pub const fn from_tag_and_superposition(tag: u64, sup: u64) -> Self {
        Self::from_tag_and_mask(tag, !sup)
    }
    #[inline(always)]
    pub const fn from_tag_and_union(tag: u64, union: u64) -> Self {
        Self::from_tag_and_superposition(tag, tag ^ union)
    }
    #[inline(always)]
    pub const fn from_tag(tag: u64) -> Self {
        Self::from_tag_and_mask(tag, tag)
    }
    #[inline(always)]
    pub const fn has(self, value: u64) -> bool {
        value & self.mask == self.tag
    }
    #[inline(always)]
    pub const fn union(self) -> u64 {
        self.tag ^ self.superposition()
    }
    #[inline(always)]
    pub const fn superposition(self) -> u64 {
        !self.mask
    }
    #[inline(always)]
    pub const fn or_unchecked(self, b: Self) -> Self {
        Self::from_tag_and_union(self.tag & b.tag, self.union() | b.union())
    }
    #[inline(always)]
    pub const fn or(self, b: Self) -> Self {
        assert!(self.superposition() == b.superposition());
        self.or_unchecked(b)
    }
    #[inline(always)]
    pub const fn and(self, b: Self) -> Self {
        Self::from_tag_and_union(self.tag | b.tag, self.union() & b.union())
    }
    #[inline(always)]
    pub const fn split(self, sub_mask: u64) -> (Self, Self) {
        // we need at least one bit to distinguish the two subsets.
        assert!(sub_mask != 0);
        // the bit shouldn't be a part of the original set mask.
        assert!(sub_mask & self.mask == 0);
        let mask = self.mask | sub_mask;
        // the subsets should have different tags.
        (
            Self::from_tag_and_mask(self.tag, mask),
            Self::from_tag_and_mask(self.tag | sub_mask, mask),
        )
    }
    #[inline(always)]
    const fn subset_value_to_raw_value(self, set: u64) -> u64 {
        self.superposition() & set
    }
    #[inline(always)]
    pub const fn raw_value_to_subset_value(self, value: u64) -> u64 {
        self.tag | value
    }
    #[inline(always)]
    pub fn typed_value_to_subset_value(self, value: T) -> u64 {
        self.raw_value_to_subset_value(value.cast())
    }
    #[inline(always)]
    pub fn subset_value_to_typed_value(self, set: u64) -> T {
        (self.subset_value_to_raw_value(set)).cast()
    }
}

#[cfg(test)]
mod test {
    use wasm_bindgen_test::wasm_bindgen_test;

    use super::BitSubset64;

    const A: BitSubset64 = BitSubset64::from_tag_and_union(0b010, 0b011);
    const _: () = assert!(A.superposition() == 0b001);
    const _: () = assert!(A.tag == 0b010);
    const _: () = assert!(!A.has(0b000));
    const _: () = assert!(A.has(0b010));
    const _: () = assert!(A.has(0b011));

    const _AS: (BitSubset64, BitSubset64) = A.split(1);
    const _: () = assert!(_AS.0.tag == 0b010);
    const _: () = assert!(_AS.0.superposition() == 0);
    const _: () = assert!(_AS.1.tag == 0b011);
    const _: () = assert!(_AS.1.superposition() == 0);

    #[test]
    #[wasm_bindgen_test]
    fn test_a() {
        assert_eq!(A.superposition(), 0b001);
        assert_eq!(A.tag, 0b010);
        assert!(!A.has(0b000));
        assert!(A.has(0b010));
        assert!(A.has(0b011));
    }

    const B: BitSubset64 = BitSubset64::from_tag_and_union(0b000110, 0b000111);
    const C: BitSubset64 = BitSubset64::from_tag_and_union(0b010100, 0b011111);
    const UBC: BitSubset64 = B.or_unchecked(C);
    const _: () = assert!(UBC.superposition() == 0b011011);
    const _: () = assert!(UBC.tag == 0b000100);
    const _: () = assert!(UBC.union() == 0b011111);

    const _UBCS: (BitSubset64, BitSubset64) = UBC.split(0b1000);
    const _: () = assert!(_UBCS.0.superposition() == 0b010011);
    const _: () = assert!(_UBCS.0.tag == 0b000100);
    const _: () = assert!(_UBCS.1.tag == 0b001100);

    #[test]
    #[wasm_bindgen_test]
    fn test_ubc() {
        assert_eq!(UBC.superposition(), 0b011011);
        assert_eq!(UBC.tag, 0b000100);
        assert_eq!(UBC.union(), 0b011111);
    }

    #[test]
    #[wasm_bindgen_test]
    #[should_panic]
    fn test_ibc() {
        B.and(C);
    }

    #[test]
    #[wasm_bindgen_test]
    #[should_panic]
    fn test_split_fail() {
        UBC.split(0b100);
    }

    const D: BitSubset64 = BitSubset64::from_tag_and_union(0b00110, 0b00111);
    const E: BitSubset64 = BitSubset64::from_tag_and_union(0b00100, 0b01111);
    const UDE: BitSubset64 = D.or_unchecked(E);
    const _: () = assert!(UDE.superposition() == 0b01011);
    const _: () = assert!(UDE.tag == 0b00100);
    const _: () = assert!(UDE.union() == 0b01111);
    const IDE: BitSubset64 = D.and(E);
    const _: () = assert!(IDE.superposition() == 0b00001);
    const _: () = assert!(IDE.tag == 0b00110);
    const _: () = assert!(IDE.union() == 0b00111);

    #[test]
    #[wasm_bindgen_test]
    fn test_ude() {
        assert_eq!(UDE.superposition(), 0b01011);
        assert_eq!(UDE.tag, 0b00100);
        assert_eq!(UDE.union(), 0b01111);
    }

    #[test]
    #[wasm_bindgen_test]
    fn test_ide() {
        assert_eq!(IDE.superposition(), 0b00001);
        assert_eq!(IDE.tag, 0b00110);
        assert_eq!(IDE.union(), 0b00111);
    }
}