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
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Small map in various sizes. These store a certain number of entries inline, and fall back
//! to a `HashMap` for larger allocations.
//!
//! Provides (almost) 1:1 API compatibility with [`HashMap`].
//!
//! # Examples
//!
//! ```
//! use compact_map::CompactMap;
//!
//! // Type inference lets us omit an explicit type signature except for the capacity
//! // (which would be `CompactMap<String, String, N>` in this example).
//! // Also with default capacity of 16.
//! let mut book_reviews = CompactMap::default();
//!
//! // Review some books.
//! book_reviews.insert(
//!     "Adventures of Huckleberry Finn".to_string(),
//!     "My favorite book.".to_string(),
//! );
//! book_reviews.insert(
//!     "Grimms' Fairy Tales".to_string(),
//!     "Masterpiece.".to_string(),
//! );
//! book_reviews.insert(
//!     "Pride and Prejudice".to_string(),
//!     "Very enjoyable.".to_string(),
//! );
//! book_reviews.insert(
//!     "The Adventures of Sherlock Holmes".to_string(),
//!     "Eye lyked it alot.".to_string(),
//! );
//!
//! // Check for a specific one.
//! // When collections store owned values (String), they can still be
//! // queried using references (&str).
//! if !book_reviews.contains_key("Les Misérables") {
//!     println!("We've got {} reviews, but Les Misérables ain't one.",
//!              book_reviews.len());
//! }
//!
//! // oops, this review has a lot of spelling mistakes, let's delete it.
//! book_reviews.remove("The Adventures of Sherlock Holmes");
//!
//! // Look up the values associated with some keys.
//! let to_find = ["Pride and Prejudice", "Alice's Adventure in Wonderland"];
//! for &book in &to_find {
//!     match book_reviews.get(book) {
//!         Some(review) => println!("{book}: {review}"),
//!         None => println!("{book} is unreviewed.")
//!     }
//! }
//!
//! // Look up the value for a key (will panic if the key is not found).
//! println!("Review for Jane: {}", book_reviews["Pride and Prejudice"]);
//!
//! // Iterate over everything.
//! for (book, review) in &book_reviews {
//!     println!("{book}: \"{review}\"");
//! }
//! ```
//!
//! A `CompactMap` with a known list of items can be initialized from an array:
//!
//! ```
//! use compact_map::CompactMap;
//!
//! // You need to specify the size of the map.
//! let solar_distance = CompactMap::<_, _, 4>::from([
//!     ("Mercury", 0.4),
//!     ("Venus", 0.7),
//!     ("Earth", 1.0),
//!     ("Mars", 1.5),
//! ]);
//! ```
//!
//! `CompactMap` implements an [`Entry` API](CompactMap::entry), which allows
//! for complex methods of getting, setting, updating and removing keys and
//! their values:
//!
//! ```
//! use compact_map::CompactMap;
//!
//! // type inference lets us omit an explicit type signature except for the capacity
//! // (which would be `CompactMap<&str, u8, N>` in this example).
//! let mut player_stats = CompactMap::<_, _, 5>::new();
//!
//! fn random_stat_buff() -> u8 {
//!     // could actually return some random value here - let's just return
//!     // some fixed value for now
//!     42
//! }
//!
//! // insert a key only if it doesn't already exist
//! player_stats.entry("health").or_insert(100);
//!
//! // insert a key using a function that provides a new value only if it
//! // doesn't already exist
//! player_stats.entry("defence").or_insert_with(random_stat_buff);
//!
//! // update a key, guarding against the key possibly not being set
//! let stat = player_stats.entry("attack").or_insert(100);
//! *stat += random_stat_buff();
//!
//! // modify an entry before an insert with in-place mutation
//! player_stats.entry("mana").and_modify(|mana| *mana += 200).or_insert(100);
//! ```
//!
//! ## Optional Features
//!
//! ### `map_entry_replace`
//!
//! **This feature is unstable and requires a nightly build of the Rust toolchain.**
//!
//! *This feature enables the `map_entry_replace` feature gate.*
//!
//! This feature enables the [`OccupiedEntry::replace_entry`] method,
//! it makes operations that would otherwise require two look-ups into operations that require only one.
//!
//! Tracking issue: [rust-lang/rust#44286](https://github.com/rust-lang/rust/issues/44286)
//!
//! ### `extract_if`
//!
//! **This feature is unstable and requires a nightly build of the Rust toolchain.**
//!
//! *This feature enables the `hash_extract_if` feature gate.*
//!
//! This feature enables the [`CompactMap::extract_if`] method,
//! provides a draining, filtering iterator over the entries of a Map.
//!
//! Tracking issue: [rust-lang/rust#59618](https://github.com/rust-lang/rust/issues/59618)
//!
//! ### `entry_insert`
//!
//! **This feature is unstable and requires a nightly build of the Rust toolchain.**
//!
//! *This feature enables the `entry_insert` feature gate.*
//!
//! This feature enables the [`Entry::insert_entry`] method.
//!
//! Tracking issue: [rust-lang/rust#65225](https://github.com/rust-lang/rust/issues/65225)
//!
//! ### `map_try_insert`
//!
//! **This feature is unstable and requires a nightly build of the Rust toolchain.**
//!
//! *This feature enables the `map_try_insert` feature gate.*
//!
//! This feature enables the [`CompactMap::try_insert`] method.
//!
//! Tracking issue: [rust-lang/rust#82766](https://github.com/rust-lang/rust/issues/82766)
//!
//! ### `many_mut`
//!
//! **This feature is unstable and requires a nightly build of the Rust toolchain.**
//!
//! *This feature enables the `map_many_mut`, `get_many_mut` feature gate.*
//!
//! This feature enables the [`CompactMap::get_many_mut`], [`CompactMap::get_many_unchecked_mut`] methods.
//!
//! Tracking issue:
//! - [rust-lang/rust#97601](https://github.com/rust-lang/rust/issues/97601)
//! - [rust-lang/rust#104642](https://github.com/rust-lang/rust/issues/104642)

#![deny(missing_docs)]
#![allow(clippy::manual_map)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![cfg_attr(feature = "map_entry_replace", feature(map_entry_replace))] // issue 44286
#![cfg_attr(feature = "extract_if", feature(hash_extract_if))] // issue 59618
#![cfg_attr(feature = "entry_insert", feature(entry_insert))] // issue 65225
#![cfg_attr(feature = "map_try_insert", feature(map_try_insert))] // issue 82766
#![cfg_attr(feature = "many_mut", feature(map_many_mut))] // issue 97601
#![cfg_attr(feature = "many_mut", feature(get_many_mut))] // issue 104642

use std::borrow::Borrow;
use std::collections::HashMap;
use std::fmt;
use std::fmt::Debug;
use std::hash::{BuildHasher, Hash};
use std::iter::FusedIterator;
use std::ops::Index;

mod base;
mod utils;
#[cfg(feature = "map_try_insert")]
pub use base::entry::OccupiedError;
pub use base::{
    entry::{Entry, OccupiedEntry, VacantEntry},
    TryReserveError,
};

const DEFAULT_MAX_INLINE_ENTRIES: usize = 16;

/// A map that inlines entries to avoid heap allocations for small maps.
pub struct CompactMap<K, V, const N: usize> {
    base: base::MapImpl<K, V, N>,
}

