use core::iter::{DoubleEndedIterator, Iterator};
use bit_vec::{BitVec, Iter};
#[derive(Debug, Clone, PartialEq, Eq)]
#[repr(transparent)]
pub struct Path(pub(crate) BitVec);
impl Path {
#[inline]
pub fn height(&self) -> usize {
self.0.len()
}
#[inline]
pub fn leaves(&self) -> usize {
2usize.pow(self.height() as u32)
}
#[inline]
pub fn directions(&self) -> Directions<'_> {
Directions(self.0.iter())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Direction {
Left,
Right,
}
impl From<bool> for Direction {
#[inline]
fn from(b: bool) -> Direction {
if b {
Direction::Left
} else {
Direction::Right
}
}
}
impl From<Direction> for bool {
fn from(direction: Direction) -> bool {
match direction {
Direction::Left => true,
Direction::Right => false,
}
}
}
pub struct Directions<'a>(Iter<'a>);
impl<'a> Iterator for Directions<'a> {
type Item = Direction;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(Into::into)
}
}
impl<'a> DoubleEndedIterator for Directions<'a> {
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
self.0.next_back().map(Into::into)
}
}