coca 0.3.0

Data structures with constant capacity
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
//! A map based on an [association list](https://en.wikipedia.org/wiki/Association_list).

use core::alloc::{Layout, LayoutError};
use core::borrow::Borrow;
use core::fmt::Debug;
use core::iter::FusedIterator;
use core::marker::PhantomData;
use core::mem::MaybeUninit;

use crate::storage::{Capacity, LayoutSpec, Storage};

use self::Entry::{Occupied, Vacant};

/// The [`LayoutSpec`] for a [`ListMap`].
pub struct ListMapLayout<K, V>(PhantomData<(K, V)>);
impl<K, V> LayoutSpec for ListMapLayout<K, V> {
    fn layout_with_capacity(items: usize) -> Result<Layout, LayoutError> {
        let keys_array = Layout::array::<K>(items)?;
        let values_array = Layout::array::<V>(items)?;
        let (extended, _) = keys_array.extend(values_array)?;
        Ok(extended.pad_to_align())
    }
}

/// A map based on an [association list](https://en.wikipedia.org/wiki/Association_list).
/// 
/// Conventionally, this refers to a linked list of key-value pairs, using a
/// linear scan to find the value associated with a given key. This is simple,
/// though inefficient, particularly on modern computer architectures, where
/// traversing each link in the list is likely to cause a cache miss.
/// 
/// For this reason, this implementation uses arrays instead, one for the keys,
/// one for the values. This way, unrelated values need not be fetched into the
/// cache during the key lookup. Nonetheless, this search is *O*(*n*), i.e. it
/// takes time linear in the number of entries, which can be problematic for
/// large maps.
/// 
/// Newly inserted entries are appended to the arrays, and a removed entry is
/// replaced by the last one in the list, meaning modifications have constant
/// overhead after the initial lookup. This also means insertion order is **not**
/// preserved.
/// 
/// As key search is the primary cost of these operations, care should be taken
/// to avoid redundant lookups. Use the [`Entry` API](ListMap::try_entry) where
/// applicable.
/// 
/// It is required that the keys implement the [`Eq`] trait, although this can
/// frequently be achieved using `#[derive(PartialEq, Eq)]`.
/// 
/// It is a logic error for a key to be modified in such a way that its equality,
/// as determined by the `Eq` trait, changes while it is in the map. This is
/// normally only possible through [`Cell`](core::cell::Cell),
/// [`RefCell`](core::cell::RefCell), global state, I/O, or unsafe code. The
/// behavior resulting from such a logic error is not specified, but will not
/// result in undefined behavior. This could include panics, incorrect results,
/// aborts, memory leaks, and non-termination.
pub struct ListMap<K, V, S: Storage<ListMapLayout<K, V>>, I: Capacity> {
    buf: S,
    len: I,
    pairs: PhantomData<(K, V)>,
}

impl<K, V, S: Storage<ListMapLayout<K, V>>, I: Capacity> From<S> for ListMap<K, V, S, I> {
    fn from(buf: S) -> Self {
        ListMap { buf, len: I::from_usize(0), pairs: PhantomData }
    }
}

impl<K, V, S: Storage<ListMapLayout<K, V>>, I: Capacity> ListMap<K, V, S, I> {
    /// Returns the number of entries the map can hold.
    #[inline]
    pub fn capacity(&self) -> usize {
        self.buf.capacity()
    }

    /// Returns a slice of all keys in the map in arbitrary order.
    /// 
    /// # Examples
    /// ```
    /// use coca::collections::InlineListMap;
    /// 
    /// let mut map = InlineListMap::<&'static str, u32, 4>::new();
    /// map.insert("a", 1);
    /// map.insert("b", 2);
    /// map.insert("c", 3);
    /// 
    /// for key in map.keys() {
    ///     println!("{}", key);
    /// }
    /// ```
    #[inline]
    pub fn keys(&self) -> &[K] {
        let ptr = self.buf.get_ptr().cast();
        unsafe { core::slice::from_raw_parts(ptr, self.len.as_usize()) }
    }

    #[inline(always)]
    fn values_offset(&self) -> usize {
        let cap = self.capacity();
        let keys_array = Layout::array::<K>(cap).unwrap();
        let values_array = Layout::array::<V>(cap).unwrap();
        let (_, offset) = keys_array.extend(values_array).unwrap();
        offset
    }

    /// Returns a slice of all values in the map in arbitrary order.
    /// 
    /// # Examples
    /// ```
    /// use coca::collections::InlineListMap;
    /// 
    /// let mut map = InlineListMap::<&'static str, u32, 4>::new();
    /// map.insert("a", 1);
    /// map.insert("b", 2);
    /// map.insert("c", 3);
    /// 
    /// for val in map.values() {
    ///     println!("{}", val);
    /// }
    /// ```
    #[inline]
    pub fn values(&self) -> &[V] {
        unsafe {
            let ptr = self.buf.get_ptr().add(self.values_offset()).cast();
            core::slice::from_raw_parts(ptr, self.len())
        }
    }

    /// Returns a mutable slice of all values in the map in arbitrary order.
    /// 
    /// # Examples
    /// ```
    /// use coca::collections::InlineListMap;
    /// 
    /// let mut map = InlineListMap::<&'static str, u32, 4>::new();
    /// map.insert("a", 1);
    /// map.insert("b", 2);
    /// map.insert("c", 3);
    /// 
    /// for val in map.values_mut() {
    ///     *val = *val * 2;
    /// }
    /// 
    /// assert_eq!(map.get("a"), Some(&2));
    /// assert_eq!(map.get("b"), Some(&4));
    /// assert_eq!(map.get("c"), Some(&6));
    /// ```
    #[inline]
    pub fn values_mut(&mut self) -> &mut [V] {
        unsafe {
            let ptr = self.buf.get_mut_ptr().add(self.values_offset()).cast();
            core::slice::from_raw_parts_mut(ptr, self.len())
        }
    }