impl<K, V, const N: usize> CompactMap<K, V, N> {
    /// Creates an empty `CompactMap`.
    ///
    /// The compact map will be able to hold up to `N` entries without spilling to the heap.
    ///
    /// # Examples
    ///
    /// ```
    /// use compact_map::CompactMap;
    /// let mut map: CompactMap<&str, i32, 16> = CompactMap::new();
    /// ```
    #[inline(always)]
    #[must_use]
    pub const fn new() -> Self {
        Self {
            base: base::MapImpl::new(),
        }
    }

    /// Returns `true` if the data has spilled into an std `HashMap`.
    ///
    /// ```
    /// use compact_map::CompactMap;
    ///
    /// let mut map: CompactMap<i32, i32, 2>  = CompactMap::new();
    /// assert!(!map.spilled());
    ///
    /// map.insert(1, 2);
    /// map.insert(3, 4);
    /// map.insert(5, 6);
    /// assert!(map.spilled());
    /// ```
    #[inline(always)]
    pub const fn spilled(&self) -> bool {
        self.base.spilled()
    }

    /// Returns the number of elements the map can hold without reallocating.
    ///
    /// When spilled, this number is a lower bound;
    /// the `CompactMap<K, V>` might be able to hold more,
    /// but is guaranteed to be able to hold at least this many.
    ///
    /// # Examples
    ///
    /// ```
    /// use compact_map::CompactMap;
    /// let map: CompactMap<i32, i32, 100> = CompactMap::new();
    /// assert!(map.capacity() >= 100);
    /// ```
    #[inline(always)]
    pub fn capacity(&self) -> usize {
        self.base.capacity()
    }

