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
//! A hash map that can be safely persisted within objects.
//!
//! This is a prototype implementation of a safely movable hash map type, implementing a (small)
//! subset of the [`std::collections::HashMap`] hash map type. There has been no effort to optimize
//! any aspect of this implementation, so predictable performance should not be expected.
use std::alloc::{Allocator, Layout};
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
use std::iter::Chain;
use std::marker::PhantomData;
use std::mem::MaybeUninit;
use std::ptr::NonNull;
#[cfg(feature = "simd")]
use std::simd;
use std::sync::Arc;

use parking_lot::RwLock;

use crate::allocators::bump_allocator::BumpAllocator;
use crate::allocators::persistently_allocatable::{CopyFrom, PersistentlyAllocatable};
use crate::collections::pvec;
use crate::tls as nando_tls;
use crate::Persistable;

macro_rules! get_bucket_for_iter {
    ($target:expr, $idx:expr) => {{
        std::ptr::addr_of!($target.table_offset)
            .byte_offset(
                ($target.table_offset + $idx * std::mem::size_of::<Bucket<K, V>>())
                    .try_into()
                    .unwrap(),
            )
            .cast_mut()
            .cast::<Bucket<K, V>>()
    }};
}

#[repr(C)]
#[doc(hidden)]
pub struct BucketEntry<K, V>
where
    K: Persistable,
    V: Persistable,
{
    key: K,
    value: V,
}

impl<K, V> Persistable for BucketEntry<K, V>
where
    K: Persistable,
    V: Persistable,
{
}

/// The initial capacity to set for newly allocated vectors backing Bucket instances.
///
/// This will be parameterizable in the future.
static BUCKET_AVERAGE_LENGTH: u8 = 20;

#[repr(C)]
#[doc(hidden)]
struct Bucket<K: Persistable, V: Persistable> {
    empty_slots: pvec::PVec<usize>,

    // NOTE the reason why the vector containing the bucket's content is not owned by the Bucket
    // is that, on resize, we want to avoid moving the contents to the new bucket that these keys
    // will hash to. Storing an offset to the vector allows us to just set `buf_offset` for the new
    // bucket and be done.
    buf: PhantomData<pvec::PVec<BucketEntry<K, V>>>,
    buf_offset: isize,
}

impl<K, V> Persistable for Bucket<K, V>
where
    K: Persistable,
    V: Persistable,
{
}

impl<K, V> Bucket<K, V>
where
    K: Eq + Default + Persistable + PersistentlyAllocatable + std::cmp::PartialEq,
    V: Persistable,
{
    fn insert<T>(&mut self, k: T, v: V)
    where
        K: CopyFrom<From = T> + PartialEq<T>,
    {
        if self.buf_offset == 0 {
            panic!("attempt to insert into uninitialized bucket");
        }

        let vector = unsafe {
            std::ptr::addr_of!(self.buf_offset)
                .byte_offset(self.buf_offset.try_into().unwrap())
                .cast_mut()
                .cast::<pvec::PVec<BucketEntry<K, V>>>()
        };

        // FIXME should be calling `to_bytes()` for both the key and the value.
        unsafe {
            match (&*vector).find(|e| e.key == k) {
                Some(idx) => {
                    #[cfg(debug_assertions)]
                    println!("found at {idx}, will replace");

                    let value_ptr = crate::unit_ptr_of!(&(&mut *vector)[idx].value);
                    nando_tls::add_new_pre_image(value_ptr, (&mut *vector)[idx].value.as_bytes());
                    (&mut *vector)[idx].value = v;
                    nando_tls::add_new_post_image_if_changed(
                        value_ptr,
                        (&mut *vector)[idx].value.as_bytes(),
                    );
                }
                None => {
                    let key = {
                        let mut key = K::default();
                        let allocator = Arc::clone(&(*vector).get_allocator().unwrap());
                        key.set_allocator(allocator);

                        key
                    };

                    let idx = match self.empty_slots.pop() {
                        Some(i) => {
                            (*vector).push_at(BucketEntry { key, value: v }, i.into());
                            i
                        }
                        None => {
                            (*vector).push(BucketEntry { key, value: v });
                            (*vector).len() - 1
                        }
                    };

                    // NOTE For the underlying offset math to work out, we first need to place the
                    // entry in its final slot in the vector, and only then copy over the original
                    // key into the newly allocated key: PString. This is definitely a mistake
                    // of the current design.
                    let entry = &mut (*vector)[idx];
                    entry.key.from(k);
                }
            }
        };
    }

    unsafe fn get_vector(&self) -> &mut pvec::PVec<BucketEntry<K, V>> {
        let vector = std::ptr::addr_of!(self.buf_offset)
            .byte_offset(self.buf_offset)
            .cast_mut()
            .cast::<pvec::PVec<BucketEntry<K, V>>>();

        &mut *vector
    }

    fn set_vector(&mut self, vector_ptr: *mut pvec::PVec<BucketEntry<K, V>>) {
        let bucket_buf_offset_addr = std::ptr::addr_of!(self.buf_offset) as usize;
        let vector_addr = vector_ptr as *const () as usize;

        let computed_offset = if vector_addr > bucket_buf_offset_addr {
            (vector_addr - bucket_buf_offset_addr).try_into().unwrap()
        } else {
            -1isize
                * <usize as TryInto<isize>>::try_into(bucket_buf_offset_addr - vector_addr).unwrap()
        };

        let buf_offset_ptr = crate::unit_ptr_of!(&self.buf_offset);
        nando_tls::add_new_pre_image(buf_offset_ptr, self.buf_offset.as_bytes());
        self.buf_offset = computed_offset;
        nando_tls::add_new_post_image_if_changed(buf_offset_ptr, self.buf_offset.as_bytes());
    }

    fn clear(&mut self) {
        let vec = unsafe { self.get_vector() };
        vec.truncate(0);
    }
}

