crab-vault-utils 0.2.13

All the utils of crab vault
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
//! # 位图模块
//!
//! 这个模块提供了一个通用的 [`Bitmap`] 结构,用于高效地进行位操作。
//!
//! [`Bitmap`] 由一个实现了 [`BitStorage`] trait 的泛型整数类型(如 `u8`, `u16`, `u32`, `u64`, `u128`)支持。
//! 它封装了底层的位运算,提供了创建、修改、查询位图以及在置位 (1) 和未置位 (0) 的位上进行迭代的功能。
//!
//! ## 主要功能
//!
//! - **泛型实现**: 可以使用任何常见的无符号整数作为底层存储。
//! - **完整的位运算**: 支持 `&`, `|`, `^`, `!` 等所有标准位运算符。
//! - **迭代器**: 提供 [`PositiveIter`] 和 [`NegativeIter`],分别用于遍历值为 1 和 0 的位的索引。
//! - **丰富的 API**: 包含 [`set`](Bitmap::set), [`get`](Bitmap::get), [`count_ones`](Bitmap::count_ones), [`any`](Bitmap::any), [`all`](Bitmap::all), [`none`](Bitmap::none) 等常用方法。
//!
//! ## 示例
//!
//! ```
//! # use crab_vault_utils::bitmap::{Bitmap, BitStorage};
//! // 使用 u32 作为存储,创建一个 32 位的位图
//! let mut artists = Bitmap::<u32>::new();
//!
//! // 将索引为 2, 8, 9 的位设置为 1
//! artists.set(2, true);
//! artists.set(8, true);
//! artists.set(9, true);
//!
//! // 检查索引为 8 的位是否为 1
//! assert!(artists.get(8));
//! // 检查索引为 5 的位是否为 0
//! assert!(!artists.get(5));
//!
//! // 计算有多少个位被设置了
//! assert_eq!(artists.count_ones(), 3);
//!
//! // 使用迭代器收集所有值为 1 的位的索引
//! let set_bits: Vec<usize> = artists.iter_ones().collect();
//! assert_eq!(set_bits, vec![2, 8, 9]);
//!
//! // 创建另一个位图并进行合并
//! let mut other_artists = Bitmap::<u32>::new();
//! other_artists.set(9, true);
//! other_artists.set(15, true);
//!
//! let all_artists = artists | other_artists;
//! let expected_bits: Vec<usize> = all_artists.iter_ones().collect();
//! assert_eq!(expected_bits, vec![2, 8, 9, 15]);
//! ```

use std::fmt::Debug;
use std::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Not, Shl, Shr};

pub trait BitStorage:
    Copy
    + Default
    + Debug
    + BitAnd<Output = Self>
    + BitAndAssign
    + BitOr<Output = Self>
    + BitOrAssign
    + BitXor<Output = Self>
    + BitXorAssign
    + Not<Output = Self>
    + From<u8>
    + Shl<usize, Output = Self>
    + Shr<usize, Output = Self>
    + PartialEq
    + Eq
{
    const BITS: usize;
    fn trailing_zeros(self) -> u32;
    fn count_ones(self) -> u32;
    fn count_zeros(self) -> u32;
}

macro_rules! impl_bit_storage_for_basic_types {
    ($($storage_type: ty), *) => {
        $(
            impl BitStorage for $storage_type {
                const BITS: usize = std::mem::size_of::<$storage_type>() * 8;

                #[inline]
                fn trailing_zeros(self) -> u32 {
                    self.trailing_zeros()
                }

                #[inline]
                fn count_ones(self) -> u32 {
                    self.count_ones()
                }

                #[inline]
                fn count_zeros(self) -> u32 {
                    self.count_zeros()
                }
            }
        )*
    };
}

impl_bit_storage_for_basic_types!(u8, u16, u32, u64, u128);

