static_bitmap.rs 2.8 KB

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