aicad 1.0.0-rc.1

A library to solve Constraint satisfaction problems with Multi-valued decision diagrams and, optionnaly, neural networks
Documentation
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
//! A fixed-capacity set of small integers, stored one bit per element.
//!
//! # Meaning
//!
//! `Bitset::new(n)` holds the elements $0, \dots, n - 1$; it starts empty (all bits to 0).
//! Membership and insertion address a single element, the set operations work word by word:
//! [`union`](Bitset::union) and [`intersect`](Bitset::intersect) update `self` in place,
//! [`size_union`](Bitset::size_union) counts $|A \cup B|$ without building the union, and the
//! `*_with_and_bit` variants combine with `other` extended by one extra element. There is no
//! removal.
//!
//! Both operands of a set operation must have been created with the same `n`. Using an element
//! of $n$ or more, or combining sets of different sizes, results in a runtime error. The storage
//! is made of whole 64-bit words, so an element below the next multiple of 64 is not rejected,
//! and one beyond it panics.
//!
//! [`reset`](Bitset::reset) sets every *word* to the given value, not every element: `reset(0)`
//! empties the set and `reset(!0)` sets all bits of the last word too, including those at $n$ and
//! above.
//!
//! # How it works
//!
//! Element $e$ lives in bit $e \bmod 64$ of word $\lfloor e / 64 \rfloor$. Sizes up to 64
//! fit in one word, kept inline in the enum; larger ones use a vector of words. Both are viewed as
//! a slice of words, so every operation is the same loop over words whatever the representation.
//!
//! # Design notes
//!
//! - **One word is stored inline.** Constraints keep a bitset in the property of every node of
//!   the diagram, and they are often small (the values of a domain, the states of an automaton),
//!   so avoiding a heap allocation per node matters more than a uniform representation.
//! - **Equality and hashing look at the words.** Two bitsets are equal exactly when they have the
//!   same storage class and the same words, which is what lets properties be compared and used as
//!   merge keys.

use std::hash::Hash;

/// A set of small non-negative integers, stored as bits in 64-bit words.
///
/// A set that fits in one word is kept inline; a larger one is a vector of words. Which one a set
/// uses depends only on the capacity given to [`Bitset::new`].
#[derive(Clone, Debug, PartialEq, Eq, Hash, deepsize::DeepSizeOf)]
pub(crate) enum Bitset {
    /// A capacity of at most 64: the single word, without a heap allocation.
    Inline(u64),
    /// A larger capacity: one word per 64 elements.
    Heap(Vec<u64>),
}

impl Bitset {
    /// The empty set able to hold the elements `0..n`.
    pub(crate) fn new(n: usize) -> Self {
        let number_words = n.div_ceil(64).max(1);
        if number_words == 1 {
            Self::Inline(0)
        } else {
            Self::Heap(vec![0; number_words])
        }
    }

    /// The words of the set, lowest elements first.
    fn words(&self) -> &[u64] {
        match self {
            Bitset::Inline(w) => std::slice::from_ref(w),
            Bitset::Heap(v) => v.as_slice(),
        }
    }

    /// The words of the set, mutably.
    fn words_mut(&mut self) -> &mut [u64] {
        match self {
            Bitset::Inline(w) => std::slice::from_mut(w),
            Bitset::Heap(v) => v.as_mut_slice(),
        }
    }

    /// Whether `element` is in the set. Panics if it is beyond the capacity (rounded up to a
    /// multiple of 64).
    pub(crate) fn contains(&self, element: usize) -> bool {
        let word = element / 64;
        let shift = element % 64;
        self.words()[word] & (1 << shift) != 0
    }

    /// Adds `element` to the set. Panics if it is beyond the capacity (rounded up to a multiple of
    /// 64).
    pub(crate) fn insert(&mut self, element: usize) {
        let word = element / 64;
        let shift = element % 64;
        self.words_mut()[word] |= 1 << shift;
    }

    /// The number of elements in the set.
    pub(crate) fn size(&self) -> usize {
        self.words()
            .iter()
            .map(|word| word.count_ones())
            .sum::<u32>() as usize
    }

    /// The number of elements in the union of `self` and `other`, without building it. The two
    /// must have the same capacity.
    pub(crate) fn size_union(&self, other: &Bitset) -> usize {
        let other_words = other.words();
        self.words()
            .iter()
            .copied()
            .enumerate()
            .map(|(i, word)| (word | other_words[i]).count_ones())
            .sum::<u32>() as usize
    }

