use std::iter::FromIterator;
use {CoordinateType, Line, Point};
#[derive(PartialEq, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct LineString<T>(pub Vec<Point<T>>)
where
T: CoordinateType;
impl<T: CoordinateType> LineString<T> {
pub fn lines<'a>(&'a self) -> impl Iterator<Item = Line<T>> + 'a {
self.0.windows(2).map(|w| unsafe {
Line::new(*w.get_unchecked(0), *w.get_unchecked(1))
})
}
pub fn points(&self) -> ::std::slice::Iter<Point<T>> {
self.0.iter()
}
pub fn points_mut(&mut self) -> ::std::slice::IterMut<Point<T>> {
self.0.iter_mut()
}
}
impl<T: CoordinateType, IP: Into<Point<T>>> From<Vec<IP>> for LineString<T> {
fn from(v: Vec<IP>) -> Self {
LineString(v.into_iter().map(|p| p.into()).collect())
}
}
impl<T: CoordinateType, IP: Into<Point<T>>> FromIterator<IP> for LineString<T> {
fn from_iter<I: IntoIterator<Item = IP>>(iter: I) -> Self {
LineString(iter.into_iter().map(|p| p.into()).collect())
}
}
impl<T: CoordinateType> IntoIterator for LineString<T> {
type Item = Point<T>;
type IntoIter = ::std::vec::IntoIter<Point<T>>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}