You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
512 B

  1. #[derive(Debug)]
  2. pub struct BitIterator<E> {
  3. t: E,
  4. n: usize,
  5. }
  6. impl<E: AsRef<[u64]>> BitIterator<E> {
  7. pub fn new(t: E) -> Self {
  8. let n = t.as_ref().len() * 64;
  9. BitIterator { t, n }
  10. }
  11. }
  12. impl<E: AsRef<[u64]>> Iterator for BitIterator<E> {
  13. type Item = bool;
  14. fn next(&mut self) -> Option<bool> {
  15. if self.n == 0 {
  16. None
  17. } else {
  18. self.n -= 1;
  19. let part = self.n / 64;
  20. let bit = self.n - (64 * part);
  21. Some(self.t.as_ref()[part] & (1 << bit) > 0)
  22. }
  23. }
  24. }