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 { default() -> Self21 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 /// 创建一个新的静态位图 new() -> Self31 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] get(&self, index: usize) -> Option<bool>44 fn get(&self, index: usize) -> Option<bool> { 45 return self.core.get(N, &self.data, index); 46 } 47 48 #[inline] set(&mut self, index: usize, value: bool) -> Option<bool>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] len(&self) -> usize54 fn len(&self) -> usize { 55 N 56 } 57 58 #[inline] size(&self) -> usize59 fn size(&self) -> usize { 60 self.data.len() * size_of::<usize>() 61 } 62 63 #[inline] first_index(&self) -> Option<usize>64 fn first_index(&self) -> Option<usize> { 65 self.core.first_index(&self.data) 66 } 67 68 #[inline] first_false_index(&self) -> Option<usize>69 fn first_false_index(&self) -> Option<usize> { 70 self.core.first_false_index(N, &self.data) 71 } 72 73 #[inline] last_index(&self) -> Option<usize>74 fn last_index(&self) -> Option<usize> { 75 self.core.last_index(N, &self.data) 76 } 77 78 #[inline] last_false_index(&self) -> Option<usize>79 fn last_false_index(&self) -> Option<usize> { 80 self.core.last_false_index(N, &self.data) 81 } 82 83 #[inline] next_index(&self, index: usize) -> Option<usize>84 fn next_index(&self, index: usize) -> Option<usize> { 85 self.core.next_index(N, &self.data, index) 86 } 87 88 #[inline] next_false_index(&self, index: usize) -> Option<usize>89 fn next_false_index(&self, index: usize) -> Option<usize> { 90 self.core.next_false_index(N, &self.data, index) 91 } 92 93 #[inline] prev_index(&self, index: usize) -> Option<usize>94 fn prev_index(&self, index: usize) -> Option<usize> { 95 self.core.prev_index(N, &self.data, index) 96 } 97 98 #[inline] prev_false_index(&self, index: usize) -> Option<usize>99 fn prev_false_index(&self, index: usize) -> Option<usize> { 100 self.core.prev_false_index(N, &self.data, index) 101 } 102 103 #[inline] invert(&mut self)104 fn invert(&mut self) { 105 self.core.invert(N, &mut self.data); 106 } 107 108 #[inline] is_full(&self) -> bool109 fn is_full(&self) -> bool { 110 self.core.is_full(N, &self.data) 111 } 112 113 #[inline] is_empty(&self) -> bool114 fn is_empty(&self) -> bool { 115 self.core.is_empty(&self.data) 116 } 117 118 #[inline] as_bytes(&self) -> &[u8]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 set_all(&mut self, value: bool)126 fn set_all(&mut self, value: bool) { 127 self.core.set_all(N, &mut self.data, value); 128 } 129 } 130