    /// An iterator visiting all keys in arbitrary order.
    /// The iterator element type is `&'a K`.
    ///
    /// # Examples
    ///
    /// ```
    /// use compact_map::CompactMap;
    ///
    /// let map: CompactMap<&str, i32, 3> = CompactMap::from([
    ///     ("a", 1),
    ///     ("b", 2),
    ///     ("c", 3),
    /// ]);
    ///
    /// for key in map.keys() {
    ///     println!("{key}");
    /// }
    /// ```
    ///
    /// # Performance
    ///
    /// - When heapless: iterating over keys takes O(len) time.
    /// - When spilled: as per docs in [HashMap::keys], iterating over keys takes O(capacity) time.
    #[inline]
    pub fn keys(&self) -> Keys<'_, K, V, N> {
        Keys {
            inner: self.base.iter(),
        }
    }

    /// Creates a consuming iterator visiting all the keys in arbitrary order.
    /// The map cannot be used after calling this.
    /// The iterator element type is `K`.
    ///
    /// # Examples
    ///
    /// ```
    /// use compact_map::CompactMap;
    ///
    /// let map: CompactMap<&str, i32, 3> = CompactMap::from([
    ///     ("a", 1),
    ///     ("b", 2),
    ///     ("c", 3),
    /// ]);
    ///
    /// let mut vec: Vec<&str> = map.into_keys().collect();
    /// // The `IntoKeys` iterator produces keys in arbitrary order, so the
    /// // keys must be sorted to test them against a sorted array.
    /// vec.sort_unstable();
    /// assert_eq!(vec, ["a", "b", "c"]);
    /// ```
    ///
    /// # Performance
    ///
    /// - When heapless: iterating over keys takes O(len) time.
    /// - When spilled: as per in [std docs](HashMap::keys), iterating over keys takes O(capacity) time.
    #[inline]
    pub fn into_keys(self) -> IntoKeys<K, V, N> {
        IntoKeys {
            inner: self.base.into_iter(),
        }
    }

    /// An iterator visiting all values in arbitrary order.
    /// The iterator element type is `&'a V`.
    ///
    /// # Examples
    ///
    /// ```
    /// use compact_map::CompactMap;
    ///
    /// let map: CompactMap<&str, i32, 3> = CompactMap::from([
    ///     ("a", 1),
    ///     ("b", 2),
    ///     ("c", 3),
    /// ]);
    ///
    /// for val in map.values() {
    ///     println!("{val}");
    /// }
    /// ```
    ///
    /// # Performance
    ///
    /// - When heapless: iterating over keys takes O(len) time.
    /// - When spilled: as per in [std docs](HashMap::keys), iterating over keys takes O(capacity) time.
    pub fn values(&self) -> Values<'_, K, V, N> {
        Values {
            base: self.base.iter(),
        }
    }

    /// An iterator visiting all values mutably in arbitrary order.
    /// The iterator element type is `&'a mut V`.
    ///
    /// # Examples
    ///
    /// ```
    /// use compact_map::CompactMap;
    ///
    /// let mut map: CompactMap<&str, i32, 3> = CompactMap::from([
    ///     ("a", 1),
    ///     ("b", 2),
    ///     ("c", 3),
    /// ]);
    ///
    /// for val in map.values_mut() {
    ///     *val = *val + 10;
    /// }
    ///
    /// for val in map.values() {
    ///     println!("{val}");
    /// }
    /// ```
    ///
    /// # Performance
    ///
    /// - When heapless: iterating over keys takes O(len) time.
    /// - When spilled: as per in [std docs](HashMap::keys), iterating over keys takes O(capacity) time.
    pub fn values_mut(&mut self) -> ValuesMut<'_, K, V, N> {
        ValuesMut {
            inner: self.base.iter_mut(),
        }
    }

    /// Creates a consuming iterator visiting all the values in arbitrary order.
    /// The map cannot be used after calling this.
    /// The iterator element type is `V`.
    ///
    /// # Examples
    ///
    /// ```
    /// use compact_map::CompactMap;
    ///
    /// let mut map: CompactMap<&str, i32, 3> = CompactMap::from([
    ///     ("a", 1),
    ///     ("b", 2),
    ///     ("c", 3),
    /// ]);
    ///
    /// let mut vec: Vec<i32> = map.into_values().collect();
    /// // The `IntoValues` iterator produces values in arbitrary order, so
    /// // the values must be sorted to test them against a sorted array.
    /// vec.sort_unstable();
    /// assert_eq!(vec, [1, 2, 3]);
    /// ```
    ///
    /// # Performance
    ///
    /// - When heapless: iterating over keys takes O(len) time.
    /// - When spilled: as per in [std docs](HashMap::keys), iterating over keys takes O(capacity) time.
    #[inline]
    pub fn into_values(self) -> IntoValues<K, V, N> {
        IntoValues {
            inner: self.base.into_iter(),
        }
    }

    /// An iterator visiting all key-value pairs in arbitrary order.
    /// The iterator element type is `(&'a K, &'a V)`.
    ///
    /// # Examples
    ///
    /// ```
    /// use compact_map::CompactMap;
    ///
    /// let map: CompactMap<&str, i32, 3> = CompactMap::from([
    ///     ("a", 1),
    ///     ("b", 2),
    ///     ("c", 3),
    /// ]);
    ///
    /// for (key, val) in map.iter() {
    ///     println!("key: {key} val: {val}");
    /// }
    /// ```
    ///
    /// # Performance
    ///
    /// - When heapless: iterating over keys takes O(len) time.
    /// - When spilled: as per in [std docs](HashMap::keys), iterating over keys takes O(capacity) time.
    #[inline]
    pub fn iter(&self) -> Iter<'_, K, V, N> {
        Iter {
            base: self.base.iter(),
        }
    }

    /// An iterator visiting all key-value pairs in arbitrary order,
    /// with mutable references to the values.
    /// The iterator element type is `(&'a K, &'a mut V)`.
    ///
    /// # Examples
    ///
    /// ```
    /// use compact_map::CompactMap;
    ///
    /// let mut map: CompactMap<&str, i32, 3> = CompactMap::from([
    ///     ("a", 1),
    ///     ("b", 2),
    ///     ("c", 3),
    /// ]);
    ///
    /// // Update all values
    /// for (_, val) in map.iter_mut() {
    ///     *val *= 2;
    /// }
    ///
    /// for (key, val) in &map {
    ///     println!("key: {key} val: {val}");
    /// }
    /// ```
    ///
    /// # Performance
    ///
    /// - When heapless: iterating over keys takes O(len) time.
    /// - When spilled: as per in [std docs](HashMap::keys), iterating over keys takes O(capacity) time.
    pub fn iter_mut(&mut self) -> IterMut<'_, K, V, N> {
        IterMut {
            base: self.base.iter_mut(),
        }
    }

    /// Returns the number of elements in the map.
    ///
    /// # Examples
    ///
    /// ```
    /// use compact_map::CompactMap;
    ///
    /// let mut a = CompactMap::default();
    /// assert_eq!(a.len(), 0);
    /// a.insert(1, "a");
    /// assert_eq!(a.len(), 1);
    /// ```
    pub fn len(&self) -> usize {
        self.base.len()
    }

    /// Returns `true` if the map contains no elements.
    ///
    /// # Examples
    ///
    /// ```
    /// use compact_map::CompactMap;
    ///
    /// let mut a = CompactMap::default();
    /// assert!(a.is_empty());
    /// a.insert(1, "a");
    /// assert!(!a.is_empty());
    /// ```
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.base.is_empty()
    }

    /// Clears the map, returning all key-value pairs as an iterator. Keeps the
    /// allocated memory for reuse.
    ///
    /// If the returned iterator is dropped before being fully consumed, it
    /// drops the remaining key-value pairs. The returned iterator keeps a
    /// mutable borrow on the map to optimize its implementation.
    ///
    /// # Examples
    ///
    /// ```
    /// use compact_map::CompactMap;
    ///
    /// let mut a = CompactMap::default();
    /// a.insert(1, "a");
    /// a.insert(2, "b");
    ///
    /// for (k, v) in a.drain().take(1) {
    ///     assert!(k == 1 || k == 2);
    ///     assert!(v == "a" || v == "b");
    /// }
    ///
    /// assert!(a.is_empty());
    /// ```
    #[inline]
    pub fn drain(&mut self) -> Drain<'_, K, V, N> {
        Drain {
            base: self.base.drain(),
        }
    }

    /// Creates an iterator which uses a closure to determine if an element should be removed.
    ///
    /// If the closure returns true, the element is removed from the map and yielded.
    /// If the closure returns false, or panics, the element remains in the map and will not be
    /// yielded.
    ///
    /// Note that `extract_if` lets you mutate every value in the filter closure, regardless of
    /// whether you choose to keep or remove it.
    ///
    /// If the returned `ExtractIf` is not exhausted, e.g. because it is dropped without iterating
    /// or the iteration short-circuits, then the remaining elements will be retained.
    /// Use [`retain`] with a negated predicate if you do not need the returned iterator.
    ///
    /// [`retain`]: CompactMap::retain
    ///
    /// # Examples
    ///
    /// Splitting a map into even and odd keys, reusing the original map:
    ///
    /// ```
    /// use compact_map::CompactMap;
    ///
    /// let mut map: CompactMap<i32, i32, 8> = (0..8).map(|x| (x, x)).collect();
    /// let extracted: CompactMap<i32, i32, 8> = map.extract_if(|k, _v| k % 2 == 0).collect();
    ///
    /// let mut evens = extracted.keys().copied().collect::<Vec<_>>();
    /// let mut odds = map.keys().copied().collect::<Vec<_>>();
    /// evens.sort();
    /// odds.sort();
    ///
    /// assert_eq!(evens, vec![0, 2, 4, 6]);
    /// assert_eq!(odds, vec![1, 3, 5, 7]);
    /// ```
    #[cfg_attr(docsrs, doc(cfg(feature = "extract_if")))]
    #[cfg(feature = "extract_if")]
    #[inline]
    pub fn extract_if<F>(&mut self, pred: F) -> ExtractIf<'_, K, V, F, N>
    where
        F: FnMut(&K, &mut V) -> bool,
    {
        ExtractIf {
            base: self.base.extract_if(pred),
        }
    }

    /// Retains only the elements specified by the predicate.
    ///
    /// In other words, remove all pairs `(k, v)` for which `f(&k, &mut v)` returns `false`.
    /// The elements are visited in unsorted (and unspecified) order.
    ///
    /// # Examples
    ///
    /// ```
    /// use compact_map::CompactMap;
    ///
    /// let mut map: CompactMap<i32, i32, 16> = (0..8).map(|x| (x, x*10)).collect();
    /// map.retain(|&k, _| k % 2 == 0);
    /// assert_eq!(map.len(), 4);
    /// ```
    ///
    /// # Performance
    ///
    /// - When heapless: iterating over keys takes O(len) time.
    /// - When spilled: as per in [std docs](HashMap::keys), iterating over keys takes O(capacity) time.
    #[inline]
    pub fn retain<F>(&mut self, f: F)
    where
        F: FnMut(&K, &mut V) -> bool,
    {
        self.base.retain(f)
    }

    /// Clears the map, removing all key-value pairs. Keeps the allocated memory
    /// for reuse.
    ///
    /// # Examples
    ///
    /// ```
    /// use compact_map::CompactMap;
    ///
    /// let mut a = CompactMap::default();
    /// a.insert(1, "a");
    /// a.clear();
    /// assert!(a.is_empty());
    /// ```
    #[inline]
    pub fn clear(&mut self) {
        self.base.clear();
    }
}

