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