linear-hashtbl 0.1.4

Linear probing hash table
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
//! Partially unsafe `RawTable` API

use core::iter::FusedIterator;
use core::marker::PhantomData;
use core::mem::ManuallyDrop;
use core::mem::MaybeUninit;

#[cfg(all(feature = "nightly", not(feature = "allocator-api2")))]
use alloc::{alloc::Allocator, alloc::Global, boxed::Box, vec::Vec};
#[cfg(feature = "allocator-api2")]
use allocator_api2::{alloc::Allocator, alloc::Global, boxed::Box, vec::Vec};

#[cfg(not(any(feature = "allocator-api2", feature = "nightly")))]
use {
    crate::__alloc::{Allocator, Global},
    alloc::{boxed::Box, vec::Vec},
};

// === Structs =================================================================

/// Raw hash table with a (partially) unsafe API
pub struct RawTable<T, S: Status = usize, A: Allocator + Clone = Global> {
    #[cfg(any(feature = "allocator-api2", feature = "nightly"))]
    data: Box<[Slot<T, S>], A>,
    #[cfg(not(any(feature = "allocator-api2", feature = "nightly")))]
    data: Box<[Slot<T, S>]>,

    /// The number of items in the table
    len: usize,
    /// The number of free slots
    free: usize,

    phantom: PhantomData<A>,
}

struct Slot<T, S: Status = usize> {
    status: S,
    data: MaybeUninit<T>,
}

/// Immutable iterator over the entries of a [`RawTable`] in arbitrary order
///
/// This `struct` is created by [`RawTable::iter()`].
pub struct Iter<'a, T, S: Status = usize> {
    iter: core::slice::Iter<'a, Slot<T, S>>,
    len: usize,
}

/// Mutable iterator over the entries of a [`RawTable`] in arbitrary order
///
/// This `struct` is created by [`RawTable::iter_mut()`].
pub struct IterMut<'a, T, S: Status = usize> {
    iter: core::slice::IterMut<'a, Slot<T, S>>,
    len: usize,
}

/// Owning iterator over the entries of a [`RawTable`] in arbitrary order
///
/// This `struct` is created by the [`IntoIterator::into_iter()`] implementation
/// for `RawTable`. The `RawTable` cannot be used anymore after calling
/// [`IntoIterator::into_iter()`].
pub struct IntoIter<T, S: Status = usize, A: Allocator = Global> {
    #[cfg(feature = "allocator-api2")]
    iter: allocator_api2::vec::IntoIter<Slot<T, S>, A>,
    #[cfg(all(not(feature = "allocator-api2"), feature = "nightly"))]
    iter: alloc::vec::IntoIter<Slot<T, S>, A>,
    #[cfg(not(any(feature = "allocator-api2", feature = "nightly")))]
    iter: alloc::vec::IntoIter<Slot<T, S>>,
    len: usize,
    phantom: PhantomData<A>,
}

/// A draining iterator over the entries of a [`RawTable`]
///
/// This `struct` is created by [`RawTable::drain()`]. While this iterator moves
/// elements out just like [`IntoIter`], it does neither consume the
/// [`RawTable`] nor free its underlying memory.
///
/// Note: Forgetting `Drain` (e.g. via [`core::mem::forget()`]) and using the
/// table afterwards is a very bad idea. It is not `unsafe` but it causes
/// correctness issues since there exist non-empty slots while the length is
/// already set to `0`.
pub struct Drain<'a, T, S: Status = usize> {
    iter: core::slice::IterMut<'a, Slot<T, S>>,
    len: usize,
}

// === Status Impls ============================================================

/// Status of a slot in the hash table
///
/// A status can either be free, a tombstone, or a hash value of the associated
/// item. This can be implemented as a `u32` or a `usize`, for instance. Since
/// there are possibly more `u64` hash values than `Status` hash values, it is
/// fine to apply some mapping (e.g. only take the lowest `n - 1` bits).
///
/// Part of the idea of storing hash values is that we can avoid recomputing the
/// hash values when rehashing (resizing) the table. Consequently, we cannot
/// meaningfully address a table that is larger than the number of `Status` hash
/// values. This is why we have [`Self::check_capacity()`].
///
/// # Safety
///
/// All valid status values are either [`Status::FREE`], a [`Status::TOMBSTONE`]
/// or a hash value. [`Status::from_hash()`] must always return a hash value,
/// and [`Status::is_hash()`] must return `true` if and only if the given
/// `Status` is indeed a hash value.
pub unsafe trait Status: Copy + Eq {
    /// Marker for the slot being free
    const FREE: Self;
    /// Marker that is placed for deleted entries in a collision list
    const TOMBSTONE: Self;

