faex 0.1.2

A fast and efficient Compact Data Structures Library
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
use super::rank_select::{Rank, Select};
use super::Access;
use crate::profiling::HeapSize;
use crate::util::{ceil_div, getbits, setbits};
use crate::Build;
use serde::{Deserialize, Serialize};
use std::fmt::{Debug, Display, Formatter};
use std::ops::Range;

/// Data Structure that represents a bit vector using a compact storage.
// TODO: Be generic over the underlying data container? This could allow to retrieve
// ints of size greater than u64 (such as u128 or other integer types that could be in the future)

#[derive(Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct BitVec {
    /// The underlying data structure
    raw_data: Vec<usize>,
    len: usize,
}

impl Debug for BitVec {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let raw_data_formatted = self
            .raw_data
            .iter()
            .map(|word| format!("{word:b}"))
            .collect::<Vec<String>>();

        f.debug_struct("BitVec")
            .field("raw_data", &raw_data_formatted)
            .field("len", &self.len)
            .finish()
    }
}

impl BitVec {
    pub const CONTAINER_WIDTH: usize = std::mem::size_of::<usize>() * 8;

    #[inline]
    pub fn new() -> Self {
        Self {
            raw_data: Vec::new(),
            len: 0,
        }
    }

    #[inline]
    pub fn with_capacity(capacity: usize) -> Self {
        // ceil division with usize
        let compact_capacity = ceil_div(capacity, BitVec::CONTAINER_WIDTH);
        Self {
            raw_data: Vec::with_capacity(compact_capacity),
            len: 0,
        }
    }

    #[inline]
    pub fn from_value(value: bool, n: usize) -> Self {
        let num_words = ceil_div(n, BitVec::CONTAINER_WIDTH);
        let mut raw_data = Vec::with_capacity(num_words);
        let value = if value { usize::MAX } else { 0 };
        raw_data.resize(num_words, value);

        // clear unused bits
        let last_word_offset = n % BitVec::CONTAINER_WIDTH;
        if last_word_offset != 0 {
            let last_word = raw_data.last_mut().unwrap();
            *last_word &= usize::MAX >> (BitVec::CONTAINER_WIDTH - last_word_offset);
        }

        Self { raw_data, len: n }
    }

    // TODO: create a method to create a BitVec with a bool value
    // repeated n times, or just use from_iter method? (with collect)
    #[inline]
    pub fn capacity(&self) -> usize {
        self.raw_data.capacity() * BitVec::CONTAINER_WIDTH
    }

    #[inline]
    pub fn len(&self) -> usize {
        self.len
    }

    #[inline]
    pub fn is_empty(&self) -> bool {
        self.raw_data.is_empty()
    }

    #[inline]
    pub fn raw_data(&self) -> &[usize] {
        &self.raw_data
    }

    #[inline]
    pub fn push(&mut self, value: bool) {
        let (block_index, offset) = (
            self.len / BitVec::CONTAINER_WIDTH,
            self.len % BitVec::CONTAINER_WIDTH,
        );

        let value = value.into();

        if offset == 0 {
            self.raw_data.push(value);
        } else {
            setbits!(self.raw_data[block_index], 1, offset, value);
        }
        self.len += 1;
    }

    /// Pushes a value into the bit vector, using the specified width.
    /// The bits will be read in a LSB-first order (i.e from right to left).
    // TODO: hardcode T as usize? then some asserts can be removed
    // TODO: panic or return result explaining the error? Maybe results would decrease performance
    #[inline]
    pub fn push_bits<T>(&mut self, value: T, width: usize)
    where
        T: Into<usize>,
    {
        // Allow to push a value of size less than the width, the bits will
        // be padded with 0s
        let value = value.into();

        // This check prevents from a single value span more than two words
        assert!(
            width <= BitVec::CONTAINER_WIDTH,
            "Width {width} is greater than the BitVec's container width ({})",
            BitVec::CONTAINER_WIDTH
        );

        // TODO: return or crash?
        if width == 0 {
            return;
        }
        let (block_index, offset) = (
            self.len / BitVec::CONTAINER_WIDTH,
            self.len % BitVec::CONTAINER_WIDTH,
        );

        if offset == 0 {
            self.raw_data.push(value);
        } else {
            let last_block = self.raw_data[block_index];
            let shifted_value = value << offset;
            let new_value = shifted_value | last_block;
            self.raw_data[block_index] = new_value;

            let filled_bits = BitVec::CONTAINER_WIDTH - offset;

            if filled_bits < width {
                let remaining_value = value >> filled_bits;
                self.raw_data.push(remaining_value);
            }
        }
        self.len += width;
    }

