1 use core::mem::size_of; 2 3 use crate::{bitmap_core::BitMapCore, traits::BitMapOps}; 4 5 /// 静态位图 6 /// 7 /// 该位图的大小在编译时确定,不可变 8 #[derive(Debug, Clone)] 9 pub struct StaticBitmap<const N: usize> 10 where 11 [(); (N + usize::BITS as usize - 1) / (usize::BITS as usize)]:, 12 { 13 pub data: [usize; (N + usize::BITS as usize - 1) / (usize::BITS as usize)], 14 core: BitMapCore<usize>, 15 } 16 17 impl<const N: usize> StaticBitmap<N> 18 where 19 [(); (N + usize::BITS as usize - 1) / (usize::BITS as usize)]:, 20 { 21 /// 创建一个新的静态位图 new() -> Self22 pub const fn new() -> Self { 23 Self { 24 data: [0; (N + usize::BITS as usize - 1) / (usize::BITS as usize)], 25 core: BitMapCore::new(), 26 } 27 } 28 } 29 30 impl<const N: usize> BitMapOps<usize> for StaticBitmap<N> 31 where 32 [(); (N + usize::BITS as usize - 1) / (usize::BITS as usize)]:, 33 { 34 #[inline] get(&self, index: usize) -> Option<bool>35 fn get(&self, index: usize) -> Option<bool> { 36 return self.core.get(N, &self.data, index); 37 } 38 39 #[inline] set(&mut self, index: usize, value: bool) -> Option<bool>40 fn set(&mut self, index: usize, value: bool) -> Option<bool> { 41 return self.core.set(N, &mut self.data, index, value); 42 } 43 44 #[inline] len(&self) -> usize45 fn len(&self) -> usize { 46 N 47 } 48 49 #[inline] size(&self) -> usize50 fn size(&self) -> usize { 51 self.data.len() * size_of::<usize>() 52 } 53 54 #[inline] first_index(&self) -> Option<usize>55 fn first_index(&self) -> Option<usize> { 56 self.core.first_index(&self.data) 57 } 58 59 #[inline] first_false_index(&self) -> Option<usize>60 fn first_false_index(&self) -> Option<usize> { 61 self.core.first_false_index(N, &self.data) 62 } 63 64 #[inline] last_index(&self) -> Option<usize>65 fn last_index(&self) -> Option<usize> { 66 self.core.last_index(N, &self.data) 67 } 68 69 #[inline] last_false_index(&self) -> Option<usize>70 fn last_false_index(&self) -> Option<usize> { 71 self.core.last_false_index(N, &self.data) 72 } 73 74 #[inline] next_index(&self, index: usize) -> Option<usize>75 fn next_index(&self, index: usize) -> Option<usize> { 76 self.core.next_index(N, &self.data, index) 77 } 78 79 #[inline] next_false_index(&self, index: usize) -> Option<usize>80 fn next_false_index(&self, index: usize) -> Option<usize> { 81 self.core.next_false_index(N, &self.data, index) 82 } 83 84 #[inline] prev_index(&self, index: usize) -> Option<usize>85 fn prev_index(&self, index: usize) -> Option<usize> { 86 self.core.prev_index(N, &self.data, index) 87 } 88 89 #[inline] prev_false_index(&self, index: usize) -> Option<usize>90 fn prev_false_index(&self, index: usize) -> Option<usize> { 91 self.core.prev_false_index(N, &self.data, index) 92 } 93 94 #[inline] invert(&mut self)95 fn invert(&mut self) { 96 self.core.invert(N, &mut self.data); 97 } 98 99 #[inline] is_full(&self) -> bool100 fn is_full(&self) -> bool { 101 self.core.is_full(N, &self.data) 102 } 103 104 #[inline] is_empty(&self) -> bool105 fn is_empty(&self) -> bool { 106 self.core.is_empty(&self.data) 107 } 108 109 #[inline] as_bytes(&self) -> &[u8]110 unsafe fn as_bytes(&self) -> &[u8] { 111 core::slice::from_raw_parts( 112 self.data.as_ptr() as *const u8, 113 core::mem::size_of::<Self>(), 114 ) 115 } 116 set_all(&mut self, value: bool)117 fn set_all(&mut self, value: bool) { 118 self.core.set_all(N, &mut self.data, value); 119 } 120 } 121