grid-math 0.2.7

Library, containing basic representation of Grid, Cell, and assosiated mathematical operations. Helpful in CLI-based gamedev.
Documentation
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
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
//! Basic representation of Grid, Cell, and assosiated mathematical operations. Helpful in CLI-based gamedev.
//!
//! This module contains the [`Cell`] type, representing the basic unit of [`Grid`],
//! the [`Grid`] type, representing a two-dimentional field of [`Cell`]s,
//! the [`Cells`] type, representing an iterator over every [`Cell`] on the [`Grid`],
//! the [`Rows`] and the [`Columns`] types, representing iterators over subgrids of [`Grid`],
//! and the [`GridMap<V>`] type, representing a wrapper around the [`HashMap<Cell, V>`]
//!
//! # Usecases
//!
//! One of the best usecases of this crate is for developing `CLI` based games:
//! `Cell` has two fields representing position on the `Grid`, which are both `u8`,
//! and the `Grid` consists of the `start` and the `end` `Cell`s,
//! making the largest possible `Grid` to be 255x255, which is enough for most terminal games.
//!
//! # Note
//!
//! - `Cell`'s global position currently represented in the `u8` for simplicity,
//!   and because this is enough for most terminal games. This may be changed to be a scalar generic in the future.
//! - Error handling is currently rather stupid (just checks with panic!), but this helps to prevent scary logical bugs.
//! - Crate is in the "work in progress" state, so the public API may change in the future. Feel free to contribute!
//!
//! # Examples
//!
//! Perform some basic calculations for `Cell`:
//! ```
//! use grid_math::{Cell, Grid};
//!
//! let grid = Grid::new(10, 10);
//! let start = grid.start();
//! let next = start.saturating_right(grid, 5).wrapping_down(grid, 15).strict_left(grid, 1);
//!
//! assert!(next.within(grid));
//! assert_eq!(next, Cell::new(4, 5));
//! ```
//!
//! Map every `Cell` on the `Grid` to the custom `String` representation:
//! ```
//! use grid_math::{Cell, Grid};
//!
//! let grid = Grid::new(3, 3);
//! let grid_string = grid
//!     .rows()
//!     .map(|row| {
//!         row.cells().map(|_| " [#]")
//!             .chain(std::iter::once("\n\n"))
//!             .collect::<String>()
//!     })
//!     .collect::<String>();
//! assert_eq!(grid_string,
//! " \
//!  [#] [#] [#]
//!
//!  [#] [#] [#]
//!
//!  [#] [#] [#]
//!
//! "
//! );
//! ```
//!
//! Store some actual data on the `Grid`, using `GridMap`:
//! ```
//! use grid_math::{Cell, Grid, GridMap};
//!
//! let grid = Grid::new(5, 5);
//! let mut map: GridMap<char> = GridMap::from(grid);
//! map.insert(map.grid().start(), '#');
//! map.insert(map.grid().end(), '@');
//!
//! assert_eq!(map.len(), 2);
//! assert_eq!(map.get(&Cell::new(0, 0)).unwrap(), &'#');
//! ```

use rand::seq::IteratorRandom;
//use std::cmp::Ordering;
use std::collections::HashMap;
use std::convert::{From, Into};
use std::fmt;
use std::iter::Filter;
use std::ops::{Deref, DerefMut};

/// `Cell` represents the basic unit of `Grid`.
///
/// Consists of global positions `global_width: u8` and `global_depth: u8`, alongside with methods implementing
/// common mathematical operations for safe interactions with grids and other cells
///
/// Due to low memory size, `Cell` implements `Copy` trait, so all methods take `self` (copy) as first argument
///
/// # Examples
///
/// You can create Cell using new(global_width, global_depth):
/// ```
/// use grid_math::Cell;
///
/// let cell = Cell::new(10, 15);
/// ```
///
/// Or use functionality of implemented `From` and `Into` traits:
/// ```
/// use grid_math::Cell;
///
/// let cell = Cell::from((9, 9));
/// let cell: Cell = (6, 7).into();
/// ```
///
/// To read global_width or global_depth values, use getters:
/// ```
/// use grid_math::Cell;
///
/// let cell = Cell::new(10, 10);
/// let w = cell.global_width();
/// let d = cell.global_depth();
/// ```
/// Or use `into()` provided by `Into` trait:
/// ```
/// use grid_math::Cell;
///
/// let cell = Cell::new(10, 10);
/// let (w, d): (u8, u8) = cell.into();
/// ```
///
/// 'Cell' implements `Display` and `Debug` trait, so you can easily print it out:
/// ```
/// use grid_math::Cell;
///
/// let cell = Cell::new(10, 10);
/// println!("Cell: {cell}"); // Cell: (10, 10)
/// assert_eq!(format!("{cell}"), "(10, 10)");
/// ```
///
/// Other methods involve interactions with `Grid`
///
/// `Cell` is designed to not mutate it's contents.
/// Instead, all operations return new instances of `Cell`
///
/// Also worth noting, that all operations on `Cell` are verified to be logically correct,
/// otherwise logically incorrect operations will be met with panic!
///
/// Here is a brief overview of `Cell` and `Grid` interactions:
///
/// Check if `Cell` is within the `Grid`:
/// ```
/// use grid_math::{Cell, Grid};
///
/// let cell = Cell::new(3, 4);
/// let grid = Grid::new(10, 10); // 10x10 grid starting at (0,0)
/// assert!(cell.within(grid));
/// ```
///
/// Get relative to the `Grid` position of `Cell`:
/// (`Grid` can start not only from (0,0))
/// ```
/// use grid_math::{Cell, Grid};
///
/// let cell = Cell::new(3, 4);
/// let grid = Grid::indented(8, 8, (2, 1)); // 8x8 grid starting at (2,1)
/// let (width, depth) = (cell.width(grid), cell.depth(grid));
/// // cell's width on grid = cell.global_width - grid.start.global_width
/// // cell's depth on grid = cell.global_depth - grid.start.global_depth
/// assert_eq!((width, depth), (1, 3));
/// // get gaps between width and depth grid borders and cell:
/// let (width_gap, depth_gap) = (cell.width_gap(grid), cell.depth_gap(grid));
/// assert_eq!((width_gap, depth_gap), (6, 4));
/// // get member of grid by relative position:
/// let member = grid.member(width, depth);
/// assert_eq!(cell, member);
/// ```
///
/// Perform some move calculations of `Cell` on `Grid`:
/// ```
/// use grid_math::{Cell, Grid};
///
/// let grid = Grid::new(10, 10);
/// let cell = grid.start(); // get grid's first cell
/// let next = cell.strict_right(grid, 3); // move to the right by 3, panics if grid bounds overflow occures
/// assert_eq!(next, Cell::new(3, 0));
/// let next = cell.saturating_down(grid, 15); // move down by 15, returns grid bound if overflow occures
/// assert_eq!(next, Cell::new(0, 9));
/// let next = cell.wrapping_right(grid, 5).strict_left(grid, 2).project_down(grid); // chain of movements
/// assert_eq!(next, Cell::new(3, 9));
/// ```
///
/// To get more examples, look at `Cell` and `Grid` methods documentation.
///
///
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Cell {
    global_width: u8,
    global_depth: u8,
}

