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
#[allow(unused)]
use papergrid::Grid;
use std::{
collections::BTreeSet,
ops::{Bound, RangeBounds},
};
pub trait Object: Sized {
fn cells(&self, count_rows: usize, count_columns: usize) -> Vec<(usize, usize)>;
fn and<O: Object>(self, rhs: O) -> Combination<Self, O> {
Combination {
lhs: self,
rhs,
combinator: combine_cells,
}
}
fn not<O: Object>(self, rhs: O) -> Combination<Self, O> {
Combination {
lhs: self,
rhs,
combinator: remove_cells,
}
}
}
pub struct Head;
impl Object for Head {
fn cells(&self, _: usize, count_columns: usize) -> Vec<(usize, usize)> {
(0..count_columns).map(|column| (0, column)).collect()
}
}
pub struct Full;
impl Object for Full {
fn cells(&self, count_rows: usize, count_columns: usize) -> Vec<(usize, usize)> {
(0..count_rows)
.map(|row| {
(0..count_columns)
.map(|column| (row, column))
.collect::<Vec<_>>()
})
.collect::<Vec<_>>()
.concat()
}
}
pub struct Row<R: RangeBounds<usize>>(pub R);
impl<R: RangeBounds<usize>> Object for Row<R> {
fn cells(&self, count_rows: usize, count_columns: usize) -> Vec<(usize, usize)> {
let (x, y) = bounds_to_usize(self.0.start_bound(), self.0.end_bound(), count_rows);
(x..y)
.map(|row| (0..count_columns).map(|column| (row, column)).collect())
.collect::<Vec<Vec<_>>>()
.concat()
}
}
pub struct Column<R: RangeBounds<usize>>(pub R);
impl<R: RangeBounds<usize>> Object for Column<R> {
fn cells(&self, count_rows: usize, count_columns: usize) -> Vec<(usize, usize)> {
let (x, y) = bounds_to_usize(self.0.start_bound(), self.0.end_bound(), count_columns);
(x..y)
.map(|column| (0..count_rows).map(|row| (row, column)).collect())
.collect::<Vec<Vec<_>>>()
.concat()
}
}
pub struct Cell(pub usize, pub usize);
impl Object for Cell {
fn cells(&self, _: usize, _: usize) -> Vec<(usize, usize)> {
vec![(self.0, self.1)]
}
}
type Combinator = fn(Vec<(usize, usize)>, Vec<(usize, usize)>) -> Vec<(usize, usize)>;
pub struct Combination<L, R> {
lhs: L,
rhs: R,
combinator: Combinator,
}
impl<L, R> Object for Combination<L, R>
where
L: Object,
R: Object,
{
fn cells(&self, count_rows: usize, count_columns: usize) -> Vec<(usize, usize)> {
let l = self.lhs.cells(count_rows, count_columns);
let r = self.rhs.cells(count_rows, count_columns);
(self.combinator)(l, r)
}
}
fn combine_cells(lhs: Vec<(usize, usize)>, rhs: Vec<(usize, usize)>) -> Vec<(usize, usize)> {
lhs.into_iter()
.chain(rhs.into_iter())
.collect::<BTreeSet<_>>()
.into_iter()
.collect()
}
fn remove_cells(lhs: Vec<(usize, usize)>, rhs: Vec<(usize, usize)>) -> Vec<(usize, usize)> {
lhs.into_iter().filter(|l| !rhs.contains(l)).collect()
}
pub(crate) 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")
}
}
}