cpp_map 0.2.0

A simple C++ std::map emulator
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
// SPDX-License-Identifier: MIT OR Apache-2.0

// Copyright 2025 Eadf (github.com/eadf)
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use cpp_map::prelude::*;
mod common;
use common::*;
use cpp_map::CppMapError;

#[test]
fn test_forward_cursor() {
    let (list, map) = llt_create_test_data(500, 20, 7);

    // Compare forward iteration
    let mut cursor = list.first();
    let mut list_items = Vec::new();
    list_items.push(cursor.unwrap());
    while let Some(next_pos) = list.next_pos(cursor) {
        list_items.push(next_pos);
        cursor = Some(next_pos);
    }
    let map_items: Vec<_> = map.iter().collect();
    assert_eq!(list_items.len(), map_items.len());

    for (idx, (m_k, m_v)) in list_items.iter().zip(map_items.iter()) {
        let kv = list.get_at(*idx);
        assert_eq!(kv, Some((*m_k, *m_v)));
    }
}

#[test]
fn test_reverse_cursor() {
    let (list, map) = llt_create_test_data(500, 30, 8);

    // Compare reverse iteration
    let mut cursor = list.last();
    let mut list_items = Vec::new();
    list_items.push(cursor.unwrap());
    while let Some(prev_pos) = list.prev_pos(cursor) {
        list_items.push(prev_pos);
        cursor = Some(prev_pos);
    }
    let map_items: Vec<_> = map.iter().rev().collect();

    assert_eq!(list_items.len(), map_items.len());

    for (idx, (m_k, m_v)) in list_items.iter().zip(map_items.iter()) {
        let kv = list.get_at(*idx);
        assert_eq!(kv, Some((*m_k, *m_v)));
    }
}

#[test]
fn test_cursor_from() {
    let (skiplist, btreemap) = llt_create_test_data(500, 40, 9);
    let (start_key, _) = btreemap.iter().take(200).next().unwrap();

    // Get cursor starting from key
    let mut cursor = skiplist.lower_bound(start_key);
    assert!(skiplist.is_pos_valid(cursor));
    let mut list_items = Vec::new();
    list_items.push(skiplist.get_at(cursor.unwrap()).unwrap());
    while let Some(next_pos) = skiplist.next_pos(cursor) {
        list_items.push(skiplist.get_at(next_pos).unwrap());
        cursor = Some(next_pos);
    }

    // Get equivalent items from BTreeMap
    let map_items: Vec<_> = btreemap.range(start_key..).collect();

    assert_eq!(list_items.len(), map_items.len());

    for ((l_k, l_v), (m_k, m_v)) in list_items.iter().zip(map_items.iter()) {
        assert_eq!(l_k, m_k);
        assert_eq!(l_v, m_v);
    }
}

#[test]
fn test_cursor_rev_from() {
    let (skiplist, btreemap) = llt_create_test_data(500, 50, 10);
    let (start_key, _) = btreemap.iter().take(300).next().unwrap();

    // Get reverse cursor starting from key
    let mut cursor = skiplist.lower_bound(start_key);
    assert!(skiplist.is_pos_valid(cursor));
    let mut list_items = Vec::new();
    list_items.push(skiplist.get_at(cursor.unwrap()).unwrap());
    while let Some(prev_pos) = skiplist.prev_pos(cursor) {
        list_items.push(skiplist.get_at(prev_pos).unwrap());
        cursor = Some(prev_pos);
    }

    // Get equivalent items from BTreeMap (need to handle ranges differently for reverse)
    let map_items: Vec<_> = btreemap.range(..=start_key).rev().collect();

    assert_eq!(list_items.len(), map_items.len());

    for ((l_k, l_v), (m_k, m_v)) in list_items.iter().zip(map_items.iter()) {
        assert_eq!(l_k, m_k);
        assert_eq!(l_v, m_v);
    }
}

#[test]
fn test_empty_cursors() {
    let list: SkipList<i32, String> = SkipList::default();

    assert!(!list.is_pos_valid(list.first()));
    assert!(!list.is_pos_valid(list.last()));
    assert!(list.lower_bound(&0).is_none());
}