/// 一个通用的位图结构,由一个实现了 [`BitStorage`] 的类型支持。
///
/// # 示例
/// ```
/// # use crab_vault_utils::bitmap::{Bitmap, BitStorage};
/// // 使用 u16 作为存储类型,总共 16 位
/// let mut bitmap = Bitmap::<u16>::new();
/// bitmap.set(3, true);
/// bitmap.set(5, true);
///
/// assert!(bitmap.get(3));
/// assert!(!bitmap.get(4));
///
/// let positions: Vec<usize> = bitmap.iter_ones().collect();
/// assert_eq!(positions, vec![3, 5]);
/// ```
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Bitmap<T: BitStorage> {
    inner: T,
}

/// 一个迭代器,用于遍历位图中所有值为 1 (positive) 的位索引。
pub struct PositiveIter<T: BitStorage> {
    bitmap: Bitmap<T>,
}

/// 一个迭代器,用于遍历位图中所有值为 0 (negative) 的位索引。
pub struct NegativeIter<T: BitStorage> {
    bitmap: Bitmap<T>,
}

impl<T: BitStorage> From<NegativeIter<T>> for PositiveIter<T> {
    fn from(NegativeIter { bitmap }: NegativeIter<T>) -> Self {
        Self { bitmap }
    }
}

impl<T: BitStorage> From<PositiveIter<T>> for NegativeIter<T> {
    fn from(PositiveIter { bitmap }: PositiveIter<T>) -> Self {
        Self { bitmap }
    }
}

impl<T: BitStorage> Iterator for PositiveIter<T> {
    type Item = usize;

    fn next(&mut self) -> Option<Self::Item> {
        if self.bitmap.inner == T::from(0) {
            return None;
        }

        let next_bit_pos = self.bitmap.inner.trailing_zeros() as usize;

        // 清除刚刚找到的位,以便下一次迭代
        let mask = T::from(1) << next_bit_pos;
        self.bitmap.inner &= !mask;
        Some(next_bit_pos)
    }
}

impl<T: BitStorage> Iterator for NegativeIter<T> {
    type Item = usize;

    fn next(&mut self) -> Option<Self::Item> {
        // 如果所有位都为 1,则没有 0 可以迭代
        if self.bitmap.inner == !T::from(0) {
            return None;
        }

        let next_bit_pos = (!self.bitmap.inner).trailing_zeros() as usize;

        // 设置刚刚找到的位为 1,以便下一次迭代
        let mask = T::from(1) << next_bit_pos;
        self.bitmap.inner |= mask;
        Some(next_bit_pos)
    }
}

impl<T: BitStorage> PositiveIter<T> {
    /// 将正迭代器(遍历 1)转换为负迭代器(遍历 0)。
    ///
    /// # 示例
    /// ```
    /// # use crab_vault_utils::bitmap::{Bitmap, BitStorage};
    /// let mut bitmap = Bitmap::<u8>::new();
    /// bitmap.set(1, true);
    /// bitmap.set(3, true);
    ///
    /// let positive_iter = bitmap.iter_ones();
    /// let negative_iter = positive_iter.invert();
    /// let zeros: Vec<usize> = negative_iter.collect();
    ///
    /// assert_eq!(zeros, vec![0, 2, 4, 5, 6, 7]);
    /// ```
    #[inline]
    pub fn invert(self) -> NegativeIter<T> {
        self.into()
    }
}

impl<T: BitStorage> NegativeIter<T> {
    /// 将负迭代器(遍历 0)转换为正迭代器(遍历 1)。
    ///
    /// # 示例
    /// ```
    /// # use crab_vault_utils::bitmap::{Bitmap, BitStorage};
    /// let mut bitmap = Bitmap::<u8>::new_full();
    /// bitmap.set(1, false);
    /// bitmap.set(3, false);
    ///
    /// let negative_iter = bitmap.iter_zeros();
    /// let positive_iter = negative_iter.invert();
    /// let ones: Vec<usize> = positive_iter.collect();
    ///
    /// assert_eq!(ones, vec![0, 2, 4, 5, 6, 7]);
    /// ```
    #[inline]
    pub fn invert(self) -> PositiveIter<T> {
        self.into()
    }
}

