alloc_bitmap.rs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. use core::ops::BitAnd;
  2. use alloc::vec::Vec;
  3. use crate::{bitmap_core::BitMapCore, traits::BitMapOps};
  4. #[derive(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. }
  25. impl BitMapOps<usize> for AllocBitmap {
  26. #[inline]
  27. fn get(&self, index: usize) -> Option<bool> {
  28. return self.core.get(self.elements, &self.data, index);
  29. }
  30. #[inline]
  31. fn set(&mut self, index: usize, value: bool) -> Option<bool> {
  32. return self.core.set(self.elements, &mut self.data, index, value);
  33. }
  34. #[inline]
  35. fn len(&self) -> usize {
  36. self.elements
  37. }
  38. #[inline]
  39. fn size(&self) -> usize {
  40. self.data.len() * core::mem::size_of::<usize>()
  41. }
  42. #[inline]
  43. fn first_index(&self) -> Option<usize> {
  44. self.core.first_index(&self.data)
  45. }
  46. #[inline]
  47. fn first_false_index(&self) -> Option<usize> {
  48. self.core.first_false_index(self.elements, &self.data)
  49. }
  50. #[inline]
  51. fn last_index(&self) -> Option<usize> {
  52. self.core.last_index(self.elements, &self.data)
  53. }
  54. #[inline]
  55. fn last_false_index(&self) -> Option<usize> {
  56. self.core.last_false_index(self.elements, &self.data)
  57. }
  58. #[inline]
  59. fn next_index(&self, index: usize) -> Option<usize> {
  60. self.core.next_index(self.elements, &self.data, index)
  61. }
  62. #[inline]
  63. fn next_false_index(&self, index: usize) -> Option<usize> {
  64. self.core.next_false_index(self.elements, &self.data, index)
  65. }
  66. #[inline]
  67. fn prev_index(&self, index: usize) -> Option<usize> {
  68. self.core.prev_index(self.elements, &self.data, index)
  69. }
  70. #[inline]
  71. fn prev_false_index(&self, index: usize) -> Option<usize> {
  72. self.core.prev_false_index(self.elements, &self.data, index)
  73. }
  74. #[inline]
  75. fn invert(&mut self) {
  76. self.core.invert(self.elements, &mut self.data);
  77. }
  78. #[inline]
  79. fn is_full(&self) -> bool {
  80. self.core.is_full(self.elements, &self.data)
  81. }
  82. #[inline]
  83. fn is_empty(&self) -> bool {
  84. self.core.is_empty(&self.data)
  85. }
  86. #[inline]
  87. unsafe fn as_bytes(&self) -> &[u8] {
  88. core::slice::from_raw_parts(
  89. self.data.as_ptr() as *const u8,
  90. core::mem::size_of::<Self>(),
  91. )
  92. }
  93. fn set_all(&mut self, value: bool) {
  94. self.core.set_all(self.elements, &mut self.data, value);
  95. }
  96. }
  97. impl BitAnd for &AllocBitmap {
  98. type Output = AllocBitmap;
  99. fn bitand(self, rhs: Self) -> Self::Output {
  100. let mut result = AllocBitmap::new(self.elements);
  101. for i in 0..rhs.data.len() {
  102. result.data[i] = self.data[i] & rhs.data[i];
  103. }
  104. result
  105. }
  106. }
  107. impl BitAnd for AllocBitmap {
  108. type Output = AllocBitmap;
  109. fn bitand(self, rhs: Self) -> Self::Output {
  110. &self & &rhs
  111. }
  112. }