#[test]
fn test_mut_v() -> Result<(), CppMapError> {
    let mut list = SkipList::default();
    let _ = list.insert(1, 1.0_f32)?;
    let _ = list.insert(2, 2.0)?;
    let _ = list.insert(3, 3.0)?;
    let _ = list.insert(6, 6.1)?;

    let cursor = list.first();
    assert_eq!(list.get_v_at(cursor.unwrap()), Some(&1.0));

    let mut cursor = list.lower_bound(&2);
    cursor = list.next_pos(cursor);

    assert_eq!(list.get_v_at(cursor.unwrap()), Some(&3.0));
    list.set_v_at(cursor.unwrap(), 3.1);
    assert_eq!(list.get_v_at(cursor.unwrap()), Some(&3.1));

    if let Some(mut _kv) = list.get_mut_at(cursor.unwrap()) {
        *_kv.1 = 3.2;
    }
    assert_eq!(list.get_v_at(cursor.unwrap()), Some(&3.2));

    cursor = list.first();
    assert_eq!(list.get_v_at(cursor.unwrap()), Some(&1.0));
    cursor = list.last();
    assert_eq!(list.get_v_at(cursor.unwrap()), Some(&6.1));

    let cursor = list.lower_bound(&3);
    assert_eq!(list.get_v_at(cursor.unwrap()), Some(&3.2));
    let cursor = list.lower_bound(&1);
    assert_eq!(list.get_v_at(cursor.unwrap()), Some(&1.0));
    let cursor = list.lower_bound(&6);
    assert_eq!(list.get_v_at(cursor.unwrap()), Some(&6.1));
    // found key is greater than the given key
    let cursor = list.upper_bound(&6);
    assert!(cursor.is_none());

    let cursor = list.upper_bound(&1);
    assert_eq!(list.get_v_at(cursor.unwrap()), Some(&2.0));

    let cursor = list.upper_bound(&2);
    assert_eq!(list.get_v_at(cursor.unwrap()), Some(&3.2));
    let cursor = list.upper_bound(&3);
    assert_eq!(list.get_v_at(cursor.unwrap()), Some(&6.1));

    // found key is either equal to or greater than the given key
    let mut cursor = list.lower_bound(&6);
    assert_eq!(list.get_v_at(cursor.unwrap()), Some(&6.1));

    //list.debug_print();
    list.remove_by_index(&mut cursor);
    assert_eq!(list.get_v_at(cursor.unwrap()), Some(&3.2));
    let index = cursor.unwrap();

    assert_eq!(list.get_at(index), Some((&3, &3.2)));
    assert_eq!(list.get_k_at(index), Some(&3));
    assert_eq!(list.get_v_at(index), Some(&3.2));
    //let v = 3.2;
    assert_eq!(list.get_mut_at(index).unwrap().1, &3.2_f32);

    println!("{:?}", list);
    skp_test_head_and_tail_is_some(&list);
    Ok(())
}

#[test]
fn test_bounds_behavior() -> Result<(), CppMapError> {
    let mut list = SkipList::default();
    // Insert keys in non-sequential order to test proper ordering
    let _ = list.insert(5, 5.0)?;
    let _ = list.insert(1, 1.0)?;
    let _ = list.insert(3, 3.0)?;
    let _ = list.insert(8, 8.0)?;
    let _ = list.insert(6, 6.0)?;

    // Test lower_bound
    // Exact match exists
    let cursor = list.lower_bound(&3);
    assert!(list.is_pos_valid(cursor));
    assert!(!list.is_at_first(cursor));
    assert!(!list.is_at_last(cursor));
    assert_eq!(list.get_k_at(cursor.unwrap()), Some(&3));

    // No exact match - should find next higher (5)
    let cursor = list.lower_bound(&4);
    assert_eq!(list.get_k_at(cursor.unwrap()), Some(&5));

    // At beginning of list
    let cursor = list.lower_bound(&0);
    assert_eq!(list.get_k_at(cursor.unwrap()), Some(&1));

    // At end of list
    let cursor = list.lower_bound(&8);
    assert_eq!(list.get_k_at(cursor.unwrap()), Some(&8));

    // Beyond end of list
    let cursor = list.lower_bound(&9);
    assert!(cursor.is_none());

    // Test upper_bound
    // Exact match exists - should find next higher (5)
    let cursor = list.upper_bound(&3);
    assert_eq!(list.get_k_at(cursor.unwrap()), Some(&5));

    // No exact match - should find next higher (5)
    let cursor = list.upper_bound(&4);
    assert_eq!(list.get_k_at(cursor.unwrap()), Some(&5));

    // At beginning of list
    let cursor = list.upper_bound(&0);
    assert!(cursor.is_some());

    // At end of list
    let cursor = list.upper_bound(&8);
    assert!(cursor.is_none());

    // Between existing elements (between 6 and 8)
    let cursor = list.upper_bound(&7);
    assert_eq!(list.get_k_at(cursor.unwrap()), Some(&8));

    // Test empty list behavior
    let empty_list: SkipList<i32, f32> = SkipList::default();
    let cursor = empty_list.lower_bound(&1);
    assert!(cursor.is_none());
    let cursor = empty_list.upper_bound(&1);
    assert!(cursor.is_none());

    // Test with single element
    let mut single_list = SkipList::default();
    let _ = single_list.insert(10, 10.0)?;
    let cursor = single_list.lower_bound(&9);
    assert_eq!(single_list.get_k_at(cursor.unwrap()), Some(&10));
    let cursor = single_list.lower_bound(&10);
    assert_eq!(single_list.get_k_at(cursor.unwrap()), Some(&10));
    let cursor = single_list.lower_bound(&11);
    assert!(cursor.is_none());
    let cursor = single_list.upper_bound(&9);
    assert_eq!(single_list.get_k_at(cursor.unwrap()), Some(&10));
    let cursor = single_list.upper_bound(&10);
    assert!(cursor.is_none());
    Ok(())
}