impl<T: BitStorage> IntoIterator for &Bitmap<T> {
    type Item = usize;
    type IntoIter = PositiveIter<T>;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.iter_ones()
    }
}

impl<T: BitStorage> IntoIterator for Bitmap<T> {
    type Item = usize;
    type IntoIter = PositiveIter<T>;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.iter_ones()
    }
}

impl<T: BitStorage> From<T> for Bitmap<T> {
    #[inline]
    fn from(val: T) -> Self {
        Self { inner: val }
    }
}

impl<T: BitStorage> Bitmap<T> {
    /// 创建一个所有位都为 0 的空位图。
    ///
    /// # 示例
    /// ```
    /// # use crab_vault_utils::bitmap::{Bitmap, BitStorage};
    /// let bitmap = Bitmap::<u16>::new_empty();
    /// assert!(bitmap.none());
    /// assert_eq!(bitmap.count_ones(), 0);
    /// ```
    #[inline]
    pub fn new_empty() -> Self {
        Self { inner: T::from(0) }
    }

    /// 创建一个所有位都为 1 的全满位图。
    ///
    /// # 示例
    /// ```
    /// # use crab_vault_utils::bitmap::{Bitmap, BitStorage};
    /// let bitmap = Bitmap::<u8>::new_full();
    /// assert!(bitmap.all());
    /// assert_eq!(bitmap.count_ones(), 8);
    /// ```
    #[inline]
    pub fn new_full() -> Self {
        Self { inner: !T::from(0) }
    }

    /// 创建一个空的位图,是 `new_empty` 的别名。
    ///
    /// # 示例
    /// ```
    /// # use crab_vault_utils::bitmap::{Bitmap, BitStorage};
    /// let bitmap = Bitmap::<u32>::new();
    /// assert!(bitmap.none());
    /// ```
    #[inline]
    pub fn new() -> Self {
        Self::new_empty()
    }

    /// 返回一个迭代器,用于遍历所有值为 1 的位的索引。
    ///
    /// # 示例
    /// ```
    /// # use crab_vault_utils::bitmap::{Bitmap, BitStorage};
    /// let mut bitmap = Bitmap::<u8>::new();
    /// bitmap.set(2, true);
    /// bitmap.set(6, true);
    /// let ones: Vec<usize> = bitmap.iter_ones().collect();
    /// assert_eq!(ones, vec![2, 6]);
    /// ```
    #[inline]
    pub const fn iter_ones(&self) -> PositiveIter<T> {
        PositiveIter { bitmap: *self }
    }

    /// 返回一个迭代器,用于遍历所有值为 0 的位的索引。
    ///
    /// # 示例
    /// ```
    /// # use crab_vault_utils::bitmap::{Bitmap, BitStorage};
    /// let mut bitmap = Bitmap::<u8>::new();
    /// bitmap.set(0, true);
    /// bitmap.set(1, true);
    /// bitmap.set(2, true);
    /// bitmap.set(3, true);
    /// // 内部值为 ...00001111
    /// let zeros: Vec<usize> = bitmap.iter_zeros().collect();
    /// assert_eq!(zeros, vec![4, 5, 6, 7]);
    /// ```
    #[inline]
    pub const fn iter_zeros(&self) -> NegativeIter<T> {
        NegativeIter { bitmap: *self }
    }

    /// 设置指定索引的位。
    ///
    /// `true` 表示设置为 1,`false` 表示设置为 0。
    ///
    /// # Panics
    ///
    /// 如果 `idx` 超出位图的范围(`idx >= T::BITS`),在调试模式下会触发 panic。
    ///
    /// # 示例
    /// ```
    /// # use crab_vault_utils::bitmap::{Bitmap, BitStorage};
    /// let mut bitmap = Bitmap::<u8>::new();
    /// bitmap.set(5, true);
    /// assert!(bitmap.get(5));
    /// bitmap.set(5, false);
    /// assert!(!bitmap.get(5));
    /// ```
    #[inline]
    pub fn set(&mut self, idx: usize, set: bool) {
        debug_assert!(idx < T::BITS, "Index out of bounds");
        let mask = T::from(1) << idx;
        if set {
            self.inner |= mask;
        } else {
            self.inner &= !mask;
        }
    }

