fixed_bitmaps/primitives/
bitmap128.rs1use super::BitmapSize;
2use core::fmt::Formatter;
3use serde::{Deserialize, Serialize};
4use std::{
5 fmt::{Debug, Display},
6 mem,
7 ops::{
8 Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Deref, Div,
9 DivAssign, Mul, MulAssign, Not, Shl, ShlAssign, Shr, ShrAssign, Sub, SubAssign,
10 },
11};
12
13#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash, Default, Serialize, Deserialize)]
45pub struct Bitmap128(u128);
46
47impl Bitmap128 {
48 pub fn capacity() -> usize {
49 Bitmap128::MAP_LENGTH
50 }
51
52 pub fn to_u128(&self) -> u128 {
53 self.0
54 }
55
56 pub fn new(value: bool) -> Bitmap128 {
70 Bitmap128(if value { u128::MAX } else { 0 })
71 }
72
73 pub fn create_bit_mask(begin: usize, end: usize, value: bool) -> Bitmap128 {
89 if value {
90 if begin >= Bitmap128::MAP_LENGTH || end < 1 {
91 Bitmap128(0)
92 } else if end >= Bitmap128::MAP_LENGTH {
93 Bitmap128(u128::MAX << begin)
94 } else {
95 Bitmap128(u128::MAX << begin & u128::MAX >> Bitmap128::MAP_LENGTH - end)
96 }
97 } else {
98 !Bitmap128::create_bit_mask(begin, end, true)
99 }
100 }
101
102 pub fn from_set(index: usize) -> Option<Bitmap128> {
117 if index >= Bitmap128::MAP_LENGTH {
118 return None;
119 }
120
121 let mut bitmap = Bitmap128::default();
122 bitmap.set(index, true).unwrap();
123 Some(bitmap)
124 }
125
126 pub fn set(&mut self, index: usize, value: bool) -> Result<(), String> {
147 if index >= Bitmap128::MAP_LENGTH {
148 return Err(String::from(
149 "Tried to set bit that's out of range of the bitmap (range: ",
150 ) + &Bitmap128::MAP_LENGTH.to_string()
151 + ", index: "
152 + &index.to_string()
153 + ")");
154 }
155
156 if value {
157 let mask = 1 << index;
158 self.0 |= mask;
159 } else {
160 let mask = u128::MAX - (1 << index);
161 self.0 &= mask;
162 }
163
164 Ok(())
165 }
166
167 pub fn set_range(&mut self, begin: usize, end: usize, value: bool) {
184 if value {
185 *self |= Bitmap128::create_bit_mask(begin, end, true);
186 } else {
187 *self &= Bitmap128::create_bit_mask(begin, end, false);
188 }
189 }
190
191 pub fn get(&self, index: usize) -> Result<bool, String> {
213 if index >= Bitmap128::MAP_LENGTH {
214 return Err(String::from(
215 "Tried to get bit that's out of range of the bitmap (range: ",
216 ) + &Bitmap128::MAP_LENGTH.to_string()
217 + ", index: "
218 + &index.to_string()
219 + ")");
220 }
221
222 let mask = 1 << index;
223 Ok(self.0 & mask > 0)
224 }
225}
226
227impl Display for Bitmap128 {
228 fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
229 write!(f, "{:b}", self.0)
230 }
231}
232
233impl Debug for Bitmap128 {
234 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
235 write!(f, "Bitmap128({:X})", self.0)
236 }
237}
238
239impl From<u128> for Bitmap128 {
240 fn from(value: u128) -> Self {
241 Bitmap128(value)
242 }
243}
244
245impl BitmapSize for Bitmap128 {
246 const MAP_LENGTH: usize = mem::size_of::<u128>() * 8;
247}
248
249impl BitAnd for Bitmap128 {
252 type Output = Self;
253
254 fn bitand(self, rhs: Self) -> Self::Output {
255 Self(self.0 & rhs.0)
256 }
257}
258
259impl BitAndAssign for Bitmap128 {
260 fn bitand_assign(&mut self, rhs: Self) {
261 self.0 &= rhs.0;
262 }
263}
264
265impl BitOr for Bitmap128 {
266 type Output = Self;
267
268 fn bitor(self, rhs: Self) -> Self::Output {
269 Self(self.0 | rhs.0)
270 }
271}
272
273impl BitOrAssign for Bitmap128 {
274 fn bitor_assign(&mut self, rhs: Self) {
275 self.0 |= rhs.0;
276 }
277}
278
279impl BitXor for Bitmap128 {
280 type Output = Self;
281
282 fn bitxor(self, rhs: Self) -> Self::Output {
283 Self(self.0 ^ rhs.0)
284 }
285}
286
287impl BitXorAssign for Bitmap128 {
288 fn bitxor_assign(&mut self, rhs: Self) {
289 self.0 ^= rhs.0;
290 }
291}
292
293impl Add for Bitmap128 {
296 type Output = Self;
297
298 fn add(self, rhs: Self) -> Self::Output {
299 Self(self.0 + rhs.0)
300 }
301}
302
303impl AddAssign for Bitmap128 {
304 fn add_assign(&mut self, rhs: Self) {
305 self.0 += rhs.0;
306 }
307}
308
309impl Sub for Bitmap128 {
310 type Output = Self;
311
312 fn sub(self, rhs: Self) -> Self::Output {
313 Self(self.0 - rhs.0)
314 }
315}
316
317impl SubAssign for Bitmap128 {
318 fn sub_assign(&mut self, rhs: Self) {
319 self.0 -= rhs.0;
320 }
321}
322
323impl Mul for Bitmap128 {
324 type Output = Self;
325
326 fn mul(self, rhs: Self) -> Self::Output {
327 Self(self.0 * rhs.0)
328 }
329}
330
331impl MulAssign for Bitmap128 {
332 fn mul_assign(&mut self, rhs: Self) {
333 self.0 *= rhs.0;
334 }
335}
336
337impl Div for Bitmap128 {
338 type Output = Self;
339
340 fn div(self, rhs: Self) -> Self::Output {
341 Self(self.0 / rhs.0)
342 }
343}
344
345impl DivAssign for Bitmap128 {
346 fn div_assign(&mut self, rhs: Self) {
347 self.0 /= rhs.0;
348 }
349}
350
351impl BitAnd<u128> for Bitmap128 {
354 type Output = Self;
355
356 fn bitand(self, rhs: u128) -> Self::Output {
357 Self(self.0 & rhs)
358 }
359}
360
361impl BitAndAssign<u128> for Bitmap128 {
362 fn bitand_assign(&mut self, rhs: u128) {
363 self.0 &= rhs;
364 }
365}
366
367impl BitOr<u128> for Bitmap128 {
368 type Output = Self;
369
370 fn bitor(self, rhs: u128) -> Self::Output {
371 Self(self.0 | rhs)
372 }
373}
374
375impl BitOrAssign<u128> for Bitmap128 {
376 fn bitor_assign(&mut self, rhs: u128) {
377 self.0 |= rhs;
378 }
379}
380
381impl BitXor<u128> for Bitmap128 {
382 type Output = Self;
383
384 fn bitxor(self, rhs: u128) -> Self::Output {
385 Self(self.0 ^ rhs)
386 }
387}
388
389impl BitXorAssign<u128> for Bitmap128 {
390 fn bitxor_assign(&mut self, rhs: u128) {
391 self.0 ^= rhs;
392 }
393}
394
395impl Add<u128> for Bitmap128 {
398 type Output = Self;
399
400 fn add(self, rhs: u128) -> Self::Output {
401 Self(self.0 + rhs)
402 }
403}
404
405impl AddAssign<u128> for Bitmap128 {
406 fn add_assign(&mut self, rhs: u128) {
407 self.0 += rhs;
408 }
409}
410
411impl Sub<u128> for Bitmap128 {
412 type Output = Self;
413
414 fn sub(self, rhs: u128) -> Self::Output {
415 Self(self.0 - rhs)
416 }
417}
418
419impl SubAssign<u128> for Bitmap128 {
420 fn sub_assign(&mut self, rhs: u128) {
421 self.0 -= rhs;
422 }
423}
424
425impl Mul<u128> for Bitmap128 {
426 type Output = Self;
427
428 fn mul(self, rhs: u128) -> Self::Output {
429 Self(self.0 * rhs)
430 }
431}
432
433impl MulAssign<u128> for Bitmap128 {
434 fn mul_assign(&mut self, rhs: u128) {
435 self.0 *= rhs;
436 }
437}
438
439impl Div<u128> for Bitmap128 {
440 type Output = Self;
441
442 fn div(self, rhs: u128) -> Self::Output {
443 Self(self.0 / rhs)
444 }
445}
446
447impl DivAssign<u128> for Bitmap128 {
448 fn div_assign(&mut self, rhs: u128) {
449 self.0 /= rhs;
450 }
451}
452
453impl Shl<usize> for Bitmap128 {
457 type Output = Self;
458
459 fn shl(self, rhs: usize) -> Self::Output {
460 Self(self.0 << rhs)
461 }
462}
463
464impl ShlAssign<usize> for Bitmap128 {
465 fn shl_assign(&mut self, rhs: usize) {
466 self.0 <<= rhs;
467 }
468}
469
470impl Shr<usize> for Bitmap128 {
471 type Output = Self;
472
473 fn shr(self, rhs: usize) -> Self::Output {
474 Self(self.0 >> rhs)
475 }
476}
477
478impl ShrAssign<usize> for Bitmap128 {
479 fn shr_assign(&mut self, rhs: usize) {
480 self.0 >>= rhs;
481 }
482}
483
484impl Not for Bitmap128 {
487 type Output = Self;
488
489 fn not(self) -> Self::Output {
490 Self(self.0 ^ u128::MAX)
491 }
492}
493
494impl Deref for Bitmap128 {
497 type Target = u128;
498
499 fn deref(&self) -> &Self::Target {
500 &self.0
501 }
502}