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
use core::ops::{Index, IndexMut};
use core::cmp::Ordering;
use core::borrow::Borrow;
use core::ptr;
use crate::iter::*;
use crate::view::*;
use crate::flattenexact::*;
pub type Coordinate = (usize, usize);
pub type Cells<'a, T> = FlattenExact<Rows<'a, T>>;
pub type CellsMut<'a, T> = FlattenExact<RowsMut<'a, T>>;
pub trait TooDeeOps<T> : Index<usize, Output=[T]> + Index<Coordinate, Output=T> {
fn num_cols(&self) -> usize;
fn num_rows(&self) -> usize;
fn size(&self) -> (usize, usize) {
(self.num_cols(), self.num_rows())
}
fn is_empty(&self) -> bool {
self.num_cols() == 0 || self.num_rows() == 0
}
fn bounds(&self) -> (Coordinate, Coordinate);
fn view(&self, start: Coordinate, end: Coordinate) -> TooDeeView<'_, T>;
fn rows(&self) -> Rows<'_, T>;
fn col(&self, col: usize) -> Col<'_, T>;
fn cells(&self) -> Cells<'_, T> {
FlattenExact::new(self.rows())
}
}
pub trait TooDeeOpsMut<T> : TooDeeOps<T> + IndexMut<usize,Output=[T]> + IndexMut<Coordinate, Output=T> {
fn view_mut(&mut self, start: Coordinate, end: Coordinate) -> TooDeeViewMut<'_, T>;
fn rows_mut(&mut self) -> RowsMut<'_, T>;
fn col_mut(&mut self, col: usize) -> ColMut<'_, T>;
fn cells_mut(&mut self) -> CellsMut<'_, T> {
FlattenExact::new(self.rows_mut())
}
fn fill<V>(&mut self, fill: V)
where
V: Borrow<T>,
T: Clone {
let value = fill.borrow();
for r in self.rows_mut() {
for v in r {
v.clone_from(value);
}
}
}
fn swap_cols(&mut self, c1: usize, c2: usize) {
let num_cols = self.num_cols();
assert!(c1 < num_cols);
assert!(c2 < num_cols);
for r in self.rows_mut() {
unsafe {
let pa: *mut T = r.get_unchecked_mut(c1);
let pb: *mut T = r.get_unchecked_mut(c2);
ptr::swap(pa, pb);
}
}
}
fn swap_rows(&mut self, mut r1: usize, mut r2: usize) {
match r1.cmp(&r2) {
Ordering::Less => {},
Ordering::Greater => {
core::mem::swap(&mut r1, &mut r2);
},
Ordering::Equal => {
return;
}
}
assert!(r2 < self.num_rows());
let mut iter = self.rows_mut();
let tmp = iter.nth(r1).unwrap();
tmp.swap_with_slice(iter.nth(r2-r1-1).unwrap());
}
fn row_pair_mut(&mut self, r1: usize, r2: usize) -> (&mut [T], &mut [T]) {
let num_rows = self.num_rows();
assert!(r1 < num_rows);
assert!(r2 < num_rows);
assert!(r1 != r2);
match r1.cmp(&r2) {
Ordering::Less => {
let mut iter = self.rows_mut();
let tmp = iter.nth(r1).unwrap();
(tmp, iter.nth(r2-r1-1).unwrap())
},
Ordering::Greater => {
let mut iter = self.rows_mut();
let tmp = iter.nth(r2).unwrap();
(iter.nth(r1-r2-1).unwrap(), tmp)
},
Ordering::Equal => {
unreachable!("r1 != r2");
},
}
}
}