shape-value 0.3.2

NaN-boxed value representation and heap types for Shape
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
//! Typed hash map for v2 runtime.
//!
//! `TypedMap<K, V>` is a `#[repr(C)]` open-addressing hash table with linear
//! probing. The compiler monomorphizes: `HashMap<string, number>` and
//! `HashMap<string, i32>` are different `TypedMap` instantiations.
//!
//! Concrete type aliases are provided for common instantiations used by FFI/JIT:
//! - `TypedMapStringF64` — `HashMap<string, number>`
//! - `TypedMapStringI64` — `HashMap<string, i64>`
//! - `TypedMapStringPtr` — `HashMap<string, *const u8>`

use super::heap_header::{HeapHeader, HEAP_KIND_V2_TYPED_MAP};
use std::alloc::{Layout, alloc_zeroed, dealloc};

/// Sentinel hash values for bucket state.
const HASH_EMPTY: u64 = 0;
const HASH_TOMBSTONE: u64 = 1;

/// Minimum hash value for an occupied bucket (after masking).
const HASH_MIN_OCCUPIED: u64 = 2;

/// Load factor threshold (75%) — grow when `len * 4 >= bucket_count * 3`.
const LOAD_FACTOR_NUM: u32 = 3;
const LOAD_FACTOR_DEN: u32 = 4;

/// Default initial bucket count.
const INITIAL_CAPACITY: u32 = 8;

/// A single bucket in the hash table.
#[repr(C)]
pub struct Bucket<K, V> {
    /// 0 = empty, 1 = tombstone, >= 2 = occupied (stores masked hash).
    pub hash: u64,
    pub key: K,
    pub value: V,
}

/// Typed open-addressing hash map with linear probing.
#[repr(C)]
pub struct TypedMap<K, V> {
    pub header: HeapHeader,
    /// Pointer to bucket array.
    pub buckets: *mut Bucket<K, V>,
    /// Number of buckets (always a power of 2).
    pub bucket_count: u32,
    /// Number of live entries.
    pub len: u32,
    /// Number of tombstones.
    pub tombstone_count: u32,
    pub _pad: u32,
}

// Concrete type aliases for common instantiations.
pub type TypedMapStringF64 = TypedMap<*const u8, f64>;
pub type TypedMapStringI64 = TypedMap<*const u8, i64>;
pub type TypedMapStringPtr = TypedMap<*const u8, *const u8>;

// i64-keyed map aliases.
pub type TypedMapI64F64 = TypedMap<i64, f64>;
pub type TypedMapI64I64 = TypedMap<i64, i64>;
pub type TypedMapI64Ptr = TypedMap<i64, *const u8>;

/// FNV-1a hash for byte slices. Simple, fast, good distribution for short keys.
#[inline]
fn fnv1a_hash(bytes: &[u8]) -> u64 {
    let mut h: u64 = 0xcbf29ce484222325;
    for &b in bytes {
        h ^= b as u64;
        h = h.wrapping_mul(0x100000001b3);
    }
    h
}

/// Ensure a hash value is >= HASH_MIN_OCCUPIED (so it can't be confused with
/// empty/tombstone sentinels).
#[inline]
fn fix_hash(h: u64) -> u64 {
    if h < HASH_MIN_OCCUPIED {
        h.wrapping_add(HASH_MIN_OCCUPIED)
    } else {
        h
    }
}

impl<K: Copy, V: Copy> TypedMap<K, V> {
    /// Allocate a new empty TypedMap.
    pub fn new() -> *mut Self {
        let layout = Layout::new::<Self>();
        let ptr = unsafe { alloc_zeroed(layout) as *mut Self };
        assert!(!ptr.is_null(), "allocation failed for TypedMap");

        unsafe {
            (*ptr).header = HeapHeader::new(HEAP_KIND_V2_TYPED_MAP);
            (*ptr).buckets = std::ptr::null_mut();
            (*ptr).bucket_count = 0;
            (*ptr).len = 0;
            (*ptr).tombstone_count = 0;
            (*ptr)._pad = 0;
        }
        ptr
    }