/// `Grid` represents the field of `Cell`
///
/// Consists of `start: Cell` and `end: Cell` fields, alongside with methods implementing
/// common mathematical operations for safe interactions with cells and other grids
///
/// `Grid` has two axis: width, and depth:
///
/// (0,0) [#] [#] [#] [#] (5,0)
///
///  [#]  [#] [#] [#] [#]  [#]
///
///  [#]  [#] [#] [#] [#]  [#]
///
///  [#]  [#] [#] [#] [#]  [#]
///
///  [#]  [#] [#] [#] [#]  [#]
///
/// (0,5) [#] [#] [#] [#] (5,5)
///
/// Due to low memory size, `Grid` implements `Copy` trait, so all methods take `self` (copy) as first argument
///
/// # Examples
///
/// You can create Grid using new(width, depth) or indented(width, depth, indent):
/// ```
/// use grid_math::Grid;
///
/// let grid = Grid::new(5, 5); // new 5x5 grid, starting at (0,0)
/// let grid = Grid::indented(5, 5, (1, 2)); // new 5x5 grid, starting at (1,2)
/// ```
///
/// Or use functionality of implemented `From` and `Into` traits:
/// ```
/// use grid_math::{Grid, Cell};
///
/// let grid = Grid::from(((1, 2), (5, 6))); // new field where `start` is (1,2), `end` is (5,6)
/// let grid: Grid = ((1, 2), (5, 6)).into();
/// //same for (cell, cell):
/// let grid = Grid::from((Cell::new(1, 2), Cell::new(5, 6)));
/// let grid: Grid = (Cell::new(1, 2), Cell::new(5, 6)).into();
/// // backwards:
/// let (start, end) = grid.into();
/// assert_eq!((start, end), (Cell::new(1, 2), Cell::new(5, 6)));
/// let ((w1, d1), (w2, d2)) = grid.into();
/// assert_eq!(((w1, d1), (w2, d2)), ((1, 2), (5, 6)));
/// ```
///
/// Important:
/// When creating `Grid` from cells, you specify `start` and `end` cell, not width and depth
/// This means that if you create grid with `start` (1, 2) and `end` (5, 6),
/// this will be 5x5 grid, not 4x4 as you can think (5 - 1 = 4, 6 - 2 = 4)
/// this is because `start` and `end` bounds included, they are actual members of grid,
/// where the `start` is the first cell on the grid, and `end` is the last cell on grid
///
/// To read `start` and `end` fields, or to calculate other common attributes, use getters:
/// ```
/// use grid_math::{Grid, Cell};
///
/// let grid = Grid::indented(8, 8, (3, 3)); // 8x8 grid, starting at (3,3)
/// let (start, end) = (grid.start(), grid.end());
/// assert_eq!((start, end), (Cell::new(3, 3), Cell::new(10, 10)));
/// let (width, depth) = (grid.width(), grid.depth());
/// assert_eq!((width, depth), (8, 8));
/// let size = grid.size();
/// assert_eq!(size, 64);
/// ```
///
/// 'Grid' implements `Display` and `Debug` trait, so you can easily print it out:
/// ```
/// use grid_math::Grid;
///
/// let grid = Grid::new(10, 10);
/// println!("Grid: {grid}"); // Grid: [(0, 0):(9, 9)]
/// assert_eq!(format!("{grid}"), "[(0, 0):(9, 9)]");
/// ```
///
/// Other advanced operations include interactions with other grids and cells:
///
/// Check if cell or subgrid is within grid:
/// ```
/// use grid_math::{Grid, Cell};
///
/// let grid = Grid::new(10, 10);
/// let cell = Cell::new(3, 4);
/// let subgrid = Grid::indented(5, 5, (2, 2));
/// assert!(subgrid.within(grid));
/// assert!(cell.within(grid));
/// ```
///
/// Get `Cell` from `Grid` by relative position:
/// ```
/// use grid_math::{Grid, Cell};
///
/// let grid = Grid::indented(5, 5, (2, 2));
/// let member = grid.member(2, 2);
///
/// assert_eq!(member, Cell::new(4, 4));
/// ```
///
/// Important:
/// When creating `Grid`, we specify `width` and `depth` in terms of length
/// But when we address member of `Grid`, we specify `width` and `depth` in terms of indexes
/// This means that `Grid` with `width` 5, and start at (0,*), will has `end` at (4,*),
/// because we have (0,*) (1,*) (2,*) (3,*) (4,*), which is 5 elements in total
/// So we used `width` 5 at `Grid` creation and got `Grid` with last cell (4,*)
/// But if we use `width` 5 when indexing member of `Grid`, we will get an error, because indexing starts at 0
///
/// Get subgrid from `Grid` by relative position:
/// ```
/// use grid_math::{Grid, Cell};
///
/// let grid = Grid::indented(5, 5, (2, 2));
/// assert_eq!(format!("{grid}"), "[(2, 2):(6, 6)]");
/// // get subgrid starting at current grid start, with specified width and depth:
/// let area = grid.area(3, 3);
/// assert_eq!(format!("{area}"), "[(2, 2):(4, 4)]");
/// // get subgrid starting at indent from current grid start and specified width and depth:
/// let slice = grid.slice(3, 3, (1, 1));
/// assert_eq!(format!("{slice}"), "[(3, 3):(5, 5)]");
/// ```
///
/// Perform some move calculations of `Cell` on `Grid`:
/// ```
/// use grid_math::{Cell, Grid};
///
/// let grid = Grid::new(10, 10);
/// let cell = grid.start(); // get grid's first cell
/// let next = cell.strict_right(grid, 3); // move to the right by 3, panics if grid bounds overflow occures
/// assert_eq!(next, Cell::new(3, 0));
/// let next = cell.saturating_down(grid, 15); // move down by 15, returns grid bound if overflow occures
/// assert_eq!(next, Cell::new(0, 9));
/// let next = cell.wrapping_right(grid, 5).strict_left(grid, 2).project_down(grid); // chain of movements
/// assert_eq!(next, Cell::new(3, 9));
/// ```
///
/// Perform some actually usefull operations, using `Iterator` functionality:
/// ```
/// use grid_math::{Cell, Grid};
///
/// let grid = Grid::new(3, 3);
/// let grid_string = grid
///     .rows()
///     .map(|row| {
///         row.cells().map(|_| " [#]")
///             .chain(std::iter::once("\n\n"))
///             .collect::<String>()
///     })
///     .collect::<String>();
/// assert_eq!(grid_string,
/// " \
///  [#] [#] [#]
///
///  [#] [#] [#]
///
///  [#] [#] [#]
///
/// "
/// );
/// ```
///
/// To get more examples, look at `Cell` and `Grid` methods documentation.
///
///
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Grid {
    start: Cell,
    end: Cell,
}

/// `Cells` represents an iterator over every `Cell` on the `Grid`
///
/// # Examples
///
/// Get every `Cell` on `width` and `depth` axis:
/// ```
/// use grid_math::{Cell, Grid};
///
/// let grid = Grid::new(3, 3);
///
/// let axis_cells: Vec<Cell> = grid
///     .cells()
///     .filter(|cell| {
///         cell.global_width() == grid.start().global_width() || cell.global_depth() == grid.start().global_depth()
///     })
///     .collect();
/// assert_eq!(axis_cells, vec![
///     Cell::new(0, 0),
///     Cell::new(1, 0),
///     Cell::new(2, 0),
///     Cell::new(0, 1),
///     Cell::new(0, 2),
/// ]);
/// ```
///
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Cells {
    grid: Grid,
    current: Cell,
    consumed: bool,
}

/// `Rows` represents an iterator over every row of `Cell` on the `Grid`
///
/// Every element of 'Rows' returns `Grid`
///
/// # Examples
///
/// Print out `Grid` in custom format:
/// ```
/// use grid_math::{Cell, Grid};
///
/// let grid = Grid::new(3, 3);
/// let grid_string = grid
///     .rows()
///     .map(|row| {
///         row.cells().map(|_| " [#]")
///             .chain(std::iter::once("\n\n"))
///             .collect::<String>()
///     })
///     .collect::<String>();
/// assert_eq!(grid_string,
/// " \
///  [#] [#] [#]
///
///  [#] [#] [#]
///
///  [#] [#] [#]
///
/// "
/// );
/// ```
///
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Rows {
    grid: Grid,
    current: Grid,
    consumed: bool,
}

/// `Columns` represents an iterator over every column of `Cell` on the `Grid`
///
/// Every element of 'Columns' returns `Grid`
///
/// # Examples
///
/// Get every `Cell` on the first column of `Grid`:
/// ```
/// use grid_math::{Cell, Grid};
///
/// let grid = Grid::new(3, 3);
///
/// let first_column_cells: Vec<Cell> = grid
///     .columns()
///     .next()
///     .unwrap()
///     .cells()
///     .collect();
///
/// assert_eq!(first_column_cells, vec![
///     Cell::new(0, 0),
///     Cell::new(0, 1),
///     Cell::new(0, 2),
/// ]);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Columns {
    grid: Grid,
    current: Grid,
    consumed: bool,
}

/// `GridMap<V>` represents a wrapper around the `HashMap<Cell, V>`
///
/// `GridMap` is helpful for storing some actual data on the `Grid`.
/// It implements `Deref` and `DerefMut` traits, so we can call methods from `HashMap`
/// directly on the `GridMap`.
///
/// The main difference between the `GridMap<V>` and the `HashMap<Cell, V>` is that
/// the `GridMap<V>` has actual bounds, that are defined by the inner `Grid`.
///
/// `GridMap` currently has rather stupid error handling, but this helps to prevent scary logical bugs.
///
/// # Examples
///
/// ```
/// use grid_math::{Cell, Grid, GridMap};
///
/// let grid = Grid::new(5, 5);
/// let mut map: GridMap<char> = GridMap::from(grid);
/// map.insert(map.grid().start(), '#');
/// map.insert(map.grid().end(), '@');
/// assert_eq!(map.len(), 2);
/// assert_eq!(map.get(&Cell::new(0, 0)).unwrap(), &'#');
/// ```
///
/// ```should_panic
/// use grid_math::{Cell, Grid, GridMap};
///
/// let grid = Grid::new(5, 5);
/// let cell = Cell::new(6, 6);
/// let mut map: GridMap<char> = GridMap::from(grid);
/// map.insert(cell, '#'); // panic!
/// ```
#[derive(Debug, Clone)]
pub struct GridMap<V> {
    grid: Grid,
    hashmap: HashMap<Cell, V>,
}

impl Cell {
    /// Creates new `Cell` with specified `global_width: u8` and `global_depth: u8` global position
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::Cell;
    ///
    /// let cell = Cell::new(10, 15);
    /// ```
    pub fn new(global_width: u8, global_depth: u8) -> Self {
        Self {
            global_width,
            global_depth,
        }
    }

    /// Checks if the `Cell` is the same as another one
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::Cell;
    ///
    /// let first = Cell::new(5, 5);
    /// let second = Cell::new(5, 5);
    /// assert!(first.at(second));
    ///
    /// let second = Cell::new(2, 3);
    /// assert!(!first.at(second));
    /// ```
    pub fn at(self, other: Cell) -> bool {
        self == other
    }

    /// Checks if the `Cell` is on the same column, or row with another one
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::Cell;
    ///
    /// let first = Cell::new(5, 5);
    /// let second = Cell::new(5, 2);
    /// assert!(first.aligns(second));
    ///
    /// let second = Cell::new(3, 5);
    /// assert!(first.aligns(second));
    ///
    /// let second = Cell::new(2, 3);
    /// assert!(!first.aligns(second));
    /// ```
    pub fn aligns(self, other: Cell) -> bool {
        self.global_width == other.global_width || self.global_depth == other.global_depth
    }

    /// Checks if the `Cell` is on the same column, or row with another one
    ///
    /// # Panics
    /// Panics if `Cell`s have no common lines
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::Cell;
    ///
    /// let first = Cell::new(5, 5);
    /// let second = Cell::new(5, 2);
    /// first.aligns_panic(second);
    /// ```
    ///
    /// ```should_panic
    /// use grid_math::Cell;
    ///
    /// let first = Cell::new(5, 5);
    /// let second = Cell::new(3, 2);
    /// first.aligns_panic(second);
    /// ```
    pub fn aligns_panic(self, other: Cell) {
        if !self.aligns(other) {
            panic!("cells have no common lines! cell:{self}, other:{other}")
        }
    }

    /// Checks if the `Cell` is within the given `Grid`
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::{Cell, Grid};
    ///
    /// let grid = Grid::new(10, 10);
    /// let cell = Cell::new(5, 5);
    /// assert!(cell.within(grid));
    ///
    /// let cell = Cell::new(9, 15);
    /// assert!(!cell.within(grid));
    /// ```
    pub fn within(self, grid: Grid) -> bool {
        (grid.start.global_width..=grid.end.global_width).contains(&self.global_width)
            && (grid.start.global_depth..=grid.end.global_depth).contains(&self.global_depth)
    }

    /// Checks if the `Cell` is within the given `Grid`
    ///
    /// # Panics
    /// Panics if the `Cell` is not within the given `Grid`
    ///
    /// # Examples
    ///
    /// ```should_panic
    /// use grid_math::{Cell, Grid};
    ///
    /// let grid = Grid::new(10, 10);
    /// let cell = Cell::new(9, 15);
    /// cell.within_panic(grid);
    /// ```
    pub fn within_panic(self, grid: Grid) {
        if !self.within(grid) {
            panic!("cell is not within given grid! cell:{self}, grid:{grid}")
        }
    }

