static_bitmap.rs 3.2 KB

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