    /// Replaces `self` by its union with `other`. The two must have the same capacity.
    pub(crate) fn union(&mut self, other: &Bitset) {
        let other_words = other.words();
        let words = self.words_mut();
        debug_assert!(words.len() == other_words.len());
        for word in 0..words.len() {
            words[word] |= other_words[word]
        }
    }

    /// Replaces `self` by the union of `other` and the single element `bit`.
    pub(crate) fn union_with_and_bit(&mut self, other: &Bitset, bit: usize) {
        let bit_word = bit / 64;
        let bit_mask = 1u64 << (bit % 64);
        let other_words = other.words();
        for (i, (word, other_word)) in self
            .words_mut()
            .iter_mut()
            .zip(other_words.iter())
            .enumerate()
        {
            let extra = if i == bit_word { bit_mask } else { 0 };
            *word |= other_word | extra;
        }
    }

    /// Replaces `self` by its intersection with `other`. The two must have the same capacity.
    pub(crate) fn intersect(&mut self, other: &Bitset) {
        let other_words = other.words();
        let words = self.words_mut();
        debug_assert!(words.len() == other_words.len());
        for word in 0..words.len() {
            words[word] &= other_words[word]
        }
    }

    /// Replaces `self` by its intersection with the union of `other` and the single element
    /// `bit`.
    pub(crate) fn intersect_with_and_bit(&mut self, other: &Bitset, bit: usize) {
        let bit_word = bit / 64;
        let bit_mask = 1u64 << (bit % 64);
        let other_words = other.words();
        for (i, (word, other_word)) in self
            .words_mut()
            .iter_mut()
            .zip(other_words.iter())
            .enumerate()
        {
            let extra = if i == bit_word { bit_mask } else { 0 };
            *word &= other_word | extra;
        }
    }

    /// Sets every word to `value`: 0 empties the set, `u64::MAX` fills every bit of its words.
    pub(crate) fn reset(&mut self, value: u64) {
        for word in self.words_mut().iter_mut() {
            *word = value;
        }
    }

    /// The words of the set by value, lowest elements first.
    pub(crate) fn iter(&self) -> impl Iterator<Item = u64> {
        self.words().iter().copied()
    }
}

impl std::fmt::Display for Bitset {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        for word in self.words().iter() {
            write!(f, " {:b}", word)?;
        }
        write!(f, "")
    }
}

#[cfg(test)]
mod test_bitset {

    use super::Bitset;

    #[test]
    pub fn test_new_is_inline_for_small_n() {
        let b = Bitset::new(8);
        assert!(matches!(b, Bitset::Inline(_)));
    }

    #[test]
    pub fn test_new_is_inline_for_n_64() {
        let b = Bitset::new(64);
        assert!(matches!(b, Bitset::Inline(_)));
    }

    #[test]
    pub fn test_new_is_heap_for_n_65() {
        let b = Bitset::new(65);
        assert!(matches!(b, Bitset::Heap(_)));
    }

    #[test]
    pub fn test_new_with_zero_elements_is_inline() {
        let b = Bitset::new(0);
        assert!(matches!(b, Bitset::Inline(_)));
    }

    #[test]
    pub fn test_the_last_element_of_a_full_word_and_of_the_next_one_fit() {
        let mut inline = Bitset::new(64);
        inline.insert(63);
        assert!(inline.contains(63));
        let mut heap = Bitset::new(65);
        heap.insert(64);
        assert!(heap.contains(64));
    }

    #[test]
    pub fn test_new_is_heap_for_large_n() {
        let b = Bitset::new(200);
        assert!(matches!(b, Bitset::Heap(_)));
    }

    #[test]
    pub fn test_insert_and_contains_inline() {
        let mut b = Bitset::new(8);
        assert!(!b.contains(3));
        b.insert(3);
        assert!(b.contains(3));
        assert!(!b.contains(4));
    }

    #[test]
    pub fn test_insert_and_contains_heap_within_first_word() {
        let mut b = Bitset::new(200);
        b.insert(5);
        assert!(b.contains(5));
        assert!(!b.contains(6));
    }

    #[test]
    pub fn test_insert_and_contains_heap_crosses_word_boundary() {
        let mut b = Bitset::new(200);
        b.insert(64);
        b.insert(130);
        assert!(b.contains(64));
        assert!(b.contains(130));
        assert!(!b.contains(63));
        assert!(!b.contains(65));
    }

    #[test]
    pub fn test_size_inline() {
        let mut b = Bitset::new(8);
        assert_eq!(b.size(), 0);
        b.insert(0);
        b.insert(5);
        b.insert(7);
        assert_eq!(b.size(), 3);
    }

