sicada 0.1.0

A weighted finite-state transducer (WFST) library, file-compatible with OpenFst
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
// TODO(porting-iteration): drop this once the porting iteration reaches this file.
#![allow(unused)]

use std::iter;
use std::slice;

type Word = u64;
const WORD_BYTES: usize = size_of::<Word>();
const WORD_BITS: usize = WORD_BYTES * 8;

#[inline(always)]
fn num_words(domain_size: usize) -> usize {
    domain_size.div_ceil(WORD_BITS)
}

#[inline(always)]
fn word_index_and_mask(index: usize) -> (usize, Word) {
    (index / WORD_BITS, 1 << (index % WORD_BITS))
}

/// Clears any bits outside the domain in the final word.
/// This ensures operations like `count` or equality checks evaluate correctly.
#[inline]
fn clear_excess_bits_in_final_word(domain_size: usize, words: &mut [Word]) {
    let num_bits_in_final_word = domain_size % WORD_BITS;
    if num_bits_in_final_word > 0
        && let Some(last) = words.last_mut()
    {
        let mask = (1 << num_bits_in_final_word) - 1;
        *last &= mask;
    }
}

/// Applies a bitwise operation to two word slices.
/// Returns `true` if `lhs` was modified.
///
/// Panics if `lhs` and `rhs` have different lengths.
#[inline]
fn update_words<Op>(lhs: &mut [Word], rhs: &[Word], op: Op) -> bool
where
    Op: Fn(Word, Word) -> Word,
{
    assert_eq!(lhs.len(), rhs.len());
    let mut changed = 0;
    for (lhs_word, &rhs_word) in iter::zip(lhs, rhs) {
        let old = *lhs_word;
        let new = op(old, rhs_word);
        *lhs_word = new;
        changed |= old ^ new;
    }
    changed != 0
}

/// A trait for performing set operations on bitsets.
pub trait BitRelations<Rhs> {
    /// Sets `self = self | other`. Returns `true` if `self` was modified.
    fn union(&mut self, other: &Rhs) -> bool;
    /// Sets `self = self - other`. Returns `true` if `self` was modified.
    fn subtract(&mut self, other: &Rhs) -> bool;
    /// Sets `self = self & other`. Returns `true` if `self` was modified.
    fn intersect(&mut self, other: &Rhs) -> bool;
}

macro_rules! bit_relations_inherent_impls {
    () => {
        #[inline]
        pub fn union<Rhs>(&mut self, other: &Rhs) -> bool
        where
            Self: BitRelations<Rhs>,
        {
            <Self as BitRelations<Rhs>>::union(self, other)
        }

        #[inline]
        pub fn subtract<Rhs>(&mut self, other: &Rhs) -> bool
        where
            Self: BitRelations<Rhs>,
        {
            <Self as BitRelations<Rhs>>::subtract(self, other)
        }

        #[inline]
        pub fn intersect<Rhs>(&mut self, other: &Rhs) -> bool
        where
            Self: BitRelations<Rhs>,
        {
            <Self as BitRelations<Rhs>>::intersect(self, other)
        }
    };
}

/// A fixed-capacity bitset backed by a dense array of words.
///
/// Memory is allocated once at creation. Operations that attempt to access
/// indices greater than or equal to the domain size will panic.
#[derive(Default, Clone, Debug, PartialEq, Eq, Hash)]
pub struct DenseBitSet {
    domain_size: usize,
    words: Vec<Word>,
}

impl DenseBitSet {
    /// Creates a new, empty bitset with the specified fixed capacity.
    #[inline]
    pub fn new_empty(domain_size: usize) -> Self {
        Self {
            domain_size,
            words: vec![0; num_words(domain_size)],
        }
    }

    /// Creates a new bitset with all bits up to `domain_size` set to 1.
    #[inline]
    pub fn new_filled(domain_size: usize) -> Self {
        let mut result = Self {
            domain_size,
            words: vec![!0; num_words(domain_size)],
        };
        clear_excess_bits_in_final_word(domain_size, &mut result.words);
        result
    }

    /// Returns the maximum capacity of the bitset.
    #[inline]
    pub fn domain_size(&self) -> usize {
        self.domain_size
    }

    /// Returns the number of set bits.
    #[inline]
    pub fn count(&self) -> usize {
        self.words.iter().map(|w| w.count_ones() as usize).sum()
    }

