deferred-map 0.3.1

High-performance generational arena using handle-based deferred insertion with O(1) operations
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
// Handle related comprehensive tests
// Handle 相关的全面测试

use crate::{DeferredMap, Handle, Key};

#[test]
fn test_handle_key() {
    let mut map = DeferredMap::<i32>::new();
    let handle = map.allocate_handle();

    let key = handle.key();
    assert!(key.index() > 0);
}

#[test]
fn test_handle_index_extraction() {
    let mut map = DeferredMap::<i32>::new();
    let handle = map.allocate_handle();

    let index = handle.index();
    // First real slot should be at index 1 (index 0 is sentinel)
    // 第一个真实 slot 应该在索引 1(索引 0 是 sentinel)
    assert_eq!(index, 1);
}

#[test]
fn test_handle_generation_extraction() {
    let mut map = DeferredMap::<i32>::new();
    let handle = map.allocate_handle();

    let generation = handle.generation();
    // First generation should be 1 (started at 0b101)
    // 第一次 generation 应该是 1(从 0b101 开始)
    assert_eq!(generation.get(), 1);
}

#[test]
fn test_handle_encoding_consistency() {
    let mut map = DeferredMap::<i32>::new();
    let handle = map.allocate_handle();

    let key = handle.key();
    let index = handle.index();
    let generation = handle.generation();

    // Verify index extraction
    // 验证 index 提取
    assert_eq!(key.index() as u64, index as u64);

    // Verify generation extraction
    // 验证 generation 提取
    assert_eq!(key.generation(), generation);
}

#[test]
fn test_multiple_handles_different_indices() {
    let mut map = DeferredMap::<i32>::new();

    let handle1 = map.allocate_handle();
    let handle2 = map.allocate_handle();
    let handle3 = map.allocate_handle();

    let index1 = handle1.index();
    let index2 = handle2.index();
    let index3 = handle3.index();

    // Each handle should have different index
    // 每个 handle 应该有不同的索引
    assert_ne!(index1, index2);
    assert_ne!(index2, index3);
    assert_ne!(index1, index3);

    // Indices should be sequential
    // 索引应该是连续的
    assert_eq!(index1 + 1, index2);
    assert_eq!(index2 + 1, index3);
}

#[test]
fn test_handle_generation_after_reuse() {
    let mut map = DeferredMap::new();

    // Allocate and insert
    // 分配并插入
    let handle1 = map.allocate_handle();
    let gen1 = handle1.generation();
    let key1 = handle1.key();
    map.insert(handle1, 42);

    // Remove to free the slot
    // 移除以释放 slot
    map.remove(key1);

    // Allocate again, should reuse same slot with incremented generation
    // 再次分配,应该复用相同的 slot,generation 递增
    let handle2 = map.allocate_handle();
    let gen2 = handle2.generation();

    // Generation (high 30 bits) should be incremented by 1
    // Generation(高 30 位)应该递增 1
    assert_eq!(gen2.get(), gen1.get() + 1);
}

#[test]
fn test_handle_cannot_be_cloned() {
    // This test verifies at compile time that Handle doesn't implement Clone
    // 此测试在编译时验证 Handle 不实现 Clone
    // If this compiles, the test passes
    // 如果编译通过,测试就通过

    let mut map = DeferredMap::<i32>::new();
    let handle = map.allocate_handle();

    // This should consume the handle
    // 这应该消耗 handle
    map.insert(handle, 42);

    // Uncommenting the following line should cause a compile error:
    // 取消注释以下行应该导致编译错误:
    // let _key2 = map.insert(handle, 100); // Error: value used after move
}

#[test]
fn test_handle_equality() {
    let mut map = DeferredMap::<i32>::new();
    let handle1 = map.allocate_handle();
    let handle2 = map.allocate_handle();

    // Handles should not be equal
    // Handle 不应该相等
    assert_ne!(handle1, handle2);

    // Create handle with same key value
    // 使用相同的 key 值创建 handle
    let key = handle1.key();
    let handle3 = Handle::new(key);
    assert_eq!(handle1, handle3);
}

#[test]
fn test_handle_debug_format() {
    let mut map = DeferredMap::<i32>::new();
    let handle = map.allocate_handle();

    let debug_str = format!("{:?}", handle);
    assert!(debug_str.contains("Handle"));
}