    /// Convert a `u64` hash value (e.g. as returned by
    /// [`Hash`][core::hash::Hash]) into a `Status` hash value
    fn from_hash(hash: u64) -> Self;
    /// Panics if `capacity` exceeds the number of possible `Status` hash values
    fn check_capacity(capacity: usize);
    /// Check if the `Status` is a hash value
    fn is_hash(self) -> bool;
    /// Convert a `Status` hash value into a `usize`
    ///
    /// The result is unspecified if the status is free, a tombstone, or
    /// invalid. In this case, the implementation is allowed to panic.
    fn hash_as_usize(self) -> usize;
}

// SAFETY: `FREE`, `TOMBSTONE`, and hash values are distinct.
unsafe impl Status for usize {
    const FREE: Self = usize::MAX;
    const TOMBSTONE: Self = usize::MAX - 1;

    #[inline]
    fn from_hash(hash: u64) -> Self {
        hash as usize & (usize::MAX >> 1)
    }

    #[inline]
    #[track_caller]
    fn check_capacity(capacity: usize) {
        assert!(
            capacity <= 1 << (usize::BITS - 1),
            "requested capacity {capacity} is too large"
        );
    }

    #[inline]
    fn is_hash(self) -> bool {
        self >> (usize::BITS - 1) == 0 // most significant bit is unset
    }

    #[inline]
    fn hash_as_usize(self) -> usize {
        debug_assert!(self.is_hash());
        self
    }
}

// SAFETY: `FREE`, `TOMBSTONE`, and hash values are distinct.
unsafe impl Status for u32 {
    const FREE: Self = u32::MAX;
    const TOMBSTONE: Self = u32::MAX - 1;

    #[inline]
    fn from_hash(hash: u64) -> Self {
        hash as u32 & (u32::MAX >> 1)
    }

    #[inline]
    #[track_caller]
    fn check_capacity(capacity: usize) {
        assert!(
            capacity <= 1 << (u32::BITS - 1),
            "requested capacity {capacity} is too large"
        );
    }

    #[inline]
    fn is_hash(self) -> bool {
        self >> (u32::BITS - 1) == 0 // most significant bit is unset
    }

    #[inline]
    fn hash_as_usize(self) -> usize {
        debug_assert!(self.is_hash());
        self as usize
    }
}

// === Slot Impls ==============================================================

impl<T, S: Status> Slot<T, S> {
    const FREE: Self = Slot {
        status: S::FREE,
        data: MaybeUninit::uninit(),
    };
}

impl<T: Clone, S: Status> Clone for Slot<T, S> {
    fn clone(&self) -> Self {
        Self {
            status: self.status,
            data: if self.status.is_hash() {
                // SAFETY: hash status means that the data is initialized
                MaybeUninit::new(unsafe { self.data.assume_init_ref() }.clone())
            } else {
                MaybeUninit::uninit()
            },
        }
    }
}

// === RawTable Impls =========================================================

impl<T, S: Status> RawTable<T, S> {
    /// Create a new `HashTable` with zero capacity
    #[inline]
    pub fn new() -> Self {
        RawTable {
            data: Vec::new().into_boxed_slice(),
            len: 0,
            free: 0,
            phantom: PhantomData,
        }
    }

    /// Create a new `HashTable` with capacity for at least `capacity` elements
    pub fn with_capacity(capacity: usize) -> Self {
        let capacity = Self::next_capacity(capacity);
        let mut data = Vec::with_capacity(capacity);
        data.resize_with(capacity, || Slot::FREE);
        RawTable {
            data: data.into_boxed_slice(),
            len: 0,
            free: capacity,
            phantom: PhantomData,
        }
    }
}

#[cfg(any(feature = "allocator-api2", feature = "nightly"))]
impl<T, S: Status, A: Clone + Allocator> RawTable<T, S, A> {
    /// Create a new `HashTable` with zero capacity
    #[inline]
    pub fn new_in(alloc: A) -> Self {
        RawTable {
            data: Vec::new_in(alloc).into_boxed_slice(),
            len: 0,
            free: 0,
            phantom: PhantomData,
        }
    }

    /// Create a new `HashTable` with capacity for at least `capacity` elements
    pub fn with_capacity_in(capacity: usize, alloc: A) -> Self {
        let capacity = Self::next_capacity(capacity);
        let mut data = Vec::with_capacity_in(capacity, alloc);
        data.resize_with(capacity, || Slot::FREE);
        RawTable {
            data: data.into_boxed_slice(),
            len: 0,
            free: capacity,
            phantom: PhantomData,
        }
    }
}