    #[inline]
    pub fn pop(&mut self) -> bool {
        assert!(self.len > 0, "Cannot pop a bit from an empty BitVec");

        let value = self.read(self.len - 1);

        // clear bit at pos len -1 so there are no dirty bits
        self.set(self.len - 1, false);
        self.len -= 1;

        if self.len % BitVec::CONTAINER_WIDTH == 0 {
            // If that bit was the last one in the last word, remove that word
            self.raw_data.pop();
        }
        value
    }

    #[inline]
    pub fn pop_bits(&mut self, n: usize) -> usize {
        assert!(
            n <= self.len,
            "Cannot pop {n} bits from a BitVec of length {}",
            self.len,
        );

        let new_len = self.len - n;
        let value = self.read_bits(new_len, n);

        let old_compact_len = ceil_div(self.len, BitVec::CONTAINER_WIDTH);
        let new_compact_len = ceil_div(new_len, BitVec::CONTAINER_WIDTH);

        if old_compact_len > new_compact_len {
            self.raw_data.truncate(new_compact_len);
        }

        // clear the bits of the last word that are not used
        let last_word_offset = new_len % BitVec::CONTAINER_WIDTH;
        if last_word_offset != 0 {
            let last_word = self.raw_data.last_mut().unwrap();
            *last_word &= usize::MAX >> (BitVec::CONTAINER_WIDTH - last_word_offset);
        }

        self.len = new_len;

        value
    }

    #[inline]
    pub fn read(&self, index: usize) -> bool {
        assert!(
            index < self.len,
            "Cannot read bit at index {index} from a BitVec of length {}",
            self.len
        );

        let (block_index, offset) = (
            index / BitVec::CONTAINER_WIDTH,
            index % BitVec::CONTAINER_WIDTH,
        );

        // TODO: use get_unchecked to avoid bounds check, since
        // we are already checking bounds.

        // Optimize this as we know we are reading 1 bit
        // getbits!(self.raw_data[block_index], 1, offset) == 1
        self.raw_data[block_index] >> offset & 0b1 == 1
    }

    #[inline]
    pub fn read_bits(&self, index: usize, len: usize) -> usize {
        if len == 0 {
            return 0;
        }
        assert!(
            len <= BitVec::CONTAINER_WIDTH,
            "requested len ({len}) is greater than the BitVec's container width ({})",
            BitVec::CONTAINER_WIDTH
        );

        assert!(
            index + len - 1 < self.len(),
            "index out of bounds: the len is {}, but the index is {} and the width is {}",
            self.len(),
            index,
            len
        );
        unsafe { self.read_bits_unchecked(index, len) }
    }

    /// # Safety
    /// Calling this method with an out-of-bounds `index + len` is undefined behavior.
    #[inline]
    pub unsafe fn read_bits_unchecked(&self, index: usize, len: usize) -> usize {
        // TODO: optimize this method to achieve better performance
        if len == 0 {
            return 0;
        }
        let offset = index % BitVec::CONTAINER_WIDTH;
        let index = index / BitVec::CONTAINER_WIDTH;

        let w1 = self.raw_data.get_unchecked(index) >> offset;
        if offset + len > BitVec::CONTAINER_WIDTH {
            let w2 = self.raw_data.get_unchecked(index + 1);
            let read_bits = BitVec::CONTAINER_WIDTH - offset;
            let rem_bits = len - read_bits;
            w1 | ((w2 & (usize::MAX >> (BitVec::CONTAINER_WIDTH - rem_bits))) << read_bits)
        } else {
            w1 & (usize::MAX >> (BitVec::CONTAINER_WIDTH - len))
        }
    }

    #[inline]
    pub fn set(&mut self, index: usize, value: bool) {
        assert!(
            index < self.len,
            "Cannot set bit at index {index} from a BitVec of length {}",
            self.len
        );

        let (block_index, offset) = (
            index / BitVec::CONTAINER_WIDTH,
            index % BitVec::CONTAINER_WIDTH,
        );
        let value: usize = value.into();

        setbits!(self.raw_data[block_index], 1, offset, value);
    }

    #[inline]
    pub fn set_bits<T>(&mut self, range: Range<usize>, value: T)
    where
        T: Into<usize>,
    {
        if range.start == range.end {
            return;
        }

        let local_container_width: usize = std::mem::size_of::<T>() * 8;
        let value = value.into();

        let start = range.start;
        let end = range.end - 1;
        let width = range.end - range.start;

        assert!(
            end < self.len,
            "Cannot set bits from index {start} to {end} from a BitVec of length {}",
            self.len
        );

        assert!(
            width <= BitVec::CONTAINER_WIDTH,
            "Range's width {width} is greater than the BitVec's container width ({})",
            BitVec::CONTAINER_WIDTH
        );

        assert!(
            width <= local_container_width,
            "Range's width {width} is greater than the provided value's bits ({local_container_width})"
        );

        if width == 0 {
            return;
        }

        let first_index = start / BitVec::CONTAINER_WIDTH;
        let first_offset = start % BitVec::CONTAINER_WIDTH;

        let last_index = end / BitVec::CONTAINER_WIDTH;
        if first_index == last_index {
            // when the int is contained in a single word
            setbits!(self.raw_data[first_index], width, first_offset, value);
        } else {
            //when the int is split between two words
            let first_word_num_bits = BitVec::CONTAINER_WIDTH - first_offset;
            let first_value = getbits!(value, first_word_num_bits, 0);
            setbits!(
                self.raw_data[first_index],
                first_word_num_bits,
                first_offset,
                first_value
            );

            let last_word_num_bits = width - first_word_num_bits;
            let last_value = getbits!(value, last_word_num_bits, first_word_num_bits);
            setbits!(self.raw_data[last_index], last_word_num_bits, 0, last_value);
        }
    }

