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
use core::fmt::Formatter;
use serde::{Deserialize, Serialize};
use std::{
fmt::Display,
ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign},
};
const MAP_LENGTH: u64 = 64;
#[derive(
PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash, Debug, Default, Serialize, Deserialize,
)]
pub struct Bitmap64(u64);
impl Bitmap64 {
pub fn to_u64(&self) -> u64 {
self.0
}
pub fn from_set(index: u64) -> Option<Bitmap64> {
if index >= MAP_LENGTH {
return None;
}
let mut bitmap = Bitmap64::default();
bitmap.set(index, true).unwrap();
Some(bitmap)
}
pub fn set(&mut self, index: u64, value: bool) -> Result<(), String> {
if index >= MAP_LENGTH {
return Err(String::from(
"Tried to set bit that's out of range of the bitmap (range: ",
) + &MAP_LENGTH.to_string()
+ ", index: "
+ &index.to_string()
+ ")");
}
if value {
let mask = 1 << index;
self.0 |= mask;
} else {
let mask = u64::MAX - (1 << index);
self.0 &= mask;
}
Ok(())
}
pub fn get(&self, index: u64) -> Result<bool, String> {
if index >= MAP_LENGTH {
return Err(String::from(
"Tried to set bit that's out of range of the bitmap (range: ",
) + &MAP_LENGTH.to_string()
+ ", index: "
+ &index.to_string()
+ ")");
}
let mask = 1 << index;
Ok(self.0 & mask > 0)
}
}
impl From<u64> for Bitmap64 {
fn from(value: u64) -> Self {
Bitmap64(value)
}
}
impl Display for Bitmap64 {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
let mut bitmap = String::new();
for i in 0..MAP_LENGTH {
bitmap.push_str(&(if self.0 & (1 << i) > 0 { 1 } else { 0 }).to_string());
}
write!(f, "{}", bitmap.chars().rev().collect::<String>())
}
}
impl BitAnd for Bitmap64 {
type Output = Self;
fn bitand(self, rhs: Self) -> Self::Output {
Self(self.0 & rhs.0)
}
}
impl BitAndAssign for Bitmap64 {
fn bitand_assign(&mut self, rhs: Self) {
self.0 &= rhs.0;
}
}
impl BitOr for Bitmap64 {
type Output = Self;
fn bitor(self, rhs: Self) -> Self::Output {
Self(self.0 | rhs.0)
}
}
impl BitOrAssign for Bitmap64 {
fn bitor_assign(&mut self, rhs: Self) {
self.0 |= rhs.0;
}
}
impl BitXor for Bitmap64 {
type Output = Self;
fn bitxor(self, rhs: Self) -> Self::Output {
Self(self.0 ^ rhs.0)
}
}
impl BitXorAssign for Bitmap64 {
fn bitxor_assign(&mut self, rhs: Self) {
self.0 ^= rhs.0;
}
}