2016-06-29 23:56:12 -04:00
|
|
|
// Copyright 2016 Joe Wilm, The Alacritty Project Contributors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2016-04-10 19:19:39 -04:00
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
//! A generic 2d grid implementation optimized for use in a terminal.
|
|
|
|
//!
|
|
|
|
//! The current implementation uses a vector of vectors to store cell data.
|
|
|
|
//! Reimplementing the store as a single contiguous vector may be desirable in
|
|
|
|
//! the future. Rotation and indexing would need to be reconsidered at that
|
|
|
|
//! time; rotation currently reorganize Vecs in the lines Vec, and indexing with
|
|
|
|
//! ranges is currently supported.
|
|
|
|
|
2016-06-29 13:21:02 -04:00
|
|
|
use std::cmp::Ordering;
|
2017-10-12 22:07:49 -04:00
|
|
|
use std::collections::{VecDeque, vec_deque};
|
2016-07-02 11:25:21 -04:00
|
|
|
use std::iter::IntoIterator;
|
2017-10-12 23:01:28 -04:00
|
|
|
use std::ops::{Deref, Range, RangeTo, RangeFrom, Index, IndexMut};
|
2016-06-08 13:39:49 -04:00
|
|
|
|
2017-01-14 20:53:48 -05:00
|
|
|
use index::{self, Point, Line, Column, IndexRange, RangeInclusive};
|
2016-07-03 20:00:00 -04:00
|
|
|
|
2017-10-12 23:01:28 -04:00
|
|
|
mod row;
|
|
|
|
pub use self::row::Row;
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
|
|
|
|
2016-12-22 13:43:06 -05:00
|
|
|
/// Convert a type to a linear index range.
|
|
|
|
pub trait ToRange {
|
2017-06-16 00:43:28 -04:00
|
|
|
fn to_range(&self) -> RangeInclusive<index::Linear>;
|
2016-12-22 13:43:06 -05:00
|
|
|
}
|
|
|
|
|
2017-01-14 20:53:48 -05:00
|
|
|
/// Bidirection iterator
|
|
|
|
pub trait BidirectionalIterator: Iterator {
|
|
|
|
fn prev(&mut self) -> Option<Self::Item>;
|
|
|
|
}
|
|
|
|
|
2017-10-12 23:01:28 -04:00
|
|
|
/// An item in the grid along with its Line and Column.
|
2017-02-11 15:49:40 -05:00
|
|
|
pub struct Indexed<T> {
|
2017-10-12 23:01:28 -04:00
|
|
|
pub inner: T,
|
2017-02-11 15:49:40 -05:00
|
|
|
pub line: Line,
|
|
|
|
pub column: Column,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Deref for Indexed<T> {
|
|
|
|
type Target = T;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn deref(&self) -> &T {
|
|
|
|
&self.inner
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
/// Represents the terminal display contents
|
2016-11-19 19:16:20 -05:00
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
|
2016-07-03 20:00:00 -04:00
|
|
|
pub struct Grid<T> {
|
|
|
|
/// Lines in the grid. Each row holds a list of cells corresponding to the
|
|
|
|
/// columns in that row.
|
2017-10-12 22:07:49 -04:00
|
|
|
raw: VecDeque<Row<T>>,
|
2016-07-03 20:00:00 -04:00
|
|
|
|
|
|
|
/// Number of columns
|
|
|
|
cols: index::Column,
|
|
|
|
|
|
|
|
/// Number of lines.
|
|
|
|
///
|
|
|
|
/// Invariant: lines is equivalent to raw.len()
|
|
|
|
lines: index::Line,
|
|
|
|
}
|
|
|
|
|
2017-01-14 20:53:48 -05:00
|
|
|
pub struct GridIterator<'a, T: 'a> {
|
|
|
|
grid: &'a Grid<T>,
|
|
|
|
pub cur: Point,
|
|
|
|
}
|
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
impl<T: Clone> Grid<T> {
|
|
|
|
pub fn new(lines: index::Line, cols: index::Column, template: &T) -> Grid<T> {
|
2017-10-12 22:07:49 -04:00
|
|
|
let mut raw = VecDeque::with_capacity(*lines);
|
2017-01-06 18:25:04 -05:00
|
|
|
for _ in IndexRange(index::Line(0)..lines) {
|
2017-10-12 22:07:49 -04:00
|
|
|
raw.push_back(Row::new(cols, template));
|
2016-07-03 20:00:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Grid {
|
2018-03-04 17:40:15 -05:00
|
|
|
raw,
|
|
|
|
cols,
|
|
|
|
lines,
|
2016-07-03 20:00:00 -04:00
|
|
|
}
|
2016-05-30 23:44:37 -04:00
|
|
|
}
|
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
pub fn resize(&mut self, lines: index::Line, cols: index::Column, template: &T) {
|
2016-06-29 13:21:02 -04:00
|
|
|
// Check that there's actually work to do and return early if not
|
2016-07-03 20:00:00 -04:00
|
|
|
if lines == self.lines && cols == self.cols {
|
2016-06-29 13:21:02 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
match self.lines.cmp(&lines) {
|
|
|
|
Ordering::Less => self.grow_lines(lines, template),
|
|
|
|
Ordering::Greater => self.shrink_lines(lines),
|
2016-06-29 13:21:02 -04:00
|
|
|
Ordering::Equal => (),
|
|
|
|
}
|
|
|
|
|
|
|
|
match self.cols.cmp(&cols) {
|
2016-07-03 20:00:00 -04:00
|
|
|
Ordering::Less => self.grow_cols(cols, template),
|
2016-06-29 13:21:02 -04:00
|
|
|
Ordering::Greater => self.shrink_cols(cols),
|
|
|
|
Ordering::Equal => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
fn grow_lines(&mut self, lines: index::Line, template: &T) {
|
2017-01-06 18:25:04 -05:00
|
|
|
for _ in IndexRange(self.num_lines()..lines) {
|
2017-10-12 22:07:49 -04:00
|
|
|
self.raw.push_back(Row::new(self.cols, template));
|
2016-06-29 13:21:02 -04:00
|
|
|
}
|
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
self.lines = lines;
|
2016-06-29 13:21:02 -04:00
|
|
|
}
|
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
fn grow_cols(&mut self, cols: index::Column, template: &T) {
|
|
|
|
for row in self.lines_mut() {
|
|
|
|
row.grow(cols, template);
|
2016-05-30 23:44:37 -04:00
|
|
|
}
|
2016-06-29 13:21:02 -04:00
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
self.cols = cols;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-10-12 22:07:49 -04:00
|
|
|
|
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
impl<T> Grid<T> {
|
|
|
|
#[inline]
|
2017-10-12 22:07:49 -04:00
|
|
|
pub fn lines(&self) -> vec_deque::Iter<Row<T>> {
|
2016-07-03 20:00:00 -04:00
|
|
|
self.raw.iter()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2017-10-12 22:07:49 -04:00
|
|
|
pub fn lines_mut(&mut self) -> vec_deque::IterMut<Row<T>> {
|
2016-07-03 20:00:00 -04:00
|
|
|
self.raw.iter_mut()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn num_lines(&self) -> index::Line {
|
2016-07-29 21:04:26 -04:00
|
|
|
self.lines
|
2016-07-03 20:00:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn num_cols(&self) -> index::Column {
|
2016-07-29 21:04:26 -04:00
|
|
|
self.cols
|
2016-07-03 20:00:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2018-01-05 20:42:55 -05:00
|
|
|
pub fn scroll_down(&mut self, region: &Range<index::Line>, positions: index::Line) {
|
2017-10-12 22:07:49 -04:00
|
|
|
if region.start == Line(0) && region.end == self.num_lines() {
|
|
|
|
// Full rotation
|
|
|
|
for _ in 0..positions.0 {
|
|
|
|
let item = self.raw.pop_back().unwrap();
|
|
|
|
self.raw.push_front(item);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Subregion rotation
|
|
|
|
for line in IndexRange((region.start + positions)..region.end).rev() {
|
|
|
|
self.swap_lines(line, line - positions);
|
|
|
|
}
|
2016-08-22 11:37:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2018-01-05 20:42:55 -05:00
|
|
|
pub fn scroll_up(&mut self, region: &Range<index::Line>, positions: index::Line) {
|
2017-10-12 22:07:49 -04:00
|
|
|
if region.start == Line(0) && region.end == self.num_lines() {
|
|
|
|
// Full rotation
|
|
|
|
for _ in 0..positions.0 {
|
|
|
|
let item = self.raw.pop_front().unwrap();
|
|
|
|
self.raw.push_back(item);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Subregion rotation
|
|
|
|
for line in IndexRange(region.start..(region.end - positions)) {
|
|
|
|
self.swap_lines(line, line + positions);
|
|
|
|
}
|
2016-08-22 11:37:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-14 20:53:48 -05:00
|
|
|
pub fn iter_from(&self, point: Point) -> GridIterator<T> {
|
|
|
|
GridIterator {
|
|
|
|
grid: self,
|
|
|
|
cur: point,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-18 21:16:58 -04:00
|
|
|
#[inline]
|
2016-12-29 11:09:29 -05:00
|
|
|
pub fn contains(&self, point: &Point) -> bool {
|
|
|
|
self.lines > point.line && self.cols > point.col
|
2016-09-16 20:12:53 -04:00
|
|
|
}
|
|
|
|
|
2016-08-22 11:37:50 -04:00
|
|
|
/// Swap two lines in the grid
|
|
|
|
///
|
|
|
|
/// This could have used slice::swap internally, but we are able to have
|
|
|
|
/// better error messages by doing the bounds checking ourselves.
|
|
|
|
#[inline]
|
|
|
|
pub fn swap_lines(&mut self, src: index::Line, dst: index::Line) {
|
2017-10-12 22:07:49 -04:00
|
|
|
self.raw.swap(*src, *dst);
|
2016-07-03 20:00:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn clear<F: Fn(&mut T)>(&mut self, func: F) {
|
|
|
|
let region = index::Line(0)..self.num_lines();
|
|
|
|
self.clear_region(region, func);
|
2016-06-29 13:21:02 -04:00
|
|
|
}
|
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
fn shrink_lines(&mut self, lines: index::Line) {
|
|
|
|
while index::Line(self.raw.len()) != lines {
|
2017-10-12 22:07:49 -04:00
|
|
|
self.raw.pop_back();
|
2016-06-29 13:21:02 -04:00
|
|
|
}
|
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
self.lines = lines;
|
2016-06-29 13:21:02 -04:00
|
|
|
}
|
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
fn shrink_cols(&mut self, cols: index::Column) {
|
|
|
|
for row in self.lines_mut() {
|
2016-06-29 13:21:02 -04:00
|
|
|
row.shrink(cols);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.cols = cols;
|
2016-05-30 23:44:37 -04:00
|
|
|
}
|
2016-04-10 19:19:39 -04:00
|
|
|
}
|
|
|
|
|
2017-01-14 20:53:48 -05:00
|
|
|
impl<'a, T> Iterator for GridIterator<'a, T> {
|
|
|
|
type Item = &'a T;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
let last_line = self.grid.num_lines() - Line(1);
|
|
|
|
let last_col = self.grid.num_cols() - Column(1);
|
|
|
|
match self.cur {
|
|
|
|
Point { line, col } if
|
|
|
|
(line == last_line) &&
|
|
|
|
(col == last_col) => None,
|
|
|
|
Point { col, .. } if
|
|
|
|
(col == last_col) => {
|
|
|
|
self.cur.line += Line(1);
|
|
|
|
self.cur.col = Column(0);
|
|
|
|
Some(&self.grid[self.cur.line][self.cur.col])
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
self.cur.col += Column(1);
|
|
|
|
Some(&self.grid[self.cur.line][self.cur.col])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T> BidirectionalIterator for GridIterator<'a, T> {
|
|
|
|
fn prev(&mut self) -> Option<Self::Item> {
|
|
|
|
let num_cols = self.grid.num_cols();
|
|
|
|
|
|
|
|
match self.cur {
|
|
|
|
Point { line: Line(0), col: Column(0) } => None,
|
|
|
|
Point { col: Column(0), .. } => {
|
|
|
|
self.cur.line -= Line(1);
|
|
|
|
self.cur.col = num_cols - Column(1);
|
|
|
|
Some(&self.grid[self.cur.line][self.cur.col])
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
self.cur.col -= Column(1);
|
|
|
|
Some(&self.grid[self.cur.line][self.cur.col])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
impl<T> Index<index::Line> for Grid<T> {
|
|
|
|
type Output = Row<T>;
|
2016-04-10 19:19:39 -04:00
|
|
|
|
2016-05-30 23:44:37 -04:00
|
|
|
#[inline]
|
2016-12-17 01:13:51 -05:00
|
|
|
fn index(&self, index: index::Line) -> &Row<T> {
|
2016-07-03 20:00:00 -04:00
|
|
|
&self.raw[index.0]
|
2016-04-10 19:19:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
impl<T> IndexMut<index::Line> for Grid<T> {
|
2016-05-30 23:44:37 -04:00
|
|
|
#[inline]
|
2016-12-17 01:13:51 -05:00
|
|
|
fn index_mut(&mut self, index: index::Line) -> &mut Row<T> {
|
2016-07-03 20:00:00 -04:00
|
|
|
&mut self.raw[index.0]
|
2016-04-10 19:19:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-29 11:09:29 -05:00
|
|
|
impl<'point, T> Index<&'point Point> for Grid<T> {
|
2016-07-03 20:00:00 -04:00
|
|
|
type Output = T;
|
2016-05-30 23:44:37 -04:00
|
|
|
|
|
|
|
#[inline]
|
2016-12-29 11:09:29 -05:00
|
|
|
fn index<'a>(&'a self, point: &Point) -> &'a T {
|
|
|
|
&self.raw[point.line.0][point.col]
|
2016-05-30 23:44:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-29 11:09:29 -05:00
|
|
|
impl<'point, T> IndexMut<&'point Point> for Grid<T> {
|
2016-05-30 23:44:37 -04:00
|
|
|
#[inline]
|
2016-12-29 11:09:29 -05:00
|
|
|
fn index_mut<'a, 'b>(&'a mut self, point: &'b Point) -> &'a mut T {
|
|
|
|
&mut self.raw[point.line.0][point.col]
|
2016-05-30 23:44:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-03 23:21:55 -04:00
|
|
|
impl<'a, T> IntoIterator for &'a Grid<T> {
|
|
|
|
type Item = &'a Row<T>;
|
2017-10-12 22:07:49 -04:00
|
|
|
type IntoIter = vec_deque::Iter<'a, Row<T>>;
|
2017-04-03 23:21:55 -04:00
|
|
|
|
|
|
|
#[inline]
|
2017-10-12 22:07:49 -04:00
|
|
|
fn into_iter(self) -> vec_deque::Iter<'a, Row<T>> {
|
2017-04-03 23:21:55 -04:00
|
|
|
self.raw.iter()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
pub trait ClearRegion<R, T> {
|
|
|
|
fn clear_region<F: Fn(&mut T)>(&mut self, region: R, func: F);
|
2016-06-29 13:21:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! clear_region_impl {
|
|
|
|
($range:ty) => {
|
2016-07-03 20:00:00 -04:00
|
|
|
impl<T> ClearRegion<$range, T> for Grid<T> {
|
|
|
|
fn clear_region<F: Fn(&mut T)>(&mut self, region: $range, func: F) {
|
2017-10-12 22:07:49 -04:00
|
|
|
for row in self.region_mut(region) {
|
2016-07-02 11:25:21 -04:00
|
|
|
for cell in row {
|
2016-07-03 20:00:00 -04:00
|
|
|
func(cell);
|
2016-06-29 13:21:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-03 20:00:00 -04:00
|
|
|
clear_region_impl!(Range<index::Line>);
|
|
|
|
clear_region_impl!(RangeTo<index::Line>);
|
|
|
|
clear_region_impl!(RangeFrom<index::Line>);
|
2016-08-22 11:37:50 -04:00
|
|
|
|
2017-10-12 23:01:28 -04:00
|
|
|
// =================================================================================================
|
|
|
|
// Regions =========================================================================================
|
|
|
|
// =================================================================================================
|
2016-08-22 11:37:50 -04:00
|
|
|
|
2017-10-12 23:01:28 -04:00
|
|
|
/// A subset of lines in the grid
|
|
|
|
///
|
|
|
|
/// May be constructed using Grid::region(..)
|
|
|
|
pub struct Region<'a, T: 'a> {
|
|
|
|
start: Line,
|
|
|
|
end: Line,
|
|
|
|
raw: &'a VecDeque<Row<T>>,
|
|
|
|
}
|
2016-08-22 11:37:50 -04:00
|
|
|
|
2017-10-12 23:01:28 -04:00
|
|
|
/// A mutable subset of lines in the grid
|
|
|
|
///
|
|
|
|
/// May be constructed using Grid::region_mut(..)
|
|
|
|
pub struct RegionMut<'a, T: 'a> {
|
|
|
|
start: Line,
|
|
|
|
end: Line,
|
|
|
|
raw: &'a mut VecDeque<Row<T>>,
|
|
|
|
}
|
2016-08-22 11:37:50 -04:00
|
|
|
|
2017-10-12 23:01:28 -04:00
|
|
|
pub trait IndexRegion<I, T> {
|
|
|
|
/// Get an immutable region of Self
|
|
|
|
fn region<'a>(&'a self, _: I) -> Region<'a, T>;
|
2016-08-22 11:37:50 -04:00
|
|
|
|
2017-10-12 23:01:28 -04:00
|
|
|
/// Get a mutable region of Self
|
|
|
|
fn region_mut<'a>(&'a mut self, _: I) -> RegionMut<'a, T>;
|
|
|
|
}
|
2016-08-22 11:37:50 -04:00
|
|
|
|
2017-10-12 23:01:28 -04:00
|
|
|
impl<T> IndexRegion<Range<Line>, T> for Grid<T> {
|
|
|
|
fn region(&self, index: Range<Line>) -> Region<T> {
|
|
|
|
assert!(index.start < self.num_lines());
|
|
|
|
assert!(index.end <= self.num_lines());
|
|
|
|
assert!(index.start <= index.end);
|
|
|
|
Region {
|
|
|
|
start: index.start,
|
|
|
|
end: index.end,
|
|
|
|
raw: &self.raw
|
|
|
|
}
|
2016-08-22 11:37:50 -04:00
|
|
|
}
|
2017-10-12 23:01:28 -04:00
|
|
|
fn region_mut(&mut self, index: Range<Line>) -> RegionMut<T> {
|
|
|
|
assert!(index.start < self.num_lines());
|
|
|
|
assert!(index.end <= self.num_lines());
|
|
|
|
assert!(index.start <= index.end);
|
|
|
|
RegionMut {
|
|
|
|
start: index.start,
|
|
|
|
end: index.end,
|
|
|
|
raw: &mut self.raw
|
|
|
|
}
|
2016-08-22 11:37:50 -04:00
|
|
|
}
|
2017-10-12 23:01:28 -04:00
|
|
|
}
|
2016-08-22 11:37:50 -04:00
|
|
|
|
2017-10-12 23:01:28 -04:00
|
|
|
impl<T> IndexRegion<RangeTo<Line>, T> for Grid<T> {
|
|
|
|
fn region(&self, index: RangeTo<Line>) -> Region<T> {
|
|
|
|
assert!(index.end <= self.num_lines());
|
|
|
|
Region {
|
|
|
|
start: Line(0),
|
|
|
|
end: index.end,
|
|
|
|
raw: &self.raw
|
|
|
|
}
|
2016-08-22 11:37:50 -04:00
|
|
|
}
|
2017-10-12 23:01:28 -04:00
|
|
|
fn region_mut(&mut self, index: RangeTo<Line>) -> RegionMut<T> {
|
|
|
|
assert!(index.end <= self.num_lines());
|
|
|
|
RegionMut {
|
|
|
|
start: Line(0),
|
|
|
|
end: index.end,
|
|
|
|
raw: &mut self.raw
|
|
|
|
}
|
2016-08-22 11:37:50 -04:00
|
|
|
}
|
2017-10-12 23:01:28 -04:00
|
|
|
}
|
2016-08-22 11:37:50 -04:00
|
|
|
|
2017-10-12 23:01:28 -04:00
|
|
|
impl<T> IndexRegion<RangeFrom<Line>, T> for Grid<T> {
|
|
|
|
fn region(&self, index: RangeFrom<Line>) -> Region<T> {
|
|
|
|
assert!(index.start < self.num_lines());
|
|
|
|
Region {
|
|
|
|
start: index.start,
|
|
|
|
end: self.num_lines(),
|
|
|
|
raw: &self.raw
|
2016-08-22 11:37:50 -04:00
|
|
|
}
|
2017-10-12 23:01:28 -04:00
|
|
|
}
|
|
|
|
fn region_mut(&mut self, index: RangeFrom<Line>) -> RegionMut<T> {
|
|
|
|
assert!(index.start < self.num_lines());
|
|
|
|
RegionMut {
|
|
|
|
start: index.start,
|
|
|
|
end: self.num_lines(),
|
|
|
|
raw: &mut self.raw
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-22 11:37:50 -04:00
|
|
|
|
2017-10-12 23:01:28 -04:00
|
|
|
pub struct RegionIter<'a, T: 'a> {
|
|
|
|
end: Line,
|
|
|
|
cur: Line,
|
|
|
|
raw: &'a VecDeque<Row<T>>,
|
|
|
|
}
|
2016-08-22 11:37:50 -04:00
|
|
|
|
2017-10-12 23:01:28 -04:00
|
|
|
pub struct RegionIterMut<'a, T: 'a> {
|
|
|
|
end: Line,
|
|
|
|
cur: Line,
|
|
|
|
raw: &'a mut VecDeque<Row<T>>,
|
|
|
|
}
|
2016-08-22 11:37:50 -04:00
|
|
|
|
2017-10-12 23:01:28 -04:00
|
|
|
impl<'a, T> IntoIterator for Region<'a, T> {
|
|
|
|
type Item = &'a Row<T>;
|
|
|
|
type IntoIter = RegionIter<'a, T>;
|
2016-08-22 11:37:50 -04:00
|
|
|
|
2017-10-12 23:01:28 -04:00
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
RegionIter {
|
|
|
|
end: self.end,
|
|
|
|
cur: self.start,
|
|
|
|
raw: self.raw
|
2016-08-22 11:37:50 -04:00
|
|
|
}
|
|
|
|
}
|
2017-10-12 23:01:28 -04:00
|
|
|
}
|
2016-08-22 11:37:50 -04:00
|
|
|
|
2017-10-12 23:01:28 -04:00
|
|
|
impl<'a, T> IntoIterator for RegionMut<'a, T> {
|
|
|
|
type Item = &'a mut Row<T>;
|
|
|
|
type IntoIter = RegionIterMut<'a, T>;
|
2016-08-22 11:37:50 -04:00
|
|
|
|
2017-10-12 23:01:28 -04:00
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
RegionIterMut {
|
|
|
|
end: self.end,
|
|
|
|
cur: self.start,
|
|
|
|
raw: self.raw
|
2016-08-22 11:37:50 -04:00
|
|
|
}
|
2017-10-12 23:01:28 -04:00
|
|
|
}
|
|
|
|
}
|
2016-08-22 11:37:50 -04:00
|
|
|
|
2017-10-12 23:01:28 -04:00
|
|
|
impl<'a, T> Iterator for RegionIter<'a, T> {
|
|
|
|
type Item = &'a Row<T>;
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
if self.cur < self.end {
|
|
|
|
let index = self.cur;
|
|
|
|
self.cur += 1;
|
|
|
|
Some(&self.raw[*index])
|
|
|
|
} else {
|
|
|
|
None
|
2016-08-22 11:37:50 -04:00
|
|
|
}
|
|
|
|
}
|
2017-10-12 23:01:28 -04:00
|
|
|
}
|
2017-01-14 20:53:48 -05:00
|
|
|
|
2017-10-12 23:01:28 -04:00
|
|
|
impl<'a, T> Iterator for RegionIterMut<'a, T> {
|
|
|
|
type Item = &'a mut Row<T>;
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
if self.cur < self.end {
|
|
|
|
let index = self.cur;
|
|
|
|
self.cur += 1;
|
|
|
|
unsafe {
|
|
|
|
Some(&mut *(&mut self.raw[index.0] as *mut _))
|
2017-01-14 20:53:48 -05:00
|
|
|
}
|
2017-10-12 23:01:28 -04:00
|
|
|
} else {
|
|
|
|
None
|
2017-01-14 20:53:48 -05:00
|
|
|
}
|
|
|
|
}
|
2016-08-22 11:37:50 -04:00
|
|
|
}
|