    #[test]
    pub fn test_size_heap() {
        let mut b = Bitset::new(200);
        b.insert(1);
        b.insert(64);
        b.insert(150);
        assert_eq!(b.size(), 3);
    }

    #[test]
    pub fn test_size_union_inline() {
        let mut a = Bitset::new(8);
        let mut b = Bitset::new(8);
        a.insert(1);
        a.insert(2);
        b.insert(2);
        b.insert(3);
        assert_eq!(a.size_union(&b), 3);
    }

    #[test]
    pub fn test_size_union_heap() {
        let mut a = Bitset::new(200);
        let mut b = Bitset::new(200);
        a.insert(1);
        a.insert(150);
        b.insert(150);
        b.insert(160);
        assert_eq!(a.size_union(&b), 3);
    }

    #[test]
    pub fn test_union_inline() {
        let mut a = Bitset::new(8);
        let mut b = Bitset::new(8);
        a.insert(1);
        b.insert(2);
        a.union(&b);
        assert!(a.contains(1));
        assert!(a.contains(2));
        assert_eq!(a.size(), 2);
    }

    #[test]
    pub fn test_union_heap() {
        let mut a = Bitset::new(200);
        let mut b = Bitset::new(200);
        a.insert(10);
        b.insert(140);
        a.union(&b);
        assert!(a.contains(10));
        assert!(a.contains(140));
        assert_eq!(a.size(), 2);
    }

    #[test]
    pub fn test_intersect_inline() {
        let mut a = Bitset::new(8);
        let mut b = Bitset::new(8);
        a.insert(1);
        a.insert(2);
        b.insert(2);
        b.insert(3);
        a.intersect(&b);
        assert!(!a.contains(1));
        assert!(a.contains(2));
        assert!(!a.contains(3));
        assert_eq!(a.size(), 1);
    }

    #[test]
    pub fn test_intersect_heap() {
        let mut a = Bitset::new(200);
        let mut b = Bitset::new(200);
        a.insert(10);
        a.insert(140);
        b.insert(140);
        b.insert(190);
        a.intersect(&b);
        assert!(!a.contains(10));
        assert!(a.contains(140));
        assert!(!a.contains(190));
    }

    #[test]
    pub fn test_union_with_and_bit_inline() {
        let mut a = Bitset::new(8);
        let b = Bitset::new(8);
        a.union_with_and_bit(&b, 5);
        assert!(a.contains(5));
        assert_eq!(a.size(), 1);
    }

    #[test]
    pub fn test_union_with_and_bit_heap() {
        let mut a = Bitset::new(200);
        let mut b = Bitset::new(200);
        b.insert(10);
        a.union_with_and_bit(&b, 140);
        assert!(a.contains(10));
        assert!(a.contains(140));
        assert_eq!(a.size(), 2);
    }

    #[test]
    pub fn test_intersect_with_and_bit_inline() {
        let mut a = Bitset::new(8);
        a.insert(3);
        a.insert(5);
        let b = Bitset::new(8);
        a.intersect_with_and_bit(&b, 5);
        assert!(!a.contains(3));
        assert!(a.contains(5));
    }

    #[test]
    pub fn test_intersect_with_and_bit_heap() {
        let mut a = Bitset::new(200);
        a.insert(10);
        a.insert(140);
        let b = Bitset::new(200);
        a.intersect_with_and_bit(&b, 140);
        assert!(!a.contains(10));
        assert!(a.contains(140));
    }

    #[test]
    pub fn test_reset_inline() {
        let mut b = Bitset::new(8);
        b.insert(3);
        b.reset(0);
        assert!(!b.contains(3));
        assert_eq!(b.size(), 0);
    }

    #[test]
    pub fn test_reset_heap() {
        let mut b = Bitset::new(200);
        b.insert(10);
        b.insert(140);
        b.reset(0);
        assert_eq!(b.size(), 0);
    }

    #[test]
    pub fn test_iter_word_count_inline() {
        let b = Bitset::new(8);
        assert_eq!(b.iter().count(), 1);
    }

    #[test]
    pub fn test_iter_word_count_heap() {
        let b = Bitset::new(200);
        assert_eq!(b.iter().count(), 4);
    }

    #[test]
    pub fn test_clone_and_eq() {
        let mut a = Bitset::new(200);
        a.insert(10);
        a.insert(140);
        let b = a.clone();
        assert_eq!(a, b);
    }

    #[test]
    pub fn test_display_does_not_panic() {
        let mut b = Bitset::new(200);
        b.insert(10);
        let _ = format!("{}", b);
    }
}