// 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. //! Defines the Row type which makes up lines in the grid use std::ops::{Index, IndexMut}; use std::ops::{Range, RangeTo, RangeFrom, RangeFull, RangeToInclusive}; use std::cmp::{max, min}; use std::slice; use index::Column; /// A row in the grid #[derive(Default, Clone, Debug, Serialize, Deserialize)] pub struct Row { inner: Vec, /// occupied entries /// /// Semantically, this value can be understood as the **end** of an /// Exclusive Range. Thus, /// /// - Zero means there are no occupied entries /// - 1 means there is a value at index zero, but nowhere else /// - `occ == inner.len` means every value is occupied pub(crate) occ: usize, } impl PartialEq for Row { fn eq(&self, other: &Self) -> bool { self.inner == other.inner } } impl Row { pub fn new(columns: Column, template: &T) -> Row { Row { inner: vec![*template; *columns], occ: 0, } } pub fn grow(&mut self, cols: Column, template: &T) { assert!(self.len() < * cols); while self.len() != *cols { self.inner.push(*template); } } /// Resets contents to the contents of `other` #[inline(never)] pub fn reset(&mut self, other: &T) { let occ = self.occ; for item in &mut self.inner[..occ] { *item = *other; } self.occ = 0; } } #[allow(clippy::len_without_is_empty)] impl Row { pub fn shrink(&mut self, cols: Column) { while self.len() != *cols { self.inner.pop(); } self.occ = min(self.occ, *cols); } pub fn len(&self) -> usize { self.inner.len() } pub fn iter(&self) -> slice::Iter { self.inner.iter() } } impl<'a, T> IntoIterator for &'a Row { type Item = &'a T; type IntoIter = slice::Iter<'a, T>; #[inline] fn into_iter(self) -> slice::Iter<'a, T> { self.iter() } } impl<'a, T> IntoIterator for &'a mut Row { type Item = &'a mut T; type IntoIter = slice::IterMut<'a, T>; #[inline] fn into_iter(self) -> slice::IterMut<'a, T> { self.occ = self.len(); self.inner.iter_mut() } } impl Index for Row { type Output = T; #[inline] fn index(&self, index: Column) -> &T { &self.inner[index.0] } } impl IndexMut for Row { #[inline] fn index_mut(&mut self, index: Column) -> &mut T { self.occ = max(self.occ, *index + 1); &mut self.inner[index.0] } } // ----------------------------------------------------------------------------- // Index ranges of columns // ----------------------------------------------------------------------------- impl Index> for Row { type Output = [T]; #[inline] fn index(&self, index: Range) -> &[T] { &self.inner[(index.start.0)..(index.end.0)] } } impl IndexMut> for Row { #[inline] fn index_mut(&mut self, index: Range) -> &mut [T] { self.occ = max(self.occ, *index.end); &mut self.inner[(index.start.0)..(index.end.0)] } } impl Index> for Row { type Output = [T]; #[inline] fn index(&self, index: RangeTo) -> &[T] { &self.inner[..(index.end.0)] } } impl IndexMut> for Row { #[inline] fn index_mut(&mut self, index: RangeTo) -> &mut [T] { self.occ = max(self.occ, *index.end); &mut self.inner[..(index.end.0)] } } impl Index> for Row { type Output = [T]; #[inline] fn index(&self, index: RangeFrom) -> &[T] { &self.inner[(index.start.0)..] } } impl IndexMut> for Row { #[inline] fn index_mut(&mut self, index: RangeFrom) -> &mut [T] { self.occ = self.len(); &mut self.inner[(index.start.0)..] } } impl Index for Row { type Output = [T]; #[inline] fn index(&self, _: RangeFull) -> &[T] { &self.inner[..] } } impl IndexMut for Row { #[inline] fn index_mut(&mut self, _: RangeFull) -> &mut [T] { self.occ = self.len(); &mut self.inner[..] } } impl Index> for Row { type Output = [T]; #[inline] fn index(&self, index: RangeToInclusive) -> &[T] { &self.inner[..=(index.end.0)] } } impl IndexMut> for Row { #[inline] fn index_mut(&mut self, index: RangeToInclusive) -> &mut [T] { self.occ = max(self.occ, *index.end); &mut self.inner[..=(index.end.0)] } }