1#![allow(unstable_name_collisions)]
13
14mod branch;
15pub mod bytetable;
16mod entry;
17mod leaf;
18
19use arrayvec::ArrayVec;
20
21use branch::*;
22pub use entry::Entry;
23use leaf::*;
24
25pub use bytetable::*;
26use rand::thread_rng;
27use rand::RngCore;
28use std::cmp::Reverse;
29use std::convert::TryInto;
30use std::fmt;
31use std::fmt::Debug;
32use std::marker::PhantomData;
33use std::mem::transmute;
34use std::ptr::NonNull;
35use std::sync::Once;
36
37#[cfg(not(target_pointer_width = "64"))]
38compile_error!("PATCH tagged pointers require 64-bit targets");
39
40static mut SIP_KEY: [u8; 16] = [0; 16];
41static INIT: Once = Once::new();
42
43fn init_sip_key() {
46 INIT.call_once(|| {
47 bytetable::init();
48
49 let mut rng = thread_rng();
50 unsafe {
51 rng.fill_bytes(&mut SIP_KEY[..]);
52 }
53 });
54}
55
56pub const fn build_segmentation<const N: usize, const M: usize>(lens: [usize; M]) -> [usize; N] {
60 let mut res = [0; N];
61 let mut seg = 0;
62 let mut off = 0;
63 while seg < M {
64 let len = lens[seg];
65 let mut i = 0;
66 while i < len {
67 res[off + i] = seg;
68 i += 1;
69 }
70 off += len;
71 seg += 1;
72 }
73 res
74}
75
76pub const fn identity_map<const N: usize>() -> [usize; N] {
78 let mut res = [0; N];
79 let mut i = 0;
80 while i < N {
81 res[i] = i;
82 i += 1;
83 }
84 res
85}
86
87pub const fn build_key_to_tree<const N: usize, const M: usize>(
92 lens: [usize; M],
93 perm: [usize; M],
94) -> [usize; N] {
95 let mut key_starts = [0; M];
96 let mut off = 0;
97 let mut i = 0;
98 while i < M {
99 key_starts[i] = off;
100 off += lens[i];
101 i += 1;
102 }
103
104 let mut tree_starts = [0; M];
105 off = 0;
106 i = 0;
107 while i < M {
108 let seg = perm[i];
109 tree_starts[seg] = off;
110 off += lens[seg];
111 i += 1;
112 }
113
114 let mut res = [0; N];
115 let mut seg = 0;
116 while seg < M {
117 let len = lens[seg];
118 let ks = key_starts[seg];
119 let ts = tree_starts[seg];
120 let mut j = 0;
121 while j < len {
122 res[ks + j] = ts + j;
123 j += 1;
124 }
125 seg += 1;
126 }
127 res
128}
129
130pub const fn invert<const N: usize>(arr: [usize; N]) -> [usize; N] {
132 let mut res = [0; N];
133 let mut i = 0;
134 while i < N {
135 res[arr[i]] = i;
136 i += 1;
137 }
138 res
139}
140
141#[doc(hidden)]
142#[macro_export]
143macro_rules! key_segmentation {
144 (@count $($e:expr),* $(,)?) => {
145 <[()]>::len(&[$($crate::key_segmentation!(@sub $e)),*])
146 };
147 (@sub $e:expr) => { () };
148 ($name:ident, $len:expr, [$($seg_len:expr),+ $(,)?]) => {
149 #[derive(Copy, Clone, Debug)]
150 pub struct $name;
151 impl $name {
152 pub const SEG_LENS: [usize; $crate::key_segmentation!(@count $($seg_len),*)] = [$($seg_len),*];
153 }
154 impl $crate::patch::KeySegmentation<$len> for $name {
155 const SEGMENTS: [usize; $len] = $crate::patch::build_segmentation::<$len, {$crate::key_segmentation!(@count $($seg_len),*)}>(Self::SEG_LENS);
156 }
157 };
158}
159
160#[doc(hidden)]
161#[macro_export]
162macro_rules! key_schema {
163 (@count $($e:expr),* $(,)?) => {
164 <[()]>::len(&[$($crate::key_schema!(@sub $e)),*])
165 };
166 (@sub $e:expr) => { () };
167 ($name:ident, $seg:ty, $len:expr, [$($perm:expr),+ $(,)?]) => {
168 #[derive(Copy, Clone, Debug)]
169 pub struct $name;
170 impl $crate::patch::KeySchema<$len> for $name {
171 type Segmentation = $seg;
172 const SEGMENT_PERM: &'static [usize] = &[$($perm),*];
173 const KEY_TO_TREE: [usize; $len] = $crate::patch::build_key_to_tree::<$len, {$crate::key_schema!(@count $($perm),*)}>(<$seg>::SEG_LENS, [$($perm),*]);
174 const TREE_TO_KEY: [usize; $len] = $crate::patch::invert(Self::KEY_TO_TREE);
175 }
176 };
177}
178
179pub trait KeySchema<const KEY_LEN: usize>: Copy + Clone + Debug {
183 type Segmentation: KeySegmentation<KEY_LEN>;
185 const SEGMENT_PERM: &'static [usize];
187 const KEY_TO_TREE: [usize; KEY_LEN];
189 const TREE_TO_KEY: [usize; KEY_LEN];
191
192 fn tree_ordered(key: &[u8; KEY_LEN]) -> [u8; KEY_LEN] {
194 let mut new_key = [0; KEY_LEN];
195 let mut i = 0;
196 while i < KEY_LEN {
197 new_key[Self::KEY_TO_TREE[i]] = key[i];
198 i += 1;
199 }
200 new_key
201 }
202
203 fn key_ordered(tree_key: &[u8; KEY_LEN]) -> [u8; KEY_LEN] {
205 let mut new_key = [0; KEY_LEN];
206 let mut i = 0;
207 while i < KEY_LEN {
208 new_key[Self::TREE_TO_KEY[i]] = tree_key[i];
209 i += 1;
210 }
211 new_key
212 }
213
214 fn segment_of_tree_depth(at_depth: usize) -> usize {
220 <Self::Segmentation as KeySegmentation<KEY_LEN>>::SEGMENTS[Self::TREE_TO_KEY[at_depth]]
221 }
222
223 fn same_segment_tree(a: usize, b: usize) -> bool {
226 <Self::Segmentation as KeySegmentation<KEY_LEN>>::SEGMENTS[Self::TREE_TO_KEY[a]]
227 == <Self::Segmentation as KeySegmentation<KEY_LEN>>::SEGMENTS[Self::TREE_TO_KEY[b]]
228 }
229}
230
231pub trait KeySegmentation<const KEY_LEN: usize>: Copy + Clone + Debug {
242 const SEGMENTS: [usize; KEY_LEN];
244}
245
246#[derive(Copy, Clone, Debug)]
250pub struct IdentitySchema {}
251
252#[derive(Copy, Clone, Debug)]
256pub struct SingleSegmentation {}
257impl<const KEY_LEN: usize> KeySchema<KEY_LEN> for IdentitySchema {
258 type Segmentation = SingleSegmentation;
259 const SEGMENT_PERM: &'static [usize] = &[0];
260 const KEY_TO_TREE: [usize; KEY_LEN] = identity_map::<KEY_LEN>();
261 const TREE_TO_KEY: [usize; KEY_LEN] = identity_map::<KEY_LEN>();
262}
263
264impl<const KEY_LEN: usize> KeySegmentation<KEY_LEN> for SingleSegmentation {
265 const SEGMENTS: [usize; KEY_LEN] = [0; KEY_LEN];
266}
267
268#[allow(dead_code)]
269#[derive(Debug, PartialEq, Copy, Clone)]
270#[repr(u8)]
271pub(crate) enum HeadTag {
272 Branch2 = 1,
274 Branch4 = 2,
275 Branch8 = 3,
276 Branch16 = 4,
277 Branch32 = 5,
278 Branch64 = 6,
279 Branch128 = 7,
280 Branch256 = 8,
281 Leaf = 16,
283}
284
285pub(crate) enum BodyPtr<const KEY_LEN: usize, O: KeySchema<KEY_LEN>, V> {
286 Leaf(NonNull<Leaf<KEY_LEN, V>>),
287 Branch(branch::BranchNN<KEY_LEN, O, V>),
288}
289
290pub(crate) enum BodyRef<'a, const KEY_LEN: usize, O: KeySchema<KEY_LEN>, V> {
293 Leaf(&'a Leaf<KEY_LEN, V>),
294 Branch(&'a Branch<KEY_LEN, O, [Option<Head<KEY_LEN, O, V>>], V>),
295}
296
297pub(crate) enum BodyMut<'a, const KEY_LEN: usize, O: KeySchema<KEY_LEN>, V> {
300 Leaf(&'a mut Leaf<KEY_LEN, V>),
301 Branch(&'a mut Branch<KEY_LEN, O, [Option<Head<KEY_LEN, O, V>>], V>),
302}
303
304pub(crate) trait Body {
305 fn tag(body: NonNull<Self>) -> HeadTag;
306}
307
308#[repr(C)]
309pub(crate) struct Head<const KEY_LEN: usize, O: KeySchema<KEY_LEN>, V> {
310 tptr: std::ptr::NonNull<u8>,
311 key_ordering: PhantomData<O>,
312 key_segments: PhantomData<O::Segmentation>,
313 value: PhantomData<V>,
314}
315
316unsafe impl<const KEY_LEN: usize, O: KeySchema<KEY_LEN>, V> Send for Head<KEY_LEN, O, V> {}
317unsafe impl<const KEY_LEN: usize, O: KeySchema<KEY_LEN>, V> Sync for Head<KEY_LEN, O, V> {}
318
319impl<const KEY_LEN: usize, O: KeySchema<KEY_LEN>, V> Head<KEY_LEN, O, V> {
320 pub(crate) fn new<T: Body + ?Sized>(key: u8, body: NonNull<T>) -> Self {
321 unsafe {
322 let tptr =
323 std::ptr::NonNull::new_unchecked((body.as_ptr() as *mut u8).map_addr(|addr| {
324 ((addr as u64 & 0x00_00_ff_ff_ff_ff_ff_ffu64)
325 | ((key as u64) << 48)
326 | ((<T as Body>::tag(body) as u64) << 56)) as usize
327 }));
328 Self {
329 tptr,
330 key_ordering: PhantomData,
331 key_segments: PhantomData,
332 value: PhantomData,
333 }
334 }
335 }
336
337 #[inline]
338 pub(crate) fn tag(&self) -> HeadTag {
339 unsafe { transmute((self.tptr.as_ptr() as u64 >> 56) as u8) }
340 }
341
342 #[inline]
343 pub(crate) fn key(&self) -> u8 {
344 (self.tptr.as_ptr() as u64 >> 48) as u8
345 }
346
347 #[inline]
348 pub(crate) fn with_key(mut self, key: u8) -> Self {
349 self.tptr = std::ptr::NonNull::new(self.tptr.as_ptr().map_addr(|addr| {
350 ((addr as u64 & 0xff_00_ff_ff_ff_ff_ff_ffu64) | ((key as u64) << 48)) as usize
351 }))
352 .unwrap();
353 self
354 }
355
356 #[inline]
357 pub(crate) fn set_body<T: Body + ?Sized>(&mut self, body: NonNull<T>) {
358 unsafe {
359 self.tptr = NonNull::new_unchecked((body.as_ptr() as *mut u8).map_addr(|addr| {
360 ((addr as u64 & 0x00_00_ff_ff_ff_ff_ff_ffu64)
361 | (self.tptr.as_ptr() as u64 & 0x00_ff_00_00_00_00_00_00u64)
362 | ((<T as Body>::tag(body) as u64) << 56)) as usize
363 }))
364 }
365 }
366
367 pub(crate) fn with_start(self, new_start_depth: usize) -> Head<KEY_LEN, O, V> {
368 let leaf_key = self.childleaf_key();
369 let i = O::TREE_TO_KEY[new_start_depth];
370 let key = leaf_key[i];
371 self.with_key(key)
372 }
373
374 pub(crate) fn body(&self) -> BodyPtr<KEY_LEN, O, V> {
380 unsafe {
381 let ptr = NonNull::new_unchecked(
382 self.tptr
383 .as_ptr()
384 .map_addr(|addr| ((((addr as u64) << 16) as i64) >> 16) as usize),
385 );
386 match self.tag() {
387 HeadTag::Leaf => BodyPtr::Leaf(ptr.cast()),
388 branch_tag => {
389 let count = 1 << (branch_tag as usize);
390 BodyPtr::Branch(NonNull::new_unchecked(std::ptr::slice_from_raw_parts(
391 ptr.as_ptr(),
392 count,
393 )
394 as *mut Branch<KEY_LEN, O, [Option<Head<KEY_LEN, O, V>>], V>))
395 }
396 }
397 }
398 }
399
400 pub(crate) fn body_mut(&mut self) -> BodyMut<'_, KEY_LEN, O, V> {
401 unsafe {
402 match self.body() {
403 BodyPtr::Leaf(mut leaf) => BodyMut::Leaf(leaf.as_mut()),
404 BodyPtr::Branch(mut branch) => {
405 let mut branch_nn = branch;
407 if Branch::rc_cow(&mut branch_nn).is_some() {
408 self.set_body(branch_nn);
409 BodyMut::Branch(branch_nn.as_mut())
410 } else {
411 BodyMut::Branch(branch.as_mut())
412 }
413 }
414 }
415 }
416 }
417
418 pub(crate) fn body_ref(&self) -> BodyRef<'_, KEY_LEN, O, V> {
420 match self.body() {
421 BodyPtr::Leaf(nn) => BodyRef::Leaf(unsafe { nn.as_ref() }),
422 BodyPtr::Branch(nn) => BodyRef::Branch(unsafe { nn.as_ref() }),
423 }
424 }
425
426 pub(crate) fn count(&self) -> u64 {
427 match self.body_ref() {
428 BodyRef::Leaf(_) => 1,
429 BodyRef::Branch(branch) => branch.leaf_count,
430 }
431 }
432
433 pub(crate) fn count_segment(&self, at_depth: usize) -> u64 {
434 match self.body_ref() {
435 BodyRef::Leaf(_) => 1,
436 BodyRef::Branch(branch) => branch.count_segment(at_depth),
437 }
438 }
439
440 pub(crate) fn hash(&self) -> u128 {
441 match self.body_ref() {
442 BodyRef::Leaf(leaf) => leaf.hash,
443 BodyRef::Branch(branch) => branch.hash,
444 }
445 }
446
447 pub(crate) fn end_depth(&self) -> usize {
448 match self.body_ref() {
449 BodyRef::Leaf(_) => KEY_LEN,
450 BodyRef::Branch(branch) => branch.end_depth as usize,
451 }
452 }
453
454 pub(crate) fn childleaf_ptr(&self) -> *const Leaf<KEY_LEN, V> {
459 match self.body_ref() {
460 BodyRef::Leaf(leaf) => leaf as *const Leaf<KEY_LEN, V>,
461 BodyRef::Branch(branch) => branch.childleaf_ptr(),
462 }
463 }
464
465 pub(crate) fn childleaf_key(&self) -> &[u8; KEY_LEN] {
466 match self.body_ref() {
467 BodyRef::Leaf(leaf) => &leaf.key,
468 BodyRef::Branch(branch) => &branch.childleaf().key,
469 }
470 }
471
472 pub(crate) fn first_divergence(
481 &self,
482 other: &Self,
483 start_depth: usize,
484 ) -> Option<(usize, u8, u8)> {
485 let limit = std::cmp::min(std::cmp::min(self.end_depth(), other.end_depth()), KEY_LEN);
486 debug_assert!(limit <= KEY_LEN);
487 let this_key = self.childleaf_key();
488 let other_key = other.childleaf_key();
489 let mut depth = start_depth;
490 while depth < limit {
491 let i = O::TREE_TO_KEY[depth];
492 let a = this_key[i];
493 let b = other_key[i];
494 if a != b {
495 return Some((depth, a, b));
496 }
497 depth += 1;
498 }
499 None
500 }
501
502 pub(crate) fn remove_leaf(
516 slot: &mut Option<Self>,
517 leaf_key: &[u8; KEY_LEN],
518 start_depth: usize,
519 ) {
520 if let Some(this) = slot {
521 let end_depth = std::cmp::min(this.end_depth(), KEY_LEN);
522 if !this.has_prefix::<KEY_LEN>(start_depth, leaf_key) {
526 return;
527 }
528 if this.tag() == HeadTag::Leaf {
529 slot.take();
530 } else {
531 let mut ed = crate::patch::branch::BranchMut::from_head(this);
532 let key = leaf_key[end_depth];
533 ed.modify_child(key, |mut opt| {
534 Self::remove_leaf(&mut opt, leaf_key, end_depth);
535 opt
536 });
537
538 if ed.leaf_count == 1 {
544 let mut remaining: Option<Head<KEY_LEN, O, V>> = None;
545 for slot_child in &mut ed.child_table {
546 if let Some(child) = slot_child.take() {
547 remaining = Some(child.with_start(start_depth));
548 break;
549 }
550 }
551 drop(ed);
552 if let Some(child) = remaining {
553 slot.replace(child);
554 }
555 } else {
556 drop(ed);
559 }
560 }
561 }
562 }
563
564 pub(crate) fn insert_leaf(mut this: Self, leaf: Self, start_depth: usize) -> Self {
574 if let Some((depth, this_byte_key, leaf_byte_key)) =
575 this.first_divergence(&leaf, start_depth)
576 {
577 let old_key = this.key();
578 let new_body = Branch::new(
579 depth,
580 this.with_key(this_byte_key),
581 leaf.with_key(leaf_byte_key),
582 );
583 return Head::new(old_key, new_body);
584 }
585
586 let end_depth = this.end_depth();
587 if end_depth != KEY_LEN {
588 let mut ed = crate::patch::branch::BranchMut::from_head(&mut this);
591 let inserted = leaf.with_start(ed.end_depth as usize);
592 let key = inserted.key();
593 ed.modify_child(key, |opt| match opt {
594 Some(old) => Some(Head::insert_leaf(old, inserted, end_depth)),
595 None => Some(inserted),
596 });
597 }
598 this
599 }
600
601 pub(crate) fn replace_leaf(mut this: Self, leaf: Self, start_depth: usize) -> Self {
602 if let Some((depth, this_byte_key, leaf_byte_key)) =
603 this.first_divergence(&leaf, start_depth)
604 {
605 let old_key = this.key();
606 let new_body = Branch::new(
607 depth,
608 this.with_key(this_byte_key),
609 leaf.with_key(leaf_byte_key),
610 );
611
612 return Head::new(old_key, new_body);
613 }
614
615 let end_depth = this.end_depth();
616 if end_depth == KEY_LEN {
617 let old_key = this.key();
618 return leaf.with_key(old_key);
619 } else {
620 let mut ed = crate::patch::branch::BranchMut::from_head(&mut this);
622 let inserted = leaf.with_start(ed.end_depth as usize);
623 let key = inserted.key();
624 ed.modify_child(key, |opt| match opt {
625 Some(old) => Some(Head::replace_leaf(old, inserted, end_depth)),
626 None => Some(inserted),
627 });
628 }
629 this
630 }
631
632 pub(crate) fn union(mut this: Self, mut other: Self, at_depth: usize) -> Self {
633 if this.hash() == other.hash() {
634 return this;
635 }
636
637 if let Some((depth, this_byte_key, other_byte_key)) =
638 this.first_divergence(&other, at_depth)
639 {
640 let old_key = this.key();
641 let new_body = Branch::new(
642 depth,
643 this.with_key(this_byte_key),
644 other.with_key(other_byte_key),
645 );
646
647 return Head::new(old_key, new_body);
648 }
649
650 let this_depth = this.end_depth();
651 let other_depth = other.end_depth();
652 if this_depth < other_depth {
653 let mut ed = crate::patch::branch::BranchMut::from_head(&mut this);
655 let inserted = other.with_start(ed.end_depth as usize);
656 let key = inserted.key();
657 ed.modify_child(key, |opt| match opt {
658 Some(old) => Some(Head::union(old, inserted, this_depth)),
659 None => Some(inserted),
660 });
661
662 drop(ed);
663 return this;
664 }
665
666 if other_depth < this_depth {
667 let old_key = this.key();
668 let this_head = this;
669 let mut ed = crate::patch::branch::BranchMut::from_head(&mut other);
670 let inserted = this_head.with_start(ed.end_depth as usize);
671 let key = inserted.key();
672 ed.modify_child(key, |opt| match opt {
673 Some(old) => Some(Head::union(old, inserted, other_depth)),
674 None => Some(inserted),
675 });
676 drop(ed);
677
678 return other.with_key(old_key);
679 }
680
681 let BodyMut::Branch(other_branch_ref) = other.body_mut() else {
683 unreachable!();
684 };
685 {
686 let mut ed = crate::patch::branch::BranchMut::from_head(&mut this);
691 for other_child in other_branch_ref
692 .child_table
693 .iter_mut()
694 .filter_map(Option::take)
695 {
696 let inserted = other_child.with_start(ed.end_depth as usize);
697 let key = inserted.key();
698 ed.modify_child(key, |opt| match opt {
699 Some(old) => Some(Head::union(old, inserted, this_depth)),
700 None => Some(inserted),
701 });
702 }
703 }
704 this
705 }
706
707 pub(crate) fn infixes<const PREFIX_LEN: usize, const INFIX_LEN: usize, F>(
708 &self,
709 prefix: &[u8; PREFIX_LEN],
710 at_depth: usize,
711 f: &mut F,
712 ) where
713 F: FnMut(&[u8; INFIX_LEN]),
714 {
715 match self.body_ref() {
716 BodyRef::Leaf(leaf) => leaf.infixes::<PREFIX_LEN, INFIX_LEN, O, F>(prefix, at_depth, f),
717 BodyRef::Branch(branch) => {
718 branch.infixes::<PREFIX_LEN, INFIX_LEN, F>(prefix, at_depth, f)
719 }
720 }
721 }
722
723 pub(crate) fn has_prefix<const PREFIX_LEN: usize>(
724 &self,
725 at_depth: usize,
726 prefix: &[u8; PREFIX_LEN],
727 ) -> bool {
728 const {
729 assert!(PREFIX_LEN <= KEY_LEN);
730 }
731 match self.body_ref() {
732 BodyRef::Leaf(leaf) => leaf.has_prefix::<O>(at_depth, prefix),
733 BodyRef::Branch(branch) => branch.has_prefix::<PREFIX_LEN>(at_depth, prefix),
734 }
735 }
736
737 pub(crate) fn get<'a>(&'a self, at_depth: usize, key: &[u8; KEY_LEN]) -> Option<&'a V>
738 where
739 O: 'a,
740 {
741 match self.body_ref() {
742 BodyRef::Leaf(leaf) => leaf.get::<O>(at_depth, key),
743 BodyRef::Branch(branch) => branch.get(at_depth, key),
744 }
745 }
746
747 pub(crate) fn segmented_len<const PREFIX_LEN: usize>(
748 &self,
749 at_depth: usize,
750 prefix: &[u8; PREFIX_LEN],
751 ) -> u64 {
752 match self.body_ref() {
753 BodyRef::Leaf(leaf) => leaf.segmented_len::<O, PREFIX_LEN>(at_depth, prefix),
754 BodyRef::Branch(branch) => branch.segmented_len::<PREFIX_LEN>(at_depth, prefix),
755 }
756 }
757
758 pub(crate) fn intersect(&self, other: &Self, at_depth: usize) -> Option<Self> {
762 if self.hash() == other.hash() {
763 return Some(self.clone());
764 }
765
766 if self.first_divergence(other, at_depth).is_some() {
767 return None;
768 }
769
770 let self_depth = self.end_depth();
771 let other_depth = other.end_depth();
772 if self_depth < other_depth {
773 let BodyRef::Branch(branch) = self.body_ref() else {
776 unreachable!();
777 };
778 return branch
779 .child_table
780 .table_get(other.childleaf_key()[O::TREE_TO_KEY[self_depth]])
781 .and_then(|self_child| other.intersect(self_child, self_depth));
782 }
783
784 if other_depth < self_depth {
785 let BodyRef::Branch(other_branch) = other.body_ref() else {
789 unreachable!();
790 };
791 return other_branch
792 .child_table
793 .table_get(self.childleaf_key()[O::TREE_TO_KEY[other_depth]])
794 .and_then(|other_child| self.intersect(other_child, other_depth));
795 }
796
797 let BodyRef::Branch(self_branch) = self.body_ref() else {
803 unreachable!();
804 };
805 let BodyRef::Branch(other_branch) = other.body_ref() else {
806 unreachable!();
807 };
808
809 let mut intersected_children = self_branch
810 .child_table
811 .iter()
812 .filter_map(Option::as_ref)
813 .filter_map(|self_child| {
814 let other_child = other_branch.child_table.table_get(self_child.key())?;
815 self_child.intersect(other_child, self_depth)
816 });
817 let first_child = intersected_children.next()?;
818 let Some(second_child) = intersected_children.next() else {
819 return Some(first_child);
820 };
821 let new_branch = Branch::new(
822 self_depth,
823 first_child.with_start(self_depth),
824 second_child.with_start(self_depth),
825 );
826 let mut head_for_branch = Head::new(0, new_branch);
831 {
832 let mut ed = crate::patch::branch::BranchMut::from_head(&mut head_for_branch);
833 for child in intersected_children {
834 let inserted = child.with_start(self_depth);
835 let k = inserted.key();
836 ed.modify_child(k, |_opt| Some(inserted));
837 }
838 }
840 Some(head_for_branch)
841 }
842
843 pub(crate) fn difference(&self, other: &Self, at_depth: usize) -> Option<Self> {
847 if self.hash() == other.hash() {
848 return None;
849 }
850
851 if self.first_divergence(other, at_depth).is_some() {
852 return Some(self.clone());
853 }
854
855 let self_depth = self.end_depth();
856 let other_depth = other.end_depth();
857 if self_depth < other_depth {
858 let mut new_branch = self.clone();
865 let other_byte_key = other.childleaf_key()[O::TREE_TO_KEY[self_depth]];
866 {
867 let mut ed = crate::patch::branch::BranchMut::from_head(&mut new_branch);
868 ed.modify_child(other_byte_key, |opt| {
869 opt.and_then(|child| child.difference(other, self_depth))
870 });
871 }
872 return Some(new_branch);
873 }
874
875 if other_depth < self_depth {
876 let BodyRef::Branch(other_branch) = other.body_ref() else {
883 unreachable!();
884 };
885 let self_byte_key = self.childleaf_key()[O::TREE_TO_KEY[other_depth]];
886 if let Some(other_child) = other_branch.child_table.table_get(self_byte_key) {
887 return self.difference(other_child, at_depth);
888 } else {
889 return Some(self.clone());
890 }
891 }
892
893 let BodyRef::Branch(self_branch) = self.body_ref() else {
899 unreachable!();
900 };
901 let BodyRef::Branch(other_branch) = other.body_ref() else {
902 unreachable!();
903 };
904
905 let mut differenced_children = self_branch
906 .child_table
907 .iter()
908 .filter_map(Option::as_ref)
909 .filter_map(|self_child| {
910 if let Some(other_child) = other_branch.child_table.table_get(self_child.key()) {
911 self_child.difference(other_child, self_depth)
912 } else {
913 Some(self_child.clone())
914 }
915 });
916
917 let first_child = differenced_children.next()?;
918 let second_child = match differenced_children.next() {
919 Some(sc) => sc,
920 None => return Some(first_child),
921 };
922
923 let new_branch = Branch::new(
924 self_depth,
925 first_child.with_start(self_depth),
926 second_child.with_start(self_depth),
927 );
928 let mut head_for_branch = Head::new(0, new_branch);
929 {
930 let mut ed = crate::patch::branch::BranchMut::from_head(&mut head_for_branch);
931 for child in differenced_children {
932 let inserted = child.with_start(self_depth);
933 let k = inserted.key();
934 ed.modify_child(k, |_opt| Some(inserted));
935 }
936 }
938 Some(head_for_branch)
942 }
943}
944
945unsafe impl<const KEY_LEN: usize, O: KeySchema<KEY_LEN>, V> ByteEntry for Head<KEY_LEN, O, V> {
946 fn key(&self) -> u8 {
947 self.key()
948 }
949}
950
951impl<const KEY_LEN: usize, O: KeySchema<KEY_LEN>, V> fmt::Debug for Head<KEY_LEN, O, V> {
952 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
953 self.tag().fmt(f)
954 }
955}
956
957impl<const KEY_LEN: usize, O: KeySchema<KEY_LEN>, V> Clone for Head<KEY_LEN, O, V> {
958 fn clone(&self) -> Self {
959 unsafe {
960 match self.body() {
961 BodyPtr::Leaf(leaf) => Self::new(self.key(), Leaf::rc_inc(leaf)),
962 BodyPtr::Branch(branch) => Self::new(self.key(), Branch::rc_inc(branch)),
963 }
964 }
965 }
966}
967
968impl<const KEY_LEN: usize, O: KeySchema<KEY_LEN>, V> Drop for Head<KEY_LEN, O, V> {
973 fn drop(&mut self) {
974 unsafe {
975 match self.body() {
976 BodyPtr::Leaf(leaf) => Leaf::rc_dec(leaf),
977 BodyPtr::Branch(branch) => Branch::rc_dec(branch),
978 }
979 }
980 }
981}
982
983#[derive(Debug)]
999pub struct PATCH<const KEY_LEN: usize, O = IdentitySchema, V = ()>
1000where
1001 O: KeySchema<KEY_LEN>,
1002{
1003 root: Option<Head<KEY_LEN, O, V>>,
1004}
1005
1006impl<const KEY_LEN: usize, O, V> Clone for PATCH<KEY_LEN, O, V>
1007where
1008 O: KeySchema<KEY_LEN>,
1009{
1010 fn clone(&self) -> Self {
1011 Self {
1012 root: self.root.clone(),
1013 }
1014 }
1015}
1016
1017impl<const KEY_LEN: usize, O, V> Default for PATCH<KEY_LEN, O, V>
1018where
1019 O: KeySchema<KEY_LEN>,
1020{
1021 fn default() -> Self {
1022 Self::new()
1023 }
1024}
1025
1026impl<const KEY_LEN: usize, O, V> PATCH<KEY_LEN, O, V>
1027where
1028 O: KeySchema<KEY_LEN>,
1029{
1030 pub fn new() -> Self {
1032 init_sip_key();
1033 PATCH { root: None }
1034 }
1035
1036 pub fn insert(&mut self, entry: &Entry<KEY_LEN, V>) {
1043 if self.root.is_some() {
1044 let this = self.root.take().expect("root should not be empty");
1045 let new_head = Head::insert_leaf(this, entry.leaf(), 0);
1046 self.root.replace(new_head);
1047 } else {
1048 self.root.replace(entry.leaf());
1049 }
1050 }
1051
1052 pub fn replace(&mut self, entry: &Entry<KEY_LEN, V>) {
1054 if self.root.is_some() {
1055 let this = self.root.take().expect("root should not be empty");
1056 let new_head = Head::replace_leaf(this, entry.leaf(), 0);
1057 self.root.replace(new_head);
1058 } else {
1059 self.root.replace(entry.leaf());
1060 }
1061 }
1062
1063 pub fn remove(&mut self, key: &[u8; KEY_LEN]) {
1067 Head::remove_leaf(&mut self.root, key, 0);
1068 }
1069
1070 pub fn len(&self) -> u64 {
1072 if let Some(root) = &self.root {
1073 root.count()
1074 } else {
1075 0
1076 }
1077 }
1078
1079 pub fn is_empty(&self) -> bool {
1081 self.len() == 0
1082 }
1083
1084 pub fn get(&self, key: &[u8; KEY_LEN]) -> Option<&V> {
1086 self.root.as_ref().and_then(|root| root.get(0, key))
1087 }
1088
1089 pub fn infixes<const PREFIX_LEN: usize, const INFIX_LEN: usize, F>(
1103 &self,
1104 prefix: &[u8; PREFIX_LEN],
1105 mut for_each: F,
1106 ) where
1107 F: FnMut(&[u8; INFIX_LEN]),
1108 {
1109 const {
1110 assert!(PREFIX_LEN + INFIX_LEN <= KEY_LEN);
1111 }
1112 assert!(
1113 O::same_segment_tree(PREFIX_LEN, PREFIX_LEN + INFIX_LEN - 1)
1114 && (PREFIX_LEN + INFIX_LEN == KEY_LEN
1115 || !O::same_segment_tree(PREFIX_LEN + INFIX_LEN - 1, PREFIX_LEN + INFIX_LEN)),
1116 "INFIX_LEN must cover a whole segment"
1117 );
1118 if let Some(root) = &self.root {
1119 root.infixes(prefix, 0, &mut for_each);
1120 }
1121 }
1122
1123 pub fn has_prefix<const PREFIX_LEN: usize>(&self, prefix: &[u8; PREFIX_LEN]) -> bool {
1128 const {
1129 assert!(PREFIX_LEN <= KEY_LEN);
1130 }
1131 if let Some(root) = &self.root {
1132 root.has_prefix(0, prefix)
1133 } else {
1134 PREFIX_LEN == 0
1135 }
1136 }
1137
1138 pub fn segmented_len<const PREFIX_LEN: usize>(&self, prefix: &[u8; PREFIX_LEN]) -> u64 {
1140 const {
1141 assert!(PREFIX_LEN <= KEY_LEN);
1142 if PREFIX_LEN > 0 && PREFIX_LEN < KEY_LEN {
1143 assert!(
1144 <O as KeySchema<KEY_LEN>>::Segmentation::SEGMENTS
1145 [O::TREE_TO_KEY[PREFIX_LEN - 1]]
1146 != <O as KeySchema<KEY_LEN>>::Segmentation::SEGMENTS
1147 [O::TREE_TO_KEY[PREFIX_LEN]],
1148 "PREFIX_LEN must align to segment boundary",
1149 );
1150 }
1151 }
1152 if let Some(root) = &self.root {
1153 root.segmented_len(0, prefix)
1154 } else {
1155 0
1156 }
1157 }
1158
1159 pub fn iter<'a>(&'a self) -> PATCHIterator<'a, KEY_LEN, O, V> {
1162 PATCHIterator::new(self)
1163 }
1164
1165 pub fn iter_ordered<'a>(&'a self) -> PATCHOrderedIterator<'a, KEY_LEN, O, V> {
1171 PATCHOrderedIterator::new(self)
1172 }
1173
1174 pub fn iter_prefix_count<'a, const PREFIX_LEN: usize>(
1178 &'a self,
1179 ) -> PATCHPrefixIterator<'a, KEY_LEN, PREFIX_LEN, O, V> {
1180 PATCHPrefixIterator::new(self)
1181 }
1182
1183 pub fn union(&mut self, other: Self) {
1187 if let Some(other) = other.root {
1188 if self.root.is_some() {
1189 let this = self.root.take().expect("root should not be empty");
1190 let merged = Head::union(this, other, 0);
1191 self.root.replace(merged);
1192 } else {
1193 self.root.replace(other);
1194 }
1195 }
1196 }
1197
1198 pub fn intersect(&self, other: &Self) -> Self {
1202 if let Some(root) = &self.root {
1203 if let Some(other_root) = &other.root {
1204 return Self {
1205 root: root.intersect(other_root, 0).map(|root| root.with_start(0)),
1206 };
1207 }
1208 }
1209 Self::new()
1210 }
1211
1212 pub fn difference(&self, other: &Self) -> Self {
1217 if let Some(root) = &self.root {
1218 if let Some(other_root) = &other.root {
1219 Self {
1220 root: root.difference(other_root, 0),
1221 }
1222 } else {
1223 (*self).clone()
1224 }
1225 } else {
1226 (*other).clone()
1227 }
1228 }
1229
1230 pub fn debug_branch_fill(&self) -> [f32; 8] {
1235 let mut counts = [0u64; 8];
1236 let mut used = [0u64; 8];
1237
1238 if let Some(root) = &self.root {
1239 let mut stack = Vec::new();
1240 stack.push(root);
1241
1242 while let Some(head) = stack.pop() {
1243 match head.body_ref() {
1244 BodyRef::Leaf(_) => {}
1245 BodyRef::Branch(b) => {
1246 let size = b.child_table.len();
1247 let idx = size.trailing_zeros() as usize - 1;
1248 counts[idx] += 1;
1249 used[idx] += b.child_table.iter().filter(|c| c.is_some()).count() as u64;
1250 for child in b.child_table.iter().filter_map(|c| c.as_ref()) {
1251 stack.push(child);
1252 }
1253 }
1254 }
1255 }
1256 }
1257
1258 let mut avg = [0f32; 8];
1259 for i in 0..8 {
1260 if counts[i] > 0 {
1261 let size = 1u64 << (i + 1);
1262 avg[i] = used[i] as f32 / (counts[i] as f32 * size as f32);
1263 }
1264 }
1265 avg
1266 }
1267}
1268
1269impl<const KEY_LEN: usize, O, V> PartialEq for PATCH<KEY_LEN, O, V>
1270where
1271 O: KeySchema<KEY_LEN>,
1272{
1273 fn eq(&self, other: &Self) -> bool {
1274 self.root.as_ref().map(|root| root.hash()) == other.root.as_ref().map(|root| root.hash())
1275 }
1276}
1277
1278impl<const KEY_LEN: usize, O, V> Eq for PATCH<KEY_LEN, O, V> where O: KeySchema<KEY_LEN> {}
1279
1280impl<'a, const KEY_LEN: usize, O, V> IntoIterator for &'a PATCH<KEY_LEN, O, V>
1281where
1282 O: KeySchema<KEY_LEN>,
1283{
1284 type Item = &'a [u8; KEY_LEN];
1285 type IntoIter = PATCHIterator<'a, KEY_LEN, O, V>;
1286
1287 fn into_iter(self) -> Self::IntoIter {
1288 PATCHIterator::new(self)
1289 }
1290}
1291
1292pub struct PATCHIterator<'a, const KEY_LEN: usize, O: KeySchema<KEY_LEN>, V> {
1295 stack: ArrayVec<std::slice::Iter<'a, Option<Head<KEY_LEN, O, V>>>, KEY_LEN>,
1296 remaining: usize,
1297}
1298
1299impl<'a, const KEY_LEN: usize, O: KeySchema<KEY_LEN>, V> PATCHIterator<'a, KEY_LEN, O, V> {
1300 pub fn new(patch: &'a PATCH<KEY_LEN, O, V>) -> Self {
1301 let mut r = PATCHIterator {
1302 stack: ArrayVec::new(),
1303 remaining: patch.len().min(usize::MAX as u64) as usize,
1304 };
1305 r.stack.push(std::slice::from_ref(&patch.root).iter());
1306 r
1307 }
1308}
1309
1310impl<'a, const KEY_LEN: usize, O: KeySchema<KEY_LEN>, V> Iterator
1311 for PATCHIterator<'a, KEY_LEN, O, V>
1312{
1313 type Item = &'a [u8; KEY_LEN];
1314
1315 fn next(&mut self) -> Option<Self::Item> {
1316 let mut iter = self.stack.last_mut()?;
1317 loop {
1318 if let Some(child) = iter.next() {
1319 if let Some(child) = child {
1320 match child.body_ref() {
1321 BodyRef::Leaf(_) => {
1322 self.remaining = self.remaining.saturating_sub(1);
1323 return Some(child.childleaf_key());
1325 }
1326 BodyRef::Branch(branch) => {
1327 self.stack.push(branch.child_table.iter());
1328 iter = self.stack.last_mut()?;
1329 }
1330 }
1331 }
1332 } else {
1333 self.stack.pop();
1334 iter = self.stack.last_mut()?;
1335 }
1336 }
1337 }
1338
1339 fn size_hint(&self) -> (usize, Option<usize>) {
1340 (self.remaining, Some(self.remaining))
1341 }
1342}
1343
1344impl<'a, const KEY_LEN: usize, O: KeySchema<KEY_LEN>, V> ExactSizeIterator
1345 for PATCHIterator<'a, KEY_LEN, O, V>
1346{
1347}
1348
1349impl<'a, const KEY_LEN: usize, O: KeySchema<KEY_LEN>, V> std::iter::FusedIterator
1350 for PATCHIterator<'a, KEY_LEN, O, V>
1351{
1352}
1353
1354pub struct PATCHOrderedIterator<'a, const KEY_LEN: usize, O: KeySchema<KEY_LEN>, V> {
1361 stack: Vec<ArrayVec<&'a Head<KEY_LEN, O, V>, 256>>,
1362 remaining: usize,
1363}
1364
1365impl<'a, const KEY_LEN: usize, O: KeySchema<KEY_LEN>, V> PATCHOrderedIterator<'a, KEY_LEN, O, V> {
1366 pub fn new(patch: &'a PATCH<KEY_LEN, O, V>) -> Self {
1367 let mut r = PATCHOrderedIterator {
1368 stack: Vec::with_capacity(KEY_LEN),
1369 remaining: patch.len().min(usize::MAX as u64) as usize,
1370 };
1371 if let Some(root) = &patch.root {
1372 r.stack.push(ArrayVec::new());
1373 match root.body_ref() {
1374 BodyRef::Leaf(_) => {
1375 r.stack[0].push(root);
1376 }
1377 BodyRef::Branch(branch) => {
1378 let first_level = &mut r.stack[0];
1379 first_level.extend(branch.child_table.iter().filter_map(|c| c.as_ref()));
1380 first_level.sort_unstable_by_key(|&k| Reverse(k.key())); }
1382 }
1383 }
1384 r
1385 }
1386}
1387
1388pub struct PATCHIntoIterator<const KEY_LEN: usize, O: KeySchema<KEY_LEN>, V> {
1393 queue: Vec<Head<KEY_LEN, O, V>>,
1394 remaining: usize,
1395}
1396
1397impl<const KEY_LEN: usize, O: KeySchema<KEY_LEN>, V> PATCHIntoIterator<KEY_LEN, O, V> {}
1398
1399impl<const KEY_LEN: usize, O: KeySchema<KEY_LEN>, V> Iterator for PATCHIntoIterator<KEY_LEN, O, V> {
1400 type Item = [u8; KEY_LEN];
1401
1402 fn next(&mut self) -> Option<Self::Item> {
1403 let q = &mut self.queue;
1404 while let Some(mut head) = q.pop() {
1405 match head.body_mut() {
1410 BodyMut::Leaf(leaf) => {
1411 self.remaining = self.remaining.saturating_sub(1);
1412 return Some(leaf.key);
1413 }
1414 BodyMut::Branch(branch) => {
1415 for slot in branch.child_table.iter_mut().rev() {
1416 if let Some(c) = slot.take() {
1417 q.push(c);
1418 }
1419 }
1420 }
1421 }
1422 }
1423 None
1424 }
1425}
1426
1427pub struct PATCHIntoOrderedIterator<const KEY_LEN: usize, O: KeySchema<KEY_LEN>, V> {
1429 queue: Vec<Head<KEY_LEN, O, V>>,
1430 remaining: usize,
1431}
1432
1433impl<const KEY_LEN: usize, O: KeySchema<KEY_LEN>, V> Iterator
1434 for PATCHIntoOrderedIterator<KEY_LEN, O, V>
1435{
1436 type Item = [u8; KEY_LEN];
1437
1438 fn next(&mut self) -> Option<Self::Item> {
1439 let q = &mut self.queue;
1440 while let Some(mut head) = q.pop() {
1441 match head.body_mut() {
1445 BodyMut::Leaf(leaf) => {
1446 self.remaining = self.remaining.saturating_sub(1);
1447 return Some(leaf.key);
1448 }
1449 BodyMut::Branch(branch) => {
1450 let slice: &mut [Option<Head<KEY_LEN, O, V>>] = &mut branch.child_table;
1451 slice
1459 .sort_unstable_by_key(|opt| (opt.is_none(), opt.as_ref().map(|h| h.key())));
1460 for slot in slice.iter_mut().rev() {
1461 if let Some(c) = slot.take() {
1462 q.push(c);
1463 }
1464 }
1465 }
1466 }
1467 }
1468 None
1469 }
1470}
1471
1472impl<const KEY_LEN: usize, O: KeySchema<KEY_LEN>, V> IntoIterator for PATCH<KEY_LEN, O, V> {
1473 type Item = [u8; KEY_LEN];
1474 type IntoIter = PATCHIntoIterator<KEY_LEN, O, V>;
1475
1476 fn into_iter(self) -> Self::IntoIter {
1477 let remaining = self.len().min(usize::MAX as u64) as usize;
1478 let mut q = Vec::new();
1479 if let Some(root) = self.root {
1480 q.push(root);
1481 }
1482 PATCHIntoIterator {
1483 queue: q,
1484 remaining,
1485 }
1486 }
1487}
1488
1489impl<const KEY_LEN: usize, O: KeySchema<KEY_LEN>, V> PATCH<KEY_LEN, O, V> {
1490 pub fn into_iter_ordered(self) -> PATCHIntoOrderedIterator<KEY_LEN, O, V> {
1492 let remaining = self.len().min(usize::MAX as u64) as usize;
1493 let mut q = Vec::new();
1494 if let Some(root) = self.root {
1495 q.push(root);
1496 }
1497 PATCHIntoOrderedIterator {
1498 queue: q,
1499 remaining,
1500 }
1501 }
1502}
1503
1504impl<'a, const KEY_LEN: usize, O: KeySchema<KEY_LEN>, V> Iterator
1505 for PATCHOrderedIterator<'a, KEY_LEN, O, V>
1506{
1507 type Item = &'a [u8; KEY_LEN];
1508
1509 fn next(&mut self) -> Option<Self::Item> {
1510 let mut level = self.stack.last_mut()?;
1511 loop {
1512 if let Some(child) = level.pop() {
1513 match child.body_ref() {
1514 BodyRef::Leaf(_) => {
1515 self.remaining = self.remaining.saturating_sub(1);
1516 return Some(child.childleaf_key());
1517 }
1518 BodyRef::Branch(branch) => {
1519 self.stack.push(ArrayVec::new());
1520 level = self.stack.last_mut()?;
1521 level.extend(branch.child_table.iter().filter_map(|c| c.as_ref()));
1522 level.sort_unstable_by_key(|&k| Reverse(k.key())); }
1524 }
1525 } else {
1526 self.stack.pop();
1527 level = self.stack.last_mut()?;
1528 }
1529 }
1530 }
1531
1532 fn size_hint(&self) -> (usize, Option<usize>) {
1533 (self.remaining, Some(self.remaining))
1534 }
1535}
1536
1537impl<'a, const KEY_LEN: usize, O: KeySchema<KEY_LEN>, V> ExactSizeIterator
1538 for PATCHOrderedIterator<'a, KEY_LEN, O, V>
1539{
1540}
1541
1542impl<'a, const KEY_LEN: usize, O: KeySchema<KEY_LEN>, V> std::iter::FusedIterator
1543 for PATCHOrderedIterator<'a, KEY_LEN, O, V>
1544{
1545}
1546
1547pub struct PATCHPrefixIterator<
1550 'a,
1551 const KEY_LEN: usize,
1552 const PREFIX_LEN: usize,
1553 O: KeySchema<KEY_LEN>,
1554 V,
1555> {
1556 stack: Vec<ArrayVec<&'a Head<KEY_LEN, O, V>, 256>>,
1557}
1558
1559impl<'a, const KEY_LEN: usize, const PREFIX_LEN: usize, O: KeySchema<KEY_LEN>, V>
1560 PATCHPrefixIterator<'a, KEY_LEN, PREFIX_LEN, O, V>
1561{
1562 fn new(patch: &'a PATCH<KEY_LEN, O, V>) -> Self {
1563 const {
1564 assert!(PREFIX_LEN <= KEY_LEN);
1565 }
1566 let mut r = PATCHPrefixIterator {
1567 stack: Vec::with_capacity(PREFIX_LEN),
1568 };
1569 if let Some(root) = &patch.root {
1570 r.stack.push(ArrayVec::new());
1571 if root.end_depth() >= PREFIX_LEN {
1572 r.stack[0].push(root);
1573 } else {
1574 let BodyRef::Branch(branch) = root.body_ref() else {
1575 unreachable!();
1576 };
1577 let first_level = &mut r.stack[0];
1578 first_level.extend(branch.child_table.iter().filter_map(|c| c.as_ref()));
1579 first_level.sort_unstable_by_key(|&k| Reverse(k.key())); }
1581 }
1582 r
1583 }
1584}
1585
1586impl<'a, const KEY_LEN: usize, const PREFIX_LEN: usize, O: KeySchema<KEY_LEN>, V> Iterator
1587 for PATCHPrefixIterator<'a, KEY_LEN, PREFIX_LEN, O, V>
1588{
1589 type Item = ([u8; PREFIX_LEN], u64);
1590
1591 fn next(&mut self) -> Option<Self::Item> {
1592 let mut level = self.stack.last_mut()?;
1593 loop {
1594 if let Some(child) = level.pop() {
1595 if child.end_depth() >= PREFIX_LEN {
1596 let key = O::tree_ordered(child.childleaf_key());
1597 let suffix_count = child.count();
1598 return Some((key[0..PREFIX_LEN].try_into().unwrap(), suffix_count));
1599 } else {
1600 let BodyRef::Branch(branch) = child.body_ref() else {
1601 unreachable!();
1602 };
1603 self.stack.push(ArrayVec::new());
1604 level = self.stack.last_mut()?;
1605 level.extend(branch.child_table.iter().filter_map(|c| c.as_ref()));
1606 level.sort_unstable_by_key(|&k| Reverse(k.key())); }
1608 } else {
1609 self.stack.pop();
1610 level = self.stack.last_mut()?;
1611 }
1612 }
1613 }
1614}
1615
1616#[cfg(test)]
1617mod tests {
1618 use super::*;
1619 use itertools::Itertools;
1620 use proptest::prelude::*;
1621 use std::collections::HashSet;
1622 use std::convert::TryInto;
1623 use std::iter::FromIterator;
1624 use std::mem;
1625
1626 #[test]
1627 fn head_tag() {
1628 let head = Head::<64, IdentitySchema, ()>::new::<Leaf<64, ()>>(0, NonNull::dangling());
1629 assert_eq!(head.tag(), HeadTag::Leaf);
1630 mem::forget(head);
1631 }
1632
1633 #[test]
1634 fn head_key() {
1635 for k in 0..=255 {
1636 let head = Head::<64, IdentitySchema, ()>::new::<Leaf<64, ()>>(k, NonNull::dangling());
1637 assert_eq!(head.key(), k);
1638 mem::forget(head);
1639 }
1640 }
1641
1642 #[test]
1643 fn head_size() {
1644 assert_eq!(mem::size_of::<Head<64, IdentitySchema, ()>>(), 8);
1645 }
1646
1647 #[test]
1648 fn empty_tree() {
1649 let _tree = PATCH::<64, IdentitySchema, ()>::new();
1650 }
1651
1652 #[test]
1653 fn tree_put_one() {
1654 const KEY_SIZE: usize = 64;
1655 let mut tree = PATCH::<KEY_SIZE, IdentitySchema, ()>::new();
1656 let entry = Entry::new(&[0; KEY_SIZE]);
1657 tree.insert(&entry);
1658 }
1659
1660 #[test]
1661 fn tree_put_same() {
1662 const KEY_SIZE: usize = 64;
1663 let mut tree = PATCH::<KEY_SIZE, IdentitySchema, ()>::new();
1664 let entry = Entry::new(&[0; KEY_SIZE]);
1665 tree.insert(&entry);
1666 tree.insert(&entry);
1667 }
1668
1669 #[test]
1670 fn tree_replace_existing() {
1671 const KEY_SIZE: usize = 64;
1672 let key = [1u8; KEY_SIZE];
1673 let mut tree = PATCH::<KEY_SIZE, IdentitySchema, u32>::new();
1674 let entry1 = Entry::with_value(&key, 1);
1675 tree.insert(&entry1);
1676 let entry2 = Entry::with_value(&key, 2);
1677 tree.replace(&entry2);
1678 assert_eq!(tree.get(&key), Some(&2));
1679 }
1680
1681 #[test]
1682 fn tree_replace_childleaf_updates_branch() {
1683 const KEY_SIZE: usize = 64;
1684 let key1 = [0u8; KEY_SIZE];
1685 let key2 = [1u8; KEY_SIZE];
1686 let mut tree = PATCH::<KEY_SIZE, IdentitySchema, u32>::new();
1687 let entry1 = Entry::with_value(&key1, 1);
1688 let entry2 = Entry::with_value(&key2, 2);
1689 tree.insert(&entry1);
1690 tree.insert(&entry2);
1691 let entry1b = Entry::with_value(&key1, 3);
1692 tree.replace(&entry1b);
1693 assert_eq!(tree.get(&key1), Some(&3));
1694 assert_eq!(tree.get(&key2), Some(&2));
1695 }
1696
1697 #[test]
1698 fn update_child_refreshes_childleaf_on_replace() {
1699 const KEY_SIZE: usize = 4;
1700 let mut tree = PATCH::<KEY_SIZE, IdentitySchema, u32>::new();
1701
1702 let key1 = [0u8; KEY_SIZE];
1703 let key2 = [1u8; KEY_SIZE];
1704 tree.insert(&Entry::with_value(&key1, 1));
1705 tree.insert(&Entry::with_value(&key2, 2));
1706
1707 let root_ref = tree.root.as_ref().expect("root exists");
1709 let before_childleaf = *root_ref.childleaf_key();
1710
1711 let slot_key = match root_ref.body_ref() {
1714 BodyRef::Branch(branch) => branch
1715 .child_table
1716 .iter()
1717 .filter_map(|c| c.as_ref())
1718 .find(|c| c.childleaf_key() == &before_childleaf)
1719 .expect("child exists")
1720 .key(),
1721 BodyRef::Leaf(_) => panic!("root should be a branch"),
1722 };
1723
1724 let new_key = [2u8; KEY_SIZE];
1726 {
1727 let mut ed = crate::patch::branch::BranchMut::from_slot(&mut tree.root);
1728 ed.modify_child(slot_key, |_| {
1729 Some(Entry::with_value(&new_key, 42).leaf::<IdentitySchema>())
1730 });
1731 }
1733
1734 let after = tree.root.as_ref().expect("root exists");
1735 assert_eq!(after.childleaf_key(), &new_key);
1736 }
1737
1738 #[test]
1739 fn remove_childleaf_updates_branch() {
1740 const KEY_SIZE: usize = 4;
1741 let mut tree = PATCH::<KEY_SIZE, IdentitySchema, u32>::new();
1742
1743 let key1 = [0u8; KEY_SIZE];
1744 let key2 = [1u8; KEY_SIZE];
1745 tree.insert(&Entry::with_value(&key1, 1));
1746 tree.insert(&Entry::with_value(&key2, 2));
1747
1748 let childleaf_before = *tree.root.as_ref().unwrap().childleaf_key();
1749 tree.remove(&childleaf_before);
1751
1752 let other = if childleaf_before == key1 { key2 } else { key1 };
1754 assert_eq!(tree.get(&childleaf_before), None);
1755 assert_eq!(tree.get(&other), Some(&2u32));
1756 let after_childleaf = tree.root.as_ref().unwrap().childleaf_key();
1757 assert_eq!(after_childleaf, &other);
1758 }
1759
1760 #[test]
1761 fn remove_collapses_branch_to_single_child() {
1762 const KEY_SIZE: usize = 4;
1763 let mut tree = PATCH::<KEY_SIZE, IdentitySchema, u32>::new();
1764
1765 let key1 = [0u8; KEY_SIZE];
1766 let key2 = [1u8; KEY_SIZE];
1767 tree.insert(&Entry::with_value(&key1, 1));
1768 tree.insert(&Entry::with_value(&key2, 2));
1769
1770 tree.remove(&key1);
1772 assert_eq!(tree.get(&key1), None);
1773 assert_eq!(tree.get(&key2), Some(&2u32));
1774 let root = tree.root.as_ref().expect("root exists");
1775 match root.body_ref() {
1776 BodyRef::Leaf(_) => {}
1777 BodyRef::Branch(_) => panic!("root should have collapsed to a leaf"),
1778 }
1779 }
1780
1781 #[test]
1782 fn branch_size() {
1783 assert_eq!(
1784 mem::size_of::<Branch<64, IdentitySchema, [Option<Head<64, IdentitySchema, ()>>; 2], ()>>(
1785 ),
1786 64
1787 );
1788 assert_eq!(
1789 mem::size_of::<Branch<64, IdentitySchema, [Option<Head<64, IdentitySchema, ()>>; 4], ()>>(
1790 ),
1791 48 + 16 * 2
1792 );
1793 assert_eq!(
1794 mem::size_of::<Branch<64, IdentitySchema, [Option<Head<64, IdentitySchema, ()>>; 8], ()>>(
1795 ),
1796 48 + 16 * 4
1797 );
1798 assert_eq!(
1799 mem::size_of::<
1800 Branch<64, IdentitySchema, [Option<Head<64, IdentitySchema, ()>>; 16], ()>,
1801 >(),
1802 48 + 16 * 8
1803 );
1804 assert_eq!(
1805 mem::size_of::<
1806 Branch<64, IdentitySchema, [Option<Head<32, IdentitySchema, ()>>; 32], ()>,
1807 >(),
1808 48 + 16 * 16
1809 );
1810 assert_eq!(
1811 mem::size_of::<
1812 Branch<64, IdentitySchema, [Option<Head<64, IdentitySchema, ()>>; 64], ()>,
1813 >(),
1814 48 + 16 * 32
1815 );
1816 assert_eq!(
1817 mem::size_of::<
1818 Branch<64, IdentitySchema, [Option<Head<64, IdentitySchema, ()>>; 128], ()>,
1819 >(),
1820 48 + 16 * 64
1821 );
1822 assert_eq!(
1823 mem::size_of::<
1824 Branch<64, IdentitySchema, [Option<Head<64, IdentitySchema, ()>>; 256], ()>,
1825 >(),
1826 48 + 16 * 128
1827 );
1828 }
1829
1830 #[test]
1833 fn tree_union_single() {
1834 const KEY_SIZE: usize = 8;
1835 let mut left = PATCH::<KEY_SIZE, IdentitySchema, ()>::new();
1836 let mut right = PATCH::<KEY_SIZE, IdentitySchema, ()>::new();
1837 let left_entry = Entry::new(&[0, 0, 0, 0, 0, 0, 0, 0]);
1838 let right_entry = Entry::new(&[0, 0, 0, 0, 0, 0, 0, 1]);
1839 left.insert(&left_entry);
1840 right.insert(&right_entry);
1841 left.union(right);
1842 assert_eq!(left.len(), 2);
1843 }
1844
1845 proptest! {
1851 #[test]
1852 fn tree_insert(keys in prop::collection::vec(prop::collection::vec(0u8..=255, 64), 1..1024)) {
1853 let mut tree = PATCH::<64, IdentitySchema, ()>::new();
1854 for key in keys {
1855 let key: [u8; 64] = key.try_into().unwrap();
1856 let entry = Entry::new(&key);
1857 tree.insert(&entry);
1858 }
1859 }
1860
1861 #[test]
1862 fn tree_len(keys in prop::collection::vec(prop::collection::vec(0u8..=255, 64), 1..1024)) {
1863 let mut tree = PATCH::<64, IdentitySchema, ()>::new();
1864 let mut set = HashSet::new();
1865 for key in keys {
1866 let key: [u8; 64] = key.try_into().unwrap();
1867 let entry = Entry::new(&key);
1868 tree.insert(&entry);
1869 set.insert(key);
1870 }
1871
1872 prop_assert_eq!(set.len() as u64, tree.len())
1873 }
1874
1875 #[test]
1876 fn tree_infixes(keys in prop::collection::vec(prop::collection::vec(0u8..=255, 64), 1..1024)) {
1877 let mut tree = PATCH::<64, IdentitySchema, ()>::new();
1878 let mut set = HashSet::new();
1879 for key in keys {
1880 let key: [u8; 64] = key.try_into().unwrap();
1881 let entry = Entry::new(&key);
1882 tree.insert(&entry);
1883 set.insert(key);
1884 }
1885 let mut set_vec = Vec::from_iter(set.into_iter());
1886 let mut tree_vec = vec![];
1887 tree.infixes(&[0; 0], &mut |&x: &[u8; 64]| tree_vec.push(x));
1888
1889 set_vec.sort();
1890 tree_vec.sort();
1891
1892 prop_assert_eq!(set_vec, tree_vec);
1893 }
1894
1895 #[test]
1896 fn tree_iter(keys in prop::collection::vec(prop::collection::vec(0u8..=255, 64), 1..1024)) {
1897 let mut tree = PATCH::<64, IdentitySchema, ()>::new();
1898 let mut set = HashSet::new();
1899 for key in keys {
1900 let key: [u8; 64] = key.try_into().unwrap();
1901 let entry = Entry::new(&key);
1902 tree.insert(&entry);
1903 set.insert(key);
1904 }
1905 let mut set_vec = Vec::from_iter(set.into_iter());
1906 let mut tree_vec = vec![];
1907 for key in &tree {
1908 tree_vec.push(*key);
1909 }
1910
1911 set_vec.sort();
1912 tree_vec.sort();
1913
1914 prop_assert_eq!(set_vec, tree_vec);
1915 }
1916
1917 #[test]
1918 fn tree_union(left in prop::collection::vec(prop::collection::vec(0u8..=255, 64), 200),
1919 right in prop::collection::vec(prop::collection::vec(0u8..=255, 64), 200)) {
1920 let mut set = HashSet::new();
1921
1922 let mut left_tree = PATCH::<64, IdentitySchema, ()>::new();
1923 for entry in left {
1924 let mut key = [0; 64];
1925 key.iter_mut().set_from(entry.iter().cloned());
1926 let entry = Entry::new(&key);
1927 left_tree.insert(&entry);
1928 set.insert(key);
1929 }
1930
1931 let mut right_tree = PATCH::<64, IdentitySchema, ()>::new();
1932 for entry in right {
1933 let mut key = [0; 64];
1934 key.iter_mut().set_from(entry.iter().cloned());
1935 let entry = Entry::new(&key);
1936 right_tree.insert(&entry);
1937 set.insert(key);
1938 }
1939
1940 left_tree.union(right_tree);
1941
1942 let mut set_vec = Vec::from_iter(set.into_iter());
1943 let mut tree_vec = vec![];
1944 left_tree.infixes(&[0; 0], &mut |&x: &[u8;64]| tree_vec.push(x));
1945
1946 set_vec.sort();
1947 tree_vec.sort();
1948
1949 prop_assert_eq!(set_vec, tree_vec);
1950 }
1951
1952 #[test]
1953 fn tree_union_empty(left in prop::collection::vec(prop::collection::vec(0u8..=255, 64), 2)) {
1954 let mut set = HashSet::new();
1955
1956 let mut left_tree = PATCH::<64, IdentitySchema, ()>::new();
1957 for entry in left {
1958 let mut key = [0; 64];
1959 key.iter_mut().set_from(entry.iter().cloned());
1960 let entry = Entry::new(&key);
1961 left_tree.insert(&entry);
1962 set.insert(key);
1963 }
1964
1965 let right_tree = PATCH::<64, IdentitySchema, ()>::new();
1966
1967 left_tree.union(right_tree);
1968
1969 let mut set_vec = Vec::from_iter(set.into_iter());
1970 let mut tree_vec = vec![];
1971 left_tree.infixes(&[0; 0], &mut |&x: &[u8;64]| tree_vec.push(x));
1972
1973 set_vec.sort();
1974 tree_vec.sort();
1975
1976 prop_assert_eq!(set_vec, tree_vec);
1977 }
1978
1979 #[test]
1984 fn cow_on_insert(base_keys in prop::collection::vec(prop::collection::vec(0u8..=255, 8), 1..1024),
1985 new_keys in prop::collection::vec(prop::collection::vec(0u8..=255, 8), 1..1024)) {
1986 let mut tree = PATCH::<8, IdentitySchema, ()>::new();
1991 for key in base_keys {
1992 let key: [u8; 8] = key[..].try_into().unwrap();
1993 let entry = Entry::new(&key);
1994 tree.insert(&entry);
1995 }
1996 let base_tree_content: Vec<[u8; 8]> = tree.iter().copied().collect();
1997
1998 let mut tree_clone = tree.clone();
1999 for key in new_keys {
2000 let key: [u8; 8] = key[..].try_into().unwrap();
2001 let entry = Entry::new(&key);
2002 tree_clone.insert(&entry);
2003 }
2004
2005 let new_tree_content: Vec<[u8; 8]> = tree.iter().copied().collect();
2006 prop_assert_eq!(base_tree_content, new_tree_content);
2007 }
2008
2009 #[test]
2010 fn cow_on_union(base_keys in prop::collection::vec(prop::collection::vec(0u8..=255, 8), 1..1024),
2011 new_keys in prop::collection::vec(prop::collection::vec(0u8..=255, 8), 1..1024)) {
2012 let mut tree = PATCH::<8, IdentitySchema, ()>::new();
2017 for key in base_keys {
2018 let key: [u8; 8] = key[..].try_into().unwrap();
2019 let entry = Entry::new(&key);
2020 tree.insert(&entry);
2021 }
2022 let base_tree_content: Vec<[u8; 8]> = tree.iter().copied().collect();
2023
2024 let mut tree_clone = tree.clone();
2025 let mut new_tree = PATCH::<8, IdentitySchema, ()>::new();
2026 for key in new_keys {
2027 let key: [u8; 8] = key[..].try_into().unwrap();
2028 let entry = Entry::new(&key);
2029 new_tree.insert(&entry);
2030 }
2031 tree_clone.union(new_tree);
2032
2033 let new_tree_content: Vec<[u8; 8]> = tree.iter().copied().collect();
2034 prop_assert_eq!(base_tree_content, new_tree_content);
2035 }
2036 }
2037
2038 #[test]
2039 fn intersect_multiple_common_children_commits_branchmut() {
2040 const KEY_SIZE: usize = 4;
2041 let mut left = PATCH::<KEY_SIZE, IdentitySchema, u32>::new();
2042 let mut right = PATCH::<KEY_SIZE, IdentitySchema, u32>::new();
2043
2044 let a = [0u8, 0u8, 0u8, 1u8];
2045 let b = [0u8, 0u8, 0u8, 2u8];
2046 let c = [0u8, 0u8, 0u8, 3u8];
2047 let d = [2u8, 0u8, 0u8, 0u8];
2048 let e = [3u8, 0u8, 0u8, 0u8];
2049
2050 left.insert(&Entry::with_value(&a, 1));
2051 left.insert(&Entry::with_value(&b, 2));
2052 left.insert(&Entry::with_value(&c, 3));
2053 left.insert(&Entry::with_value(&d, 4));
2054
2055 right.insert(&Entry::with_value(&a, 10));
2056 right.insert(&Entry::with_value(&b, 11));
2057 right.insert(&Entry::with_value(&c, 12));
2058 right.insert(&Entry::with_value(&e, 13));
2059
2060 let res = left.intersect(&right);
2061 assert_eq!(res.len(), 3);
2063 assert!(res.get(&a).is_some());
2064 assert!(res.get(&b).is_some());
2065 assert!(res.get(&c).is_some());
2066 }
2067
2068 #[test]
2069 fn difference_multiple_children_commits_branchmut() {
2070 const KEY_SIZE: usize = 4;
2071 let mut left = PATCH::<KEY_SIZE, IdentitySchema, u32>::new();
2072 let mut right = PATCH::<KEY_SIZE, IdentitySchema, u32>::new();
2073
2074 let a = [0u8, 0u8, 0u8, 1u8];
2075 let b = [0u8, 0u8, 0u8, 2u8];
2076 let c = [0u8, 0u8, 0u8, 3u8];
2077 let d = [2u8, 0u8, 0u8, 0u8];
2078 let e = [3u8, 0u8, 0u8, 0u8];
2079
2080 left.insert(&Entry::with_value(&a, 1));
2081 left.insert(&Entry::with_value(&b, 2));
2082 left.insert(&Entry::with_value(&c, 3));
2083 left.insert(&Entry::with_value(&d, 4));
2084
2085 right.insert(&Entry::with_value(&a, 10));
2086 right.insert(&Entry::with_value(&b, 11));
2087 right.insert(&Entry::with_value(&c, 12));
2088 right.insert(&Entry::with_value(&e, 13));
2089
2090 let res = left.difference(&right);
2091 assert_eq!(res.len(), 1);
2093 assert!(res.get(&d).is_some());
2094 }
2095
2096 #[test]
2097 fn slot_edit_branchmut_insert_update() {
2098 const KEY_SIZE: usize = 8;
2100 let mut tree = PATCH::<KEY_SIZE, IdentitySchema, u32>::new();
2101
2102 let entry1 = Entry::with_value(&[0u8; KEY_SIZE], 1u32);
2103 let entry2 = Entry::with_value(&[1u8; KEY_SIZE], 2u32);
2104 tree.insert(&entry1);
2105 tree.insert(&entry2);
2106 assert_eq!(tree.len(), 2);
2107
2108 {
2110 let mut ed = crate::patch::branch::BranchMut::from_slot(&mut tree.root);
2111
2112 let start_depth = ed.end_depth as usize;
2114 let inserted = Entry::with_value(&[2u8; KEY_SIZE], 3u32)
2115 .leaf::<IdentitySchema>()
2116 .with_start(start_depth);
2117 let key = inserted.key();
2118
2119 ed.modify_child(key, |opt| match opt {
2120 Some(old) => Some(Head::insert_leaf(old, inserted, start_depth)),
2121 None => Some(inserted),
2122 });
2123 }
2125
2126 assert_eq!(tree.len(), 3);
2127 assert_eq!(tree.get(&[2u8; KEY_SIZE]), Some(&3u32));
2128 }
2129}