1use papergrid::config::Position;
2
3#[cfg(feature = "std")]
4use crate::grid::records::vec_records::VecRecords;
5
6/// A records representation which can be modified by moving rows/columns around.
7pub trait Resizable {
8 /// Swap cells with one another.
9 fn swap(&mut self, lhs: Position, rhs: Position);
10 /// Swap rows with one another.
11 fn swap_row(&mut self, lhs: usize, rhs: usize);
12 /// Swap columns with one another.
13 fn swap_column(&mut self, lhs: usize, rhs: usize);
14 /// Adds a new row to a data set.
15 fn push_row(&mut self);
16 /// Adds a new column to a data set.
17 fn push_column(&mut self);
18 /// Removes a row from a data set by index.
19 fn remove_row(&mut self, row: usize);
20 /// Removes a column from a data set by index.
21 fn remove_column(&mut self, column: usize);
22 /// Inserts a row at index.
23 fn insert_row(&mut self, row: usize);
24 /// Inserts column at index.
25 fn insert_column(&mut self, column: usize);
26}
27
28impl<T> Resizable for &'_ mut T
29where
30 T: Resizable,
31{
32 fn swap(&mut self, lhs: Position, rhs: Position) {
33 T::swap(self, lhs, rhs)
34 }
35
36 fn swap_row(&mut self, lhs: usize, rhs: usize) {
37 T::swap_row(self, lhs, rhs)
38 }
39
40 fn swap_column(&mut self, lhs: usize, rhs: usize) {
41 T::swap_column(self, lhs, rhs)
42 }
43
44 fn push_row(&mut self) {
45 T::push_row(self)
46 }
47
48 fn push_column(&mut self) {
49 T::push_column(self)
50 }
51
52 fn remove_row(&mut self, row: usize) {
53 T::remove_row(self, row)
54 }
55
56 fn remove_column(&mut self, column: usize) {
57 T::remove_column(self, column)
58 }
59
60 fn insert_row(&mut self, row: usize) {
61 T::insert_row(self, row)
62 }
63
64 fn insert_column(&mut self, column: usize) {
65 T::insert_column(self, column)
66 }
67}
68
69#[cfg(feature = "std")]
70impl<T> Resizable for Vec<Vec<T>>
71where
72 T: Default + Clone,
73{
74 fn swap(&mut self, lhs: Position, rhs: Position) {
75 if lhs == rhs {
76 return;
77 }
78
79 let t = std::mem::take(&mut self[lhs.0][lhs.1]);
80 let t = std::mem::replace(&mut self[rhs.0][rhs.1], t);
81 let _ = std::mem::replace(&mut self[lhs.0][lhs.1], t);
82 }
83
84 fn swap_row(&mut self, lhs: usize, rhs: usize) {
85 let t = std::mem::take(&mut self[lhs]);
86 let t = std::mem::replace(&mut self[rhs], t);
87 let _ = std::mem::replace(&mut self[lhs], t);
88 }
89
90 fn swap_column(&mut self, lhs: usize, rhs: usize) {
91 for row in self.iter_mut() {
92 row.swap(lhs, rhs);
93 }
94 }
95
96 fn push_row(&mut self) {
97 let count_columns = self.get(0).map(|l| l.len()).unwrap_or(0);
98 self.push(vec![T::default(); count_columns]);
99 }
100
101 fn push_column(&mut self) {
102 for row in self.iter_mut() {
103 row.push(T::default());
104 }
105 }
106
107 fn remove_row(&mut self, row: usize) {
108 let _ = self.remove(row);
109 }
110
111 fn remove_column(&mut self, column: usize) {
112 for row in self.iter_mut() {
113 let _ = row.remove(column);
114 }
115 }
116
117 fn insert_row(&mut self, row: usize) {
118 let count_columns = self.get(0).map(|l| l.len()).unwrap_or(0);
119 self.insert(row, vec![T::default(); count_columns]);
120 }
121
122 fn insert_column(&mut self, column: usize) {
123 for row in self {
124 row.insert(column, T::default());
125 }
126 }
127}
128
129#[cfg(feature = "std")]
130impl<T> Resizable for VecRecords<T>
131where
132 T: Default + Clone,
133{
134 fn swap(&mut self, lhs: Position, rhs: Position) {
135 if lhs == rhs {
136 return;
137 }
138
139 let t = std::mem::take(&mut self[lhs.0][lhs.1]);
140 let t = std::mem::replace(&mut self[rhs.0][rhs.1], t);
141 let _ = std::mem::replace(&mut self[lhs.0][lhs.1], t);
142 }
143
144 fn swap_row(&mut self, lhs: usize, rhs: usize) {
145 let t = std::mem::take(&mut self[lhs]);
146 let t = std::mem::replace(&mut self[rhs], t);
147 let _ = std::mem::replace(&mut self[lhs], t);
148 }
149
150 fn swap_column(&mut self, lhs: usize, rhs: usize) {
151 for row in self.iter_mut() {
152 row.swap(lhs, rhs);
153 }
154 }
155
156 fn push_row(&mut self) {
157 let records = std::mem::replace(self, VecRecords::new(vec![]));
158 let mut data: Vec<Vec<_>> = records.into();
159
160 let count_columns = data.get(0).map(|l| l.len()).unwrap_or(0);
161 data.push(vec![T::default(); count_columns]);
162
163 *self = VecRecords::new(data);
164 }
165
166 fn push_column(&mut self) {
167 let records = std::mem::replace(self, VecRecords::new(vec![]));
168 let mut data: Vec<Vec<_>> = records.into();
169
170 for row in &mut data {
171 row.push(T::default());
172 }
173
174 *self = VecRecords::new(data);
175 }
176
177 fn remove_row(&mut self, row: usize) {
178 let records = std::mem::replace(self, VecRecords::new(vec![]));
179 let mut data: Vec<Vec<_>> = records.into();
180
181 let _ = data.remove(row);
182
183 *self = VecRecords::new(data);
184 }
185
186 fn remove_column(&mut self, column: usize) {
187 let records = std::mem::replace(self, VecRecords::new(vec![]));
188 let mut data: Vec<Vec<_>> = records.into();
189
190 for row in &mut data {
191 let _ = row.remove(column);
192 }
193
194 *self = VecRecords::new(data);
195 }
196
197 fn insert_row(&mut self, row: usize) {
198 let records = std::mem::replace(self, VecRecords::new(vec![]));
199 let mut data: Vec<Vec<_>> = records.into();
200
201 let count_columns = data.get(0).map(|l| l.len()).unwrap_or(0);
202 data.insert(row, vec![T::default(); count_columns]);
203
204 *self = VecRecords::new(data);
205 }
206
207 fn insert_column(&mut self, column: usize) {
208 let records = std::mem::replace(self, VecRecords::new(vec![]));
209 let mut data: Vec<Vec<_>> = records.into();
210
211 for row in &mut data {
212 row.insert(column, T::default());
213 }
214
215 *self = VecRecords::new(data);
216 }
217}
218