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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
// ladata::grid::dyn2d
//
//!
//
use crate::error::{LadataError as Error, LadataResult as Result};
use core::{
cmp::min,
ops::{Index, IndexMut},
};
use alloc::{vec, vec::Vec};
/// A dynamic 2D grid, backed by a [`Vec`].
///
/// Internally the elements are stored in *row major order*,
/// meaning the elements of each row are stored sequentially.
pub struct DynGrid2D<T> {
cols: usize,
rows: usize,
grid: Vec<T>,
}
/// # constructors
impl<T: Clone> DynGrid2D<T> {
/// Creates a new `DynGrid2D` with a size of `cols` × `rows`, filled with `element`.
///
/// # Examples
/// ```
/// use ladata::grid::DynGrid2D;
///
/// let mut s = DynGrid2D::new(' ', 10, 10);
/// ```
#[inline]
pub fn new(element: T, cols: usize, rows: usize) -> Self {
Self {
cols,
rows,
grid: vec![element; cols * rows],
}
}
/// Creates a new `DynGrid2D` from a slice of rows.
///
/// All rows must have the same length.
pub fn from_rows(rows: &[Vec<T>]) -> Result<Self> {
let row_len = rows.get(0).map(Vec::len).unwrap_or(0);
if !rows.iter().all(|row| row.len() == row_len) {
return Err(Error::DimensionMismatch);
}
Ok(Self {
cols: row_len,
rows: rows.len(),
grid: Self::flatten(rows),
})
}
/// Creates a new `DynGrid2D` from a slice of `columns`.
///
/// All columns must have the same length.
pub fn from_cols(columns: &[Vec<T>]) -> Result<Self> {
let col_len = columns.get(0).map(Vec::len).unwrap_or(0);
if !columns.iter().all(|col| col.len() == col_len) {
return Err(Error::DimensionMismatch);
}
let rows = col_len;
let cols = columns.len();
let indices_row_order = (0..rows).flat_map(move |row| (0..cols).map(move |col| (col, row)));
let grid = indices_row_order
.map(|(col, row)| columns[col][row].clone())
.collect();
Ok(DynGrid2D { cols, rows, grid })
}
/// Creates a new `DynGrid2D` from the given flat slice of `elements`, in *row major order*.
///
/// The number of `elements` must equal `cols`×`rows`.
pub fn from_row_order(elements: &[T], cols: usize, rows: usize) -> Result<Self> {
let total_len = cols * rows;
if total_len != elements.len() {
return Err(Error::DimensionMismatch);
}
Ok(Self {
cols,
rows,
grid: elements.to_vec(),
})
}
/// Creates a new `DynGrid2D` from the given flat slice of `elements`, in *column major order*.
///
/// The number of `elements` must equal `cols`×`rows`.
pub fn from_col_order(elements: &[T], cols: usize, rows: usize) -> Result<Self> {
let total_len = cols * rows;
if total_len != elements.len() {
return Err(Error::DimensionMismatch);
}
let indices_row_order = (0..rows).flat_map(move |row| (0..cols).map(move |col| (col, row)));
let grid = indices_row_order
.map(|(col, row)| {
let index = col * rows + row;
elements[index].clone()
})
.collect();
Ok(DynGrid2D { cols, rows, grid })
}
/// Creates a new `DynGrid2D` with the specified number of `rows` and `col`umn`s`,
/// filling each element with the result of calling the given function.
///
/// The `function` is called once for every location going in *row major order*.
pub fn from_fn_row_order<F: FnMut() -> T>(mut function: F, cols: usize, rows: usize) -> Self {
let len = cols * rows;
let grid = (0..len).map(|_| function()).collect();
DynGrid2D { cols, rows, grid }
}
/// Creates a new `DynGrid2D` with the specified number of `rows` and `col`umn`s`,
/// filling each element with the result of calling the given function.
///
/// The `function` is called once for every location going in *column major order*.
pub fn from_fn_col_order<F: FnMut() -> T>(mut function: F, cols: usize, rows: usize) -> Self {
let len = cols * rows;
let grid_col_order = (0..len).map(|_| function()).collect::<Vec<_>>();
DynGrid2D::from_col_order(&grid_col_order, rows, cols)
.expect("from_fn_col_order should never fail")
}
/// Creates a new `DynGrid2D` with the specified number of `rows` and `col`umn`s`,
/// filling each element with the elements produced by the provided `iterator`.
///
/// The elements are inserted into the grid in *row major order*.
pub fn from_iter_row_order<I>(iterator: I, cols: usize, rows: usize) -> Result<Self>
where
I: Iterator<Item = T>,
{
let len = cols * rows;
let grid = iterator.take(len).collect::<Vec<_>>();
if grid.len() < len {
return Err(Error::NotEnoughElements(len));
}
Ok(DynGrid2D { cols, rows, grid })
}
/// Creates a new `DynGrid2D` with the specified number of `rows` and `col`umn`s`,
/// filling each element with the elements produced by the provided `iterator`.
///
/// The elements are inserted into the grid in *column major order*.
pub fn from_iter_col_order<I>(iterator: I, cols: usize, rows: usize) -> Result<Self>
where
I: Iterator<Item = T>,
{
let total_len = cols * rows;
let grid_col_order = iterator.take(total_len).collect::<Vec<_>>();
DynGrid2D::from_col_order(&grid_col_order, cols, rows)
.map_err(|_| Error::NotEnoughElements(total_len))
}
// Flattens a slice of vecs into a single vec.
#[inline]
fn flatten(nested: &[Vec<T>]) -> Vec<T> {
nested.iter().flat_map(|row| row.clone()).collect()
}
}
/// # constructors (Copy)
impl<T: Copy> DynGrid2D<T> {
/// Creates a new `DynGrid2D` by concatenating the elements inside `chunk`,
/// with a total capacity of `rows`×`cols`×`chunk.len()`.
pub fn from_chunks(chunk: &[T], cols: usize, rows: usize) -> Self {
let grid = chunk.repeat(cols * rows);
DynGrid2D { grid, cols, rows }
}
}
/// # general query methods
impl<T> DynGrid2D<T> {
/// Returns the length of the grid (`rows` × `cols`).
#[inline]
#[allow(clippy::len_without_is_empty)]
pub const fn len(&self) -> usize {
self.cols * self.rows
}
/// Returns the number of rows.
#[inline]
pub const fn num_rows(&self) -> usize {
self.rows
}
/// Returns the number of columns.
#[inline]
pub const fn num_cols(&self) -> usize {
self.cols
}
/// Returns the length of a row (== `num_cols`).
#[inline]
pub const fn row_len(&self) -> usize {
self.cols
}
/// Returns the length of a column (== `num_rows`).
#[inline]
pub const fn col_len(&self) -> usize {
self.rows
}
/// Translates 2D `col`,`row` coordinates into a 1D index.
#[inline]
pub const fn get_index(&self, col: usize, row: usize) -> Result<usize> {
if row < self.rows && col < self.cols {
Ok(self.get_index_unchecked(col, row))
} else {
Err(Error::Indices2dOutOfBounds(col, row))
}
}
/// Translates 2D `col`,`row` coordinates into a 1D index.
///
/// This function doesn't check whether the dimensions are right.
#[inline]
pub const fn get_index_unchecked(&self, col: usize, row: usize) -> usize {
row * self.row_len() + col
}
/// Translates 1D index into 2D `col`,`row` coordinates.
#[inline]
pub const fn get_coords(&self, index: usize) -> Result<(usize, usize)> {
if index < self.len() {
Ok((index / self.cols, index % self.cols))
} else {
Err(Error::IndexOutOfBounds(index))
}
}
/// Translates 1D index into 2D `col`,`row` coordinates.
///
/// # Panics
/// If out of bounds.
#[inline]
pub const fn get_coords_unchecked(&self, index: usize) -> (usize, usize) {
(index / self.cols, index % self.cols)
}
// chunks
/// Returns the number of chunks (`capacity()`/`chunk_len`).
#[inline]
pub const fn chunked_capacity(&self, chunk_len: usize) -> usize {
self.len() / chunk_len
}
/// Returns the number of chunks per row.
#[inline]
pub const fn chunks_per_row(&self, chunk_len: usize) -> usize {
self.row_len() / chunk_len
}
/// Translates 2D `col`,`row` coordinates, with chunk length, into a 1D index.
///
/// - it assumes the `row_len` to be an exact multiple of `chunk_len`.
/// - only full chunks are allowed.
pub const fn get_chunk_index(&self, chunk_len: usize, col: usize, row: usize) -> Result<usize> {
if row < self.rows && col < (self.cols / chunk_len) {
Ok(row * self.row_len() + col * chunk_len)
} else {
Err(Error::Indices2dOutOfBounds(col, row))
}
}
/// Translates 2D `col`,`row` coordinates, with chunk length, into a 1D index.
///
/// # Panics
/// If out of bounds.
pub const fn get_chunk_index_unchecked(
&self,
chunk_len: usize,
row: usize,
col: usize,
) -> usize {
row * self.row_len() + col * chunk_len
}
}
/// # single element get/set methods
impl<T> DynGrid2D<T> {
// get_ref
/// Returns a reference to the element at the given `row` and `col`umn.
#[inline]
pub fn get_ref(&self, col: usize, row: usize) -> Result<&T> {
self.get_index(col, row).map(|idx| &self.grid[idx])
}
/// Returns a reference to the element at the given `row` and `col`umn.
///
/// # Panics
/// If out of bounds.
#[inline]
pub fn get_ref_unchecked(&self, col: usize, row: usize) -> &T {
&self.grid[self.get_index_unchecked(col, row)]
}
/// Returns a reference to the element at the given 1D index, in *row major order*.
#[inline]
pub fn get_ref_row_order(&self, index: usize) -> Result<&T> {
if index < self.len() {
Ok(&self.grid[index])
} else {
Err(Error::IndexOutOfBounds(index))
}
}
/// Returns a reference to the element at the given 1D index, in *row major order*.
///
/// # Panics
/// If out of bounds.
#[inline]
pub fn get_ref_row_order_unchecked(&self, index: usize) -> &T {
&self.grid[index]
}
/// Returns a reference to the element at the given 1D index, in *column major order*.
#[inline]
pub fn get_ref_col_order(&self, index: usize) -> Result<&T> {
if index < self.len() {
Ok(self.get_ref_col_order_unchecked(index))
} else {
Err(Error::IndexOutOfBounds(index))
}
}
/// Returns a reference to the element at the given 1D index, in *column major order*.
///
/// # Panics
/// If out of bounds.
#[inline]
pub fn get_ref_col_order_unchecked(&self, index: usize) -> &T {
let col = index / self.rows;
let row = index % self.rows;
self.get_ref_unchecked(col, row)
}
// get mut
/// Returns a mutable reference to the element at the given `row` and `col`umn.
#[inline]
pub fn get_ref_mut(&mut self, col: usize, row: usize) -> Result<&mut T> {
self.get_index(col, row).map(|idx| &mut self.grid[idx])
}
/// Returns a mutable reference to the element at the given `row` and `col`umn.
///
/// # Panics
/// If out of bounds.
#[inline]
pub fn get_ref_mut_unchecked(&mut self, col: usize, row: usize) -> &mut T {
let idx = self.get_index_unchecked(col, row);
&mut self.grid[idx]
}
/// Returns a mutable reference to the element at the given 1D index, in *row major order*.
#[inline]
pub fn get_ref_mut_row_order(&mut self, index: usize) -> Result<&mut T> {
if index < self.len() {
Ok(&mut self.grid[index])
} else {
Err(Error::IndexOutOfBounds(index))
}
}
/// Returns a mutable reference to the element at the given 1D index, in *row major order*.
///
/// # Panics
/// If out of bounds.
#[inline]
pub fn get_ref_mut_row_order_unchecked(&mut self, index: usize) -> &mut T {
&mut self.grid[index]
}
/// Returns a mutable reference to the element at the given 1D index, in *column major order*.
#[inline]
pub fn get_ref_mut_col_order(&mut self, index: usize) -> Result<&mut T> {
if index < self.len() {
Ok(self.get_ref_mut_col_order_unchecked(index))
} else {
Err(Error::IndexOutOfBounds(index))
}
}
/// Returns a mutable reference to the element at the given 1D index, in *column major order*.
///
/// # Panics
/// If out of bounds.
#[inline]
pub fn get_ref_mut_col_order_unchecked(&mut self, index: usize) -> &mut T {
let col = index / self.rows;
let row = index % self.rows;
self.get_ref_mut_unchecked(col, row)
}
// set
/// Sets the `element` at the given `row` and `col`umn.
#[inline]
pub fn set(&mut self, element: T, col: usize, row: usize) -> Result<()> {
self.get_ref_mut(col, row).map(|index| {
*index = element;
})
}
/// Sets the `element` at the given `row` and `col`umn.
///
/// # Panics
/// If out of bounds.
#[inline]
pub fn set_unchecked(&mut self, element: T, col: usize, row: usize) {
let index = self.get_ref_mut_unchecked(col, row);
*index = element;
}
/// Sets the `element` at the given 1D index, in *row major order*.
#[inline]
pub fn set_row_order(&mut self, element: T, index: usize) -> Result<()> {
self.get_ref_mut_row_order(index).map(|index| {
*index = element;
})
}
/// Sets the element at the given 1D index, in *row major order*.
///
/// # Panics
/// If out of bounds.
#[inline]
pub fn set_row_order_unchecked(&mut self, element: T, index: usize) {
*self.get_ref_mut_row_order_unchecked(index) = element;
}
/// Sets the element at the given 1D index, in *column major order*.
#[inline]
pub fn set_col_order(&mut self, element: T, index: usize) -> Result<()> {
self.get_ref_mut_col_order(index).map(|index| {
*index = element;
})
}
/// Returns a mutable reference to the element at the given 1D index, in *column major order*.
///
/// # Panics
/// If out of bounds.
#[inline]
pub fn set_col_order_unchecked(&mut self, element: T, index: usize) {
*self.get_ref_mut_col_order_unchecked(index) = element;
}
}
/// # single element get methods (Copy)
impl<T: Copy> DynGrid2D<T> {
// get
/// Returns the element at the given `row` and `col`umn.
#[inline]
pub fn get(&self, col: usize, row: usize) -> Result<T> {
self.get_index(col, row).map(|idx| self.grid[idx])
}
/// Returns the element at the given `row` and `col`umn.
///
/// # Panics
/// If out of bounds.
#[inline]
pub fn get_unchecked(&self, col: usize, row: usize) -> T {
self.grid[self.get_index_unchecked(col, row)]
}
/// Returns the element at the given 1D index, in *row major order*.
#[inline]
pub fn get_row_order(&self, index: usize) -> Result<T> {
if index < self.len() {
Ok(self.grid[index])
} else {
Err(Error::IndexOutOfBounds(index))
}
}
/// Returns the element at the given 1D index, in *row major order*.
///
/// # Panics
/// If out of bounds.
#[inline]
pub fn get_row_order_unchecked(&self, index: usize) -> T {
self.grid[index]
}
/// Returns the element at the given 1D index, in *column major order*.
#[inline]
pub fn get_col_order(&self, index: usize) -> Result<T> {
if index < self.len() {
Ok(self.get_col_order_unchecked(index))
} else {
Err(Error::IndexOutOfBounds(index))
}
}
/// Returns the element at the given 1D index, in *column major order*.
///
/// # Panics
/// If out of bounds.
#[inline]
pub fn get_col_order_unchecked(&self, index: usize) -> T {
let col = index / self.rows;
let row = index % self.rows;
self.get_unchecked(col, row)
}
}
/// # iterators
impl<T> DynGrid2D<T> {
// all elements iter
/// Returns an iterator over references to all elements in *row major order*.
#[inline]
pub fn iter_ref(&self) -> impl DoubleEndedIterator<Item = &T> {
self.grid.iter()
}
/// Returns an iterator over mutable references to all elements in *row major order*.
#[inline]
pub fn iter_ref_mut(&mut self) -> impl DoubleEndedIterator<Item = &mut T> {
self.grid.iter_mut()
}
/// Returns an iterator over references to all elements in *col major order*.
#[inline]
pub fn iter_ref_col_order(&self) -> impl DoubleEndedIterator<Item = &T> {
(0..self.cols).flat_map(move |col| (0..self.rows).map(move |row| &self[(col, row)]))
}
// // TODO FIXME
// /// Returns an iterator over mutable references to all elements in *col major order*.
// pub fn iter_mut_col_order(&mut self) -> impl Iterator<Item = &mut T> {
// }
// row iter
/// Returns an iterator over references to all elements in the given row.
#[inline]
pub fn row_iter_ref(&self, row: usize) -> Result<impl DoubleEndedIterator<Item = &T>> {
let start = self.get_index(0, row)?;
let end = start + self.row_len();
Ok(self.grid[start..end].iter())
}
/// Returns an iterator over references to all elements in the given row.
///
/// # Panics
/// If out of bounds.
#[inline]
pub fn row_iter_ref_unchecked(&self, row: usize) -> impl DoubleEndedIterator<Item = &T> {
let start = self.get_index_unchecked(0, row);
let end = start + self.row_len();
self.grid[start..end].iter()
}
/// Returns an iterator over mutable references to all elements in the given row.
#[inline]
pub fn row_iter_ref_mut(
&mut self,
row: usize,
) -> Result<impl DoubleEndedIterator<Item = &mut T>> {
let start = self.get_index(0, row)?;
let end = start + self.row_len();
Ok(self.grid[start..end].iter_mut())
}
/// Returns an iterator over mutable references to all elements in the given row.
///
/// # Panics
/// If out of bounds.
#[inline]
pub fn row_iter_ref_mut_unchecked(
&mut self,
row: usize,
) -> impl DoubleEndedIterator<Item = &mut T> {
let start = self.get_index_unchecked(0, row);
let end = start + self.row_len();
self.grid[start..end].iter_mut()
}
// column iter
/// Returns an iterator over references to all elements in the given `col`umn.
#[inline]
pub fn col_iter_ref(&self, col: usize) -> Result<impl DoubleEndedIterator<Item = &T>> {
if col >= self.cols {
return Err(Error::Indices2dOutOfBounds(0, col));
}
Ok((0..self.col_len()).map(move |row| &self[(col, row)]))
}
/// Returns an iterator over references to all elements in the given `col`umn.
///
/// # Panics
/// If out of bounds.
#[inline]
pub fn col_iter_ref_unchecked(&self, col: usize) -> impl DoubleEndedIterator<Item = &T> {
(0..self.col_len()).map(move |row| &self[(col, row)])
}
/// Returns an iterator over references to all elements in the given `col`umn.
// IMPROVE: DoubleEndedIterator?
#[inline]
pub fn col_iter_ref_mut(&mut self, col: usize) -> Result<impl Iterator<Item = &mut T>> {
if col >= self.cols {
return Err(Error::Indices2dOutOfBounds(0, col));
}
let col_len = self.col_len();
Ok(self.iter_ref_mut().skip(col).step_by(col_len))
}
/// Returns an iterator over references to all elements in the given `col`umn.
///
/// # Panics
/// If out of bounds.
// IMPROVE: DoubleEndedIterator?
#[inline]
pub fn col_iter_ref_mut_unchecked(&mut self, col: usize) -> impl Iterator<Item = &mut T> {
let col_len = self.col_len();
self.iter_ref_mut().skip(col).step_by(col_len)
}
// all rows iter
/// Returns an iterator over all rows.
///
/// Each `Item` is itself another `Iterator` over references to the elements in that column.
#[inline]
pub fn rows_iter_ref(
&self,
) -> impl DoubleEndedIterator<Item = impl DoubleEndedIterator<Item = &T>> {
(0..self.rows).map(move |row| self.row_iter_ref(row).expect("rows_iter should never fail"))
}
// TODO MAYBE FIX cannot infer an appropriate lifetime for autoref due to conflicting requirements
//
// /// Returns an iterator over all rows.
// ///
// /// Each `Item` is itself another `Iterator` over mutable references to the elements in that column.
// #[inline]
// pub fn rows_iter_ref_mut(
// &mut self,
// ) -> impl DoubleEndedIterator<Item = impl DoubleEndedIterator<Item = &mut T>> {
// (0..self.rows).map(move |row| self.row_iter_ref_mut(row).expect("rows_iter should never fail"))
// }
// all columns iter
/// Returns an iterator over all columns.
///
/// Each `Item` is itself another `Iterator` over references to the elements in that column.
#[inline]
pub fn cols_iter_ref(
&self,
) -> impl DoubleEndedIterator<Item = impl DoubleEndedIterator<Item = &T>> {
(0..self.cols).map(move |col| self.col_iter_ref(col).expect("cols_iter should never fail"))
}
// TODO MAYBE
// pub fn cols_iter_ref_mut(&mut self) {}
// chunks iter
/// Returns an iterator over `chunk_len` references to elements in *row major order*.
///
/// # Panics
/// If `chunk_size` is 0.
#[inline]
pub fn chunks_iter_ref(&self, chunk_size: usize) -> impl DoubleEndedIterator<Item = &[T]> {
self.grid.chunks(chunk_size)
}
/// Returns an iterator over `chunk_len` mutable references to elements in *row major order*.
/// # Panics
/// If `chunk_size` is 0.
#[inline]
pub fn chunks_iter_ref_mut(
&mut self,
chunk_size: usize,
) -> impl DoubleEndedIterator<Item = &mut [T]> {
self.grid.chunks_mut(chunk_size)
}
}
/// # iterators (Copy)
impl<T: Copy> DynGrid2D<T> {
// all elements iter
/// Returns an iterator over copies of all elements in *row major order*.
#[inline]
pub fn iter(&self) -> impl DoubleEndedIterator<Item = T> + '_ {
self.grid.iter().copied()
}
/// Returns an iterator over references to all elements in *col major order*.
#[inline]
pub fn iter_col_order(&self) -> impl DoubleEndedIterator<Item = T> + '_ {
(0..self.cols).flat_map(move |col| (0..self.rows).map(move |row| self[(col, row)]))
}
// row iter
/// Returns an iterator over references to all elements in the given row.
#[inline]
pub fn row_iter(&self, row: usize) -> Result<impl DoubleEndedIterator<Item = T> + '_> {
let start = self.get_index(0, row)?;
let end = start + self.row_len();
Ok(self.grid[start..end].iter().copied())
}
/// Returns an iterator over references to all elements in the given row.
///
/// # Panics
/// If out of bounds.
#[inline]
pub fn row_iter_unchecked(&self, row: usize) -> impl DoubleEndedIterator<Item = T> + '_ {
let start = self.get_index_unchecked(0, row);
let end = start + self.row_len();
self.grid[start..end].iter().copied()
}
/// Returns an iterator over references to all elements in the given row,
/// starting on the given `col`umn, for `len` elements.
//
// TODO FIXME: do not go over the row len limit
#[inline]
pub fn row_iter_bounded(
&self,
row: usize,
col: usize,
len: usize,
) -> Result<impl DoubleEndedIterator<Item = T> + '_> {
#[cfg(feature = "std")] // DEBUG
{
println!("\nrow_iter_bounded → row:{row}, col:{col}, len:{len}");
println!(
"grid row_len:{}, col_len:{} len:{}",
self.row_len(),
self.col_len(),
self.len()
);
}
let start = self.get_index(col, row)?;
#[cfg(feature = "std")] // DEBUG
println!("start: ({row},{col}) = index {start}");
let end = min(start + len, start + self.row_len());
#[cfg(feature = "std")] // DEBUG
println!("end = min({0}+{1}, {0}+{2})", start, len, self.row_len());
let _len2 = min(len, self.row_len().saturating_sub(col));
#[cfg(feature = "std")] // DEBUG
{
println!("len1:{len} vs len2:{_len2}");
println!();
}
// THINK:
// EXAMPLE for row_len:4
// len
// let end = min(start + len, start + self.row_len().saturating_sub(len));
// println!("end = min({0}+{1}, {0}+{2})", start, len, self.row_len().saturating_sub(len));
// let len = min(self.row_len().saturating_sub(len)); // TODO: row_len - len
// let num_cells = min(cells.len(), self.row_len().saturating_sub(col as usize)); // TEMP
Ok(self.grid[start..end].iter().copied())
}
// TODO: row_iter_bounded_unchecked
// column iter
/// Returns an iterator over copies of all elements in the given `col`umn.
#[inline]
pub fn col_iter(&self, col: usize) -> Result<impl DoubleEndedIterator<Item = T> + '_> {
if col >= self.cols {
return Err(Error::Indices2dOutOfBounds(0, col));
}
Ok((0..self.col_len()).map(move |row| self[(col, row)]))
}
/// Returns an iterator over copies of all elements in the given `col`umn.
///
/// # Panics
/// If out of bounds.
#[inline]
pub fn col_iter_unchecked(&self, col: usize) -> impl DoubleEndedIterator<Item = T> + '_ {
(0..self.col_len()).map(move |row| self[(col, row)])
}
/// Returns an iterator over all rows.
///
/// Each `Item` is itself another `Iterator` over copies of the elements in that column.
#[inline]
pub fn rows_iter(
&self,
) -> impl DoubleEndedIterator<Item = impl DoubleEndedIterator<Item = T> + '_> {
(0..self.rows).map(move |row| self.row_iter(row).expect("rows_iter should never fail"))
}
/// Returns an iterator over all columns.
///
/// Each `Item` is itself another `Iterator` over copies of the elements in that column.
#[inline]
pub fn cols_iter(
&self,
) -> impl DoubleEndedIterator<Item = impl DoubleEndedIterator<Item = T> + '_> {
(0..self.cols).map(move |col| self.col_iter(col).expect("cols_iter should never fail"))
}
}
/// # collecting to Vec
impl<T: Clone> DynGrid2D<T> {
/// Collects the `DynGrid2D` into a `Vec` of rows.
#[inline]
pub fn as_rows(&self) -> Vec<Vec<T>> {
self.rows_iter_ref()
.map(|row_iter| row_iter.cloned().collect())
.collect()
}
/// Collects the `DynGrid2D` into a `Vec` of columns.
#[inline]
pub fn as_cols(&self) -> Vec<Vec<T>> {
self.cols_iter_ref()
.map(|col_iter| col_iter.cloned().collect())
.collect()
}
/// Collects the `DynGrid2D` into a `Vec` of elements in *row major order*.
#[inline]
pub fn as_row_order(&self) -> Vec<T> {
self.iter_ref().cloned().collect()
}
/// Collects the `DynGrid2D` into a `Vec` of elements in *column major order*.
#[inline]
pub fn as_col_order(&self) -> Vec<T> {
self.iter_ref_col_order().cloned().collect()
}
}
/// # exposing the inner Vec
impl<T> DynGrid2D<T> {
/// Returns the underlying Vec.
#[inline]
pub fn into_vec(self) -> Vec<T> {
self.grid
}
/// Returns a shared reference to the underlying Vec.
#[inline]
pub fn ref_vec(&self) -> &Vec<T> {
&self.grid
}
/// Returns an exclusive reference to the underlying Vec.
#[inline]
pub fn mut_vec(&mut self) -> &mut Vec<T> {
&mut self.grid
}
}
/// # slices
impl<T> DynGrid2D<T> {
/// Returns a slice of the grid.
#[inline]
pub fn as_slice(&self) -> &[T] {
self.grid.as_slice()
}
/// Returns a mutable slice of the grid.
#[inline]
pub fn as_mut_slice(&mut self) -> &mut [T] {
self.grid.as_mut_slice()
}
/// Returns a slice of requested `row`.
#[inline]
pub fn row_slice(&self, row: usize) -> Result<&[T]> {
let start = self.get_index(0, row)?;
let end = start + self.row_len();
Ok(&self.grid[start..end])
}
/// Returns a slice of requested `row`.
///
/// # Panics
/// If out of bounds.
#[inline]
pub fn row_slice_unchecked(&self, row: usize) -> &[T] {
let start = self.get_index_unchecked(0, row);
let end = start + self.row_len();
&self.grid[start..end]
}
/// Returns a mutable slice of requested `row`.
#[inline]
pub fn row_mut_slice(&mut self, row: usize) -> Result<&mut [T]> {
let start = self.get_index(0, row)?;
let end = start + self.row_len();
Ok(&mut self.grid[start..end])
}
/// Returns a mutable slice of requested `row`.
///
/// # Panics
/// If out of bounds.
#[inline]
pub fn row_mut_slice_unchecked(&mut self, row: usize) -> &mut [T] {
let start = self.get_index_unchecked(0, row);
let end = start + self.row_len();
&mut self.grid[start..end]
}
}
/// # get chunks
impl<T> DynGrid2D<T> {
/// Returns a slice of the chunk of elements at the given `row` and `col`umn.
#[inline]
pub fn get_chunk(&self, chunk_len: usize, col: usize, row: usize) -> Result<&[T]> {
self.get_chunk_index(chunk_len, col, row)
.map(|index| &self.grid[index..index + chunk_len])
}
/// Returns a slice of the chunk of elements at the given `row` and `col`umn.
///
/// # Panics
/// If out of bounds.
#[inline]
pub fn get_chunk_unchecked(&self, chunk_len: usize, col: usize, row: usize) -> &[T] {
let index = self.get_chunk_index_unchecked(chunk_len, col, row);
&self.grid[index..index + chunk_len]
}
/// Returns a mutable slice of the chunk of elements at the given `row` and `col`umn.
#[inline]
pub fn get_chunk_mut(&mut self, chunk_len: usize, col: usize, row: usize) -> Result<&mut [T]> {
self.get_chunk_index(chunk_len, col, row)
.map(move |index| &mut self.grid[index..index + chunk_len])
}
/// Returns a mutable slice of the chunk of elements at the given `row` and `col`umn.
///
/// # Panics
/// If out of bounds.
#[inline]
pub fn get_chunk_mut_unchecked(
&mut self,
chunk_len: usize,
row: usize,
col: usize,
) -> &mut [T] {
let index = self.get_chunk_index_unchecked(chunk_len, col, row);
&mut self.grid[index..index + chunk_len]
}
}
/// # set chunks
impl<T: Clone> DynGrid2D<T> {
/// Sets the elements on a chunk.
#[inline]
pub fn set_chunk(
&mut self,
chunk_len: usize,
row: usize,
col: usize,
elements: &[T],
) -> Result<()> {
let chunk = self.get_chunk_mut(chunk_len, col, row)?;
for (n, element) in chunk.iter_mut().enumerate() {
*element = elements[n].clone();
}
Ok(())
}
/// Sets the elements on a chunk.
///
/// # Panics
/// If out of bounds.
#[inline]
pub fn set_chunk_unchecked(
&mut self,
chunk_len: usize,
row: usize,
col: usize,
elements: &[T],
) {
let chunk = self.get_chunk_mut_unchecked(chunk_len, col, row);
for (n, element) in chunk.iter_mut().enumerate() {
*element = elements[n].clone();
}
}
}
mod core_impls {
use super::{DynGrid2D, Index, IndexMut};
use core::any::type_name;
use core::fmt;
// T:Clone
impl<T: Clone> Clone for DynGrid2D<T> {
fn clone(&self) -> Self {
Self {
cols: self.cols,
rows: self.rows,
grid: self.grid.clone(),
}
}
}
// T:PartialEq
impl<T: PartialEq> PartialEq for DynGrid2D<T> {
fn eq(&self, other: &Self) -> bool {
self.grid == other.grid && self.cols == other.cols && self.rows == other.rows
}
}
// T:Eq
impl<T: Eq> Eq for DynGrid2D<T> {}
//
impl<T> fmt::Debug for DynGrid2D<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"DynGrid2D {{ {}×{}, {} }}",
self.rows,
self.cols,
type_name::<T>()
)
}
}
impl<T> Index<(usize, usize)> for DynGrid2D<T> {
type Output = T;
fn index(&self, (col, row): (usize, usize)) -> &Self::Output {
self.get_ref(col, row)
.unwrap_or_else(|_| panic!("Index indices {}, {} out of bounds", col, row))
}
}
impl<T> IndexMut<(usize, usize)> for DynGrid2D<T> {
fn index_mut(&mut self, (col, row): (usize, usize)) -> &mut Self::Output {
self.get_ref_mut(col, row)
.unwrap_or_else(|_| panic!("Index mut indices {}, {} out of bounds", col, row))
}
}
}