    /// An iterator visiting all key-value pairs in arbitrary order.
    /// The iterator element type is `(&'a K, &'a V)`.
    /// 
    /// # Examples
    /// ```
    /// use coca::collections::InlineListMap;
    /// 
    /// let mut map = InlineListMap::<&'static str, u32, 4>::new();
    /// map.insert("a", 1);
    /// map.insert("b", 2);
    /// map.insert("c", 3);
    /// 
    /// for (key, val) in map.iter() {
    ///     println!("{} -> {}", key, val);
    /// }
    /// ```
    pub fn iter(&self) -> Iter<'_, K, V, S, I> {
        Iter { map: self, front: I::from_usize(0) }
    }

    /// 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 coca::collections::InlineListMap;
    /// 
    /// let mut map = InlineListMap::<&'static str, u32, 4>::new();
    /// map.insert("a", 1);
    /// map.insert("b", 2);
    /// map.insert("c", 3);
    /// 
    /// for (_, val) in map.iter_mut() {
    ///     *val *= 2;
    /// }
    /// 
    /// assert_eq!(map.get("a"), Some(&2));
    /// assert_eq!(map.get("b"), Some(&4));
    /// assert_eq!(map.get("c"), Some(&6));
    pub fn iter_mut(&mut self) -> IterMut<'_, K, V, S, I> {
        IterMut { map: self, front: I::from_usize(0) }
    }

    /// Returns the number of entries in the map.
    #[inline]
    pub fn len(&self) -> usize {
        self.len.as_usize()
    }

    /// Returns `true` if the map contains no entries, or `false` otherwise.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.len.as_usize() == 0
    }

    /// Returns `true` if the map contains the maximum number of entries it can hold, or `false` otherwise.
    #[inline]
    pub fn is_full(&self) -> bool {
        self.len.as_usize() == self.buf.capacity()
    }

    /// Clears the map without taking ownership, and returns all key-value pairs as an iterator.
    /// 
    /// If the iterator is only partially consumed, or not consumed at all,
    /// all remaining key-value pairs will still be removed.
    /// 
    /// It is unspecified how many pairs will be removed if a panic occurs while
    /// dropping an element, or if the [`Drain`] value is leaked.
    /// 
    /// # Examples
    /// ```
    /// use coca::collections::InlineListMap;
    /// 
    /// let mut map = InlineListMap::<&'static str, u32, 4>::new();
    /// map.insert("a", 1);
    /// map.insert("b", 2);
    /// 
    /// for (k, v) in map.drain().take(1) {
    ///     let a = k == "a" && v == 1;
    ///     let b = k == "b" && v == 2;
    ///     assert!(a || b);
    /// }
    /// 
    /// assert!(map.is_empty());
    /// ```
    pub fn drain(&mut self) -> Drain<'_, K, V, S, I> {
        Drain { map: self }
    }

    /// 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 `drain_filter` lets you mutate every value in the filter
    /// closure, regardless of whether you choose to keep or remove it.
    /// 
    /// If the iterator is only partially consumed, or not consumed at all,
    /// all remaining key-value pairs will still be subjected to the closure
    /// and removed and dropped if it returns true.
    /// 
    /// It is unspecified how many pairs will be subjected to the closure if a
    /// panic occurs in the closure, or a panic occurs while dropping an element,
    /// or if the [`DrainFilter`] value is leaked.
    /// 
    /// # Examples
    /// ```
    /// use coca::collections::{InlineListMap, InlineVec};
    /// 
    /// let mut map = InlineListMap::<u32, u32, 8>::new();
    /// (0..8).for_each(|x| { map.insert(x, x); });
    /// let drained = map.drain_filter(|k, v| { *v = *v * *v; k % 2 == 0 });
    /// 
    /// let mut evens = InlineVec::<u32, 4>::new();
    /// let mut odds = InlineVec::<u32, 4>::new();
    /// 
    /// evens.extend(drained.map(|(_x, x_squared)| x_squared));
    /// evens.sort_unstable();
    /// assert_eq!(evens, [0, 4, 16, 36]);
    /// 
    /// odds.extend(map.into_values());
    /// odds.sort_unstable();
    /// assert_eq!(odds, [1, 9, 25, 49]);
    /// ```
    pub fn drain_filter<F: FnMut(&K, &mut V) -> bool>(&mut self, pred: F) -> DrainFilter<'_, K, V, S, I, F> {
        DrainFilter { map: self, should_remove: pred, front: I::from_usize(0) }
    }

    /// Clears the map, removing all key-value pairs.
    /// 
    /// # Examples
    /// ```
    /// use coca::collections::InlineListMap;
    /// 
    /// let mut map = InlineListMap::<&'static str, u32, 4>::new();
    /// 
    /// map.insert("a", 1);
    /// map.insert("b", 2);
    /// map.insert("c", 3);
    /// map.insert("d", 4);
    /// assert!(map.is_full());
    /// 
    /// map.clear();
    /// assert!(map.is_empty());
    /// ```
    pub fn clear(&mut self) {
        unsafe {
            let keys = self.buf.get_mut_ptr().cast::<K>();
            let values = self.buf.get_mut_ptr().add(self.values_offset()).cast::<V>();

            for i in 0..self.len() {
                keys.add(i).drop_in_place();
                values.add(i).drop_in_place();
            }

            self.len = I::from_usize(0);
        }
    }

    /// Gets the given key's corresponding [`Entry`] in the map for in-place manipulation.
    /// 
    /// # Panics
    /// Panics if the map is full and does not contain the given key.
    /// See [`try_entry`](ListMap::try_entry) for a checked version that never panics.
    pub fn entry(&mut self, key: K) -> Entry<'_, K, V, S, I>
    where
        K: Eq
    {
        self.try_entry(key).ok().expect("map is already at capacity")
    }

    /// Gets the given key's corresponding [`Entry`] in the map for in-place manipulation.
    /// 
    /// Returns [`Err(key)`] if the map is full and does not contain the given key.
    /// 
    /// # Examples
    /// ```
    /// use coca::collections::InlineListMap;
    /// 
    /// let mut letters = InlineListMap::<char, u32, 32>::new();
    /// 
    /// for ch in "i am, therefore i'm coded".chars() {
    ///     let counter = letters.try_entry(ch).unwrap().or_insert(0);
    ///     *counter += 1;
    /// }
    /// 
    /// assert_eq!(letters.get(&'a'), Some(&1));
    /// assert_eq!(letters.get(&'e'), Some(&4));
    /// assert_eq!(letters.get(&'i'), Some(&2));
    /// assert_eq!(letters.get(&'o'), Some(&2));
    /// assert_eq!(letters.get(&'u'), None);
    /// ```
    pub fn try_entry(&mut self, key: K) -> Result<Entry<'_, K, V, S, I>, K>
    where
        K: Eq
    {
        if let Some((idx, _)) = self.lookup(&key) {
            Ok(Occupied(OccupiedEntry { key, idx, map: self, }))
        } else if self.is_full() {
            Err(key)
        } else {
            Ok(Vacant(VacantEntry { key, map: self }))
        }
    }

    #[inline(always)]
    fn lookup<Q>(&self, key: &Q) -> Option<(usize, &K)>
    where
        K: Borrow<Q> + Eq,
        Q: Eq + ?Sized,
    {
        self.keys().iter().enumerate().find(|(_, k)| (*k).borrow() == key)
    }

    /// Returns a reference to the value associated with the given key.
    /// 
    /// The key may be any borrowed form of the map's key type,
    /// but `Eq` on the borrowed form *must* match that for the key type.
    /// 
    /// # Examples
    /// ```
    /// use coca::collections::InlineListMap;
    /// 
    /// let mut map = InlineListMap::<&'static str, u32, 4>::new();
    /// map.insert("a", 1);
    /// 
    /// assert_eq!(map.get("a"), Some(&1));
    /// assert_eq!(map.get("b"), None);
    /// ```
    pub fn get<Q>(&self, key: &Q) -> Option<&V>
    where
        K: Borrow<Q> + Eq,
        Q: Eq + ?Sized,
    {
        let (idx, _) = self.lookup(key)?;
        Some(&self.values()[idx])
    }

    /// Returns the key-value pair corresponding to the given key.
    /// 
    /// The key may be any borrowed form of the map's key type,
    /// but `Eq` on the borrowed form *must* match that for the key type.
    /// 
    /// # Examples
    /// ```
    /// use coca::collections::InlineListMap;
    /// 
    /// let mut map = InlineListMap::<&'static str, u32, 4>::new();
    /// map.insert("a", 1);
    /// 
    /// assert_eq!(map.get_key_value("a"), Some((&"a", &1)));
    /// assert_eq!(map.get_key_value("b"), None);
    /// ```
    pub fn get_key_value<Q>(&self, key: &Q) -> Option<(&K, &V)>
    where
        K: Borrow<Q> + Eq,
        Q: Eq + ?Sized,
    {
        let (idx, k) = self.lookup(key)?;
        Some((k, &self.values()[idx]))
    }

    /// Returns `true` if the map contains a value for the given key, or `false` otherwise.
    /// 
    /// The key may be any borrowed form of the map's key type,
    /// but `Eq` on the borrowed form *must* match that for the key type.
    /// 
    /// # Examples
    /// ```
    /// use coca::collections::InlineListMap;
    /// 
    /// let mut map = InlineListMap::<&'static str, u32, 4>::new();
    /// map.insert("a", 1);
    /// 
    /// assert_eq!(map.contains_key("a"), true);
    /// assert_eq!(map.contains_key("b"), false);
    /// ```
    pub fn contains_key<Q>(&self, key: &Q) -> bool
    where
        K: Borrow<Q> + Eq,
        Q: Eq + ?Sized,
    {
        self.lookup(key).is_some()
    }

    /// Returns a mutable reference to the value associated with the given key.
    /// 
    /// The key may be any borrowed form of the map's key type,
    /// but `Eq` on the borrowed form *must* match that for the key type.
    /// 
    /// # Examples
    /// ```
    /// use coca::collections::InlineListMap;
    /// 
    /// let mut map = InlineListMap::<&'static str, u32, 4>::new();
    /// map.insert("a", 1);
    /// 
    /// if let Some(x) = map.get_mut(&"a") {
    ///     *x = *x + 2;
    /// }
    /// 
    /// assert_eq!(map.get("a"), Some(&3));
    /// ```
    pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
    where
        K: Borrow<Q> + Eq,
        Q: Eq + ?Sized,
    {
        let (idx, _) = self.lookup(key)?;
        Some(&mut self.values_mut()[idx])
    }

    /// 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.
    /// 
    /// # Panics
    /// Panics if the map is full and the given key is not present. See
    /// [`try_insert`](ListMap::try_insert) for a checked version that never panics.
    #[inline]
    #[track_caller]
    pub fn insert(&mut self, key: K, value: V) -> Option<V> where K: Eq {
        self.try_insert(key, value).ok().expect("map is already at capacity")
    }

    /// Inserts a key-value pair into the map.
    /// 
    /// If the map did not have this key present, `Ok(None)` is returned if the
    /// key-value pair is inserted, or [`Err((key, value))`] if the map is full.
    /// 
    /// 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.
    /// 
    /// # Examples
    /// ```
    /// use coca::collections::InlineListMap;
    /// 
    /// let mut map = InlineListMap::<&'static str, u32, 4>::new();
    /// assert_eq!(map.try_insert("a", 37), Ok(None));
    /// assert_eq!(map.try_insert("a", 42), Ok(Some(37)));
    /// 
    /// map.insert("b", 23);
    /// map.insert("c", 19);
    /// map.insert("d", 8);
    /// assert_eq!(map.is_full(), true);
    /// assert_eq!(map.try_insert("e", 0), Err(("e", 0)));
    /// ```
    pub fn try_insert(&mut self, key: K, value: V) -> Result<Option<V>, (K, V)> where K: Eq {
        if let Some((idx, _)) = self.lookup(&key) {
            return Ok(Some(core::mem::replace(&mut self.values_mut()[idx], value)));
        } else if self.is_full() {
            return Err((key, value));
        }

        let idx = self.len();
        
        unsafe {
            let buf_ptr = self.buf.get_mut_ptr();
            
            let key_ptr = buf_ptr.cast::<K>().add(idx);
            key_ptr.write(key);
            
            let value_ptr = buf_ptr.add(self.values_offset()).cast::<V>().add(idx);
            value_ptr.write(value);
        }
        
        self.len = I::from_usize(idx + 1);
        Ok(None)
    }

    /// Removes a key from the map, returning the value associated with the key
    /// if it was previously in the map.
    /// 
    /// The key may be any borrowed form of the map's key type,
    /// but [`Eq`] on the borrowed form *must* match that for the key type.
    /// 
    /// # Examples
    /// ```
    /// use coca::collections::InlineListMap;
    /// 
    /// let mut map = InlineListMap::<&'static str, u32, 4>::new();
    /// map.insert("a", 1);
    /// 
    /// assert_eq!(map.remove("a"), Some(1));
    /// assert_eq!(map.remove("a"), None);
    /// ```
    pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
    where
        K: Borrow<Q> + Eq,
        Q: Eq + ?Sized,
    {
        let (idx, _) = self.lookup(key)?;
        let new_len = self.len() - 1;

        unsafe {
            let buf_ptr = self.buf.get_mut_ptr();

            let keys = buf_ptr.cast::<K>();
            keys.add(idx).drop_in_place();

            let values = buf_ptr.add(self.values_offset()).cast::<V>();
            let result = values.add(idx).read();

            if idx != new_len {
                core::ptr::copy_nonoverlapping(keys.add(new_len), keys.add(idx), 1);
                core::ptr::copy_nonoverlapping(values.add(new_len), values.add(idx), 1);
            }

            self.len = I::from_usize(new_len);
            Some(result)
        }
    }

    /// Removes a key from the map, returning the stored key and associated
    /// value if the key was previously in the map.
    /// 
    /// The key may be any borrowed form of the map's key type,
    /// but [`Eq`] on the borrowed form *must* match that for the key type.
    /// 
    /// # Examples
    /// ```
    /// use coca::collections::InlineListMap;
    /// 
    /// let mut map = InlineListMap::<&'static str, u32, 4>::new();
    /// map.insert("a", 1);
    /// 
    /// assert_eq!(map.remove_entry("a"), Some(("a", 1)));
    /// assert_eq!(map.remove_entry("a"), None);
    /// ```
    pub fn remove_entry<Q>(&mut self, key: &Q) -> Option<(K, V)>
    where
        K: Borrow<Q> + Eq,
        Q: Eq + ?Sized,
    {
        let (idx, _) = self.lookup(key)?;
        let new_len = self.len() - 1;

        unsafe {
            let buf_ptr = self.buf.get_mut_ptr();

            let keys = buf_ptr.cast::<K>();
            let k = keys.add(idx).read();

            let values = buf_ptr.add(self.values_offset()).cast::<V>();
            let v = values.add(idx).read();

            if idx != new_len {
                core::ptr::copy_nonoverlapping(keys.add(new_len), keys.add(idx), 1);
                core::ptr::copy_nonoverlapping(values.add(new_len), values.add(idx), 1);
            }

            self.len = I::from_usize(new_len);
            Some((k, v))
        }
    }

    /// Retains only the elements specified by the predicate.
    /// 
    /// In other words, remove all key-value pairs `(k, v)` such that `pred(&k, &mut v)`
    /// returns `false`. The elements are visited in arbitrary (and unspecified) order.
    /// 
    /// # Examples
    /// ```
    /// use coca::collections::InlineListMap;
    /// 
    /// let mut map = InlineListMap::<u32, u32, 8>::new();
    /// (0..8).for_each(|x| { map.insert(x, x*10); });
    /// assert_eq!(map.len(), 8);
    /// 
    /// map.retain(|&k, _| k % 2 == 0);
    /// assert_eq!(map.len(), 4);
    /// ```
    pub fn retain<F: FnMut(&K, &mut V) -> bool>(&mut self, mut pred: F) {
        self.drain_filter(|k, v| !(pred)(k, v)).for_each(drop);
    }

    /// Creates a consuming iterator visiting all keys in arbitrary order.
    /// The map cannot be used after calling this. The iterator element type is `K`.
    /// 
    /// # Examples
    /// ```
    /// use coca::collections::{InlineListMap, InlineVec};
    /// 
    /// let mut map = InlineListMap::<&'static str, u32, 4>::new();
    /// map.insert("a", 1);
    /// map.insert("b", 2);
    /// map.insert("c", 3);
    /// 
    /// let mut vec = InlineVec::<&'static str, 4>::new();
    /// vec.extend(map.into_keys());
    /// // The keys are visited in arbitrary order,
    /// // so they must be sorted for this test.
    /// vec.sort_unstable();
    /// assert_eq!(vec, ["a", "b", "c"]);
    /// ```
    pub fn into_keys(self) -> IntoKeys<K, V, S, I> {
        IntoKeys { base: self.into_iter() }
    }

    /// Creates a consuming iterator visiting all values in arbitrary order.
    /// The map cannot be used after calling this. The iterator element type is `K`.
    /// 
    /// # Examples
    /// ```
    /// use coca::collections::{InlineListMap, InlineVec};
    /// 
    /// let mut map = InlineListMap::<&'static str, u32, 4>::new();
    /// map.insert("a", 1);
    /// map.insert("b", 2);
    /// map.insert("c", 3);
    /// 
    /// let mut vec = InlineVec::<u32, 4>::new();
    /// vec.extend(map.into_values());
    /// // The values are visited in arbitrary order,
    /// // so they must be sorted for this test.
    /// vec.sort_unstable();
    /// assert_eq!(vec, [1, 2, 3]);
    /// ```
    pub fn into_values(self) -> IntoValues<K, V, S, I> {
        IntoValues { base: self.into_iter() }
    }
}

