colony 0.3.0

A fast associative data-structure that chooses its own keys
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
#![doc = include_str!("./doc.md")]
#![warn(missing_debug_implementations)]
#![warn(missing_docs)]

use std::alloc::{alloc, dealloc, handle_alloc_error, Layout, LayoutError};
use std::fmt::{Debug, Formatter};
use std::mem::ManuallyDrop;
use std::ops::{Index, IndexMut};
use std::panic::{RefUnwindSafe, UnwindSafe};
use std::ptr::NonNull;
use std::{fmt, mem, ptr};

pub use guard::*;
pub use iter::*;

use crate::index_opt::IndexOpt;
use crate::skipfield::{SkipfieldElement, SkipfieldPtr};

mod guard;
mod index_opt;
mod iter;
mod skipfield;

/// A `Colony` that uses `FlagGuard`, see the documentation for [`Colony`] for more information about guards.
///
/// Also see [`Colony::flagged`].
pub type FlaggedColony<T> = Colony<T, FlagGuard>;

/// A `Colony` that uses `NoGuard`, see the documentation for [`Colony`] for more information about guards.
///
/// Also see [`Colony::unguarded`].
pub type UnguardedColony<T> = Colony<T, NoGuard>;

const EMPTY_SKIPFIELD: &[SkipfieldElement] = &[0, 0];

const MAX_CAPACITY: usize = isize::MAX as usize;

#[doc = include_str!("./doc.md")]
pub struct Colony<T, G: Guard = GenerationGuard> {
    elements: NonNull<Slot<T, G>>,
    // Initialized from [-1, capacity]
    // Element at -1 and elements in [len, capacity] are zero
    // Valid for reads (but not writes) even when capacity is zero
    skipfield: NonNull<SkipfieldElement>,
    capacity: usize,
    touched: usize,
    len: usize,
    next_free: IndexOpt,
    id: G::__Id,
}

impl<T> Colony<T> {
    /// Constructs an empty colony using [`GenerationGuard`].
    ///
    /// Does not allocate.
    /// See [`Colony::flagged`] and [`Colony::unguarded`] to create colonies with different guards.
    ///
    /// # Examples
    ///
    /// ```
    /// # use colony::{Colony, GenerationGuard};
    /// let colony: Colony<i32, GenerationGuard> = Colony::new();
    /// ```
    pub fn new() -> Self {
        Self::default()
    }
}

impl<T> FlaggedColony<T> {
    /// Constructs an empty colony using [`FlagGuard`].
    ///
    /// Does not allocate.
    /// See [`Colony::new`] and [`Colony::unguarded`] to create colonies with different guards.
    ///
    /// # Examples
    ///
    /// ```
    /// # use colony::{Colony, FlaggedColony};
    /// let colony: FlaggedColony<i32> = Colony::flagged();
    /// ```
    pub fn flagged() -> Self {
        Self::default()
    }
}

impl<T> UnguardedColony<T> {
    /// Constructs an empty colony using [`NoGuard`].
    ///
    /// Does not allocate.
    /// See [`Colony::new`] and [`Colony::flagged`] to create colonies with different guards.
    ///
    /// # Examples
    ///
    /// ```
    /// # use colony::{Colony, UnguardedColony};
    /// let colony: UnguardedColony<i32> = Colony::unguarded();
    /// ```
    pub fn unguarded() -> Self {
        Self::default()
    }
}

impl<T, G: Guard> Default for Colony<T, G> {
    fn default() -> Self {
        let skipfield = unsafe {
            let ptr = EMPTY_SKIPFIELD.as_ptr().add(1) as *mut _;
            NonNull::new_unchecked(ptr)
        };

        Self {
            elements: NonNull::dangling(),
            skipfield,
            capacity: 0,
            touched: 0,
            len: 0,
            next_free: IndexOpt::none(),
            id: G::__sentinel_id(),
        }
    }
}

impl<T, G: Guard> Colony<T, G> {
    const MIN_NON_ZERO_CAP: usize = if mem::size_of::<T>() == 1 {
        8
    } else if mem::size_of::<T>() <= 1024 {
        4
    } else {
        1
    };

    // Preconditions:
    // * index < touched
    unsafe fn slot(&self, index: usize) -> &Slot<T, G> {
        debug_assert!(index < self.touched);
        &*self.elements.as_ptr().add(index)
    }

    // Preconditions:
    // * index < touched
    unsafe fn slot_mut(&mut self, index: usize) -> &mut Slot<T, G> {
        debug_assert!(index < self.touched);
        &mut *self.elements.as_ptr().add(index)
    }

    fn skipfield(&self) -> SkipfieldPtr {
        SkipfieldPtr::new(self.skipfield)
    }

