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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
pub mod array2d {
    use core::iter::{DoubleEndedIterator, Iterator};
    use core::mem;
    use core::ops::{Index, IndexMut};

    /// trait that is used for indexing the 2d array
    pub trait GridIdx {
        fn no_row(&self) -> usize;
        fn no_column(&self) -> usize;
    }

    #[derive(Debug)]
    pub struct ColumMut<'a, T>
    {
        pub(super) v: &'a mut [T],
        pub(super) skip: usize,
    }

    impl<'a, T> Index<usize> for ColumMut<'a, T>
    {
        type Output = T;

        fn index(&self, idx: usize) -> &Self::Output {
            let pos = idx * (1 + self.skip);
            &self.v[pos]
        }
    }

    impl<'a, T> IndexMut<usize> for ColumMut<'a, T>
    {
        /// indexing the column, mutable
        fn index_mut(&mut self, idx: usize) -> &mut Self::Output {
            let pos = idx * (1 + self.skip);
            &mut self.v[pos]
        }
    }

    impl<'a, T> Iterator for ColumMut<'a, T>
    {
        type Item = &'a mut T;

        /// next item in the column, mutable
        fn next(&mut self) -> Option<Self::Item> {
            let tmp = mem::take(&mut self.v);
            if let Some((fst, snd)) = tmp.split_first_mut() {
                if snd.is_empty() {
                    self.v = &mut [];
                } else {
                    self.v = snd.get_mut(self.skip..).unwrap();
                }
                Some(fst)
            } else {
                None
            }
        }
    }

    #[derive(Debug)]
    pub struct Column<'a, T>
    {
        pub(super) v: &'a [T],
        pub(super) skip: usize,
    }

    impl<'a, T> Index<usize> for Column<'a, T>
    {
        type Output = T;

        fn index(&self, idx: usize) -> &Self::Output {
            let pos = idx * (1 + self.skip);
            &self.v[pos]
        }
    }

    impl<'a, T> Iterator for Column<'a, T>
    {
        type Item = &'a T;

        /// next item in the column
        fn next(&mut self) -> Option<Self::Item> {
            if let Some((fst, snd)) = self.v.split_first() {
                if snd.is_empty() {
                    self.v = &[];
                } else {
                    self.v = snd.get(self.skip..).unwrap();
                }
                Some(fst)
            } else {
                None
            }
        }
    }