impl<K, V, S: Storage<ListMapLayout<K, V>>, I: Capacity> Drop for ListMap<K, V, S, I> {
    fn drop(&mut self) {
        self.clear();
    }
}

impl<K: Debug, V: Debug, S: Storage<ListMapLayout<K, V>>, I: Capacity> Debug for ListMap<K, V, S, I> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_map().entries(self.iter()).finish()
    }
}

impl<Q, K, V, S, I> core::ops::Index<&'_ Q> for ListMap<K, V, S, I>
where
    Q: Eq + ?Sized,
    K: Eq + Borrow<Q>,
    S: Storage<ListMapLayout<K, V>>,
    I: Capacity,
{
    type Output = V;

    fn index(&self, key: &Q) -> &V {
        self.get(key).expect("no entry found for key")
    }
}

impl<K, V, S, I> Extend<(K, V)> for ListMap<K, V, S, I>
where
    K: Eq,
    S: Storage<ListMapLayout<K, V>>,
    I: Capacity
{
    fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T) {
        let iter = iter.into_iter();
        iter.for_each(move |(k, v)| { self.insert(k, v); });
    }
}

impl<'a, K, V, S, I> Extend<(&'a K, &'a V)> for ListMap<K, V, S, I>
where
    K: Clone + Eq,
    V: Clone,
    S: Storage<ListMapLayout<K, V>>,
    I: Capacity
{
    fn extend<T: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iter: T) {
        let iter = iter.into_iter();
        iter.for_each(|(k, v)| {
            self.insert(k.clone(), v.clone());
        });
    }
}

