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
//! ビット列を管理する

use std::{
    fmt::Debug,
    ops::{Deref, DerefMut, Index},
};

/// ビット列を高速に処理する
#[derive(Clone)]
pub struct BitSet<const SIZE: usize> {
    bits: Vec<u64>,
}

impl<const SIZE: usize> Default for BitSet<SIZE> {
    fn default() -> Self {
        Self::new()
    }
}

impl<const SIZE: usize> BitSet<SIZE> {
    /// ⌈size / 64⌉個のu64
    const ARRAY_SIZE: usize = (SIZE + 64 - 1) / 64;

    /// あまりのビット
    const REM_BIT: usize = SIZE % 64;

    /// 一時的な値
    const TMP_BOOL: [bool; 2] = [false, true];

    /// Bitsetを初期化する
    /// - `size`: ビットの数
    pub fn new() -> Self {
        Self {
            bits: vec![0; Self::ARRAY_SIZE],
        }
    }

    /// `idx`bit目を1に設定
    pub fn set(&mut self, index: usize) {
        let arr_idx = index / 64;
        let bit_idx = index % 64;
        self.bits[arr_idx] |= 1 << bit_idx;
    }

    /// `idx`bit目を0に設定
    pub fn unset(&mut self, index: usize) {
        let arr_idx = index / 64;
        let bit_idx = index % 64;
        self.bits[arr_idx] &= !(1 << bit_idx);
    }

    /// `idx`bit目を反転
    pub fn flip(&mut self, index: usize) {
        if self[index] {
            self.unset(index);
        } else {
            self.set(index);
        }
    }

    /// すべてのbitが0になっているかを判定する
    pub fn any(&self) -> bool {
        self.bits.iter().all(|&b64| b64 == 0)
    }

    /// すべてのbitが1になっているかを判定する
    pub fn all(&self) -> bool {
        // あまりだけ個別に判定
        let filter = !0_u64 >> (64 - Self::REM_BIT);
        self.bits[Self::ARRAY_SIZE - 1] ^ filter == 0
            && self
                .bits
                .iter()
                .take(Self::ARRAY_SIZE - 1)
                .all(|&b64| b64 == !0)
    }

    /// あるbitを更新する
    fn update(&mut self, index: usize, new_val: bool) {
        if new_val {
            self.set(index);
        } else {
            self.unset(index);
        }
    }

    /// 1であるビットの数を求める
    pub fn count_ones(&self) -> usize {
        self.bits
            .iter()
            .map(|b64| b64.count_ones() as usize)
            .sum::<usize>()
    }

    /// あるbitの可変参照を取得する
    /// - `index`: 取得するbitのインデックス
    pub fn get_mut(&mut self, index: usize) -> Option<BitMut<'_, SIZE>> {
        if index < SIZE {
            let default = self[index];
            Some(BitMut {
                bitset: self,
                idx: index,
                new_val: default,
            })
        } else {
            None
        }
    }
}

impl<const SIZE: usize> Index<usize> for BitSet<SIZE> {
    type Output = bool;
    fn index(&self, index: usize) -> &Self::Output {
        let arr_idx = index / 64;
        let bit_idx = index % 64;
        if (self.bits[arr_idx] >> bit_idx) & 1 == 0 {
            &Self::TMP_BOOL[0]
        } else {
            &Self::TMP_BOOL[1]
        }
    }
}

/// bitsetの更新を行う
pub struct BitMut<'a, const SIZE: usize> {
    bitset: &'a mut BitSet<SIZE>,
    idx: usize,
    new_val: bool,
}

impl<const SIZE: usize> Deref for BitMut<'_, SIZE> {
    type Target = bool;
    fn deref(&self) -> &Self::Target {
        &self.new_val
    }
}

impl<const SIZE: usize> DerefMut for BitMut<'_, SIZE> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.new_val
    }
}

impl<const SIZE: usize> Drop for BitMut<'_, SIZE> {
    fn drop(&mut self) {
        self.bitset.update(self.idx, self.new_val);
    }
}

impl<const SIZE: usize> Debug for BitSet<SIZE> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut bit_str = format!("{:b}", self.bits[Self::ARRAY_SIZE - 1]);
        // ゼロ埋め
        bit_str = "0".repeat(Self::REM_BIT - bit_str.len()) + &bit_str;
        bit_str = self.bits[..Self::ARRAY_SIZE - 1]
            .iter()
            .rev()
            .map(|b64| format!(",{:0>64b}", b64))
            .fold(bit_str, |acc, b64| acc + &b64);
        write!(f, "BitSet {{ {:?} }}", bit_str)
    }
}