alloc_bitmap.rs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. use alloc::vec::Vec;
  2. use crate::{bitmap_core::BitMapCore, traits::BitMapOps};
  3. #[derive(Clone)]
  4. pub struct AllocBitmap {
  5. elements: usize,
  6. data: Vec<usize>,
  7. core: BitMapCore<usize>,
  8. }
  9. impl AllocBitmap {
  10. pub fn new(elements: usize) -> Self {
  11. let data = vec![0usize; (elements + usize::BITS as usize - 1) / (usize::BITS as usize)];
  12. Self {
  13. elements,
  14. data,
  15. core: BitMapCore::new(),
  16. }
  17. }
  18. }
  19. impl BitMapOps<usize> for AllocBitmap {
  20. #[inline]
  21. fn get(&self, index: usize) -> Option<bool> {
  22. return self.core.get(self.elements, &self.data, index);
  23. }
  24. #[inline]
  25. fn set(&mut self, index: usize, value: bool) -> Option<bool> {
  26. return self.core.set(self.elements, &mut self.data, index, value);
  27. }
  28. #[inline]
  29. fn len(&self) -> usize {
  30. self.elements
  31. }
  32. #[inline]
  33. fn size(&self) -> usize {
  34. self.data.len() * core::mem::size_of::<usize>()
  35. }
  36. #[inline]
  37. fn first_index(&self) -> Option<usize> {
  38. self.core.first_index(&self.data)
  39. }
  40. #[inline]
  41. fn first_false_index(&self) -> Option<usize> {
  42. self.core.first_false_index(self.elements, &self.data)
  43. }
  44. #[inline]
  45. fn last_index(&self) -> Option<usize> {
  46. self.core.last_index(self.elements, &self.data)
  47. }
  48. #[inline]
  49. fn last_false_index(&self) -> Option<usize> {
  50. self.core.last_false_index(self.elements, &self.data)
  51. }
  52. #[inline]
  53. fn next_index(&self, index: usize) -> Option<usize> {
  54. self.core.next_index(self.elements, &self.data, index)
  55. }
  56. #[inline]
  57. fn next_false_index(&self, index: usize) -> Option<usize> {
  58. self.core.next_false_index(self.elements, &self.data, index)
  59. }
  60. #[inline]
  61. fn prev_index(&self, index: usize) -> Option<usize> {
  62. self.core.prev_index(self.elements, &self.data, index)
  63. }
  64. #[inline]
  65. fn prev_false_index(&self, index: usize) -> Option<usize> {
  66. self.core.prev_false_index(self.elements, &self.data, index)
  67. }
  68. #[inline]
  69. fn invert(&mut self) {
  70. self.core.invert(self.elements, &mut self.data);
  71. }
  72. #[inline]
  73. fn is_full(&self) -> bool {
  74. self.core.is_full(self.elements, &self.data)
  75. }
  76. #[inline]
  77. fn is_empty(&self) -> bool {
  78. self.core.is_empty(&self.data)
  79. }
  80. #[inline]
  81. unsafe fn as_bytes(&self) -> &[u8] {
  82. core::slice::from_raw_parts(
  83. self.data.as_ptr() as *const u8,
  84. core::mem::size_of::<Self>(),
  85. )
  86. }
  87. fn set_all(&mut self, value: bool) {
  88. self.core.set_all(self.elements, &mut self.data, value);
  89. }
  90. }