    /// Returns `global_width` field of `Cell`
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::Cell;
    ///
    /// let cell = Cell::new(8, 8);
    /// let w = cell.global_width();
    /// assert_eq!(w, 8);
    /// ```
    pub fn global_width(self) -> u8 {
        self.global_width
    }

    /// Returns `global_depth` field of `Cell`
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::Cell;
    ///
    /// let cell = Cell::new(8, 8);
    /// let d = cell.global_depth();
    /// assert_eq!(d, 8);
    /// ```
    pub fn global_depth(self) -> u8 {
        self.global_depth
    }

    /// Calculates the `width` of the `Cell` relative to the given `Grid`
    /// `width` here means position / index / x of `Cell` on width axis
    ///
    /// # Panics
    /// Panics if the `Cell` is not within the given `Grid`
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::{Cell, Grid};
    ///
    /// let cell = Cell::new(8, 8);
    /// let grid = Grid::indented(7, 7, (4, 4)); // 7x7 grid starting at (4,4)
    /// let width = cell.width(grid); // width = 4
    /// assert_eq!(width, 4);
    /// ```
    pub fn width(self, grid: Grid) -> u8 {
        self.within_panic(grid);
        self.global_width - grid.start.global_width
    }

    /// Calculates the gap between the `width` of `Cell` and the `width` of `Grid`
    ///
    /// # Panics
    /// Panics if the `Cell` is not within the given `Grid`
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::{Cell, Grid};
    ///
    /// let cell = Cell::new(8, 8);
    /// let grid = Grid::indented(7, 7, (4, 4)); // 7x7 grid starting at (4,4)
    /// let width_gap = cell.width_gap(grid); // width_gap = 2
    /// assert_eq!(width_gap, 2);
    /// ```
    pub fn width_gap(self, grid: Grid) -> u8 {
        self.within_panic(grid);
        grid.end.global_width - self.global_width
    }

    /// Calculates the `depth` of `Cell` relative to the given `Grid`
    /// `depth` here means position / index / y of `Cell` on depth axis
    ///
    /// # Panics
    /// Panics if the `Cell` is not within the given `Grid`
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::{Cell, Grid};
    ///
    /// let cell = Cell::new(8, 8);
    /// let grid = Grid::indented(7, 7, (4, 4)); // 7x7 grid starting at (4,4)
    /// let depth = cell.depth(grid); // depth = 4
    /// assert_eq!(depth, 4);
    /// ```
    pub fn depth(self, grid: Grid) -> u8 {
        self.within_panic(grid);
        self.global_depth - grid.start.global_depth
    }

    /// Calculates the gap between the `depth` of `Cell` and the `depth` of `Grid`
    ///
    /// # Panics
    /// Panics if the `Cell` is not within the given `Grid`
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::{Cell, Grid};
    ///
    /// let cell = Cell::new(8, 8);
    /// let grid = Grid::indented(7, 7, (4, 4)); // 7x7 grid starting at (4,4)
    /// let depth_gap = cell.depth_gap(grid); // depth_gap = 2
    /// assert_eq!(depth_gap, 2);
    /// ```
    pub fn depth_gap(self, grid: Grid) -> u8 {
        self.within_panic(grid);
        grid.end.global_depth - self.global_depth
    }

    /// Checks if the `up` operation on `Cell` will violate the given `Grid` upper border
    ///
    /// # Panics
    /// Panics if the `Cell` is not within the given `Grid`
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::{Cell, Grid};
    ///
    /// let grid = Grid::new(10, 10);
    /// let cell = Cell::new(2, 2);
    /// assert!(cell.will_underflow_depth(grid, 3));
    /// assert!(!cell.will_underflow_depth(grid, 2));
    /// ```
    pub fn will_underflow_depth(self, grid: Grid, step: u8) -> bool {
        self.within_panic(grid);
        self.global_depth < step || self.global_depth - step < grid.start.global_depth
    }

    /// Checks if the `down` operation on `Cell` will violate the given `Grid` lower border
    ///
    /// # Panics
    /// Panics if the `Cell` is not within the given `Grid`
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::{Cell, Grid};
    ///
    /// let grid = Grid::new(10, 10);
    /// let cell = Cell::new(7, 7);
    /// assert!(cell.will_overflow_depth(grid, 3));
    /// assert!(!cell.will_overflow_depth(grid, 2));
    /// ```
    pub fn will_overflow_depth(self, grid: Grid, step: u8) -> bool {
        self.within_panic(grid);
        self.global_depth > u8::MAX - step || self.global_depth + step > grid.end.global_depth
    }

    /// Checks if the `left` operation on `Cell` will violate the given `Grid` left border
    ///
    /// # Panics
    /// Panics if the `Cell` is not within the given `Grid`
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::{Cell, Grid};
    ///
    /// let grid = Grid::new(10, 10);
    /// let cell = Cell::new(2, 2);
    /// assert!(cell.will_underflow_width(grid, 3));
    /// assert!(!cell.will_underflow_width(grid, 2));
    /// ```
    pub fn will_underflow_width(self, grid: Grid, step: u8) -> bool {
        self.within_panic(grid);
        self.global_width < step || self.global_width - step < grid.start.global_width
    }

    /// Checks if the `right` operation on `Cell` will violate the given `Grid` right border
    ///
    /// # Panics
    /// Panics if the `Cell` is not within the given `Grid`
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::{Cell, Grid};
    ///
    /// let grid = Grid::new(10, 10);
    /// let cell = Cell::new(7, 7);
    /// assert!(cell.will_overflow_width(grid, 3));
    /// assert!(!cell.will_overflow_width(grid, 2));
    /// ```
    pub fn will_overflow_width(self, grid: Grid, step: u8) -> bool {
        self.within_panic(grid);
        self.global_width > u8::MAX - step || self.global_width + step > grid.end.global_width
    }

    /// Moves current `Cell` upwards by `step` relative to the given `Grid`
    /// This operation does not mutate current `Cell` fields,
    /// instead it calculates new position and returns new `Cell`
    ///
    /// # Panics
    /// Panics if the `Cell` is not within the given `Grid`
    /// Panics if this operation will violate the given `Grid` upper border
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::{Cell, Grid};
    ///
    /// let grid = Grid::new(10, 10);
    /// let cell = Cell::new(2, 2);
    /// let next = cell.strict_up(grid, 2);
    /// assert_eq!(next, Cell::new(2, 0));
    /// ```
    ///
    /// ```should_panic
    /// use grid_math::{Cell, Grid};
    ///
    /// let grid = Grid::new(10, 10);
    /// let cell = Cell::new(2, 2);
    /// let next = cell.strict_up(grid, 3); // panic!
    /// ```
    pub fn strict_up(self, grid: Grid, step: u8) -> Cell {
        if self.will_underflow_depth(grid, step) {
            panic!(
                "this operation will violate grid upper bounds! cell:{self}, grid:{grid}, step:{step}"
            );
        }
        Cell {
            global_width: self.global_width,
            global_depth: self.global_depth - step,
        }
    }

    /// Moves current `Cell` downwards by `step` relative to the given `Grid`
    /// This operation does not mutate current `Cell` fields,
    /// instead it calculates new position and returns new `Cell`
    ///
    /// # Panics
    /// Panics if the `Cell` is not within the given `Grid`
    /// Panics if this operation will violate the given `Grid` lower border
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::{Cell, Grid};
    ///
    /// let grid = Grid::new(10, 10);
    /// let cell = Cell::new(7, 7);
    /// let next = cell.strict_down(grid, 2);
    /// assert_eq!(next, Cell::new(7, 9));
    /// ```
    ///
    /// ```should_panic
    /// use grid_math::{Cell, Grid};
    ///
    /// let grid = Grid::new(10, 10);
    /// let cell = Cell::new(7, 7);
    /// let next = cell.strict_down(grid, 3); // panic!
    /// ```
    pub fn strict_down(self, grid: Grid, step: u8) -> Cell {
        if self.will_overflow_depth(grid, step) {
            panic!(
                "this operation will violate grid lower bounds! cell:{self}, grid:{grid}, step:{step}"
            );
        }
        Cell {
            global_width: self.global_width,
            global_depth: self.global_depth + step,
        }
    }

    /// Moves current `Cell` to the left by `step` relative to the given `Grid`
    /// This operation does not mutate current `Cell` fields,
    /// instead it calculates new position and returns new `Cell`
    ///
    /// # Panics
    /// Panics if the `Cell` is not within the given `Grid`
    /// Panics if this operation will violate the given `Grid` left border
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::{Cell, Grid};
    ///
    /// let grid = Grid::new(10, 10);
    /// let cell = Cell::new(2, 2);
    /// let next = cell.strict_left(grid, 2);
    /// assert_eq!(next, Cell::new(0, 2));
    /// ```
    ///
    /// ```should_panic
    /// use grid_math::{Cell, Grid};
    ///
    /// let grid = Grid::new(10, 10);
    /// let cell = Cell::new(2, 2);
    /// let next = cell.strict_left(grid, 3); // panic!
    /// ```
    pub fn strict_left(self, grid: Grid, step: u8) -> Cell {
        if self.will_underflow_width(grid, step) {
            panic!(
                "this operation will violate grid left bounds! cell:{self}, grid:{grid}, step:{step}"
            );
        }
        Cell {
            global_width: self.global_width - step,
            global_depth: self.global_depth,
        }
    }

    /// Moves current `Cell` to the right by `step` relative to the given `Grid`
    /// This operation does not mutate current `Cell` fields,
    /// instead it calculates new position and returns new `Cell`
    ///
    /// # Panics
    /// Panics if the `Cell` is not within the given `Grid`
    /// Panics if this operation will violate the given `Grid` right border
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::{Cell, Grid};
    ///
    /// let grid = Grid::new(10, 10);
    /// let cell = Cell::new(7, 7);
    /// let next = cell.strict_right(grid, 2);
    /// assert_eq!(next, Cell::new(9, 7));
    /// ```
    ///
    /// ```should_panic
    /// use grid_math::{Cell, Grid};
    ///
    /// let grid = Grid::new(10, 10);
    /// let cell = Cell::new(7, 7);
    /// let next = cell.strict_right(grid, 3); // panic!
    /// ```
    pub fn strict_right(self, grid: Grid, step: u8) -> Cell {
        if self.will_overflow_width(grid, step) {
            panic!(
                "this operation will violate grid right bounds! cell:{self}, grid:{grid}, step:{step}"
            );
        }
        Cell {
            global_width: self.global_width + step,
            global_depth: self.global_depth,
        }
    }

