alloc_bitmap.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. use core::ops::BitAnd;
  2. use alloc::vec::Vec;
  3. use crate::{bitmap_core::BitMapCore, traits::BitMapOps};
  4. #[derive(Debug, Clone)]
  5. pub struct AllocBitmap {
  6. elements: usize,
  7. data: Vec<usize>,
  8. core: BitMapCore<usize>,
  9. }
  10. impl AllocBitmap {
  11. pub fn new(elements: usize) -> Self {
  12. let data = vec![0usize; elements.div_ceil(usize::BITS as usize)];
  13. Self {
  14. elements,
  15. data,
  16. core: BitMapCore::new(),
  17. }
  18. }
  19. pub fn bitand_assign(&mut self, rhs: &Self) {
  20. for i in 0..rhs.data.len() {
  21. self.data[i] &= rhs.data[i];
  22. }
  23. }
  24. pub fn data(&self) -> &[usize] {
  25. &self.data
  26. }
  27. }
  28. impl BitMapOps<usize> for AllocBitmap {
  29. #[inline]
  30. fn get(&self, index: usize) -> Option<bool> {
  31. return self.core.get(self.elements, &self.data, index);
  32. }
  33. #[inline]
  34. fn set(&mut self, index: usize, value: bool) -> Option<bool> {
  35. return self.core.set(self.elements, &mut self.data, index, value);
  36. }
  37. #[inline]
  38. fn len(&self) -> usize {
  39. self.elements
  40. }
  41. #[inline]
  42. fn size(&self) -> usize {
  43. self.data.len() * core::mem::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.elements, &self.data)
  52. }
  53. #[inline]
  54. fn last_index(&self) -> Option<usize> {
  55. self.core.last_index(self.elements, &self.data)
  56. }
  57. #[inline]
  58. fn last_false_index(&self) -> Option<usize> {
  59. self.core.last_false_index(self.elements, &self.data)
  60. }
  61. #[inline]
  62. fn next_index(&self, index: usize) -> Option<usize> {
  63. self.core.next_index(self.elements, &self.data, index)
  64. }
  65. #[inline]
  66. fn next_false_index(&self, index: usize) -> Option<usize> {
  67. self.core.next_false_index(self.elements, &self.data, index)
  68. }
  69. #[inline]
  70. fn prev_index(&self, index: usize) -> Option<usize> {
  71. self.core.prev_index(self.elements, &self.data, index)
  72. }
  73. #[inline]
  74. fn prev_false_index(&self, index: usize) -> Option<usize> {
  75. self.core.prev_false_index(self.elements, &self.data, index)
  76. }
  77. #[inline]
  78. fn invert(&mut self) {
  79. self.core.invert(self.elements, &mut self.data);
  80. }
  81. #[inline]
  82. fn is_full(&self) -> bool {
  83. self.core.is_full(self.elements, &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(self.elements, &mut self.data, value);
  98. }
  99. }
  100. impl BitAnd for &AllocBitmap {
  101. type Output = AllocBitmap;
  102. fn bitand(self, rhs: Self) -> Self::Output {
  103. let mut result = AllocBitmap::new(self.elements);
  104. for i in 0..rhs.data.len() {
  105. result.data[i] = self.data[i] & rhs.data[i];
  106. }
  107. result
  108. }
  109. }
  110. impl BitAnd for AllocBitmap {
  111. type Output = AllocBitmap;
  112. fn bitand(self, rhs: Self) -> Self::Output {
  113. &self & &rhs
  114. }
  115. }