Skip to content

Commit

Permalink
ucup
Browse files Browse the repository at this point in the history
  • Loading branch information
bminaiev committed Feb 11, 2024
1 parent 9f9e2cc commit df58794
Show file tree
Hide file tree
Showing 10 changed files with 966 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[workspace]
members = [
"main",
# "main",
# DO not format this file.
"algo_lib",
]

Expand Down
33 changes: 32 additions & 1 deletion algo_lib/src/collections/bit_set.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::ops::{BitAndAssign, BitOrAssign, Not};
use std::ops::{BitAndAssign, BitOrAssign, BitXorAssign, Not};

#[derive(Hash, Clone, Eq, PartialOrd, PartialEq, Debug)]
pub struct BitSet {
Expand Down Expand Up @@ -39,6 +39,24 @@ impl BitSet {
res
}

// bit [i] becomes [i - shift]. Bits before [0] are dropped.
pub fn shift_lower(&self, shift: usize) -> Self {
let mut res = Self::new(self.bit_len());
let whole = shift / 64;
let offset = shift % 64;
for i in 0..self.values.len() {
if i < whole {
continue;
}
// TODO: test
res.values[i - whole] |= self.values[i] >> offset;
if offset != 0 && i - whole != 0 {
res.values[i - whole - 1] |= self.values[i] << (64 - offset);
}
}
res
}

#[allow(unused)]
pub fn set(&mut self, pos: usize, val: bool) {
if val {
Expand Down Expand Up @@ -163,3 +181,16 @@ impl BitAndAssign<&BitSet> for BitSet {
}
}
}

impl BitXorAssign<&BitSet> for BitSet {
fn bitxor_assign(&mut self, rhs: &BitSet) {
self.ensure_length(rhs.bit_len());
let len = rhs.values.len();
for (x, y) in self.values[0..len]
.iter_mut()
.zip(rhs.values[0..len].iter())
{
*x ^= *y;
}
}
}
Loading

0 comments on commit df58794

Please sign in to comment.