    /// Moves current `Cell` upwards by `step` relative to the given `Grid`
    ///
    /// This operation does not mutate current `Cell` fields,
    /// instead it calculates new position and returns new `Cell`
    ///
    /// If this operation will cross `Grid` upper border,
    /// returns `Cell` with `depth` = `Grid` upper depth limit
    ///
    /// # Panics
    /// Panics if the `Cell` is not within the given `Grid`
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::{Cell, Grid};
    ///
    /// let grid = Grid::new(10, 10);
    /// let cell = Cell::new(2, 2);
    /// let next = cell.saturating_up(grid, 2);
    /// assert_eq!(next, Cell::new(2, 0));
    /// let next = cell.saturating_up(grid, 5);
    /// assert_eq!(next, Cell::new(2, 0));
    /// ```
    pub fn saturating_up(self, grid: Grid, step: u8) -> Cell {
        let next_depth = if self.will_underflow_depth(grid, step) {
            grid.start.global_depth
        } else {
            self.global_depth - step
        };
        Cell {
            global_width: self.global_width,
            global_depth: next_depth,
        }
    }

    /// Moves current `Cell` downwards by `step` relative to the given `Grid`
    ///
    /// This operation does not mutate current `Cell` fields,
    /// instead it calculates new position and returns new `Cell`
    ///
    /// If this operation will cross `Grid` lower border,
    /// returns `Cell` with `depth` = `Grid` lower depth limit
    ///
    /// # Panics
    /// Panics if the `Cell` is not within the given `Grid`
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::{Cell, Grid};
    ///
    /// let grid = Grid::new(10, 10);
    /// let cell = Cell::new(7, 7);
    /// let next = cell.saturating_down(grid, 2);
    /// assert_eq!(next, Cell::new(7, 9));
    /// let next = cell.saturating_down(grid, 5);
    /// assert_eq!(next, Cell::new(7, 9));
    /// ```
    pub fn saturating_down(self, grid: Grid, step: u8) -> Cell {
        let next_depth = if self.will_overflow_depth(grid, step) {
            grid.end.global_depth
        } else {
            self.global_depth + step
        };
        Cell {
            global_width: self.global_width,
            global_depth: next_depth,
        }
    }

    /// Moves current `Cell` to the left by `step` relative to the given `Grid`
    ///
    /// This operation does not mutate current `Cell` fields,
    /// instead it calculates new position and returns new `Cell`
    ///
    /// If this operation will cross `Grid` left border,
    /// returns `Cell` with `width` = `Grid` left width limit
    ///
    /// # Panics
    /// Panics if the `Cell` is not within the given `Grid`
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::{Cell, Grid};
    ///
    /// let grid = Grid::new(10, 10);
    /// let cell = Cell::new(2, 2);
    /// let next = cell.saturating_left(grid, 2);
    /// assert_eq!(next, Cell::new(0, 2));
    /// let next = cell.saturating_left(grid, 5);
    /// assert_eq!(next, Cell::new(0, 2));
    /// ```
    pub fn saturating_left(self, grid: Grid, step: u8) -> Cell {
        let next_width = if self.will_underflow_width(grid, step) {
            grid.start.global_width
        } else {
            self.global_width - step
        };
        Cell {
            global_width: next_width,
            global_depth: self.global_depth,
        }
    }

    /// Moves current `Cell` to the right by `step` relative to the given `Grid`
    ///
    /// This operation does not mutate current `Cell` fields,
    /// instead it calculates new position and returns new `Cell`
    ///
    /// If this operation will cross `Grid` right border,
    /// returns `Cell` with `width` = `Grid` right width limit
    ///
    /// # Panics
    /// Panics if the `Cell` is not within the given `Grid`
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::{Cell, Grid};
    ///
    /// let grid = Grid::new(10, 10);
    /// let cell = Cell::new(7, 7);
    /// let next = cell.saturating_right(grid, 2);
    /// assert_eq!(next, Cell::new(9, 7));
    /// let next = cell.saturating_right(grid, 5);
    /// assert_eq!(next, Cell::new(9, 7));
    /// ```
    pub fn saturating_right(self, grid: Grid, step: u8) -> Cell {
        let next_width = if self.will_overflow_width(grid, step) {
            grid.end.global_width
        } else {
            self.global_width + step
        };
        Cell {
            global_width: next_width,
            global_depth: self.global_depth,
        }
    }

    /// Moves current `Cell` upwards by `step` relative to the given `Grid`
    ///
    /// This operation does not mutate current `Cell` fields,
    /// instead it calculates new position and returns new `Cell` and `bool`
    ///
    /// This operation is similar to the overflowing operations on integer types
    /// It returns new `Cell` and 'bool' signaling that overflow happened
    ///
    /// # Panics
    /// Panics if the `Cell` is not within the given `Grid`
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::{Cell, Grid};
    ///
    /// let grid = Grid::new(10, 10);
    /// let cell = Cell::new(2, 2);
    /// let (next, overflowed) = cell.overflowing_up(grid, 2);
    /// assert_eq!((next, overflowed), (Cell::new(2, 0), false));
    /// let (next, overflowed) = cell.overflowing_up(grid, 5);
    /// assert_eq!((next, overflowed), (Cell::new(2, 7), true));
    /// ```
    pub fn overflowing_up(self, grid: Grid, step: u8) -> (Cell, bool) {
        let underflowed = self.will_underflow_depth(grid, step);
        let next_depth = if underflowed {
            grid.end.global_depth - ((step - self.depth(grid) - 1) % grid.depth())
        } else {
            self.global_depth - step
        };
        (
            Cell {
                global_width: self.global_width,
                global_depth: next_depth,
            },
            underflowed,
        )
    }

    /// Moves current `Cell` downwards by `step` relative to the given `Grid`
    ///
    /// This operation does not mutate current `Cell` fields,
    /// instead it calculates new position and returns new `Cell` and `bool`
    ///
    /// This operation is similar to the overflowing operations on integer types
    /// It returns new `Cell` and 'bool' signaling that overflow happened
    ///
    /// # Panics
    /// Panics if the `Cell` is not within the given `Grid`
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::{Cell, Grid};
    ///
    /// let grid = Grid::new(10, 10);
    /// let cell = Cell::new(7, 7);
    /// let (next, overflowed) = cell.overflowing_down(grid, 2);
    /// assert_eq!((next, overflowed), (Cell::new(7, 9), false));
    /// let (next, overflowed) = cell.overflowing_down(grid, 5);
    /// assert_eq!((next, overflowed), (Cell::new(7, 2), true));
    /// ```
    pub fn overflowing_down(self, grid: Grid, step: u8) -> (Cell, bool) {
        let overflowed = self.will_overflow_depth(grid, step);
        let next_depth = if overflowed {
            grid.start.global_depth + ((step - self.depth_gap(grid) - 1) % grid.depth())
        } else {
            self.global_depth + step
        };
        (
            Cell {
                global_width: self.global_width,
                global_depth: next_depth,
            },
            overflowed,
        )
    }

    /// Moves current `Cell` to the left by `step` relative to the given `Grid`
    ///
    /// This operation does not mutate current `Cell` fields,
    /// instead it calculates new position and returns new `Cell` and `bool`
    ///
    /// This operation is similar to the overflowing operations on integer types
    /// It returns new `Cell` and 'bool' signaling that overflow happened
    ///
    /// # Panics
    /// Panics if the `Cell` is not within the given `Grid`
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::{Cell, Grid};
    ///
    /// let grid = Grid::new(10, 10);
    /// let cell = Cell::new(2, 2);
    /// let (next, overflowed) = cell.overflowing_left(grid, 2);
    /// assert_eq!((next, overflowed), (Cell::new(0, 2), false));
    /// let (next, overflowed) = cell.overflowing_left(grid, 5);
    /// assert_eq!((next, overflowed), (Cell::new(7, 2), true));
    /// ```
    pub fn overflowing_left(self, grid: Grid, step: u8) -> (Cell, bool) {
        let underflowed = self.will_underflow_width(grid, step);
        let next_width = if underflowed {
            grid.end.global_width - ((step - self.width(grid) - 1) % grid.width())
        } else {
            self.global_width - step
        };
        (
            Cell {
                global_width: next_width,
                global_depth: self.global_depth,
            },
            underflowed,
        )
    }

    /// Moves current `Cell` to the right by `step` relative to the given `Grid`
    ///
    /// This operation does not mutate current `Cell` fields,
    /// instead it calculates new position and returns new `Cell` and `bool`
    ///
    /// This operation is similar to the overflowing operations on integer types
    /// It returns new `Cell` and 'bool' signaling that overflow happened
    ///
    /// # Panics
    /// Panics if the `Cell` is not within the given `Grid`
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::{Cell, Grid};
    ///
    /// let grid = Grid::new(10, 10);
    /// let cell = Cell::new(7, 7);
    /// let (next, overflowed) = cell.overflowing_right(grid, 2);
    /// assert_eq!((next, overflowed), (Cell::new(9, 7), false));
    /// let (next, overflowed) = cell.overflowing_right(grid, 5);
    /// assert_eq!((next, overflowed), (Cell::new(2, 7), true));
    /// ```
    pub fn overflowing_right(self, grid: Grid, step: u8) -> (Cell, bool) {
        let overflowed = self.will_overflow_width(grid, step);
        let next_width = if overflowed {
            grid.start.global_width + ((step - self.width_gap(grid) - 1) % grid.width())
        } else {
            self.global_width + step
        };
        (
            Cell {
                global_width: next_width,
                global_depth: self.global_depth,
            },
            overflowed,
        )
    }

