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
use crate::bitmap::{Bitmap, MutableBitmap};

use super::utils::{BitChunk, BitChunkIterExact, BitChunksExact};

/// Applies a function to every bit of this [`MutableBitmap`] in chunks
///
/// This function can be for operations like `!` to a [`MutableBitmap`].
pub fn unary_assign<T: BitChunk, F: Fn(T) -> T>(bitmap: &mut MutableBitmap, op: F) {
    let mut chunks = bitmap.bitchunks_exact_mut::<T>();

    chunks.by_ref().for_each(|chunk| {
        let new_chunk: T = match (chunk as &[u8]).try_into() {
            Ok(a) => T::from_ne_bytes(a),
            Err(_) => unreachable!(),
        };
        let new_chunk = op(new_chunk);
        chunk.copy_from_slice(new_chunk.to_ne_bytes().as_ref());
    });

    if chunks.remainder().is_empty() {
        return;
    }
    let mut new_remainder = T::zero().to_ne_bytes();
    chunks
        .remainder()
        .iter()
        .enumerate()
        .for_each(|(index, b)| new_remainder[index] = *b);
    new_remainder = op(T::from_ne_bytes(new_remainder)).to_ne_bytes();

    let len = chunks.remainder().len();
    chunks
        .remainder()
        .copy_from_slice(&new_remainder.as_ref()[..len]);
}

impl std::ops::Not for MutableBitmap {
    type Output = Self;

    #[inline]
    fn not(mut self) -> Self {
        unary_assign(&mut self, |a: u64| !a);
        self
    }
}

fn binary_assign_impl<I, T, F>(lhs: &mut MutableBitmap, mut rhs: I, op: F)
where
    I: BitChunkIterExact<T>,
    T: BitChunk,
    F: Fn(T, T) -> T,
{
    let mut lhs_chunks = lhs.bitchunks_exact_mut::<T>();

    lhs_chunks
        .by_ref()
        .zip(rhs.by_ref())
        .for_each(|(lhs, rhs)| {
            let new_chunk: T = match (lhs as &[u8]).try_into() {
                Ok(a) => T::from_ne_bytes(a),
                Err(_) => unreachable!(),
            };
            let new_chunk = op(new_chunk, rhs);
            lhs.copy_from_slice(new_chunk.to_ne_bytes().as_ref());
        });

    let rem_lhs = lhs_chunks.remainder();
    let rem_rhs = rhs.remainder();
    if rem_lhs.is_empty() {
        return;
    }
    let mut new_remainder = T::zero().to_ne_bytes();
    lhs_chunks
        .remainder()
        .iter()
        .enumerate()
        .for_each(|(index, b)| new_remainder[index] = *b);
    new_remainder = op(T::from_ne_bytes(new_remainder), rem_rhs).to_ne_bytes();

    let len = lhs_chunks.remainder().len();
    lhs_chunks
        .remainder()
        .copy_from_slice(&new_remainder.as_ref()[..len]);
}

/// Apply a bitwise binary operation to a [`MutableBitmap`].
///
/// This function can be used for operations like `&=` to a [`MutableBitmap`].
/// # Panics
/// This function panics iff `lhs.len() != `rhs.len()`
pub fn binary_assign<T: BitChunk, F>(lhs: &mut MutableBitmap, rhs: &Bitmap, op: F)
where
    F: Fn(T, T) -> T,
{
    assert_eq!(lhs.len(), rhs.len());

    let (slice, offset, length) = rhs.as_slice();
    if offset == 0 {
        let iter = BitChunksExact::<T>::new(slice, length);
        binary_assign_impl(lhs, iter, op)
    } else {
        let rhs_chunks = rhs.chunks::<T>();
        binary_assign_impl(lhs, rhs_chunks, op)
    }
}

#[inline]
/// Compute bitwise OR operation in-place
fn or_assign<T: BitChunk>(lhs: &mut MutableBitmap, rhs: &Bitmap) {
    if rhs.unset_bits() == 0 {
        assert_eq!(lhs.len(), rhs.len());
        lhs.clear();
        lhs.extend_constant(rhs.len(), true);
    } else if rhs.unset_bits() == rhs.len() {
        // bitmap remains
    } else {
        binary_assign(lhs, rhs, |x: T, y| x | y)
    }
}

impl<'a> std::ops::BitOrAssign<&'a Bitmap> for &mut MutableBitmap {
    #[inline]
    fn bitor_assign(&mut self, rhs: &'a Bitmap) {
        or_assign::<u64>(self, rhs)
    }
}

impl<'a> std::ops::BitOr<&'a Bitmap> for MutableBitmap {
    type Output = Self;

    #[inline]
    fn bitor(mut self, rhs: &'a Bitmap) -> Self {
        or_assign::<u64>(&mut self, rhs);
        self
    }
}

#[inline]
/// Compute bitwise `&` between `lhs` and `rhs`, assigning it to `lhs`
fn and_assign<T: BitChunk>(lhs: &mut MutableBitmap, rhs: &Bitmap) {
    if rhs.unset_bits() == 0 {
        // bitmap remains
    }
    if rhs.unset_bits() == rhs.len() {
        assert_eq!(lhs.len(), rhs.len());
        lhs.clear();
        lhs.extend_constant(rhs.len(), false);
    } else {
        binary_assign(lhs, rhs, |x: T, y| x & y)
    }
}

impl<'a> std::ops::BitAndAssign<&'a Bitmap> for &mut MutableBitmap {
    #[inline]
    fn bitand_assign(&mut self, rhs: &'a Bitmap) {
        and_assign::<u64>(self, rhs)
    }
}

impl<'a> std::ops::BitAnd<&'a Bitmap> for MutableBitmap {
    type Output = Self;

    #[inline]
    fn bitand(mut self, rhs: &'a Bitmap) -> Self {
        and_assign::<u64>(&mut self, rhs);
        self
    }
}

#[inline]
/// Compute bitwise XOR operation
fn xor_assign<T: BitChunk>(lhs: &mut MutableBitmap, rhs: &Bitmap) {
    binary_assign(lhs, rhs, |x: T, y| x ^ y)
}

impl<'a> std::ops::BitXorAssign<&'a Bitmap> for &mut MutableBitmap {
    #[inline]
    fn bitxor_assign(&mut self, rhs: &'a Bitmap) {
        xor_assign::<u64>(self, rhs)
    }
}

impl<'a> std::ops::BitXor<&'a Bitmap> for MutableBitmap {
    type Output = Self;

    #[inline]
    fn bitxor(mut self, rhs: &'a Bitmap) -> Self {
        xor_assign::<u64>(&mut self, rhs);
        self
    }
}