/// Numerator for the fraction of usable slots
const RATIO_N: usize = 3;
/// Denominator for the fraction of usable slots
const RATIO_D: usize = 4;

/// Minimal non-zero capacity (including spare slots)
const MIN_CAP: usize = 16;

impl<T, S: Status, A: Clone + Allocator> RawTable<T, S, A> {
    /// Get the next largest array capacity for `requested` elements
    ///
    /// `requested` does not include the 25 % spare slots, while the returned
    /// value does. The result is guaranteed to be a power of two
    /// (or 0 in case `requested == 0`).
    ///
    /// This uses [`Status::check_capacity()`] to check if we can address the
    /// new array properly.
    #[inline]
    fn next_capacity(requested: usize) -> usize {
        if requested == 0 {
            return 0;
        }
        let capacity = core::cmp::max((requested * RATIO_D / RATIO_N).next_power_of_two(), MIN_CAP);
        S::check_capacity(capacity);
        capacity
    }

    /// Get the number of elements stored in the table
    #[inline(always)]
    pub fn len(&self) -> usize {
        self.len
    }

    /// Returns `true` iff no elements are stored in the table
    pub fn is_empty(&self) -> bool {
        self.len == 0
    }

    /// Get the capacity (excluding spare slots)
    #[inline]
    pub fn capacity(&self) -> usize {
        self.data.len() / RATIO_D * RATIO_N
    }

    /// Get the number of slots (i.e. [`Self::capacity()`] plus spare slots)
    #[inline]
    pub fn slots(&self) -> usize {
        self.data.len()
    }

    /// Reserve space for `additional` elements
    ///
    /// If there are no other modifying operations in between, the next
    /// `additional` insertions are guaranteed to not cause a rehash (or resize)
    /// of the table.
    #[inline]
    pub fn reserve(&mut self, additional: usize) {
        let spare = additional + self.data.len() / RATIO_D * (RATIO_D - RATIO_N);
        if self.free < spare {
            self.reserve_rehash(additional)
        }
    }
    /// Rehash the table with capacity for `self.len + additional` elements
    #[cold]
    fn reserve_rehash(&mut self, additional: usize) {
        let new_cap = Self::next_capacity(self.len + additional);

        #[cfg(any(feature = "allocator-api2", feature = "nightly"))]
        let mut new_data = Vec::with_capacity_in(new_cap, Box::allocator(&self.data).clone());
        #[cfg(not(any(feature = "allocator-api2", feature = "nightly")))]
        let mut new_data = Vec::with_capacity(new_cap);

        new_data.resize_with(new_cap, || Slot::FREE);
        let old_data = core::mem::replace(&mut self.data, new_data.into_boxed_slice());

        if new_cap == 0 {
            self.free = 0;
            return;
        }

        let new_data = &mut self.data[..];
        let new_mask = new_cap - 1;
        // `.into_vec()` is needed for `allocator-api2` boxes
        for slot in old_data.into_vec() {
            let status = slot.status;
            if !status.is_hash() {
                continue;
            }
            // SAFETY: hash status means that the data is initialized
            let data = unsafe { slot.data.assume_init() };

            let mut index = status.hash_as_usize() & new_mask;
            loop {
                // SAFETY: masked index is in bounds (`new_data.len() != 0`)
                let new_slot = unsafe { new_data.get_unchecked_mut(index) };
                if new_slot.status == S::FREE {
                    new_slot.data.write(data);
                    new_slot.status = status;
                    break;
                }
                index = (index + 1) & new_mask;
            }
        }

        self.free = new_cap - self.len;
    }

    /// Clear the table
    ///
    /// This does not change the table's capacity.
    pub fn clear(&mut self) {
        if self.len == 0 {
            return;
        }
        for slot in self.data.iter_mut() {
            let status = slot.status;
            slot.status = S::FREE;
            if status.is_hash() {
                // SAFETY: hash status means that the data is initialized. We
                // marked the slot as `FREE` above, so there is no danger of
                // double frees.
                unsafe { slot.data.assume_init_drop() };
                self.len -= 1;
                if self.len == 0 {
                    return;
                }
            }
        }
        // SAFETY: `self.len <= self.data.len()` holds by invariant
        unsafe { core::hint::unreachable_unchecked() }
    }