    /// Moves current `Cell` upwards by `step` relative to the given `Grid`
    ///
    /// This operation is a wrapper around the `overflowing_up()` method,
    /// and returns only new `Cell`, without `bool`
    ///
    /// # Panics
    /// Panics if the `Cell` is not within the given `Grid`
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::{Cell, Grid};
    ///
    /// let grid = Grid::new(10, 10);
    /// let cell = Cell::new(2, 2);
    /// let next = cell.wrapping_up(grid, 2);
    /// assert_eq!(next, Cell::new(2, 0));
    /// let next = cell.wrapping_up(grid, 5);
    /// assert_eq!(next, Cell::new(2, 7));
    /// ```
    pub fn wrapping_up(self, grid: Grid, step: u8) -> Cell {
        self.overflowing_up(grid, step).0
    }

    /// Moves current `Cell` downwards by `step` relative to the given `Grid`
    ///
    /// This operation is a wrapper around the `overflowing_down()` method,
    /// and returns only new `Cell`, without `bool`
    ///
    /// # Panics
    /// Panics if the `Cell` is not within the given `Grid`
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::{Cell, Grid};
    ///
    /// let grid = Grid::new(10, 10);
    /// let cell = Cell::new(7, 7);
    /// let next = cell.wrapping_down(grid, 2);
    /// assert_eq!(next, Cell::new(7, 9));
    /// let next = cell.wrapping_down(grid, 5);
    /// assert_eq!(next, Cell::new(7, 2));
    /// ```
    pub fn wrapping_down(self, grid: Grid, step: u8) -> Cell {
        self.overflowing_down(grid, step).0
    }

    /// Moves current `Cell` to the left by `step` relative to the given `Grid`
    ///
    /// This operation is a wrapper around the `overflowing_left()` method,
    /// and returns only new `Cell`, without `bool`
    ///
    /// # Panics
    /// Panics if the `Cell` is not within the given `Grid`
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::{Cell, Grid};
    ///
    /// let grid = Grid::new(10, 10);
    /// let cell = Cell::new(2, 2);
    /// let next = cell.wrapping_left(grid, 2);
    /// assert_eq!(next, Cell::new(0, 2));
    /// let next = cell.wrapping_left(grid, 5);
    /// assert_eq!(next, Cell::new(7, 2));
    /// ```
    pub fn wrapping_left(self, grid: Grid, step: u8) -> Cell {
        self.overflowing_left(grid, step).0
    }

    /// Moves current `Cell` to the right by `step` relative to the given `Grid`
    ///
    /// This operation is a wrapper around the `overflowing_right()` method,
    /// and returns only new `Cell`, without `bool`
    ///
    /// # Panics
    /// Panics if the `Cell` is not within the given `Grid`
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::{Cell, Grid};
    ///
    /// let grid = Grid::new(10, 10);
    /// let cell = Cell::new(7, 7);
    /// let next = cell.wrapping_right(grid, 2);
    /// assert_eq!(next, Cell::new(9, 7));
    /// let next = cell.wrapping_right(grid, 5);
    /// assert_eq!(next, Cell::new(2, 7));
    /// ```
    pub fn wrapping_right(self, grid: Grid, step: u8) -> Cell {
        self.overflowing_right(grid, step).0
    }

    /// Projects current `Cell` onto the top side of the given `Grid`
    ///
    /// This operation does not mutate current `Cell` fields,
    /// instead it calculates new position and returns new `Cell`
    ///
    /// # Panics
    /// Panics if the `Cell` is not within the given `Grid`
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::{Cell, Grid};
    ///
    /// let grid = Grid::new(10, 10);
    /// let cell = Cell::new(2, 2);
    /// let next = cell.project_up(grid);
    /// assert_eq!(next, Cell::new(2, 0));
    /// ```
    pub fn project_up(self, grid: Grid) -> Cell {
        self.saturating_up(grid, u8::MAX)
    }

    /// Projects current `Cell` onto the bottom side of the given `Grid`
    ///
    /// This operation does not mutate current `Cell` fields,
    /// instead it calculates new position and returns new `Cell`
    ///
    /// # Panics
    /// Panics if the `Cell` is not within the given `Grid`
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::{Cell, Grid};
    ///
    /// let grid = Grid::new(10, 10);
    /// let cell = Cell::new(7, 7);
    /// let next = cell.project_down(grid);
    /// assert_eq!(next, Cell::new(7, 9));
    /// ```
    pub fn project_down(self, grid: Grid) -> Cell {
        self.saturating_down(grid, u8::MAX)
    }

    /// Projects current `Cell` onto the left side of the given `Grid`
    ///
    /// This operation does not mutate current `Cell` fields,
    /// instead it calculates new position and returns new `Cell`
    ///
    /// # Panics
    /// Panics if the `Cell` is not within the given `Grid`
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::{Cell, Grid};
    ///
    /// let grid = Grid::new(10, 10);
    /// let cell = Cell::new(2, 2);
    /// let next = cell.project_left(grid);
    /// assert_eq!(next, Cell::new(0, 2));
    /// ```
    pub fn project_left(self, grid: Grid) -> Cell {
        self.saturating_left(grid, u8::MAX)
    }

    /// Projects current `Cell` onto the right side of the given `Grid`
    ///
    /// This operation does not mutate current `Cell` fields,
    /// instead it calculates new position and returns new `Cell`
    ///
    /// # Panics
    /// Panics if the `Cell` is not within the given `Grid`
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::{Cell, Grid};
    ///
    /// let grid = Grid::new(10, 10);
    /// let cell = Cell::new(7, 7);
    /// let next = cell.project_right(grid);
    /// assert_eq!(next, Cell::new(9, 7));
    /// ```
    pub fn project_right(self, grid: Grid) -> Cell {
        self.saturating_right(grid, u8::MAX)
    }

    /// Moves current `Cell` towards another by `step` relative to the given `Grid`,
    /// executing corresponding strict_move operation
    ///
    /// `target: Cell` acts as a direction, and can only represent `up`, `down`, `left` or `right`
    ///
    /// # Panics
    /// Panics if the current, or target `Cell` is not within the given `Grid`
    /// Panics if the `target` does not align with the current `Cell`
    /// Panics if operation violates the given `Grid` bounds
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::{Cell, Grid};
    ///
    /// let grid = Grid::new(10, 10);
    /// let cell = Cell::new(7, 5);
    /// let (up, down, left, right) = (
    ///     cell.project_up(grid),
    ///     cell.project_down(grid),
    ///     cell.project_left(grid),
    ///     cell.project_right(grid)
    /// );
    /// let next = cell.strict_towards(grid, up, 1);
    /// assert_eq!(next, cell.strict_up(grid, 1));
    /// let next = cell.strict_towards(grid, down, 1);
    /// assert_eq!(next, cell.strict_down(grid, 1));
    /// let next = cell.strict_towards(grid, left, 1);
    /// assert_eq!(next, cell.strict_left(grid, 1));
    /// let next = cell.strict_towards(grid, right, 1);
    /// assert_eq!(next, cell.strict_right(grid, 1));
    /// ```
    pub fn strict_towards(self, grid: Grid, target: Cell, step: u8) -> Cell {
        self.within_panic(grid);
        target.within_panic(grid);
        self.aligns_panic(target);
        match ((self.into()), (target.into())) {
            ((_, d1), (_, d2)) if d1 > d2 => self.strict_up(grid, step),
            ((_, d1), (_, d2)) if d1 < d2 => self.strict_down(grid, step),
            ((w1, _), (w2, _)) if w1 > w2 => self.strict_left(grid, step),
            ((w1, _), (w2, _)) if w1 < w2 => self.strict_right(grid, step),
            _ => target,
        }
    }

    /// Moves current `Cell` towards another by `step` relative to the given `Grid`,
    /// executing corresponding saturating_move operation
    ///
    /// `target: Cell` acts as a direction, and can only represent `up`, `down`, `left` or `right`
    ///
    /// # Panics
    /// Panics if the current, or target `Cell` is not within the given `Grid`
    /// Panics if the `target` does not align with the current `Cell`
    ///
    pub fn saturating_towards(self, grid: Grid, target: Cell, step: u8) -> Cell {
        self.within_panic(grid);
        target.within_panic(grid);
        self.aligns_panic(target);
        match ((self.into()), (target.into())) {
            ((_, d1), (_, d2)) if d1 > d2 => self.saturating_up(grid, step),
            ((_, d1), (_, d2)) if d1 < d2 => self.saturating_down(grid, step),
            ((w1, _), (w2, _)) if w1 > w2 => self.saturating_left(grid, step),
            ((w1, _), (w2, _)) if w1 < w2 => self.saturating_right(grid, step),
            _ => target,
        }
    }

    /// Moves current `Cell` towards another by `step` relative to the given `Grid`,
    /// executing corresponding overflowing_move operation
    ///
    /// `target: Cell` acts as a direction, and can only represent `up`, `down`, `left` or `right`
    ///
    /// # Panics
    /// Panics if the current, or target `Cell` is not within the given `Grid`
    /// Panics if the `target` does not align with the current `Cell`
    ///
    pub fn overflowing_towards(self, grid: Grid, target: Cell, step: u8) -> (Cell, bool) {
        self.within_panic(grid);
        target.within_panic(grid);
        self.aligns_panic(target);
        match ((self.into()), (target.into())) {
            ((_, d1), (_, d2)) if d1 > d2 => self.overflowing_up(grid, step),
            ((_, d1), (_, d2)) if d1 < d2 => self.overflowing_down(grid, step),
            ((w1, _), (w2, _)) if w1 > w2 => self.overflowing_left(grid, step),
            ((w1, _), (w2, _)) if w1 < w2 => self.overflowing_right(grid, step),
            _ => (target, false),
        }
    }

    /// Moves current `Cell` towards another by `step` relative to the given `Grid`,
    /// executing corresponding wrapping_move operation
    ///
    /// `target: Cell` acts as a direction, and can only represent `up`, `down`, `left` or `right`
    ///
    /// # Panics
    /// Panics if the current, or target `Cell` is not within the given `Grid`
    /// Panics if the `target` does not align with the current `Cell`
    ///
    pub fn wrapping_towards(self, grid: Grid, target: Cell, step: u8) -> Cell {
        self.overflowing_towards(grid, target, step).0
    }

    /// Projects current `Cell` onto the `target` side of the given `Grid`,
    /// executing corresponding project operation
    ///
    /// `target: Cell` acts as a direction, and can only represent `up`, `down`, `left` or `right`
    ///
    /// # Panics
    /// Panics if the current, or target `Cell` is not within the given `Grid`
    /// Panics if the `target` does not align with the current `Cell`
    ///
    pub fn project_towards(self, grid: Grid, target: Cell) -> Cell {
        self.saturating_towards(grid, target, u8::MAX)
    }