#[test]
fn test_handle_with_large_index() {
    let mut map = DeferredMap::<i32>::new();

    // Allocate many handles to get large indices
    // 分配许多 handle 以获得较大的索引
    let mut handles = Vec::new();
    for _ in 0..1000 {
        handles.push(map.allocate_handle());
    }

    let last_handle = handles.last().unwrap();
    let index = last_handle.index();

    // Index should be around 1000 (starting from 1)
    // 索引应该接近 1000(从 1 开始)
    assert!(index >= 1000);
}

#[test]
fn test_handle_generation_consistency_after_multiple_reuses() {
    let mut map = DeferredMap::new();

    let handle1 = map.allocate_handle();
    let index = handle1.index();
    let mut prev_gen = handle1.generation();
    let mut current_key = handle1.key();
    map.insert(handle1, 1);

    // Reuse the same slot multiple times
    // 多次复用相同的 slot
    for i in 2..=10 {
        map.remove(current_key);

        let handle = map.allocate_handle();
        assert_eq!(handle.index(), index); // Should reuse same slot | 应该复用相同的 slot

        let curr_gen = handle.generation();
        assert_eq!(curr_gen.get(), prev_gen.get() + 1); // Generation (high 30 bits) should increment by 1 | Generation(高 30 位)应该递增 1
        prev_gen = curr_gen;

        current_key = handle.key();
        map.insert(handle, i);
    }
}

#[test]
fn test_handle_with_max_index() {
    // Test handle with maximum u32 index
    // 测试最大 u32 索引的 handle
    let max_index = u32::MAX;
    let generation = 1u32;
    let key = (generation as u64) << 32 | max_index as u64;
    #[cfg(debug_assertions)]
    let key = crate::DefaultKey {
        raw: key,
        #[cfg(debug_assertions)]
        map_id: 0,
    };
    let handle = Handle::new(key);

    assert_eq!(handle.index(), max_index);
    assert_eq!(handle.generation().get(), generation); // generation is stored directly in high 32 bits
}

#[test]
fn test_handle_with_max_generation() {
    // Test handle with maximum 32-bit generation
    // 测试最大 32 位 generation 的 handle
    let index = 1u32;
    let max_generation = u32::MAX; // Now generation is full 32 bits
    let key = (max_generation as u64) << 32 | index as u64;
    #[cfg(debug_assertions)]
    let key = crate::DefaultKey {
        raw: key,
        #[cfg(debug_assertions)]
        map_id: 0,
    };
    let handle = Handle::new(key);

    assert_eq!(handle.index(), index);
    assert_eq!(handle.generation().get(), max_generation);
}

// ============================================================================
// release_handle API Tests
// release_handle API 测试
// ============================================================================

#[test]
fn test_release_handle_basic() {
    let mut map = DeferredMap::<i32>::new();

    // Allocate a handle but don't use it
    // 分配一个 handle 但不使用
    let handle = map.allocate_handle();

    // Release it
    // 释放它
    map.release_handle(handle);

    // Map should still be empty
    // Map 应该仍然为空
    assert_eq!(map.len(), 0);
}

#[test]
fn test_release_handle_allows_reuse() {
    let mut map = DeferredMap::<i32>::new();

    // Allocate and release
    // 分配并释放
    let handle1 = map.allocate_handle();
    let index1 = handle1.index();
    map.release_handle(handle1);

    // Allocate again, should reuse the same slot
    // 再次分配,应该复用相同的 slot
    let handle2 = map.allocate_handle();
    let index2 = handle2.index();

    assert_eq!(index1, index2);
}

#[test]
fn test_release_handle_increments_generation() {
    let mut map = DeferredMap::<i32>::new();

    // Allocate and release
    // 分配并释放
    let handle1 = map.allocate_handle();
    let gen1 = handle1.generation();
    map.release_handle(handle1);

    // Allocate again, generation should increment
    // 再次分配,generation 应该递增
    let handle2 = map.allocate_handle();
    let gen2 = handle2.generation();

    // Generation (high 30 bits) should increment by 1
    // Generation(高 30 位)应该递增 1
    assert_eq!(gen2.get(), gen1.get() + 1);
}

#[test]
fn test_release_handle_multiple_handles() {
    let mut map = DeferredMap::<i32>::new();

    // Allocate multiple handles
    // 分配多个 handle
    let handles: Vec<_> = (0..10).map(|_| map.allocate_handle()).collect();

    // Release all of them
    // 全部释放
    for handle in handles {
        map.release_handle(handle);
    }

    // Map should still be empty
    // Map 应该仍然为空
    assert_eq!(map.len(), 0);
}