    /// 获取指定索引的位的值。
    ///
    /// 返回 `true` 如果该位为 1,否则返回 `false`。
    ///
    /// # Panics
    ///
    /// 如果 `idx` 超出位图的范围(`idx >= T::BITS`),在调试模式下会触发 panic。
    ///
    /// # 示例
    /// ```
    /// # use crab_vault_utils::bitmap::{Bitmap, BitStorage};
    /// let mut bitmap = Bitmap::<u8>::new();
    /// bitmap.set(7, true);
    /// assert_eq!(bitmap.get(7), true);
    /// assert_eq!(bitmap.get(0), false);
    /// ```
    #[inline]
    pub fn get(&self, idx: usize) -> bool {
        debug_assert!(idx < T::BITS, "Index out of bounds");
        let mask = T::from(1) << idx;
        (self.inner & mask) != T::from(0)
    }

    /// 检查指定索引的位是否为 1。`get` 的别名。
    ///
    /// # 示例
    /// ```
    /// # use crab_vault_utils::bitmap::{Bitmap, BitStorage};
    /// let mut bitmap = Bitmap::<u8>::new();
    /// bitmap.set(1, true);
    /// assert!(bitmap.is_one_on(1));
    /// ```
    #[inline]
    pub fn is_one_on(&self, idx: usize) -> bool {
        self.get(idx)
    }

    /// 检查指定索引的位是否为 0。
    ///
    /// # 示例
    /// ```
    /// # use crab_vault_utils::bitmap::{Bitmap, BitStorage};
    /// let mut bitmap = Bitmap::<u8>::new();
    /// bitmap.set(1, true);
    /// assert!(bitmap.is_zero_on(0));
    /// ```
    #[inline]
    pub fn is_zero_on(&self, idx: usize) -> bool {
        !self.get(idx)
    }

    /// 将两个位图进行合并(并集),等同于 `|` 按位或操作。
    ///
    /// # 示例
    /// ```
    /// # use crab_vault_utils::bitmap::{Bitmap, BitStorage};
    /// let mut b1 = Bitmap::<u8>::new();
    /// b1.set(1, true); // 00000010
    /// let mut b2 = Bitmap::<u8>::new();
    /// b2.set(2, true); // 00000100
    ///
    /// let merged = b1.merge(b2); // 00000110
    /// assert!(merged.get(1));
    /// assert!(merged.get(2));
    /// ```
    #[inline]
    pub fn merge(self, rhs: Bitmap<T>) -> Bitmap<T> {
        self | rhs
    }

    /// 计算值为 1 的位的数量。
    ///
    /// # 示例
    /// ```
    /// # use crab_vault_utils::bitmap::{Bitmap, BitStorage};
    /// let mut bitmap = Bitmap::<u8>::new();
    /// bitmap.set(0, true);
    /// bitmap.set(2, true);
    /// bitmap.set(4, true);
    /// assert_eq!(bitmap.count_ones(), 3);
    /// ```
    #[inline]
    pub fn count_ones(&self) -> u32 {
        self.inner.count_ones()
    }

    /// 计算值为 0 的位的数量。
    ///
    /// # 示例
    /// ```
    /// # use crab_vault_utils::bitmap::{Bitmap, BitStorage};
    /// let mut bitmap = Bitmap::<u8>::new();
    /// bitmap.set(0, true);
    /// bitmap.set(2, true);
    /// bitmap.set(4, true);
    /// // u8 有 8 位,3 个是 1,所以 5 个是 0
    /// assert_eq!(bitmap.count_zeros(), 5);
    /// ```
    #[inline]
    pub fn count_zeros(&self) -> u32 {
        T::BITS as u32 - self.inner.count_ones()
    }