    /// Returns the total number of elements in the colony.
    ///
    ///  # Examples
    ///
    /// ```
    /// # use colony::Colony;
    /// let mut colony = Colony::new();
    ///
    /// let handle = colony.insert("foo");
    /// assert_eq!(colony.len(), 1);
    /// colony.remove(handle);
    /// assert_eq!(colony.len(), 0);
    /// ```
    pub fn len(&self) -> usize {
        self.len
    }

    /// Returns `true` if there are no elements in the colony.
    ///
    /// # Examples
    ///
    /// ```
    /// # use colony::Colony;
    /// let mut colony = Colony::new();
    /// assert!(colony.is_empty());
    /// colony.insert("foo");
    /// assert!(!colony.is_empty());
    /// ```
    pub fn is_empty(&self) -> bool {
        self.len == 0
    }

    /// Returns the capacity of the colony.
    ///
    /// The colony will not allocate more memory unless the [`len`](Colony::len) would overflow the capacity.
    /// This means you can be sure that [`insert`](Colony::insert) will not panic while the colony's length is lesser than its capacity.
    ///
    /// # Examples
    ///
    /// ```
    /// # use colony::Colony;
    /// let mut colony = Colony::new();
    ///
    /// // No space is initially allocated
    /// assert_eq!(colony.capacity(), 0);
    ///
    /// // After insertion space is made for one or more new elements
    /// colony.insert("foo");
    /// assert!(colony.capacity() >= 1);
    /// ```
    pub fn capacity(&self) -> usize {
        self.capacity
    }

    /// Returns a reference to a element by the handle returned by [`insert`](Colony::insert).
    ///
    /// Some care needs to be taken with respect to aliasing of handles when not using [`GenerationGuard`].
    /// See [`Colony`] for more information.
    ///
    /// # Examples
    ///
    /// ```
    /// # use colony::Colony;
    /// let mut colony = Colony::new();
    /// let handle = colony.insert("foo");
    ///
    /// assert_eq!(colony.get(handle), Some(&"foo"));
    /// colony.remove(handle);
    /// assert_eq!(colony.get(handle), None);
    /// ```
    pub fn get(&self, handle: G::Handle) -> Option<&T>
    where
        G: CheckedGuard,
    {
        let index = G::__extract_index(&handle);

        if index >= self.touched {
            return None;
        }

        unsafe {
            let slot = self.slot(index);

            if !slot.guard.__check(&handle, self.id) {
                return None;
            }

            Some(slot.occupied())
        }
    }

    /// Returns a reference to an element at an index assuming that it exists.
    ///
    /// This is mostly useful with [`UnguardedColony`], where the regular [`get`](Colony::get) method cannot be used.
    ///
    /// # Safety
    ///
    /// An element must exist at the index provided.
    ///
    /// # Examples
    ///
    /// ```
    /// # use colony::Colony;
    /// let mut colony = Colony::new();
    /// let handle = colony.insert("foo");
    ///
    /// unsafe {
    ///     let result = colony.get_unchecked(handle.index);
    ///     assert_eq!(result, &"foo");
    /// }
    /// ```
    pub unsafe fn get_unchecked(&self, index: usize) -> &T {
        self.slot(index).occupied()
    }

    /// Returns a reference to a element by the handle returned by [`insert`](Colony::insert).
    ///
    /// See [`get`](Colony::get) for more information.
    pub fn get_mut(&mut self, handle: G::Handle) -> Option<&mut T>
    where
        G: CheckedGuard,
    {
        let index = G::__extract_index(&handle);

        if index >= self.touched {
            return None;
        }

        unsafe {
            let colony_id = self.id;
            let slot = self.slot_mut(index);

            if !slot.guard.__check(&handle, colony_id) {
                return None;
            }

            Some(slot.occupied_mut())
        }
    }

    /// Returns a mutable reference to an element at an index assuming that it exists.
    ///
    /// See [`get_unchecked`](Colony::get_unchecked) for more information.
    ///
    /// # Safety
    ///
    /// An element must exist at the index provided.
    pub unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut T {
        self.slot_mut(index).occupied_mut()
    }

    /// Inserts an element into the colony at an unspecified index.
    ///
    /// Some care needs to be taken with respect to aliasing of handles when not using [`GenerationGuard`].
    /// See [`Colony`] for more information.
    ///
    /// # Panics
    ///
    /// See [`reserve`](Self::reserve).
    ///
    /// # Examples
    ///
    /// ```
    /// # use colony::Colony;
    /// let mut colony = Colony::new();
    /// let handle = colony.insert("foo");
    /// assert_eq!(colony[handle], "foo");
    /// ```
    pub fn insert(&mut self, value: T) -> G::Handle {
        unsafe {
            if let Some(free) = self.next_free.as_opt() {
                self.insert_into_free(free, value)
            } else {
                self.insert_at_end(value)
            }
        }
    }

