terminal_emulator/grid/
row.rs1use std::cmp::{max, min};
18use std::ops::{Index, IndexMut};
19use std::ops::{Range, RangeFrom, RangeFull, RangeTo, RangeToInclusive};
20use std::slice;
21
22use crate::index::Column;
23
24#[derive(Default, Clone, Debug)]
26pub struct Row<T> {
27    inner: Vec<T>,
28
29    pub(crate) occ: usize,
38}
39
40impl<T: PartialEq> PartialEq for Row<T> {
41    fn eq(&self, other: &Self) -> bool {
42        self.inner == other.inner
43    }
44}
45
46impl<T: Copy + Clone> Row<T> {
47    pub fn new(columns: Column, template: &T) -> Self {
48        Self {
49            inner: vec![*template; *columns],
50            occ: 0,
51        }
52    }
53
54    pub fn grow(&mut self, cols: Column, template: &T) {
55        assert!(self.len() < *cols);
56
57        while self.len() != *cols {
58            self.inner.push(*template);
59        }
60    }
61
62    #[inline(never)]
64    pub fn reset(&mut self, other: &T) {
65        let occ = self.occ;
66        for item in &mut self.inner[..occ] {
67            *item = *other;
68        }
69
70        self.occ = 0;
71    }
72}
73
74#[allow(clippy::len_without_is_empty)]
75impl<T> Row<T> {
76    pub fn shrink(&mut self, cols: Column) {
77        while self.len() != *cols {
78            self.inner.pop();
79        }
80
81        self.occ = min(self.occ, *cols);
82    }
83
84    pub fn len(&self) -> usize {
85        self.inner.len()
86    }
87
88    pub fn iter(&self) -> slice::Iter<'_, T> {
89        self.inner.iter()
90    }
91}
92
93impl<'a, T> IntoIterator for &'a Row<T> {
94    type Item = &'a T;
95    type IntoIter = slice::Iter<'a, T>;
96
97    #[inline]
98    fn into_iter(self) -> slice::Iter<'a, T> {
99        self.iter()
100    }
101}
102
103impl<'a, T> IntoIterator for &'a mut Row<T> {
104    type Item = &'a mut T;
105    type IntoIter = slice::IterMut<'a, T>;
106
107    #[inline]
108    fn into_iter(self) -> slice::IterMut<'a, T> {
109        self.occ = self.len();
110        self.inner.iter_mut()
111    }
112}
113
114impl<T> Index<Column> for Row<T> {
115    type Output = T;
116
117    #[inline]
118    fn index(&self, index: Column) -> &T {
119        &self.inner[index.0]
120    }
121}
122
123impl<T> IndexMut<Column> for Row<T> {
124    #[inline]
125    fn index_mut(&mut self, index: Column) -> &mut T {
126        self.occ = max(self.occ, *index + 1);
127        &mut self.inner[index.0]
128    }
129}
130
131impl<T> Index<Range<Column>> for Row<T> {
136    type Output = [T];
137
138    #[inline]
139    fn index(&self, index: Range<Column>) -> &[T] {
140        &self.inner[(index.start.0)..(index.end.0)]
141    }
142}
143
144impl<T> IndexMut<Range<Column>> for Row<T> {
145    #[inline]
146    fn index_mut(&mut self, index: Range<Column>) -> &mut [T] {
147        self.occ = max(self.occ, *index.end);
148        &mut self.inner[(index.start.0)..(index.end.0)]
149    }
150}
151
152impl<T> Index<RangeTo<Column>> for Row<T> {
153    type Output = [T];
154
155    #[inline]
156    fn index(&self, index: RangeTo<Column>) -> &[T] {
157        &self.inner[..(index.end.0)]
158    }
159}
160
161impl<T> IndexMut<RangeTo<Column>> for Row<T> {
162    #[inline]
163    fn index_mut(&mut self, index: RangeTo<Column>) -> &mut [T] {
164        self.occ = max(self.occ, *index.end);
165        &mut self.inner[..(index.end.0)]
166    }
167}
168
169impl<T> Index<RangeFrom<Column>> for Row<T> {
170    type Output = [T];
171
172    #[inline]
173    fn index(&self, index: RangeFrom<Column>) -> &[T] {
174        &self.inner[(index.start.0)..]
175    }
176}
177
178impl<T> IndexMut<RangeFrom<Column>> for Row<T> {
179    #[inline]
180    fn index_mut(&mut self, index: RangeFrom<Column>) -> &mut [T] {
181        self.occ = self.len();
182        &mut self.inner[(index.start.0)..]
183    }
184}
185
186impl<T> Index<RangeFull> for Row<T> {
187    type Output = [T];
188
189    #[inline]
190    fn index(&self, _: RangeFull) -> &[T] {
191        &self.inner[..]
192    }
193}
194
195impl<T> IndexMut<RangeFull> for Row<T> {
196    #[inline]
197    fn index_mut(&mut self, _: RangeFull) -> &mut [T] {
198        self.occ = self.len();
199        &mut self.inner[..]
200    }
201}
202
203impl<T> Index<RangeToInclusive<Column>> for Row<T> {
204    type Output = [T];
205
206    #[inline]
207    fn index(&self, index: RangeToInclusive<Column>) -> &[T] {
208        &self.inner[..=(index.end.0)]
209    }
210}
211
212impl<T> IndexMut<RangeToInclusive<Column>> for Row<T> {
213    #[inline]
214    fn index_mut(&mut self, index: RangeToInclusive<Column>) -> &mut [T] {
215        self.occ = max(self.occ, *index.end);
216        &mut self.inner[..=(index.end.0)]
217    }
218}