impl<K, V, S1, I1, S2, I2> PartialEq<ListMap<K, V, S2, I2>> for ListMap<K, V, S1, I1>
where
    K: Eq,
    V: PartialEq,
    S1: Storage<ListMapLayout<K, V>>,
    S2: Storage<ListMapLayout<K, V>>,
    I1: Capacity,
    I2: Capacity,
{
    /// Tests for `self` and `other` to be equal, and is used by `==`.
    /// 
    /// Note that this is *O*(1) if the two maps have different sizes,
    /// but *O*(*n*²) if they are the same size.
    fn eq(&self, other: &ListMap<K, V, S2, I2>) -> bool {
        if self.len() != other.len() {
            return false;
        }

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

impl<K: Eq, V: Eq, S: Storage<ListMapLayout<K, V>>, I: Capacity> Eq for ListMap<K, V, S, I> {}

#[cfg(feature = "alloc")]
#[cfg_attr(docs_rs, doc(cfg(feature = "alloc")))]
impl<K, V, I: Capacity> crate::collections::AllocListMap<K, V, I> {
    /// Constructs a new, empty [`AllocListMap`](crate::collections::AllocListMap)
    /// with the specified capacity.
    pub fn with_capacity(capacity: usize) -> Self {
        Self::from(crate::storage::AllocStorage::with_capacity(capacity))
    }
}

#[cfg(feature = "alloc")]
#[cfg_attr(docs_rs, doc(cfg(feature = "alloc")))]
impl<K: Clone, V: Clone, I: Capacity> Clone for crate::collections::AllocListMap<K, V, I> {
    fn clone(&self) -> Self {
        let buf = crate::storage::AllocStorage::with_capacity(self.capacity());
        let mut result = ListMap {
            buf, len: self.len, pairs: PhantomData
        };

        unsafe {
            let base_ptr = result.buf.get_mut_ptr();
            let keys_ptr = base_ptr.cast::<K>();
            let values_ptr = base_ptr.add(result.values_offset()).cast::<V>();

            for (idx, (k, v)) in self.iter().enumerate() {
                keys_ptr.add(idx).write(k.clone());
                values_ptr.add(idx).write(v.clone());
            }
        }

        result
    }
}

/// A statically-sized storage block for a [`ListMap`].
#[repr(C)]
pub struct InlineStorage<K, V, const N: usize> {
    keys: [MaybeUninit<K>; N],
    values: [MaybeUninit<V>; N],
}

unsafe impl<K, V, const N: usize> Storage<ListMapLayout<K, V>> for InlineStorage<K, V, N> {
    fn get_ptr(&self) -> *const u8 {
        (self as *const Self).cast()
    }

    fn get_mut_ptr(&mut self) -> *mut u8 {
        (self as *mut Self).cast()
    }

    fn capacity(&self) -> usize {
        N
    }
}

impl<K, V, I: Capacity, const N: usize> ListMap<K, V, InlineStorage<K, V, N>, I> {
    /// Constructs a new, empty [`InlineListMap`](crate::collections::InlineListMap).
    pub fn new() -> Self {
        let buf = unsafe { InlineStorage {
            keys: MaybeUninit::uninit().assume_init(),
            values: MaybeUninit::uninit().assume_init(),
        }};

        Self::from(buf)
    }
}

impl<K, V, I: Capacity, const N: usize> Default for ListMap<K, V, InlineStorage<K, V, N>, I> {
    fn default() -> Self {
        Self::new()
    }
}

impl<K: Clone, V: Clone, I: Capacity, const N: usize> Clone for ListMap<K, V, InlineStorage<K, V, N>, I> {
    fn clone(&self) -> Self {
        let buf = unsafe { InlineStorage {
            keys: MaybeUninit::uninit().assume_init(),
            values: MaybeUninit::uninit().assume_init(),
        }};

        let mut result = ListMap {
            buf, len: self.len, pairs: PhantomData,
        };

        unsafe {
            let base_ptr = result.buf.get_mut_ptr();
            let keys_ptr = base_ptr.cast::<K>();
            let values_ptr = base_ptr.add(result.values_offset()).cast::<V>();

            for (idx, (k, v)) in self.iter().enumerate() {
                keys_ptr.add(idx).write(k.clone());
                values_ptr.add(idx).write(v.clone());
            }
        }

        result
    }
}

/// A view into an occupied entry in a [`ListMap`]. It is part of the [`Entry`] enum.
pub struct OccupiedEntry<'a, K, V, S: Storage<ListMapLayout<K, V>>, I: Capacity> {
    key: K,
    idx: usize,
    map: &'a mut ListMap<K, V, S, I>,
}

impl<'a, K, V, S: Storage<ListMapLayout<K, V>>, I: Capacity> OccupiedEntry<'a, K, V, S, I> {
    /// Gets a reference to the key used to create the entry.
    pub fn key(&self) -> &K {
        &self.key
    }

    /// Take the ownership of the key and value from the map.
    /// 
    /// # Examples
    /// ```
    /// use coca::collections::InlineListMap;
    /// use coca::collections::list_map::Entry;
    /// 
    /// let mut map = InlineListMap::<&'static str, u32, 4>::new();
    /// 
    /// map.entry("foobar").or_insert(12);
    /// assert_eq!(map.get("foobar"), Some(&12));
    /// 
    /// if let Entry::Occupied(o) = map.entry("foobar") {
    ///     o.remove_entry();
    /// }
    /// 
    /// assert_eq!(map.contains_key("foobar"), false);
    /// ```
    pub fn remove_entry(self) -> (K, V) {
        unsafe {
            let base_ptr = self.map.buf.get_mut_ptr();

            let keys_ptr = base_ptr.cast::<K>();
            let k = keys_ptr.add(self.idx).read();

            let values_ptr = base_ptr.add(self.map.values_offset()).cast::<V>();
            let v = values_ptr.add(self.idx).read();

            let new_len = self.map.len() - 1;
            if self.idx != new_len {
                core::ptr::copy_nonoverlapping(keys_ptr.add(new_len), keys_ptr.add(self.idx), 1);
                core::ptr::copy_nonoverlapping(values_ptr.add(new_len), values_ptr.add(self.idx), 1);
            }

            self.map.len = I::from_usize(new_len);
            (k , v)
        }
    }

    /// Gets a reference to the value in the entry.
    /// 
    /// # Examples
    /// ```
    /// use coca::collections::InlineListMap;
    /// use coca::collections::list_map::Entry;
    /// 
    /// let mut map = InlineListMap::<&'static str, u32, 4>::new();
    /// 
    /// map.entry("foobar").or_insert(12);
    /// assert_eq!(map.get("foobar"), Some(&12));
    /// 
    /// if let Entry::Occupied(o) = map.entry("foobar") {
    ///     assert_eq!(o.get(), &12);
    /// }
    /// ```
    pub fn get(&self) -> &V {
        unsafe {
            let base_ptr = self.map.buf.get_ptr();
            let values_ptr = base_ptr.add(self.map.values_offset()).cast::<V>();
            &*values_ptr.add(self.idx)
        }
    }

    /// Gets a mutable reference to the value in the entry.
    /// 
    /// If you need a reference to the value which may outlive the `Entry`,
    /// see [`into_mut`](OccupiedEntry::into_mut).
    /// 
    /// # Examples
    /// ```
    /// use coca::collections::InlineListMap;
    /// use coca::collections::list_map::Entry;
    /// 
    /// let mut map = InlineListMap::<&'static str, u32, 4>::new();
    /// 
    /// map.entry("foobar").or_insert(12);
    /// assert_eq!(map.get("foobar"), Some(&12));
    /// 
    /// if let Entry::Occupied(mut o) = map.entry("foobar") {
    ///     *o.get_mut() += 10;
    ///     assert_eq!(*o.get(), 22);
    /// 
    ///     // You can use the same Entry multiple times:
    ///     *o.get_mut() += 2;
    /// }
    /// 
    /// assert_eq!(map.get("foobar"), Some(&24));
    /// ```
    pub fn get_mut(&mut self) -> &mut V {
        unsafe {
            let base_ptr = self.map.buf.get_mut_ptr();
            let values_ptr = base_ptr.add(self.map.values_offset()).cast::<V>();
            &mut *values_ptr.add(self.idx)
        }
    }

    /// Converts the `OccupiedEntry` into a mutable reference to the value
    /// in the entry with a lifetime bound to the map itself.
    /// 
    /// If you need multiple references to the `OccupiedEntry`,
    /// see [`get_mut`](OccupiedEntry::get_mut).
    /// 
    /// # Examples
    /// ```
    /// use coca::collections::InlineListMap;
    /// use coca::collections::list_map::Entry;
    /// 
    /// let mut map = InlineListMap::<&'static str, u32, 4>::new();
    /// 
    /// map.entry("foobar").or_insert(12);
    /// assert_eq!(map.get("foobar"), Some(&12));
    /// 
    /// if let Entry::Occupied(o) = map.entry("foobar") {
    ///     *o.into_mut() += 10;
    /// }
    /// 
    /// assert_eq!(map.get("foobar"), Some(&22));
    /// ```
    pub fn into_mut(self) -> &'a mut V {
        unsafe {
            let base_ptr = self.map.buf.get_mut_ptr();
            let values_ptr = base_ptr.add(self.map.values_offset()).cast::<V>();
            &mut *values_ptr.add(self.idx)
        }
    }

    /// Sets the value of the entry, and returns the entry's old value.
    /// 
    /// # Examples
    /// ```
    /// use coca::collections::InlineListMap;
    /// use coca::collections::list_map::Entry;
    /// 
    /// let mut map = InlineListMap::<&'static str, u32, 4>::new();
    /// 
    /// map.entry("foobar").or_insert(12);
    /// assert_eq!(map.get("foobar"), Some(&12));
    /// 
    /// if let Entry::Occupied(mut o) = map.entry("foobar") {
    ///     assert_eq!(o.insert(15), 12);
    /// }
    /// 
    /// assert_eq!(map.get("foobar"), Some(&15));
    /// ```
    pub fn insert(&mut self, value: V) -> V {
        unsafe {
            let base_ptr = self.map.buf.get_mut_ptr();
            let values_ptr = base_ptr.add(self.map.values_offset()).cast::<V>();
            values_ptr.add(self.idx).replace(value)
        }
    }

    /// Takes the value out of the entry, and returns it.
    /// 
    /// # Examples
    /// ```
    /// use coca::collections::InlineListMap;
    /// use coca::collections::list_map::Entry;
    /// 
    /// let mut map = InlineListMap::<&'static str, u32, 4>::new();
    /// 
    /// map.entry("foobar").or_insert(12);
    /// assert_eq!(map.get("foobar"), Some(&12));
    /// 
    /// if let Entry::Occupied(o) = map.entry("foobar") {
    ///     assert_eq!(o.remove(), 12);
    /// }
    /// 
    /// assert_eq!(map.contains_key("foobar"), false);
    /// ```
    pub fn remove(self) -> V {
        unsafe {
            let base_ptr = self.map.buf.get_mut_ptr();

            let values_ptr = base_ptr.add(self.map.values_offset()).cast::<V>();
            let value = values_ptr.add(self.idx).read();
            
            let new_len = self.map.len() - 1;
            if self.idx != new_len {
                let keys_ptr = base_ptr.cast::<K>();
                core::ptr::copy_nonoverlapping(keys_ptr.add(new_len), keys_ptr.add(self.idx), 1);
                core::ptr::copy_nonoverlapping(values_ptr.add(new_len), values_ptr.add(self.idx), 1);
            }

            self.map.len = I::from_usize(new_len);
            value
        }
    }

    /// Replaces the entry, returning the old key-value pair. The new key in the map will be the key used to create the entry.
    /// 
    /// # Examples
    /// ```
    /// use coca::collections::InlineListMap;
    /// use coca::collections::list_map::Entry;
    /// 
    /// let mut map = InlineListMap::<&'static str, u32, 4>::new();
    /// map.insert("foobar", 15);
    /// 
    /// if let Entry::Occupied(entry) = map.entry("foobar") {
    ///     let (old_key, old_value) = entry.replace_entry(16);
    ///     
    ///     assert_eq!(old_key, "foobar");
    ///     assert_eq!(old_value, 15);
    /// }
    /// 
    /// assert_eq!(map.get("foobar"), Some(&16));
    /// ```
    pub fn replace_entry(self, value: V) -> (K, V) {
        unsafe {
            let base_ptr = self.map.buf.get_mut_ptr();

            let keys_ptr = base_ptr.cast::<K>();
            let k = keys_ptr.add(self.idx).replace(self.key);

            let values_ptr = base_ptr.add(self.map.values_offset()).cast::<V>();
            let v = values_ptr.add(self.idx).replace(value);

            (k, v)
        }
    }

    /// Replaces the key in the map with the one used to create the entry.
    /// 
    /// This matters for key types that can be `==` without being identical.
    pub fn replace_key(self) -> K {
        unsafe {
            let keys_ptr = self.map.buf.get_mut_ptr().cast::<K>();
            keys_ptr.add(self.idx).replace(self.key)
        }
    }
}