    /// [`Self::clear()`] but without dropping any entry
    pub fn clear_no_drop(&mut self) {
        if self.len == 0 {
            return;
        }
        for slot in self.data.iter_mut() {
            let status = slot.status;
            slot.status = S::FREE;
            if status.is_hash() {
                self.len -= 1;
                if self.len == 0 {
                    return;
                }
            }
        }
        // SAFETY: `self.len <= self.data.len()` holds by invariant
        unsafe { core::hint::unreachable_unchecked() }
    }

    /// Like [`Self::clear_no_drop()`], but also sets the capacity to 0
    ///
    /// If the space is not needed anymore, this should generally be faster than
    /// [`Self::clear_no_drop()`], since we do not need to mark every slot as
    /// free.
    #[inline]
    pub fn reset_no_drop(&mut self) {
        self.len = 0;

        #[cfg(any(feature = "allocator-api2", feature = "nightly"))]
        let empty = Vec::new_in(Box::allocator(&self.data).clone());
        #[cfg(not(any(feature = "allocator-api2", feature = "nightly")))]
        let empty = Vec::new();
        self.data = empty.into_boxed_slice();
    }

    /// Returns `true` iff there is an entry at `slot`
    ///
    /// # Safety
    ///
    /// `slot` must be less than `self.slots()`
    #[inline(always)]
    pub unsafe fn is_slot_occupied_unchecked(&self, slot: usize) -> bool {
        debug_assert!(slot < self.data.len());
        // SAFETY: `slot <= self.slots.len()` is ensured by the caller
        unsafe { self.data.get_unchecked(slot) }.status.is_hash()
    }

    /// Find the index of an element or a slot
    #[inline]
    pub fn find(&self, hash: u64, eq: impl Fn(&T) -> bool) -> Option<usize> {
        if self.len == 0 {
            return None;
        }
        debug_assert_ne!(self.free, 0, "find may diverge");

        debug_assert!(self.data.len().is_power_of_two());
        let mask = self.data.len() - 1;
        let mut index = hash as usize & mask;
        let hash_status = S::from_hash(hash);

        loop {
            // SAFETY: masked index is in bounds (`self.data.len() != 0`)
            let slot = unsafe { self.data.get_unchecked(index) };
            if slot.status == hash_status {
                // SAFETY: hash status means that the data is initialized
                if eq(unsafe { slot.data.assume_init_ref() }) {
                    return Some(index);
                }
            } else if slot.status == S::FREE {
                return None;
            }
            index = (index + 1) & mask;
        }
    }

    /// Find the index of an element or a slot to insert it
    ///
    /// Returns `Ok(index)` if the element was found and `Err(index)` for an
    /// insertion slot.
    ///
    /// `eq` is guaranteed to only be called for entries that are present in the
    /// table.
    #[inline]
    pub fn find_or_find_insert_slot(
        &mut self,
        hash: u64,
        eq: impl Fn(&T) -> bool,
    ) -> Result<usize, usize> {
        self.reserve(1);

        debug_assert!(self.data.len().is_power_of_two());
        let mask = self.data.len() - 1;
        let mut index = hash as usize & mask;
        let mut first_tombstone = None;
        let hash_status = S::from_hash(hash);

        loop {
            // SAFETY: masked index is in bounds (`self.data.len() != 0`)
            let slot = unsafe { self.data.get_unchecked(index) };
            if slot.status == hash_status {
                // SAFETY: hash status means that the data is initialized
                if eq(unsafe { slot.data.assume_init_ref() }) {
                    return Ok(index);
                }
            } else if slot.status == S::FREE {
                return Err(first_tombstone.unwrap_or(index));
            } else if slot.status == S::TOMBSTONE && first_tombstone.is_none() {
                first_tombstone = Some(index);
            }
            index = (index + 1) & mask;
        }
    }

    /// Get a reference to an entry in the table
    ///
    /// `hash` is the entry's hash value and `eq` returns true if the given
    /// element is the searched one.
    #[inline]
    pub fn get(&self, hash: u64, eq: impl Fn(&T) -> bool) -> Option<&T> {
        let index = self.find(hash, eq)?;
        debug_assert!(self.data[index].status.is_hash());
        // SAFETY: `find()` guarantees the returned index to be in bounds and
        // the entry there to be occupied.
        Some(unsafe { self.data.get_unchecked(index).data.assume_init_ref() })
    }

    /// Get a mutable reference to an entry in the table
    ///
    /// `hash` is the entry's hash value and `eq` returns true if the given
    /// element is the searched one.
    #[inline]
    pub fn get_mut(&mut self, hash: u64, eq: impl Fn(&T) -> bool) -> Option<&mut T> {
        let index = self.find(hash, eq)?;
        debug_assert!(self.data[index].status.is_hash());
        // SAFETY: `find()` guarantees the returned index to be in bounds and
        // the entry there to be occupied.
        Some(unsafe { self.data.get_unchecked_mut(index).data.assume_init_mut() })
    }