    /// Returns `true` if the bitset contains no elements.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.words.iter().all(|&w| w == 0)
    }

    /// Clears all elements in the bitset.
    #[inline]
    pub fn clear(&mut self) {
        self.words.fill(0);
    }

    /// Returns `true` if the bitset contains `index`.
    ///
    /// Panics if `index` is greater than or equal to the domain size.
    #[inline]
    pub fn contains(&self, index: usize) -> bool {
        assert!(index < self.domain_size, "Index out of bounds");
        let (word_index, mask) = word_index_and_mask(index);
        (self.words[word_index] & mask) != 0
    }

    /// Inserts `index` into the bitset. Returns `true` if the bit was newly inserted.
    ///
    /// Panics if `index` is greater than or equal to the domain size.
    #[inline]
    pub fn insert(&mut self, index: usize) -> bool {
        assert!(index < self.domain_size, "Index out of bounds");
        let (word_index, mask) = word_index_and_mask(index);
        let word = &mut self.words[word_index];
        let old = *word;
        *word |= mask;
        old != *word
    }

    /// Removes `index` from the bitset. Returns `true` if the bit was present.
    ///
    /// Panics if `index` is greater than or equal to the domain size.
    #[inline]
    pub fn remove(&mut self, index: usize) -> bool {
        assert!(index < self.domain_size, "Index out of bounds");
        let (word_index, mask) = word_index_and_mask(index);
        let word = &mut self.words[word_index];
        let old = *word;
        *word &= !mask;
        old != *word
    }

    /// Returns an iterator over the indices of set bits in ascending order.
    #[inline]
    pub fn iter(&self) -> BitIter<'_> {
        BitIter::new(&self.words)
    }

    bit_relations_inherent_impls! {}
}

impl BitRelations<DenseBitSet> for DenseBitSet {
    fn union(&mut self, other: &DenseBitSet) -> bool {
        assert_eq!(self.domain_size, other.domain_size);
        update_words(&mut self.words, &other.words, |a, b| a | b)
    }

    fn subtract(&mut self, other: &DenseBitSet) -> bool {
        assert_eq!(self.domain_size, other.domain_size);
        update_words(&mut self.words, &other.words, |a, b| a & !b)
    }

    fn intersect(&mut self, other: &DenseBitSet) -> bool {
        assert_eq!(self.domain_size, other.domain_size);
        update_words(&mut self.words, &other.words, |a, b| a & b)
    }
}

/// A dynamic bitset that automatically expands its capacity.
///
/// It wraps a `DenseBitSet` and resizes the underlying storage when an
/// operation requires an index outside the current domain size.
#[derive(Default, Clone, Debug, PartialEq, Eq, Hash)]
pub struct GrowableBitSet {
    bit_set: DenseBitSet,
}

impl GrowableBitSet {
    /// Creates a new, empty growable bitset.
    #[inline]
    pub fn new() -> Self {
        Self {
            bit_set: DenseBitSet::new_empty(0),
        }
    }

    /// Creates a new, empty growable bitset with the specified initial capacity.
    #[inline]
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            bit_set: DenseBitSet::new_empty(capacity),
        }
    }

    /// Ensures that the bitset can hold at least `min_domain_size` elements.
    #[inline]
    pub fn ensure(&mut self, min_domain_size: usize) {
        if self.bit_set.domain_size < min_domain_size {
            self.bit_set.domain_size = min_domain_size;
            let required_words = num_words(min_domain_size);
            if self.bit_set.words.len() < required_words {
                self.bit_set.words.resize(required_words, 0);
            }
        }
    }

    /// Inserts `index` into the bitset, resizing if necessary.
    /// Returns `true` if the bit was newly inserted.
    #[inline]
    pub fn insert(&mut self, index: usize) -> bool {
        self.ensure(index + 1);
        self.bit_set.insert(index)
    }

    /// Removes `index` from the bitset. Returns `true` if the bit was present.
    #[inline]
    pub fn remove(&mut self, index: usize) -> bool {
        if index >= self.bit_set.domain_size {
            return false;
        }
        self.bit_set.remove(index)
    }

    /// Returns `true` if the bitset contains `index`.
    #[inline]
    pub fn contains(&self, index: usize) -> bool {
        if index >= self.bit_set.domain_size {
            return false;
        }
        self.bit_set.contains(index)
    }

    /// Clears all elements in the bitset.
    #[inline]
    pub fn clear(&mut self) {
        self.bit_set.clear();
    }

    /// Returns the number of set bits.
    #[inline]
    pub fn count(&self) -> usize {
        self.bit_set.count()
    }

    /// Returns `true` if the bitset contains no elements.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.bit_set.is_empty()
    }

    /// Returns the maximum capacity currently tracking.
    #[inline]
    pub fn domain_size(&self) -> usize {
        self.bit_set.domain_size
    }

    /// Returns an iterator over the indices of set bits in ascending order.
    #[inline]
    pub fn iter(&self) -> BitIter<'_> {
        self.bit_set.iter()
    }
}