pub struct BucketIter<'a, K, V>
where
    K: Persistable,
    V: Persistable,
{
    inner: pvec::PVecIter<'a, BucketEntry<K, V>>,
}

impl<'a, K, V> Iterator for BucketIter<'a, K, V>
where
    K: Persistable,
    V: Persistable,
{
    type Item = (&'a K, &'a V);

    fn next(&mut self) -> Option<Self::Item> {
        match self.inner.next() {
            None => None,
            Some(ref entry) => Some((&entry.key, &entry.value)),
        }
    }
}

impl<'a, K, V> IntoIterator for &'a Bucket<K, V>
where
    K: Persistable,
    V: Persistable,
{
    type Item = (&'a K, &'a V);

    type IntoIter = BucketIter<'a, K, V>;

    fn into_iter(self) -> Self::IntoIter {
        let vector = unsafe {
            std::ptr::addr_of!(self.buf_offset)
                .byte_offset(self.buf_offset)
                .cast_mut()
                .cast::<pvec::PVec<BucketEntry<K, V>>>()
                .as_ref()
        };

        BucketIter {
            inner: vector.unwrap().into_iter(),
        }
    }
}

// TODO we should probably make geometric resizing the default and just get rid of this enum
#[allow(dead_code)]
#[doc(hidden)]
enum ResizePolicy {
    Conservative(usize),
    Geometric(usize),
}

#[repr(C)]
/// A persistable hash map, which is safely movable between runtime instances.
///
/// This implementation leverages the [`PVec`] implementation to provide key/value pair backing
/// storage for a collection of Buckets. Note that because of how resizing is currently performed,
/// and the use of a stateless bump allocator to manage allocations, we end up leaking the space
/// for all the Bucket tables we had allocated for all past capacities.
///
/// [`PVec`]: ../pvec/struct.PVec.html
#[derive(Debug)]
pub struct PHashMap<K: Persistable, V: Persistable> {
    num_elements: usize,
    capacity: usize,
    num_buckets: usize,
    table: PhantomData<Bucket<K, V>>,
    table_offset: usize,

    #[doc(hidden)]
    allocator: MaybeUninit<Arc<RwLock<BumpAllocator>>>,
}

impl<K, V> Persistable for PHashMap<K, V>
where
    K: Persistable,
    V: Persistable,
{
}