    /// Get a reference to the entry at `slot`
    ///
    /// # Safety
    ///
    /// `slot` must be the index of an occupied slot. This is the case if `slot`
    /// has been returned by [`RawTable::find()`] or
    /// [`RawTable::find_or_find_insert_slot()`] in the `Ok` case, and no
    /// modifications happened in between.
    #[inline]
    pub unsafe fn get_at_slot_unchecked(&self, slot: usize) -> &T {
        debug_assert!(self.data[slot].status.is_hash(), "slot is empty");
        // SAFETY: The caller ensures that `slot` is the index of an occupied
        // slot.
        unsafe { self.data.get_unchecked(slot).data.assume_init_ref() }
    }

    /// Get a mutable reference to the entry entry at `slot`
    ///
    /// # Safety
    ///
    /// `slot` must be the index of an occupied slot. This is the case if `slot`
    /// has been returned by [`RawTable::find()`] or
    /// [`RawTable::find_or_find_insert_slot()`] in the `Ok` case, and no
    /// modifications happened in between.
    #[inline]
    pub unsafe fn get_at_slot_unchecked_mut(&mut self, slot: usize) -> &mut T {
        debug_assert!(self.data[slot].status.is_hash(), "slot is empty");
        // SAFETY: The caller ensures that `slot` is the index of an occupied
        // slot.
        unsafe { self.data.get_unchecked_mut(slot).data.assume_init_mut() }
    }

    /// Insert `val` in `slot`
    ///
    /// `hash` is the hash value of `val`. Returns a mutable reference to the
    /// inserted value.
    ///
    /// # Safety
    ///
    /// `slot` must be the index of an empty slot. This is the case if `slot`
    /// has been returned by [`RawTable::find_or_find_insert_slot()`] in the
    /// `Err` case, and no modifications happened in between.
    #[inline]
    pub unsafe fn insert_in_slot_unchecked(&mut self, hash: u64, slot: usize, val: T) -> &mut T {
        debug_assert!(!self.data[slot].status.is_hash(), "slot is occupied");

        // SAFETY: Validity of the slot is guaranteed by the caller.
        let slot = unsafe { self.data.get_unchecked_mut(slot) };
        if slot.status != S::TOMBSTONE {
            debug_assert!(slot.status == S::FREE);
            self.free -= 1;
        }
        self.len += 1;
        let res = slot.data.write(val);
        slot.status = S::from_hash(hash);
        res
    }

    /// Find and remove an entry from the table, returning it
    ///
    /// `hash` is the entry's hash value and `eq` returns true if the given
    /// element is the searched one.
    #[inline]
    pub fn remove_entry(&mut self, hash: u64, eq: impl Fn(&T) -> bool) -> Option<T> {
        let index = self.find(hash, eq)?;
        // SAFETY: `find()` guarantees the returned index to be in bounds and
        // the entry there to be occupied.
        Some(unsafe { self.remove_at_slot_unchecked(index) })
    }

    /// Remove the entry at `slot`
    ///
    /// Returns the entry value.
    ///
    /// # Safety
    ///
    /// `slot` must be the index of an occupied slot. This is the case if `slot`
    /// has been returned by [`RawTable::find()`] or
    /// [`RawTable::find_or_find_insert_slot()`] in the `Ok` case, and no
    /// modifications happened in between.
    #[inline]
    pub unsafe fn remove_at_slot_unchecked(&mut self, slot: usize) -> T {
        debug_assert_ne!(self.len, 0);
        debug_assert!(self.data[slot].status.is_hash());
        let next_slot_index = (slot + 1) & (self.data.len() - 1);
        // SAFETY (next 2): The caller ensures that `slot` is in bounds, hence
        // `self.data.len() != 0` and `next_slot_index` is in bounds, too.
        let next_slot_status = unsafe { self.data.get_unchecked(next_slot_index) }.status;
        let slot = unsafe { self.data.get_unchecked_mut(slot) };
        slot.status = if next_slot_status == S::FREE {
            self.free += 1;
            S::FREE
        } else {
            S::TOMBSTONE
        };
        self.len -= 1;
        // SAFETY: The slot's status was a hash meaning that the entry is
        // occupied. We set the status to `FREE` or `TOMBSTONE`, so we don't
        // duplicate the value.
        unsafe { slot.data.assume_init_read() }
    }

