1use crate::Vector;
2use alloc::vec::Vec;
3use core::cmp::{Ord, Ordering};
4use core::fmt::Debug;
5use core::marker::PhantomData;
6use core::mem;
7
8pub const B: usize = 8;
9pub const MAX_CHILDREN: usize = B * 2;
10pub const MAX_KEYS: usize = MAX_CHILDREN - 1;
11
12#[derive(Debug)]
13pub struct BVecTreeNode<K, V> {
14 keys: [Option<(K, V)>; MAX_KEYS],
15 children: [Option<u32>; MAX_CHILDREN],
16 cur_keys: usize,
17 leaf: bool,
18}
19
20impl<K, V> Default for BVecTreeNode<K, V> {
21 fn default() -> Self {
22 Self {
23 keys: Default::default(),
24 children: Default::default(),
25 cur_keys: 0,
26 leaf: false,
27 }
28 }
29}
30
31impl<K: Ord, V> BVecTreeNode<K, V> {
32 pub fn find_key_id(&self, value: &K) -> (usize, bool) {
33 let keys = &self.keys[..self.cur_keys];
42
43 for (i, item) in keys.iter().enumerate().take(self.cur_keys) {
44 match value.cmp(&item.as_ref().unwrap().0) {
45 Ordering::Greater => {}
46 Ordering::Equal => {
47 return (i, true);
48 }
49 Ordering::Less => {
50 return (i, false);
51 }
52 }
53 }
54 (self.cur_keys, false)
55 }
56
57 pub fn shift_right(&mut self, idx: usize) {
59 debug_assert!(self.cur_keys != MAX_KEYS);
60
61 self.keys[idx..(self.cur_keys + 1)].rotate_right(1);
62 if !self.leaf {
63 self.children[idx..(self.cur_keys + 2)].rotate_right(1);
64 }
65 self.cur_keys += 1;
66 }
67
68 pub fn shift_right_rchild(&mut self, idx: usize) {
70 debug_assert!(self.cur_keys != MAX_KEYS);
71
72 self.keys[idx..(self.cur_keys + 1)].rotate_right(1);
73 if !self.leaf {
74 self.children[(idx + 1)..(self.cur_keys + 2)].rotate_right(1);
75 }
76 self.cur_keys += 1;
77 }
78
79 pub fn shift_left(&mut self, idx: usize) {
81 debug_assert!(self.keys[idx].is_none());
82 debug_assert!(self.children[idx].is_none());
83
84 self.keys[idx..self.cur_keys].rotate_left(1);
85 self.children[idx..(self.cur_keys + 1)].rotate_left(1);
86 self.cur_keys -= 1;
87 }
88
89 pub fn shift_left_rchild(&mut self, idx: usize) {
91 debug_assert!(self.keys[idx].is_none());
92 debug_assert!(self.children[idx + 1].is_none());
93
94 self.keys[idx..self.cur_keys].rotate_left(1);
95 self.children[(idx + 1)..(self.cur_keys + 1)].rotate_left(1);
96 self.cur_keys -= 1;
97 }
98
99 fn remove_key(&mut self, key_id: usize) -> (Option<(K, V)>, Option<u32>) {
100 let key = self.keys[key_id].take();
101 let child = self.children[key_id].take();
102
103 self.shift_left(key_id);
104
105 (key, child)
106 }
107
108 fn remove_key_rchild(&mut self, key_id: usize) -> (Option<(K, V)>, Option<u32>) {
109 let key = self.keys[key_id].take();
110 let child = self.children[key_id + 1].take();
111
112 self.shift_left_rchild(key_id);
113
114 (key, child)
115 }
116
117 pub fn insert_leaf_key(&mut self, idx: usize, key: (K, V)) {
118 debug_assert!(self.leaf);
119 debug_assert!(idx <= self.cur_keys);
120 self.shift_right(idx);
121 self.keys[idx] = Some(key);
122 }
123
124 pub fn insert_node_at(&mut self, value: (K, V), idx: usize) -> Option<(K, V)> {
125 let exact = if self.cur_keys > idx {
126 value.0 == self.keys[idx].as_ref().unwrap().0
127 } else {
128 false
129 };
130
131 if exact {
132 mem::replace(&mut self.keys[idx], Some(value))
133 } else {
134 self.shift_right(idx);
135 self.keys[idx] = Some(value);
136 None
137 }
138 }
139
140 pub fn insert_node(&mut self, value: (K, V)) -> Option<(K, V)> {
141 let idx = self.find_key_id(&value.0).0;
142 self.insert_node_at(value, idx)
143 }
144
145 pub fn insert_node_rchild_at(&mut self, value: (K, V), idx: usize) -> Option<(K, V)> {
146 let exact = if self.cur_keys > idx {
147 debug_assert!(value.0 <= self.keys[idx].as_ref().unwrap().0);
148 value.0 == self.keys[idx].as_ref().unwrap().0
149 } else {
150 false
151 };
152
153 if exact {
154 mem::replace(&mut self.keys[idx], Some(value))
155 } else {
156 self.shift_right_rchild(idx);
157 self.keys[idx] = Some(value);
158 None
159 }
160 }
161
162 pub fn insert_node_rchild(&mut self, value: (K, V)) -> Option<(K, V)> {
163 let idx = self.find_key_id(&value.0).0;
164 self.insert_node_rchild_at(value, idx)
165 }
166
167 pub fn merge(&mut self, mid: (K, V), other: &mut Self) {
169 debug_assert!(self.cur_keys + 1 + other.cur_keys <= MAX_KEYS);
170
171 if self.cur_keys > 0 {
172 debug_assert!(mid.0 > self.keys[self.cur_keys - 1].as_ref().unwrap().0);
173 }
174
175 if other.cur_keys > 0 {
176 debug_assert!(mid.0 < other.keys[0].as_ref().unwrap().0);
177 }
178
179 self.keys[self.cur_keys] = Some(mid);
180
181 for i in 0..other.cur_keys {
182 self.keys[self.cur_keys + 1 + i] = other.keys[i].take();
183 }
184
185 for i in 0..=other.cur_keys {
186 self.children[self.cur_keys + 1 + i] = other.children[i].take();
187 }
188
189 self.cur_keys += 1 + other.cur_keys;
190 other.cur_keys = 0;
191 }
192}
193
194pub struct BVecTreeMap<S, K, V> {
195 root: Option<u32>,
196 free_head: Option<u32>,
197 tree_buf: S,
198 len: usize,
199 _phantom: PhantomData<(K, V)>,
200}
201
202impl<S: Default + Vector<BVecTreeNode<K, V>>, K, V> Default for BVecTreeMap<S, K, V> {
203 fn default() -> Self {
204 Self {
205 root: None,
206 free_head: None,
207 tree_buf: S::default(),
208 len: 0,
209 _phantom: PhantomData::default(),
210 }
211 }
212}
213
214impl<K: Ord + Debug, V: Debug> BVecTreeMap<Vec<BVecTreeNode<K, V>>, K, V> {
215 pub fn new() -> Self {
216 Self::default()
217 }
218}
219
220impl<S: Vector<BVecTreeNode<K, V>>, K: Ord + Debug, V: Debug> BVecTreeMap<S, K, V> {
221 pub fn new_in(buf: S) -> Self {
236 Self {
237 tree_buf: buf,
238 root: None,
239 free_head: None,
240 len: 0,
241 _phantom: PhantomData::default(),
242 }
243 }
244
245 pub fn len(&self) -> usize {
246 self.len
247 }
248
249 pub fn is_empty(&self) -> bool {
250 self.len() == 0
251 }
252
253 pub fn clear(&mut self) {
254 self.root = None;
255 self.free_head = None;
256 self.tree_buf.clear();
257 }
258
259 pub fn height(&self) -> usize {
260 if let Some(idx) = self.root {
261 let mut ret = 1;
262
263 let mut cur_node = idx;
264
265 while !self.get_node(cur_node).leaf {
266 cur_node = self.get_node(cur_node).children[0].unwrap();
267 ret += 1
268 }
269
270 ret
271 } else {
272 0
273 }
274 }
275
276 pub fn get_mut(&mut self, key: &K) -> Option<&mut V> {
277 if let Some(idx) = self.root {
278 let mut cur_node = idx;
279
280 loop {
281 let node = self.get_node_mut(cur_node);
282
283 let (idx, exact) = node.find_key_id(key);
284
285 if exact {
286 return Some(&mut self.get_node_mut(cur_node).keys[idx].as_mut().unwrap().1);
287 }
288
289 if node.leaf {
290 break;
291 }
292
293 cur_node = node.children[idx].unwrap();
294 }
295 }
296
297 None
298 }
299
300 pub fn contains_key(&self, key: &K) -> bool {
301 if let Some(idx) = self.root {
302 let mut cur_node = idx;
303
304 loop {
305 let node = self.get_node(cur_node);
306
307 let (idx, exact) = node.find_key_id(key);
308
309 if exact {
310 return true;
311 }
312
313 if node.leaf {
314 break;
315 }
316
317 cur_node = node.children[idx].unwrap();
318 }
319 }
320
321 false
322 }
323
324 pub fn insert(&mut self, key: K, value: V) -> Option<V> {
325 let ret = self.insert_internal((key, value));
326 if ret.is_some() {
327 Some(ret.unwrap().1)
328 } else {
329 self.len += 1;
330 None
331 }
332 }
333
334 fn insert_internal(&mut self, value: (K, V)) -> Option<(K, V)> {
335 if let Some(idx) = self.root {
336 let root_node = self.get_node_mut(idx);
337
338 if root_node.cur_keys == MAX_KEYS {
339 let new_root = self.allocate_node();
340 let new_root_node = self.get_node_mut(new_root);
341 new_root_node.children[0] = Some(idx);
342 self.root = Some(new_root);
343 self.split_child(new_root, 0);
344 }
345 } else {
346 self.root = Some(self.allocate_node());
347 self.get_node_mut(self.root.unwrap()).leaf = true;
348 }
349
350 let mut cur_node = self.root.unwrap();
351
352 loop {
353 let node = self.get_node_mut(cur_node);
354
355 if node.leaf {
356 break;
357 }
358
359 let (mut idx, exact) = node.find_key_id(&value.0);
360
361 if exact {
362 return node.insert_node_at(value, idx);
363 } else {
364 let child = node.children[idx].unwrap();
365
366 if self.get_node(child).cur_keys == MAX_KEYS {
367 self.split_child(cur_node, idx);
368
369 match value
370 .0
371 .cmp(&self.get_node(cur_node).keys[idx].as_ref().unwrap().0)
372 {
373 Ordering::Greater => {
374 idx += 1;
375 }
376 Ordering::Equal => {
377 return self.get_node_mut(cur_node).insert_node_at(value, idx);
378 }
379 Ordering::Less => {}
380 }
381 }
382
383 cur_node = self.get_node(cur_node).children[idx].unwrap();
384 }
385 }
386
387 self.insert_node(cur_node, value)
388 }
389
390 pub fn remove(&mut self, key: &K) -> Option<V> {
391 let ret = self.remove_entry(key);
392 if ret.is_some() {
393 self.len -= 1;
394 Some(ret.unwrap().1)
395 } else {
396 None
397 }
398 }
399
400 pub fn remove_entry(&mut self, key: &K) -> Option<(K, V)> {
401 let mut cur_node = self.root;
402
403 while let Some(node_idx) = cur_node {
404 let node = self.get_node(node_idx);
405
406 let (idx, exact) = node.find_key_id(key);
407
408 if exact {
409 if node.leaf {
410 let ret = self.remove_key(node_idx, idx).0;
411 return ret;
412 } else {
413 let left_child = node.children[idx].unwrap();
414 let right_child = node.children[idx + 1].unwrap();
415
416 if self.get_node(left_child).cur_keys > B - 1 {
417 let mut lr_child = left_child;
418 while !self.get_node(lr_child).leaf {
419 self.ensure_node_degree(lr_child, self.get_node(lr_child).cur_keys);
420 let lr_node = self.get_node(lr_child);
421 lr_child = lr_node.children[lr_node.cur_keys].unwrap();
422 }
423 let (pred, _) =
424 self.remove_key(lr_child, self.get_node(lr_child).cur_keys - 1);
425 return mem::replace(&mut self.get_node_mut(node_idx).keys[idx], pred);
426 } else if self.get_node(right_child).cur_keys > B - 1 {
427 let mut rl_child = right_child;
428 while !self.get_node(rl_child).leaf {
429 self.ensure_node_degree(rl_child, 0);
430 let rl_node = self.get_node(rl_child);
431 rl_child = rl_node.children[0].unwrap();
432 }
433 let (succ, _) = self.remove_key(rl_child, 0);
434 return mem::replace(&mut self.get_node_mut(node_idx).keys[idx], succ);
435 } else {
436 let ret = self.merge_children(node_idx, idx);
437 if cur_node == self.root {
438 self.root = Some(ret);
439 }
440 cur_node = Some(left_child);
441 continue;
442 }
443 }
444 }
445
446 if !node.leaf {
447 let ret = self.ensure_node_degree(node_idx, idx);
448 if ret != node_idx {
449 if cur_node == self.root {
450 self.root = Some(ret);
451 }
452 cur_node = Some(ret);
453 } else {
454 let node = self.get_node(node_idx);
455 cur_node = node.children[node.find_key_id(key).0];
456 }
457 } else {
458 cur_node = node.children[idx];
459 }
460 }
461
462 None
463 }
464
465 fn remove_key(&mut self, node_id: u32, key_id: usize) -> (Option<(K, V)>, Option<u32>) {
466 let node = self.get_node_mut(node_id);
467 node.remove_key(key_id)
468 }
469
470 fn merge_children(&mut self, parent: u32, key_id: usize) -> u32 {
474 let parent_node = self.get_node_mut(parent);
475 let left_child = parent_node.children[key_id].unwrap();
476 let right_child = parent_node.children[key_id + 1].unwrap();
477
478 let (mid, _) = parent_node.remove_key_rchild(key_id);
479 let (left_node, right_node) = self.get_two_nodes_mut(left_child, right_child);
480
481 left_node.merge(mid.unwrap(), right_node);
482
483 self.free_node(right_child);
484
485 if self.get_node(parent).cur_keys == 0 {
486 self.get_node_mut(parent).children[0] = None;
487 self.free_node(parent);
488 left_child
489 } else {
490 parent
491 }
492 }
493
494 fn ensure_node_degree(&mut self, parent: u32, child_id: usize) -> u32 {
495 let parent_node = self.get_node(parent);
496 let child_node_id = parent_node.children[child_id].unwrap();
497 let child_node = self.get_node(child_node_id);
498
499 if child_node.cur_keys < B {
500 if child_id != 0
501 && self
502 .get_node(parent_node.children[child_id - 1].unwrap())
503 .cur_keys
504 > B - 1
505 {
506 let (key, (left, right)) = self.get_key_nodes_mut(parent, child_id - 1);
507 let left_key = key.take().unwrap();
508 right.insert_node(left_key);
509 let (nkey, rchild) = left.remove_key_rchild(left.cur_keys - 1);
510 right.children[0] = rchild;
511 *key = nkey;
512 } else if child_id != parent_node.cur_keys
513 && self
514 .get_node(parent_node.children[child_id + 1].unwrap())
515 .cur_keys
516 > B - 1
517 {
518 let (key, (left, right)) = self.get_key_nodes_mut(parent, child_id);
519 let right_key = key.take().unwrap();
520 left.insert_node_rchild(right_key);
521 let (nkey, lchild) = right.remove_key(0);
522 left.children[left.cur_keys] = lchild;
523 *key = nkey;
524 } else if child_id > 0 {
525 return self.merge_children(parent, child_id - 1);
526 } else {
527 return self.merge_children(parent, child_id);
528 }
529 }
530
531 parent
532 }
533
534 fn split_child(&mut self, parent: u32, child_id: usize) {
535 let node_to_split = self.get_node(parent).children[child_id].unwrap();
536 let new_node = self.allocate_node();
537
538 let (left, right) = self.get_two_nodes_mut(node_to_split, new_node);
539
540 for i in 0..(B - 1) {
542 right.keys[i] = left.keys[i + B].take();
543 }
544
545 let mid = left.keys[B - 1].take().unwrap();
546
547 left.cur_keys = B - 1;
548 right.cur_keys = B - 1;
549
550 if left.leaf {
551 right.leaf = true;
552 } else {
553 for i in 0..B {
554 right.children[i] = left.children[i + B].take();
555 }
556 }
557
558 self.insert_node(parent, mid);
559
560 debug_assert!(self.get_node(parent).children[child_id].is_none());
561 debug_assert!(self.get_node(parent).children[child_id + 1].is_some());
562 let right_child = self.get_node_mut(parent).children[child_id + 1].take();
563 self.get_node_mut(parent).children[child_id] = right_child;
564 self.get_node_mut(parent).children[child_id + 1] = Some(new_node);
565 }
566
567 fn insert_node(&mut self, node_id: u32, value: (K, V)) -> Option<(K, V)> {
568 self.get_node_mut(node_id).insert_node(value)
569 }
570
571 fn get_node_mut(&mut self, id: u32) -> &mut BVecTreeNode<K, V> {
572 self.tree_buf.slice_mut().get_mut(id as usize).unwrap()
573 }
574
575 fn get_two_nodes_mut(
577 &mut self,
578 left: u32,
579 right: u32,
580 ) -> (&mut BVecTreeNode<K, V>, &mut BVecTreeNode<K, V>) {
581 debug_assert!(left != right);
582
583 if left < right {
584 let (_, br) = self.tree_buf.slice_mut().split_at_mut(left as usize);
585 let (left_ret, right_side) = br.split_first_mut().unwrap();
586 let (_, br) = right_side.split_at_mut((right - left - 1) as usize);
587 let (right_ret, _) = br.split_first_mut().unwrap();
588 (left_ret, right_ret)
589 } else {
590 let (_, br) = self.tree_buf.slice_mut().split_at_mut(right as usize);
591 let (right_ret, right_side) = br.split_first_mut().unwrap();
592 let (_, br) = right_side.split_at_mut((left - right - 1) as usize);
593 let (left_ret, _) = br.split_first_mut().unwrap();
594 (left_ret, right_ret)
595 }
596 }
597
598 fn get_key_nodes_mut(
599 &mut self,
600 parent: u32,
601 key: usize,
602 ) -> (
603 &mut Option<(K, V)>,
604 (&mut BVecTreeNode<K, V>, &mut BVecTreeNode<K, V>),
605 ) {
606 let parent_node = self.get_node_mut(parent);
607 let left = parent_node.children[key].unwrap();
608 let right = parent_node.children[key + 1].unwrap();
609 debug_assert!(left != parent);
610 debug_assert!(right != parent);
611 let key_mut = unsafe { &mut *(&mut parent_node.keys[key] as *mut _) };
613 (key_mut, self.get_two_nodes_mut(left, right))
614 }
615
616 fn get_node(&self, id: u32) -> &BVecTreeNode<K, V> {
617 self.tree_buf.slice().get(id as usize).unwrap()
618 }
619
620 fn allocate_node(&mut self) -> u32 {
621 if let Some(idx) = self.free_head {
622 let free_node = self.get_node_mut(idx);
623 let child_zero = free_node.children[0];
624 *free_node = BVecTreeNode::default();
625 self.free_head = child_zero;
626 idx
627 } else {
628 let ret = self.tree_buf.len() as u32;
629 self.tree_buf.push(BVecTreeNode::default());
630 ret
631 }
632 }
633
634 fn free_node(&mut self, node_id: u32) {
635 let head = self.free_head;
636 let node = self.get_node_mut(node_id);
637
638 debug_assert!(node.keys.iter().filter_map(|x| x.as_ref()).count() == 0);
640 debug_assert!(node.children.iter().filter_map(|x| x.as_ref()).count() == 0);
641
642 node.children[0] = head;
643 self.free_head = Some(node_id);
644 }
645}
646
647#[cfg(test)]
648mod tests {
649 use crate::BVecTreeMap;
650 use rand::{seq::SliceRandom, Rng, SeedableRng};
651 use rand_xorshift::XorShiftRng;
652 use std::collections::BTreeSet;
653
654 #[test]
655 fn test_random_add() {
656 for _ in 0..200 {
657 let seed = rand::thread_rng().gen_range(0, !0u64);
658 println!("Seed: {:x}", seed);
659
660 let mut rng: XorShiftRng = SeedableRng::seed_from_u64(seed);
661
662 let entries: Vec<_> = (0..1000).map(|_| rng.gen_range(0, 50000usize)).collect();
663 let entries_s: Vec<_> = (0..1000).map(|_| rng.gen_range(0, 50000usize)).collect();
664
665 let mut tree = BVecTreeMap::new();
666 let mut set = BTreeSet::new();
667
668 for i in entries.iter() {
669 set.insert(*i);
670 tree.insert(*i, ());
671 }
672
673 for i in entries_s.iter() {
674 assert_eq!(set.contains(i), tree.contains_key(i));
675 }
676
677 assert_eq!(tree.len(), set.len());
678 }
679 }
680
681 #[test]
682 fn test_random_remove() {
683 for _ in 0..500 {
684 let seed = rand::thread_rng().gen_range(0, !0u64);
685 println!("Seed: {:x}", seed);
686
687 let mut rng: XorShiftRng = SeedableRng::seed_from_u64(seed);
688
689 let entries: Vec<_> = (0..1000).map(|_| rng.gen_range(0, 50000usize)).collect();
690
691 let mut tree = BVecTreeMap::new();
692 let mut set = BTreeSet::new();
693
694 for i in entries.iter() {
695 set.insert(*i);
696 tree.insert(*i, ());
697 }
698
699 let mut entries_r: Vec<_> = set.iter().copied().collect();
700 entries_r.shuffle(&mut rng);
701
702 for i in entries_r.iter().take(200) {
703 let ret_set = set.remove(&i);
704 let ret_tree = tree.remove(&i);
705
706 assert!(
707 ret_tree.is_some() || !ret_set,
708 "{:?} {:?} {:?}",
709 ret_tree,
710 i,
711 tree.contains_key(&i)
712 );
713 }
714
715 assert_eq!(tree.len(), set.len());
716 }
717 }
718}