impl BitRelations<GrowableBitSet> for GrowableBitSet {
    fn union(&mut self, other: &GrowableBitSet) -> bool {
        self.ensure(other.domain_size());

        let min_words = other.bit_set.words.len();

        update_words(
            &mut self.bit_set.words[..min_words],
            &other.bit_set.words[..min_words],
            |a, b| a | b,
        )
    }

    fn subtract(&mut self, other: &GrowableBitSet) -> bool {
        let min_words = std::cmp::min(self.bit_set.words.len(), other.bit_set.words.len());
        update_words(
            &mut self.bit_set.words[..min_words],
            &other.bit_set.words[..min_words],
            |a, b| a & !b,
        )
    }

    fn intersect(&mut self, other: &GrowableBitSet) -> bool {
        let min_words = std::cmp::min(self.bit_set.words.len(), other.bit_set.words.len());
        let changed = update_words(
            &mut self.bit_set.words[..min_words],
            &other.bit_set.words[..min_words],
            |a, b| a & b,
        );

        let cleared = self.bit_set.words[min_words..]
            .iter()
            .any(|&word| word != 0);
        if cleared {
            self.bit_set.words[min_words..].fill(0);
        }

        changed || cleared
    }
}

/// An iterator over the set bits of a bitset.
///
/// Uses CPU trailing zero instructions to locate the next set bit in O(1) time
/// per word traversed.
pub struct BitIter<'a> {
    word: Word,
    offset: usize,
    iter: slice::Iter<'a, Word>,
}

impl<'a> BitIter<'a> {
    #[inline]
    fn new(words: &'a [Word]) -> Self {
        Self {
            word: 0,
            offset: usize::MAX.wrapping_sub(WORD_BITS - 1),
            iter: words.iter(),
        }
    }
}

impl<'a> Iterator for BitIter<'a> {
    type Item = usize;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        loop {
            if self.word != 0 {
                let bit_pos = self.word.trailing_zeros() as usize;
                self.word ^= 1 << bit_pos;
                return Some(bit_pos + self.offset);
            }

            self.word = *self.iter.next()?;
            self.offset = self.offset.wrapping_add(WORD_BITS);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_dense_bitset() {
        let mut bs = DenseBitSet::new_empty(100);
        assert!(bs.is_empty());

        assert!(bs.insert(10));
        assert!(!bs.insert(10));
        assert!(bs.contains(10));

        let mut bs2 = DenseBitSet::new_empty(100);
        bs2.insert(10);
        bs2.insert(20);

        assert!(bs.union(&bs2));
        assert!(bs.contains(20));

        assert_eq!(bs.iter().collect::<Vec<_>>(), vec![10, 20]);
    }

    #[test]
    fn test_growable_bitset() {
        let mut bs = GrowableBitSet::new();
        assert!(bs.insert(5));
        assert!(bs.insert(1024)); // Auto-resizes
        assert_eq!(bs.count(), 2);

        let mut bs2 = GrowableBitSet::new();
        bs2.insert(1024);
        bs2.insert(2048);

        bs.union(&bs2); // Auto-resizes self to match bs2
        assert!(bs.contains(2048));
        assert_eq!(bs.count(), 3);
    }

    #[test]
    fn intersect_reports_only_an_actual_change_across_different_domains() {
        let mut large = GrowableBitSet::with_capacity(129);
        let small = GrowableBitSet::with_capacity(1);

        assert!(!large.intersect(&small), "two empty sets do not change");

        large.insert(128);
        assert!(large.intersect(&small), "the out-of-domain bit is cleared");
        assert!(large.is_empty());
        assert!(
            !large.intersect(&small),
            "repeating the intersection does not report a change"
        );
    }
}