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