impl<K, V> PHashMap<K, V>
where
    K: Eq + Default + Hash + Persistable + PersistentlyAllocatable,
    V: Persistable,
{
    /// Instantiates a new empty hashmap. No allocations are made as a result of this call.
    pub fn new() -> Self {
        Self {
            num_elements: 0,
            capacity: 0,
            num_buckets: 0,
            table: PhantomData,
            table_offset: 0,
            allocator: MaybeUninit::uninit(),
        }
    }

    #[doc(hidden)]
    fn get_allocator_internal(&'_ self) -> &'_ Arc<RwLock<BumpAllocator>> {
        // NOTE This should not be called before `set_allocator()` has been called for a newly
        // instantiated object.
        unsafe { self.allocator.assume_init_ref() }
    }

    #[doc(hidden)]
    pub fn set_hasher(&mut self) {
        todo!("set_hasher")
    }

    /// Returns the number of elements in the map.
    pub fn len(&self) -> usize {
        self.num_elements
    }

    /// Allocates space for the [`PHashMap`] to hold at least `capacity` elements before a
    /// reallocation is needed.
    pub fn with_capacity(&mut self, capacity: usize) {
        let capacity_ptr = crate::unit_ptr_of!(&self.capacity);
        nando_tls::add_new_pre_image(capacity_ptr, self.capacity.as_bytes());

        let (table_offset, num_buckets) = {
            let (allocation_res, adjusted_capacity, num_buckets) =
                self.allocate_table_with_capacity(capacity);
            self.capacity = std::cmp::max(capacity, adjusted_capacity);

            let base = std::ptr::addr_of!(self.table_offset) as *const ();
            (
                (allocation_res.as_ptr() as *const () as usize) - (base as usize),
                num_buckets,
            )
        };

        nando_tls::add_new_post_image_if_changed(capacity_ptr, self.capacity.as_bytes());

        let table_offset_ptr = crate::unit_ptr_of!(&self.table_offset);
        nando_tls::add_new_pre_image(table_offset_ptr, self.table_offset.as_bytes());
        self.table_offset = table_offset;
        nando_tls::add_new_post_image_if_changed(table_offset_ptr, self.table_offset.as_bytes());

        let num_buckets_ptr = crate::unit_ptr_of!(&self.num_buckets);
        nando_tls::add_new_pre_image(num_buckets_ptr, self.num_buckets.as_bytes());
        self.num_buckets = num_buckets;
        nando_tls::add_new_post_image_if_changed(num_buckets_ptr, self.num_buckets.as_bytes());

        // allocate a contiguous region for the vectors of the above buckets
        let vector_start = self
            .allocate_space_for_backing_vectors(num_buckets)
            .as_ptr();

        let allocator = self.get_allocator_internal();
        for i in 0..self.num_buckets {
            unsafe {
                let bucket_addr = std::ptr::addr_of!(self.table_offset)
                    .byte_offset(
                        (self.table_offset + i * std::mem::size_of::<Bucket<K, V>>())
                            .try_into()
                            .unwrap(),
                    )
                    .cast_mut()
                    .cast::<Bucket<K, V>>();

                nando_tls::add_new_pre_image(bucket_addr as *const (), (*bucket_addr).as_bytes());
                // initialize them with the avg per bucket capacity
                *bucket_addr = Bucket {
                    empty_slots: pvec::PVec::new(),
                    buf: PhantomData,
                    buf_offset: 0,
                };

                (*bucket_addr)
                    .empty_slots
                    .set_allocator(Arc::clone(&allocator));
                (*bucket_addr)
                    .empty_slots
                    .resize_to_capacity((BUCKET_AVERAGE_LENGTH).into());

                let vector_addr = vector_start.byte_offset(
                    (i * std::mem::size_of::<pvec::PVec<BucketEntry<K, V>>>())
                        .try_into()
                        .unwrap(),
                );

                // NOTE @unstable the below should always give us a correct result, as, at this point, the vector
                // allocation is guaranteed to be further down the object than the address of the
                // Bucket table. That might break in the future, if we change the implementation of
                // the bump allocator, or use a different allocator.
                (*bucket_addr).buf_offset = (vector_addr as *const () as usize
                    - std::ptr::addr_of!((*bucket_addr).buf_offset) as usize)
                    .try_into()
                    .unwrap();
                nando_tls::add_new_post_image_if_changed(
                    bucket_addr as *const (),
                    (*bucket_addr).as_bytes(),
                );
            }
        }
    }

    /// Inserts a key-value pair into the [`PHashMap`], resizing if necessary.
    ///
    /// If the element was already in the [`PHashMap`], the entry for the key is updated, and the
    /// old value is returned. Otherwise, `None` is returned.
    // TODO set the allocator for the entry
    pub fn insert<T>(&mut self, k: T, v: V) -> Option<V>
    where
        T: std::hash::Hash,
        K: CopyFrom<From = T> + PartialEq<T>,
    {
        // FIXME this will repeat the hashing process from below to return the result, should
        // do some rewriting to avoid this kind of thing.
        let old_value = match self.get(&k) {
            None => None,
            Some(v) => Some(unsafe { std::mem::transmute_copy(v) }),
        };

        // First, we need to check if we have reached capacity
        if old_value.is_none() && self.under_pressure() {
            #[cfg(debug_assertions)]
            println!(
                "will resize, current num elements is {}, capacity is {}",
                self.num_elements, self.capacity
            );
            // double our capacity
            unsafe { self.resize(ResizePolicy::Geometric(2)) };
        }

        let index: usize = self.get_hash_for_other_key(&k);
        let bucket = unsafe { self.get_bucket_at_index(index) };

        bucket.insert(k, v);

        // Only update the number of elements if we inserted a new key as opposed to overwriting a
        // previous value.
        if old_value.is_none() {
            let num_elements_ptr = crate::unit_ptr_of!(&self.num_elements);
            nando_tls::add_new_pre_image(num_elements_ptr, self.num_elements.as_bytes());
            self.num_elements += 1;
            nando_tls::add_new_post_image_if_changed(
                num_elements_ptr,
                self.num_elements.as_bytes(),
            );
        }

        old_value
    }

    pub fn clear(&mut self) {
        let num_elements_ptr = crate::unit_ptr_of!(&self.num_elements);
        nando_tls::add_new_pre_image(num_elements_ptr, self.num_elements.as_bytes());
        self.num_elements = 0;
        nando_tls::add_new_post_image_if_changed(num_elements_ptr, self.num_elements.as_bytes());

        for bucket_idx in 0..self.num_buckets {
            let bucket = unsafe { self.get_bucket_at_index(bucket_idx) };
            bucket.clear();
        }
    }

    /// Returns a reference to the value corresponding to the key.
    pub fn get<T>(&self, k: &T) -> Option<&V>
    where
        T: std::hash::Hash,
        K: CopyFrom<From = T> + PartialEq<T>,
    {
        let index: usize = self.get_hash_for_other_key::<T>(k);
        let bucket = unsafe { self.get_bucket_at_index(index) };
        let bucket_storage = unsafe { bucket.get_vector() };

        for e in bucket_storage.iter() {
            if e.key == *k {
                return Some(&e.value);
            }
        }

        None
    }

    fn get_pkey(&self, k: &K) -> Option<&V> {
        let index: usize = self.get_hash_for_key(k);
        let bucket = unsafe { self.get_bucket_at_index(index) };
        let bucket_storage = unsafe { bucket.get_vector() };

        for e in bucket_storage.iter() {
            if e.key == *k {
                return Some(&e.value);
            }
        }

        None
    }

    /// Returns a mutable reference to the value corresponding to the (persistable) key.
    pub fn get_mut(&self, k: &K) -> Option<&mut V> {
        let index: usize = self.get_hash_for_key(k);
        let bucket = unsafe { self.get_bucket_at_index(index) };
        let bucket_storage = unsafe { bucket.get_vector() };

        for e in bucket_storage.iter_mut() {
            if e.key == *k {
                return Some(&mut e.value);
            }
        }

        None
    }

    // FIXME bad name
    /// Returns a mutable reference to the value corresponding to the key.
    pub fn get_mut_non_persistent_key<T>(&self, k: &T) -> Option<&mut V>
    where
        T: std::hash::Hash,
        K: CopyFrom<From = T> + PartialEq<T>,
    {
        let index: usize = self.get_hash_for_other_key::<T>(k);
        let bucket = unsafe { self.get_bucket_at_index(index) };
        let bucket_storage = unsafe { bucket.get_vector() };

        for (idx, e) in bucket_storage.iter().enumerate() {
            if e.key == *k {
                return Some(&mut bucket_storage[idx].value);
            }
        }

        None
    }

    /// Returns `true` if the map contains a value for the specified key.
    pub fn contains(&self, k: &K) -> bool {
        let index: usize = self.get_hash_for_key(&k);
        let bucket = unsafe { self.get_bucket_at_index(index) };
        let bucket_storage = unsafe { bucket.get_vector() };

        for e in bucket_storage.iter() {
            if e.key == *k {
                return true;
            }
        }

        false
    }

    /// Returns `true` if the map contains a value for the specified key. Note that this supports
    /// keys that can be converted into the native key type (unlike [`contains`]).
    pub fn contains_key<T>(&self, k: &T) -> bool
    where
        T: std::hash::Hash,
        K: CopyFrom<From = T> + PartialEq<T>,
    {
        let index: usize = self.get_hash_for_other_key::<T>(k);
        let bucket = unsafe { self.get_bucket_at_index(index) };
        let bucket_storage = unsafe { bucket.get_vector() };

        for e in bucket_storage.iter() {
            if e.key == *k {
                return true;
            }
        }

        false
    }

    #[doc(hidden)]
    fn allocate_table_with_capacity(&self, capacity: usize) -> (NonNull<[u8]>, usize, usize) {
        let allocator = self.get_allocator_internal();

        // target a load factor of 0.8
        let bucket_avg_length = BUCKET_AVERAGE_LENGTH as usize;
        let (num_buckets, adjusted_capacity) = match capacity > bucket_avg_length {
            true => (
                ((capacity / bucket_avg_length) * 5 / 4).next_power_of_two(),
                capacity,
            ),
            false => {
                let num_buckets = (bucket_avg_length * 5 / 4).next_power_of_two();
                let adjusted_capacity = num_buckets * bucket_avg_length * 4 / 5;

                (num_buckets, adjusted_capacity)
            }
        };

        let source_layout = Layout::array::<Bucket<K, V>>(num_buckets).unwrap();
        let allocator = allocator.read();

        (
            allocator
                .allocate(source_layout)
                .expect("failed to allocate in `with_capacity`"),
            adjusted_capacity,
            num_buckets,
        )
    }

    #[doc(hidden)]
    fn allocate_space_for_backing_vectors(&self, num_vectors: usize) -> NonNull<[u8]> {
        let allocator = self.get_allocator_internal();

        let source_layout = Layout::array::<pvec::PVec<BucketEntry<K, V>>>(num_vectors).unwrap();
        let vector_start = {
            let allocator = allocator.read();

            allocator
                .allocate(source_layout)
                .expect("failed to allocate in `with_capacity`")
        };

        for i in 0..num_vectors {
            unsafe {
                let vector_addr = vector_start.as_ptr().byte_offset(
                    (i * std::mem::size_of::<pvec::PVec<BucketEntry<K, V>>>())
                        .try_into()
                        .unwrap(),
                );
                let vector_mut = (vector_addr as *const ())
                    .cast_mut()
                    .cast::<pvec::PVec<BucketEntry<K, V>>>();

                nando_tls::add_new_pre_image(vector_mut as *const (), (&*vector_mut).as_bytes());
                *vector_mut = pvec::PVec::new();
                (*vector_mut).set_allocator(Arc::clone(&allocator));
                (*vector_mut).resize_to_capacity(BUCKET_AVERAGE_LENGTH.into());
                nando_tls::add_new_post_image_if_changed(
                    vector_mut as *const (),
                    (&*vector_mut).as_bytes(),
                );
            }
        }

        vector_start
    }

    #[doc(hidden)]
    fn under_pressure(&self) -> bool {
        self.num_elements as f64 / self.capacity as f64 >= 0.9
    }

    #[doc(hidden)]
    fn get_hash_for_key<T>(&self, key: &T) -> usize
    where
        T: std::hash::Hash,
        K: From<T>,
    {
        let mut hasher = DefaultHasher::default();
        let key_hash = {
            key.hash(&mut hasher);
            hasher.finish()
        } as usize;

        key_hash % self.num_buckets
    }

    fn get_hash_for_other_key<T>(&self, key: &T) -> usize
    where
        T: std::hash::Hash,
        K: CopyFrom<From = T>,
    {
        let mut hasher = DefaultHasher::default();
        let key_hash = {
            key.hash(&mut hasher);
            hasher.finish()
        } as usize;

        key_hash % self.num_buckets
    }

    unsafe fn get_bucket_at_index_ptr(&self, idx: usize) -> *mut Bucket<K, V> {
        std::ptr::addr_of!(self.table_offset)
            .byte_offset(
                (self.table_offset + idx * std::mem::size_of::<Bucket<K, V>>())
                    .try_into()
                    .unwrap(),
            )
            .cast_mut()
            .cast::<Bucket<K, V>>()
    }

    #[doc(hidden)]
    unsafe fn get_bucket_at_index(&self, idx: usize) -> &mut Bucket<K, V> {
        &mut *self.get_bucket_at_index_ptr(idx)
    }

    // FIXME clean up
    #[doc(hidden)]
    unsafe fn resize(&mut self, resize_policy: ResizePolicy) {
        let capacity = match resize_policy {
            ResizePolicy::Conservative(to_add) => self.capacity + to_add,
            ResizePolicy::Geometric(to_multiply) => self.capacity * to_multiply,
        };

        let capacity_ptr = crate::unit_ptr_of!(&self.capacity);
        nando_tls::add_new_pre_image(capacity_ptr, self.capacity.as_bytes());

        // first allocate a new table with the new capacity to hold the new buckets
        let (table_offset, num_buckets) = {
            let (allocation_res, adjusted_capacity, num_buckets) =
                self.allocate_table_with_capacity(capacity);

            self.capacity = std::cmp::max(capacity, adjusted_capacity);

            let base = std::ptr::addr_of!(self.table_offset) as *const ();
            (
                (allocation_res.as_ptr() as *const () as usize) - (base as usize),
                num_buckets,
            )
        };

        nando_tls::add_new_post_image_if_changed(capacity_ptr, self.capacity.as_bytes());

        let table_offset_old = self.table_offset;
        let num_buckets_old = self.num_buckets;

        let table_offset_ptr = crate::unit_ptr_of!(&self.table_offset);
        nando_tls::add_new_pre_image(table_offset_ptr, self.table_offset.as_bytes());
        self.table_offset = table_offset;
        nando_tls::add_new_post_image_if_changed(table_offset_ptr, self.table_offset.as_bytes());

        let num_buckets_ptr = crate::unit_ptr_of!(&self.num_buckets);
        nando_tls::add_new_pre_image(num_buckets_ptr, self.num_buckets.as_bytes());
        self.num_buckets = num_buckets;
        nando_tls::add_new_post_image_if_changed(num_buckets_ptr, self.num_buckets.as_bytes());

        // allocate enough space for the new vectors, since we will be reusing the previously
        // allocated vectors after we're done rehashing their old contents.
        let allocator = self.get_allocator_internal();
        let num_new_vectors = self.num_buckets - num_buckets_old + 1;
        let vector_start = self
            .allocate_space_for_backing_vectors(num_new_vectors)
            .as_ptr();
        let mut last_used_vector = 0;

        for i in 0..num_buckets {
            let bucket = self.get_bucket_at_index(i);
            nando_tls::add_new_pre_image(
                std::ptr::addr_of!(*bucket) as *const (),
                (*bucket).as_bytes(),
            );
            *bucket = Bucket {
                empty_slots: pvec::PVec::new(),
                buf: PhantomData,
                buf_offset: 0,
            };
            (*bucket).empty_slots.set_allocator(Arc::clone(&allocator));
            (*bucket)
                .empty_slots
                .resize_to_capacity((BUCKET_AVERAGE_LENGTH).into());
            nando_tls::add_new_post_image_if_changed(
                std::ptr::addr_of!(*bucket) as *const (),
                (*bucket).as_bytes(),
            );
        }

        let mut reusable_vectors: Vec<&mut pvec::PVec<BucketEntry<K, V>>> = vec![];

        // rehash old keys, move to new map
        for old_bucket_idx in 0..num_buckets_old {
            let old_bucket = std::ptr::addr_of!(self.table_offset)
                .byte_offset(
                    (table_offset_old + old_bucket_idx * std::mem::size_of::<Bucket<K, V>>())
                        .try_into()
                        .unwrap(),
                )
                .cast_mut()
                .cast::<Bucket<K, V>>();

            let target_vector = (*old_bucket).get_vector();
            let mut target_vector_reassigned = false;

            for j in 0..target_vector.len() {
                let entry = match target_vector.get_ref(j) {
                    None => continue,
                    Some(p) => p.as_ref().unwrap(),
                };

                let new_idx = self.get_hash_for_key(&(*entry).key);
                let new_bucket = std::ptr::addr_of!(self.table_offset)
                    .byte_offset(
                        (self.table_offset + new_idx * std::mem::size_of::<Bucket<K, V>>())
                            .try_into()
                            .unwrap(),
                    )
                    .cast_mut()
                    .cast::<Bucket<K, V>>();

                nando_tls::add_new_pre_image(new_bucket as *const (), (&*new_bucket).as_bytes());

                if (*new_bucket).buf_offset == 0 {
                    let new_bucket_vector_ptr =
                        if target_vector.len() == (*old_bucket).empty_slots.len() {
                            target_vector.truncate(0);
                            target_vector_reassigned = true;

                            std::ptr::addr_of!(*target_vector).cast_mut()
                        } else if reusable_vectors.len() > 0 {
                            let vector_to_reuse = reusable_vectors.pop().unwrap();
                            std::ptr::addr_of!(*vector_to_reuse).cast_mut()
                        } else {
                            if last_used_vector >= num_new_vectors {
                                panic!(
                                    "about to use unallocated vector {} / {}",
                                    last_used_vector,
                                    self.num_buckets - num_buckets_old
                                );
                            }

                            let vector_addr = vector_start.byte_offset(
                                (last_used_vector
                                    * std::mem::size_of::<pvec::PVec<BucketEntry<K, V>>>())
                                .try_into()
                                .unwrap(),
                            );
                            last_used_vector += 1;

                            (vector_addr as *const ())
                                .cast_mut()
                                .cast::<pvec::PVec<BucketEntry<K, V>>>()
                        };

                    (*new_bucket).set_vector(new_bucket_vector_ptr);
                }

                nando_tls::add_new_post_image_if_changed(
                    new_bucket as *const (),
                    (&*new_bucket).as_bytes(),
                );

                // finally, move the rehashed entry
                let entry_to_place = match target_vector.pop_at(j) {
                    None => continue,
                    Some(e) => {
                        (*old_bucket).empty_slots.push(j);
                        e
                    }
                };

                let target_vector = (*new_bucket).get_vector();
                let placed_entry = target_vector.push(entry_to_place);
                (&mut *placed_entry).key.adjust_from(&(*entry).key);
                (&mut *placed_entry).value.adjust_from(&(*entry).value);
            }

            if !target_vector_reassigned {
                target_vector.truncate(0);
                reusable_vectors.push(target_vector);
            }
        }

        // at this point there are buckets that have not had their `buf_offset` set to an
        // appropriate value because none of the pre-existing keys ended up hashing into them. We
        // need to fix that.
        for i in 0..num_buckets {
            let bucket = self.get_bucket_at_index(i);
            if bucket.buf_offset != 0 {
                // bucket has already been initialized during rehashing, nothing to do.
                continue;
            }

            let vector_addr = match reusable_vectors.pop() {
                Some(v) => std::ptr::addr_of!(*v).cast_mut(),
                None => {
                    if last_used_vector >= num_new_vectors {
                        panic!(
                            "about to use unallocated vector {} / {}",
                            last_used_vector,
                            self.num_buckets - num_buckets_old
                        );
                    }

                    let vector_addr = (vector_start.byte_offset(
                        (last_used_vector * std::mem::size_of::<pvec::PVec<BucketEntry<K, V>>>())
                            .try_into()
                            .unwrap(),
                    ) as *const ())
                        .cast_mut()
                        .cast::<pvec::PVec<BucketEntry<K, V>>>();
                    last_used_vector += 1;

                    vector_addr
                }
            };

            bucket.set_vector(vector_addr);

            nando_tls::add_new_post_image_if_changed(
                std::ptr::addr_of!(*bucket) as *const (),
                (*bucket).as_bytes(),
            );
        }
    }

    pub fn iter(&self) -> PHashMapIter<'_, K, V> {
        let bucket_for_iter = unsafe { &mut *get_bucket_for_iter!(self, 0) };

        PHashMapIter {
            inner: self,
            current_bucket_idx: 0,
            bucket_iter: bucket_for_iter.into_iter(),
        }
    }

    pub fn group_buckets(&self, num_groups: usize) -> Vec<std::ops::Range<usize>> {
        let num_buckets = self.num_buckets;
        let mut groups = Vec::with_capacity(num_groups);

        let mut start_idx = 0;
        let elements_per_group = (num_buckets as f64 / num_groups as f64).ceil() as usize;
        while start_idx < num_buckets {
            groups.push(start_idx..std::cmp::min(num_buckets, start_idx + elements_per_group));
            start_idx += elements_per_group;
        }

        groups
    }

    pub fn iter_for_bucket_range(
        &self,
        start_bucket: usize,
        end_bucket: usize,
    ) -> PHashMapRangeIter<'_, K, V> {
        let bucket_for_iter = unsafe { &mut *get_bucket_for_iter!(self, start_bucket) };

        PHashMapRangeIter {
            inner: self,
            current_bucket_idx: start_bucket,
            end_bucket_idx: end_bucket,
            bucket_iter: bucket_for_iter.into_iter(),
        }
    }

    // NOTE The {PIter, POther} variants used below are a lazy hack, but they will do
    // for prototyping. Once we have an allocator for magpie collections that allocates on the heap
    // (instead of requiring a backing file), we can operate entirely with PHashMap instances.
    pub fn union_as_map<'a, F>(
        &'a self,
        other: &'a std::collections::HashMap<K, V>,
        value_tiebreak_fn: F,
    ) -> std::collections::HashMap<K, V>
    where
        K: Copy,
        V: Copy,
        F: Fn((&'a K, &'a V), (&'a K, &'a V)) -> (&'a K, &'a V),
    {
        let diff_self_to_other = MapDifferencePIter {
            iter: self.iter(),
            other,
        };
        let diff_other_to_self = MapDifferencePOther {
            iter: other.iter(),
            other: self,
        };

        match self.len() >= other.len() {
            true => {
                let intersection_iter = MapIntersectionPOther {
                    iter: other.iter(),
                    other: self,
                    value_tiebreak_fn,
                };
                let union_iter = MapUnionPOther {
                    iter: intersection_iter.chain(diff_self_to_other.chain(diff_other_to_self)),
                };

                union_iter.map(|e| (*e.0, *e.1)).collect()
            }

            false => {
                let intersection_iter = MapIntersectionPIter {
                    iter: self.iter(),
                    other,
                    value_tiebreak_fn,
                };
                let union_iter = MapUnionPIter {
                    iter: intersection_iter.chain(diff_self_to_other.chain(diff_other_to_self)),
                };

                union_iter.map(|e| (*e.0, *e.1)).collect()
            }
        }
    }
}

