use smallvec::SmallVec;
use crate::ids::StopIdx;
use crate::time::Duration;
#[derive(Debug, Clone, Default)]
pub struct Endpoints {
stops: SmallVec<[(StopIdx, Duration); 50]>,
}
impl Endpoints {
pub fn new() -> Self {
Self::default()
}
pub fn len(&self) -> usize {
self.stops.len()
}
pub fn is_empty(&self) -> bool {
self.stops.is_empty()
}
pub fn as_slice(&self) -> &[(StopIdx, Duration)] {
&self.stops
}
pub fn push(&mut self, stop: StopIdx, walk: Duration) {
self.stops.push((stop, walk));
}
}
pub trait IntoEndpoints {
fn into_endpoints(self) -> Endpoints;
}
impl IntoEndpoints for StopIdx {
fn into_endpoints(self) -> Endpoints {
let mut e = Endpoints::new();
e.push(self, Duration::ZERO);
e
}
}
impl IntoEndpoints for (StopIdx, Duration) {
fn into_endpoints(self) -> Endpoints {
let mut e = Endpoints::new();
e.push(self.0, self.1);
e
}
}
impl IntoEndpoints for &[StopIdx] {
fn into_endpoints(self) -> Endpoints {
let mut e = Endpoints::new();
for &s in self {
e.push(s, Duration::ZERO);
}
e
}
}
impl<const N: usize> IntoEndpoints for &[StopIdx; N] {
fn into_endpoints(self) -> Endpoints {
(self.as_slice()).into_endpoints()
}
}
impl IntoEndpoints for &[(StopIdx, Duration)] {
fn into_endpoints(self) -> Endpoints {
let mut e = Endpoints::new();
for &p in self {
e.push(p.0, p.1);
}
e
}
}
impl<const N: usize> IntoEndpoints for &[(StopIdx, Duration); N] {
fn into_endpoints(self) -> Endpoints {
(self.as_slice()).into_endpoints()
}
}
impl IntoEndpoints for Vec<StopIdx> {
fn into_endpoints(self) -> Endpoints {
(self.as_slice()).into_endpoints()
}
}
impl IntoEndpoints for &Vec<StopIdx> {
fn into_endpoints(self) -> Endpoints {
(self.as_slice()).into_endpoints()
}
}
impl IntoEndpoints for Vec<(StopIdx, Duration)> {
fn into_endpoints(self) -> Endpoints {
let mut e = Endpoints::new();
for p in self {
e.push(p.0, p.1);
}
e
}
}
impl IntoEndpoints for &Vec<(StopIdx, Duration)> {
fn into_endpoints(self) -> Endpoints {
(self.as_slice()).into_endpoints()
}
}
impl IntoEndpoints for &Endpoints {
fn into_endpoints(self) -> Endpoints {
self.clone()
}
}
impl IntoEndpoints for Endpoints {
fn into_endpoints(self) -> Endpoints {
self
}
}