    /// Number of entries.
    ///
    /// # Safety
    /// `this` must point to a valid `TypedMap`.
    #[inline]
    pub unsafe fn len(this: *const Self) -> u32 {
        unsafe { (*this).len }
    }

    /// Whether the map is empty.
    ///
    /// # Safety
    /// `this` must point to a valid `TypedMap`.
    #[inline]
    pub unsafe fn is_empty(this: *const Self) -> bool {
        unsafe { (*this).len == 0 }
    }

    /// Deallocate the map and its bucket array.
    ///
    /// # Safety
    /// `ptr` must point to a `TypedMap` allocated by this module.
    pub unsafe fn drop_map(ptr: *mut Self) {
        unsafe {
            let map = &*ptr;
            if map.bucket_count > 0 && !map.buckets.is_null() {
                let bucket_layout = Layout::array::<Bucket<K, V>>(map.bucket_count as usize)
                    .expect("invalid bucket layout");
                dealloc(map.buckets as *mut u8, bucket_layout);
            }
            let layout = Layout::new::<Self>();
            dealloc(ptr as *mut u8, layout);
        }
    }

    /// Allocate a zeroed bucket array. All buckets have hash = 0 (empty).
    fn alloc_buckets(count: u32) -> *mut Bucket<K, V> {
        let layout =
            Layout::array::<Bucket<K, V>>(count as usize).expect("invalid bucket layout");
        let ptr = unsafe { alloc_zeroed(layout) as *mut Bucket<K, V> };
        assert!(!ptr.is_null(), "allocation failed for TypedMap buckets");
        ptr
    }

    /// Ensure the table has room for at least one more entry, growing if needed.
    ///
    /// # Safety
    /// `this` must point to a valid `TypedMap`.
    unsafe fn ensure_capacity(this: *mut Self) {
        unsafe {
            let map = &*this;
            if map.bucket_count == 0 {
                (*this).buckets = Self::alloc_buckets(INITIAL_CAPACITY);
                (*this).bucket_count = INITIAL_CAPACITY;
                return;
            }
            // Grow when (len + tombstones) * 4 >= bucket_count * 3
            let used = map.len + map.tombstone_count;
            if used * LOAD_FACTOR_DEN >= map.bucket_count * LOAD_FACTOR_NUM {
                Self::grow(this);
            }
        }
    }

    /// Double the bucket array and rehash all live entries.
    ///
    /// # Safety
    /// `this` must point to a valid `TypedMap` with bucket_count > 0.
    unsafe fn grow(this: *mut Self) {
        unsafe {
            let map = &*this;
            let old_buckets = map.buckets;
            let old_count = map.bucket_count;

            let new_count = old_count * 2;
            let new_buckets = Self::alloc_buckets(new_count);
            let mask = new_count - 1; // power-of-2 mask

            // Rehash live entries
            for i in 0..old_count {
                let bucket = &*old_buckets.add(i as usize);
                if bucket.hash >= HASH_MIN_OCCUPIED {
                    let mut idx = (bucket.hash as u32) & mask;
                    loop {
                        let dst = &*new_buckets.add(idx as usize);
                        if dst.hash == HASH_EMPTY {
                            std::ptr::write(
                                new_buckets.add(idx as usize),
                                Bucket {
                                    hash: bucket.hash,
                                    key: bucket.key,
                                    value: bucket.value,
                                },
                            );
                            break;
                        }
                        idx = (idx + 1) & mask;
                    }
                }
            }

            // Free old buckets
            let old_layout = Layout::array::<Bucket<K, V>>(old_count as usize)
                .expect("invalid bucket layout");
            dealloc(old_buckets as *mut u8, old_layout);

            (*this).buckets = new_buckets;
            (*this).bucket_count = new_count;
            (*this).tombstone_count = 0;
        }
    }
}

// --- String-keyed map operations ---
//
// These operate on TypedMap<*const u8, V> where keys are `*const StringObj`.
// We compare by string content, not pointer identity.

use super::string_obj::StringObj;