impl<K, V> Into<std::collections::HashMap<K, V>> for &PHashMap<K, V>
where
    K: Eq + Default + Hash + Persistable + PersistentlyAllocatable + Copy,
    V: Persistable + Copy,
{
    fn into(self) -> std::collections::HashMap<K, V> {
        let mut res = std::collections::HashMap::with_capacity(self.len());
        for (k, v) in self.iter() {
            res.insert(*k, *v);
        }

        res
    }
}

impl<K, V> PersistentlyAllocatable for PHashMap<K, V>
where
    K: Eq + Default + Hash + Persistable + PersistentlyAllocatable,
    V: Persistable,
{
    /// Sets the allocator that is going to be used by the [`PHashMap`] instance whenever it needs to
    /// extend its internal capacity.
    ///
    /// **NOTE** This needs to be set _before_ any subsequent calls are made that would trigger an
    /// allocation.
    fn set_allocator(&mut self, allocator: Arc<RwLock<BumpAllocator>>) {
        self.allocator.write(Arc::clone(&allocator));
        for i in 0..self.num_buckets {
            unsafe {
                let bucket = self.get_bucket_at_index(i);
                bucket.get_vector().set_allocator(Arc::clone(&allocator));
            }
        }
    }

    fn get_allocator(&self) -> Option<Arc<RwLock<BumpAllocator>>> {
        Some(Arc::clone(self.get_allocator_internal()))
    }
}

