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
use super::*;

#[cfg(feature = "speed")]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone)]
pub struct RepetitionTable {
    count_map: Box<[u8]>,
    mask: usize,
}

#[cfg(feature = "speed")]
impl Default for RepetitionTable {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(feature = "speed")]
impl RepetitionTable {
    pub fn new() -> Self {
        let size = REPETITION_TABLE_SIZE << 20;
        Self {
            count_map: vec![0; size].into_boxed_slice(),
            mask: size - 1,
        }
    }

    #[inline(always)]
    const fn get_index(&self, key: u64) -> usize {
        (key as usize) & self.mask
    }

    #[inline(always)]
    pub fn get_repetition(&self, key: u64) -> u8 {
        *get_item_unchecked!(self.count_map, self.get_index(key))
    }

    #[inline(always)]
    pub fn insert(&mut self, key: u64) {
        *get_item_unchecked_mut!(self.count_map, self.get_index(key)) += 1;
    }

    pub fn insert_and_get_repetition(&mut self, key: u64) -> u8 {
        let count_entry = get_item_unchecked_mut!(self.count_map, self.get_index(key));
        *count_entry += 1;
        *count_entry
    }

    pub fn remove(&mut self, key: u64) {
        let index = self.get_index(key);
        let entry = get_item_unchecked_mut!(self.count_map, index);
        if *entry == 0 {
            panic!(
                "Tried to remove the key {} that doesn't exist!",
                key.stringify()
            )
        }
        *entry -= 1;
    }

    #[inline(always)]
    pub fn clear(&mut self) {
        self.count_map.iter_mut().for_each(|entry| *entry = 0);
    }
}

#[cfg(not(feature = "speed"))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Default, Debug, Clone)]
pub struct RepetitionTable {
    count_map: std::collections::HashMap<u64, u8>,
}

#[cfg(not(feature = "speed"))]
impl RepetitionTable {
    pub fn new() -> Self {
        Self::default()
    }

    #[inline(always)]
    pub fn get_repetition(&self, key: u64) -> u8 {
        self.count_map.get(&key).copied().unwrap_or_default()
    }

    #[inline(always)]
    pub fn insert(&mut self, key: u64) {
        *self.count_map.entry(key).or_insert(0) += 1;
    }

    pub fn insert_and_get_repetition(&mut self, key: u64) -> u8 {
        let count_entry = self.count_map.entry(key).or_insert(0);
        *count_entry += 1;
        *count_entry
    }

    pub fn remove(&mut self, key: u64) {
        let count_entry = self.count_map.get_mut(&key).unwrap_or_else(|| {
            panic!(
                "Tried to remove the key {} that doesn't exist!",
                key.stringify()
            )
        });
        if *count_entry == 1 {
            self.count_map.remove(&key);
            return;
        }
        *count_entry -= 1;
    }

    #[inline(always)]
    pub fn clear(&mut self) {
        self.count_map.clear();
    }
}