impl<V: Copy> TypedMap<*const u8, V> {
    /// Insert a key-value pair. If the key already exists, updates the value
    /// and returns the old value.
    ///
    /// The map retains the key pointer (caller must ensure it stays alive via
    /// refcounting). If the key already exists, the existing key pointer is kept.
    ///
    /// # Safety
    /// `this` must point to a valid `TypedMap`. `key` must point to a valid `StringObj`.
    pub unsafe fn insert(this: *mut Self, key: *const StringObj, value: V) -> Option<V> {
        unsafe {
            Self::ensure_capacity(this);

            let key_str = StringObj::as_str(key);
            let hash = fix_hash(fnv1a_hash(key_str.as_bytes()));
            let map = &*this;
            let mask = map.bucket_count - 1;
            let mut idx = (hash as u32) & mask;
            let mut first_tombstone: Option<u32> = None;

            loop {
                let bucket = &*map.buckets.add(idx as usize);
                match bucket.hash {
                    HASH_EMPTY => {
                        // Key not found — insert at first tombstone if we saw one, else here.
                        let insert_idx = first_tombstone.unwrap_or(idx);
                        if first_tombstone.is_some() {
                            (*this).tombstone_count -= 1;
                        }
                        std::ptr::write(
                            (*this).buckets.add(insert_idx as usize),
                            Bucket {
                                hash,
                                key: key as *const u8,
                                value,
                            },
                        );
                        (*this).len += 1;
                        return None;
                    }
                    HASH_TOMBSTONE => {
                        if first_tombstone.is_none() {
                            first_tombstone = Some(idx);
                        }
                    }
                    h if h == hash => {
                        // Hash matches — compare key content.
                        let existing_key = bucket.key as *const StringObj;
                        let existing_str = StringObj::as_str(existing_key);
                        if existing_str == key_str {
                            // Key exists — update value, return old.
                            let old = bucket.value;
                            (*(*this).buckets.add(idx as usize)).value = value;
                            return Some(old);
                        }
                    }
                    _ => {}
                }
                idx = (idx + 1) & mask;
            }
        }
    }

    /// Look up a value by string key.
    ///
    /// # Safety
    /// `this` must point to a valid `TypedMap`. `key` must point to a valid `StringObj`.
    pub unsafe fn get(this: *const Self, key: *const StringObj) -> Option<V> {
        unsafe {
            let map = &*this;
            if map.len == 0 || map.bucket_count == 0 {
                return None;
            }

            let key_str = StringObj::as_str(key);
            let hash = fix_hash(fnv1a_hash(key_str.as_bytes()));
            let mask = map.bucket_count - 1;
            let mut idx = (hash as u32) & mask;

            loop {
                let bucket = &*map.buckets.add(idx as usize);
                match bucket.hash {
                    HASH_EMPTY => return None,
                    HASH_TOMBSTONE => {}
                    h if h == hash => {
                        let existing_key = bucket.key as *const StringObj;
                        let existing_str = StringObj::as_str(existing_key);
                        if existing_str == key_str {
                            return Some(bucket.value);
                        }
                    }
                    _ => {}
                }
                idx = (idx + 1) & mask;
            }
        }
    }

    /// Check if the map contains a key.
    ///
    /// # Safety
    /// `this` must point to a valid `TypedMap`. `key` must point to a valid `StringObj`.
    pub unsafe fn contains_key(this: *const Self, key: *const StringObj) -> bool {
        unsafe { Self::get(this, key).is_some() }
    }

    /// Remove a key from the map, returning the value if it was present.
    ///
    /// # Safety
    /// `this` must point to a valid `TypedMap`. `key` must point to a valid `StringObj`.
    pub unsafe fn remove(this: *mut Self, key: *const StringObj) -> Option<V> {
        unsafe {
            let map = &*this;
            if map.len == 0 || map.bucket_count == 0 {
                return None;
            }

            let key_str = StringObj::as_str(key);
            let hash = fix_hash(fnv1a_hash(key_str.as_bytes()));
            let mask = map.bucket_count - 1;
            let mut idx = (hash as u32) & mask;

            loop {
                let bucket = &*map.buckets.add(idx as usize);
                match bucket.hash {
                    HASH_EMPTY => return None,
                    HASH_TOMBSTONE => {}
                    h if h == hash => {
                        let existing_key = bucket.key as *const StringObj;
                        let existing_str = StringObj::as_str(existing_key);
                        if existing_str == key_str {
                            let old_value = bucket.value;
                            // Replace with tombstone
                            (*(*this).buckets.add(idx as usize)).hash = HASH_TOMBSTONE;
                            (*this).len -= 1;
                            (*this).tombstone_count += 1;
                            return Some(old_value);
                        }
                    }
                    _ => {}
                }
                idx = (idx + 1) & mask;
            }
        }
    }
}