    /// Checks if the `Cell` is on the edge of the given `Grid`
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::{Cell, Grid};
    ///
    /// let grid = Grid::new(10, 10);
    /// let cell = Cell::new(7, 9);
    /// assert!(cell.on_the_edge(grid));
    /// let cell = Cell::new(4, 3);
    /// assert!(!cell.on_the_edge(grid));
    /// ```
    pub fn on_the_edge(self, grid: Grid) -> bool {
        self.global_width == grid.start.global_width
            || self.global_width == grid.end.global_width
            || self.global_depth == grid.start.global_depth
            || self.global_depth == grid.end.global_depth
    }
}

impl fmt::Display for Cell {
    /// implements display for `Cell`
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::Cell;
    ///
    /// let cell = Cell::new(5, 6);
    /// assert_eq!(format!("{cell}"), "(5, 6)");
    /// ```
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "({w}, {d})",
            w = self.global_width,
            d = self.global_depth
        )
    }
}

impl From<(u8, u8)> for Cell {
    /// implements constructor for `Cell` from (u8, u8)
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::Cell;
    ///
    /// let pos = (5, 6);
    /// let cell = Cell::from(pos);
    /// assert_eq!((pos.0, pos.1), (cell.global_width(), cell.global_depth()));
    /// ```
    fn from(value: (u8, u8)) -> Self {
        Self {
            global_width: value.0,
            global_depth: value.1,
        }
    }
}

#[allow(clippy::from_over_into)]
impl Into<(u8, u8)> for Cell {
    /// implements conversion from `Cell` into (u8, u8)
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::Cell;
    ///
    /// let cell = Cell::new(5, 6);
    /// let pos: (u8, u8) = cell.into();
    /// assert_eq!((pos.0, pos.1), (cell.global_width(), cell.global_depth()));
    /// ```
    fn into(self) -> (u8, u8) {
        (self.global_width, self.global_depth)
    }
}

impl Grid {
    /// Creates new `Grid` with specified `width: u8` and `depth: u8`, starting at (0,0)
    ///
    /// # Panics
    /// Panics if `width` or `depth` parameters < 1
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::Grid;
    ///
    /// let grid = Grid::new(10, 10);
    /// assert_eq!(format!("{grid}"), "[(0, 0):(9, 9)]");
    /// ```
    pub fn new(width: u8, depth: u8) -> Self {
        if width < 1 || depth < 1 {
            panic!("can't create grid with width < 0 or depth < 0!")
        }
        Self {
            start: Cell {
                global_width: 0,
                global_depth: 0,
            },
            end: Cell {
                global_width: width - 1,
                global_depth: depth - 1,
            },
        }
    }

    /// Creates new `Grid` with specified `width: u8` and `depth: u8`, starting at indent
    ///
    /// # Panics
    /// Panics if `width` or `depth` parameters < 1
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::{Grid, Cell};
    ///
    /// let grid = Grid::indented(5, 5, (2, 2));
    /// assert_eq!(format!("{grid}"), "[(2, 2):(6, 6)]");
    ///
    /// // use `Cell` as indent:
    /// let cell = Cell::new(2, 2);
    /// let grid = Grid::indented(5, 5, cell.into());
    /// assert_eq!(format!("{grid}"), "[(2, 2):(6, 6)]");
    /// ```
    pub fn indented(width: u8, depth: u8, indent: (u8, u8)) -> Self {
        if width < 1 || depth < 1 {
            panic!("can't create grid with width < 0 or depth < 0!")
        }
        Self {
            start: Cell {
                global_width: indent.0,
                global_depth: indent.1,
            },
            end: Cell {
                global_width: indent.0 + width - 1,
                global_depth: indent.1 + depth - 1,
            },
        }
    }

    /// Checks if the `Grid` is within the another `Grid`
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::Grid;
    ///
    /// let grid = Grid::new(10, 10);
    /// let subgrid = grid.area(5, 5);
    /// assert!(subgrid.within(grid));
    ///
    /// let subgrid = Grid::new(10, 12);
    /// assert!(!subgrid.within(grid));
    /// ```
    pub fn within(self, grid: Grid) -> bool {
        self.start.within(grid) && self.end.within(grid)
    }

    /// Checks if the `Grid` is within the another `Grid`
    ///
    /// # Panics
    /// Panics if the `Grid` is not within the another `Grid`
    ///
    /// # Examples
    ///
    /// ```should_panic
    /// use grid_math::Grid;
    ///
    /// let grid = Grid::new(10, 10);
    /// let subgrid = Grid::new(10, 12);
    /// subgrid.within_panic(grid);
    /// ```
    pub fn within_panic(self, grid: Grid) {
        if !self.within(grid) {
            panic!("subgrid is not within given grid! subgrid:{self}, grid:{grid}")
        }
    }

    /// Returns new `Cell` by `width: u8` and `depth: u8` relative to the current `Grid`
    ///
    /// # Panics
    /// Panics if `width` or `depth` of the requested member exceeds borders of the current `Grid`
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::{Grid, Cell};
    ///
    /// let grid = Grid::indented(5, 5, (2, 2)); // 5x5 grid, starting at (2,2)
    /// let member = grid.member(4, 4);
    /// assert_eq!(member, Cell::new(6, 6));
    /// ```
    pub fn member(self, width: u8, depth: u8) -> Cell {
        self.start
            .strict_right(self, width)
            .strict_down(self, depth)
    }

    /// Returns new `Grid` with `width: u8` and `depth: u8`, which is a subgrid
    /// of current `Grid`, starting at current `Grid` start
    ///
    /// # Panics
    /// Panics if `width` or `depth` parameters < 1
    /// Panics if `width` or `depth` of the requested area exceeds borders of the current `Grid`
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::{Grid, Cell};
    ///
    /// let grid = Grid::indented(5, 5, (2, 2)); // 5x5 grid, starting at (2,2)
    /// let area = grid.area(3, 3);
    /// assert_eq!(format!("{area}"), "[(2, 2):(4, 4)]");
    /// ```
    pub fn area(self, width: u8, depth: u8) -> Grid {
        if width < 1 || depth < 1 {
            panic!("can't create grid with width < 0 or depth < 0!")
        }
        Grid {
            start: self.start,
            end: self
                .start
                .strict_right(self, width - 1)
                .strict_down(self, depth - 1),
        }
    }

    /// Returns new `Grid` with `width: u8` and `depth: u8`, which is a subgrid
    /// of current `Grid`, starting at current `Grid` start + indent
    ///
    /// # Panics
    /// Panics if `width` or `depth` parameters < 1
    /// Panics if `width` or `depth` of the requested slice exceeds borders of the current `Grid`
    /// Panics if `indent` of the requested slice exceeds borders of the current `Grid`
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::{Grid, Cell};
    ///
    /// let grid = Grid::new(10, 10);
    /// let slice = grid.slice(3, 3, (2, 2));
    /// assert_eq!(format!("{slice}"), "[(2, 2):(4, 4)]");
    ///
    /// // use `Cell` as indent:
    /// let cell = Cell::new(2, 2);
    /// let slice = grid.slice(3, 3, cell.into());
    /// assert_eq!(format!("{slice}"), "[(2, 2):(4, 4)]");
    /// ```
    pub fn slice(self, width: u8, depth: u8, indent: (u8, u8)) -> Grid {
        if width < 1 || depth < 1 {
            panic!("can't create grid with width < 0 or depth < 0!")
        }
        Grid {
            start: self
                .start
                .strict_right(self, indent.0)
                .strict_down(self, indent.1),
            end: self
                .start
                .strict_right(self, indent.0 + width - 1)
                .strict_down(self, indent.1 + depth - 1),
        }
    }

    /// Returns `start` cell of `Grid`
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::{Grid, Cell};
    ///
    /// let grid = Grid::new(10, 10);
    /// let start = grid.start();
    /// assert_eq!(start, Cell::new(0, 0));
    /// ```
    pub fn start(self) -> Cell {
        self.start
    }

    /// Returns `end` cell of `Grid`
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::{Grid, Cell};
    ///
    /// let grid = Grid::new(10, 10);
    /// let end = grid.end();
    /// assert_eq!(end, Cell::new(9, 9));
    /// ```
    pub fn end(self) -> Cell {
        self.end
    }

    /// Calculates `width` of `Grid`
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::Grid;
    ///
    /// let grid = Grid::new(10, 10);
    /// let width = grid.width();
    /// assert_eq!(width, 10);
    /// ```
    pub fn width(self) -> u8 {
        self.end.global_width - self.start.global_width + 1
    }

    /// Calculates `depth` of `Grid`
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::Grid;
    ///
    /// let grid = Grid::new(10, 10);
    /// let depth = grid.depth();
    /// assert_eq!(depth, 10);
    /// ```
    pub fn depth(self) -> u8 {
        self.end.global_depth - self.start.global_depth + 1
    }

    /// Calculates `size: u16` of `Grid`
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::Grid;
    ///
    /// let grid = Grid::new(10, 10);
    /// let size = grid.size();
    /// assert_eq!(size, 100);
    /// ```
    pub fn size(self) -> u16 {
        self.width() as u16 * self.depth() as u16
    }

    /// Returns `Cells`, which is an iterator over every cell of the `Grid`
    ///
    /// # Examples
    ///
    /// Get every `Cell` on `width` and `depth` axis:
    /// ```
    /// use grid_math::{Cell, Grid};
    ///
    /// let grid = Grid::new(3, 3);
    ///
    /// let axis_cells: Vec<Cell> = grid
    ///     .cells()
    ///     .filter(|cell| {
    ///         cell.global_width() == grid.start().global_width() || cell.global_depth() == grid.start().global_depth()
    ///     })
    ///     .collect();
    /// assert_eq!(axis_cells, vec![
    ///     Cell::new(0, 0),
    ///     Cell::new(1, 0),
    ///     Cell::new(2, 0),
    ///     Cell::new(0, 1),
    ///     Cell::new(0, 2),
    /// ]);
    /// ```
    pub fn cells(self) -> Cells {
        Cells::from(self)
    }