pub struct PHashMapIter<'a, K, V>
where
    K: Persistable,
    V: Persistable,
{
    inner: &'a PHashMap<K, V>,
    current_bucket_idx: usize,
    bucket_iter: BucketIter<'a, K, V>,
}

impl<'a, K, V> Iterator for PHashMapIter<'a, K, V>
where
    K: Persistable,
    V: Persistable,
{
    type Item = (&'a K, &'a V);

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            match self.bucket_iter.next() {
                Some(i) => return Some(i),
                None => {}
            }

            if self.current_bucket_idx == self.inner.num_buckets - 1 {
                return None;
            }

            self.current_bucket_idx += 1;
            let bucket_for_iter =
                unsafe { &mut *get_bucket_for_iter!(self.inner, self.current_bucket_idx) };
            self.bucket_iter = bucket_for_iter.into_iter();
        }
    }
}

impl<'a, K, V> IntoIterator for &'a PHashMap<K, V>
where
    K: Persistable,
    V: Persistable,
{
    type Item = (&'a K, &'a V);

    type IntoIter = PHashMapIter<'a, K, V>;

    fn into_iter(self) -> Self::IntoIter {
        let bucket_for_iter = unsafe { &mut *get_bucket_for_iter!(self, 0) };
        PHashMapIter {
            inner: self,
            current_bucket_idx: 0,
            bucket_iter: bucket_for_iter.into_iter(),
        }
    }
}