    /// Get an immutable iterator over the entries of the table
    #[inline]
    pub fn iter(&self) -> Iter<'_, T, S> {
        Iter {
            iter: self.data.iter(),
            len: self.len,
        }
    }

    /// Get a mutable iterator over the entries of the table
    #[inline]
    pub fn iter_mut(&mut self) -> IterMut<'_, T, S> {
        IterMut {
            iter: self.data.iter_mut(),
            len: self.len,
        }
    }

    /// Get a draining iterator over the entries of the table
    ///
    /// A draining iterator removes all elements from the table but does not
    /// change the table's capacity.
    ///
    /// Note: Forgetting the returned `Drain` (e.g., via
    /// [`core::mem::forget()`]) and using the table afterwards is a very
    /// bad idea. It is not `unsafe` but it causes correctness issues since
    /// there exist non-empty slots while the length is already set to `0`.
    #[inline]
    pub fn drain(&mut self) -> Drain<'_, T, S> {
        let len = self.len;
        self.len = 0;
        self.free = self.data.len();
        Drain {
            iter: self.data.iter_mut(),
            len,
        }
    }

    /// Retain only the elements for which `predicate` returns `true`
    ///
    /// `predicate` is guaranteed to only be called for entries that are present
    /// in the table.
    /// `drop` is called for every element that is removed from the table.
    pub fn retain(&mut self, mut predicate: impl FnMut(&mut T) -> bool, mut drop: impl FnMut(T)) {
        if self.len == 0 {
            return;
        }
        debug_assert!(self.data.len() >= self.len);
        let mut i = self.len;
        let mut last_is_free = self.data[0].status == S::FREE;
        // iterate from the back such that we can potentially remove tombstones
        for slot in self.data.iter_mut().rev() {
            if !slot.status.is_hash() {
                if slot.status == S::FREE {
                    last_is_free = true;
                } else {
                    debug_assert!(slot.status == S::TOMBSTONE);
                    if last_is_free {
                        slot.status = S::FREE;
                        self.free += 1;
                    } else {
                        last_is_free = false;
                    }
                }
                continue;
            }
            // SAFETY: The slot's status is a hash value meaning that the data
            // is initialized.
            if !predicate(unsafe { slot.data.assume_init_mut() }) {
                self.len -= 1;
                if last_is_free {
                    slot.status = S::FREE;
                    self.free += 1;
                } else {
                    slot.status = S::TOMBSTONE;
                }
                // SAFETY: `slot.data` is initialized (see above). We set the
                // status to `FREE` or `TOMBSTONE` above, so we don't duplicate
                // the value.
                drop(unsafe { slot.data.assume_init_read() });
            } else {
                last_is_free = false;
            }
            i -= 1;
            if i == 0 {
                if self.len < self.data.len() / RATIO_D * (RATIO_D - RATIO_N)
                    && self.data.len() >= MIN_CAP
                {
                    // shrink the table
                    self.reserve_rehash(0);
                }
                return;
            }
        }
        // SAFETY: `self.len <= self.data.len()` holds by invariant
        unsafe { core::hint::unreachable_unchecked() }
    }
}

impl<T: Clone, S: Status, A: Clone + Allocator> Clone for RawTable<T, S, A> {
    #[inline]
    fn clone(&self) -> Self {
        Self {
            data: self.data.clone(),
            len: self.len,
            free: self.free,
            phantom: PhantomData,
        }
    }
}

impl<T, S: Status, A: Clone + Default + Allocator> Default for RawTable<T, S, A> {
    fn default() -> Self {
        RawTable {
            #[cfg(any(feature = "allocator-api2", feature = "nightly"))]
            data: Vec::new_in(A::default()).into_boxed_slice(),
            #[cfg(not(any(feature = "allocator-api2", feature = "nightly")))]
            data: Vec::new().into_boxed_slice(),
            len: 0,
            free: 0,
            phantom: PhantomData,
        }
    }
}

impl<T, S: Status, A: Clone + Allocator> Drop for RawTable<T, S, A> {
    fn drop(&mut self) {
        if core::mem::needs_drop::<T>() {
            self.clear();
        }
    }
}

impl<T, S: Status, A: Clone + Allocator> IntoIterator for RawTable<T, S, A> {
    type Item = T;
    type IntoIter = IntoIter<T, S, A>;

    fn into_iter(self) -> Self::IntoIter {
        let this = ManuallyDrop::new(self);
        IntoIter {
            // SAFETY: We move out of `this` (`this` is `ManuallyDrop` and never
            // dropped)
            iter: unsafe { core::ptr::read(&this.data) }
                .into_vec()
                .into_iter(),
            len: this.len,
            phantom: PhantomData,
        }
    }
}