impl<K, V, const N: usize> CompactMap<K, V, N>
where
    K: Eq + Hash,
{
    /// Reserves capacity for at least `additional` more elements to be inserted
    /// in the `CompactMap`. The collection may reserve more space to speculatively
    /// avoid frequent reallocations. After calling `reserve`,
    /// capacity will be greater than or equal to `self.len() + additional`.
    /// Does nothing if capacity is already sufficient.
    ///
    /// If current variant is heapless and `self.len() + additional` is greater than `N`,
    /// the map will spill to [`HashMap`] immediately; otherwise, it's a no-op.
    ///
    /// # Panics
    ///
    /// Panics if the new allocation size overflows [`usize`].
    ///
    /// # Examples
    ///
    /// ```
    /// use compact_map::CompactMap;
    /// let mut map: CompactMap<&str, i32, 16> = CompactMap::new();
    /// map.reserve(32);
    /// assert!(map.capacity() >= 32);
    /// assert!(map.spilled());
    /// ```
    #[inline]
    pub fn reserve(&mut self, additional: usize) {
        self.base.reserve(additional)
    }

    /// Tries to reserve capacity for at least `additional` more elements to be inserted
    /// in the `HashMap`. The collection may reserve more space to speculatively
    /// avoid frequent reallocations. After calling `try_reserve`,
    /// capacity will be greater than or equal to `self.len() + additional` if
    /// it returns `Ok(())`.
    /// Does nothing if capacity is already sufficient.
    ///
    /// If current variant is heapless and `self.len() + additional` is greater than `N`,
    /// the map will spill to [`HashMap`] immediately; otherwise, it's a no-op.
    ///
    /// # Errors
    ///
    /// If the capacity overflows, or the allocator reports a failure, then an error
    /// is returned.
    ///
    /// # Examples
    ///
    /// ```
    /// use compact_map::CompactMap;
    /// let mut map: CompactMap<&str, i32, 16> = CompactMap::new();
    ///
    /// map.try_reserve(10).expect("why is the test harness OOMing on a handful of bytes?");
    /// ```
    #[inline]
    pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
        self.base.try_reserve(additional)
    }

    /// Manually spills to a [`HashMap`].
    ///
    /// # Examples
    ///
    /// ```
    /// use compact_map::CompactMap;
    /// let mut map: CompactMap<i32, i32, 16> = CompactMap::new();
    /// map.spill();
    /// assert!(map.spilled());
    #[inline]
    pub fn spill(&mut self) {
        self.base.spill()
    }

    /// Shrinks the map into a heapless map with capacity `M`.
    ///
    /// # Errors
    ///
    /// If `M` is less than the current length of the map, the original map is returned.
    ///
    /// # Examples
    ///
    /// ```
    /// use compact_map::CompactMap;
    ///
    /// let mut map: CompactMap<i32, i32, 16> = CompactMap::new();
    /// map.insert(1, 2);
    /// map.insert(3, 4);
    /// let map = map.shrink_into_heapless::<2>().unwrap();
    /// ```
    #[inline]
    pub fn shrink_into_heapless<const M: usize>(
        self,
    ) -> Result<CompactMap<K, V, M>, CompactMap<K, V, N>> {
        self.base
            .shrink_into_heapless()
            .map(|base| CompactMap { base })
            .map_err(|base| CompactMap { base })
    }

    /// This is a proxy to the underlying [`HashMap::shrink_to_fit`] method.
    /// And it's a no-op if the map is heapless.
    ///
    /// Shrinks the capacity of the map as much as possible. It will drop
    /// down as much as possible while maintaining the internal rules
    /// and possibly leaving some space in accordance with the resize policy.
    ///
    /// # Examples
    ///
    /// ```
    /// use compact_map::CompactMap;
    ///
    /// let mut map: CompactMap<i32, i32, 10> = CompactMap::new();
    /// map.reserve(90);
    /// map.insert(1, 2);
    /// map.insert(3, 4);
    /// assert!(map.capacity() >= 100);
    /// map.shrink_to_fit();
    /// assert!(map.capacity() >= 2);
    /// ```
    #[inline]
    pub fn shrink_to_fit(&mut self) {
        self.base.shrink_to_fit();
    }

    /// This is a proxy to the underlying [`HashMap::shrink_to`] method.
    /// And it's a no-op if the map is heapless.
    ///
    /// Shrinks the capacity of the map with a lower limit. It will drop
    /// down no lower than the supplied limit while maintaining the internal rules
    /// and possibly leaving some space in accordance with the resize policy.
    ///
    /// If the current capacity is less than the lower limit, this is a no-op.
    ///
    /// # Examples
    ///
    /// ```
    /// use compact_map::CompactMap;
    ///
    /// let mut map: CompactMap<i32, i32, 10> = CompactMap::new();
    /// map.reserve(90);
    /// map.insert(1, 2);
    /// map.insert(3, 4);
    /// assert!(map.capacity() >= 100);
    /// map.shrink_to(10);
    /// assert!(map.capacity() >= 10);
    /// map.shrink_to(0);
    /// assert!(map.capacity() >= 2);
    /// ```
    #[inline]
    pub fn shrink_to(&mut self, min_capacity: usize) {
        self.base.shrink_to(min_capacity);
    }

    /// Gets the given key's corresponding entry in the map for in-place manipulation.
    ///
    /// # Examples
    ///
    /// ```
    /// use compact_map::CompactMap;
    ///
    /// let mut letters = CompactMap::default();
    ///
    /// for ch in "a short treatise on fungi".chars() {
    ///     letters.entry(ch).and_modify(|counter| *counter += 1).or_insert(1);
    /// }
    ///
    /// assert_eq!(letters[&'s'], 2);
    /// assert_eq!(letters[&'t'], 3);
    /// assert_eq!(letters[&'u'], 1);
    /// assert_eq!(letters.get(&'y'), None);
    /// ```
    #[inline]
    pub fn entry(&mut self, key: K) -> Entry<'_, K, V, N> {
        self.base.entry(key)
    }

    /// Returns a reference to the value corresponding to the key.
    ///
    /// The key may be any borrowed form of the map's key type, but
    /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for
    /// the key type.
    ///
    /// # Examples
    ///
    /// ```
    /// use compact_map::CompactMap;
    ///
    /// let mut map = CompactMap::default();
    /// map.insert(1, "a");
    /// assert_eq!(map.get(&1), Some(&"a"));
    /// assert_eq!(map.get(&2), None);
    /// ```
    #[inline]
    pub fn get<Q>(&self, k: &Q) -> Option<&V>
    where
        K: Borrow<Q>,
        Q: Hash + Eq + ?Sized,
    {
        self.base.get(k)
    }

    /// Returns the key-value pair corresponding to the supplied key.
    ///
    /// The supplied key may be any borrowed form of the map's key type, but
    /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for
    /// the key type.
    ///
    /// # Examples
    ///
    /// ```
    /// use compact_map::CompactMap;
    ///
    /// let mut map = CompactMap::default();
    /// map.insert(1, "a");
    /// assert_eq!(map.get_key_value(&1), Some((&1, &"a")));
    /// assert_eq!(map.get_key_value(&2), None);
    /// ```
    #[inline]
    pub fn get_key_value<Q>(&self, k: &Q) -> Option<(&K, &V)>
    where
        K: Borrow<Q>,
        Q: Hash + Eq + ?Sized,
    {
        self.base.get_key_value(k)
    }

    /// Attempts to get mutable references to `N` values in the map at once.
    ///
    /// Returns an array of length `N` with the results of each query. For soundness, at most one
    /// mutable reference will be returned to any value. `None` will be returned if any of the
    /// keys are duplicates or missing.
    ///
    /// # Examples
    ///
    /// ```
    /// use compact_map::CompactMap;
    ///
    /// let mut libraries = CompactMap::default();
    /// libraries.insert("Bodleian Library".to_string(), 1602);
    /// libraries.insert("Athenæum".to_string(), 1807);
    /// libraries.insert("Herzogin-Anna-Amalia-Bibliothek".to_string(), 1691);
    /// libraries.insert("Library of Congress".to_string(), 1800);
    ///
    /// let got = libraries.get_many_mut([
    ///     "Athenæum",
    ///     "Library of Congress",
    /// ]);
    /// assert_eq!(
    ///     got,
    ///     Some([
    ///         &mut 1807,
    ///         &mut 1800,
    ///     ]),
    /// );
    ///
    /// // Missing keys result in None
    /// let got = libraries.get_many_mut([
    ///     "Athenæum",
    ///     "New York Public Library",
    /// ]);
    /// assert_eq!(got, None);
    ///
    /// // Duplicate keys result in None
    /// let got = libraries.get_many_mut([
    ///     "Athenæum",
    ///     "Athenæum",
    /// ]);
    /// assert_eq!(got, None);
    /// ```
    #[cfg_attr(docsrs, doc(cfg(feature = "many_mut")))]
    #[cfg(feature = "many_mut")]
    #[inline]
    pub fn get_many_mut<Q, const M: usize>(&mut self, ks: [&Q; M]) -> Option<[&'_ mut V; M]>
    where
        K: Borrow<Q>,
        Q: Hash + Eq + ?Sized,
    {
        self.base.get_many_mut(ks)
    }

    /// Attempts to get mutable references to `N` values in the map at once, without validating that
    /// the values are unique.
    ///
    /// Returns an array of length `N` with the results of each query. `None` will be returned if
    /// any of the keys are missing.
    ///
    /// For a safe alternative see [`get_many_mut`](Self::get_many_mut).
    ///
    /// # Safety
    ///
    /// Calling this method with overlapping keys is *[undefined behavior]* even if the resulting
    /// references are not used.
    ///
    /// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
    ///
    /// # Examples
    ///
    /// ```
    /// use compact_map::CompactMap;
    ///
    /// let mut libraries = CompactMap::default();
    /// libraries.insert("Bodleian Library".to_string(), 1602);
    /// libraries.insert("Athenæum".to_string(), 1807);
    /// libraries.insert("Herzogin-Anna-Amalia-Bibliothek".to_string(), 1691);
    /// libraries.insert("Library of Congress".to_string(), 1800);
    ///
    /// let got = libraries.get_many_mut([
    ///     "Athenæum",
    ///     "Library of Congress",
    /// ]);
    /// assert_eq!(
    ///     got,
    ///     Some([
    ///         &mut 1807,
    ///         &mut 1800,
    ///     ]),
    /// );
    ///
    /// // Missing keys result in None
    /// let got = libraries.get_many_mut([
    ///     "Athenæum",
    ///     "New York Public Library",
    /// ]);
    /// assert_eq!(got, None);
    /// ```
    #[cfg_attr(docsrs, doc(cfg(feature = "many_mut")))]
    #[cfg(feature = "many_mut")]
    #[inline]
    pub unsafe fn get_many_unchecked_mut<Q, const M: usize>(
        &mut self,
        ks: [&Q; M],
    ) -> Option<[&'_ mut V; M]>
    where
        K: Borrow<Q>,
        Q: Hash + Eq + ?Sized,
    {
        self.base.get_many_unchecked_mut(ks)
    }

    /// Returns `true` if the map contains a value for the specified key.
    ///
    /// The key may be any borrowed form of the map's key type, but
    /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for
    /// the key type.
    ///
    /// # Examples
    ///
    /// ```
    /// use compact_map::CompactMap;
    ///
    /// let mut map = CompactMap::default();
    /// map.insert(1, "a");
    /// assert_eq!(map.contains_key(&1), true);
    /// assert_eq!(map.contains_key(&2), false);
    /// ```
    #[inline]
    pub fn contains_key<Q>(&self, k: &Q) -> bool
    where
        K: Borrow<Q>,
        Q: Hash + Eq + ?Sized,
    {
        self.base.contains_key(k)
    }

    /// Returns a mutable reference to the value corresponding to the key.
    ///
    /// The key may be any borrowed form of the map's key type, but
    /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for
    /// the key type.
    ///
    /// # Examples
    ///
    /// ```
    /// use compact_map::CompactMap;
    ///
    /// let mut map = CompactMap::default();
    /// map.insert(1, "a");
    /// if let Some(x) = map.get_mut(&1) {
    ///     *x = "b";
    /// }
    /// assert_eq!(map[&1], "b");
    /// ```
    #[inline]
    pub fn get_mut<Q>(&mut self, k: &Q) -> Option<&mut V>
    where
        K: Borrow<Q>,
        Q: Hash + Eq + ?Sized,
    {
        self.base.get_mut(k)
    }

    /// Inserts a key-value pair into the map.
    ///
    /// If the map did not have this key present, [`None`] is returned.
    ///
    /// If the map did have this key present, the value is updated, and the old
    /// value is returned. The key is not updated, though; this matters for
    /// types that can be `==` without being identical. See the [module-level
    /// documentation] for more.
    ///
    /// # Examples
    ///
    /// ```
    /// use compact_map::CompactMap;
    ///
    /// let mut map = CompactMap::default();
    /// assert_eq!(map.insert(37, "a"), None);
    /// assert_eq!(map.is_empty(), false);
    ///
    /// map.insert(37, "b");
    /// assert_eq!(map.insert(37, "c"), Some("b"));
    /// assert_eq!(map[&37], "c");
    /// ```
    #[inline]
    pub fn insert(&mut self, k: K, v: V) -> Option<V> {
        self.base.insert(k, v)
    }

    /// Tries to insert a key-value pair into the map, and returns
    /// a mutable reference to the value in the entry.
    ///
    /// If the map already had this key present, nothing is updated, and
    /// an error containing the occupied entry and the value is returned.
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// use compact_map::CompactMap;
    ///
    /// let mut map = CompactMap::default();
    /// assert_eq!(map.try_insert(37, "a").unwrap(), &"a");
    ///
    /// let err = map.try_insert(37, "b").unwrap_err();
    /// assert_eq!(err.entry.key(), &37);
    /// assert_eq!(err.entry.get(), &"a");
    /// assert_eq!(err.value, "b");
    /// ```
    #[cfg_attr(docsrs, doc(cfg(feature = "map_try_insert")))]
    #[cfg(feature = "map_try_insert")]
    pub fn try_insert(&mut self, key: K, value: V) -> Result<&mut V, OccupiedError<'_, K, V, N>> {
        match self.entry(key) {
            Entry::Occupied(entry) => Err(OccupiedError { entry, value }),
            Entry::Vacant(entry) => Ok(entry.insert(value)),
        }
    }

    /// Removes a key from the map, returning the value at the key if the key
    /// was previously in the map.
    ///
    /// The key may be any borrowed form of the map's key type, but
    /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for
    /// the key type.
    ///
    /// # Examples
    ///
    /// ```
    /// use compact_map::CompactMap;
    ///
    /// let mut map = CompactMap::default();
    /// map.insert(1, "a");
    /// assert_eq!(map.remove(&1), Some("a"));
    /// assert_eq!(map.remove(&1), None);
    /// ```
    #[inline]
    pub fn remove<Q>(&mut self, k: &Q) -> Option<V>
    where
        K: Borrow<Q>,
        Q: Hash + Eq + ?Sized,
    {
        self.base.remove(k)
    }

    /// Removes a key from the map, returning the stored key and value if the
    /// key was previously in the map.
    ///
    /// The key may be any borrowed form of the map's key type, but
    /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for
    /// the key type.
    ///
    /// # Examples
    ///
    /// ```
    /// use compact_map::CompactMap;
    ///
    /// # fn main() {
    /// let mut map = CompactMap::default();
    /// map.insert(1, "a");
    /// assert_eq!(map.remove_entry(&1), Some((1, "a")));
    /// assert_eq!(map.remove(&1), None);
    /// # }
    /// ```
    #[inline]
    pub fn remove_entry<Q>(&mut self, k: &Q) -> Option<(K, V)>
    where
        K: Borrow<Q>,
        Q: Hash + Eq + ?Sized,
    {
        self.base.remove_entry(k)
    }

    /// Converts the map into a [`HashMap`].
    ///
    /// If the map has spilled into a `HashMap`, this will return that `HashMap`.
    /// Otherwise, it will create a new `HashMap` and move all the entries into it.
    #[inline]
    pub fn into_hashmap(self) -> HashMap<K, V> {
        self.base.into_hashmap()
    }

    /// Converts the map into a [`HashMap`] with a given hasher.
    ///
    /// This will always create a new `HashMap` and move all the entries into it.
    ///
    /// See also [`HashMap::with_hasher`].
    #[inline]
    pub fn into_hashmap_with_hasher<S: BuildHasher>(self, hash_builder: S) -> HashMap<K, V, S> {
        let mut map = HashMap::with_capacity_and_hasher(self.len(), hash_builder);
        map.extend(self.base);
        map
    }

    /// Converts the map into a [`HashMap`] with at least the specified capacity, using
    /// `hasher` to hash the keys. The capacity will always be at least `self.len()`.
    ///
    /// This will always create a new `HashMap` and move all the entries into it.
    ///
    /// See also [`HashMap::with_capacity_and_hasher`].
    #[inline]
    pub fn into_hashmap_with_capacity_and_hasher<S: BuildHasher>(
        self,
        capacity: usize,
        hash_builder: S,
    ) -> HashMap<K, V, S> {
        let mut map = HashMap::with_capacity_and_hasher(capacity.max(self.len()), hash_builder);
        map.extend(self.base);
        map
    }
}