    /// 检查位图中是否至少有一个位是 1。
    ///
    /// # 示例
    /// ```
    /// # use crab_vault_utils::bitmap::{Bitmap, BitStorage};
    /// let mut b1 = Bitmap::<u8>::new();
    /// b1.set(3, true);
    /// assert!(b1.any());
    ///
    /// let b2 = Bitmap::<u8>::new();
    /// assert!(!b2.any());
    /// ```
    #[inline]
    pub fn any(&self) -> bool {
        self.inner != T::from(0)
    }

    /// 检查位图中是否所有位都是 1。
    ///
    /// # 示例
    /// ```
    /// # use crab_vault_utils::bitmap::{Bitmap, BitStorage};
    /// let b1 = Bitmap::<u8>::new_full();
    /// assert!(b1.all());
    ///
    /// let mut b2 = Bitmap::<u8>::new_full();
    /// b2.set(4, false);
    /// assert!(!b2.all());
    /// ```
    #[inline]
    pub fn all(&self) -> bool {
        self.inner == !T::from(0)
    }

    /// 检查位图中是否所有位都是 0。
    ///
    /// # 示例
    /// ```
    /// # use crab_vault_utils::bitmap::{Bitmap, BitStorage};
    /// let b1 = Bitmap::<u8>::new();
    /// assert!(b1.none());
    ///
    /// let mut b2 = Bitmap::<u8>::new();
    /// b2.set(0, true);
    /// assert!(!b2.none());
    /// ```
    #[inline]
    pub fn none(&self) -> bool {
        self.inner == T::from(0)
    }

    /// 查找第一个值为 1 的位的索引。
    ///
    /// 如果所有位都为 0,则返回 `None`。
    ///
    /// # 示例
    /// ```
    /// # use crab_vault_utils::bitmap::{Bitmap, BitStorage};
    /// let mut bitmap = Bitmap::<u16>::new();
    /// bitmap.set(5, true);
    /// bitmap.set(10, true);
    /// assert_eq!(bitmap.first_one(), Some(5));
    ///
    /// let empty_bitmap = Bitmap::<u16>::new();
    /// assert_eq!(empty_bitmap.first_one(), None);
    /// ```
    #[inline]
    pub fn first_one(&self) -> Option<usize> {
        if self.none() {
            None
        } else {
            Some(self.inner.trailing_zeros() as usize)
        }
    }
}

impl<T: BitStorage> BitAnd for Bitmap<T> {
    type Output = Self;
    /// 按位与(&)。
    ///
    /// # 示例
    /// ```
    /// # use crab_vault_utils::bitmap::{Bitmap, BitStorage};
    /// let mut b1 = Bitmap::<u8>::from(0b__0000_1101); // 位 0, 2, 3 的值为 1
    /// let mut b2 = Bitmap::<u8>::from(0b__0000_1011); // 位 0, 1, 3 的值为 1
    /// let result = b1 & b2;
    /// let expected = Bitmap::<u8>::from(0b__0000_1001); // 位 0, 3 的值为 1
    /// assert_eq!(result, expected);
    /// ```
    fn bitand(self, rhs: Self) -> Self::Output {
        Self {
            inner: self.inner & rhs.inner,
        }
    }
}

impl<T: BitStorage> BitAndAssign for Bitmap<T> {
    /// 按位或后赋值(&=)。
    ///
    /// # 示例
    /// ```
    /// # use crab_vault_utils::bitmap::{Bitmap, BitStorage};
    /// let mut b1 = Bitmap::<u8>::from(0b__0000_1101); // 位 0, 2, 3 的值为 1
    /// let mut b2 = Bitmap::<u8>::from(0b__0000_1011); // 位 0, 1, 3 的值为 1
    /// b1 &= b2;
    /// let expected = Bitmap::<u8>::from(0b__0000_1001); // 位 0, 3 的值为 1
    /// assert_eq!(b1, expected);
    /// ```
    fn bitand_assign(&mut self, rhs: Self) {
        self.inner &= rhs.inner
    }
}