// --- i64-keyed map operations ---
//
// These operate on TypedMap<i64, V>. Keys are compared by raw integer
// equality. The hash mixes the i64 with FNV-1a over its bytes for a decent
// distribution across small integers.

impl<V: Copy> TypedMap<i64, V> {
    /// Insert a key-value pair. If the key already exists, updates the value
    /// and returns the old value.
    ///
    /// # Safety
    /// `this` must point to a valid `TypedMap<i64, V>`.
    pub unsafe fn insert_i64(this: *mut Self, key: i64, value: V) -> Option<V> {
        unsafe {
            Self::ensure_capacity(this);

            let hash = fix_hash(fnv1a_hash(&key.to_le_bytes()));
            let map = &*this;
            let mask = map.bucket_count - 1;
            let mut idx = (hash as u32) & mask;
            let mut first_tombstone: Option<u32> = None;

            loop {
                let bucket = &*map.buckets.add(idx as usize);
                match bucket.hash {
                    HASH_EMPTY => {
                        let insert_idx = first_tombstone.unwrap_or(idx);
                        if first_tombstone.is_some() {
                            (*this).tombstone_count -= 1;
                        }
                        std::ptr::write(
                            (*this).buckets.add(insert_idx as usize),
                            Bucket { hash, key, value },
                        );
                        (*this).len += 1;
                        return None;
                    }
                    HASH_TOMBSTONE => {
                        if first_tombstone.is_none() {
                            first_tombstone = Some(idx);
                        }
                    }
                    h if h == hash => {
                        if bucket.key == key {
                            let old = bucket.value;
                            (*(*this).buckets.add(idx as usize)).value = value;
                            return Some(old);
                        }
                    }
                    _ => {}
                }
                idx = (idx + 1) & mask;
            }
        }
    }

    /// Look up a value by i64 key.
    ///
    /// # Safety
    /// `this` must point to a valid `TypedMap<i64, V>`.
    pub unsafe fn get_i64(this: *const Self, key: i64) -> Option<V> {
        unsafe {
            let map = &*this;
            if map.len == 0 || map.bucket_count == 0 {
                return None;
            }

            let hash = fix_hash(fnv1a_hash(&key.to_le_bytes()));
            let mask = map.bucket_count - 1;
            let mut idx = (hash as u32) & mask;

            loop {
                let bucket = &*map.buckets.add(idx as usize);
                match bucket.hash {
                    HASH_EMPTY => return None,
                    HASH_TOMBSTONE => {}
                    h if h == hash => {
                        if bucket.key == key {
                            return Some(bucket.value);
                        }
                    }
                    _ => {}
                }
                idx = (idx + 1) & mask;
            }
        }
    }

    /// Check if the map contains an i64 key.
    ///
    /// # Safety
    /// `this` must point to a valid `TypedMap<i64, V>`.
    pub unsafe fn contains_key_i64(this: *const Self, key: i64) -> bool {
        unsafe { Self::get_i64(this, key).is_some() }
    }

