static_bitmap.rs 3.0 KB

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