    // Preconditions:
    // * elements[free] is unoccupied and the head of its skipblock
    // * len < touched
    unsafe fn insert_into_free(&mut self, free: usize, value: T) -> G::Handle {
        debug_assert!(self.len < self.touched);

        self.skipfield().unskip_leftmost(free);
        self.remove_skipblock_from_skiplist(free, free);

        self.len += 1;

        let colony_id = self.id;
        let slot = self.slot_mut(free);
        slot.fill(value);
        G::__new_handle(&slot.guard, free, colony_id)
    }

    // Preconditions:
    // * len == touched
    unsafe fn insert_at_end(&mut self, value: T) -> G::Handle {
        if self.len == self.capacity {
            self.reserve(1);
        }

        self.insert_at_end_unchecked(value)
    }

    // Preconditions:
    // * len == touched < capacity
    unsafe fn insert_at_end_unchecked(&mut self, value: T) -> G::Handle {
        debug_assert!(self.len == self.touched);

        let slot = Slot::new_full(value);
        let handle = G::__new_handle(&slot.guard, self.touched, self.id);

        unsafe {
            self.elements.as_ptr().add(self.touched).write(slot);
        }

        self.touched += 1;
        self.len += 1;

        handle
    }

    /// Removes the element with the given handle, if it exists.
    ///
    /// Some care needs to be taken with respect to aliasing of handle when not using [`GenerationGuard`].
    /// See [`Colony`] for more information.
    ///
    /// # Examples
    ///
    /// ```
    /// # use colony::Colony;
    /// let mut colony = Colony::new();
    /// let handle = colony.insert("foo");
    /// assert_eq!(colony.remove(handle), Some("foo"));
    /// assert_eq!(colony.remove(handle), None);
    /// ```
    pub fn remove(&mut self, handle: G::Handle) -> Option<T>
    where
        G: CheckedGuard,
    {
        let index = G::__extract_index(&handle);

        if index >= self.touched {
            return None;
        }

        unsafe {
            let colony_id = self.id;
            let slot = self.slot_mut(index);

            if !slot.guard.__check(&handle, colony_id) {
                return None;
            }

            Some(self.remove_unchecked(index))
        }
    }

    /// Removes the element with the given index, assuming it exists.
    ///
    /// This is mostly useful with [`UnguardedColony`] where [`remove`](Colony::remove) cannot be used.
    ///
    /// # Safety
    ///
    /// An element must exist with the index provided.
    ///
    /// # Examples
    ///
    /// ```
    /// # use colony::Colony;
    /// let mut colony = Colony::new();
    /// let handle = colony.insert("foo");
    ///
    /// unsafe {
    ///     let result = colony.remove_unchecked(handle.index);
    ///     assert_eq!(result, "foo");
    /// }
    /// ```
    pub unsafe fn remove_unchecked(&mut self, index: usize) -> T {
        unsafe {
            let (result, reuse) = self.slot_mut(index).empty();
            let (start, end) = self.skipfield().skip(index);

            if reuse {
                let has_left = start != index;
                let has_right = end != index;

                if !has_left && !has_right {
                    self.stitch_no_left_no_right(index);
                } else if has_left && !has_right {
                    self.stitch_only_left(index);
                } else if !has_left && has_right {
                    self.stitch_only_right(index);
                } else {
                    self.stitch_left_and_right(index, start, end);
                }
            }

            self.len -= 1;
            result
        }
    }

    unsafe fn stitch_no_left_no_right(&mut self, index: usize) {
        self.add_skipblock_to_skiplist(index, index);
    }

    unsafe fn stitch_only_left(&mut self, index: usize) {
        let next = mem::replace(
            &mut self.slot_mut(index - 1).unoccupied_mut().next,
            IndexOpt::some(index),
        );

        if let Some(next) = next.as_opt() {
            self.slot_mut(next).unoccupied_mut().prev = IndexOpt::some(index);
        }

        *self.slot_mut(index).unoccupied_mut() = Unoccupied {
            prev: IndexOpt::some(index - 1),
            next,
        };
    }

    unsafe fn stitch_only_right(&mut self, index: usize) {
        let prev = mem::replace(
            &mut self.slot_mut(index + 1).unoccupied_mut().prev,
            IndexOpt::some(index),
        );

        match prev.as_opt() {
            Some(prev) => self.slot_mut(prev).unoccupied_mut().next = IndexOpt::some(index),
            None => self.next_free = IndexOpt::some(index),
        }

        *self.slot_mut(index).unoccupied_mut() = Unoccupied {
            prev,
            next: IndexOpt::some(index + 1),
        };
    }