    // TODO: create a remove function similar to Vec's api
}

impl Default for BitVec {
    fn default() -> Self {
        Self::new()
    }
}

impl HeapSize for BitVec {
    fn heap_size_in_bits(&self) -> usize {
        self.raw_data.heap_size_in_bits()
    }
}

impl Access for BitVec {
    fn access(&self, index: usize) -> Option<bool> {
        if index >= self.len {
            return None;
        }
        Some(self.read(index))
    }
}

impl Rank for BitVec {
    fn rank(&self, index: usize) -> Option<usize> {
        // rank is the number of 1s before the index (range [0, index))
        if index > self.len {
            return None;
        }

        let (block_index, block_offset) = (
            index / BitVec::CONTAINER_WIDTH,
            index % BitVec::CONTAINER_WIDTH,
        );

        let prev_blocks = &self.raw_data[..block_index];
        let prev_rank = prev_blocks
            .iter()
            .map(|block| block.count_ones())
            .sum::<u32>() as usize;

        let last_block = self.raw_data.get(block_index).copied().unwrap_or(0);
        // getbits!(last_block, block_offset, 0) optimized, we know that block_offset is always <64 so this does not overflow
        let last_block_target = last_block & ((1 << block_offset) - 1);
        let last_block_rank = last_block_target.count_ones() as usize;

        Some(prev_rank + last_block_rank)
    }
}

impl Select for BitVec {
    fn select(&self, rank: usize) -> Option<usize> {
        if rank == 0 {
            return Some(0);
        }

        let mut rank_count = 0;
        for (i, block) in self.raw_data.iter().enumerate() {
            let block_rank = block.count_ones() as usize;
            if rank_count + block_rank >= rank {
                // Select inside word
                let mut bit_index = 0;
                let mut word = *block;
                while rank_count < rank {
                    let bit = word & 0b1;
                    if bit == 1 {
                        rank_count += 1;
                    }
                    word >>= 1;
                    bit_index += 1;
                }
                return Some(i * BitVec::CONTAINER_WIDTH + bit_index);
            }
            rank_count += block_rank;
        }
        None
    }

    fn select0(&self, rank0: usize) -> Option<usize> {
        if rank0 == 0 {
            return Some(0);
        }

        let mut rank0_count = 0;
        for (i, block) in self.raw_data.iter().enumerate() {
            let block_rank0 = if i == self.raw_data.len() - 1 {
                // At the last word, it may not be fully completed and there may be dirty bits
                // at value 0, so we do not count them
                (self.len - i * BitVec::CONTAINER_WIDTH) - block.count_ones() as usize
            } else {
                block.count_zeros() as usize
            };

            if rank0_count + block_rank0 >= rank0 {
                // Select inside word
                let mut bit_index = 0;
                let mut word = *block;
                while rank0_count < rank0 {
                    let bit = word & 0b1;
                    if bit == 0 {
                        rank0_count += 1;
                    }
                    word >>= 1;
                    bit_index += 1;
                }
                return Some(i * BitVec::CONTAINER_WIDTH + bit_index);
            }
            rank0_count += block_rank0;
        }

        None
    }
}

impl Display for BitVec {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut iter = self.iter();
        write!(f, "[")?;
        if let Some(bit) = iter.next() {
            let bit = if bit { 1 } else { 0 };
            write!(f, "{bit}")?;
        }
        for bit in iter {
            let bit = if bit { 1 } else { 0 };
            write!(f, ", {bit}")?;
        }
        write!(f, "]")
    }
}

pub struct BitVecSpec;

impl BitVecSpec {
    pub const fn new() -> Self {
        Self {}
    }
}

impl Default for BitVecSpec {
    fn default() -> Self {
        Self::new()
    }
}

impl BitVec {
    pub const fn spec() -> BitVecSpec {
        BitVecSpec::new()
    }
}

impl Build<BitVec, BitVec> for BitVecSpec {
    fn build(&self, data: BitVec) -> BitVec {
        data
    }
}

pub mod iter;
#[cfg(test)]
mod tests;