    /// Remove an i64 key from the map, returning the value if it was present.
    ///
    /// # Safety
    /// `this` must point to a valid `TypedMap<i64, V>`.
    pub unsafe fn remove_i64(this: *mut Self, key: i64) -> Option<V> {
        unsafe {
            let map = &*this;
            if map.len == 0 || map.bucket_count == 0 {
                return None;
            }

            let hash = fix_hash(fnv1a_hash(&key.to_le_bytes()));
            let mask = map.bucket_count - 1;
            let mut idx = (hash as u32) & mask;

            loop {
                let bucket = &*map.buckets.add(idx as usize);
                match bucket.hash {
                    HASH_EMPTY => return None,
                    HASH_TOMBSTONE => {}
                    h if h == hash => {
                        if bucket.key == key {
                            let old_value = bucket.value;
                            (*(*this).buckets.add(idx as usize)).hash = HASH_TOMBSTONE;
                            (*this).len -= 1;
                            (*this).tombstone_count += 1;
                            return Some(old_value);
                        }
                    }
                    _ => {}
                }
                idx = (idx + 1) & mask;
            }
        }
    }
}

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

    /// Helper: create a StringObj from a &str for testing.
    fn make_key(s: &str) -> *mut StringObj {
        StringObj::new(s)
    }

    #[test]
    fn test_new_empty_map() {
        let map = TypedMapStringF64::new();
        unsafe {
            assert_eq!(TypedMap::len(map), 0);
            assert!(TypedMap::is_empty(map));
            assert_eq!((*map).header.kind(), HEAP_KIND_V2_TYPED_MAP);
            assert_eq!((*map).header.get_refcount(), 1);
            TypedMap::drop_map(map);
        }
    }

    #[test]
    fn test_insert_and_get() {
        let map = TypedMapStringF64::new();
        let k1 = make_key("x");
        let k2 = make_key("y");
        let k3 = make_key("z");

        unsafe {
            assert_eq!(TypedMap::insert(map, k1, 1.0), None);
            assert_eq!(TypedMap::insert(map, k2, 2.0), None);
            assert_eq!(TypedMap::insert(map, k3, 3.0), None);
            assert_eq!(TypedMap::len(map), 3);

            // Look up using fresh key pointers (same content, different allocation)
            let lookup_x = make_key("x");
            let lookup_y = make_key("y");
            let lookup_z = make_key("z");

            assert_eq!(TypedMap::get(map, lookup_x), Some(1.0));
            assert_eq!(TypedMap::get(map, lookup_y), Some(2.0));
            assert_eq!(TypedMap::get(map, lookup_z), Some(3.0));

            let missing = make_key("missing");
            assert_eq!(TypedMap::get(map, missing), None);

            StringObj::drop(lookup_x);
            StringObj::drop(lookup_y);
            StringObj::drop(lookup_z);
            StringObj::drop(missing);
            StringObj::drop(k1);
            StringObj::drop(k2);
            StringObj::drop(k3);
            TypedMap::drop_map(map);
        }
    }

    #[test]
    fn test_insert_update_returns_old_value() {
        let map = TypedMapStringF64::new();
        let k1 = make_key("key");
        let k2 = make_key("key"); // same content

        unsafe {
            assert_eq!(TypedMap::insert(map, k1, 1.0), None);
            assert_eq!(TypedMap::insert(map, k2, 2.0), Some(1.0)); // returns old
            assert_eq!(TypedMap::len(map), 1); // still 1 entry

            let lookup = make_key("key");
            assert_eq!(TypedMap::get(map, lookup), Some(2.0)); // updated value

            StringObj::drop(lookup);
            StringObj::drop(k1);
            StringObj::drop(k2);
            TypedMap::drop_map(map);
        }
    }

    #[test]
    fn test_contains_key() {
        let map = TypedMapStringF64::new();
        let k = make_key("present");

        unsafe {
            TypedMap::insert(map, k, 42.0);

            let lookup_yes = make_key("present");
            let lookup_no = make_key("absent");
            assert!(TypedMap::contains_key(map, lookup_yes));
            assert!(!TypedMap::contains_key(map, lookup_no));

            StringObj::drop(lookup_yes);
            StringObj::drop(lookup_no);
            StringObj::drop(k);
            TypedMap::drop_map(map);
        }
    }

    #[test]
    fn test_remove() {
        let map = TypedMapStringI64::new();
        let k1 = make_key("a");
        let k2 = make_key("b");

        unsafe {
            TypedMap::insert(map, k1, 10i64);
            TypedMap::insert(map, k2, 20i64);
            assert_eq!(TypedMap::len(map), 2);

            let rm_key = make_key("a");
            assert_eq!(TypedMap::remove(map, rm_key), Some(10i64));
            assert_eq!(TypedMap::len(map), 1);

            // Can't find removed key
            let lookup = make_key("a");
            assert_eq!(TypedMap::get(map, lookup), None);

            // Other key still present
            let lookup_b = make_key("b");
            assert_eq!(TypedMap::get(map, lookup_b), Some(20i64));

            // Remove non-existent key returns None
            let rm_missing = make_key("missing");
            assert_eq!(TypedMap::remove(map, rm_missing), None);

            StringObj::drop(rm_key);
            StringObj::drop(lookup);
            StringObj::drop(lookup_b);
            StringObj::drop(rm_missing);
            StringObj::drop(k1);
            StringObj::drop(k2);
            TypedMap::drop_map(map);
        }
    }

    #[test]
    fn test_grow_and_rehash() {
        let map = TypedMapStringF64::new();
        let mut keys = Vec::new();

        unsafe {
            // Insert enough entries to trigger multiple grows.
            for i in 0..50 {
                let key = make_key(&format!("key_{i}"));
                TypedMap::insert(map, key, i as f64);
                keys.push(key);
            }

            assert_eq!(TypedMap::len(map), 50);

            // Verify all entries are still accessible after rehashing.
            for i in 0..50 {
                let lookup = make_key(&format!("key_{i}"));
                assert_eq!(
                    TypedMap::get(map, lookup),
                    Some(i as f64),
                    "missing key_{i} after grow"
                );
                StringObj::drop(lookup);
            }

            for k in keys {
                StringObj::drop(k);
            }
            TypedMap::drop_map(map);
        }
    }

    #[test]
    fn test_collision_handling() {
        // Insert many keys — some will collide due to hash masking.
        let map = TypedMapStringF64::new();
        let mut keys = Vec::new();

        unsafe {
            for i in 0..20 {
                let key = make_key(&format!("{i}"));
                TypedMap::insert(map, key, i as f64);
                keys.push(key);
            }

            assert_eq!(TypedMap::len(map), 20);

            for i in 0..20 {
                let lookup = make_key(&format!("{i}"));
                assert_eq!(TypedMap::get(map, lookup), Some(i as f64));
                StringObj::drop(lookup);
            }

            for k in keys {
                StringObj::drop(k);
            }
            TypedMap::drop_map(map);
        }
    }

    #[test]
    fn test_remove_then_insert_reuses_tombstone() {
        let map = TypedMapStringF64::new();

        unsafe {
            let k1 = make_key("alpha");
            TypedMap::insert(map, k1, 1.0);

            let rm = make_key("alpha");
            TypedMap::remove(map, rm);
            assert_eq!(TypedMap::len(map), 0);
            assert_eq!((*map).tombstone_count, 1);

            // Insert same key again — should reuse tombstone slot.
            let k2 = make_key("alpha");
            TypedMap::insert(map, k2, 2.0);
            assert_eq!(TypedMap::len(map), 1);
            assert_eq!((*map).tombstone_count, 0);

            let lookup = make_key("alpha");
            assert_eq!(TypedMap::get(map, lookup), Some(2.0));

            StringObj::drop(lookup);
            StringObj::drop(k1);
            StringObj::drop(k2);
            StringObj::drop(rm);
            TypedMap::drop_map(map);
        }
    }

    #[test]
    fn test_get_on_empty_map() {
        let map = TypedMapStringF64::new();
        unsafe {
            let k = make_key("anything");
            assert_eq!(TypedMap::get(map, k), None);
            StringObj::drop(k);
            TypedMap::drop_map(map);
        }
    }

    #[test]
    fn test_string_ptr_map() {
        let map = TypedMapStringPtr::new();
        unsafe {
            let k = make_key("ptr_key");
            let val_str = make_key("value");
            TypedMap::insert(map, k, val_str as *const u8);

            let lookup = make_key("ptr_key");
            let result = TypedMap::get(map, lookup);
            assert!(result.is_some());
            let retrieved = result.unwrap() as *const StringObj;
            assert_eq!(StringObj::as_str(retrieved), "value");

            StringObj::drop(lookup);
            StringObj::drop(k);
            StringObj::drop(val_str);
            TypedMap::drop_map(map);
        }
    }
}