impl<K, V, const N: usize, const M: usize> PartialEq<CompactMap<K, V, M>> for CompactMap<K, V, N>
where
    K: Eq + Hash,
    V: PartialEq,
{
    fn eq(&self, other: &CompactMap<K, V, M>) -> bool {
        if self.len() != other.len() {
            return false;
        }

        self.iter()
            .all(|(key, value)| other.get(key).map_or(false, |v| *value == *v))
    }
}

impl<K, V, const N: usize> Eq for CompactMap<K, V, N>
where
    K: Eq + Hash,
    V: Eq,
{
}

impl<K, V, const N: usize> Debug for CompactMap<K, V, N>
where
    K: Debug,
    V: Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_map().entries(self.iter()).finish()
    }
}

impl<K, V> Default for CompactMap<K, V, DEFAULT_MAX_INLINE_ENTRIES> {
    fn default() -> Self {
        Self::new()
    }
}

impl<K, Q: ?Sized, V, const N: usize> Index<&Q> for CompactMap<K, V, N>
where
    K: Eq + Hash + Borrow<Q>,
    Q: Eq + Hash,
{
    type Output = V;

    /// Returns a reference to the value corresponding to the supplied key.
    ///
    /// # Panics
    ///
    /// Panics if the key is not present in the `CompactMap`.
    #[inline]
    fn index(&self, key: &Q) -> &V {
        self.get(key).expect("no entry found for key")
    }
}