    unsafe fn stitch_left_and_right(&mut self, index: usize, start: usize, end: usize) {
        self.remove_skipblock_from_skiplist(start, index - 1);
        self.remove_skipblock_from_skiplist(index + 1, end);
        self.add_skipblock_to_skiplist(start, end);

        self.slot_mut(index - 1).unoccupied_mut().next = IndexOpt::some(index);
        self.slot_mut(index + 1).unoccupied_mut().prev = IndexOpt::some(index);

        *self.slot_mut(index).unoccupied_mut() = Unoccupied {
            prev: IndexOpt::some(index - 1),
            next: IndexOpt::some(index + 1),
        };
    }

    // Preconditions:
    // * start and end are part of the same skipblock
    // * start <= end
    unsafe fn remove_skipblock_from_skiplist(&mut self, start: usize, end: usize) {
        // Careful not to alias first and last
        let prev = self.slot_mut(start).unoccupied().prev;
        let next = self.slot_mut(end).unoccupied().next;

        match prev.as_opt() {
            Some(prev) => self.slot_mut(prev).unoccupied_mut().next = next,
            None => self.next_free = next,
        }

        if let Some(next) = next.as_opt() {
            self.slot_mut(next).unoccupied_mut().prev = prev;
        }
    }

    // Preconditions:
    // * start <= end
    // * indices from start through end are all unoccupied, but not in the skiplist
    unsafe fn add_skipblock_to_skiplist(&mut self, start: usize, end: usize) {
        self.slot_mut(start).unoccupied_mut().prev = IndexOpt::none();
        self.slot_mut(end).unoccupied_mut().next = self.next_free;

        if let Some(old_head) = self.next_free.as_opt() {
            self.slot_mut(old_head).unoccupied_mut().prev = IndexOpt::some(end);
        }

        self.next_free = IndexOpt::some(start);
    }

    /// Removes all elements from the colony.
    ///
    /// This is equivalent to `*self = Colony::default()`, except that the capacity remains unchanged.
    /// This is an `O(n)` operation even if `T` doesn't implement `Drop`.
    ///
    /// # Panics
    ///
    /// When using [`GenerationalGuard], this may panic if all colony IDs have been exhausted.
    /// See [`Colony`] for more information about the colony ID limit.
    ///
    /// # Examples
    ///
    /// ```
    /// # use colony::Colony;
    /// let mut colony = Colony::new();
    ///
    /// let foo = colony.insert("foo");
    /// colony.clear();
    /// let bar = colony.insert("bar");
    ///
    /// assert_eq!(colony.get(foo), None);
    /// assert_eq!(colony.get(bar), Some(&"bar"));
    /// ```
    pub fn clear(&mut self) {
        if mem::needs_drop::<(G, T)>() {
            for value in self.values_mut() {
                unsafe {
                    ptr::drop_in_place(value);
                }
            }
        }

        unsafe {
            ptr::write_bytes(self.skipfield.as_ptr(), 0, self.touched);
        }

        self.id = G::__new_id();
        self.len = 0;
        self.touched = 0;
        self.next_free = IndexOpt::none();
    }

    /// Increases the capacity of the colony to at least `self.len() + additional`.
    ///
    /// If the colony is already sufficiently large, this is a no-op.
    /// This can be used as an optimization, or as a way to make sure [`insert`](Colony::insert) won't panic.
    ///
    /// # Panics
    ///
    /// * If this method allocates, an allocation failure may panic.
    /// * If the capacity would grow over `isize::MAX`.
    /// * When using [`GenerationGuard`], this method creates a unique ID for the colony upon the first allocation.
    ///   This method may panic if all available IDs have been exhausted.
    ///
    /// # Examples
    ///
    /// ```
    /// # use colony::Colony;
    /// let mut colony = Colony::<i32>::new();
    /// colony.reserve(100);
    /// assert!(colony.capacity() >= 100);
    /// ```
    pub fn reserve(&mut self, additional: usize) {
        if additional > self.capacity - self.len {
            unsafe {
                self.do_reserve(additional);
            }
        }
    }