impl<K: Debug, V: Debug, S: Storage<ListMapLayout<K, V>>, I: Capacity> Debug for OccupiedEntry<'_, K, V, S, I> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("OccupiedEntry")
            .field("key", &self.key)
            .field("value", self.get())
            .finish()
    }
}

/// A view into a vacant entry in a [`ListMap`]. It is part of the [`Entry`] enum.
pub struct VacantEntry<'a, K, V, S: Storage<ListMapLayout<K, V>>, I: Capacity> {
    key: K,
    map: &'a mut ListMap<K, V, S, I>,
}

impl<'a, K, V, S: Storage<ListMapLayout<K, V>>, I: Capacity> VacantEntry<'a, K, V, S, I> {
    /// Gets a reference to the key that would be used when inserting through the `VacantEntry`.
    pub fn key(&self) -> &K {
        &self.key
    }

    /// Take ownership of the key.
    pub fn into_key(self) -> K {
        self.key
    }

    /// Sets the value of the entry with the `VacantEntry`'s key, and returns a mutable reference to it.
    pub fn insert(self, value: V) -> &'a mut V {
        unsafe {
            let len = self.map.len();
            let base_ptr = self.map.buf.get_mut_ptr();

            let keys_ptr = base_ptr.cast::<K>();
            keys_ptr.add(len).write(self.key);

            let values_ptr = base_ptr.add(self.map.values_offset()).cast::<V>();
            let v_ptr = values_ptr.add(len);
            v_ptr.write(value);

            self.map.len = I::from_usize(len + 1);
            &mut *v_ptr
        }
    }
}