impl<T: BitStorage> BitOr for Bitmap<T> {
    type Output = Self;
    /// 按位或(|)。
    ///
    /// # 示例
    /// ```
    /// # use crab_vault_utils::bitmap::{Bitmap, BitStorage};
    /// let mut b1 = Bitmap::<u8>::from(0b__0000_1101); // 位 0, 2, 3 的值为 1
    /// let mut b2 = Bitmap::<u8>::from(0b__0000_1011); // 位 0, 1, 3 的值为 1
    /// let result = b1 | b2;
    /// let expected = Bitmap::<u8>::from(0b__0000_1111); // 位 0, 1, 2, 3 的值为 1
    /// assert_eq!(result, expected);
    /// ```
    fn bitor(self, rhs: Self) -> Self::Output {
        Self {
            inner: self.inner | rhs.inner,
        }
    }
}

impl<T: BitStorage> BitOrAssign for Bitmap<T> {
    /// 按位或后赋值(|=)。
    ///
    /// # 示例
    /// ```
    /// # use crab_vault_utils::bitmap::{Bitmap, BitStorage};
    /// let mut b1 = Bitmap::<u8>::from(0b__0000_1101); // 位 0, 2, 3 的值为 1
    /// let mut b2 = Bitmap::<u8>::from(0b__0000_1011); // 位 0, 1, 3 的值为 1
    /// b1 |= b2;
    /// let expected = Bitmap::<u8>::from(0b__0000_1111); // 位 0, 1, 2, 3 的值为 1
    /// assert_eq!(b1, expected);
    /// ```
    fn bitor_assign(&mut self, rhs: Self) {
        self.inner |= rhs.inner
    }
}

impl<T: BitStorage> BitXor for Bitmap<T> {
    type Output = Self;
    /// 按位异或(^)
    ///
    /// # 示例
    /// ```
    /// # use crab_vault_utils::bitmap::{Bitmap, BitStorage};
    /// let mut b1 = Bitmap::<u8>::from(0b__0000_1101); // 位 0, 2, 3 的值为 1
    /// let mut b2 = Bitmap::<u8>::from(0b__0000_1011); // 位 0, 1, 3 的值为 1
    /// let result = b1 ^ b2;
    /// let expected = Bitmap::<u8>::from(0b__0000_0110); // 位 1, 2 的值为 1
    /// assert_eq!(result, expected);
    /// ```
    fn bitxor(self, rhs: Self) -> Self::Output {
        Self {
            inner: self.inner ^ rhs.inner,
        }
    }
}

impl<T: BitStorage> BitXorAssign for Bitmap<T> {
    /// 按位或后赋值(^=)。
    ///
    /// # 示例
    /// ```
    /// # use crab_vault_utils::bitmap::{Bitmap, BitStorage};
    /// let mut b1 = Bitmap::<u8>::from(0b__0000_1101); // 位 0, 2, 3 的值为 1
    /// let mut b2 = Bitmap::<u8>::from(0b__0000_1011); // 位 0, 1, 3 的值为 1
    /// b1 ^= b2;
    /// let expected = Bitmap::<u8>::from(0b__0000_0110); // 位 1, 2 的值为 1
    /// assert_eq!(b1, expected);
    /// ```
    fn bitxor_assign(&mut self, rhs: Self) {
        self.inner ^= rhs.inner
    }
}

impl<T: BitStorage> Not for Bitmap<T> {
    type Output = Self;
    /// 按位取反(!)
    ///
    /// # 示例
    /// ```
    /// # use crab_vault_utils::bitmap::{Bitmap, BitStorage};
    /// let b = Bitmap::<u8>::from(0b__1111_0000);
    /// let result = !b;
    /// let expected = Bitmap::<u8>::from(0b__0000_1111);
    /// assert_eq!(result, expected);
    /// ```
    fn not(self) -> Self::Output {
        Self { inner: !self.inner }
    }
}