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> Default for StaticBitmap<N> 18 where 19 [(); (N + usize::BITS as usize - 1) / (usize::BITS as usize)]:, 20 { 21 fn default() -> Self { 22 Self::new() 23 } 24 } 25 26 impl<const N: usize> StaticBitmap<N> 27 where 28 [(); (N + usize::BITS as usize - 1) / (usize::BITS as usize)]:, 29 { 30 /// 创建一个新的静态位图 31 pub const fn new() -> Self { 32 Self { 33 data: [0; (N + usize::BITS as usize - 1) / (usize::BITS as usize)], 34 core: BitMapCore::new(), 35 } 36 } 37 } 38 39 impl<const N: usize> BitMapOps<usize> for StaticBitmap<N> 40 where 41 [(); (N + usize::BITS as usize - 1) / (usize::BITS as usize)]:, 42 { 43 #[inline] 44 fn get(&self, index: usize) -> Option<bool> { 45 return self.core.get(N, &self.data, index); 46 } 47 48 #[inline] 49 fn set(&mut self, index: usize, value: bool) -> Option<bool> { 50 return self.core.set(N, &mut self.data, index, value); 51 } 52 53 #[inline] 54 fn len(&self) -> usize { 55 N 56 } 57 58 #[inline] 59 fn size(&self) -> usize { 60 self.data.len() * size_of::<usize>() 61 } 62 63 #[inline] 64 fn first_index(&self) -> Option<usize> { 65 self.core.first_index(&self.data) 66 } 67 68 #[inline] 69 fn first_false_index(&self) -> Option<usize> { 70 self.core.first_false_index(N, &self.data) 71 } 72 73 #[inline] 74 fn last_index(&self) -> Option<usize> { 75 self.core.last_index(N, &self.data) 76 } 77 78 #[inline] 79 fn last_false_index(&self) -> Option<usize> { 80 self.core.last_false_index(N, &self.data) 81 } 82 83 #[inline] 84 fn next_index(&self, index: usize) -> Option<usize> { 85 self.core.next_index(N, &self.data, index) 86 } 87 88 #[inline] 89 fn next_false_index(&self, index: usize) -> Option<usize> { 90 self.core.next_false_index(N, &self.data, index) 91 } 92 93 #[inline] 94 fn prev_index(&self, index: usize) -> Option<usize> { 95 self.core.prev_index(N, &self.data, index) 96 } 97 98 #[inline] 99 fn prev_false_index(&self, index: usize) -> Option<usize> { 100 self.core.prev_false_index(N, &self.data, index) 101 } 102 103 #[inline] 104 fn invert(&mut self) { 105 self.core.invert(N, &mut self.data); 106 } 107 108 #[inline] 109 fn is_full(&self) -> bool { 110 self.core.is_full(N, &self.data) 111 } 112 113 #[inline] 114 fn is_empty(&self) -> bool { 115 self.core.is_empty(&self.data) 116 } 117 118 #[inline] 119 unsafe fn as_bytes(&self) -> &[u8] { 120 core::slice::from_raw_parts( 121 self.data.as_ptr() as *const u8, 122 core::mem::size_of::<Self>(), 123 ) 124 } 125 126 fn set_all(&mut self, value: bool) { 127 self.core.set_all(N, &mut self.data, value); 128 } 129 } 130