// === Iterators ===============================================================

// --- Iter --------------------------------------------------------------------

impl<'a, T, S: Status> Iterator for Iter<'a, T, S> {
    type Item = &'a T;

    #[inline]
    fn next(&mut self) -> Option<&'a T> {
        if self.len == 0 {
            return None;
        }
        loop {
            let next = self.iter.next();
            debug_assert!(next.is_some());
            // SAFETY: The remaining part of the slice has at least `self.len`
            // elements by invariant
            let slot = unsafe { next.unwrap_unchecked() };
            if slot.status.is_hash() {
                self.len -= 1;
                // SAFETY: `slot.status` is a hash value, so `slot.data` is
                // initialized.
                return Some(unsafe { slot.data.assume_init_ref() });
            }
        }
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        (self.len, Some(self.len))
    }
}

impl<T, S: Status> ExactSizeIterator for Iter<'_, T, S> {
    #[inline]
    fn len(&self) -> usize {
        self.len
    }
}

impl<T, S: Status> FusedIterator for Iter<'_, T, S> {}

// --- IterMut -----------------------------------------------------------------

impl<'a, T, S: Status> Iterator for IterMut<'a, T, S> {
    type Item = &'a mut T;

    #[inline]
    fn next(&mut self) -> Option<&'a mut T> {
        if self.len == 0 {
            return None;
        }
        loop {
            let next = self.iter.next();
            debug_assert!(next.is_some());
            // SAFETY: The remaining part of the slice has at least `self.len`
            // elements by invariant
            let slot = unsafe { next.unwrap_unchecked() };
            if slot.status.is_hash() {
                self.len -= 1;
                // SAFETY: `slot.status` is a hash value, so `slot.data` is
                // initialized.
                return Some(unsafe { slot.data.assume_init_mut() });
            }
        }
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        (self.len, Some(self.len))
    }
}

impl<T, S: Status> ExactSizeIterator for IterMut<'_, T, S> {
    #[inline]
    fn len(&self) -> usize {
        self.len
    }
}

impl<T, S: Status> FusedIterator for IterMut<'_, T, S> {}

// --- IntoIter ----------------------------------------------------------------

impl<T, S: Status, A: Allocator> Iterator for IntoIter<T, S, A> {
    type Item = T;

    #[inline]
    fn next(&mut self) -> Option<T> {
        if self.len == 0 {
            return None;
        }
        loop {
            let next = self.iter.next();
            debug_assert!(next.is_some());
            // SAFETY: The remaining part of the vector has at least `self.len`
            // elements by invariant
            let slot = unsafe { next.unwrap_unchecked() };
            if slot.status.is_hash() {
                self.len -= 1;
                // SAFETY: `slot.status` is a hash value, so `slot.data` is
                // initialized.
                return Some(unsafe { slot.data.assume_init() });
            }
        }
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        (self.len, Some(self.len))
    }
}

impl<T, S: Status, A: Allocator> ExactSizeIterator for IntoIter<T, S, A> {
    #[inline]
    fn len(&self) -> usize {
        self.len
    }
}

impl<T, S: Status, A: Allocator> FusedIterator for IntoIter<T, S, A> {}

impl<T, S: Status, A: Allocator> Drop for IntoIter<T, S, A> {
    fn drop(&mut self) {
        while self.len != 0 {
            let next = self.iter.next();
            debug_assert!(next.is_some());
            // SAFETY: The remaining part of the vector has at least `self.len`
            // elements by invariant
            let mut slot = unsafe { next.unwrap_unchecked() };
            if slot.status.is_hash() {
                self.len -= 1;
                // SAFETY: `slot.status` is a hash value, so `slot.data` is
                // initialized. We drop/forget `slot`, so we don't risk a double
                // free.
                unsafe { slot.data.assume_init_drop() };
            }
        }
    }
}

// --- Drain -------------------------------------------------------------------

impl<T, S: Status> Iterator for Drain<'_, T, S> {
    type Item = T;

    #[inline]
    fn next(&mut self) -> Option<T> {
        if self.len == 0 {
            return None;
        }
        loop {
            let next = self.iter.next();
            debug_assert!(next.is_some());
            // SAFETY: The remaining part of the slice has at least `self.len`
            // elements by invariant
            let slot = unsafe { next.unwrap_unchecked() };
            let status = slot.status;
            slot.status = S::FREE;
            if status.is_hash() {
                self.len -= 1;
                // SAFETY: `slot.status` is a hash value, so `slot.data` is
                // initialized. We set `slot.status = S::FREE` above, so we
                // don't duplicate the value.
                return Some(unsafe { slot.data.assume_init_read() });
            }
        }
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        (self.len, Some(self.len))
    }
}

