17db6e063SLoGin use core::ops::BitAnd; 27db6e063SLoGin 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 { new(elements: usize) -> Self15b2ca6800SLoGin 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 } 23*0102d69fSLoGin bitand_assign(&mut self, rhs: &Self)24*0102d69fSLoGin pub fn bitand_assign(&mut self, rhs: &Self) { 25*0102d69fSLoGin for i in 0..rhs.data.len() { 26*0102d69fSLoGin self.data[i] &= rhs.data[i]; 27*0102d69fSLoGin } 28*0102d69fSLoGin } 29b2ca6800SLoGin } 30b2ca6800SLoGin 31b2ca6800SLoGin impl BitMapOps<usize> for AllocBitmap { 32b2ca6800SLoGin #[inline] get(&self, index: usize) -> Option<bool>33b2ca6800SLoGin fn get(&self, index: usize) -> Option<bool> { 34b2ca6800SLoGin return self.core.get(self.elements, &self.data, index); 35b2ca6800SLoGin } 36b2ca6800SLoGin 37b2ca6800SLoGin #[inline] set(&mut self, index: usize, value: bool) -> Option<bool>38b2ca6800SLoGin fn set(&mut self, index: usize, value: bool) -> Option<bool> { 39b2ca6800SLoGin return self.core.set(self.elements, &mut self.data, index, value); 40b2ca6800SLoGin } 41b2ca6800SLoGin 42b2ca6800SLoGin #[inline] len(&self) -> usize43b2ca6800SLoGin fn len(&self) -> usize { 44b2ca6800SLoGin self.elements 45b2ca6800SLoGin } 46b2ca6800SLoGin 47b2ca6800SLoGin #[inline] size(&self) -> usize48b2ca6800SLoGin fn size(&self) -> usize { 49b2ca6800SLoGin self.data.len() * core::mem::size_of::<usize>() 50b2ca6800SLoGin } 51b2ca6800SLoGin 52b2ca6800SLoGin #[inline] first_index(&self) -> Option<usize>53b2ca6800SLoGin fn first_index(&self) -> Option<usize> { 54b2ca6800SLoGin self.core.first_index(&self.data) 55b2ca6800SLoGin } 56b2ca6800SLoGin 57b2ca6800SLoGin #[inline] first_false_index(&self) -> Option<usize>58b2ca6800SLoGin fn first_false_index(&self) -> Option<usize> { 59b2ca6800SLoGin self.core.first_false_index(self.elements, &self.data) 60b2ca6800SLoGin } 61b2ca6800SLoGin 62b2ca6800SLoGin #[inline] last_index(&self) -> Option<usize>63b2ca6800SLoGin fn last_index(&self) -> Option<usize> { 64b2ca6800SLoGin self.core.last_index(self.elements, &self.data) 65b2ca6800SLoGin } 66b2ca6800SLoGin 67b2ca6800SLoGin #[inline] last_false_index(&self) -> Option<usize>68b2ca6800SLoGin fn last_false_index(&self) -> Option<usize> { 69b2ca6800SLoGin self.core.last_false_index(self.elements, &self.data) 70b2ca6800SLoGin } 71b2ca6800SLoGin 72b2ca6800SLoGin #[inline] next_index(&self, index: usize) -> Option<usize>73b2ca6800SLoGin fn next_index(&self, index: usize) -> Option<usize> { 74b2ca6800SLoGin self.core.next_index(self.elements, &self.data, index) 75b2ca6800SLoGin } 76b2ca6800SLoGin 77b2ca6800SLoGin #[inline] next_false_index(&self, index: usize) -> Option<usize>78b2ca6800SLoGin fn next_false_index(&self, index: usize) -> Option<usize> { 79b2ca6800SLoGin self.core.next_false_index(self.elements, &self.data, index) 80b2ca6800SLoGin } 81b2ca6800SLoGin 82b2ca6800SLoGin #[inline] prev_index(&self, index: usize) -> Option<usize>83b2ca6800SLoGin fn prev_index(&self, index: usize) -> Option<usize> { 84b2ca6800SLoGin self.core.prev_index(self.elements, &self.data, index) 85b2ca6800SLoGin } 86b2ca6800SLoGin 87b2ca6800SLoGin #[inline] prev_false_index(&self, index: usize) -> Option<usize>88b2ca6800SLoGin fn prev_false_index(&self, index: usize) -> Option<usize> { 89b2ca6800SLoGin self.core.prev_false_index(self.elements, &self.data, index) 90b2ca6800SLoGin } 91b2ca6800SLoGin 92b2ca6800SLoGin #[inline] invert(&mut self)93b2ca6800SLoGin fn invert(&mut self) { 94b2ca6800SLoGin self.core.invert(self.elements, &mut self.data); 95b2ca6800SLoGin } 96b2ca6800SLoGin 97b2ca6800SLoGin #[inline] is_full(&self) -> bool98b2ca6800SLoGin fn is_full(&self) -> bool { 99b2ca6800SLoGin self.core.is_full(self.elements, &self.data) 100b2ca6800SLoGin } 101b2ca6800SLoGin 102b2ca6800SLoGin #[inline] is_empty(&self) -> bool103b2ca6800SLoGin fn is_empty(&self) -> bool { 104b2ca6800SLoGin self.core.is_empty(&self.data) 105b2ca6800SLoGin } 106b2ca6800SLoGin 107b2ca6800SLoGin #[inline] as_bytes(&self) -> &[u8]108b2ca6800SLoGin unsafe fn as_bytes(&self) -> &[u8] { 109b2ca6800SLoGin core::slice::from_raw_parts( 110b2ca6800SLoGin self.data.as_ptr() as *const u8, 111b2ca6800SLoGin core::mem::size_of::<Self>(), 112b2ca6800SLoGin ) 113b2ca6800SLoGin } 114b2ca6800SLoGin set_all(&mut self, value: bool)115b2ca6800SLoGin fn set_all(&mut self, value: bool) { 116b2ca6800SLoGin self.core.set_all(self.elements, &mut self.data, value); 117b2ca6800SLoGin } 118b2ca6800SLoGin } 1197db6e063SLoGin 120*0102d69fSLoGin impl BitAnd for &AllocBitmap { 121*0102d69fSLoGin type Output = AllocBitmap; 1227db6e063SLoGin bitand(self, rhs: Self) -> Self::Output1237db6e063SLoGin fn bitand(self, rhs: Self) -> Self::Output { 1247db6e063SLoGin let mut result = AllocBitmap::new(self.elements); 1257db6e063SLoGin for i in 0..rhs.data.len() { 1267db6e063SLoGin result.data[i] = self.data[i] & rhs.data[i]; 1277db6e063SLoGin } 1287db6e063SLoGin result 1297db6e063SLoGin } 1307db6e063SLoGin } 131*0102d69fSLoGin 132*0102d69fSLoGin impl BitAnd for AllocBitmap { 133*0102d69fSLoGin type Output = AllocBitmap; 134*0102d69fSLoGin bitand(self, rhs: Self) -> Self::Output135*0102d69fSLoGin fn bitand(self, rhs: Self) -> Self::Output { 136*0102d69fSLoGin &self & &rhs 137*0102d69fSLoGin } 138*0102d69fSLoGin } 139