    // Preconditions:
    // * len + additional > capacity
    #[cold]
    unsafe fn do_reserve(&mut self, additional: usize) {
        let new_cap = self.len.checked_add(additional);
        let new_cap = new_cap.filter(|&new_cap| new_cap < MAX_CAPACITY);
        let Some(new_cap) = new_cap else {
            panic!("capacity overflow");
        };

        let new_id = if self.capacity == 0 {
            Some(G::__new_id())
        } else {
            None
        };

        let new_cap = usize::max(new_cap, self.capacity * 2);
        let new_cap = usize::max(new_cap, Self::MIN_NON_ZERO_CAP);

        self.resize(new_cap);

        if let Some(new_id) = new_id {
            self.id = new_id;
        }
    }

    // Preconditions:
    // * new_cap >= touched
    unsafe fn resize(&mut self, new_cap: usize) {
        debug_assert!(new_cap >= self.touched);
        let old_cap = self.capacity;

        let (old_layout, _) = Self::layout(old_cap).unwrap_unchecked();
        let Ok((new_layout, skipfield_offset)) = Self::layout(new_cap) else {
            panic!("could not layout");
        };

        let old_alloc = self.elements.as_ptr() as *mut u8;

        debug_assert_ne!(new_layout.size(), 0);
        let new_alloc = alloc(new_layout);

        if new_alloc.is_null() {
            handle_alloc_error(new_layout);
        }

        let new_elements = new_alloc as *mut Slot<T, G>;
        let new_skipfield = new_alloc.add(skipfield_offset) as *mut SkipfieldElement;
        self.copy_memory(new_elements, new_skipfield, new_cap);

        if old_cap > 0 {
            debug_assert_ne!(old_layout.size(), 0);
            dealloc(old_alloc, old_layout);
        }

        self.elements = NonNull::new_unchecked(new_elements);
        self.skipfield = NonNull::new_unchecked(new_skipfield);
        self.capacity = new_cap;
    }

    // Preconditions:
    // * new_elements, new_skipfield were allocated from a layout of capacity new_cap
    // * new_cap >= touched
    unsafe fn copy_memory(
        &self,
        new_elements: *mut Slot<T, G>,
        new_skipfield: *mut SkipfieldElement,
        new_cap: usize,
    ) {
        debug_assert!(new_cap >= self.touched);
        ptr::copy_nonoverlapping(self.elements.as_ptr(), new_elements, self.touched);
        self.copy_skipfield(new_skipfield, new_cap);
    }

    // Preconditions:
    // * new_skipfield was allocated from a layout of capacity new_cap
    // * new_cap >= touched
    unsafe fn copy_skipfield(&self, new_skipfield: *mut SkipfieldElement, new_cap: usize) {
        let true_old_skipfield = self.skipfield.as_ptr().sub(1);
        let true_old_skipfield_len = self.touched + 2;
        let true_new_skipfield = new_skipfield.sub(1);
        let true_new_skipfield_len = new_cap + 2;
        let remaining_skipfield = true_new_skipfield.add(true_old_skipfield_len);

        ptr::copy_nonoverlapping(
            true_old_skipfield,
            true_new_skipfield,
            true_old_skipfield_len,
        );

        ptr::write_bytes(
            remaining_skipfield,
            0,
            true_new_skipfield_len - true_old_skipfield_len,
        );
    }

    fn layout(capacity: usize) -> Result<(Layout, usize), LayoutError> {
        let layout = Layout::array::<Slot<T, G>>(capacity)?;
        let (layout, _) = layout.extend(Layout::new::<SkipfieldElement>())?;
        let (layout, skipfield_offset) =
            layout.extend(Layout::array::<SkipfieldElement>(capacity)?)?;
        let (layout, _) = layout.extend(Layout::new::<SkipfieldElement>())?;

        Ok((layout, skipfield_offset))
    }

    /// Creates an iterator over the values in the colony and their handles.
    ///
    /// If you want an iterator over only the values (and not the handles) then call [`values`](Colony::values).
    ///
    /// # Examples
    ///
    /// ```
    /// # use colony::Colony;
    /// let mut colony = Colony::new();
    /// let foo = colony.insert("foo");
    /// let bar = colony.insert("bar");
    ///
    /// let expected = [(foo, &"foo"), (bar, &"bar")].into_iter();
    /// assert!(Iterator::eq(colony.iter(), expected));
    /// ```
    pub fn iter(&self) -> Iter<T, G> {
        Iter::new(self)
    }

    /// Creates an iterator over just the values of the colony.
    ///
    /// If you want to iterate over the handle for each value too, call [`iter`](Colony::iter).
    ///
    /// # Examples
    ///
    /// ```
    /// # use colony::Colony;
    /// let mut colony = Colony::new();
    /// colony.insert("foo");
    /// colony.insert("bar");
    ///
    /// let expected = ["foo", "bar"].iter();
    /// assert!(Iterator::eq(colony.values(), expected));
    /// ```
    pub fn values(&self) -> Values<T, G> {
        Values::new(self)
    }

