Debian Codegraph @

third_party/rust/enumset/src/repr/array.rs

raw
1 use crate::repr::primitive::PrimitiveIter;
2 use crate::repr::EnumSetTypeRepr;
3 use core::ops::*;
4
5 /// An implementation of `EnumSetTypeRepr` based on an arbitrary size array.
6 ///
7 /// `N` **must** not be `0`, or else everything will break.
8 #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Hash)]
9 pub struct ArrayRepr<const N: usize>(pub [u64; N]);
10 impl<const N: usize> ArrayRepr<N> {
11 fn split_bit(bit: u32) -> (usize, u32) {
12 (bit as usize / 64, bit % 64)
13 }
14 }
15
16 impl<const N: usize> BitAnd for ArrayRepr<N> {
17 type Output = Self;
18 fn bitand(mut self, rhs: Self) -> Self::Output {
19 for i in 0..N {
20 self.0[i] &= rhs.0[i];
21 }
22 self
23 }
24 }
25 impl<const N: usize> BitOr for ArrayRepr<N> {
26 type Output = Self;
27 fn bitor(mut self, rhs: Self) -> Self::Output {
28 for i in 0..N {
29 self.0[i] |= rhs.0[i];
30 }
31 self
32 }
33 }
34 impl<const N: usize> BitXor for ArrayRepr<N> {
35 type Output = Self;
36 fn bitxor(mut self, rhs: Self) -> Self::Output {
37 for i in 0..N {
38 self.0[i] ^= rhs.0[i];
39 }
40 self
41 }
42 }
43 impl<const N: usize> Not for ArrayRepr<N> {
44 type Output = Self;
45 fn not(mut self) -> Self::Output {
46 for i in 0..N {
47 self.0[i] = !self.0[i];
48 }
49 self
50 }
51 }
52
53 impl<const N: usize> EnumSetTypeRepr for ArrayRepr<N> {
54 const PREFERRED_ARRAY_LEN: usize = N;
55 const WIDTH: u32 = N as u32 * 64;
56 const EMPTY: Self = ArrayRepr([0; N]);
57
58 fn is_empty(&self) -> bool {
59 self.0.iter().all(|x| *x == 0)
60 }
61
62 fn add_bit(&mut self, bit: u32) {
63 let (idx, bit) = Self::split_bit(bit);
64 self.0[idx].add_bit(bit);
65 }
66 fn remove_bit(&mut self, bit: u32) {
67 let (idx, bit) = Self::split_bit(bit);
68 self.0[idx].remove_bit(bit);
69 }
70 fn has_bit(&self, bit: u32) -> bool {
71 let (idx, bit) = Self::split_bit(bit);
72 self.0[idx].has_bit(bit)
73 }
74
75 fn count_ones(&self) -> u32 {
76 self.0.iter().map(|x| x.count_ones()).sum()
77 }
78 fn leading_zeros(&self) -> u32 {
79 let mut accum = 0;
80 for i in (0..N).rev() {
81 if self.0[i] != 0 {
82 return accum + self.0[i].leading_zeros();
83 }
84 accum += 64;
85 }
86 Self::WIDTH
87 }
88 fn trailing_zeros(&self) -> u32 {
89 let mut accum = 0;
90 for i in 0..N {
91 if self.0[i] != 0 {
92 return accum + self.0[i].trailing_zeros();
93 }
94 accum += 64;
95 }
96 Self::WIDTH
97 }
98
99 fn and_not(&self, other: Self) -> Self {
100 let mut new = Self([0; N]);
101 for i in 0..N {
102 new.0[i] = self.0[i] & !other.0[i];
103 }
104 new
105 }
106
107 type Iter = ArrayIter<N>;
108 fn iter(self) -> Self::Iter {
109 ArrayIter::new(self)
110 }
111
112 fn from_u8(v: u8) -> Self {
113 Self::from_u64(v as u64)
114 }
115 fn from_u16(v: u16) -> Self {
116 Self::from_u64(v as u64)
117 }
118 fn from_u32(v: u32) -> Self {
119 Self::from_u64(v as u64)
120 }
121 fn from_u64(v: u64) -> Self {
122 let mut new = Self([0; N]);
123 new.0[0] = v;
124 new
125 }
126 fn from_u128(v: u128) -> Self {
127 let mut new = Self([0; N]);
128 new.0[0] = v as u64;
129 if N != 1 {
130 new.0[1] = (v >> 64) as u64;
131 }
132 new
133 }
134 fn from_usize(v: usize) -> Self {
135 Self::from_u64(v as u64)
136 }
137
138 fn from_u8_opt(v: u8) -> Option<Self> {
139 Some(Self::from_u8(v))
140 }
141 fn from_u16_opt(v: u16) -> Option<Self> {
142 Some(Self::from_u16(v))
143 }
144 fn from_u32_opt(v: u32) -> Option<Self> {
145 Some(Self::from_u32(v))
146 }
147 fn from_u64_opt(v: u64) -> Option<Self> {
148 Some(Self::from_u64(v))
149 }
150 fn from_u128_opt(v: u128) -> Option<Self> {
151 if N == 1 && (v >> 64) != 0 {
152 None
153 } else {
154 Some(Self::from_u128(v))
155 }
156 }
157 fn from_usize_opt(v: usize) -> Option<Self> {
158 Some(Self::from_usize(v))
159 }
160
161 fn to_u8(&self) -> u8 {
162 self.to_u64().to_u8()
163 }
164 fn to_u16(&self) -> u16 {
165 self.to_u64().to_u16()
166 }
167 fn to_u32(&self) -> u32 {
168 self.to_u64().to_u32()
169 }
170 fn to_u64(&self) -> u64 {
171 self.0[0]
172 }
173 fn to_u128(&self) -> u128 {
174 let hi = if N == 1 { 0 } else { (self.0[1] as u128) << 64 };
175 self.0[0] as u128 | hi
176 }
177 fn to_usize(&self) -> usize {
178 self.to_u64().to_usize()
179 }
180
181 fn to_u8_opt(&self) -> Option<u8> {
182 self.to_u64_opt().and_then(|x| x.to_u8_opt())
183 }
184 fn to_u16_opt(&self) -> Option<u16> {
185 self.to_u64_opt().and_then(|x| x.to_u16_opt())
186 }
187 fn to_u32_opt(&self) -> Option<u32> {
188 self.to_u64_opt().and_then(|x| x.to_u32_opt())
189 }
190 fn to_u64_opt(&self) -> Option<u64> {
191 for i in 1..N {
192 if self.0[i] != 0 {
193 return None;
194 }
195 }
196 Some(self.to_u64())
197 }
198 fn to_u128_opt(&self) -> Option<u128> {
199 for i in 2..N {
200 if self.0[i] != 0 {
201 return None;
202 }
203 }
204 Some(self.to_u128())
205 }
206 fn to_usize_opt(&self) -> Option<usize> {
207 self.to_u64_opt().and_then(|x| x.to_usize_opt())
208 }
209
210 fn to_u64_array<const O: usize>(&self) -> [u64; O] {
211 let mut array = [0; O];
212 let copy_len = if N < O { N } else { O };
213 array[..copy_len].copy_from_slice(&self.0[..copy_len]);
214 array
215 }
216 fn to_u64_array_opt<const O: usize>(&self) -> Option<[u64; O]> {
217 if N > O {
218 for i in O..N {
219 if self.0[i] != 0 {
220 return None;
221 }
222 }
223 }
224 Some(self.to_u64_array())
225 }
226
227 fn from_u64_array<const O: usize>(v: [u64; O]) -> Self {
228 ArrayRepr(ArrayRepr::<O>(v).to_u64_array::<N>())
229 }
230 fn from_u64_array_opt<const O: usize>(v: [u64; O]) -> Option<Self> {
231 ArrayRepr::<O>(v).to_u64_array_opt::<N>().map(ArrayRepr)
232 }
233
234 fn to_u64_slice(&self, out: &mut [u64]) {
235 let copy_len = if N < out.len() { N } else { out.len() };
236 out[..copy_len].copy_from_slice(&self.0[..copy_len]);
237 for i in copy_len..out.len() {
238 out[i] = 0;
239 }
240 }
241 #[must_use]
242 fn to_u64_slice_opt(&self, out: &mut [u64]) -> Option<()> {
243 if N > out.len() {
244 for i in out.len()..N {
245 if self.0[i] != 0 {
246 return None;
247 }
248 }
249 }
250 self.to_u64_slice(out);
251 Some(())
252 }
253
254 fn from_u64_slice(v: &[u64]) -> Self {
255 let mut new = ArrayRepr([0; N]);
256 let copy_len = if N < v.len() { N } else { v.len() };
257 new.0[..copy_len].copy_from_slice(&v[..copy_len]);
258 new
259 }
260 fn from_u64_slice_opt(v: &[u64]) -> Option<Self> {
261 if v.len() > N {
262 for i in N..v.len() {
263 if v[i] != 0 {
264 return None;
265 }
266 }
267 }
268 Some(Self::from_u64_slice(v))
269 }
270 }
271
272 #[derive(Clone, Debug)]
273 pub struct ArrayIter<const N: usize> {
274 data: [PrimitiveIter<u64>; N],
275 done: bool,
276 idx_f: usize,
277 idx_r: usize,
278 }
279
280 impl<const N: usize> ArrayIter<N> {
281 pub fn new(array: ArrayRepr<N>) -> Self {
282 let mut new = [PrimitiveIter(0); N];
283 for i in 0..N {
284 new[i] = PrimitiveIter(array.0[i])
285 }
286 ArrayIter { data: new, done: false, idx_f: 0, idx_r: N - 1 }
287 }
288 }
289
290 impl<const N: usize> Iterator for ArrayIter<N> {
291 type Item = u32;
292
293 fn next(&mut self) -> Option<Self::Item> {
294 if self.done {
295 return None;
296 }
297 while self.idx_f <= self.idx_r {
298 if let Some(x) = self.data[self.idx_f].next() {
299 return Some(self.idx_f as u32 * 64 + x);
300 } else {
301 self.idx_f += 1;
302 }
303 }
304 self.done = true;
305 None
306 }
307
308 fn size_hint(&self) -> (usize, Option<usize>) {
309 let mut sum = 0;
310 for i in self.idx_f..self.idx_r + 1 {
311 sum += self.data[i].0.count_ones() as usize;
312 }
313 (sum, Some(sum))
314 }
315 }
316
317 impl<const N: usize> DoubleEndedIterator for ArrayIter<N> {
318 fn next_back(&mut self) -> Option<Self::Item> {
319 if self.done {
320 return None;
321 }
322 while self.idx_f <= self.idx_r {
323 if let Some(x) = self.data[self.idx_r].next_back() {
324 return Some(self.idx_r as u32 * 64 + x);
325 } else {
326 if self.idx_r == 0 {
327 break;
328 }
329 self.idx_r -= 1;
330 }
331 }
332 self.done = true;
333 None
334 }
335 }