    /// Returns `Rows`, which is an iterator over every row of the `Grid`
    ///
    /// # Examples
    ///
    /// Print out `Grid` in custom format:
    /// ```
    /// use grid_math::{Cell, Grid};
    ///
    /// let grid = Grid::new(3, 3);
    /// let grid_string = grid
    ///     .rows()
    ///     .map(|row| {
    ///         row.cells().map(|_| " [#]")
    ///             .chain(std::iter::once("\n\n"))
    ///             .collect::<String>()
    ///     })
    ///     .collect::<String>();
    /// assert_eq!(grid_string,
    /// " \
    ///  [#] [#] [#]
    ///
    ///  [#] [#] [#]
    ///
    ///  [#] [#] [#]
    ///
    /// "
    /// );
    /// ```
    pub fn rows(self) -> Rows {
        Rows::from(self)
    }

    /// Returns `Columns`, which is an iterator over every column of the `Grid`
    ///
    /// # Examples
    ///
    /// Get every `Cell` on the first column of `Grid`:
    /// ```
    /// use grid_math::{Cell, Grid};
    ///
    /// let grid = Grid::new(3, 3);
    ///
    /// let first_column_cells: Vec<Cell> = grid
    ///     .columns()
    ///     .next()
    ///     .unwrap()
    ///     .cells()
    ///     .collect();
    ///
    /// assert_eq!(first_column_cells, vec![
    ///     Cell::new(0, 0),
    ///     Cell::new(0, 1),
    ///     Cell::new(0, 2),
    /// ]);
    /// ```
    pub fn columns(self) -> Columns {
        Columns::from(self)
    }
}

impl From<(Cell, Cell)> for Grid {
    /// implements constructor for `Grid` from (Cell, Cell)
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::{Cell, Grid};
    ///
    /// let cells = (Cell::new(2, 2), Cell::new(5, 5));
    /// let grid = Grid::from(cells);
    /// assert_eq!((cells.0, cells.1), (grid.start(), grid.end()));
    /// ```
    fn from(value: (Cell, Cell)) -> Self {
        let (start, end) = value;
        if start.global_width > end.global_width || start.global_depth > end.global_depth {
            panic!("start cell overflows end cell! start:{start}, end:{end}")
        }
        Self { start, end }
    }
}

#[allow(clippy::from_over_into)]
impl Into<(Cell, Cell)> for Grid {
    /// implements conversion from `Grid` into (Cell, Cell)
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::{Cell, Grid};
    ///
    /// let grid = Grid::new(5, 5);
    /// let cells: (Cell, Cell) = grid.into();
    /// assert_eq!((cells.0, cells.1), (grid.start(), grid.end()));
    /// ```
    fn into(self) -> (Cell, Cell) {
        (self.start, self.end)
    }
}

impl From<((u8, u8), (u8, u8))> for Grid {
    /// implements constructor for `Grid` from ((u8, u8), (u8, u8))
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::{Cell, Grid};
    ///
    /// let vals = ((2, 2), (5, 5));
    /// let grid = Grid::from(vals);
    /// assert_eq!((Cell::from(vals.0), Cell::from(vals.1)), (grid.start(), grid.end()));
    /// ```
    fn from(value: ((u8, u8), (u8, u8))) -> Self {
        let (start, end): (Cell, Cell) = (value.0.into(), value.1.into());
        if start.global_width > end.global_width || start.global_depth > end.global_depth {
            panic!("start cell overflows end cell! start:{start}, end:{end}")
        }
        Self { start, end }
    }
}

#[allow(clippy::from_over_into)]
impl Into<((u8, u8), (u8, u8))> for Grid {
    /// implements conversion from `Grid` into ((u8, u8), (u8, u8))
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::{Cell, Grid};
    ///
    /// let grid = Grid::new(5, 5);
    /// let vals: ((u8, u8), (u8, u8)) = grid.into();
    /// assert_eq!((Cell::from(vals.0), Cell::from(vals.1)), (grid.start(), grid.end()));
    /// ```
    fn into(self) -> ((u8, u8), (u8, u8)) {
        (self.start.into(), self.end.into())
    }
}

impl fmt::Display for Grid {
    /// implements display for `Grid`
    ///
    /// # Examples
    ///
    /// ```
    /// use grid_math::Grid;
    ///
    /// let grid = Grid::new(5, 6);
    /// assert_eq!(format!("{grid}"), "[(0, 0):(4, 5)]");
    /// ```
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "[{start}:{end}]", start = self.start, end = self.end)
    }
}

impl From<Grid> for Cells {
    /// Creates new iterator over every `Cell` on the `Grid`
    ///
    /// # Examples:
    ///
    /// ```
    /// use grid_math::{Grid, Cells};
    ///
    /// let grid = Grid::new(5, 5);
    /// let cells = Cells::from(grid);
    /// ```
    fn from(grid: Grid) -> Self {
        Self {
            grid,
            current: grid.start,
            consumed: false,
        }
    }
}

impl From<Grid> for Columns {
    /// Creates new iterator over every column on the `Grid`
    ///
    /// # Examples:
    ///
    /// ```
    /// use grid_math::{Grid, Columns};
    ///
    /// let grid = Grid::new(5, 5);
    /// let columns = Columns::from(grid);
    /// ```
    fn from(grid: Grid) -> Self {
        Self {
            grid,
            current: Grid {
                start: grid.start,
                end: grid.start.project_down(grid),
            },
            consumed: false,
        }
    }
}

impl From<Grid> for Rows {
    /// Creates new iterator over every row on the `Grid`
    ///
    /// # Examples:
    ///
    /// ```
    /// use grid_math::{Grid, Rows};
    ///
    /// let grid = Grid::new(5, 5);
    /// let rows = Rows::from(grid);
    /// ```
    fn from(grid: Grid) -> Self {
        Self {
            grid,
            current: Grid {
                start: grid.start,
                end: grid.start.project_right(grid),
            },
            consumed: false,
        }
    }
}

impl Iterator for Cells {
    type Item = Cell;
    fn next(&mut self) -> Option<Self::Item> {
        if self.consumed {
            return None;
        }
        if self.current == self.grid.end {
            self.consumed = true;
            return Some(self.current);
        }
        let previous = self.current;
        match self.current.overflowing_right(self.grid, 1) {
            (next, true) => self.current = next.wrapping_down(self.grid, 1),
            (next, false) => self.current = next,
        }
        Some(previous)
    }
}

impl Iterator for Columns {
    type Item = Grid;
    fn next(&mut self) -> Option<Self::Item> {
        if self.consumed {
            return None;
        }
        if self.current.end == self.grid.end {
            self.consumed = true;
            return Some(self.current);
        }
        let previous = self.current;
        self.current = Grid {
            start: self.current.start.saturating_right(self.grid, 1),
            end: self.current.end.saturating_right(self.grid, 1),
        };
        Some(previous)
    }
}

impl Iterator for Rows {
    type Item = Grid;
    fn next(&mut self) -> Option<Self::Item> {
        if self.consumed {
            return None;
        }
        if self.current.end == self.grid.end {
            self.consumed = true;
            return Some(self.current);
        }
        let previous = self.current;
        self.current = Grid {
            start: self.current.start.saturating_down(self.grid, 1),
            end: self.current.end.saturating_down(self.grid, 1),
        };
        Some(previous)
    }
}

impl<V> From<Grid> for GridMap<V> {
    /// Creates new `GridMap` from the given `Grid` with empty `HashMap<Cell, V>`
    ///
    /// # Examples:
    ///
    /// ```
    /// use grid_math::{Grid, GridMap};
    ///
    /// let grid = Grid::new(5, 5);
    /// let map: GridMap<char> = GridMap::from(grid);
    /// ```
    fn from(grid: Grid) -> Self {
        Self {
            grid,
            hashmap: HashMap::new(),
        }
    }
}

impl<V> From<(Grid, HashMap<Cell, V>)> for GridMap<V> {
    /// Creates new `GridMap` from the existing `HashMap<Cell, V>` and the given `Grid`
    ///
    /// # Panics
    /// Panics if the given `HashMap<Cell, V>` contains `Cell`s that are not within the given `Grid`
    ///
    /// # Examples:
    ///
    /// ```
    /// use grid_math::{Cell, Grid, GridMap};
    /// use std::collections::HashMap;
    ///
    /// let grid = Grid::new(5, 5);
    /// let mut hashmap: HashMap<Cell, char> = HashMap::new();
    /// let target = Cell::new(1, 2);
    /// hashmap.insert(target, '#');
    /// let map: GridMap<char> = GridMap::from((grid, hashmap));
    /// assert_eq!(map.get(&target), Some(&'#'));
    /// ```
    ///
    /// ```should_panic
    /// use grid_math::{Cell, Grid, GridMap};
    /// use std::collections::HashMap;
    ///
    /// let grid = Grid::new(5, 5);
    /// let mut hashmap: HashMap<Cell, char> = HashMap::new();
    /// let target = Cell::new(6, 2);
    /// hashmap.insert(target, '#');
    /// let map: GridMap<char> = GridMap::from((grid, hashmap)); // panic!
    /// ```
    fn from(data: (Grid, HashMap<Cell, V>)) -> Self {
        data.1.keys().for_each(|cell| cell.within_panic(data.0));
        Self {
            grid: data.0,
            hashmap: data.1,
        }
    }
}

impl<V> GridMap<V> {
    /// Creates new `GridMap` with `Grid` of specified sizes, and with empty `HashMap<Cell, V>`
    ///
    /// # Examples:
    ///
    /// ```
    /// use grid_math::{Grid, GridMap};
    ///
    /// let map: GridMap<char> = GridMap::new(5, 5);
    ///
    /// assert_eq!(map.grid(), Grid::new(5, 5));
    /// ```
    pub fn new(width: u8, depth: u8) -> Self {
        Self {
            grid: Grid::new(width, depth),
            hashmap: HashMap::new(),
        }
    }