    /// Creates an iterator over the values in the colony and their handles, by mutable reference.
    ///
    /// See [`iter`](Colony::iter).
    pub fn iter_mut(&mut self) -> IterMut<T, G> {
        IterMut::new(self)
    }

    /// Creates an iterator over just the values in the colony, by mutable reference.
    ///
    /// See [`values`](Colony::values).
    pub fn values_mut(&mut self) -> ValuesMut<T, G> {
        ValuesMut::new(self)
    }
}

impl<T, G: Guard> Drop for Colony<T, G> {
    fn drop(&mut self) {
        unsafe {
            if mem::needs_drop::<T>() {
                for value in self.values_mut() {
                    ptr::drop_in_place(value);
                }
            }

            if self.capacity > 0 {
                let (layout, _) = Self::layout(self.capacity).unwrap_unchecked();
                dealloc(self.elements.as_ptr() as *mut u8, layout);
            }
        }
    }
}

impl<T, G: CheckedGuard> Index<G::Handle> for Colony<T, G> {
    type Output = T;

    fn index(&self, index: G::Handle) -> &T {
        self.get(index)
            .expect("no element with that handle exists in this colony")
    }
}

impl<T, G: CheckedGuard> IndexMut<G::Handle> for Colony<T, G> {
    fn index_mut(&mut self, index: G::Handle) -> &mut T {
        self.get_mut(index)
            .expect("no element with that handle exists in this colony")
    }
}

impl<T, G: Guard> Extend<T> for Colony<T, G> {
    fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
        let mut iter = iter.into_iter();

        unsafe {
            while let Some(free) = self.next_free.as_opt() {
                let Some(value) = iter.next() else { return };
                self.insert_into_free(free, value);
            }

            while let Some(value) = iter.next() {
                if self.len == self.capacity {
                    let (lower, _) = iter.size_hint();
                    self.reserve(lower.saturating_add(1));
                }

                self.insert_at_end_unchecked(value);
            }
        }
    }
}

impl<T, G: Guard> FromIterator<T> for Colony<T, G> {
    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
        let mut result = Self::default();
        result.extend(iter);
        result
    }
}

impl<T: Clone, G: Guard> Clone for Colony<T, G> {
    fn clone(&self) -> Self {
        Self::from_iter(self.values().cloned())
    }
}

impl<T: Debug, G: Guard> Debug for Colony<T, G> {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        let iter = self.iter().map(|(_, value)| value);
        f.debug_list().entries(iter).finish()
    }
}

