use crate::Balance;
use alloc::vec::Vec;
#[derive(Clone, Debug, PartialEq)]
pub struct Path {
raw: Vec<Balance>,
}
impl Path {
pub fn new(movements: Vec<Balance>) -> Self {
Self { raw: movements }
}
pub fn len(&self) -> usize {
self.raw.len()
}
pub fn is_empty(&self) -> bool {
self.raw.is_empty()
}
pub fn get(&self, index: usize) -> Option<&Balance> {
self.raw.get(index)
}
pub fn iter(&self) -> impl Iterator<Item = &Balance> {
self.raw.iter()
}
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut Balance> {
self.raw.iter_mut()
}
pub fn push(&mut self, movement: Balance) {
self.raw.push(movement);
}
pub fn pop(&mut self) -> Option<Balance> {
self.raw.pop()
}
pub fn clear(&mut self) {
self.raw.clear();
}
pub fn to_vector(&self) -> (i8, i8) {
let mut x = 0;
let mut y = 0;
for movement in self.raw.iter() {
let (a, b) = movement.to_vector();
x += a;
y += b;
}
(x, y)
}
pub fn from_vector(x: i8, y: i8) -> Self {
let mut movements = Vec::new();
let mut x = x;
let mut y = y;
while x != 0 || y != 0 {
let (a, b) = (x.signum(), y.signum());
x -= a;
y -= b;
movements.push(Balance::from_vector(a, b));
}
Self { raw: movements }
}
pub fn normalized(&self) -> Self {
let (x, y) = self.to_vector();
Self::from_vector(x, y)
}
pub fn reversed(&self) -> Self {
let mut movements = Vec::new();
for movement in self.raw.iter().rev() {
movements.push(*movement);
}
Self { raw: movements }
}
pub fn each(&self, f: impl Fn(Balance) -> Balance) -> Self {
let mut movements = Vec::with_capacity(self.raw.len());
for movement in self.raw.iter() {
movements.push(f(*movement));
}
Self { raw: movements }
}
pub fn each_with(&self, f: impl Fn(Balance, Balance) -> Balance, other: Balance) -> Self {
let mut movements = Vec::with_capacity(self.raw.len());
for movement in self.raw.iter() {
movements.push(f(*movement, other));
}
Self { raw: movements }
}
pub fn each_zip(&self, f: impl Fn(Balance, Balance) -> Balance, other: &Self) -> Self {
let mut movements = Vec::with_capacity(self.raw.len());
for (a, b) in self.raw.iter().zip(other.raw.iter()) {
movements.push(f(*a, *b));
}
Self { raw: movements }
}
}