#[test]
fn test_bounds_with_operations() -> Result<(), CppMapError> {
    let mut list = SkipList::default();
    let _ = list.insert(2_i32, 2.0_f32)?;
    let _ = list.insert(4, 4.0)?;
    let _ = list.insert(6, 6.0)?;
    let _ = list.insert(8, 8.0)?;

    // Test that bounds work after modifications
    let cursor = list.lower_bound(&5);
    assert_eq!(list.get_k_at(cursor.unwrap()), Some(&6));
    assert_eq!(list.get_v_at(cursor.unwrap()), Some(&6.0));

    // Test upper_bound after insertion
    let cursor = list.upper_bound(&5);
    assert_eq!(list.get_k_at(cursor.unwrap()), Some(&6));
    let _ = list.insert(5, 5.0)?;
    let cursor = list.upper_bound(&5);
    assert_eq!(list.get_k_at(cursor.unwrap()), Some(&6));

    // Test bounds after removal
    let mut cursor = list.lower_bound(&4);
    assert_eq!(list.get_k_at(cursor.unwrap()), Some(&4));
    list.remove_by_index(&mut cursor);
    let cursor = list.lower_bound(&4);
    assert_eq!(list.get_k_at(cursor.unwrap()), Some(&5));

    // use the Debug and Display methods
    let _ = format!("{}", list.first().unwrap());
    let _ = format!("{:?}", list.last());
    let _ = format!("{}", list.first().unwrap());
    let _ = format!("{:?}", list.first().unwrap());
    let _idx: usize = list.first().unwrap().into();

    let _cursor: usize = list.last().unwrap().into();

    skp_test_head_and_tail_is_some(&list);
    list.clear();
    let _ = format!("{:?}", list);
    Ok(())
}

#[test]
fn test_bounds_with_duplicate_behavior() -> Result<(), CppMapError> {
    // Even though our list doesn't allow duplicates, test how it behaves
    let mut list = SkipList::default();
    let _ = list.insert(1, 1.0)?;
    let _ = list.insert(1, 1.1)?; // This should not overwrite

    // Should find the existing (overwritten) entry
    let cursor = list.lower_bound(&1);
    assert_eq!(list.get_v_at(cursor.unwrap()), Some(&1.0));
    let cursor = list.upper_bound(&1);
    assert!(cursor.is_none());
    Ok(())
}

#[test]
fn test_remove_current_basic() -> Result<(), CppMapError> {
    let mut list = SkipList::default();
    let _ = list.insert(1, 1.0)?;
    let _ = list.insert(2, 2.0)?;
    let _ = list.insert(3, 3.0)?;

    // Remove middle element
    let mut cursor = list.lower_bound(&2);
    let removed = list.remove_by_index(&mut cursor);
    assert_eq!(removed.unwrap(), (2, 2.0));
    assert_eq!(list.get_k_at(cursor.unwrap()), Some(&1)); // Should move to prev
    assert_eq!(list.len(), 2);

    // Verify list integrity
    let cursor = list.lower_bound(&1);
    let cursor = list.next_pos(cursor);
    assert_eq!(list.get_k_at(cursor.unwrap()), Some(&3)); // 1->3 now
    Ok(())
}