impl<K, V, const N: usize, const M: usize> From<[(K, V); N]> for CompactMap<K, V, M>
where
    K: Eq + Hash,
{
    /// # Examples
    ///
    /// ```
    /// use compact_map::CompactMap;
    ///
    /// let map1: CompactMap<i32, i32, 16> = CompactMap::from([(1, 2), (3, 4)]);
    /// let map2: CompactMap<i32, i32, 32> = [(1, 2), (3, 4)].into();
    /// assert_eq!(map1, map2);
    /// ```
    fn from(arr: [(K, V); N]) -> Self {
        Self {
            base: base::MapImpl::from(arr),
        }
    }
}

/// An iterator over the keys of a `CompactMap`.
///
/// This `struct` is created by the [`keys`] method on [`CompactMap`]. See its
/// documentation for more.
///
/// [`keys`]: CompactMap::keys
///
/// # Example
///
/// ```
/// use compact_map::CompactMap;
///
/// let map: CompactMap<&str, i32, 16> = CompactMap::from([
///     ("a", 1),
/// ]);
/// let iter_keys = map.keys();
/// ```
pub struct Keys<'a, K, V, const N: usize> {
    pub(crate) inner: base::iter::IterInner<'a, K, V, N>,
}

impl<K, V, const N: usize> Clone for Keys<'_, K, V, N> {
    fn clone(&self) -> Self {
        Self {
            inner: self.inner.clone(),
        }
    }
}

impl<K: Debug, V, const N: usize> Debug for Keys<'_, K, V, N> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_list().entries(self.clone()).finish()
    }
}

impl<'a, K, V, const N: usize> Iterator for Keys<'a, K, V, N> {
    type Item = &'a K;

    #[inline]
    fn next(&mut self) -> Option<&'a K> {
        self.inner.next().map(|(k, _)| k)
    }
    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.inner.size_hint()
    }
    #[inline]
    fn count(self) -> usize {
        self.inner.len()
    }
    #[inline]
    fn fold<B, F>(self, init: B, mut f: F) -> B
    where
        Self: Sized,
        F: FnMut(B, Self::Item) -> B,
    {
        self.inner.fold(init, |acc, (k, _)| f(acc, k))
    }
}
impl<K, V, const N: usize> ExactSizeIterator for Keys<'_, K, V, N> {
    #[inline]
    fn len(&self) -> usize {
        self.inner.len()
    }
}
impl<K, V, const N: usize> FusedIterator for Keys<'_, K, V, N> {}

/// An owning iterator over the keys of a `CompactMap`.
///
/// This `struct` is created by the [`into_keys`] method on [`CompactMap`].
/// See its documentation for more.
///
/// [`into_keys`]: CompactMap::into_keys
///
/// # Example
///
/// ```
/// use compact_map::CompactMap;
///
/// let map: CompactMap<&str, i32, 16> = CompactMap::from([
///     ("a", 1),
/// ]);
/// let iter_keys = map.into_keys();
/// ```
pub struct IntoKeys<K, V, const N: usize> {
    pub(crate) inner: base::iter::IntoIterInner<K, V, N>,
}

impl<K, V, const N: usize> Iterator for IntoKeys<K, V, N> {
    type Item = K;

    #[inline]
    fn next(&mut self) -> Option<K> {
        self.inner.next().map(|(k, _)| k)
    }
    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.inner.size_hint()
    }
    #[inline]
    fn count(self) -> usize {
        self.inner.len()
    }
    #[inline]
    fn fold<B, F>(self, init: B, mut f: F) -> B
    where
        Self: Sized,
        F: FnMut(B, Self::Item) -> B,
    {
        self.inner.fold(init, |acc, (k, _)| f(acc, k))
    }
}
impl<K, V, const N: usize> ExactSizeIterator for IntoKeys<K, V, N> {
    #[inline]
    fn len(&self) -> usize {
        self.inner.len()
    }
}
impl<K, V, const N: usize> FusedIterator for IntoKeys<K, V, N> {}

