xref: /DragonOS/kernel/crates/bitmap/src/static_bitmap.rs (revision eb49bb993a39964f92494ec3effafed3fb9adfd8)
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     /// 创建一个新的静态位图
22     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]
35     fn get(&self, index: usize) -> Option<bool> {
36         return self.core.get(N, &self.data, index);
37     }
38 
39     #[inline]
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]
45     fn len(&self) -> usize {
46         N
47     }
48 
49     #[inline]
50     fn size(&self) -> usize {
51         self.data.len() * size_of::<usize>()
52     }
53 
54     #[inline]
55     fn first_index(&self) -> Option<usize> {
56         self.core.first_index(&self.data)
57     }
58 
59     #[inline]
60     fn first_false_index(&self) -> Option<usize> {
61         self.core.first_false_index(N, &self.data)
62     }
63 
64     #[inline]
65     fn last_index(&self) -> Option<usize> {
66         self.core.last_index(N, &self.data)
67     }
68 
69     #[inline]
70     fn last_false_index(&self) -> Option<usize> {
71         self.core.last_false_index(N, &self.data)
72     }
73 
74     #[inline]
75     fn next_index(&self, index: usize) -> Option<usize> {
76         self.core.next_index(N, &self.data, index)
77     }
78 
79     #[inline]
80     fn next_false_index(&self, index: usize) -> Option<usize> {
81         self.core.next_false_index(N, &self.data, index)
82     }
83 
84     #[inline]
85     fn prev_index(&self, index: usize) -> Option<usize> {
86         self.core.prev_index(N, &self.data, index)
87     }
88 
89     #[inline]
90     fn prev_false_index(&self, index: usize) -> Option<usize> {
91         self.core.prev_false_index(N, &self.data, index)
92     }
93 
94     #[inline]
95     fn invert(&mut self) {
96         self.core.invert(N, &mut self.data);
97     }
98 
99     #[inline]
100     fn is_full(&self) -> bool {
101         self.core.is_full(N, &self.data)
102     }
103 
104     #[inline]
105     fn is_empty(&self) -> bool {
106         self.core.is_empty(&self.data)
107     }
108 
109     #[inline]
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 
117     fn set_all(&mut self, value: bool) {
118         self.core.set_all(N, &mut self.data, value);
119     }
120 }
121