#[test]
fn test_remove_current_edge_cases() -> Result<(), CppMapError> {
    // Test empty list
    let empty_list: SkipList<i32, f32> = SkipList::default();
    let cursor = empty_list.first();
    assert_eq!(cursor, None);

    // Test single element list
    let mut single_list = SkipList::default();
    let _ = single_list.insert(1, 1.0)?;
    let mut cursor = single_list.lower_bound(&1);
    let removed = single_list.remove_by_index(&mut cursor);
    assert_eq!(removed.unwrap(), (1, 1.0));
    assert!(!single_list.is_pos_valid(cursor)); // Cursor should be invalidated
    assert!(single_list.is_empty());

    // Test removing head
    let mut list = SkipList::default();
    let _ = list.insert(1, 1.0)?;
    let _ = list.insert(2, 2.0)?;
    let mut cursor = list.lower_bound(&1);
    let _removed = list.remove_by_index(&mut cursor);
    assert_eq!(list.get_k_at(cursor.unwrap()), Some(&2)); // Should move to next
    assert_eq!(list.len(), 1);

    // Test removing tail
    let mut list = SkipList::default();
    let _ = list.insert(1, 1.0)?;
    let _ = list.insert(2, 2.0)?;
    let mut cursor = list.lower_bound(&2);
    let _removed = list.remove_by_index(&mut cursor);
    assert_eq!(list.get_k_at(cursor.unwrap()), Some(&1)); // Should be invalid
    assert_eq!(list.len(), 1);

    // Verify cursor is properly invalidated when list becomes empty
    let mut list = SkipList::default();
    let _ = list.insert(1, 1.0)?;
    let mut cursor = list.lower_bound(&1);
    let _removed = list.remove_by_index(&mut cursor);
    assert!(!list.is_pos_valid(cursor));
    assert!(cursor.is_none());
    Ok(())
}

#[test]
fn test_remove_current_with_prev_next_behavior() -> Result<(), CppMapError> {
    let mut list = SkipList::default();
    let _ = list.insert(1, 1.0)?;
    let _ = list.insert(2, 2.0)?;
    let _ = list.insert(3, 3.0)?;

    // Remove middle element (has both prev and next)
    let mut cursor = list.lower_bound(&2);
    let _removed = list.remove_by_index(&mut cursor);
    // Should move to prev (1) according to implementation
    assert_eq!(list.get_k_at(cursor.unwrap()), Some(&1));
    let cursor = list.next_pos(cursor);
    assert_eq!(list.get_k_at(cursor.unwrap()), Some(&3));
    let mut cursor = list.prev_pos(cursor);

    // Remove again (now at head)
    let _removed = list.remove_by_index(&mut cursor);
    // Should move to next (3) since no prev exists
    assert_eq!(list.get_k_at(cursor.unwrap()), Some(&3));

    // Remove last remaining element
    let _removed = list.remove_by_index(&mut cursor);
    assert!(!list.is_pos_valid(cursor));
    assert!(list.is_empty());
    Ok(())
}

#[test]
fn test_remove_current_multiple_operations() -> Result<(), CppMapError> {
    let mut list = SkipList::default();
    for i in 1..=10_i32 {
        let _ = list.insert(i, i as f32)?;
    }

    // Remove in sequence
    let mut cursor = list.lower_bound(&5);
    assert_eq!(list.get_k_at(cursor.unwrap()), Some(&5));
    assert_eq!(list.get_v_at(cursor.unwrap()), Some(&5.0));

    // Should remove: 5 → 4 → 3 → 2 → 1
    for expected in [5, 4_i32, 3, 2, 1].iter() {
        let removed = list.remove_by_index(&mut cursor);
        assert_eq!(removed.unwrap().1, *expected as f32);
        assert_eq!(removed.unwrap().0, *expected);
        if *expected > 1 {
            assert_eq!(list.get_k_at(cursor.unwrap()).unwrap(), &(expected - 1));
        } else {
            assert_eq!(list.get_k_at(cursor.unwrap()).unwrap(), &(6));
        }
    }

    // Now at head, should move to next when removing
    let mut cursor = list.lower_bound(&5);
    assert_eq!(list.get_k_at(cursor.unwrap()), Some(&6));

    // Remove the rest
    while list.remove_by_index(&mut cursor).is_some() {}

    assert!(list.is_empty());
    assert!(!list.is_pos_valid(cursor));
    Ok(())
}