/// An iterator over the values of a `CompactMap`.
///
/// This `struct` is created by the [`values`] method on [`CompactMap`]. See its
/// documentation for more.
///
/// [`values`]: CompactMap::values
///
/// # Example
///
/// ```
/// use compact_map::CompactMap;
///
/// let map: CompactMap<&str, i32, 16> = CompactMap::from([
///     ("a", 1),
/// ]);
/// let iter_values = map.values();
/// ```
pub struct Values<'a, K: 'a, V: 'a, const N: usize> {
    base: base::iter::IterInner<'a, K, V, N>,
}

impl<'a, K, V, const N: usize> Iterator for Values<'a, K, V, N> {
    type Item = &'a V;

    #[inline]
    fn next(&mut self) -> Option<&'a V> {
        self.base.next().map(|(_, v)| v)
    }
    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.base.size_hint()
    }
    #[inline]
    fn count(self) -> usize {
        self.base.len()
    }
    #[inline]
    fn fold<B, F>(self, init: B, mut f: F) -> B
    where
        Self: Sized,
        F: FnMut(B, Self::Item) -> B,
    {
        self.base.fold(init, |acc, (_, v)| f(acc, v))
    }
}
impl<K, V, const N: usize> ExactSizeIterator for Values<'_, K, V, N> {
    #[inline]
    fn len(&self) -> usize {
        self.base.len()
    }
}
impl<K, V, const N: usize> FusedIterator for Values<'_, K, V, N> {}

/// A mutable iterator over the values of a `CompactMap`.
///
/// This `struct` is created by the [`values_mut`] method on [`CompactMap`]. See its
/// documentation for more.
///
/// [`values_mut`]: CompactMap::values_mut
///
/// # Example
///
/// ```
/// use compact_map::CompactMap;
///
/// let mut map: CompactMap<&str, i32, 16> = CompactMap::from([
///     ("a", 1),
/// ]);
/// let iter_values = map.values_mut();
/// ```
pub struct ValuesMut<'a, K: 'a, V: 'a, const N: usize> {
    inner: base::iter::IterMutInner<'a, K, V, N>,
}

impl<'a, K, V, const N: usize> Iterator for ValuesMut<'a, K, V, N> {
    type Item = &'a mut V;

    #[inline]
    fn next(&mut self) -> Option<&'a mut V> {
        self.inner.next().map(|(_, v)| v)
    }
    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.inner.size_hint()
    }
    #[inline]
    fn count(self) -> usize {
        self.inner.len()
    }
    #[inline]
    fn fold<B, F>(self, init: B, mut f: F) -> B
    where
        Self: Sized,
        F: FnMut(B, Self::Item) -> B,
    {
        self.inner.fold(init, |acc, (_, v)| f(acc, v))
    }
}
impl<K, V, const N: usize> ExactSizeIterator for ValuesMut<'_, K, V, N> {
    #[inline]
    fn len(&self) -> usize {
        self.inner.len()
    }
}
impl<K, V, const N: usize> FusedIterator for ValuesMut<'_, K, V, N> {}

/// An owning iterator over the values of a `CompactMap`.
///
/// This `struct` is created by the [`into_values`] method on [`CompactMap`].
/// See its documentation for more.
///
/// [`into_values`]: HashMap::into_values
///
/// # Example
///
/// ```
/// use compact_map::CompactMap;
///
/// let map: CompactMap<&str, i32, 16> = CompactMap::from([
///     ("a", 1),
/// ]);
/// let iter_keys = map.into_values();
/// ```
pub struct IntoValues<K, V, const N: usize> {
    inner: base::iter::IntoIterInner<K, V, N>,
}

impl<K, V, const N: usize> Iterator for IntoValues<K, V, N> {
    type Item = V;

    #[inline]
    fn next(&mut self) -> Option<V> {
        self.inner.next().map(|(_, v)| v)
    }
    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.inner.size_hint()
    }
    #[inline]
    fn count(self) -> usize {
        self.inner.len()
    }
    #[inline]
    fn fold<B, F>(self, init: B, mut f: F) -> B
    where
        Self: Sized,
        F: FnMut(B, Self::Item) -> B,
    {
        self.inner.fold(init, |acc, (_, v)| f(acc, v))
    }
}
impl<K, V, const N: usize> ExactSizeIterator for IntoValues<K, V, N> {
    #[inline]
    fn len(&self) -> usize {
        self.inner.len()
    }
}
impl<K, V, const N: usize> FusedIterator for IntoValues<K, V, N> {}

/// An iterator over the entries of a `CompactMap`.
///
/// This `struct` is created by the [`iter`] method on [`CompactMap`]. See its
/// documentation for more.
///
/// [`iter`]: CompactMap::iter
///
/// # Example
///
/// ```
/// use compact_map::CompactMap;
///
/// let map: CompactMap<&str, i32, 16> = CompactMap::from([
///     ("a", 1),
/// ]);
/// let iter = map.iter();
/// ```
pub struct Iter<'a, K, V, const N: usize> {
    pub(crate) base: base::iter::IterInner<'a, K, V, N>,
}

impl<'a, K, V, const N: usize> Iterator for Iter<'a, K, V, N> {
    type Item = (&'a K, &'a V);

    #[inline]
    fn next(&mut self) -> Option<(&'a K, &'a V)> {
        self.base.next()
    }
    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.base.size_hint()
    }
    #[inline]
    fn count(self) -> usize {
        self.base.count()
    }
    #[inline]
    fn fold<B, F>(self, init: B, f: F) -> B
    where
        Self: Sized,
        F: FnMut(B, Self::Item) -> B,
    {
        self.base.fold(init, f)
    }
}
impl<'a, K, V, const N: usize> ExactSizeIterator for Iter<'a, K, V, N> {
    #[inline]
    fn len(&self) -> usize {
        self.base.len()
    }
}
impl<'a, K, V, const N: usize> FusedIterator for Iter<'a, K, V, N> {}

/// A mutable iterator over the entries of a `CompactMap`.
///
/// This `struct` is created by the [`iter_mut`] method on [`CompactMap`]. See its
/// documentation for more.
///
/// [`iter_mut`]: CompactMap::iter_mut
///
/// # Example
///
/// ```
/// use compact_map::CompactMap;
///
/// let mut map: CompactMap<&str, i32, 16> = CompactMap::from([
///     ("a", 1),
/// ]);
/// let iter = map.iter_mut();
/// ```
pub struct IterMut<'a, K: 'a, V: 'a, const N: usize> {
    base: base::iter::IterMutInner<'a, K, V, N>,
}

impl<'a, K, V, const N: usize> Iterator for IterMut<'a, K, V, N> {
    type Item = (&'a K, &'a mut V);

    #[inline]
    fn next(&mut self) -> Option<(&'a K, &'a mut V)> {
        self.base.next()
    }
    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.base.size_hint()
    }
    #[inline]
    fn count(self) -> usize {
        self.base.len()
    }
    #[inline]
    fn fold<B, F>(self, init: B, f: F) -> B
    where
        Self: Sized,
        F: FnMut(B, Self::Item) -> B,
    {
        self.base.fold(init, f)
    }
}
impl<K, V, const N: usize> ExactSizeIterator for IterMut<'_, K, V, N> {
    #[inline]
    fn len(&self) -> usize {
        self.base.len()
    }
}
impl<K, V, const N: usize> FusedIterator for IterMut<'_, K, V, N> {}

