Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. impl <'a> IntoIterator for &'a Bitmap {
  2. type Item = u16;
  3. type IntoIter = SharedBitmapIterator<'a>;
  4. fn into_iter(self) -> Self::IntoIter {
  5. return SharedBitmapIterator { offset: 0, bitmap: &self };
  6. }
  7. }
  8.  
  9. impl IntoIterator for Bitmap {
  10. type Item = u16;
  11. type IntoIter = OwnedBitmapIterator;
  12. fn into_iter(self) -> Self::IntoIter {
  13. return OwnedBitmapIterator { offset: 0, bitmap: self };
  14. }
  15. }
  16.  
  17.  
  18. pub struct SharedBitmapIterator<'a> {
  19. offset: usize,
  20. bitmap: &'a Bitmap,
  21. }
  22.  
  23.  
  24. pub struct OwnedBitmapIterator {
  25. offset: usize,
  26. bitmap: Bitmap,
  27. }
  28.  
  29.  
  30. impl <'a> Iterator for SharedBitmapIterator<'a> {
  31. type Item = u16;
  32.  
  33. fn next(&mut self) -> Option<Self::Item> {
  34. loop {
  35. let bucket = self.offset / BITS_PER_BUCKET;
  36. let bit_pos = self.offset % BITS_PER_BUCKET;
  37.  
  38. if bucket >= self.bitmap.num_buckets() {
  39. return None;
  40. }
  41.  
  42. // Optimization: skip whole bucket when its value is zero.
  43. if self.bitmap.buckets[bucket] == 0 {
  44. self.offset += 64;
  45. continue;
  46. }
  47.  
  48. if self.bitmap.buckets[bucket] & (1 << bit_pos) == 0 {
  49. self.offset += 1;
  50. continue;
  51. }
  52.  
  53. let x = self.offset;
  54. self.offset += 1;
  55. return Some(x as u16);
  56. }
  57. }
  58. }
  59.  
  60.  
  61. impl Iterator for OwnedBitmapIterator {
  62. type Item = u16;
  63.  
  64. fn next(&mut self) -> Option<Self::Item> {
  65. loop {
  66. let bucket = self.offset / BITS_PER_BUCKET;
  67. let bit_pos = self.offset % BITS_PER_BUCKET;
  68.  
  69. if bucket >= self.bitmap.num_buckets() {
  70. return None;
  71. }
  72.  
  73. // Optimization: skip whole bucket when its value is zero.
  74. if self.bitmap.buckets[bucket] == 0 {
  75. self.offset += 64;
  76. continue;
  77. }
  78.  
  79. if self.bitmap.buckets[bucket] & (1 << bit_pos) == 0 {
  80. self.offset += 1;
  81. continue;
  82. }
  83.  
  84. let x = self.offset;
  85. self.offset += 1;
  86. return Some(x as u16);
  87. }
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement