1use std::{
7 collections::HashSet,
8 ops::{Add, Bound, RangeBounds, RangeFull, Sub},
9};
10
11use papergrid::records::Records;
12pub use papergrid::{Entity, EntityIterator};
13
14use crate::Table;
15
16pub trait Object: Sized {
20 type Iter: Iterator<Item = Entity>;
22
23 fn cells<R>(&self, table: &Table<R>) -> Self::Iter
25 where
26 R: Records;
27
28 fn and<O>(self, rhs: O) -> UnionCombination<Self, O>
31 where
32 O: Object,
33 {
34 UnionCombination { lhs: self, rhs }
35 }
36
37 fn not<O>(self, rhs: O) -> DiffCombination<Self, O>
39 where
40 O: Object,
41 {
42 DiffCombination { lhs: self, rhs }
43 }
44
45 fn intersect<O>(self, rhs: O) -> IntersectionCombination<Self, O>
47 where
48 O: Object,
49 {
50 IntersectionCombination { lhs: self, rhs }
51 }
52
53 fn inverse(self) -> InversionCombination<Self> {
55 InversionCombination { obj: self }
56 }
57}
58
59#[derive(Debug)]
65pub struct UnionCombination<L, R> {
66 lhs: L,
67 rhs: R,
68}
69
70impl<L, R> Object for UnionCombination<L, R>
71where
72 L: Object,
73 R: Object,
74{
75 type Iter = UnionIter<L::Iter, R::Iter>;
76
77 fn cells<T>(&self, table: &Table<T>) -> Self::Iter
78 where
79 T: Records,
80 {
81 let lhs = self.lhs.cells(table);
82 let rhs = self.rhs.cells(table);
83
84 UnionIter::new(lhs, rhs, table.count_rows(), table.count_columns())
85 }
86}
87
88#[derive(Debug)]
92pub struct DiffCombination<L, R> {
93 lhs: L,
94 rhs: R,
95}
96
97impl<L, R> Object for DiffCombination<L, R>
98where
99 L: Object,
100 R: Object,
101{
102 type Iter = DiffIter<L::Iter>;
103
104 fn cells<T>(&self, table: &Table<T>) -> Self::Iter
105 where
106 T: Records,
107 {
108 let lhs = self.lhs.cells(table);
109 let rhs = self.rhs.cells(table);
110
111 DiffIter::new(lhs, rhs, table.count_rows(), table.count_columns())
112 }
113}
114
115#[derive(Debug)]
120pub struct IntersectionCombination<L, R> {
121 lhs: L,
122 rhs: R,
123}
124
125impl<L, R> Object for IntersectionCombination<L, R>
126where
127 L: Object,
128 R: Object,
129{
130 type Iter = IntersectIter<L::Iter>;
131 fn cells<T>(&self, table: &Table<T>) -> Self::Iter
132 where
133 T: Records,
134 {
135 let lhs = self.lhs.cells(table);
136 let rhs = self.rhs.cells(table);
137
138 IntersectIter::new(lhs, rhs, table.count_rows(), table.count_columns())
139 }
140}
141
142#[derive(Debug)]
147pub struct InversionCombination<O> {
148 obj: O,
149}
150
151impl<O> Object for InversionCombination<O>
152where
153 O: Object,
154{
155 type Iter = InversionIter;
156
157 fn cells<T>(&self, table: &Table<T>) -> Self::Iter
158 where
159 T: Records,
160 {
161 let obj = self.obj.cells(table);
162
163 InversionIter::new(obj, table.count_rows(), table.count_columns())
164 }
165}
166
167#[derive(Debug)]
171pub struct Segment<C, R> {
172 columns: C,
173 rows: R,
174}
175
176impl Segment<RangeFull, RangeFull> {
177 pub fn all() -> SegmentAll {
179 SegmentAll
180 }
181}
182
183impl<C, R> Segment<C, R>
184where
185 C: RangeBounds<usize>,
186 R: RangeBounds<usize>,
187{
188 pub fn new(rows: R, columns: C) -> Self {
190 Self { columns, rows }
191 }
192}
193
194impl<C, R> Object for Segment<C, R>
195where
196 C: RangeBounds<usize>,
197 R: RangeBounds<usize>,
198{
199 type Iter = SectorIter;
200
201 fn cells<T>(&self, table: &Table<T>) -> Self::Iter
202 where
203 T: Records,
204 {
205 let (rows_start, rows_end) = bounds_to_usize(
206 self.rows.start_bound(),
207 self.rows.end_bound(),
208 table.count_rows(),
209 );
210
211 let (cols_start, cols_end) = bounds_to_usize(
212 self.columns.start_bound(),
213 self.columns.end_bound(),
214 table.count_columns(),
215 );
216
217 SectorIter::new(rows_start, rows_end, cols_start, cols_end)
218 }
219}
220
221#[derive(Debug)]
225pub struct SegmentAll;
226
227impl Object for SegmentAll {
228 type Iter = EntityOnce;
229 fn cells<T>(&self, _: &Table<T>) -> Self::Iter
230 where
231 T: Records,
232 {
233 EntityOnce::new(Some(Entity::Global))
234 }
235}
236
237#[derive(Debug)]
240pub struct Frame;
241
242impl Object for Frame {
243 type Iter = FrameIter;
244
245 fn cells<T>(&self, table: &Table<T>) -> Self::Iter
246 where
247 T: Records,
248 {
249 FrameIter::new(table.count_rows(), table.count_columns())
250 }
251}
252
253#[derive(Debug)]
258pub struct FirstRow;
259
260impl Object for FirstRow {
261 type Iter = EntityOnce;
262
263 fn cells<T>(&self, table: &Table<T>) -> Self::Iter
264 where
265 T: Records,
266 {
267 if table.is_empty() {
268 return EntityOnce::new(None);
269 }
270
271 EntityOnce::new(Some(Entity::Row(0)))
272 }
273}
274
275impl Add<usize> for FirstRow {
276 type Output = Row;
277
278 fn add(self, rhs: usize) -> Self::Output {
279 Row { index: rhs }
280 }
281}
282
283#[derive(Debug)]
287pub struct LastRow;
288
289impl Object for LastRow {
290 type Iter = EntityOnce;
291
292 fn cells<T>(&self, table: &Table<T>) -> Self::Iter
293 where
294 T: Records,
295 {
296 if table.is_empty() {
297 return EntityOnce::new(None);
298 }
299
300 let count_rows = table.count_rows();
301 let row = if count_rows == 0 { 0 } else { count_rows - 1 };
302 EntityOnce::new(Some(Entity::Row(row)))
303 }
304}
305
306impl Sub<usize> for LastRow {
307 type Output = LastRowOffset;
308
309 fn sub(self, rhs: usize) -> Self::Output {
310 LastRowOffset { offset: rhs }
311 }
312}
313
314#[derive(Debug, Clone, Copy)]
316pub struct Row {
317 index: usize,
318}
319
320impl Object for Row {
321 type Iter = EntityOnce;
322
323 fn cells<T>(&self, table: &Table<T>) -> Self::Iter
324 where
325 T: Records,
326 {
327 if table.is_empty() {
328 return EntityOnce::new(None);
329 }
330
331 if self.index >= table.count_rows() {
332 return EntityOnce::new(None);
333 }
334
335 EntityOnce::new(Some(Entity::Row(self.index)))
336 }
337}
338
339impl From<Row> for usize {
340 fn from(val: Row) -> Self {
341 val.index
342 }
343}
344
345#[derive(Debug)]
347pub struct LastRowOffset {
348 offset: usize,
349}
350
351impl Object for LastRowOffset {
352 type Iter = EntityOnce;
353
354 fn cells<T>(&self, table: &Table<T>) -> Self::Iter
355 where
356 T: Records,
357 {
358 if table.is_empty() {
359 return EntityOnce::new(None);
360 }
361
362 let count_rows = table.count_rows();
363 let row = if count_rows == 0 { 0 } else { count_rows - 1 };
364 if self.offset > row {
365 return EntityOnce::new(None);
366 }
367
368 let row = row - self.offset;
369 EntityOnce::new(Some(Entity::Row(row)))
370 }
371}
372
373#[derive(Debug)]
377pub struct Rows<R> {
378 range: R,
379}
380
381impl<R> Rows<R> {
382 pub fn new(range: R) -> Self
386 where
387 R: RangeBounds<usize>,
388 {
389 Self { range }
390 }
391
392 pub(crate) fn get_range(&self) -> &R {
393 &self.range
394 }
395}
396
397impl Rows<()> {
398 pub fn single(index: usize) -> Row {
402 Row { index }
403 }
404
405 pub fn first() -> FirstRow {
409 FirstRow
410 }
411
412 pub fn last() -> LastRow {
416 LastRow
417 }
418}
419
420impl<R> Object for Rows<R>
421where
422 R: RangeBounds<usize>,
423{
424 type Iter = RowsIter;
425
426 fn cells<T>(&self, table: &Table<T>) -> Self::Iter
427 where
428 T: Records,
429 {
430 let (x, y) = bounds_to_usize(
431 self.range.start_bound(),
432 self.range.end_bound(),
433 table.count_rows(),
434 );
435
436 RowsIter::new(x, y)
437 }
438}
439
440#[derive(Debug)]
444pub struct Columns<R> {
445 range: R,
446}
447
448impl<R> Columns<R>
449where
450 R: RangeBounds<usize>,
451{
452 pub fn new(range: R) -> Self {
456 Self { range }
457 }
458
459 pub(crate) fn get_range(&self) -> &R {
460 &self.range
461 }
462}
463
464impl Columns<()> {
465 pub fn single(index: usize) -> Column {
469 Column(index)
470 }
471
472 pub fn first() -> FirstColumn {
476 FirstColumn
477 }
478
479 pub fn last() -> LastColumn {
483 LastColumn
484 }
485}
486
487impl<R> Object for Columns<R>
488where
489 R: RangeBounds<usize>,
490{
491 type Iter = ColumnsIter;
492
493 fn cells<T>(&self, table: &Table<T>) -> Self::Iter
494 where
495 T: Records,
496 {
497 let (x, y) = bounds_to_usize(
498 self.range.start_bound(),
499 self.range.end_bound(),
500 table.count_columns(),
501 );
502 ColumnsIter::new(x, y)
503 }
504}
505
506#[derive(Debug)]
508pub struct FirstColumn;
509
510impl Object for FirstColumn {
511 type Iter = EntityOnce;
512
513 fn cells<T>(&self, table: &Table<T>) -> Self::Iter
514 where
515 T: Records,
516 {
517 if table.is_empty() {
518 return EntityOnce::new(None);
519 }
520
521 EntityOnce::new(Some(Entity::Column(0)))
522 }
523}
524
525impl Add<usize> for FirstColumn {
526 type Output = Column;
527
528 fn add(self, rhs: usize) -> Self::Output {
529 Column(rhs)
530 }
531}
532
533#[derive(Debug)]
535pub struct LastColumn;
536
537impl Object for LastColumn {
538 type Iter = EntityOnce;
539
540 fn cells<T>(&self, table: &Table<T>) -> Self::Iter
541 where
542 T: Records,
543 {
544 if table.is_empty() {
545 return EntityOnce::new(None);
546 }
547
548 let col = table.count_columns().saturating_sub(1);
549 EntityOnce::new(Some(Entity::Column(col)))
550 }
551}
552
553impl Sub<usize> for LastColumn {
554 type Output = LastColumnOffset;
555
556 fn sub(self, rhs: usize) -> Self::Output {
557 LastColumnOffset { offset: rhs }
558 }
559}
560
561#[derive(Debug, Clone, Copy)]
563pub struct Column(usize);
564
565impl Object for Column {
566 type Iter = EntityOnce;
567
568 fn cells<T>(&self, table: &Table<T>) -> Self::Iter
569 where
570 T: Records,
571 {
572 if table.is_empty() {
573 return EntityOnce::new(None);
574 }
575
576 let col = self.0;
577 if col >= table.count_columns() {
578 return EntityOnce::new(None);
579 }
580
581 EntityOnce::new(Some(Entity::Column(col)))
582 }
583}
584
585impl From<usize> for Column {
586 fn from(i: usize) -> Self {
587 Self(i)
588 }
589}
590
591impl From<Column> for usize {
592 fn from(val: Column) -> Self {
593 val.0
594 }
595}
596
597#[derive(Debug)]
599pub struct LastColumnOffset {
600 offset: usize,
601}
602
603impl Object for LastColumnOffset {
604 type Iter = EntityOnce;
605
606 fn cells<T>(&self, table: &Table<T>) -> Self::Iter
607 where
608 T: Records,
609 {
610 if table.is_empty() {
611 return EntityOnce::new(None);
612 }
613
614 let col = table.count_columns().saturating_sub(1);
615 if self.offset > col {
616 return EntityOnce::new(None);
617 }
618
619 let col = col - self.offset;
620 EntityOnce::new(Some(Entity::Column(col)))
621 }
622}
623
624#[derive(Debug)]
628pub struct Cell(pub usize, pub usize);
629
630impl Object for Cell {
631 type Iter = EntityOnce;
632
633 fn cells<T>(&self, _: &Table<T>) -> Self::Iter
634 where
635 T: Records,
636 {
637 EntityOnce::new(Some(Entity::Cell(self.0, self.1)))
638 }
639}
640
641#[derive(Debug)]
645pub struct SectorIter {
646 iter: SectorCellsIter,
647}
648
649impl SectorIter {
650 const fn new(rows_start: usize, rows_end: usize, cols_start: usize, cols_end: usize) -> Self {
651 Self {
652 iter: SectorCellsIter::new(rows_start, rows_end, cols_start, cols_end),
653 }
654 }
655}
656
657impl Iterator for SectorIter {
658 type Item = Entity;
659
660 fn next(&mut self) -> Option<Self::Item> {
661 let (row, col) = self.iter.next()?;
662 Some(Entity::Cell(row, col))
663 }
664}
665
666#[derive(Debug)]
667struct SectorCellsIter {
668 rows_end: usize,
669 cols_start: usize,
670 cols_end: usize,
671 row: usize,
672 col: usize,
673}
674
675impl SectorCellsIter {
676 const fn new(rows_start: usize, rows_end: usize, cols_start: usize, cols_end: usize) -> Self {
677 Self {
678 rows_end,
679 cols_start,
680 cols_end,
681 row: rows_start,
682 col: cols_start,
683 }
684 }
685}
686
687impl Iterator for SectorCellsIter {
688 type Item = (usize, usize);
689
690 fn next(&mut self) -> Option<Self::Item> {
691 if self.row >= self.rows_end {
692 return None;
693 }
694
695 if self.col >= self.cols_end {
696 return None;
697 }
698
699 let row = self.row;
700 let col = self.col;
701
702 self.col += 1;
703
704 if self.col == self.cols_end {
705 self.row += 1;
706 self.col = self.cols_start;
707 }
708
709 Some((row, col))
710 }
711}
712
713#[derive(Debug)]
717pub struct FrameIter {
718 rows: usize,
719 cols: usize,
720 row: usize,
721 col: usize,
722}
723
724impl FrameIter {
725 const fn new(count_rows: usize, count_columns: usize) -> Self {
726 Self {
727 rows: count_rows,
728 cols: count_columns,
729 row: 0,
730 col: 0,
731 }
732 }
733}
734
735impl Iterator for FrameIter {
736 type Item = Entity;
737
738 fn next(&mut self) -> Option<Self::Item> {
739 if self.cols == 0 || self.rows == 0 {
740 return None;
741 }
742
743 if self.row == self.rows {
744 return None;
745 }
746
747 let row = self.row;
748 let col = self.col;
749
750 self.col += 1;
751
752 if self.col == self.cols {
753 self.row += 1;
754 self.col = 0;
755 }
756
757 Some(Entity::Cell(row, col))
758 }
759}
760
761#[derive(Debug)]
765pub struct RowsIter {
766 start: usize,
767 end: usize,
768}
769
770impl RowsIter {
771 const fn new(start: usize, end: usize) -> Self {
772 Self { start, end }
773 }
774}
775
776impl Iterator for RowsIter {
777 type Item = Entity;
778
779 fn next(&mut self) -> Option<Self::Item> {
780 if self.start == self.end {
781 return None;
782 }
783
784 let col = self.start;
785 self.start += 1;
786
787 Some(Entity::Row(col))
788 }
789}
790
791#[derive(Debug)]
795pub struct ColumnsIter {
796 start: usize,
797 end: usize,
798}
799
800impl ColumnsIter {
801 const fn new(start: usize, end: usize) -> Self {
802 Self { start, end }
803 }
804}
805
806impl Iterator for ColumnsIter {
807 type Item = Entity;
808
809 fn next(&mut self) -> Option<Self::Item> {
810 if self.start == self.end {
811 return None;
812 }
813
814 let col = self.start;
815 self.start += 1;
816
817 Some(Entity::Column(col))
818 }
819}
820
821#[derive(Debug)]
823pub struct EntityOnce {
824 entity: Option<Entity>,
825}
826
827impl EntityOnce {
828 const fn new(entity: Option<Entity>) -> Self {
829 Self { entity }
830 }
831}
832
833impl Iterator for EntityOnce {
834 type Item = Entity;
835
836 fn next(&mut self) -> Option<Self::Item> {
837 self.entity.take()
838 }
839}
840
841#[derive(Debug)]
843pub struct UnionIter<L, R> {
844 lhs: Option<L>,
845 rhs: R,
846 seen: HashSet<(usize, usize)>,
847 current: Option<EntityIterator>,
848 count_rows: usize,
849 count_cols: usize,
850}
851
852impl<L, R> UnionIter<L, R>
853where
854 L: Iterator<Item = Entity>,
855 R: Iterator<Item = Entity>,
856{
857 fn new(lhs: L, rhs: R, count_rows: usize, count_cols: usize) -> Self {
858 let size = match lhs.size_hint() {
859 (s1, Some(s2)) if s1 == s2 => s1,
860 _ => 0,
861 };
862
863 Self {
864 lhs: Some(lhs),
865 rhs,
866 seen: HashSet::with_capacity(size),
867 current: None,
868 count_rows,
869 count_cols,
870 }
871 }
872}
873
874impl<L, R> Iterator for UnionIter<L, R>
875where
876 L: Iterator<Item = Entity>,
877 R: Iterator<Item = Entity>,
878{
879 type Item = Entity;
880
881 fn next(&mut self) -> Option<Self::Item> {
882 if let Some(iter) = self.current.as_mut() {
883 for p in iter.by_ref() {
884 if self.lhs.is_none() && self.seen.contains(&p) {
885 continue;
886 }
887
888 self.seen.insert(p);
889 return Some(Entity::Cell(p.0, p.1));
890 }
891 }
892
893 if let Some(lhs) = self.lhs.as_mut() {
894 for entity in lhs.by_ref() {
895 let mut iter = entity.iter(self.count_rows, self.count_cols);
896 if let Some(p) = iter.by_ref().next() {
897 self.seen.insert(p);
898 self.current = Some(iter);
899 return Some(Entity::Cell(p.0, p.1));
900 }
901 }
902
903 self.lhs = None;
904 }
905
906 for entity in self.rhs.by_ref() {
907 let mut iter = entity.iter(self.count_rows, self.count_cols);
908
909 for p in iter.by_ref() {
910 if !self.seen.contains(&p) {
911 self.seen.insert(p);
912 self.current = Some(iter);
913 return Some(Entity::Cell(p.0, p.1));
914 }
915 }
916 }
917
918 None
919 }
920}
921
922#[derive(Debug)]
924pub struct DiffIter<L> {
925 lhs: L,
926 seen: HashSet<(usize, usize)>,
927 count_rows: usize,
928 count_cols: usize,
929 current: Option<EntityIterator>,
930}
931
932impl<L> DiffIter<L>
933where
934 L: Iterator<Item = Entity>,
935{
936 fn new<R>(lhs: L, rhs: R, count_rows: usize, count_cols: usize) -> Self
937 where
938 R: Iterator<Item = Entity>,
939 {
940 let size = match rhs.size_hint() {
941 (s1, Some(s2)) if s1 == s2 => s1,
942 _ => 0,
943 };
944
945 let mut seen = HashSet::with_capacity(size);
946 for entity in rhs {
947 seen.extend(entity.iter(count_rows, count_cols));
948 }
949
950 Self {
951 lhs,
952 seen,
953 count_rows,
954 count_cols,
955 current: None,
956 }
957 }
958}
959
960impl<L> Iterator for DiffIter<L>
961where
962 L: Iterator<Item = Entity>,
963{
964 type Item = Entity;
965
966 fn next(&mut self) -> Option<Self::Item> {
967 if let Some(iter) = self.current.as_mut() {
968 for p in iter.by_ref() {
969 if !self.seen.contains(&p) {
970 return Some(Entity::Cell(p.0, p.1));
971 }
972 }
973 }
974
975 for entity in self.lhs.by_ref() {
976 let mut iter = entity.iter(self.count_rows, self.count_cols);
977
978 for p in iter.by_ref() {
979 if !self.seen.contains(&p) {
980 self.current = Some(iter);
981 return Some(Entity::Cell(p.0, p.1));
982 }
983 }
984 }
985
986 None
987 }
988}
989
990#[derive(Debug)]
992pub struct IntersectIter<L> {
993 lhs: L,
994 seen: HashSet<(usize, usize)>,
995 count_rows: usize,
996 count_cols: usize,
997 current: Option<EntityIterator>,
998}
999
1000impl<L> IntersectIter<L>
1001where
1002 L: Iterator<Item = Entity>,
1003{
1004 fn new<R>(lhs: L, rhs: R, count_rows: usize, count_cols: usize) -> Self
1005 where
1006 R: Iterator<Item = Entity>,
1007 {
1008 let size = match rhs.size_hint() {
1009 (s1, Some(s2)) if s1 == s2 => s1,
1010 _ => 0,
1011 };
1012
1013 let mut seen = HashSet::with_capacity(size);
1014 for entity in rhs {
1015 seen.extend(entity.iter(count_rows, count_cols));
1016 }
1017
1018 Self {
1019 lhs,
1020 seen,
1021 count_rows,
1022 count_cols,
1023 current: None,
1024 }
1025 }
1026}
1027
1028impl<L> Iterator for IntersectIter<L>
1029where
1030 L: Iterator<Item = Entity>,
1031{
1032 type Item = Entity;
1033
1034 fn next(&mut self) -> Option<Self::Item> {
1035 if let Some(iter) = self.current.as_mut() {
1036 for p in iter.by_ref() {
1037 if self.seen.contains(&p) {
1038 return Some(Entity::Cell(p.0, p.1));
1039 }
1040 }
1041 }
1042
1043 for entity in self.lhs.by_ref() {
1044 let mut iter = entity.iter(self.count_rows, self.count_cols);
1045
1046 for p in iter.by_ref() {
1047 if self.seen.contains(&p) {
1048 self.current = Some(iter);
1049 return Some(Entity::Cell(p.0, p.1));
1050 }
1051 }
1052 }
1053
1054 None
1055 }
1056}
1057
1058#[derive(Debug)]
1060pub struct InversionIter {
1061 all: SectorCellsIter,
1062 seen: HashSet<(usize, usize)>,
1063}
1064
1065impl InversionIter {
1066 fn new<O>(obj: O, count_rows: usize, count_columns: usize) -> Self
1067 where
1068 O: Iterator<Item = Entity>,
1069 {
1070 let size = match obj.size_hint() {
1071 (s1, Some(s2)) if s1 == s2 => s1,
1072 _ => 0,
1073 };
1074
1075 let mut seen = HashSet::with_capacity(size);
1076 for entity in obj {
1077 seen.extend(entity.iter(count_rows, count_columns));
1078 }
1079
1080 let all = SectorCellsIter::new(0, count_rows, 0, count_columns);
1081
1082 Self { all, seen }
1083 }
1084}
1085
1086impl Iterator for InversionIter {
1087 type Item = Entity;
1088
1089 fn next(&mut self) -> Option<Self::Item> {
1090 for p in self.all.by_ref() {
1091 if !self.seen.contains(&p) {
1092 return Some(Entity::Cell(p.0, p.1));
1093 }
1094 }
1095
1096 None
1097 }
1098}
1099
1100pub(crate) fn bounds_to_usize(
1102 left: Bound<&usize>,
1103 right: Bound<&usize>,
1104 count_elements: usize,
1105) -> (usize, usize) {
1106 match (left, right) {
1107 (Bound::Included(x), Bound::Included(y)) => (*x, y + 1),
1108 (Bound::Included(x), Bound::Excluded(y)) => (*x, *y),
1109 (Bound::Included(x), Bound::Unbounded) => (*x, count_elements),
1110 (Bound::Unbounded, Bound::Unbounded) => (0, count_elements),
1111 (Bound::Unbounded, Bound::Included(y)) => (0, y + 1),
1112 (Bound::Unbounded, Bound::Excluded(y)) => (0, *y),
1113 (Bound::Excluded(_), Bound::Unbounded)
1114 | (Bound::Excluded(_), Bound::Included(_))
1115 | (Bound::Excluded(_), Bound::Excluded(_)) => {
1116 unreachable!("A start bound can't be excluded")
1117 }
1118 }
1119}
1120
1121#[cfg(test)]
1122mod tests {
1123 use super::*;
1124
1125 #[test]
1126 fn columns_test() {
1127 assert_eq!(
1128 vec_cells(Columns::new(..), 2, 3),
1129 [Entity::Column(0), Entity::Column(1), Entity::Column(2)]
1130 );
1131 assert_eq!(
1132 vec_cells(Columns::new(1..), 2, 3),
1133 [Entity::Column(1), Entity::Column(2)]
1134 );
1135 assert_eq!(vec_cells(Columns::new(2..), 2, 3), [Entity::Column(2)]);
1136 assert_eq!(vec_cells(Columns::new(3..), 2, 3), []);
1137 assert_eq!(vec_cells(Columns::new(0..1), 2, 3), [Entity::Column(0)]);
1138 assert_eq!(vec_cells(Columns::new(1..2), 2, 3), [Entity::Column(1)]);
1139 assert_eq!(vec_cells(Columns::new(2..3), 2, 3), [Entity::Column(2)]);
1140 assert_eq!(vec_cells(Columns::new(..), 0, 0), []);
1141 assert_eq!(vec_cells(Columns::new(..), 2, 0), []);
1142 assert_eq!(vec_cells(Columns::new(..), 0, 3), []);
1143 }
1144
1145 #[test]
1146 fn first_column_test() {
1147 assert_eq!(vec_cells(Columns::first(), 5, 2), [Entity::Column(0)]);
1148 assert_eq!(vec_cells(Columns::first(), 0, 0), []);
1149 assert_eq!(vec_cells(Columns::first(), 10, 0), []);
1150 assert_eq!(vec_cells(Columns::first(), 0, 10), []);
1151 }
1152
1153 #[test]
1154 fn last_column_test() {
1155 assert_eq!(vec_cells(Columns::last(), 5, 2), [Entity::Column(1)]);
1156 assert_eq!(vec_cells(Columns::last(), 5, 29), [Entity::Column(28)]);
1157 assert_eq!(vec_cells(Columns::last(), 0, 0), []);
1158 assert_eq!(vec_cells(Columns::last(), 10, 0), []);
1159 assert_eq!(vec_cells(Columns::last(), 0, 10), []);
1160 }
1161
1162 #[test]
1163 fn last_column_sub_test() {
1164 assert_eq!(vec_cells(Columns::last(), 5, 2), [Entity::Column(1)]);
1165 assert_eq!(vec_cells(Columns::last() - 0, 5, 2), [Entity::Column(1)]);
1166 assert_eq!(vec_cells(Columns::last() - 1, 5, 2), [Entity::Column(0)]);
1167 assert_eq!(vec_cells(Columns::last() - 2, 5, 2), []);
1168 assert_eq!(vec_cells(Columns::last() - 100, 5, 2), []);
1169 }
1170
1171 #[test]
1172 fn first_column_add_test() {
1173 assert_eq!(vec_cells(Columns::first(), 5, 2), [Entity::Column(0)]);
1174 assert_eq!(vec_cells(Columns::first() + 0, 5, 2), [Entity::Column(0)]);
1175 assert_eq!(vec_cells(Columns::first() + 1, 5, 2), [Entity::Column(1)]);
1176 assert_eq!(vec_cells(Columns::first() + 2, 5, 2), []);
1177 assert_eq!(vec_cells(Columns::first() + 100, 5, 2), []);
1178 }
1179
1180 #[test]
1181 fn rows_test() {
1182 assert_eq!(
1183 vec_cells(Rows::new(..), 2, 3),
1184 [Entity::Row(0), Entity::Row(1)]
1185 );
1186 assert_eq!(vec_cells(Rows::new(1..), 2, 3), [Entity::Row(1)]);
1187 assert_eq!(vec_cells(Rows::new(2..), 2, 3), []);
1188 assert_eq!(vec_cells(Rows::new(0..1), 2, 3), [Entity::Row(0)],);
1189 assert_eq!(vec_cells(Rows::new(1..2), 2, 3), [Entity::Row(1)],);
1190 assert_eq!(vec_cells(Rows::new(..), 0, 0), []);
1191 assert_eq!(vec_cells(Rows::new(..), 0, 3), []);
1192 assert_eq!(vec_cells(Rows::new(..), 2, 0), []);
1193 }
1194
1195 #[test]
1196 fn last_row_test() {
1197 assert_eq!(vec_cells(Rows::last(), 5, 2), [Entity::Row(4)]);
1198 assert_eq!(vec_cells(Rows::last(), 100, 2), [Entity::Row(99)]);
1199 assert_eq!(vec_cells(Rows::last(), 0, 0), []);
1200 assert_eq!(vec_cells(Rows::last(), 5, 0), []);
1201 assert_eq!(vec_cells(Rows::last(), 0, 2), []);
1202 }
1203
1204 #[test]
1205 fn first_row_test() {
1206 assert_eq!(vec_cells(Rows::first(), 5, 2), [Entity::Row(0)]);
1207 assert_eq!(vec_cells(Rows::first(), 100, 2), [Entity::Row(0)]);
1208 assert_eq!(vec_cells(Rows::first(), 0, 0), []);
1209 assert_eq!(vec_cells(Rows::first(), 5, 0), []);
1210 assert_eq!(vec_cells(Rows::first(), 0, 2), []);
1211 }
1212
1213 #[test]
1214 fn last_row_sub_test() {
1215 assert_eq!(vec_cells(Rows::last(), 5, 2), [Entity::Row(4)]);
1216 assert_eq!(vec_cells(Rows::last() - 0, 5, 2), [Entity::Row(4)]);
1217 assert_eq!(vec_cells(Rows::last() - 1, 5, 2), [Entity::Row(3)]);
1218 assert_eq!(vec_cells(Rows::last() - 2, 5, 2), [Entity::Row(2)]);
1219 assert_eq!(vec_cells(Rows::last() - 3, 5, 2), [Entity::Row(1)]);
1220 assert_eq!(vec_cells(Rows::last() - 4, 5, 2), [Entity::Row(0)]);
1221 assert_eq!(vec_cells(Rows::last() - 5, 5, 2), []);
1222 assert_eq!(vec_cells(Rows::last() - 100, 5, 2), []);
1223 assert_eq!(vec_cells(Rows::last() - 1, 0, 0), []);
1224 assert_eq!(vec_cells(Rows::last() - 1, 5, 0), []);
1225 assert_eq!(vec_cells(Rows::last() - 1, 0, 2), []);
1226 }
1227
1228 #[test]
1229 fn first_row_add_test() {
1230 assert_eq!(vec_cells(Rows::first(), 5, 2), [Entity::Row(0)]);
1231 assert_eq!(vec_cells(Rows::first() + 0, 5, 2), [Entity::Row(0)]);
1232 assert_eq!(vec_cells(Rows::first() + 1, 5, 2), [Entity::Row(1)]);
1233 assert_eq!(vec_cells(Rows::first() + 2, 5, 2), [Entity::Row(2)]);
1234 assert_eq!(vec_cells(Rows::first() + 3, 5, 2), [Entity::Row(3)]);
1235 assert_eq!(vec_cells(Rows::first() + 4, 5, 2), [Entity::Row(4)]);
1236 assert_eq!(vec_cells(Rows::first() + 5, 5, 2), []);
1237 assert_eq!(vec_cells(Rows::first() + 100, 5, 2), []);
1238 assert_eq!(vec_cells(Rows::first() + 1, 0, 0), []);
1239 assert_eq!(vec_cells(Rows::first() + 1, 5, 0), []);
1240 assert_eq!(vec_cells(Rows::first() + 1, 0, 2), []);
1241 }
1242
1243 #[test]
1244 fn frame_test() {
1245 assert_eq!(
1246 vec_cells(Frame, 2, 3),
1247 [
1248 Entity::Cell(0, 0),
1249 Entity::Cell(0, 1),
1250 Entity::Cell(0, 2),
1251 Entity::Cell(1, 0),
1252 Entity::Cell(1, 1),
1253 Entity::Cell(1, 2)
1254 ]
1255 );
1256 assert_eq!(vec_cells(Frame, 0, 0), []);
1257 assert_eq!(vec_cells(Frame, 2, 0), []);
1258 assert_eq!(vec_cells(Frame, 0, 2), []);
1259 }
1260
1261 #[test]
1262 fn segment_test() {
1263 assert_eq!(
1264 vec_cells(Segment::new(.., ..), 2, 3),
1265 [
1266 Entity::Cell(0, 0),
1267 Entity::Cell(0, 1),
1268 Entity::Cell(0, 2),
1269 Entity::Cell(1, 0),
1270 Entity::Cell(1, 1),
1271 Entity::Cell(1, 2)
1272 ]
1273 );
1274 assert_eq!(
1275 vec_cells(Segment::new(1.., ..), 2, 3),
1276 [Entity::Cell(1, 0), Entity::Cell(1, 1), Entity::Cell(1, 2)]
1277 );
1278 assert_eq!(vec_cells(Segment::new(2.., ..), 2, 3), []);
1279
1280 assert_eq!(
1281 vec_cells(Segment::new(.., 1..), 2, 3),
1282 [
1283 Entity::Cell(0, 1),
1284 Entity::Cell(0, 2),
1285 Entity::Cell(1, 1),
1286 Entity::Cell(1, 2)
1287 ]
1288 );
1289 assert_eq!(
1290 vec_cells(Segment::new(.., 2..), 2, 3),
1291 [Entity::Cell(0, 2), Entity::Cell(1, 2)]
1292 );
1293 assert_eq!(vec_cells(Segment::new(.., 3..), 2, 3), []);
1294
1295 assert_eq!(
1296 vec_cells(Segment::new(1.., 1..), 2, 3),
1297 [Entity::Cell(1, 1), Entity::Cell(1, 2)]
1298 );
1299 assert_eq!(
1300 vec_cells(Segment::new(1..2, 1..2), 2, 3),
1301 [Entity::Cell(1, 1)]
1302 );
1303
1304 assert_eq!(vec_cells(Segment::new(5.., 5..), 2, 3), []);
1305 }
1306
1307 #[test]
1308 fn object_and_test() {
1309 assert_eq!(
1310 vec_cells(Cell(0, 0).and(Cell(0, 0)), 2, 3),
1311 [Entity::Cell(0, 0)]
1312 );
1313 assert_eq!(
1314 vec_cells(Cell(0, 0).and(Cell(1, 2)), 2, 3),
1315 [Entity::Cell(0, 0), Entity::Cell(1, 2)]
1316 );
1317 assert_eq!(vec_cells(Cell(0, 0).and(Cell(1, 2)), 0, 0), []);
1318 }
1319
1320 #[test]
1321 fn object_not_test() {
1322 assert_eq!(vec_cells(Cell(0, 0).not(Cell(0, 0)), 2, 3), []);
1323 assert_eq!(
1324 vec_cells(Rows::first().not(Cell(0, 0)), 2, 3),
1325 [Entity::Cell(0, 1), Entity::Cell(0, 2)]
1326 );
1327 assert_eq!(vec_cells(Rows::first().not(Cell(0, 0)), 0, 0), []);
1328 }
1329
1330 #[test]
1331 fn object_intersect_test() {
1332 assert_eq!(
1333 vec_cells(Segment::all().intersect(Rows::single(1)), 2, 3),
1334 [Entity::Cell(1, 0), Entity::Cell(1, 1), Entity::Cell(1, 2)]
1335 );
1336 assert_eq!(
1337 vec_cells(Cell(0, 0).intersect(Cell(0, 0)), 2, 3),
1338 [Entity::Cell(0, 0)]
1339 );
1340 assert_eq!(
1341 vec_cells(Rows::first().intersect(Cell(0, 0)), 2, 3),
1342 [Entity::Cell(0, 0)]
1343 );
1344 assert_eq!(vec_cells(Rows::first().intersect(Cell(0, 0)), 0, 0), []);
1345 }
1346
1347 #[test]
1348 fn object_inverse_test() {
1349 assert_eq!(vec_cells(Segment::all().inverse(), 2, 3), []);
1350 assert_eq!(
1351 vec_cells(Cell(0, 0).inverse(), 2, 3),
1352 [
1353 Entity::Cell(0, 1),
1354 Entity::Cell(0, 2),
1355 Entity::Cell(1, 0),
1356 Entity::Cell(1, 1),
1357 Entity::Cell(1, 2)
1358 ]
1359 );
1360 assert_eq!(
1361 vec_cells(Rows::first().inverse(), 2, 3),
1362 [Entity::Cell(1, 0), Entity::Cell(1, 1), Entity::Cell(1, 2)]
1363 );
1364 assert_eq!(vec_cells(Rows::first().inverse(), 0, 0), []);
1365 }
1366
1367 fn vec_cells<O: Object>(o: O, count_rows: usize, count_cols: usize) -> Vec<Entity> {
1368 let data = vec![vec![String::default(); count_cols]; count_rows];
1369 let table = crate::builder::Builder::from(data).build();
1370 o.cells(&table).collect::<Vec<_>>()
1371 }
1372}