pub struct PHashMapRangeIter<'a, K, V>
where
    K: Persistable,
    V: Persistable,
{
    inner: &'a PHashMap<K, V>,
    current_bucket_idx: usize,
    end_bucket_idx: usize,
    bucket_iter: BucketIter<'a, K, V>,
}

impl<'a, K, V> Iterator for PHashMapRangeIter<'a, K, V>
where
    K: Persistable,
    V: Persistable,
{
    type Item = (&'a K, &'a V);

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            match self.bucket_iter.next() {
                Some(i) => return Some(i),
                None => {}
            }

            if self.current_bucket_idx == self.inner.num_buckets - 1
                || self.current_bucket_idx == self.end_bucket_idx - 1
            {
                return None;
            }

            self.current_bucket_idx += 1;
            let bucket_for_iter =
                unsafe { &mut *get_bucket_for_iter!(self.inner, self.current_bucket_idx) };
            self.bucket_iter = bucket_for_iter.into_iter();
        }
    }
}

pub struct MapIntersection<'a, K, V, F>
where
    K: Eq + Default + Hash + Persistable + PersistentlyAllocatable,
    V: Persistable,
{
    iter: PHashMapIter<'a, K, V>,
    other: &'a PHashMap<K, V>,
    value_tiebreak_fn: F,
}

