summaryrefslogtreecommitdiff
path: root/src/index.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/index.rs')
-rw-r--r--src/index.rs77
1 files changed, 70 insertions, 7 deletions
diff --git a/src/index.rs b/src/index.rs
index afeaa271..6dbfbc40 100644
--- a/src/index.rs
+++ b/src/index.rs
@@ -17,7 +17,9 @@
/// Indexing types and implementations for Grid and Line
use std::cmp::{Ord, Ordering};
use std::fmt;
-use std::ops::{self, Deref, Add, Range, RangeInclusive};
+use std::ops::{self, Deref, Range, RangeInclusive, Add, Sub, AddAssign, SubAssign};
+
+use crate::grid::BidirectionalIterator;
/// The side of a cell
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
@@ -70,6 +72,67 @@ impl From<Point> for Point<usize> {
}
}
+impl<T> Point<T>
+where
+ T: Copy + Default + SubAssign<usize> + PartialEq,
+{
+ pub fn iter(&self, last_col: Column, last_line: T) -> PointIterator<T> {
+ PointIterator {
+ cur: *self,
+ last_col,
+ last_line,
+ }
+ }
+}
+
+pub struct PointIterator<T> {
+ pub cur: Point<T>,
+ last_col: Column,
+ last_line: T,
+}
+
+impl<T> Iterator for PointIterator<T>
+where
+ T: Copy + Default + SubAssign<usize> + PartialEq,
+{
+ type Item = Point<T>;
+
+ fn next(&mut self) -> Option<Self::Item> {
+ match self.cur {
+ Point { line, col } if line == Default::default() && col == self.last_col => None,
+ Point { col, .. } if col == self.last_col => {
+ self.cur.line -= 1;
+ self.cur.col = Column(0);
+ Some(self.cur)
+ },
+ _ => {
+ self.cur.col += Column(1);
+ Some(self.cur)
+ }
+ }
+ }
+}
+
+impl<T> BidirectionalIterator for PointIterator<T>
+where
+ T: Copy + Default + AddAssign<usize> + SubAssign<usize> + PartialEq,
+{
+ fn prev(&mut self) -> Option<Self::Item> {
+ match self.cur {
+ Point { line, col: Column(0) } if line == self.last_line => None,
+ Point { col: Column(0), .. } => {
+ self.cur.line += 1;
+ self.cur.col = self.last_col;
+ Some(self.cur)
+ },
+ _ => {
+ self.cur.col -= Column(1);
+ Some(self.cur)
+ }
+ }
+ }
+}
+
/// A line
///
/// Newtype to avoid passing values incorrectly
@@ -312,28 +375,28 @@ macro_rules! ops {
}
}
}
- impl ops::AddAssign<$ty> for $ty {
+ impl AddAssign<$ty> for $ty {
#[inline]
fn add_assign(&mut self, rhs: $ty) {
self.0 += rhs.0
}
}
- impl ops::SubAssign<$ty> for $ty {
+ impl SubAssign<$ty> for $ty {
#[inline]
fn sub_assign(&mut self, rhs: $ty) {
self.0 -= rhs.0
}
}
- impl ops::AddAssign<usize> for $ty {
+ impl AddAssign<usize> for $ty {
#[inline]
fn add_assign(&mut self, rhs: usize) {
self.0 += rhs
}
}
- impl ops::SubAssign<usize> for $ty {
+ impl SubAssign<usize> for $ty {
#[inline]
fn sub_assign(&mut self, rhs: usize) {
self.0 -= rhs
@@ -347,7 +410,7 @@ macro_rules! ops {
}
}
- impl ops::Add<usize> for $ty {
+ impl Add<usize> for $ty {
type Output = $ty;
#[inline]
@@ -356,7 +419,7 @@ macro_rules! ops {
}
}
- impl ops::Sub<usize> for $ty {
+ impl Sub<usize> for $ty {
type Output = $ty;
#[inline]