1use crate::generic::{
2 map::{BTreeMap, M},
3 node::{Address, Balance, Item, Node, Offset},
4};
5use cc_traits::{SimpleCollectionMut, SimpleCollectionRef, Slab, SlabMut};
6use smallvec::SmallVec;
7use std::{borrow::Borrow, mem::MaybeUninit};
8
9pub trait BTreeExt<K, V> {
26 fn root_id(&self) -> Option<usize>;
30
31 fn node(&self, id: usize) -> &Node<K, V>;
35
36 fn get_in<Q: ?Sized>(&self, key: &Q, id: usize) -> Option<&V>
38 where
39 K: Borrow<Q>,
40 Q: Ord;
41
42 fn item(&self, addr: Address) -> Option<&Item<K, V>>;
44
45 fn first_item_address(&self) -> Option<Address>;
49
50 fn first_back_address(&self) -> Address;
54
55 fn last_item_address(&self) -> Option<Address>;
59
60 fn last_valid_address(&self) -> Address;
62
63 fn normalize(&self, addr: Address) -> Option<Address>;
65
66 fn leaf_address(&self, addr: Address) -> Address;
70
71 fn previous_item_address(&self, addr: Address) -> Option<Address>;
94
95 fn previous_front_address(&self, addr: Address) -> Option<Address>;
121
122 fn next_item_address(&self, addr: Address) -> Option<Address>;
145
146 fn next_back_address(&self, addr: Address) -> Option<Address>;
172
173 fn next_item_or_back_address(&self, addr: Address) -> Option<Address>;
175
176 fn address_of<Q: ?Sized>(&self, key: &Q) -> Result<Address, Address>
182 where
183 K: Borrow<Q>,
184 Q: Ord;
185
186 fn address_in<Q: ?Sized>(&self, id: usize, key: &Q) -> Result<Address, Address>
190 where
191 K: Borrow<Q>,
192 Q: Ord;
193
194 #[cfg(debug_assertions)]
198 fn validate(&self)
199 where
200 K: Ord;
201
202 #[cfg(debug_assertions)]
206 fn validate_node(
207 &self,
208 id: usize,
209 parent: Option<usize>,
210 min: Option<&K>,
211 max: Option<&K>,
212 ) -> usize
213 where
214 K: Ord;
215}
216
217pub trait BTreeExtMut<K, V> {
229 fn set_len(&mut self, len: usize);
231
232 fn set_root_id(&mut self, id: Option<usize>);
234
235 fn node_mut(&mut self, id: usize) -> &mut Node<K, V>;
239
240 fn get_mut_in(&mut self, key: &K, id: usize) -> Option<&mut V>
242 where
243 K: Ord;
244
245 fn item_mut(&mut self, addr: Address) -> Option<&mut Item<K, V>>;
247
248 fn insert_at(&mut self, addr: Address, item: Item<K, V>) -> Address;
253
254 fn insert_exactly_at(
270 &mut self,
271 addr: Address,
272 item: Item<K, V>,
273 opt_right_id: Option<usize>,
274 ) -> Address;
275
276 fn replace_at(&mut self, addr: Address, key: K, value: V) -> (K, V);
278
279 fn replace_value_at(&mut self, addr: Address, value: V) -> V;
281
282 fn remove_at(&mut self, addr: Address) -> Option<(Item<K, V>, Address)>;
288
289 fn rebalance(&mut self, node_id: usize, addr: Address) -> Address;
291
292 fn update_in<T, F>(&mut self, id: usize, key: K, action: F) -> T
294 where
295 K: Ord,
296 F: FnOnce(Option<V>) -> (Option<V>, T);
297
298 fn update_at<T, F>(&mut self, addr: Address, action: F) -> T
300 where
301 K: Ord,
302 F: FnOnce(V) -> (Option<V>, T);
303
304 fn remove_rightmost_leaf_of(&mut self, node_id: usize) -> (Item<K, V>, usize);
309
310 fn allocate_node(&mut self, node: Node<K, V>) -> usize;
312
313 fn release_node(&mut self, id: usize) -> Node<K, V>;
315}
316
317impl<K, V, C: Slab<Node<K, V>>> BTreeExt<K, V> for BTreeMap<K, V, C>
318where
319 C: SimpleCollectionRef,
320{
321 #[inline]
322 fn root_id(&self) -> Option<usize> {
323 self.root
324 }
325
326 #[inline]
327 fn node(&self, id: usize) -> &Node<K, V> {
328 C::into_ref(self.nodes.get(id).unwrap())
329 }
330
331 #[inline]
332 fn get_in<Q: ?Sized>(&self, key: &Q, mut id: usize) -> Option<&V>
333 where
334 K: Borrow<Q>,
335 Q: Ord,
336 {
337 loop {
338 match self.node(id).get(key) {
339 Ok(value_opt) => return value_opt,
340 Err(child_id) => id = child_id,
341 }
342 }
343 }
344
345 fn item(&self, addr: Address) -> Option<&Item<K, V>> {
346 self.node(addr.id).item(addr.offset)
347 }
348
349 fn first_item_address(&self) -> Option<Address> {
350 match self.root {
351 Some(mut id) => loop {
352 match self.node(id).child_id_opt(0) {
353 Some(child_id) => id = child_id,
354 None => return Some(Address::new(id, 0.into())),
355 }
356 },
357 None => None,
358 }
359 }
360
361 fn first_back_address(&self) -> Address {
362 match self.root {
363 Some(mut id) => loop {
364 match self.node(id).child_id_opt(0) {
365 Some(child_id) => id = child_id,
366 None => return Address::new(id, 0.into()), }
368 },
369 None => Address::nowhere(),
370 }
371 }
372
373 fn last_item_address(&self) -> Option<Address> {
374 match self.root {
375 Some(mut id) => loop {
376 let node = self.node(id);
377 let index = node.item_count();
378 match node.child_id_opt(index) {
379 Some(child_id) => id = child_id,
380 None => return Some(Address::new(id, (index - 1).into())),
381 }
382 },
383 None => None,
384 }
385 }
386
387 fn last_valid_address(&self) -> Address {
388 match self.root {
389 Some(mut id) => loop {
390 let node = self.node(id);
391 let index = node.item_count();
392 match node.child_id_opt(index) {
393 Some(child_id) => id = child_id,
394 None => return Address::new(id, index.into()),
395 }
396 },
397 None => Address::nowhere(),
398 }
399 }
400
401 fn normalize(&self, mut addr: Address) -> Option<Address> {
402 if addr.is_nowhere() {
403 None
404 } else {
405 loop {
406 let node = self.node(addr.id);
407 if addr.offset >= node.item_count() {
408 match node.parent() {
409 Some(parent_id) => {
410 addr.offset = self.node(parent_id).child_index(addr.id).unwrap().into();
411 addr.id = parent_id;
412 }
413 None => return None,
414 }
415 } else {
416 return Some(addr);
417 }
418 }
419 }
420 }
421
422 #[inline]
423 fn leaf_address(&self, mut addr: Address) -> Address {
424 if !addr.is_nowhere() {
425 loop {
426 let node = self.node(addr.id);
427 match node.child_id_opt(addr.offset.unwrap()) {
428 Some(child_id) => {
430 addr.id = child_id;
431 addr.offset = self.node(child_id).item_count().into()
432 }
433 None => break,
434 }
435 }
436 }
437
438 addr
439 }
440
441 #[inline]
443 fn previous_item_address(&self, mut addr: Address) -> Option<Address> {
444 if addr.is_nowhere() {
445 return None;
446 }
447
448 loop {
449 let node = self.node(addr.id);
450
451 match node.child_id_opt(addr.offset.unwrap()) {
452 Some(child_id) => {
454 addr.offset = self.node(child_id).item_count().into();
455 addr.id = child_id;
456 }
457 None => loop {
458 if addr.offset > 0 {
459 addr.offset.decr();
460 return Some(addr);
461 }
462
463 match self.node(addr.id).parent() {
464 Some(parent_id) => {
465 addr.offset = self.node(parent_id).child_index(addr.id).unwrap().into();
466 addr.id = parent_id;
467 }
468 None => return None,
469 }
470 },
471 }
472 }
473 }
474
475 #[inline]
476 fn previous_front_address(&self, mut addr: Address) -> Option<Address> {
477 if addr.is_nowhere() {
478 return None;
479 }
480
481 loop {
482 let node = self.node(addr.id);
483 match addr.offset.value() {
484 Some(offset) => {
485 let index = if offset < node.item_count() {
486 offset
487 } else {
488 node.item_count()
489 };
490
491 match node.child_id_opt(index) {
492 Some(child_id) => {
493 addr.offset = (self.node(child_id).item_count()).into();
494 addr.id = child_id;
495 }
496 None => {
497 addr.offset.decr();
498 break;
499 }
500 }
501 }
502 None => match node.parent() {
503 Some(parent_id) => {
504 addr.offset = self.node(parent_id).child_index(addr.id).unwrap().into();
505 addr.offset.decr();
506 addr.id = parent_id;
507 break;
508 }
509 None => return None,
510 },
511 }
512 }
513
514 Some(addr)
515 }
516
517 #[inline]
519 fn next_item_address(&self, mut addr: Address) -> Option<Address> {
520 if addr.is_nowhere() {
521 return None;
522 }
523
524 let item_count = self.node(addr.id).item_count();
525 match addr.offset.partial_cmp(&item_count) {
526 Some(std::cmp::Ordering::Less) => {
527 addr.offset.incr();
528 }
529 Some(std::cmp::Ordering::Greater) => {
530 return None;
531 }
532 _ => (),
533 }
534
535 loop {
538 let node = self.node(addr.id);
539
540 match node.child_id_opt(addr.offset.unwrap()) {
541 Some(child_id) => {
543 addr.offset = 0.into();
544 addr.id = child_id;
545 }
546 None => {
547 loop {
548 let node = self.node(addr.id);
549
550 if addr.offset < node.item_count() {
551 return Some(addr);
552 }
553
554 match node.parent() {
555 Some(parent_id) => {
556 addr.offset =
557 self.node(parent_id).child_index(addr.id).unwrap().into();
558 addr.id = parent_id;
559 }
560 None => {
561 return None;
563 }
564 }
565 }
566 }
567 }
568 }
569 }
570
571 #[inline]
572 fn next_back_address(&self, mut addr: Address) -> Option<Address> {
573 if addr.is_nowhere() {
574 return None;
575 }
576
577 loop {
578 let node = self.node(addr.id);
579 let index = match addr.offset.value() {
580 Some(offset) => offset + 1,
581 None => 0,
582 };
583
584 if index <= node.item_count() {
585 match node.child_id_opt(index) {
586 Some(child_id) => {
587 addr.offset = Offset::before();
588 addr.id = child_id;
589 }
590 None => {
591 addr.offset = index.into();
592 break;
593 }
594 }
595 } else {
596 match node.parent() {
597 Some(parent_id) => {
598 addr.offset = self.node(parent_id).child_index(addr.id).unwrap().into();
599 addr.id = parent_id;
600 break;
601 }
602 None => return None,
603 }
604 }
605 }
606
607 Some(addr)
608 }
609
610 #[inline]
611 fn next_item_or_back_address(&self, mut addr: Address) -> Option<Address> {
612 if addr.is_nowhere() {
613 return None;
614 }
615
616 let item_count = self.node(addr.id).item_count();
617 match addr.offset.partial_cmp(&item_count) {
618 Some(std::cmp::Ordering::Less) => {
619 addr.offset.incr();
620 }
621 Some(std::cmp::Ordering::Greater) => {
622 return None;
623 }
624 _ => (),
625 }
626
627 let original_addr_shifted = addr;
628
629 loop {
630 let node = self.node(addr.id);
631
632 match node.child_id_opt(addr.offset.unwrap()) {
633 Some(child_id) => {
635 addr.offset = 0.into();
636 addr.id = child_id;
637 }
638 None => loop {
639 let node = self.node(addr.id);
640
641 if addr.offset < node.item_count() {
642 return Some(addr);
643 }
644
645 match node.parent() {
646 Some(parent_id) => {
647 addr.offset = self.node(parent_id).child_index(addr.id).unwrap().into();
648 addr.id = parent_id;
649 }
650 None => return Some(original_addr_shifted),
651 }
652 },
653 }
654 }
655 }
656
657 fn address_of<Q: ?Sized>(&self, key: &Q) -> Result<Address, Address>
658 where
659 K: Borrow<Q>,
660 Q: Ord,
661 {
662 match self.root {
663 Some(id) => self.address_in(id, key),
664 None => Err(Address::nowhere()),
665 }
666 }
667
668 fn address_in<Q: ?Sized>(&self, mut id: usize, key: &Q) -> Result<Address, Address>
669 where
670 K: Borrow<Q>,
671 Q: Ord,
672 {
673 loop {
674 match self.node(id).offset_of(key) {
675 Ok(offset) => return Ok(Address { id, offset }),
676 Err((offset, None)) => return Err(Address::new(id, offset.into())),
677 Err((_, Some(child_id))) => {
678 id = child_id;
679 }
680 }
681 }
682 }
683
684 #[cfg(debug_assertions)]
685 fn validate(&self)
686 where
687 K: Ord,
688 {
689 if let Some(id) = self.root {
690 self.validate_node(id, None, None, None);
691 }
692 }
693
694 #[cfg(debug_assertions)]
696 fn validate_node(
697 &self,
698 id: usize,
699 parent: Option<usize>,
700 mut min: Option<&K>,
701 mut max: Option<&K>,
702 ) -> usize
703 where
704 K: Ord,
705 {
706 let node = self.node(id);
707 node.validate(parent, min, max);
708
709 let mut depth = None;
710 for (i, child_id) in node.children().enumerate() {
711 let (child_min, child_max) = node.separators(i);
712 let min = child_min.or_else(|| min.take());
713 let max = child_max.or_else(|| max.take());
714
715 let child_depth = self.validate_node(child_id, Some(id), min, max);
716 match depth {
717 None => depth = Some(child_depth),
718 Some(depth) => {
719 if depth != child_depth {
720 panic!("tree not balanced")
721 }
722 }
723 }
724 }
725
726 match depth {
727 Some(depth) => depth + 1,
728 None => 0,
729 }
730 }
731}
732
733impl<K, V, C: SlabMut<Node<K, V>>> BTreeExtMut<K, V> for BTreeMap<K, V, C>
734where
735 C: SimpleCollectionRef,
736 C: SimpleCollectionMut,
737{
738 #[inline]
739 fn set_len(&mut self, new_len: usize) {
740 self.len = new_len
741 }
742
743 #[inline]
744 fn set_root_id(&mut self, id: Option<usize>) {
745 self.root = id
746 }
747
748 #[inline]
749 fn node_mut(&mut self, id: usize) -> &mut Node<K, V> {
750 C::into_mut(self.nodes.get_mut(id).unwrap())
751 }
752
753 #[inline]
754 fn get_mut_in<'a>(&'a mut self, key: &K, mut id: usize) -> Option<&'a mut V>
755 where
756 K: Ord,
757 {
758 let value_ptr = loop {
763 match self.node_mut(id).get_mut(key) {
764 Ok(value_opt) => break value_opt.map(|value_ref| value_ref as *mut V),
765 Err(child_id) => id = child_id,
766 }
767 };
768
769 unsafe { value_ptr.map(|ptr| &mut *ptr) }
770 }
771
772 fn item_mut(&mut self, addr: Address) -> Option<&mut Item<K, V>> {
773 self.node_mut(addr.id).item_mut(addr.offset)
774 }
775
776 fn insert_at(&mut self, addr: Address, item: Item<K, V>) -> Address {
777 self.insert_exactly_at(self.leaf_address(addr), item, None)
778 }
779
780 fn insert_exactly_at(
781 &mut self,
782 addr: Address,
783 item: Item<K, V>,
784 opt_right_id: Option<usize>,
785 ) -> Address {
786 if addr.is_nowhere() {
787 if self.is_empty() {
788 let new_root = Node::leaf(None, item);
789 let id = self.allocate_node(new_root);
790 self.root = Some(id);
791 self.len += 1;
792 Address {
793 id,
794 offset: 0.into(),
795 }
796 } else {
797 panic!("invalid item address")
798 }
799 } else if self.is_empty() {
800 panic!("invalid item address")
801 } else {
802 self.node_mut(addr.id)
803 .insert(addr.offset, item, opt_right_id);
804 let new_addr = self.rebalance(addr.id, addr);
805 self.len += 1;
806 new_addr
807 }
808 }
809
810 fn replace_at(&mut self, addr: Address, key: K, value: V) -> (K, V) {
811 self.node_mut(addr.id)
812 .item_mut(addr.offset)
813 .unwrap()
814 .set(key, value)
815 }
816
817 fn replace_value_at(&mut self, addr: Address, value: V) -> V {
818 self.node_mut(addr.id)
819 .item_mut(addr.offset)
820 .unwrap()
821 .set_value(value)
822 }
823
824 #[inline]
825 fn remove_at(&mut self, addr: Address) -> Option<(Item<K, V>, Address)> {
826 self.len -= 1;
827 match self.node_mut(addr.id).leaf_remove(addr.offset) {
828 Some(Ok(item)) => {
829 let addr = self.rebalance(addr.id, addr);
831 Some((item, addr))
832 }
833 Some(Err(left_child_id)) => {
834 let new_addr = self.next_item_or_back_address(addr).unwrap();
836 let (separator, leaf_id) = self.remove_rightmost_leaf_of(left_child_id);
837 let item = self.node_mut(addr.id).replace(addr.offset, separator);
838 let addr = self.rebalance(leaf_id, new_addr);
839 Some((item, addr))
840 }
841 None => None,
842 }
843 }
844
845 fn update_in<T, F>(&mut self, mut id: usize, key: K, action: F) -> T
846 where
847 K: Ord,
848 F: FnOnce(Option<V>) -> (Option<V>, T),
849 {
850 loop {
851 match self.node(id).offset_of(&key) {
852 Ok(offset) => unsafe {
853 let mut value = MaybeUninit::uninit();
854 let item = self.node_mut(id).item_mut(offset).unwrap();
855 std::mem::swap(&mut value, item.maybe_uninit_value_mut());
856 let (opt_new_value, result) = action(Some(value.assume_init()));
857 match opt_new_value {
858 Some(new_value) => {
859 let mut new_value = MaybeUninit::new(new_value);
860 std::mem::swap(&mut new_value, item.maybe_uninit_value_mut());
861 }
862 None => {
863 let (item, _) = self.remove_at(Address::new(id, offset)).unwrap();
864 item.forget_value()
867 }
868 }
869
870 return result;
871 },
872 Err((offset, None)) => {
873 let (opt_new_value, result) = action(None);
874 if let Some(new_value) = opt_new_value {
875 let leaf_addr = Address::new(id, offset.into());
876 self.insert_exactly_at(leaf_addr, Item::new(key, new_value), None);
877 }
878
879 return result;
880 }
881 Err((_, Some(child_id))) => {
882 id = child_id;
883 }
884 }
885 }
886 }
887
888 fn update_at<T, F>(&mut self, addr: Address, action: F) -> T
889 where
890 K: Ord,
891 F: FnOnce(V) -> (Option<V>, T),
892 {
893 unsafe {
894 let mut value = MaybeUninit::uninit();
895 let item = self.node_mut(addr.id).item_mut(addr.offset).unwrap();
896 std::mem::swap(&mut value, item.maybe_uninit_value_mut());
897 let (opt_new_value, result) = action(value.assume_init());
898 match opt_new_value {
899 Some(new_value) => {
900 let mut new_value = MaybeUninit::new(new_value);
901 std::mem::swap(&mut new_value, item.maybe_uninit_value_mut());
902 }
903 None => {
904 let (item, _) = self.remove_at(addr).unwrap();
905 item.forget_value()
908 }
909 }
910
911 result
912 }
913 }
914
915 #[inline]
916 fn rebalance(&mut self, mut id: usize, mut addr: Address) -> Address {
917 let mut balance = self.node(id).balance();
918
919 loop {
920 match balance {
921 Balance::Balanced => break,
922 Balance::Overflow => {
923 assert!(!self.node_mut(id).is_underflowing());
924 let (median_offset, median, right_node) = self.node_mut(id).split();
925 let right_id = self.allocate_node(right_node);
926
927 match self.node(id).parent() {
928 Some(parent_id) => {
929 let parent = self.node_mut(parent_id);
930 let offset = parent.child_index(id).unwrap().into();
931 parent.insert(offset, median, Some(right_id));
932
933 if addr.id == id {
935 match addr.offset.partial_cmp(&median_offset) {
936 Some(std::cmp::Ordering::Equal) => {
937 addr = Address {
938 id: parent_id,
939 offset,
940 }
941 }
942 Some(std::cmp::Ordering::Greater) => {
943 addr = Address {
944 id: right_id,
945 offset: (addr.offset.unwrap() - median_offset - 1)
946 .into(),
947 }
948 }
949 _ => (),
950 }
951 } else if addr.id == parent_id && addr.offset >= offset {
952 addr.offset.incr()
953 }
954
955 id = parent_id;
956 balance = parent.balance()
957 }
958 None => {
959 let left_id = id;
960 let new_root = Node::binary(None, left_id, median, right_id);
961 let root_id = self.allocate_node(new_root);
962
963 self.root = Some(root_id);
964 self.node_mut(left_id).set_parent(Some(root_id));
965 self.node_mut(right_id).set_parent(Some(root_id));
966
967 if addr.id == id {
969 match addr.offset.partial_cmp(&median_offset) {
970 Some(std::cmp::Ordering::Equal) => {
971 addr = Address {
972 id: root_id,
973 offset: 0.into(),
974 }
975 }
976 Some(std::cmp::Ordering::Greater) => {
977 addr = Address {
978 id: right_id,
979 offset: (addr.offset.unwrap() - median_offset - 1)
980 .into(),
981 }
982 }
983 _ => (),
984 }
985 }
986
987 break;
988 }
989 };
990 }
991 Balance::Underflow(is_empty) => {
992 match self.node(id).parent() {
993 Some(parent_id) => {
994 let index = self.node(parent_id).child_index(id).unwrap();
995 if self.try_rotate_left(parent_id, index, &mut addr)
998 || self.try_rotate_right(parent_id, index, &mut addr)
999 {
1000 break;
1001 } else {
1002 let (new_balance, new_addr) = self.merge(parent_id, index, addr);
1005 balance = new_balance;
1006 addr = new_addr;
1007 id = parent_id
1010 }
1011 }
1012 None => {
1013 if is_empty {
1015 self.root = self.node(id).child_id_opt(0);
1016
1017 match self.root {
1019 Some(root_id) => {
1020 let root = self.node_mut(root_id);
1021 root.set_parent(None);
1022
1023 if addr.id == id {
1024 addr.id = root_id;
1025 addr.offset = root.item_count().into()
1026 }
1027 }
1028 None => addr = Address::nowhere(),
1029 }
1030
1031 self.release_node(id);
1032 }
1033
1034 break;
1035 }
1036 }
1037 }
1038 }
1039 }
1040
1041 addr
1042 }
1043
1044 #[inline]
1045 fn remove_rightmost_leaf_of(&mut self, mut id: usize) -> (Item<K, V>, usize) {
1046 loop {
1047 match self.node_mut(id).remove_rightmost_leaf() {
1048 Ok(result) => return (result, id),
1049 Err(child_id) => {
1050 id = child_id;
1051 }
1052 }
1053 }
1054 }
1055
1056 #[inline]
1057 fn allocate_node(&mut self, node: Node<K, V>) -> usize {
1058 let mut children: SmallVec<[usize; M]> = SmallVec::new();
1059 let id = self.nodes.insert(node);
1060
1061 for child_id in self.node(id).children() {
1062 children.push(child_id)
1063 }
1064
1065 for child_id in children {
1066 self.node_mut(child_id).set_parent(Some(id))
1067 }
1068
1069 id
1070 }
1071
1072 #[inline]
1073 fn release_node(&mut self, id: usize) -> Node<K, V> {
1074 self.nodes.remove(id).unwrap()
1075 }
1076}