impl<'a, K, V, F> Iterator for MapIntersection<'a, K, V, F>
where
    K: Eq + Default + Hash + Persistable + PersistentlyAllocatable,
    V: Persistable,
    F: Fn((&'a K, &'a V), (&'a K, &'a V)) -> (&'a K, &'a V),
{
    type Item = (&'a K, &'a V);

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            let element = self.iter.next()?;

            match self.other.get_pkey(&element.0) {
                None => {}
                Some(value) => {
                    return Some((self.value_tiebreak_fn)(
                        (element.0, element.1),
                        (element.0, value),
                    ))
                }
            }
        }
    }
}

pub struct MapIntersectionPIter<'a, K, V, F>
where
    K: Eq + Default + Hash + Persistable + PersistentlyAllocatable,
    V: Persistable,
{
    iter: PHashMapIter<'a, K, V>,
    other: &'a std::collections::HashMap<K, V>,
    value_tiebreak_fn: F,
}

impl<'a, K, V, F> Iterator for MapIntersectionPIter<'a, K, V, F>
where
    K: Eq + Default + Hash + Persistable + PersistentlyAllocatable,
    V: Persistable,
    F: Fn((&'a K, &'a V), (&'a K, &'a V)) -> (&'a K, &'a V),
{
    type Item = (&'a K, &'a V);

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            let element = self.iter.next()?;

            match self.other.get(&element.0) {
                None => {}
                Some(value) => {
                    return Some((self.value_tiebreak_fn)(
                        (element.0, element.1),
                        (element.0, value),
                    ))
                }
            }
        }
    }
}