    /// Shadows `insert` method from the `HashMap`, and reimplements it
    /// so it checks first if the key (`Cell`) is within the `Grid`, and then inserts it into the `HashMap`.
    /// This method currently has bad error handling, but this may change in the future
    ///
    /// # Panics
    /// Panics, if the key (`Cell`) is not within the inner `Grid`
    ///
    /// # Examples:
    ///
    /// ```
    /// use grid_math::{Grid, GridMap};
    ///
    /// let grid = Grid::new(5, 5);
    /// let mut map: GridMap<char> = GridMap::from(grid);
    /// map.insert(map.grid().start(), '#');
    /// map.insert(map.grid().end(), '@');
    /// assert_eq!(map.len(), 2);
    /// ```
    ///
    /// ```should_panic
    /// use grid_math::{Cell, Grid, GridMap};
    ///
    /// let grid = Grid::new(5, 5);
    /// let cell = Cell::new(6, 6);
    /// let mut map: GridMap<char> = GridMap::from(grid);
    /// map.insert(cell, '#'); // panic!
    /// ```
    pub fn insert(&mut self, cell: Cell, value: V) -> Option<V> {
        cell.within_panic(self.grid);
        self.hashmap.insert(cell, value)
    }

    /// Inserts new object only if the `Cell` is not occupied.
    /// Returns `true` if inserted, and `false` if not
    ///
    /// # Panics
    /// Panics, if the key (`Cell`) is not within the inner `Grid`
    ///
    /// # Examples:
    ///
    /// ```
    /// use grid_math::{Grid, GridMap};
    ///
    /// let grid = Grid::new(5, 5);
    /// let mut map: GridMap<char> = GridMap::from(grid);
    /// assert!(map.vacant_insert(map.grid().start(), '#'));
    /// assert!(!map.vacant_insert(map.grid().start(), '@'));
    /// assert_eq!(map.get(&map.grid().start()), Some(&'#'));
    /// ```
    ///
    /// ```should_panic
    /// use grid_math::{Cell, Grid, GridMap};
    ///
    /// let grid = Grid::new(5, 5);
    /// let cell = Cell::new(6, 6);
    /// let mut map: GridMap<char> = GridMap::from(grid);
    /// map.vacant_insert(cell, '#'); // panic!
    /// ```
    pub fn vacant_insert(&mut self, cell: Cell, value: V) -> bool {
        cell.within_panic(self.grid);
        if self.vacant(cell) {
            self.hashmap.insert(cell, value);
            true
        } else {
            false
        }
    }

    /// Returns the inner `Grid`
    ///
    /// # Examples:
    ///
    /// ```
    /// use grid_math::{Grid, GridMap};
    ///
    /// let grid = Grid::new(5, 5);
    /// let map: GridMap<char> = GridMap::from(grid);
    ///
    /// assert_eq!(grid, map.grid());
    /// ```
    pub fn grid(&self) -> Grid {
        self.grid
    }

    /// Checks if the `Cell` is occupied. This is an alias for `contains_key` method
    ///
    /// # Panics
    /// Panics, if the given `Cell` is not within the inner `Grid`
    ///
    /// # Examples:
    ///
    /// ```
    /// use grid_math::{Cell, Grid, GridMap};
    ///
    /// let grid = Grid::new(5, 5);
    /// let cell = Cell::new(2, 3);
    /// let mut map: GridMap<char> = GridMap::from(grid);
    /// map.insert(cell, '#');
    ///
    /// assert!(map.occupied(cell));
    /// assert!(!map.occupied(map.grid().start()));
    /// ```
    pub fn occupied(&self, cell: Cell) -> bool {
        cell.within_panic(self.grid);
        self.contains_key(&cell)
    }

    /// Checks if the `Cell` is free
    ///
    /// # Panics
    /// Panics, if the given `Cell` is not within the inner `Grid`
    ///
    /// # Examples:
    ///
    /// ```
    /// use grid_math::{Cell, Grid, GridMap};
    ///
    /// let grid = Grid::new(5, 5);
    /// let cell = Cell::new(2, 3);
    /// let mut map: GridMap<char> = GridMap::from(grid);
    /// map.insert(cell, '#');
    ///
    /// assert!(!map.vacant(cell));
    /// assert!(map.vacant(map.grid().start()));
    /// ```
    pub fn vacant(&self, cell: Cell) -> bool {
        cell.within_panic(self.grid);
        !self.contains_key(&cell)
    }

    /// Returns count of occupied `Cell`s
    ///
    /// # Examples:
    ///
    /// ```
    /// use grid_math::{Cell, Grid, GridMap};
    ///
    /// let mut map: GridMap<char> = GridMap::new(5, 5);
    /// map.insert(Cell::new(2, 3), '#');
    /// map.insert(Cell::new(4, 1), '@');
    ///
    /// assert_eq!(map.occupied_count(), 2);
    /// ```
    pub fn occupied_count(&self) -> u16 {
        self.len() as u16
    }

    /// Returns count of vacant `Cell`s
    ///
    /// # Examples:
    ///
    /// ```
    /// use grid_math::{Cell, Grid, GridMap};
    ///
    /// let mut map: GridMap<char> = GridMap::new(5, 5);
    /// map.insert(Cell::new(2, 3), '#');
    /// map.insert(Cell::new(4, 1), '@');
    ///
    /// assert_eq!(map.vacant_count(), 23);
    /// ```
    pub fn vacant_count(&self) -> u16 {
        self.grid.size() - self.occupied_count()
    }

    /// Returns an iterator over every occupied `Cell`
    ///
    /// # Examples:
    ///
    /// ```
    /// use grid_math::{Cell, Grid, GridMap};
    ///
    /// let mut map: GridMap<char> = GridMap::new(5, 5);
    /// map.insert(Cell::new(2, 3), '#');
    /// map.insert(Cell::new(4, 1), '@');
    ///
    /// assert_eq!(map.all_occupied().count(), 2);
    /// ```
    pub fn all_occupied(&self) -> Filter<Cells, impl FnMut(&Cell) -> bool> {
        self.grid.cells().filter(|&cell| self.occupied(cell))
    }

    /// Returns an iterator over every vacant `Cell`
    ///
    /// # Examples:
    ///
    /// ```
    /// use grid_math::{Cell, Grid, GridMap};
    ///
    /// let mut map: GridMap<char> = GridMap::new(5, 5);
    /// map.insert(Cell::new(2, 3), '#');
    /// map.insert(Cell::new(4, 1), '@');
    ///
    /// assert_eq!(map.all_vacant().count(), 23);
    /// ```
    pub fn all_vacant(&self) -> Filter<Cells, impl FnMut(&Cell) -> bool> {
        self.grid.cells().filter(|&cell| self.vacant(cell))
    }

    /// Returns first occupied `Cell`
    ///
    /// # Note
    /// This returns first `Cell` in `Grid` order, so (2, 3) will go after (4, 1)
    ///
    /// # Examples:
    ///
    /// ```
    /// use grid_math::{Cell, Grid, GridMap};
    ///
    /// let mut map: GridMap<char> = GridMap::new(5, 5);
    /// map.insert(Cell::new(2, 3), '#');
    /// map.insert(Cell::new(4, 1), '@');
    ///
    /// assert_eq!(map.first_occupied(), Some(Cell::new(4, 1)));
    /// ```
    pub fn first_occupied(&self) -> Option<Cell> {
        self.all_occupied().next()
    }

    /// Returns first vacant `Cell`
    ///
    /// # Note
    /// This returns first `Cell` in `Grid` order
    ///
    /// # Examples:
    ///
    /// ```
    /// use grid_math::{Cell, Grid, GridMap};
    ///
    /// let mut map: GridMap<char> = GridMap::new(5, 5);
    /// map.insert(Cell::new(2, 3), '#');
    /// map.insert(Cell::new(4, 1), '@');
    ///
    /// assert_eq!(map.first_vacant(), Some(Cell::new(0, 0)));
    /// ```
    pub fn first_vacant(&self) -> Option<Cell> {
        self.all_vacant().next()
    }

    /// Returns random occupied `Cell`
    ///
    /// # Examples:
    ///
    /// ```
    /// use grid_math::{Cell, Grid, GridMap};
    ///
    /// let mut map: GridMap<char> = GridMap::new(5, 5);
    /// map.insert(Cell::new(2, 3), '#');
    /// map.insert(Cell::new(4, 1), '@');
    ///
    /// assert_ne!(map.get(&map.random_occupied().unwrap()), None);
    /// assert_ne!(map.get(&map.random_occupied().unwrap()), None);
    /// assert_ne!(map.get(&map.random_occupied().unwrap()), None);
    /// ```
    pub fn random_occupied(&self) -> Option<Cell> {
        self.all_occupied().choose(&mut rand::rng())
    }

    /// Returns random vacant `Cell`
    ///
    /// # Examples:
    ///
    /// ```
    /// use grid_math::{Cell, Grid, GridMap};
    ///
    /// let mut map: GridMap<char> = GridMap::new(5, 5);
    /// map.insert(Cell::new(2, 3), '#');
    /// map.insert(Cell::new(4, 1), '@');
    ///
    /// assert_eq!(map.get(&map.random_vacant().unwrap()), None);
    /// assert_eq!(map.get(&map.random_vacant().unwrap()), None);
    /// assert_eq!(map.get(&map.random_vacant().unwrap()), None);
    /// ```
    pub fn random_vacant(&self) -> Option<Cell> {
        self.all_vacant().choose(&mut rand::rng())
    }
}

/// Implements `Deref` trait for GridMap, to return ref to the inner `HashMap`,
/// so we can call methods from `HashMap` directly on the `GridMap`
///
/// # Examples:
///
/// ```
/// use grid_math::{Grid, GridMap};
///
/// let grid = Grid::new(5, 5);
/// let mut map: GridMap<char> = GridMap::from(grid);
/// map.insert(map.grid().start(), '#');
///
/// assert_eq!(map.len(), 1);
/// ```
impl<V> Deref for GridMap<V> {
    type Target = HashMap<Cell, V>;
    fn deref(&self) -> &Self::Target {
        &self.hashmap
    }
}

/// Implements `DerefMut` trait for GridMap, to return mut ref to the inner `HashMap`,
/// so we can call methods from `HashMap` directly on the `GridMap`
///
/// # Examples:
///
/// ```
/// use grid_math::{Grid, GridMap};
///
/// let grid = Grid::new(5, 5);
/// let mut map: GridMap<char> = GridMap::from(grid);
/// map.insert(map.grid().start(), '#');
///
/// assert_eq!(map.len(), 1);
/// ```
impl<V> DerefMut for GridMap<V> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.hashmap
    }
}

// 🦀!⭐!!!