#[test]
fn test_release_handle_doesnt_affect_len() {
    let mut map = DeferredMap::new();

    // Insert some elements
    // 插入一些元素
    let h1 = map.allocate_handle();
    map.insert(h1, 1);

    let h2 = map.allocate_handle();
    map.insert(h2, 2);

    assert_eq!(map.len(), 2);

    // Allocate and release a handle
    // 分配并释放一个 handle
    let h3 = map.allocate_handle();
    map.release_handle(h3);

    // Length should remain unchanged
    // 长度应该保持不变
    assert_eq!(map.len(), 2);
}

#[test]
fn test_release_handle_interleaved_with_insertions() {
    let mut map = DeferredMap::new();

    // Allocate multiple handles
    // 分配多个 handle
    let h1 = map.allocate_handle();
    let k1 = h1.key();
    let h2 = map.allocate_handle();
    let h3 = map.allocate_handle();
    let k3 = h3.key();
    let h4 = map.allocate_handle();

    // Insert some, release others
    // 插入一些,释放其他
    map.insert(h1, 1);
    map.release_handle(h2); // Release unused | 释放未使用的
    map.insert(h3, 3);
    map.release_handle(h4); // Release unused | 释放未使用的

    // Verify only inserted values are accessible
    // 验证只有插入的值可访问
    assert_eq!(map.get(k1), Some(&1));
    assert_eq!(map.get(k3), Some(&3));
    assert_eq!(map.len(), 2);
}

#[test]
fn test_release_handle_then_insert_at_same_slot() {
    let mut map = DeferredMap::new();

    // Allocate and release
    // 分配并释放
    let handle1 = map.allocate_handle();
    let index1 = handle1.index();
    map.release_handle(handle1);

    // Allocate again and insert
    // 再次分配并插入
    let handle2 = map.allocate_handle();
    let index2 = handle2.index();
    let key2 = handle2.key();
    map.insert(handle2, 42);

    // Should use the same index
    // 应该使用相同的索引
    assert_eq!(index1, index2);
    assert_eq!(map.get(key2), Some(&42));
}

#[test]
fn test_release_handle_lifo_order() {
    let mut map = DeferredMap::<i32>::new();

    // Allocate multiple handles first (without releasing)
    // 先分配多个 handle(不释放)
    let h1 = map.allocate_handle();
    let idx1 = h1.index();

    let h2 = map.allocate_handle();
    let idx2 = h2.index();

    let h3 = map.allocate_handle();
    let idx3 = h3.index();

    // Release them in order
    // 按顺序释放它们
    map.release_handle(h1);
    map.release_handle(h2);
    map.release_handle(h3);

    // Allocate again, should reuse in LIFO order (last released first)
    // 再次分配,应该以 LIFO 顺序复用(最后释放的先用)
    let h4 = map.allocate_handle();
    assert_eq!(h4.index(), idx3);

    let h5 = map.allocate_handle();
    assert_eq!(h5.index(), idx2);

    let h6 = map.allocate_handle();
    assert_eq!(h6.index(), idx1);
}

#[test]
fn test_release_handle_with_capacity_check() {
    let mut map = DeferredMap::<i32>::new();

    // Allocate multiple handles first (to grow capacity)
    // 先分配多个 handle(以增长容量)
    let handles: Vec<_> = (0..10).map(|_| map.allocate_handle()).collect();

    // Capacity should have grown
    // 容量应该增长了
    assert!(map.capacity() >= 10);

    // Now release all of them
    // 现在全部释放
    for h in handles {
        map.release_handle(h);
    }

    // Capacity should remain
    // 容量应该保持
    assert!(map.capacity() >= 10);

    // But map should still be empty
    // 但 map 应该仍然为空
    assert_eq!(map.len(), 0);
}

#[test]
fn test_release_handle_mixed_with_removal() {
    let mut map = DeferredMap::new();

    // Allocate and insert
    // 分配并插入
    let h1 = map.allocate_handle();
    let k1 = h1.key();
    map.insert(h1, 1);

    // Allocate and release
    // 分配并释放
    let h2 = map.allocate_handle();
    map.release_handle(h2);

    // Remove inserted value
    // 删除插入的值
    map.remove(k1);

    // Allocate two new handles
    // 分配两个新 handle
    let h3 = map.allocate_handle();
    let k3 = h3.key();
    let h4 = map.allocate_handle();
    let k4 = h4.key();

    // Both should reuse slots
    // 两者都应该复用 slot
    map.insert(h3, 3);
    map.insert(h4, 4);

    assert_eq!(map.get(k3), Some(&3));
    assert_eq!(map.get(k4), Some(&4));
    assert_eq!(map.len(), 2);
}