/// An owning iterator over the entries of a `CompactMap`.
///
/// This `struct` is created by the [`into_iter`] method on [`CompactMap`]
/// (provided by the [`IntoIterator`] trait). See its documentation for more.
///
/// [`into_iter`]: IntoIterator::into_iter
///
/// # Example
///
/// ```
/// use compact_map::CompactMap;
///
/// let map: CompactMap<&str, i32, 16> = CompactMap::from([
///     ("a", 1),
/// ]);
/// let iter = map.into_iter();
/// ```
pub struct IntoIter<K, V, const N: usize> {
    base: base::iter::IntoIterInner<K, V, N>,
}

impl<K: Debug, V: Debug, const N: usize> Debug for IntoIter<K, V, N> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.base.fmt(f)
    }
}

impl<K, V, const N: usize> Iterator for IntoIter<K, V, N> {
    type Item = (K, V);

    #[inline]
    fn next(&mut self) -> Option<(K, V)> {
        self.base.next()
    }
    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.base.size_hint()
    }
    #[inline]
    fn count(self) -> usize {
        self.base.len()
    }
    #[inline]
    fn fold<B, F>(self, init: B, f: F) -> B
    where
        Self: Sized,
        F: FnMut(B, Self::Item) -> B,
    {
        self.base.fold(init, f)
    }
}
impl<K, V, const N: usize> ExactSizeIterator for IntoIter<K, V, N> {
    #[inline]
    fn len(&self) -> usize {
        self.base.len()
    }
}
impl<K, V, const N: usize> FusedIterator for IntoIter<K, V, N> {}

impl<'a, K, V, const N: usize> IntoIterator for &'a CompactMap<K, V, N> {
    type Item = (&'a K, &'a V);
    type IntoIter = Iter<'a, K, V, N>;

    #[inline]
    fn into_iter(self) -> Iter<'a, K, V, N> {
        self.iter()
    }
}

impl<'a, K, V, const N: usize> IntoIterator for &'a mut CompactMap<K, V, N> {
    type Item = (&'a K, &'a mut V);
    type IntoIter = IterMut<'a, K, V, N>;

    #[inline]
    fn into_iter(self) -> IterMut<'a, K, V, N> {
        self.iter_mut()
    }
}

/// A draining iterator over the entries of a `CompactMap`.
///
/// This `struct` is created by the [`drain`] method on [`CompactMap`]. See its
/// documentation for more.
///
/// [`drain`]: CompactMap::drain
///
/// # Example
///
/// ```
/// use compact_map::CompactMap;
///
/// let mut map: CompactMap<&str, i32, 16> = CompactMap::from([
///     ("a", 1),
///     ("b", 2),
///     ("c", 3),
/// ]);
/// let iter = map.drain();
/// ```
pub struct Drain<'a, K: 'a, V: 'a, const N: usize> {
    base: base::drain::DrainInner<'a, K, V, N>,
}

impl<K: Debug, V: Debug, const N: usize> Debug for Drain<'_, K, V, N> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.base.fmt(f)
    }
}

impl<'a, K, V, const N: usize> Iterator for Drain<'a, K, V, N> {
    type Item = (K, V);

    #[inline]
    fn next(&mut self) -> Option<(K, V)> {
        self.base.next()
    }
    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.base.size_hint()
    }
    #[inline]
    fn count(self) -> usize {
        self.base.count()
    }
    #[inline]
    fn fold<B, F>(self, init: B, f: F) -> B
    where
        Self: Sized,
        F: FnMut(B, Self::Item) -> B,
    {
        self.base.fold(init, f)
    }
}
impl<K, V, const N: usize> ExactSizeIterator for Drain<'_, K, V, N> {
    #[inline]
    fn len(&self) -> usize {
        self.base.len()
    }
}
impl<K, V, const N: usize> FusedIterator for Drain<'_, K, V, N> {}

/// A draining, filtering iterator over the entries of a `CompactMap`.
///
/// This `struct` is created by the [`extract_if`] method on [`CompactMap`].
///
/// [`extract_if`]: CompactMap::extract_if
///
/// # Example
///
/// ```
/// use compact_map::CompactMap;
///
/// let mut map: CompactMap<&str, i32, 16> = CompactMap::from([
///     ("a", 1),
///     ("b", 2),
///     ("c", 3),
/// ]);
/// let iter = map.extract_if(|_k, v| *v % 2 == 0);
/// ```
#[cfg_attr(docsrs, doc(cfg(feature = "extract_if")))]
#[cfg(feature = "extract_if")]
#[must_use = "iterators are lazy and do nothing unless consumed"]
pub struct ExtractIf<'a, K, V, F, const N: usize>
where
    F: FnMut(&K, &mut V) -> bool,
{
    base: base::extract_if::ExtractIfInner<'a, K, V, F, N>,
}

#[cfg_attr(docsrs, doc(cfg(feature = "extract_if")))]
#[cfg(feature = "extract_if")]
impl<K, V, F, const N: usize> Iterator for ExtractIf<'_, K, V, F, N>
where
    F: FnMut(&K, &mut V) -> bool,
{
    type Item = (K, V);

    #[inline]
    fn next(&mut self) -> Option<(K, V)> {
        self.base.next()
    }
    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.base.size_hint()
    }
}

#[cfg_attr(docsrs, doc(cfg(feature = "extract_if")))]
#[cfg(feature = "extract_if")]
impl<K, V, F, const N: usize> FusedIterator for ExtractIf<'_, K, V, F, N> where
    F: FnMut(&K, &mut V) -> bool
{
}

#[cfg_attr(docsrs, doc(cfg(feature = "extract_if")))]
#[cfg(feature = "extract_if")]
impl<'a, K, V, F, const N: usize> Debug for ExtractIf<'a, K, V, F, N>
where
    F: FnMut(&K, &mut V) -> bool,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ExtractIf").finish_non_exhaustive()
    }
}

impl<K, V, const N: usize> IntoIterator for CompactMap<K, V, N> {
    type Item = (K, V);
    type IntoIter = IntoIter<K, V, N>;

    /// Creates a consuming iterator, that is, one that moves each key-value
    /// pair out of the map in arbitrary order. The map cannot be used after
    /// calling this.
    ///
    /// # Examples
    ///
    /// ```
    /// use compact_map::CompactMap;
    ///
    /// let map: CompactMap<&str, i32, 16> = CompactMap::from([
    ///     ("a", 1),
    ///     ("b", 2),
    ///     ("c", 3),
    /// ]);
    ///
    /// // Not possible with .iter()
    /// let vec: Vec<(&str, i32)> = map.into_iter().collect();
    /// ```
    #[inline]
    fn into_iter(self) -> IntoIter<K, V, N> {
        IntoIter {
            base: self.base.into_iter(),
        }
    }
}

impl<K, V, const N: usize> FromIterator<(K, V)> for CompactMap<K, V, N>
where
    K: Eq + Hash,
{
    fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self {
        let mut map = CompactMap::new();
        map.extend(iter);
        map
    }
}

impl<K, V, const N: usize> Extend<(K, V)> for CompactMap<K, V, N>
where
    K: Eq + Hash,
{
    fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T) {
        self.base.extend(iter);
    }
}