impl<'a, T, G: Guard> IntoIterator for &'a Colony<T, G> {
    type Item = (G::Handle, &'a T);
    type IntoIter = Iter<'a, T, G>;

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

impl<'a, T, G: Guard> IntoIterator for &'a mut Colony<T, G> {
    type Item = (G::Handle, &'a mut T);
    type IntoIter = IterMut<'a, T, G>;

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

unsafe impl<T, G: Guard> Send for Colony<T, G>
where
    T: Send,
    G: Send,
{
}

unsafe impl<T, G: Guard> Sync for Colony<T, G>
where
    T: Sync,
    G: Sync,
{
}

impl<T, G: Guard> UnwindSafe for Colony<T, G>
where
    T: UnwindSafe,
    G: UnwindSafe,
{
}

impl<T, G: Guard> RefUnwindSafe for Colony<T, G>
where
    T: RefUnwindSafe,
    G: RefUnwindSafe,
{
}

struct Slot<T, G: Guard> {
    guard: G,
    inner: SlotInner<T>,
}

union SlotInner<T> {
    occupied: ManuallyDrop<T>,
    unoccupied: Unoccupied,
}

#[derive(Copy, Clone)]
struct Unoccupied {
    prev: IndexOpt,
    next: IndexOpt,
}

impl<T, G: Guard> Slot<T, G> {
    pub unsafe fn occupied(&self) -> &T {
        &self.inner.occupied
    }

    pub unsafe fn occupied_mut(&mut self) -> &mut T {
        &mut self.inner.occupied
    }

    pub unsafe fn unoccupied(&self) -> &Unoccupied {
        &self.inner.unoccupied
    }

    pub unsafe fn unoccupied_mut(&mut self) -> &mut Unoccupied {
        &mut self.inner.unoccupied
    }

    pub unsafe fn new_full(value: T) -> Self {
        Self {
            guard: G::__new(),
            inner: SlotInner {
                occupied: ManuallyDrop::new(value),
            },
        }
    }

    pub unsafe fn fill(&mut self, value: T) {
        self.guard.__fill();

        self.inner = SlotInner {
            occupied: ManuallyDrop::new(value),
        };
    }

    pub unsafe fn empty(&mut self) -> (T, bool) {
        let value = ManuallyDrop::take(&mut self.inner.occupied);

        self.inner = SlotInner {
            unoccupied: Unoccupied {
                prev: IndexOpt::none(),
                next: IndexOpt::none(),
            },
        };

        let reuse = self.guard.__empty();
        (value, reuse)
    }
}

#[cfg(test)]
mod test {
    use std::cmp::Ordering;
    use std::fmt::{Debug, Formatter};
    use std::sync::Arc;
    use std::{fmt, iter, mem, slice};

    use crate::{Colony, Handle, UnguardedColony};

    const N: &[usize] = &[0, 1, 5, 10, 100, 1_000, 10_000, 100_000];

    #[derive(Clone)]
    struct Model<T> {
        slots: Vec<Option<T>>,
        colony: UnguardedColony<T>,
    }

    impl<T> Model<T> {
        fn new() -> Self {
            Self {
                slots: Vec::new(),
                colony: Colony::default(),
            }
        }

        pub fn insert(&mut self, value: T) -> usize
        where
            T: Clone,
        {
            let index = self.colony.insert(value.clone());

            match index.cmp(&self.slots.len()) {
                Ordering::Equal => self.slots.push(Some(value)),
                Ordering::Less => {
                    assert!(self.slots[index].is_none());
                    self.slots[index] = Some(value);
                }
                Ordering::Greater => panic!("out of bounds index"),
            }

            index
        }

        pub fn remove(&mut self, index: usize)
        where
            T: Eq + Debug,
        {
            assert!(index < self.slots.len());
            let Some(expected) = self.slots[index].take() else {
                panic!("not occupied");
            };

            let actual = unsafe { self.colony.remove_unchecked(index) };

            assert_eq!(actual, expected);
        }

        pub fn check(&self)
        where
            T: Eq,
        {
            let expected = self.slots.iter().filter_map(|slot| slot.as_ref());
            let actual = self.colony.iter().map(|(_, value)| value);
            assert!(Iterator::eq(actual, expected));
        }
    }

    impl<T: Debug> Debug for Model<T> {
        fn fmt(&self, f: &mut Formatter) -> fmt::Result {
            #[derive(Debug)]
            #[allow(unused)]
            enum Slot<'a, T> {
                Occupied(&'a T),
                Unoccupied {
                    prev: Option<usize>,
                    next: Option<usize>,
                },
            }

            let mut slots = Vec::new();

            for (i, slot) in self.slots.iter().enumerate() {
                let slot = match slot {
                    Some(value) => Slot::Occupied(value),
                    None => unsafe {
                        let super::Unoccupied { prev, next } = self.colony.slot(i).unoccupied();

                        Slot::Unoccupied {
                            prev: prev.as_opt(),
                            next: next.as_opt(),
                        }
                    },
                };

                slots.push(slot);
            }

            let skipfield = unsafe {
                slice::from_raw_parts(self.colony.skipfield.as_ptr(), self.colony.touched)
            };

            f.debug_struct("Model")
                .field("len", &self.colony.len)
                .field("touched", &self.colony.touched)
                .field("capacity", &self.colony.capacity)
                .field("next_free", &self.colony.next_free.as_opt())
                .field("slots", &slots)
                .field("skipfield", &skipfield)
                .finish()
        }
    }

    #[test]
    fn drops() {
        for &size in N {
            let arc = Arc::new(());
            let mut colony = Colony::new();

            for _ in 0..size {
                colony.insert(arc.clone());
            }

            assert_eq!(Arc::strong_count(&arc), size + 1);
            drop(colony);
            assert_eq!(Arc::strong_count(&arc), 1);
        }
    }

    #[test]
    fn different_colonies_dont_alias() {
        let mut colony_1 = Colony::new();
        let handle_1 = colony_1.insert(1);

        let mut colony_2 = Colony::new();
        let handle_2 = colony_2.insert(1);

        assert_ne!(handle_1, handle_2);
        assert!(colony_1.get(handle_2).is_none());
        assert!(colony_2.get(handle_1).is_none());
    }

    #[test]
    fn clear() {
        let mut colony = Colony::new();
        let handle = colony.insert(42);
        colony.clear();
        assert!(colony.get(handle).is_none());
    }

    #[test]
    fn insert_after_clear_doesnt_alias() {
        let mut colony = Colony::new();

        let handle_1 = colony.insert(1);
        colony.clear();
        let handle_2 = colony.insert(2);

        assert_eq!(handle_1.index, handle_2.index);
        assert_ne!(handle_1, handle_2);
        assert!(colony.get(handle_1).is_none());
    }

    #[test]
    fn handle_is_null_pointer_optimized() {
        assert_eq!(mem::size_of::<Handle>(), 16);
        assert_eq!(mem::size_of::<Option<Handle>>(), 16);
    }

    #[test]
    fn get() {
        let mut colony = Colony::new();
        let handle = colony.insert(42);
        assert_eq!(colony.get(handle), Some(&42));
    }

    #[test]
    fn get_after_remove_generation() {
        let mut colony = Colony::new();

        let handle = colony.insert(42);
        colony.remove(handle);

        assert_eq!(colony.get(handle), None);
    }

    #[test]
    fn get_after_remove_flag() {
        let mut colony = Colony::flagged();

        let handle = colony.insert(42);
        colony.remove(handle);

        assert_eq!(colony.get(handle), None);
    }

    #[test]
    fn get_after_readd() {
        let mut colony = Colony::new();

        let handle_1 = colony.insert(42);
        colony.remove(handle_1);
        let handle_2 = colony.insert(42);

        assert_ne!(handle_1, handle_2);
        assert_eq!(colony.get(handle_1), None);
    }

    #[test]
    fn reserve() {
        fn test<T>(size: usize) {
            let mut colony = Colony::<T>::new();
            colony.reserve(size);
        }

        for &size in N {
            test::<()>(size);
            test::<u8>(size);
            test::<u32>(size);
            test::<[u32; 32]>(size);
        }
    }

    #[test]
    fn insert() {
        fn test<I>(values: I)
        where
            I: Iterator,
            I::Item: Eq + Clone,
        {
            let mut model = Model::new();

            for (i, value) in values.enumerate() {
                let index = model.insert(value);
                assert_eq!(index, i);
            }

            model.check();
        }

        for &size in N {
            test(iter::repeat(()).take(size));
            test(iter::repeat(42u8).take(size));
            test(iter::repeat(42u32).take(size));
            test(iter::repeat([42u32; 32]).take(size));
        }
    }

    #[test]
    fn remove_all_forward() {
        for &size in N {
            let mut model = Model::new();

            for i in 0..size {
                model.insert(i);
            }

            for i in 0..size {
                model.remove(i);
            }

            model.check();
        }
    }

    #[test]
    fn remove_all_backward() {
        for &size in N {
            let mut model = Model::new();

            for i in 0..size {
                model.insert(i);
            }

            for i in (0..size).rev() {
                model.remove(i);
            }

            model.check();
        }
    }

    #[test]
    fn reuse_slot() {
        for &size in N {
            if size == 0 {
                continue;
            }

            let mut model = Model::new();

            for i in 0..size {
                model.insert(i);
            }

            for i in 0..size {
                model.remove(i);

                let index = model.insert(i);
                assert_eq!(index, i);
            }

            model.check();
        }
    }

    #[test]
    fn join_skipblocks() {
        let mut model = Model::new();

        for i in 0..5 {
            model.insert(i);
        }

        model.remove(1);
        model.remove(3);
        model.remove(2);

        model.check();
    }

    #[test]
    fn remove_and_readd_twice() {
        let mut model = Model::new();

        assert_eq!(model.insert(1), 0);
        model.remove(0);
        assert_eq!(model.insert(2), 0);
        assert_eq!(model.insert(3), 1);

        model.check();
    }

    #[test]
    fn insert_after_skipblock_join() {
        let mut model = Model::new();

        assert_eq!(model.insert(1), 0);
        assert_eq!(model.insert(2), 1);
        assert_eq!(model.insert(3), 2);

        model.remove(0);
        model.remove(2);
        model.remove(1);

        assert_eq!(model.insert(5), 0);

        model.check();
    }

    #[test]
    fn skipblock_join_and_reinsert_with_other_skipblock() {
        let mut model = Model::new();

        model.insert(1);
        model.insert(2);
        model.insert(3);
        model.insert(4);
        model.insert(5);

        model.remove(4);
        model.remove(0);
        model.remove(2);
        model.remove(1);

        model.insert(6);
        model.insert(7);
        model.insert(8);
        model.insert(9);
        model.insert(10);

        model.check();
    }

    #[test]
    fn multiple_skipblocks_with_join() {
        let mut model = Model::new();

        model.insert(1);
        model.insert(1);
        model.insert(1);
        model.insert(1);
        model.insert(1);
        model.insert(1);

        model.remove(2);
        model.remove(5);
        model.remove(0);
        model.remove(1);

        model.insert(1);
        model.insert(1);

        model.remove(4);

        model.insert(1);
        model.insert(1);
        model.insert(1);

        model.check();
    }
}