impl<K: Debug, V: Debug, S: Storage<ListMapLayout<K, V>>, I: Capacity> Debug for VacantEntry<'_, K, V, S, I> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_tuple("VacantEntry").field(&self.key).finish()
    }
}

/// A view into a single entry in a map, which may be either vacant or occupied.
/// 
/// This `enum` is constructed from the [`try_entry`](ListMap::try_entry) method on [`ListMap`].
pub enum Entry<'a, K, V, S: Storage<ListMapLayout<K, V>>, I: Capacity> {
    /// An occupied entry.
    Occupied(OccupiedEntry<'a, K, V, S, I>),
    /// A vacant entry.
    Vacant(VacantEntry<'a, K, V, S, I>),
}

impl<'a, K, V, S: Storage<ListMapLayout<K, V>>, I: Capacity> Entry<'a, K, V, S, I> {
    /// Ensures a value is in the entry by inserting the `default` if empty,
    /// and returns a mutable reference to the value in the entry.
    /// 
    /// # Examples
    /// ```
    /// use coca::collections::InlineListMap;
    /// 
    /// let mut map = InlineListMap::<&'static str, u32, 4>::new();
    ///
    /// map.entry("foobar").or_insert(3);
    /// assert_eq!(map.get("foobar"), Some(&3));
    /// 
    /// *map.entry("foobar").or_insert(5) *= 2;
    /// assert_eq!(map.get("foobar"), Some(&6));
    /// ```
    pub fn or_insert(self, default: V) -> &'a mut V {
        match self {
            Occupied(entry) => entry.into_mut(),
            Vacant(entry) => entry.insert(default),
        }
    }

    /// Ensures a value is in the entry by inserting the result of the `default`
    /// function if empty, and returns a mutable reference to the value in the entry.
    /// 
    /// # Examples
    /// ```
    /// use coca::collections::InlineListMap;
    /// 
    /// let mut map = InlineListMap::<&'static str, u32, 4>::new();
    /// let bazz = 0xDEADBEEF;
    /// 
    /// map.entry("foobar").or_insert_with(|| bazz);
    /// assert_eq!(map.get("foobar"), Some(&bazz));
    /// ```
    pub fn or_insert_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V {
        match self {
            Occupied(entry) => entry.into_mut(),
            Vacant(entry) => entry.insert(default()),
        }
    }

    /// Ensures a value is in the entry by inserting, if empty, the result of the default function.
    /// This method allows for generating key-derived values for insertion by providing the default
    /// function a reference to the key that was moved during the `.entry(key)` method call.
    ///
    /// The reference to the moved key is provided so that cloning or copying the key is
    /// unnecessary, unlike with `.or_insert_with(|| ... )`.
    ///
    /// # Examples
    /// ```
    /// use coca::collections::InlineListMap;
    /// 
    /// let mut map = InlineListMap::<&'static str, usize, 4>::new();
    /// 
    /// map.entry("foobar").or_insert_with_key(|key| key.chars().count());
    /// 
    /// assert_eq!(map.get("foobar"), Some(&6));
    /// ```
    pub fn or_insert_with_key<F: FnOnce(&K) -> V>(self, default: F) -> &'a mut V {
        match self {
            Occupied(entry) => entry.into_mut(),
            Vacant(entry) => {
                let value = default(entry.key());
                entry.insert(value)
            }
        }
    }

    /// Returns a reference to the key used to create the entry.
    pub fn key(&self) -> &K {
        match *self {
            Occupied(ref entry) => entry.key(),
            Vacant(ref entry) => entry.key(),
        }
    }

    /// Provides in-place mutable access to an occupied entry before any
    /// potential inserts into the map.
    ///
    /// # Examples
    /// ```
    /// use coca::collections::InlineListMap;
    /// 
    /// let mut map = InlineListMap::<&'static str, u32, 4>::new();
    /// 
    /// map.entry("foobar")
    ///     .and_modify(|v| { *v += 1 })
    ///     .or_insert(37);
    /// assert_eq!(map.get("foobar"), Some(&37));
    /// 
    /// map.entry("foobar")
    ///     .and_modify(|v| { *v += 1 })
    ///     .or_insert(42);
    /// assert_eq!(map.get("foobar"), Some(&38));
    /// ```
    #[must_use]
    pub fn and_modify<F: FnOnce(&mut V)>(self, f: F) -> Self {
        match self {
            Occupied(mut entry) => {
                f(entry.get_mut());
                Occupied(entry)
            },
            Vacant(entry) => Vacant(entry),
        }
    }
}