impl<T, S: Status> ExactSizeIterator for Drain<'_, T, S> {
    #[inline]
    fn len(&self) -> usize {
        self.len
    }
}

impl<T, S: Status> FusedIterator for Drain<'_, T, S> {}

impl<T, S: Status> Drop for Drain<'_, T, S> {
    fn drop(&mut self) {
        while self.len != 0 {
            let next = self.iter.next();
            debug_assert!(next.is_some());
            // SAFETY: The remaining part of the slice has at least `self.len`
            // elements by invariant
            let slot = unsafe { next.unwrap_unchecked() };
            let status = slot.status;
            slot.status = S::FREE;
            if status.is_hash() {
                self.len -= 1;
                // SAFETY: `slot.status` is a hash value, so `slot.data` is
                // initialized. We set `slot.status = S::FREE` above, so we
                // don't drop the value twice.
                unsafe { slot.data.assume_init_drop() };
            }
        }
    }
}

// === Tests ===================================================================

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn new_cap_len_0() {
        let table = RawTable::<u32, u32>::new();
        assert_eq!(table.capacity(), 0);
        assert_eq!(table.len(), 0);
    }

    #[test]
    fn insert_get_iter() {
        let mut table = RawTable::<u32, u32>::new();
        let numbers = [1, 4, 0, 5, 14, 4];
        let sorted = [0, 1, 4, 5, 14];

        for number in numbers {
            match table.find_or_find_insert_slot(number as u64, |&x| x == number) {
                Ok(_) => assert_eq!(number, 4),
                Err(slot) => unsafe {
                    table.insert_in_slot_unchecked(number as u64, slot, number);
                },
            }
        }
        assert_eq!(table.len(), 5);
        assert!(table.capacity() >= 5);

        for mut number in numbers {
            assert_eq!(table.get(number as u64, |&x| x == number), Some(&number));
            assert_eq!(
                table.get_mut(number as u64, |&x| x == number),
                Some(&mut number)
            );
        }

        let iter = table.iter();
        assert_eq!(iter.len(), 5);
        let mut res: Vec<u32> = iter.copied().collect();
        res.sort();
        assert_eq!(&res[..], &sorted[..]);

        let iter = table.iter_mut();
        assert_eq!(iter.len(), 5);
        let mut res: Vec<u32> = iter.map(|&mut x| x).collect();
        res.sort();
        assert_eq!(&res[..], &sorted[..]);

        let iter = table.into_iter();
        assert_eq!(iter.len(), 5);
        let mut res: Vec<u32> = iter.collect();
        res.sort();
        assert_eq!(&res[..], &sorted[..]);
    }

    #[test]
    fn retain_drain() {
        let mut table = RawTable::<u32, u32>::new();
        let numbers = [1, 2, 3, 4, 5, 6, 7];

        for number in numbers {
            match table.find_or_find_insert_slot(number as u64, |&x| x == number) {
                Ok(_) => unreachable!(),
                Err(slot) => unsafe {
                    table.insert_in_slot_unchecked(number as u64, slot, number);
                },
            }
        }
        assert_eq!(table.len(), 7);

        table.retain(
            |&mut x| x.is_multiple_of(2),
            |x| assert!(!x.is_multiple_of(2)),
        );
        assert_eq!(table.len(), 3);

        let iter = table.drain();
        assert_eq!(iter.len(), 3);
        let mut res: Vec<u32> = iter.collect();
        res.sort();
        assert_eq!(&res[..], &[2, 4, 6]);
        assert_eq!(table.len(), 0);
    }

    #[test]
    fn remove() {
        let mut table = RawTable::<u32, u32>::new();
        let numbers = [4, 0, 2];

        for number in numbers {
            match table.find_or_find_insert_slot(number as u64, |&x| x == number) {
                Ok(_) => unreachable!(),
                Err(slot) => unsafe {
                    table.insert_in_slot_unchecked(number as u64, slot, number);
                },
            }
        }
        assert_eq!(table.len(), 3);

        assert_eq!(table.remove_entry(0, |&x| x == 0), Some(0));
        assert_eq!(table.len(), 2);
        assert_eq!(table.remove_entry(0, |&x| x == 0), None);
        assert_eq!(table.len(), 2);
    }
}