bits.rs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // Use bitfields instead of HashSets.
  2. /*
  3. Or MAYBE, just use Vec<bool> ?!
  4. While that would use more memory, it would certainly be faster!
  5. Is saving a few bytes (these days) worth it, really?
  6. The iterator would be interesting...
  7. And we could handle any crazy size as well... :O
  8. */
  9. use bit_field::BitField;
  10. // If I wanted to do 4x4 or 5x5, I would need more bits. (u32).
  11. use num::Integer;
  12. use std::ops::RangeBounds;
  13. use std::fmt;
  14. #[derive(Copy, Clone, PartialEq)]
  15. pub struct GenBits<T: Integer + BitField>(pub T);
  16. impl<T: Integer + BitField> GenBits<T> {
  17. pub fn clear(&mut self) {
  18. self.0 = T::zero();
  19. }
  20. pub fn set(&mut self, bit: usize, value: bool) {
  21. self.0.set_bit(bit, value);
  22. }
  23. #[must_use]
  24. pub fn get(&self, bit: usize) -> bool {
  25. self.0.get_bit(bit)
  26. }
  27. pub fn set_bits<R: RangeBounds<u8> + IntoIterator<Item = u8>>(&mut self, bits: R) {
  28. for i in bits {
  29. self.set(i as usize, true);
  30. }
  31. }
  32. #[must_use]
  33. pub fn count_set(&self) -> u8 {
  34. let mut count: u8 = 0;
  35. for i in 0..T::BIT_LENGTH {
  36. if self.get(i) {
  37. count += 1;
  38. }
  39. }
  40. count
  41. }
  42. /// Display bits that are set.
  43. /// +1 is added to the display, so it matches the cell values (1..N)
  44. pub fn display(&self) -> String {
  45. self.iter()
  46. .map(|i| (i+1).to_string())
  47. // .map(|i| i.to_string())
  48. .collect::<Vec<_>>().join(",")
  49. }
  50. }
  51. impl<T: Integer + BitField + ToString> fmt::Debug for GenBits<T> {
  52. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  53. let result = self.iter()
  54. // .map(|i| (i+1).to_string())
  55. .map(|i| i.to_string())
  56. .collect::<Vec<_>>().join(",");
  57. if result.is_empty() {
  58. return write!(f, "{{}}")
  59. } else {
  60. return write!(f, "<{}>", result)
  61. }
  62. }
  63. }
  64. pub struct GenBitsIterator<'a, T: Integer + BitField> {
  65. pub possible: &'a GenBits<T>,
  66. pub index: usize,
  67. }
  68. impl<T: Integer + BitField> GenBits<T> {
  69. pub fn iter(&self) -> GenBitsIterator<T> {
  70. GenBitsIterator {
  71. possible: self,
  72. index: 0,
  73. }
  74. }
  75. }
  76. impl<'a, T: Integer + BitField> Iterator for GenBitsIterator<'a, T> {
  77. type Item = u8;
  78. fn next(&mut self) -> Option<u8> {
  79. while (self.index < T::BIT_LENGTH) && (!self.possible.get(self.index as usize)) {
  80. self.index += 1;
  81. }
  82. if self.index == T::BIT_LENGTH {
  83. None
  84. } else {
  85. self.index += 1;
  86. Some((self.index - 1) as u8)
  87. }
  88. }
  89. }
  90. #[cfg(test)]
  91. mod tests {
  92. // use crate::sudoku::*;
  93. use crate::bits::*;
  94. #[test]
  95. fn check_u16() {
  96. let mut p = GenBits::<u16>(0);
  97. p.clear();
  98. for i in 0..9 {
  99. let mut result = p.get(i);
  100. assert_eq!(result, false);
  101. p.set(i, true);
  102. result = p.get(i);
  103. assert_eq!(result, true);
  104. }
  105. p = GenBits::<u16>(0);
  106. p.set(3, true);
  107. p.set(5, true);
  108. p.set(6, true);
  109. assert_eq!(3, p.count_set());
  110. let values: Vec<u8> = p.iter().collect();
  111. assert_eq!(values, vec!(3, 5, 6));
  112. assert_eq!(3, p.count_set());
  113. p.set(0, true);
  114. assert_eq!(4, p.count_set());
  115. p = GenBits::<u16>(0);
  116. p.set_bits(0..6);
  117. for i in 0..6 {
  118. let result = p.get(i);
  119. assert_eq!(result, true);
  120. }
  121. assert_eq!(p.get(6), false);
  122. }
  123. #[test]
  124. fn check_u32() {
  125. let mut p = GenBits::<u32>(0);
  126. p.clear();
  127. for i in 0..29 {
  128. let mut result = p.get(i);
  129. assert_eq!(result, false);
  130. p.set(i, true);
  131. result = p.get(i);
  132. assert_eq!(result, true);
  133. }
  134. p = GenBits::<u32>(0);
  135. p.set(13, true);
  136. p.set(15, true);
  137. p.set(26, true);
  138. assert_eq!(3, p.count_set());
  139. let values: Vec<u8> = p.iter().collect();
  140. assert_eq!(values, vec!(13, 15, 26));
  141. assert_eq!(3, p.count_set());
  142. p.set(0, true);
  143. assert_eq!(4, p.count_set());
  144. p = GenBits::<u32>(0);
  145. p.set_bits(10..26);
  146. for i in 10..26 {
  147. let result = p.get(i);
  148. assert_eq!(result, true);
  149. }
  150. assert_eq!(p.get(9), false);
  151. assert_eq!(p.get(27), false);
  152. }
  153. }