    /// wrapper struct for iterating over rows
    #[derive(Debug)]
    pub struct Rows<'a, T> {
        pub(super) v: &'a [T],
        pub(super) columns: usize,
        pub(super) skip_columns: usize,
    }

    impl<'a, T> DoubleEndedIterator for Rows<'a, T>
    {
        fn next_back(&mut self) -> Option<Self::Item> {
            if self.v.is_empty() {
                None
            } else {
                let (fst, snd) = self.v.split_at(self.v.len() - self.columns);
                if fst.is_empty() {
                    self.v = &[];
                } else {
                    self.v = fst.get(..fst.len() - self.skip_columns).unwrap();
                }
                Some(snd)
            }
        }

        fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
            let (adj,overflow) = n.overflowing_mul(self.columns + self.skip_columns);
            if adj >= self.v.len() || overflow{
                self.v = &[];
            }else{
                self.v = self.v.get(..self.v.len() - adj).unwrap();
            }
            self.next_back()
        }


    }

    impl<'a, T> Iterator for Rows<'a, T>
    {
        type Item = &'a [T];

        fn next(&mut self) -> Option<Self::Item> {
            if self.v.is_empty() {
                None
            } else {
                let (fst, snd) = self.v.split_at(self.columns);
                if snd.is_empty() {
                    self.v = &[];
                } else {
                    self.v = snd.get(self.skip_columns..).unwrap();
                }

                Some(fst)
            }
        }
    }

    /// wrapper struct for iterating over mutable rows
    #[derive(Debug)]
    pub struct RowsMut<'a, T> {
        pub(super) v: &'a mut [T],
        pub(super) no_columns: usize,
        pub(super) skip_columns: usize,
    }

    impl<'a, T> DoubleEndedIterator for RowsMut<'a, T>
    {
        fn next_back(&mut self) -> Option<Self::Item> {
            if self.v.is_empty() {
                None
            } else {
                let tmp = mem::take(&mut self.v);
                let tmp_len = tmp.len();
                let (fst, snd) = tmp.split_at_mut(tmp_len - self.no_columns);
                if fst.is_empty() {
                    self.v = &mut [];
                } else {
                    self.v = fst.get_mut(..tmp_len - self.no_columns - self.skip_columns).unwrap();
                }
                Some(snd)
            }
        }

        fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
            let (adj,overflow)  = n.overflowing_mul(self.no_columns + self.skip_columns);
            if adj >= self.v.len() || overflow{
                self.v = &mut [];
            }
            else {
                let tmp = mem::take(&mut self.v);
                self.v = tmp.get_mut(..self.v.len() - adj).unwrap();
            }

            self.next_back()
        }
    }

    impl<'a, T> Iterator for RowsMut<'a, T> {
        type Item = &'a mut [T];

        fn next(&mut self) -> Option<Self::Item> {
            if !self.v.is_empty() && self.skip_columns < self.no_columns {
                let tmp = mem::take(&mut self.v);
                let (head, tail) = tmp.split_at_mut(self.no_columns);
                if tail.is_empty() {
                    self.v = &mut [];
                } else {
                    self.v = tail.get_mut(self.skip_columns..).unwrap()
                }
                return Some(head);
            }
            None
        }
    }

    /// struct that expresses index in 2d array
    pub struct GridPos {
        pub row: usize,
        pub column: usize,
    }

    impl GridPos {
        pub fn new(r: usize, c: usize) -> Self {
            Self { row: r, column: c }
        }
    }

    impl GridIdx for GridPos {
        fn no_row(&self) -> usize {
            self.row
        }

        fn no_column(&self) -> usize {
            self.column
        }
    }

    impl GridIdx for (usize, usize) {
        fn no_row(&self) -> usize {
            self.0
        }

        fn no_column(&self) -> usize {
            self.1
        }
    }

    impl GridIdx for [usize; 2] {
        fn no_row(&self) -> usize {
            self[0]
        }

        fn no_column(&self) -> usize {
            self[1]
        }
    }

    #[derive(Debug)]
    /// the main struct for the 2d array
    pub struct Array2d<T> {
        vec_slice: Box<[T]>,
        no_rows: usize,
        no_columns: usize,
    }

    /// default implementation, creates an empty array
    impl<T> Default for Array2d<T> {
        fn default() -> Self {
            Array2d {
                vec_slice: Box::new([]),
                no_rows: 0,
                no_columns: 0,
            }
        }
    }

    impl<T> Array2d<T> {
        /// create a new 2d array each elem of type T where T is clonable
        pub fn filled_with(element: T, r: usize, c: usize) -> Self
            where
                T: Clone,
        {
            assert!(r >= 1 && c >= 1);
            let v = vec![element; r * c];
            let vb = v.into_boxed_slice();
            Array2d {
                vec_slice: vb,
                no_rows: r,
                no_columns: c,
            }
        }

        // get mutable column
        pub fn column_mut(&mut self, no_column: usize) -> ColumMut<'_, T>
        {
            assert!(no_column < self.column_count());
            let c = self.column_count();
            ColumMut {
                v: self.vec_slice.get_mut(no_column..self.vec_slice.len() - self.column_count() + no_column + 1).unwrap(),
                skip: c - 1,
            }
        }

        /// get column
        pub fn column(&self, no_column: usize) -> Column<'_, T>
        {
            assert!(no_column < self.column_count());
            Column {
                v: self.vec_slice.get(no_column..self.vec_slice.len() - self.column_count() + no_column + 1).unwrap(),
                skip: self.column_count() - 1,
            }
        }

        /// create a new 2d array each elem of type T where T is the default implementation
        pub fn filled_with_default(r: usize, c: usize) -> Self
            where
                T: Default,
        {
            assert!(r >= 1 && c >= 1);
            let mut v = Vec::with_capacity(r * c);
            for _ in 0..(r * c) {
                v.push(T::default());
            }
            let vb = v.into_boxed_slice();
            Array2d {
                vec_slice: vb,
                no_rows: r,
                no_columns: c,
            }
        }

        /// return the 2d array as 1d slice iterable
        pub fn iter(&self) -> impl Iterator<Item=&T> {
            self.vec_slice.iter()
        }

        /// return the row count
        pub fn row_count(&self) -> usize {
            self.no_rows
        }

        /// return the column count
        pub fn column_count(&self) -> usize {
            self.no_columns
        }

        /// convert 2d position to 1d position row_to * column_count + column_to, row_major
        pub fn d2_index_d1<F>(&self, pos: &F) -> usize
            where
                F: GridIdx,
        {
            pos.no_row() * self.column_count() + pos.no_column()
        }

        /// swap two position values
        pub fn swap<F, K>(&mut self, pos1: &F, pos2: &K)
            where
                F: GridIdx,
                K: GridIdx,
        {
            let converted_rc1 = self.d2_index_d1(pos1);
            let converted_rc2 = self.d2_index_d1(pos2);
            self.vec_slice.swap(converted_rc1, converted_rc2);
        }

        /// return the row between containing the index
        pub fn row_between(&self, row_index: usize) -> (usize, usize) {
            assert!(row_index < self.row_count());
            let start = row_index * self.column_count();
            let end = start + self.column_count();
            (start, end)
        }

        /// return row as iterable
        pub fn iter_row(&self, row_index: usize) -> impl Iterator<Item=&T> {
            let (start, end) = self.row_between(row_index);
            self.vec_slice[start..end].iter()
        }

        /// return row as mutable
        pub fn mut_row(&mut self, row_index: usize) -> &mut [T] {
            let (start, end) = self.row_between(row_index);
            &mut self.vec_slice[start..end]
        }

        /// return row as mutable iterable
        pub fn iter_mut_row(&mut self, row_index: usize) -> impl Iterator<Item=&mut T> {
            let (start, end) = self.row_between(row_index);
            self.vec_slice[start..end].iter_mut()
        }

        /// iterate over the rows as mutable
        pub fn iter_mut_rows(&mut self) -> RowsMut<'_, T> {
            let c = self.column_count();
            RowsMut {
                v: &mut self.vec_slice,
                no_columns: c,
                skip_columns: 0,
            }
        }

        /// iterate over the rows
        pub fn iter_rows(&self) -> Rows<'_, T> {
            //impl Iterator<Item=impl Iterator<Item=&T>> {
            //(0_usize..self.row_count()).map(move |row_index| self.iter_row(row_index))
            let c = self.column_count();
            Rows {
                v: &self.vec_slice,
                columns: c,
                skip_columns: 0,
            }
        }

        pub fn as_slice(&self) -> &[T] {
            &self.vec_slice
        }
    }

    impl<T, Idx: GridIdx> Index<Idx> for Array2d<T> {
        type Output = T;
        fn index(&self, index: Idx) -> &Self::Output {
            &self.vec_slice[self.d2_index_d1(&GridPos::new(index.no_row(), index.no_column()))]
        }
    }

    impl<T, Idx: GridIdx> IndexMut<Idx> for Array2d<T> {
        fn index_mut(&mut self, index: Idx) -> &mut Self::Output {
            &mut self.vec_slice[self.d2_index_d1(&GridPos::new(index.no_row(), index.no_column()))]
        }
    }
}