Files
arrayvec
cfg_if
crossbeam_deque
crossbeam_epoch
crossbeam_utils
either
fid_rs
lazy_static
libc
louds_rs
memoffset
nodrop
num_cpus
rayon
rayon_core
scopeguard
  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
/// Cache table of `popcount` results.
pub struct PopcountTable {
    bit_length: u8,

    /// `table[target_num] == target_num.popcount()`
    table: Vec<u8>,
}

impl PopcountTable {
    /// Constructor.
    ///
    /// Time-complexity:  `O(bit_length)` (Assuming `u64::count_ones()` takes `O(1)`)
    /// Space-complexity: `O(bit_length)`
    ///
    /// `bit_length` must be in [1, 64].
    ///
    /// # Panics
    /// When `bit_length` is out of [1, 64].
    pub fn new(bit_length: u8) -> PopcountTable {
        assert!(
            1 <= bit_length && bit_length <= 64,
            "bit_length (= {}) must be in [1, 64]",
            bit_length
        );

        let table = (0..=(1 << bit_length) - 1)
            .map(|target: u64| target.count_ones() as u8)
            .collect();
        PopcountTable { bit_length, table }
    }

    /// Returns the same value as `target.count_ones()` in `O(1)`.
    ///
    /// # Panics
    /// When `target` is out of [0, 2^ `self.bit_length` ).
    pub fn popcount(&self, target: u64) -> u8 {
        assert!(
            target <= ((1 << self.bit_length) - 1),
            "target = {} must be < 2^{}, while PopcountTable::bit_length = {}",
            target,
            self.bit_length,
            self.bit_length
        );

        self.table[target as usize]
    }
}

#[cfg(test)]
mod new_success_tests {
    // well-tested in popcount_success_tests
}

#[cfg(test)]
mod new_failure_tests {
    use super::PopcountTable;

    #[test]
    #[should_panic]
    fn new_0() {
        let _ = PopcountTable::new(0);
    }

    #[test]
    #[should_panic]
    fn new_65() {
        let _ = PopcountTable::new(65);
    }
}

#[cfg(test)]
mod popcount_success_tests {
    use super::PopcountTable;
    use std::ops::RangeInclusive;

    macro_rules! parameterized_tests {
        ($($name:ident: $value:expr,)*) => {
        $(
            #[test]
            fn $name() {
                let bit_length = $value;
                let tbl = PopcountTable::new(bit_length);

                let range: RangeInclusive<u64> = 0..= ((1 << bit_length) - 1);
                for target in range {
                    assert_eq!(tbl.popcount(target), target.count_ones() as u8);
                }
            }
        )*
        }
    }

    parameterized_tests! {
        bit_length1: 1,
        bit_length2: 2,
        bit_length4: 4,
        bit_length8: 8,
        bit_length16: 16,
        // wants to test 32, 64 but takes too long time

        bit_length15: 15,
        bit_length17: 17,
    }
}

#[cfg(test)]
mod popcount_failure_tests {
    use super::PopcountTable;

    macro_rules! parameterized_tests {
        ($($name:ident: $value:expr,)*) => {
        $(
            #[test]
            #[should_panic]
            fn $name() {
                let bit_length = $value;
                let tbl = PopcountTable::new(bit_length);
                let _ = tbl.popcount(1 << bit_length);
            }
        )*
        }
    }

    parameterized_tests! {
        bit_length1: 1,
        bit_length2: 2,
        bit_length4: 4,
        bit_length8: 8,
        bit_length16: 16,

        bit_length15: 15,
        bit_length17: 17,
    }
}