1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
//! This module contains an [`Extract`] structure which is used to
//! obtain an ordinary segment from the [`Table`].
//!
//! There's a similar structure [`Highlight`] which does a highlighting a of segments.
//!
//! [`Table`]: crate::Table
//! [`Highlight`]: crate::settings::highlight::Highlight
use core::cmp::min;
use core::ops::{Bound, RangeBounds, RangeFull};
use crate::{
grid::records::{ExactRecords, Records, Resizable},
settings::TableOption,
};
/// Returns a new [`Table`] that reflects a segment of the referenced [`Table`]
///
/// # Example
///
#[cfg_attr(feature = "std", doc = "```")]
#[cfg_attr(not(feature = "std"), doc = "```ignore")]
/// use tabled::{Table, settings::{Format, object::Rows, Modify, Extract}};
///
/// let data = vec![
/// (0, "Grodno", true),
/// (1, "Minsk", true),
/// (2, "Hamburg", false),
/// (3, "Brest", true),
/// ];
///
/// let table = Table::new(&data)
/// .with(Modify::new(Rows::new(1..)).with(Format::content(|s| format!(": {} :", s))))
/// .with(Extract::segment(1..=2, 1..))
/// .to_string();
///
/// assert_eq!(table, "+------------+----------+\n\
/// | : Grodno : | : true : |\n\
/// +------------+----------+\n\
/// | : Minsk : | : true : |\n\
/// +------------+----------+");
/// ```
///
/// [`Table`]: crate::Table
#[derive(Debug)]
pub struct Extract<R, C> {
rows: R,
columns: C,
}
impl<R, C> Extract<R, C>
where
R: RangeBounds<usize>,
C: RangeBounds<usize>,
{
/// Returns a new [`Table`] that reflects a segment of the referenced [`Table`]
///
/// ```rust,no_run
/// # use tabled::settings::Extract;
/// let rows = 1..3;
/// let columns = 1..;
/// Extract::segment(rows, columns);
/// ```
///
/// # Range
///
/// A [`RangeBounds`] argument can be less than or equal to the shape of a [`Table`]
///
/// If a [`RangeBounds`] argument is malformed or too large the thread will panic
///
/// ```text
/// // Empty Full Out of bounds
/// Extract::segment(0..0, 0..0) Extract::segment(.., ..) Extract::segment(0..1, ..4)
/// []. . . [O O O [O O O X] //ERROR
/// . . . O O O . . .
/// . . . O O O] . . .
/// ```
///
/// [`Table`]: crate::Table
pub fn segment(rows: R, columns: C) -> Self {
Extract { rows, columns }
}
}
impl<R> Extract<R, RangeFull>
where
R: RangeBounds<usize>,
{
/// Returns a new [`Table`] that reflects a segment of the referenced [`Table`]
///
/// The segment is defined by [`RangeBounds`] for Rows
///
/// ```rust,no_run
/// # use tabled::settings::Extract;
/// Extract::rows(1..3);
/// ```
///
/// # Range
///
/// A [`RangeBounds`] argument can be less than or equal to the shape of a [`Table`]
///
/// If a [`RangeBounds`] argument is malformed or too large the thread will panic
///
/// ```text
/// // Empty Full Out of bounds
/// Extract::rows(0..0) Extract::rows(..) Extract::rows(0..4)
/// []. . . [O O O [O O O
/// . . . O O O O O O
/// . . . O O O] O O O
/// X X X] // ERROR
/// ```
///
/// [`Table`]: crate::Table
pub fn rows(rows: R) -> Self {
Extract { rows, columns: .. }
}
}
impl<C> Extract<RangeFull, C>
where
C: RangeBounds<usize>,
{
/// Returns a new [`Table`] that reflects a segment of the referenced [`Table`]
///
/// The segment is defined by [`RangeBounds`] for columns.
///
/// ```rust,no_run
/// # use tabled::settings::Extract;
/// Extract::columns(1..3);
/// ```
///
/// # Range
///
/// A [`RangeBounds`] argument can be less than or equal to the shape of a [`Table`]
///
/// If a [`RangeBounds`] argument is malformed or too large the thread will panic
///
/// ```text
/// // Empty Full Out of bounds
/// Extract::columns(0..0) Extract::columns(..) Extract::columns(0..4)
/// []. . . [O O O [O O O X
/// . . . O O O O O O X
/// . . . O O O] O O O X] // ERROR
/// ```
///
/// [`Table`]: crate::Table
pub fn columns(columns: C) -> Self {
Extract { rows: .., columns }
}
}
impl<R, C, RR, D, Cfg> TableOption<RR, D, Cfg> for Extract<R, C>
where
R: RangeBounds<usize> + Clone,
C: RangeBounds<usize> + Clone,
RR: Records + ExactRecords + Resizable,
{
fn change(self, records: &mut RR, _: &mut Cfg, _: &mut D) {
let count_rows = records.count_rows();
let count_columns = records.count_columns();
let mut rows = bounds_to_usize(self.rows.start_bound(), self.rows.end_bound(), count_rows);
let mut cols = bounds_to_usize(
self.columns.start_bound(),
self.columns.end_bound(),
count_columns,
);
// Cleanup table in case if boundaries are exceeded.
//
// todo: can be optimized by adding a clear() method to Resizable
rows.0 = min(rows.0, count_rows);
cols.0 = min(cols.0, count_columns);
extract(records, (count_rows, count_columns), rows, cols);
}
}
/// Returns a new [`Grid`] that reflects a segment of the referenced [`Grid`].
///
/// # Example
///
/// ```text
/// grid
/// +---+---+---+
/// |0-0|0-1|0-2|
/// +---+---+---+
/// |1-0|1-1|1-2|
/// +---+---+---+
/// |2-0|2-1|2-2|
/// +---+---+---+
///
/// let rows = ..;
/// let columns = ..1;
/// grid.extract(rows, columns)
///
/// grid
/// +---+
/// |0-0|
/// +---+
/// |1-0|
/// +---+
/// |2-0|
/// +---+
/// ```
fn extract<R>(
records: &mut R,
(count_rows, count_cols): (usize, usize),
(start_row, end_row): (usize, usize),
(start_col, end_col): (usize, usize),
) where
R: Resizable,
{
for (i, row) in (0..start_row).enumerate() {
let row = row - i;
records.remove_row(row);
}
let count_rows = count_rows - start_row;
let end_row = end_row - start_row;
for (i, row) in (end_row..count_rows).enumerate() {
let row = row - i;
records.remove_row(row);
}
for (i, col) in (0..start_col).enumerate() {
let col = col - i;
records.remove_column(col);
}
let count_cols = count_cols - start_col;
let end_col = end_col - start_col;
for (i, col) in (end_col..count_cols).enumerate() {
let col = col - i;
records.remove_column(col);
}
}
fn bounds_to_usize(
left: Bound<&usize>,
right: Bound<&usize>,
count_elements: usize,
) -> (usize, usize) {
match (left, right) {
(Bound::Included(x), Bound::Included(y)) => (*x, y + 1),
(Bound::Included(x), Bound::Excluded(y)) => (*x, *y),
(Bound::Included(x), Bound::Unbounded) => (*x, count_elements),
(Bound::Unbounded, Bound::Unbounded) => (0, count_elements),
(Bound::Unbounded, Bound::Included(y)) => (0, y + 1),
(Bound::Unbounded, Bound::Excluded(y)) => (0, *y),
(Bound::Excluded(_), Bound::Unbounded)
| (Bound::Excluded(_), Bound::Included(_))
| (Bound::Excluded(_), Bound::Excluded(_)) => {
unreachable!("A start bound can't be excluded")
}
}
}
|