impl<'a, K, V: Default, S: Storage<ListMapLayout<K, V>>, I: Capacity> Entry<'a, K, V, S, I> {
    /// Ensures a value is in the entry by inserting the default value if empty,
    /// and returns a mutable reference to the value in the entry.
    /// 
    /// # Examples
    /// ```
    /// use coca::collections::InlineListMap;
    /// 
    /// let mut map = InlineListMap::<&'static str, u32, 4>::new();
    /// map.entry("foobar").or_default();
    /// 
    /// assert_eq!(map.get("foobar"), Some(&0));
    /// ```
    pub fn or_default(self) -> &'a mut V {
        match self {
            Occupied(entry) => entry.into_mut(),
            Vacant(entry) => entry.insert(V::default())
        }
    }
}

impl<K: Debug, V: Debug, S: Storage<ListMapLayout<K, V>>, I: Capacity> Debug for Entry<'_, K, V, S, I> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match *self {
            Vacant(ref v) => f.debug_tuple("Entry").field(v).finish(),
            Occupied(ref o) => f.debug_tuple("Entry").field(o).finish(),
        }
    }
}

/// An iterator over the entries of a [`ListMap`].
/// 
/// This `struct` is created by the [`iter`](ListMap::iter) method on `ListMap`.
/// See its documentation for more.
pub struct Iter<'a, K, V, S: Storage<ListMapLayout<K, V>>, I: Capacity> {
    map: &'a ListMap<K, V, S, I>,
    front: I,
}

impl<'a, K, V, S: Storage<ListMapLayout<K, V>>, I: Capacity> Iterator for Iter<'a, K, V, S, I> {
    type Item = (&'a K, &'a V);

    fn next(&mut self) -> Option<Self::Item> {
        let front = self.front.as_usize();
        if front >= self.map.len() { return None; }

        let k = &self.map.keys()[front];
        let v = &self.map.values()[front];
        self.front = I::from_usize(front + 1);

        Some((k, v))
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let front = self.front.as_usize();
        let len = self.map.len() - front;
        (len, Some(len))
    }
}

impl<K, V, S: Storage<ListMapLayout<K, V>>, I: Capacity> ExactSizeIterator for Iter<'_, K, V, S, I> {}
impl<K, V, S: Storage<ListMapLayout<K, V>>, I: Capacity> FusedIterator for Iter<'_, K, V, S, I> {}

impl<'a, K, V, S: Storage<ListMapLayout<K, V>>, I: Capacity> IntoIterator for &'a ListMap<K, V, S, I> {
    type Item = (&'a K, &'a V);
    type IntoIter = Iter<'a, K, V, S, I>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

/// A mutable iterator over the entries of a [`ListMap`].
/// 
/// This `struct` is created by the [`iter_mut`](ListMap::iter_mut)
/// method on `ListMap`. See its documentation for more.
pub struct IterMut<'a, K, V, S: Storage<ListMapLayout<K, V>>, I: Capacity> {
    map: &'a mut ListMap<K, V, S, I>,
    front: I,
}

impl<'a, K, V, S: Storage<ListMapLayout<K, V>>, I: Capacity> Iterator for IterMut<'a, K, V, S, I> {
    type Item = (&'a K, &'a mut V);

    fn next(&mut self) -> Option<Self::Item> {
        let front = self.front.as_usize();
        if front >= self.map.len() { return None; }

        let (k, v) = unsafe {
            let base_ptr = self.map.buf.get_mut_ptr();
            
            let keys_ptr = base_ptr.cast::<K>();
            let k = keys_ptr.add(front).as_ref().unwrap();

            let values_ptr = base_ptr.add(self.map.values_offset()).cast::<V>();
            let v = values_ptr.add(front).as_mut().unwrap();

            (k, v)
        };

        self.front = I::from_usize(front + 1);
        Some((k, v))
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let front = self.front.as_usize();
        let len = self.map.len() - front;
        (len, Some(len))
    }
}

impl<K, V, S: Storage<ListMapLayout<K, V>>, I: Capacity> ExactSizeIterator for IterMut<'_, K, V, S, I> {}
impl<K, V, S: Storage<ListMapLayout<K, V>>, I: Capacity> FusedIterator for IterMut<'_, K, V, S, I> {}

impl<'a, K, V, S: Storage<ListMapLayout<K, V>>, I: Capacity> IntoIterator for &'a mut ListMap<K, V, S, I> {
    type Item = (&'a K, &'a mut V);
    type IntoIter = IterMut<'a, K, V, S, I>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter_mut()
    }
}

/// An owning iterator over the entries of a [`ListMap`].
/// 
/// This `struct` is created by the [`into_iter`](IntoIterator::into_iter)
/// method on `ListMap` (provided by the [`IntoIterator`] trait). See its
/// documentation for more.
pub struct IntoIter<K, V, S: Storage<ListMapLayout<K, V>>, I: Capacity> {
    map: ListMap<K, V, S, I>,
}

impl<K, V, S: Storage<ListMapLayout<K, V>>, I: Capacity> Iterator for IntoIter<K, V, S, I> {
    type Item = (K, V);

