1 use core::ops::BitAnd; 2 3 use alloc::vec::Vec; 4 5 use crate::{bitmap_core::BitMapCore, traits::BitMapOps}; 6 7 #[derive(Clone)] 8 pub struct AllocBitmap { 9 elements: usize, 10 data: Vec<usize>, 11 core: BitMapCore<usize>, 12 } 13 14 impl AllocBitmap { 15 pub fn new(elements: usize) -> Self { 16 let data = vec![0usize; (elements + usize::BITS as usize - 1) / (usize::BITS as usize)]; 17 Self { 18 elements, 19 data, 20 core: BitMapCore::new(), 21 } 22 } 23 } 24 25 impl BitMapOps<usize> for AllocBitmap { 26 #[inline] 27 fn get(&self, index: usize) -> Option<bool> { 28 return self.core.get(self.elements, &self.data, index); 29 } 30 31 #[inline] 32 fn set(&mut self, index: usize, value: bool) -> Option<bool> { 33 return self.core.set(self.elements, &mut self.data, index, value); 34 } 35 36 #[inline] 37 fn len(&self) -> usize { 38 self.elements 39 } 40 41 #[inline] 42 fn size(&self) -> usize { 43 self.data.len() * core::mem::size_of::<usize>() 44 } 45 46 #[inline] 47 fn first_index(&self) -> Option<usize> { 48 self.core.first_index(&self.data) 49 } 50 51 #[inline] 52 fn first_false_index(&self) -> Option<usize> { 53 self.core.first_false_index(self.elements, &self.data) 54 } 55 56 #[inline] 57 fn last_index(&self) -> Option<usize> { 58 self.core.last_index(self.elements, &self.data) 59 } 60 61 #[inline] 62 fn last_false_index(&self) -> Option<usize> { 63 self.core.last_false_index(self.elements, &self.data) 64 } 65 66 #[inline] 67 fn next_index(&self, index: usize) -> Option<usize> { 68 self.core.next_index(self.elements, &self.data, index) 69 } 70 71 #[inline] 72 fn next_false_index(&self, index: usize) -> Option<usize> { 73 self.core.next_false_index(self.elements, &self.data, index) 74 } 75 76 #[inline] 77 fn prev_index(&self, index: usize) -> Option<usize> { 78 self.core.prev_index(self.elements, &self.data, index) 79 } 80 81 #[inline] 82 fn prev_false_index(&self, index: usize) -> Option<usize> { 83 self.core.prev_false_index(self.elements, &self.data, index) 84 } 85 86 #[inline] 87 fn invert(&mut self) { 88 self.core.invert(self.elements, &mut self.data); 89 } 90 91 #[inline] 92 fn is_full(&self) -> bool { 93 self.core.is_full(self.elements, &self.data) 94 } 95 96 #[inline] 97 fn is_empty(&self) -> bool { 98 self.core.is_empty(&self.data) 99 } 100 101 #[inline] 102 unsafe fn as_bytes(&self) -> &[u8] { 103 core::slice::from_raw_parts( 104 self.data.as_ptr() as *const u8, 105 core::mem::size_of::<Self>(), 106 ) 107 } 108 109 fn set_all(&mut self, value: bool) { 110 self.core.set_all(self.elements, &mut self.data, value); 111 } 112 } 113 114 impl BitAnd for AllocBitmap { 115 type Output = Self; 116 117 fn bitand(self, rhs: Self) -> Self::Output { 118 let mut result = AllocBitmap::new(self.elements); 119 for i in 0..rhs.data.len() { 120 result.data[i] = self.data[i] & rhs.data[i]; 121 } 122 result 123 } 124 } 125