use super::ChainFeatures;
use crate::LPR;
use alloc::vec::Vec;
#[derive(Clone, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Chain {
pub(crate) cost: usize,
pub(crate) features: ChainFeatures,
pub(crate) path: Vec<LPR>,
}
impl Chain {
pub fn new() -> Self {
Chain {
cost: 0,
features: ChainFeatures::new(),
path: Vec::new(),
}
}
pub fn from_features(features: ChainFeatures) -> Self {
Chain {
cost: 0,
features,
path: Vec::new(),
}
}
pub fn from_path<I>(path: I) -> Self
where
I: IntoIterator<Item = LPR>,
{
let path = Vec::from_iter(path);
Chain {
cost: 0,
features: ChainFeatures::new(),
path,
}
}
pub const fn cost(&self) -> usize {
self.cost
}
pub const fn features(&self) -> &ChainFeatures {
&self.features
}
pub const fn path(&self) -> &Vec<LPR> {
&self.path
}
pub fn set_cost(&mut self, cost: usize) {
self.cost = cost;
}
pub fn set_features(&mut self, features: ChainFeatures) {
self.features = features
}
pub fn with_features(self, features: ChainFeatures) -> Self {
Self { features, ..self }
}
pub fn with_cost(self, cost: usize) -> Self {
Self { cost, ..self }
}
pub fn with_path<I>(self, path: I) -> Self
where
I: IntoIterator<Item = LPR>,
{
let path = Vec::from_iter(path);
Self { path, ..self }
}
pub fn iter(&self) -> impl Iterator<Item = &LPR> {
self.path.iter()
}
}
impl IntoIterator for Chain {
type Item = LPR;
type IntoIter = alloc::vec::IntoIter<LPR>;
fn into_iter(self) -> Self::IntoIter {
self.path.into_iter()
}
}
impl<'a> IntoIterator for &'a Chain {
type Item = &'a LPR;
type IntoIter = core::slice::Iter<'a, LPR>;
fn into_iter(self) -> Self::IntoIter {
self.path.iter()
}
}