    fn next(&mut self) -> Option<Self::Item> {
        if self.map.is_empty() { return None; }

        let new_len = self.map.len() - 1;
        self.map.len = I::from_usize(new_len);

        unsafe {
            let base_ptr = self.map.buf.get_ptr();
            
            let keys_ptr = base_ptr.cast::<K>();
            let k = keys_ptr.add(new_len).read();

            let values_ptr = base_ptr.add(self.map.values_offset()).cast::<V>();
            let v = values_ptr.add(new_len).read();

            Some((k, v))
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let len = self.map.len();
        (len, Some(len))
    }
}

impl<K, V, S: Storage<ListMapLayout<K, V>>, I: Capacity> ExactSizeIterator for IntoIter<K, V, S, I> {}
impl<K, V, S: Storage<ListMapLayout<K, V>>, I: Capacity> FusedIterator for IntoIter<K, V, S, I> {}

impl<K, V, S: Storage<ListMapLayout<K, V>>, I: Capacity> IntoIterator for ListMap<K, V, S, I> {
    type Item = (K, V);
    type IntoIter = IntoIter<K, V, S, I>;

    fn into_iter(self) -> Self::IntoIter {
        IntoIter { map: self }
    }
}

/// An owning iterator over the keys of a [`ListMap`].
/// 
/// This `struct` is created by the [`into_keys`](ListMap::into_keys) method on `ListMap`.
/// See its documentation for more.
pub struct IntoKeys<K, V, S: Storage<ListMapLayout<K, V>>, I: Capacity> {
    base: IntoIter<K, V, S, I>,
}

impl<K, V, S: Storage<ListMapLayout<K, V>>, I: Capacity> Iterator for IntoKeys<K, V, S, I> {
    type Item = K;

    fn next(&mut self) -> Option<Self::Item> {
        self.base.next().map(|(k, _)| k)
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.base.size_hint()
    }
}

impl<K, V, S: Storage<ListMapLayout<K, V>>, I: Capacity> ExactSizeIterator for IntoKeys<K, V, S, I> {}
impl<K, V, S: Storage<ListMapLayout<K, V>>, I: Capacity> FusedIterator for IntoKeys<K, V, S, I> {}

/// An owning iterator over the values of a [`ListMap`].
/// 
/// This `struct` is created by the [`into_values`](ListMap::into_values) method on `ListMap`.
/// See its documentation for more.
pub struct IntoValues<K, V, S: Storage<ListMapLayout<K, V>>, I: Capacity> {
    base: IntoIter<K, V, S, I>,
}

impl<K, V, S: Storage<ListMapLayout<K, V>>, I: Capacity> Iterator for IntoValues<K, V, S, I> {
    type Item = V;

    fn next(&mut self) -> Option<Self::Item> {
        self.base.next().map(|(_, v)| v)
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.base.size_hint()
    }
}

impl<K, V, S: Storage<ListMapLayout<K, V>>, I: Capacity> ExactSizeIterator for IntoValues<K, V, S, I> {}
impl<K, V, S: Storage<ListMapLayout<K, V>>, I: Capacity> FusedIterator for IntoValues<K, V, S, I> {}

/// A draining iterator over the entries of a [`ListMap`].
/// 
/// This `struct` is created by the [`drain`](ListMap::drain) method on `ListMap`.
/// See its documentation for more.
pub struct Drain<'a, K, V, S: Storage<ListMapLayout<K, V>>, I: Capacity> {
    map: &'a mut ListMap<K, V, S, I>,
}

impl<'a, K, V, S: Storage<ListMapLayout<K, V>>, I: Capacity> Iterator for Drain<'a, K, V, S, I> {
    type Item = (K, V);

    fn next(&mut self) -> Option<Self::Item> {
        let len = self.map.len();
        if len == 0 { return None; }

        let new_len = len - 1;
        self.map.len = I::from_usize(new_len);

        unsafe {
            let base_ptr = self.map.buf.get_ptr();
            
            let keys_ptr = base_ptr.cast::<K>();
            let k = keys_ptr.add(new_len).read();

            let values_ptr = base_ptr.add(self.map.values_offset()).cast::<V>();
            let v = values_ptr.add(new_len).read();

            Some((k, v))
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let len = self.map.len();
        (len, Some(len))
    }
}

impl<K, V, S: Storage<ListMapLayout<K, V>>, I: Capacity> ExactSizeIterator for Drain<'_, K, V, S, I> {}
impl<K, V, S: Storage<ListMapLayout<K, V>>, I: Capacity> FusedIterator for Drain<'_, K, V, S, I> {}

impl<K, V, S: Storage<ListMapLayout<K, V>>, I: Capacity> Drop for Drain<'_, K, V, S, I> {
    fn drop(&mut self) {
        self.for_each(drop);
    }
}

/// A draining, filtering iterator over the entries of a [`ListMap`].
/// 
/// This `struct` is created by the [`drain_filter`](ListMap::drain_filter)
/// method on `ListMap`. See its documentation for more.
pub struct DrainFilter<'a, K, V, S, I, F>
where
    S: Storage<ListMapLayout<K, V>>,
    I: Capacity,
    F: FnMut(&K, &mut V) -> bool,
{
    map: &'a mut ListMap<K, V, S, I>,
    should_remove: F,
    front: I,
}

impl<'a, K, V, S, I, F> Iterator for DrainFilter<'a, K, V, S, I, F>
where
    S: Storage<ListMapLayout<K, V>>,
    I: Capacity,
    F: FnMut(&K, &mut V) -> bool
{
    type Item = (K, V);

    fn next(&mut self) -> Option<Self::Item> {
        let mut front = self.front.as_usize();
        while front < self.map.len() {
            unsafe {
                let base_ptr = self.map.buf.get_mut_ptr();
                let keys_ptr = base_ptr.cast::<K>();
                let values_ptr = base_ptr.add(self.map.values_offset()).cast::<V>();

                let k = keys_ptr.add(front).as_ref().unwrap();
                let v = values_ptr.add(front).as_mut().unwrap();

                if (self.should_remove)(k, v) {
                    let new_len = self.map.len() - 1;
                    self.map.len = I::from_usize(new_len);

                    let k = keys_ptr.add(front).read();
                    let v = values_ptr.add(front).read();

                    if front < new_len {
                        core::ptr::copy_nonoverlapping(keys_ptr.add(new_len), keys_ptr.add(front), 1);
                        core::ptr::copy_nonoverlapping(values_ptr.add(new_len), values_ptr.add(front), 1);
                    }

                    self.front = I::from_usize(front);
                    return Some((k, v));
                }
            }

            front += 1;
        }

        self.front = I::from_usize(front);
        None
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let max_len = self.map.len() - self.front.as_usize();
        (0, Some(max_len))
    }
}

impl<K, V, S: Storage<ListMapLayout<K, V>>, I: Capacity, F: FnMut(&K, &mut V) -> bool> FusedIterator for DrainFilter<'_, K, V, S, I, F> {}

impl<'a, K, V, S, I, F> Drop for DrainFilter<'a, K, V, S, I, F>
where
    S: Storage<ListMapLayout<K, V>>,
    I: Capacity,
    F: FnMut(&K, &mut V) -> bool
{
    fn drop(&mut self) {
        self.for_each(drop);
    }
}