pub struct MapIntersectionPOther<'a, K, V, F>
where
    K: Eq + Default + Hash + Persistable + PersistentlyAllocatable,
    V: Persistable,
{
    iter: std::collections::hash_map::Iter<'a, K, V>,
    other: &'a PHashMap<K, V>,
    value_tiebreak_fn: F,
}

impl<'a, K, V, F> Iterator for MapIntersectionPOther<'a, K, V, F>
where
    K: Eq + Default + Hash + Persistable + PersistentlyAllocatable,
    V: Persistable,
    F: Fn((&'a K, &'a V), (&'a K, &'a V)) -> (&'a K, &'a V),
{
    type Item = (&'a K, &'a V);

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            let element = self.iter.next()?;

            match self.other.get_pkey(&element.0) {
                None => {}
                Some(value) => {
                    return Some((self.value_tiebreak_fn)(
                        (element.0, element.1),
                        (element.0, value),
                    ))
                }
            }
        }
    }
}

pub struct MapDifference<'a, K, V>
where
    K: Persistable,
    V: Persistable,
{
    iter: PHashMapIter<'a, K, V>,
    other: &'a PHashMap<K, V>,
}

impl<'a, K, V> Iterator for MapDifference<'a, K, V>
where
    K: Eq + Default + Hash + Persistable + PersistentlyAllocatable,
    V: Persistable,
{
    type Item = (&'a K, &'a V);

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            let element = self.iter.next()?;

            if !self.other.contains(element.0) {
                return Some(element);
            }
        }
    }
}

pub struct MapDifferencePIter<'a, K, V>
where
    K: Persistable,
    V: Persistable,
{
    iter: PHashMapIter<'a, K, V>,
    other: &'a std::collections::HashMap<K, V>,
}

impl<'a, K, V> Iterator for MapDifferencePIter<'a, K, V>
where
    K: Eq + Default + Hash + Persistable + PersistentlyAllocatable,
    V: Persistable,
{
    type Item = (&'a K, &'a V);

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            let element = self.iter.next()?;

            if !self.other.contains_key(element.0) {
                return Some(element);
            }
        }
    }
}

pub struct MapDifferencePOther<'a, K, V>
where
    K: Persistable,
    V: Persistable,
{
    iter: std::collections::hash_map::Iter<'a, K, V>,
    other: &'a PHashMap<K, V>,
}

impl<'a, K, V> Iterator for MapDifferencePOther<'a, K, V>
where
    K: Eq + Default + Hash + Persistable + PersistentlyAllocatable,
    V: Persistable,
{
    type Item = (&'a K, &'a V);

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            let element = self.iter.next()?;

            if !self.other.contains(element.0) {
                return Some(element);
            }
        }
    }
}

pub struct MapUnion<'a, K, V, F>
where
    K: Eq + Default + Hash + Persistable + PersistentlyAllocatable,
    V: Persistable,
    F: Fn((&'a K, &'a V), (&'a K, &'a V)) -> (&'a K, &'a V),
{
    iter: Chain<
        MapIntersection<'a, K, V, F>,
        Chain<MapDifferencePIter<'a, K, V>, MapDifferencePOther<'a, K, V>>,
    >,
}

impl<'a, K, V, F> Iterator for MapUnion<'a, K, V, F>
where
    K: Eq + Default + Hash + Persistable + PersistentlyAllocatable,
    V: Persistable,
    F: Fn((&'a K, &'a V), (&'a K, &'a V)) -> (&'a K, &'a V),
{
    type Item = (&'a K, &'a V);

    fn next(&mut self) -> Option<Self::Item> {
        self.iter.next()
    }
}

pub struct MapUnionPIter<'a, K, V, F>
where
    K: Eq + Default + Hash + Persistable + PersistentlyAllocatable,
    V: Persistable,
    F: Fn((&'a K, &'a V), (&'a K, &'a V)) -> (&'a K, &'a V),
{
    iter: Chain<
        MapIntersectionPIter<'a, K, V, F>,
        Chain<MapDifferencePIter<'a, K, V>, MapDifferencePOther<'a, K, V>>,
    >,
}

impl<'a, K, V, F> Iterator for MapUnionPIter<'a, K, V, F>
where
    K: Eq + Default + Hash + Persistable + PersistentlyAllocatable,
    V: Persistable,
    F: Fn((&'a K, &'a V), (&'a K, &'a V)) -> (&'a K, &'a V),
{
    type Item = (&'a K, &'a V);

    fn next(&mut self) -> Option<Self::Item> {
        self.iter.next()
    }
}
pub struct MapUnionPOther<'a, K, V, F>
where
    K: Eq + Default + Hash + Persistable + PersistentlyAllocatable,
    V: Persistable,
    F: Fn((&'a K, &'a V), (&'a K, &'a V)) -> (&'a K, &'a V),
{
    iter: Chain<
        MapIntersectionPOther<'a, K, V, F>,
        Chain<MapDifferencePIter<'a, K, V>, MapDifferencePOther<'a, K, V>>,
    >,
}

impl<'a, K, V, F> Iterator for MapUnionPOther<'a, K, V, F>
where
    K: Eq + Default + Hash + Persistable + PersistentlyAllocatable,
    V: Persistable,
    F: Fn((&'a K, &'a V), (&'a K, &'a V)) -> (&'a K, &'a V),
{
    type Item = (&'a K, &'a V);

    fn next(&mut self) -> Option<Self::Item> {
        self.iter.next()
    }
}