eta_algorithms/data_structs/bitmap/
handle.rs

1use crate::data_structs::array::Array;
2use crate::data_structs::bitmap::consts::{DIV_SHIFT, MASK};
3use std::collections::HashMap;
4use std::ops::BitOrAssign;
5
6#[derive(Copy, Clone)]
7pub struct Handle {
8    pub bit_mask: usize,
9    pub chunk: u8,
10}
11
12impl BitOrAssign for Handle {
13    fn bitor_assign(&mut self, rhs: Self) {
14        self.chunk = rhs.chunk;
15        self.bit_mask |= rhs.bit_mask;
16    }
17}
18
19impl Handle {
20    pub fn new_batch(offsets: &[usize]) -> Array<Self> {
21        let mut array = Array::new_default_bytes(offsets.len(), 0);
22        let mut len = 0;
23        let mut chunk_table = HashMap::<u8, usize>::new();
24        for offset in offsets {
25            let chunk_offset = (offset >> DIV_SHIFT) as u8;
26            let index = match chunk_table.get(&chunk_offset) {
27                Some(index) => *index,
28                None => {
29                    let index = len;
30                    chunk_table.insert(chunk_offset, index);
31                    len += 1;
32                    index
33                }
34            };
35            let bit_offset = offset & MASK;
36            let mask: usize = 1 << bit_offset;
37            unsafe {
38                *array.index_unchecked_mut(index) |= Self {
39                    bit_mask: mask,
40                    chunk: chunk_offset,
41                }
42            };
43        }
44        array.resize(len);
45        array
46    }
47}