1use super::{Comparator, Forest, INNER_SIZE, Node, NodeData, NodePool, Path};
4use crate::packed_option::PackedOption;
5#[cfg(test)]
6use alloc::string::String;
7#[cfg(test)]
8use core::fmt;
9use core::{
10 marker::PhantomData,
11 mem,
12 ops::{Bound, RangeBounds},
13};
14use wasmtime_core::{alloc::PanicOnOom as _, error::OutOfMemory};
15
16struct MapTypes<K, V>(PhantomData<(K, V)>);
18
19impl<K, V> Forest for MapTypes<K, V>
20where
21 K: Copy,
22 V: Copy,
23{
24 type Key = K;
25 type Value = V;
26 type LeafKeys = [K; INNER_SIZE - 1];
27 type LeafValues = [V; INNER_SIZE - 1];
28
29 fn splat_key(key: Self::Key) -> Self::LeafKeys {
30 [key; INNER_SIZE - 1]
31 }
32
33 fn splat_value(value: Self::Value) -> Self::LeafValues {
34 [value; INNER_SIZE - 1]
35 }
36}
37
38pub struct MapForest<K, V>
40where
41 K: Copy,
42 V: Copy,
43{
44 nodes: NodePool<MapTypes<K, V>>,
45}
46
47impl<K, V> MapForest<K, V>
48where
49 K: Copy,
50 V: Copy,
51{
52 pub fn new() -> Self {
54 Self {
55 nodes: NodePool::new(),
56 }
57 }
58
59 pub fn clear(&mut self) {
63 self.nodes.clear();
64 }
65}
66
67#[derive(Clone)]
76pub struct Map<K, V>
77where
78 K: Copy,
79 V: Copy,
80{
81 root: PackedOption<Node>,
82 unused: PhantomData<(K, V)>,
83}
84
85impl<K, V> Map<K, V>
86where
87 K: Copy,
88 V: Copy,
89{
90 pub fn new() -> Self {
92 Self {
93 root: None.into(),
94 unused: PhantomData,
95 }
96 }
97
98 pub fn is_empty(&self) -> bool {
100 self.root.is_none()
101 }
102
103 pub fn get<C: Comparator<K>>(&self, key: K, forest: &MapForest<K, V>, comp: &C) -> Option<V> {
105 self.root
106 .expand()
107 .and_then(|root| Path::default().find(key, root, &forest.nodes, comp))
108 }
109
110 pub fn get_or_less<C: Comparator<K>>(
118 &self,
119 key: K,
120 forest: &MapForest<K, V>,
121 comp: &C,
122 ) -> Option<(K, V)> {
123 self.root.expand().and_then(|root| {
124 let mut path = Path::default();
125 match path.find(key, root, &forest.nodes, comp) {
126 Some(v) => Some((key, v)),
127 None => path.prev(root, &forest.nodes),
128 }
129 })
130 }
131
132 pub fn insert<C: Comparator<K>>(
134 &mut self,
135 key: K,
136 value: V,
137 forest: &mut MapForest<K, V>,
138 comp: &C,
139 ) -> Option<V> {
140 self.cursor_mut(forest, comp).insert(key, value)
141 }
142
143 pub fn try_insert<C: Comparator<K>>(
145 &mut self,
146 key: K,
147 value: V,
148 forest: &mut MapForest<K, V>,
149 comp: &C,
150 ) -> Result<Option<V>, OutOfMemory> {
151 self.cursor_mut(forest, comp).try_insert(key, value)
152 }
153
154 pub fn remove<C: Comparator<K>>(
156 &mut self,
157 key: K,
158 forest: &mut MapForest<K, V>,
159 comp: &C,
160 ) -> Option<V> {
161 let mut c = self.cursor_mut(forest, comp);
162 if c.goto(key).is_some() {
163 c.remove()
164 } else {
165 None
166 }
167 }
168
169 pub fn clear(&mut self, forest: &mut MapForest<K, V>) {
171 if let Some(root) = self.root.take() {
172 forest.nodes.free_tree(root);
173 }
174 }
175
176 pub fn retain<F>(&mut self, forest: &mut MapForest<K, V>, mut predicate: F)
182 where
183 F: FnMut(K, &mut V) -> bool,
184 {
185 let mut path = Path::default();
186 if let Some(root) = self.root.expand() {
187 path.first(root, &forest.nodes);
188 }
189 while let Some((node, entry)) = path.leaf_pos() {
190 let keep = {
191 let (ks, vs) = forest.nodes[node].unwrap_leaf_mut();
192 predicate(ks[entry], &mut vs[entry])
193 };
194 if keep {
195 path.next(&forest.nodes);
196 } else {
197 self.root = path.remove(&mut forest.nodes).into();
198 }
199 }
200 }
201
202 pub fn cursor<'a, C: Comparator<K>>(
206 &'a self,
207 forest: &'a MapForest<K, V>,
208 comp: &'a C,
209 ) -> MapCursor<'a, K, V, C> {
210 MapCursor::new(self, forest, comp)
211 }
212
213 pub fn cursor_mut<'a, C: Comparator<K>>(
217 &'a mut self,
218 forest: &'a mut MapForest<K, V>,
219 comp: &'a C,
220 ) -> MapCursorMut<'a, K, V, C> {
221 MapCursorMut::new(self, forest, comp)
222 }
223
224 pub fn iter<'a>(&'a self, forest: &'a MapForest<K, V>) -> MapIter<'a, K, V> {
226 MapIter {
227 root: self.root,
228 pool: &forest.nodes,
229 path: Path::default(),
230 }
231 }
232
233 pub fn into_iter(self, forest: MapForest<K, V>) -> MapIntoIter<K, V> {
235 MapIntoIter {
236 root: self.root,
237 pool: forest.nodes,
238 path: Path::default(),
239 }
240 }
241
242 pub fn range<'a, R, C>(
244 &'a self,
245 range: R,
246 forest: &'a MapForest<K, V>,
247 comp: &'a C,
248 ) -> MapRange<'a, K, V, C>
249 where
250 R: RangeBounds<K>,
251 C: Comparator<K>,
252 {
253 MapRange {
254 cursor: self.cursor(forest, comp),
255 start: copied_bound(range.start_bound()),
256 end: copied_bound(range.end_bound()),
257 direction: Direction::None,
258 }
259 }
260}
261
262fn copied_bound<K>(start_bound: Bound<&K>) -> Bound<K>
263where
264 K: Copy,
265{
266 match start_bound {
267 Bound::Unbounded => Bound::Unbounded,
268 Bound::Included(x) => Bound::Included(*x),
269 Bound::Excluded(x) => Bound::Excluded(*x),
270 }
271}
272
273impl<K, V> Default for Map<K, V>
274where
275 K: Copy,
276 V: Copy,
277{
278 fn default() -> Self {
279 Self::new()
280 }
281}
282
283#[cfg(test)]
284impl<K, V> Map<K, V>
285where
286 K: Copy + fmt::Display,
287 V: Copy,
288{
289 fn verify<C: Comparator<K>>(&self, forest: &MapForest<K, V>, comp: &C)
291 where
292 NodeData<MapTypes<K, V>>: fmt::Display,
293 {
294 if let Some(root) = self.root.expand() {
295 forest.nodes.verify_tree(root, comp);
296 }
297 }
298
299 fn tpath<C: Comparator<K>>(&self, key: K, forest: &MapForest<K, V>, comp: &C) -> String {
301 use alloc::string::ToString;
302 match self.root.expand() {
303 None => "map(empty)".to_string(),
304 Some(root) => {
305 let mut path = Path::default();
306 path.find(key, root, &forest.nodes, comp);
307 path.to_string()
308 }
309 }
310 }
311}
312
313struct MapCursorRaw<K, V>
314where
315 K: Copy,
316 V: Copy,
317{
318 path: Path<MapTypes<K, V>>,
319}
320
321impl<K, V> MapCursorRaw<K, V>
322where
323 K: Copy,
324 V: Copy,
325{
326 fn new() -> Self {
327 Self {
328 path: Path::default(),
329 }
330 }
331
332 fn is_empty(&self, root: &PackedOption<Node>) -> bool {
333 root.is_none()
334 }
335
336 fn next(&mut self, pool: &NodePool<MapTypes<K, V>>) -> Option<(K, V)> {
337 self.path.next(pool)
338 }
339
340 fn prev(
341 &mut self,
342 root: &PackedOption<Node>,
343 pool: &NodePool<MapTypes<K, V>>,
344 ) -> Option<(K, V)> {
345 root.expand().and_then(|root| self.path.prev(root, pool))
346 }
347
348 fn key(&self, pool: &NodePool<MapTypes<K, V>>) -> Option<K> {
349 self.path
350 .leaf_pos()
351 .and_then(|(node, entry)| pool[node].unwrap_leaf().0.get(entry).cloned())
352 }
353
354 fn value(&self, pool: &NodePool<MapTypes<K, V>>) -> Option<V> {
355 self.path
356 .leaf_pos()
357 .and_then(|(node, entry)| pool[node].unwrap_leaf().1.get(entry).cloned())
358 }
359
360 fn value_mut<'a>(&self, pool: &'a mut NodePool<MapTypes<K, V>>) -> Option<&'a mut V> {
361 self.path
362 .leaf_pos()
363 .and_then(move |(node, entry)| pool[node].unwrap_leaf_mut().1.get_mut(entry))
364 }
365
366 fn goto(
367 &mut self,
368 root: &PackedOption<Node>,
369 pool: &NodePool<MapTypes<K, V>>,
370 elem: K,
371 comp: &impl Comparator<K>,
372 ) -> Option<V> {
373 root.expand().and_then(|root| {
374 let v = self.path.find(elem, root, pool, comp);
375 if v.is_none() {
376 self.path.normalize(pool);
377 }
378 v
379 })
380 }
381
382 fn goto_first(
383 &mut self,
384 root: &PackedOption<Node>,
385 pool: &NodePool<MapTypes<K, V>>,
386 ) -> Option<V> {
387 root.map(|root| self.path.first(root, pool).1)
388 }
389
390 fn goto_end(&mut self) {
391 self.path = Path::default();
392 }
393
394 fn insert(
395 &mut self,
396 root: &mut PackedOption<Node>,
397 pool: &mut NodePool<MapTypes<K, V>>,
398 key: K,
399 value: V,
400 comp: &impl Comparator<K>,
401 ) -> Option<V> {
402 self.try_insert(root, pool, key, value, comp).panic_on_oom()
403 }
404
405 fn try_insert(
406 &mut self,
407 root: &mut PackedOption<Node>,
408 pool: &mut NodePool<MapTypes<K, V>>,
409 key: K,
410 value: V,
411 comp: &impl Comparator<K>,
412 ) -> Result<Option<V>, OutOfMemory> {
413 match root.expand() {
414 None => {
415 let node = pool.alloc_node(NodeData::leaf(key, value))?;
416 *root = node.into();
417 self.path.set_root_node(node);
418 Ok(None)
419 }
420 Some(r) => {
421 let old = self.path.find(key, r, pool, comp);
423 if old.is_some() {
424 *self.path.value_mut(pool) = value;
425 } else {
426 *root = self.path.insert(key, value, pool)?.into();
427 }
428 Ok(old)
429 }
430 }
431 }
432
433 fn remove(
434 &mut self,
435 root: &mut PackedOption<Node>,
436 pool: &mut NodePool<MapTypes<K, V>>,
437 ) -> Option<V> {
438 let value = self.value(pool);
439 if value.is_some() {
440 *root = self.path.remove(pool).into();
441 }
442 value
443 }
444}
445
446pub struct MapCursor<'a, K, V, C>
451where
452 K: 'a + Copy,
453 V: 'a + Copy,
454 C: 'a + Comparator<K>,
455{
456 root: &'a PackedOption<Node>,
457 pool: &'a NodePool<MapTypes<K, V>>,
458 comp: &'a C,
459 raw: MapCursorRaw<K, V>,
460}
461
462impl<'a, K, V, C> MapCursor<'a, K, V, C>
463where
464 K: Copy,
465 V: Copy,
466 C: Comparator<K>,
467{
468 fn new(container: &'a Map<K, V>, forest: &'a MapForest<K, V>, comp: &'a C) -> Self {
470 Self {
471 root: &container.root,
472 pool: &forest.nodes,
473 comp,
474 raw: MapCursorRaw::new(),
475 }
476 }
477
478 pub fn is_empty(&self) -> bool {
480 self.raw.is_empty(self.root)
481 }
482
483 pub fn next(&mut self) -> Option<(K, V)> {
488 self.raw.next(self.pool)
489 }
490
491 pub fn prev(&mut self) -> Option<(K, V)> {
495 self.raw.prev(self.root, self.pool)
496 }
497
498 pub fn key(&self) -> Option<K> {
500 self.raw.key(self.pool)
501 }
502
503 pub fn value(&self) -> Option<V> {
505 self.raw.value(self.pool)
506 }
507
508 pub fn goto(&mut self, elem: K) -> Option<V> {
515 self.raw.goto(self.root, self.pool, elem, self.comp)
516 }
517
518 pub fn goto_first(&mut self) -> Option<V> {
520 self.raw.goto_first(self.root, self.pool)
521 }
522
523 pub fn goto_end(&mut self) {
525 self.raw.goto_end();
526 }
527}
528
529pub struct MapCursorMut<'a, K, V, C>
534where
535 K: 'a + Copy,
536 V: 'a + Copy,
537 C: 'a + Comparator<K>,
538{
539 root: &'a mut PackedOption<Node>,
540 pool: &'a mut NodePool<MapTypes<K, V>>,
541 comp: &'a C,
542 raw: MapCursorRaw<K, V>,
543}
544
545impl<'a, K, V, C> MapCursorMut<'a, K, V, C>
546where
547 K: Copy,
548 V: Copy,
549 C: Comparator<K>,
550{
551 fn new(container: &'a mut Map<K, V>, forest: &'a mut MapForest<K, V>, comp: &'a C) -> Self {
553 Self {
554 root: &mut container.root,
555 pool: &mut forest.nodes,
556 comp,
557 raw: MapCursorRaw::new(),
558 }
559 }
560
561 pub fn is_empty(&self) -> bool {
563 self.raw.is_empty(self.root)
564 }
565
566 pub fn next(&mut self) -> Option<(K, V)> {
571 self.raw.next(self.pool)
572 }
573
574 pub fn prev(&mut self) -> Option<(K, V)> {
578 self.raw.prev(self.root, self.pool)
579 }
580
581 pub fn key(&self) -> Option<K> {
583 self.raw.key(self.pool)
584 }
585
586 pub fn value(&self) -> Option<V> {
588 self.raw.value(self.pool)
589 }
590
591 pub fn value_mut(&mut self) -> Option<&mut V> {
593 self.raw.value_mut(self.pool)
594 }
595
596 pub fn goto(&mut self, elem: K) -> Option<V> {
603 self.raw.goto(self.root, self.pool, elem, self.comp)
604 }
605
606 pub fn goto_first(&mut self) -> Option<V> {
608 self.raw.goto_first(self.root, self.pool)
609 }
610
611 pub fn insert(&mut self, key: K, value: V) -> Option<V> {
617 self.raw.insert(self.root, self.pool, key, value, self.comp)
618 }
619
620 pub fn try_insert(&mut self, key: K, value: V) -> Result<Option<V>, OutOfMemory> {
622 self.raw
623 .try_insert(self.root, self.pool, key, value, self.comp)
624 }
625
626 pub fn remove(&mut self) -> Option<V> {
629 self.raw.remove(self.root, self.pool)
630 }
631}
632
633pub struct MapIter<'a, K, V>
635where
636 K: 'a + Copy,
637 V: 'a + Copy,
638{
639 root: PackedOption<Node>,
640 pool: &'a NodePool<MapTypes<K, V>>,
641 path: Path<MapTypes<K, V>>,
642}
643
644impl<'a, K, V> Iterator for MapIter<'a, K, V>
645where
646 K: 'a + Copy,
647 V: 'a + Copy,
648{
649 type Item = (K, V);
650
651 fn next(&mut self) -> Option<Self::Item> {
652 match self.root.take() {
656 Some(root) => Some(self.path.first(root, self.pool)),
657 None => self.path.next(self.pool),
658 }
659 }
660}
661
662pub struct MapIntoIter<K, V>
664where
665 K: Copy,
666 V: Copy,
667{
668 root: PackedOption<Node>,
669 pool: NodePool<MapTypes<K, V>>,
670 path: Path<MapTypes<K, V>>,
671}
672
673impl<K, V> Iterator for MapIntoIter<K, V>
674where
675 K: Copy,
676 V: Copy,
677{
678 type Item = (K, V);
679
680 fn next(&mut self) -> Option<Self::Item> {
681 match self.root.take() {
685 Some(root) => Some(self.path.first(root, &self.pool)),
686 None => self.path.next(&self.pool),
687 }
688 }
689}
690
691#[derive(Clone, Copy, Debug, PartialEq, Eq)]
692enum Direction {
693 None,
694 Next,
695 NextBack,
696}
697
698pub struct MapRange<'a, K, V, C>
700where
701 K: Copy,
702 V: Copy,
703 C: Comparator<K>,
704{
705 cursor: MapCursor<'a, K, V, C>,
706 start: Bound<K>,
707 end: Bound<K>,
708
709 direction: Direction,
718}
719
720impl<'a, K, V, C> Iterator for MapRange<'a, K, V, C>
721where
722 K: Copy,
723 V: Copy,
724 C: Comparator<K>,
725{
726 type Item = (K, V);
727
728 fn next(&mut self) -> Option<Self::Item> {
729 let old_direction = mem::replace(&mut self.direction, Direction::Next);
730
731 let key_val = (|| {
732 if old_direction == Direction::Next {
733 self.cursor.next()
734 } else {
735 match self.start {
736 Bound::Included(k) => {
737 self.cursor.goto(k);
738 Some((self.cursor.key()?, self.cursor.value()?))
739 }
740 Bound::Excluded(k) => {
741 self.cursor.goto(k);
742 if self
743 .cursor
744 .key()
745 .is_some_and(|key| self.cursor.comp.cmp(k, key).is_eq())
746 {
747 self.cursor.next()
748 } else {
749 Some((self.cursor.key()?, self.cursor.value()?))
750 }
751 }
752 Bound::Unbounded => {
753 let val = self.cursor.goto_first()?;
754 Some((self.cursor.key()?, val))
755 }
756 }
757 }
758 })();
759
760 match key_val {
761 Some((key, val)) if self.is_in_bounds(key) => {
762 self.start = Bound::Excluded(key);
763 Some((key, val))
764 }
765 _ => {
766 self.cursor.goto_end();
767 None
768 }
769 }
770 }
771}
772
773impl<'a, K, V, C> DoubleEndedIterator for MapRange<'a, K, V, C>
774where
775 K: Copy,
776 V: Copy,
777 C: Comparator<K>,
778{
779 fn next_back(&mut self) -> Option<Self::Item> {
780 let old_direction = mem::replace(&mut self.direction, Direction::NextBack);
781
782 let key_val = (|| {
783 if old_direction == Direction::NextBack {
784 self.cursor.prev()
785 } else {
786 match self.end {
787 Bound::Included(k) => match self.cursor.goto(k) {
788 Some(_) => Some((self.cursor.key()?, self.cursor.value()?)),
789 None => self.cursor.prev(),
790 },
791 Bound::Excluded(k) => {
792 self.cursor.goto(k);
793 if self
794 .cursor
795 .key()
796 .is_some_and(|key| self.cursor.comp.cmp(k, key).is_eq())
797 {
798 self.cursor.prev()
799 } else {
800 Some((self.cursor.key()?, self.cursor.value()?))
801 }
802 }
803 Bound::Unbounded => {
804 self.cursor.goto_end();
805 self.cursor.prev()
806 }
807 }
808 }
809 })();
810
811 match key_val {
812 Some((key, val)) if self.is_in_bounds(key) => {
813 self.end = Bound::Excluded(key);
814 Some((key, val))
815 }
816 _ => {
817 self.cursor.goto_end();
818 None
819 }
820 }
821 }
822}
823
824impl<'a, K, V, C> MapRange<'a, K, V, C>
825where
826 K: Copy,
827 V: Copy,
828 C: Comparator<K>,
829{
830 fn is_in_bounds(&self, key: K) -> bool {
831 self.is_in_start_bound(key) && self.is_in_end_bound(key)
832 }
833
834 fn is_in_start_bound(&self, key: K) -> bool {
835 match self.start {
836 Bound::Included(k) => self.cursor.comp.cmp(key, k).is_ge(),
837 Bound::Excluded(k) => self.cursor.comp.cmp(key, k).is_gt(),
838 Bound::Unbounded => true,
839 }
840 }
841
842 fn is_in_end_bound(&self, key: K) -> bool {
843 match self.end {
844 Bound::Included(k) => self.cursor.comp.cmp(k, key).is_ge(),
845 Bound::Excluded(k) => self.cursor.comp.cmp(k, key).is_gt(),
846 Bound::Unbounded => true,
847 }
848 }
849}
850
851#[cfg(test)]
852impl<'a, K, V, C> MapCursorMut<'a, K, V, C>
853where
854 K: Copy + fmt::Display,
855 V: Copy + fmt::Display,
856 C: Comparator<K>,
857{
858 fn verify(&self) {
859 self.raw.path.verify(self.pool);
860 self.root.map(|root| self.pool.verify_tree(root, self.comp));
861 }
862
863 fn tpath(&self) -> String {
865 use alloc::string::ToString;
866 self.raw.path.to_string()
867 }
868}
869
870#[cfg(test)]
871mod tests {
872 use super::*;
873 use alloc::{vec, vec::Vec};
874 use core::mem;
875
876 #[test]
877 fn node_size() {
878 type F = MapTypes<u32, u32>;
880 assert_eq!(mem::size_of::<NodeData<F>>(), 64);
881 }
882
883 #[test]
884 fn empty() {
885 let mut f = MapForest::<u32, f32>::new();
886 f.clear();
887
888 let mut m = Map::<u32, f32>::new();
889 assert!(m.is_empty());
890 m.clear(&mut f);
891
892 assert_eq!(m.get(7, &f, &()), None);
893 assert_eq!(m.iter(&f).next(), None);
894 assert_eq!(m.get_or_less(7, &f, &()), None);
895 m.retain(&mut f, |_, _| unreachable!());
896
897 let mut c = m.cursor_mut(&mut f, &());
898 assert!(c.is_empty());
899 assert_eq!(c.key(), None);
900 assert_eq!(c.value(), None);
901 assert_eq!(c.next(), None);
902 assert_eq!(c.prev(), None);
903 c.verify();
904 assert_eq!(c.tpath(), "<empty path>");
905 assert_eq!(c.goto_first(), None);
906 assert_eq!(c.tpath(), "<empty path>");
907 }
908
909 #[test]
910 fn inserting() {
911 let f = &mut MapForest::<u32, f32>::new();
912 let mut m = Map::<u32, f32>::new();
913
914 assert_eq!(m.insert(50, 5.0, f, &()), None);
916 assert_eq!(m.insert(50, 5.5, f, &()), Some(5.0));
917 assert_eq!(m.insert(20, 2.0, f, &()), None);
918 assert_eq!(m.insert(80, 8.0, f, &()), None);
919 assert_eq!(m.insert(40, 4.0, f, &()), None);
920 assert_eq!(m.insert(60, 6.0, f, &()), None);
921 assert_eq!(m.insert(90, 9.0, f, &()), None);
922 assert_eq!(m.insert(200, 20.0, f, &()), None);
923
924 m.verify(f, &());
925
926 assert_eq!(
927 m.iter(f).collect::<Vec<_>>(),
928 [
929 (20, 2.0),
930 (40, 4.0),
931 (50, 5.5),
932 (60, 6.0),
933 (80, 8.0),
934 (90, 9.0),
935 (200, 20.0),
936 ]
937 );
938
939 assert_eq!(m.get(0, f, &()), None);
940 assert_eq!(m.get(20, f, &()), Some(2.0));
941 assert_eq!(m.get(30, f, &()), None);
942 assert_eq!(m.get(40, f, &()), Some(4.0));
943 assert_eq!(m.get(50, f, &()), Some(5.5));
944 assert_eq!(m.get(60, f, &()), Some(6.0));
945 assert_eq!(m.get(70, f, &()), None);
946 assert_eq!(m.get(80, f, &()), Some(8.0));
947 assert_eq!(m.get(100, f, &()), None);
948
949 assert_eq!(m.get_or_less(0, f, &()), None);
950 assert_eq!(m.get_or_less(20, f, &()), Some((20, 2.0)));
951 assert_eq!(m.get_or_less(30, f, &()), Some((20, 2.0)));
952 assert_eq!(m.get_or_less(40, f, &()), Some((40, 4.0)));
953 assert_eq!(m.get_or_less(200, f, &()), Some((200, 20.0)));
954 assert_eq!(m.get_or_less(201, f, &()), Some((200, 20.0)));
955
956 {
957 let mut c = m.cursor_mut(f, &());
958 assert_eq!(c.prev(), Some((200, 20.0)));
959 assert_eq!(c.prev(), Some((90, 9.0)));
960 assert_eq!(c.prev(), Some((80, 8.0)));
961 assert_eq!(c.prev(), Some((60, 6.0)));
962 assert_eq!(c.prev(), Some((50, 5.5)));
963 assert_eq!(c.prev(), Some((40, 4.0)));
964 assert_eq!(c.prev(), Some((20, 2.0)));
965 assert_eq!(c.prev(), None);
966 }
967
968 assert_eq!(m.tpath(50, f, &()), "node0[2]");
970 assert_eq!(m.tpath(80, f, &()), "node0[4]");
971 assert_eq!(m.tpath(200, f, &()), "node0[6]");
972
973 assert_eq!(m.remove(80, f, &()), Some(8.0));
974 assert_eq!(m.tpath(50, f, &()), "node0[2]");
975 assert_eq!(m.tpath(80, f, &()), "node0[4]");
976 assert_eq!(m.tpath(200, f, &()), "node0[5]");
977 assert_eq!(m.remove(80, f, &()), None);
978 m.verify(f, &());
979
980 assert_eq!(m.remove(20, f, &()), Some(2.0));
981 assert_eq!(m.tpath(50, f, &()), "node0[1]");
982 assert_eq!(m.tpath(80, f, &()), "node0[3]");
983 assert_eq!(m.tpath(200, f, &()), "node0[4]");
984 assert_eq!(m.remove(20, f, &()), None);
985 m.verify(f, &());
986
987 {
990 let mut c = m.cursor_mut(f, &());
991 assert_eq!(c.goto_first(), Some(4.0));
992 assert_eq!(c.key(), Some(40));
993 assert_eq!(c.value(), Some(4.0));
994 assert_eq!(c.next(), Some((50, 5.5)));
995 assert_eq!(c.next(), Some((60, 6.0)));
996 assert_eq!(c.next(), Some((90, 9.0)));
997 assert_eq!(c.next(), Some((200, 20.0)));
998 c.verify();
999 assert_eq!(c.next(), None);
1000 c.verify();
1001 }
1002
1003 assert_eq!(m.remove(200, f, &()), Some(20.0));
1005 assert_eq!(m.remove(40, f, &()), Some(4.0));
1006 assert_eq!(m.remove(60, f, &()), Some(6.0));
1007 m.verify(f, &());
1008 assert_eq!(m.remove(50, f, &()), Some(5.5));
1009 m.verify(f, &());
1010 assert_eq!(m.remove(90, f, &()), Some(9.0));
1011 m.verify(f, &());
1012 assert!(m.is_empty());
1013 }
1014
1015 #[test]
1016 fn split_level0_leaf() {
1017 let f = &mut MapForest::<u32, f32>::new();
1019
1020 fn full_leaf(f: &mut MapForest<u32, f32>) -> Map<u32, f32> {
1021 let mut m = Map::new();
1022 for n in 1..8 {
1023 m.insert(n * 10, n as f32 * 1.1, f, &());
1024 }
1025 m
1026 }
1027
1028 let mut m = full_leaf(f);
1030 m.insert(5, 4.2, f, &());
1031 m.verify(f, &());
1032 assert_eq!(m.get(5, f, &()), Some(4.2));
1033
1034 m.retain(f, |k, v| {
1036 *v = (k / 10) as f32;
1037 (k % 20) == 0
1038 });
1039 assert_eq!(
1040 m.iter(f).collect::<Vec<_>>(),
1041 [(20, 2.0), (40, 4.0), (60, 6.0)]
1042 );
1043
1044 let mut m = full_leaf(f);
1046 m.insert(80, 4.2, f, &());
1047 m.verify(f, &());
1048 assert_eq!(m.get(80, f, &()), Some(4.2));
1049
1050 let mut m = full_leaf(f);
1052 m.insert(35, 4.2, f, &());
1053 m.verify(f, &());
1054 assert_eq!(m.get(35, f, &()), Some(4.2));
1055
1056 let mut m = full_leaf(f);
1058 m.insert(45, 4.2, f, &());
1059 m.verify(f, &());
1060 assert_eq!(m.get(45, f, &()), Some(4.2));
1061
1062 m.clear(f);
1063 assert!(m.is_empty());
1064 }
1065
1066 #[test]
1067 fn split_level1_leaf() {
1068 let f = &mut MapForest::<u32, f32>::new();
1070
1071 fn full(f: &mut MapForest<u32, f32>) -> Map<u32, f32> {
1079 let mut m = Map::new();
1080
1081 for row in 1..9 {
1084 for col in 1..5 {
1085 m.insert(row * 100 + col * 10, row as f32 + col as f32 * 0.1, f, &());
1086 }
1087 }
1088
1089 for row in 1..9 {
1091 for col in 5..8 {
1092 m.insert(row * 100 + col * 10, row as f32 + col as f32 * 0.1, f, &());
1093 }
1094 }
1095
1096 m
1097 }
1098
1099 let mut m = full(f);
1100 m.verify(f, &());
1102 assert_eq!(m.tpath(110, f, &()), "node2[0]--node0[0]");
1103 assert_eq!(m.tpath(140, f, &()), "node2[0]--node0[3]");
1104 assert_eq!(m.tpath(210, f, &()), "node2[1]--node1[0]");
1105 assert_eq!(m.tpath(270, f, &()), "node2[1]--node1[6]");
1106 assert_eq!(m.tpath(310, f, &()), "node2[2]--node3[0]");
1107 assert_eq!(m.tpath(810, f, &()), "node2[7]--node8[0]");
1108 assert_eq!(m.tpath(870, f, &()), "node2[7]--node8[6]");
1109
1110 {
1111 let mut c = m.cursor_mut(f, &());
1112 assert_eq!(c.goto_first(), Some(1.1));
1113 assert_eq!(c.key(), Some(110));
1114 }
1115
1116 m.insert(0, 4.2, f, &());
1118 m.verify(f, &());
1119 assert_eq!(m.get(0, f, &()), Some(4.2));
1120
1121 f.clear();
1123 m = full(f);
1124 m.insert(135, 4.2, f, &());
1125 m.verify(f, &());
1126 assert_eq!(m.get(135, f, &()), Some(4.2));
1127
1128 f.clear();
1130 m = full(f);
1131 m.insert(145, 4.2, f, &());
1132 m.verify(f, &());
1133 assert_eq!(m.get(145, f, &()), Some(4.2));
1134
1135 f.clear();
1137 m = full(f);
1138 m.insert(175, 4.2, f, &());
1139 m.verify(f, &());
1140 assert_eq!(m.get(175, f, &()), Some(4.2));
1141
1142 f.clear();
1144 m = full(f);
1145 m.insert(435, 4.2, f, &());
1146 m.verify(f, &());
1147 assert_eq!(m.get(435, f, &()), Some(4.2));
1148
1149 f.clear();
1151 m = full(f);
1152 m.insert(445, 4.2, f, &());
1153 m.verify(f, &());
1154 assert_eq!(m.get(445, f, &()), Some(4.2));
1155
1156 f.clear();
1158 m = full(f);
1159 m.insert(535, 4.2, f, &());
1160 m.verify(f, &());
1161 assert_eq!(m.get(535, f, &()), Some(4.2));
1162
1163 f.clear();
1165 m = full(f);
1166 m.insert(545, 4.2, f, &());
1167 m.verify(f, &());
1168 assert_eq!(m.get(545, f, &()), Some(4.2));
1169
1170 f.clear();
1172 m = full(f);
1173 m.insert(835, 4.2, f, &());
1174 m.verify(f, &());
1175 assert_eq!(m.get(835, f, &()), Some(4.2));
1176
1177 f.clear();
1179 m = full(f);
1180 m.insert(845, 4.2, f, &());
1181 m.verify(f, &());
1182 assert_eq!(m.get(845, f, &()), Some(4.2));
1183
1184 f.clear();
1186 m = full(f);
1187 m.insert(805, 4.2, f, &());
1188 m.verify(f, &());
1189 assert_eq!(m.get(805, f, &()), Some(4.2));
1190
1191 m.clear(f);
1192 m.verify(f, &());
1193 }
1194
1195 fn two_leaf(f: &mut MapForest<u32, f32>) -> Map<u32, f32> {
1198 f.clear();
1199 let mut m = Map::new();
1200 for n in 1..9 {
1201 m.insert(n * 10, n as f32, f, &());
1202 }
1203 m
1204 }
1205
1206 #[test]
1207 fn remove_level1() {
1208 let f = &mut MapForest::<u32, f32>::new();
1209 let mut m = two_leaf(f);
1210
1211 m.verify(f, &());
1213 assert_eq!(m.tpath(10, f, &()), "node2[0]--node0[0]");
1214 assert_eq!(m.tpath(40, f, &()), "node2[0]--node0[3]");
1215 assert_eq!(m.tpath(49, f, &()), "node2[0]--node0[4]");
1216 assert_eq!(m.tpath(50, f, &()), "node2[1]--node1[0]");
1217 assert_eq!(m.tpath(80, f, &()), "node2[1]--node1[3]");
1218
1219 assert_eq!(m.insert(55, 5.5, f, &()), None);
1221 assert_eq!(m.remove(50, f, &()), Some(5.0));
1222 m.verify(f, &());
1223 assert_eq!(m.tpath(49, f, &()), "node2[0]--node0[4]");
1224 assert_eq!(m.tpath(50, f, &()), "node2[0]--node0[4]");
1225 assert_eq!(m.tpath(55, f, &()), "node2[1]--node1[0]");
1226
1227 assert_eq!(m.insert(15, 1.5, f, &()), None);
1229 assert_eq!(m.remove(10, f, &()), Some(1.0));
1230 m.verify(f, &());
1231
1232 assert_eq!(m.remove(55, f, &()), Some(5.5));
1237 m.verify(f, &());
1238 assert_eq!(m.tpath(55, f, &()), "node2[0]--node0[4]");
1239 assert_eq!(m.tpath(60, f, &()), "node2[1]--node1[0]");
1240
1241 assert_eq!(m.insert(90, 9.0, f, &()), None);
1245 assert_eq!(m.insert(100, 10.0, f, &()), None);
1246 m.verify(f, &());
1247 assert_eq!(m.tpath(55, f, &()), "node2[0]--node0[4]");
1248 assert_eq!(m.tpath(60, f, &()), "node2[1]--node1[0]");
1249
1250 assert_eq!(m.remove(20, f, &()), Some(2.0));
1255 m.verify(f, &());
1256
1257 assert_eq!(m.tpath(50, f, &()), "node2[0]--node0[3]");
1260 assert_eq!(m.tpath(60, f, &()), "node2[0]--node0[3]");
1261 assert_eq!(m.tpath(70, f, &()), "node2[1]--node1[0]");
1262
1263 assert_eq!(m.remove(15, f, &()), Some(1.5));
1266 m.verify(f, &());
1267 }
1268
1269 #[test]
1270 fn remove_level1_rightmost() {
1271 let f = &mut MapForest::<u32, f32>::new();
1272 let mut m = two_leaf(f);
1273
1274 assert_eq!(m.remove(60, f, &()), Some(6.0));
1278 assert_eq!(m.remove(80, f, &()), Some(8.0));
1279 assert_eq!(m.remove(50, f, &()), Some(5.0));
1280 m.verify(f, &());
1281
1282 assert_eq!(m.tpath(50, f, &()), "node2[0]--node0[4]");
1284 assert_eq!(m.tpath(70, f, &()), "node2[1]--node1[0]");
1285
1286 assert_eq!(m.remove(70, f, &()), Some(7.0));
1288 m.verify(f, &());
1289 }
1290
1291 fn level3_sparse(f: &mut MapForest<u32, f32>) -> Map<u32, f32> {
1294 f.clear();
1295 let mut m = Map::new();
1296 for n in 1..133 {
1297 m.insert(n * 10, n as f32, f, &());
1298 }
1299 m
1300 }
1301
1302 #[test]
1303 fn level3_removes() {
1304 let f = &mut MapForest::<u32, f32>::new();
1305 let mut m = level3_sparse(f);
1306 m.verify(f, &());
1307
1308 assert_eq!(m.tpath(0, f, &()), "node11[0]--node2[0]--node0[0]");
1313 assert_eq!(m.tpath(10000, f, &()), "node11[7]--node41[4]--node40[4]");
1314
1315 assert_eq!(m.tpath(640, f, &()), "node11[3]--node21[3]--node19[3]");
1317 assert_eq!(m.tpath(650, f, &()), "node11[4]--node26[0]--node20[0]");
1318
1319 assert_eq!(m.remove(640, f, &()), Some(64.0));
1321 m.verify(f, &());
1322 assert_eq!(m.tpath(650, f, &()), "node11[3]--node26[3]--node20[3]");
1323
1324 assert_eq!(m.tpath(1130, f, &()), "node11[6]--node41[0]--node35[0]");
1327 assert_eq!(m.tpath(1140, f, &()), "node11[6]--node41[0]--node35[1]");
1328 assert_eq!(m.remove(1130, f, &()), Some(113.0));
1329 m.verify(f, &());
1330 assert_eq!(m.tpath(1140, f, &()), "node11[6]--node41[0]--node37[0]");
1331 }
1332
1333 #[test]
1334 fn insert_many() {
1335 let f = &mut MapForest::<u32, f32>::new();
1336 let mut m = Map::<u32, f32>::new();
1337
1338 let mm = 4096;
1339 let mut x = 0;
1340
1341 for n in 0..mm {
1342 assert_eq!(m.insert(x, n as f32, f, &()), None);
1343 m.verify(f, &());
1344
1345 x = (x + n + 1) % mm;
1346 }
1347
1348 x = 0;
1349 for n in 0..mm {
1350 assert_eq!(m.get(x, f, &()), Some(n as f32));
1351 x = (x + n + 1) % mm;
1352 }
1353
1354 x = 0;
1355 for n in 0..mm {
1356 assert_eq!(m.remove(x, f, &()), Some(n as f32));
1357 m.verify(f, &());
1358
1359 x = (x + n + 1) % mm;
1360 }
1361
1362 assert!(m.is_empty());
1363 }
1364
1365 #[test]
1366 fn immutable_cursor() {
1367 let f = &mut MapForest::<u32, f32>::new();
1368 let mut m = Map::<u32, f32>::new();
1369
1370 for i in 100..200 {
1371 m.insert(i, i as f32, f, &());
1372 }
1373
1374 let mut c = m.cursor(f, &());
1375
1376 let v = c.goto_first().unwrap();
1377 assert_eq!(v, 100.0);
1378 assert_eq!(c.key(), Some(100));
1379 assert_eq!(c.value(), Some(100.0));
1380
1381 let (k, v) = c.next().unwrap();
1382 assert_eq!(k, 101);
1383 assert_eq!(v, 101.0);
1384 assert_eq!(c.key(), Some(101));
1385 assert_eq!(c.value(), Some(101.0));
1386
1387 let (k, v) = c.next().unwrap();
1388 assert_eq!(k, 102);
1389 assert_eq!(v, 102.0);
1390 assert_eq!(c.key(), Some(102));
1391 assert_eq!(c.value(), Some(102.0));
1392
1393 let (k, v) = c.prev().unwrap();
1394 assert_eq!(k, 101);
1395 assert_eq!(v, 101.0);
1396 assert_eq!(c.key(), Some(101));
1397 assert_eq!(c.value(), Some(101.0));
1398
1399 let v = c.goto(175).unwrap();
1400 assert_eq!(v, 175.0);
1401 assert_eq!(c.key(), Some(175));
1402 assert_eq!(c.value(), Some(175.0));
1403
1404 let (k, v) = c.next().unwrap();
1405 assert_eq!(k, 176);
1406 assert_eq!(v, 176.0);
1407 assert_eq!(c.key(), Some(176));
1408 assert_eq!(c.value(), Some(176.0));
1409
1410 let (k, v) = c.prev().unwrap();
1411 assert_eq!(k, 175);
1412 assert_eq!(v, 175.0);
1413 assert_eq!(c.key(), Some(175));
1414 assert_eq!(c.value(), Some(175.0));
1415
1416 let v = c.goto(200);
1417 assert!(v.is_none());
1418 assert!(c.key().is_none());
1419 assert!(c.value().is_none());
1420
1421 for i in (100..200).rev() {
1422 let (k, v) = c.prev().unwrap();
1423 assert_eq!(k, i);
1424 assert_eq!(v, i as f32);
1425 assert_eq!(c.key(), Some(i));
1426 assert_eq!(c.value(), Some(i as f32));
1427 }
1428 assert!(c.prev().is_none());
1429
1430 assert_eq!(c.key(), Some(100));
1431 assert_eq!(c.value(), Some(100.0));
1432 for i in 101..200 {
1433 let (k, v) = c.next().unwrap();
1434 assert_eq!(k, i);
1435 assert_eq!(v, i as f32);
1436 assert_eq!(c.key(), Some(i));
1437 assert_eq!(c.value(), Some(i as f32));
1438 }
1439 assert!(c.next().is_none());
1440 assert!(c.key().is_none());
1441 assert!(c.value().is_none());
1442 }
1443
1444 #[test]
1445 fn into_iter() {
1446 let mut f = MapForest::<u32, f32>::new();
1447 let mut m = Map::<u32, f32>::new();
1448
1449 for i in 10..20 {
1450 m.insert(i, i as f32, &mut f, &());
1451 }
1452
1453 assert_eq!(
1454 m.into_iter(f).collect::<Vec<_>>(),
1455 vec![
1456 (10, 10.0),
1457 (11, 11.0),
1458 (12, 12.0),
1459 (13, 13.0),
1460 (14, 14.0),
1461 (15, 15.0),
1462 (16, 16.0),
1463 (17, 17.0),
1464 (18, 18.0),
1465 (19, 19.0),
1466 ],
1467 );
1468 }
1469
1470 #[test]
1471 fn range() {
1472 let mut f = MapForest::<u32, f32>::new();
1473 let mut m = Map::<u32, f32>::new();
1474
1475 for i in 10..20 {
1476 m.insert(i, i as f32, &mut f, &());
1477 }
1478
1479 assert_eq!(
1480 m.range(.., &f, &()).collect::<Vec<_>>(),
1481 vec![
1482 (10, 10.0),
1483 (11, 11.0),
1484 (12, 12.0),
1485 (13, 13.0),
1486 (14, 14.0),
1487 (15, 15.0),
1488 (16, 16.0),
1489 (17, 17.0),
1490 (18, 18.0),
1491 (19, 19.0),
1492 ],
1493 );
1494
1495 assert_eq!(
1496 m.range(5..12, &f, &()).collect::<Vec<_>>(),
1497 vec![(10, 10.0), (11, 11.0)],
1498 );
1499
1500 assert_eq!(
1501 m.range(18..30, &f, &()).collect::<Vec<_>>(),
1502 vec![(18, 18.0), (19, 19.0)],
1503 );
1504
1505 assert_eq!(
1506 m.range(..13, &f, &()).collect::<Vec<_>>(),
1507 vec![(10, 10.0), (11, 11.0), (12, 12.0)],
1508 );
1509
1510 assert_eq!(
1511 m.range(18.., &f, &()).collect::<Vec<_>>(),
1512 vec![(18, 18.0), (19, 19.0)],
1513 );
1514
1515 assert_eq!(
1516 m.range(12..=15, &f, &()).collect::<Vec<_>>(),
1517 vec![(12, 12.0), (13, 13.0), (14, 14.0), (15, 15.0)],
1518 );
1519
1520 assert_eq!(m.range(0..5, &f, &()).collect::<Vec<_>>(), vec![]);
1522 assert_eq!(m.range(30..40, &f, &()).collect::<Vec<_>>(), vec![]);
1523
1524 for i in 30..40 {
1526 if i % 2 == 0 {
1527 m.insert(i, i as f32, &mut f, &());
1528 }
1529 }
1530 assert_eq!(
1531 m.range(31..35, &f, &()).collect::<Vec<_>>(),
1532 vec![(32, 32.0), (34, 34.0)],
1533 );
1534 assert_eq!(
1535 m.range(29..33, &f, &()).collect::<Vec<_>>(),
1536 vec![(30, 30.0), (32, 32.0)],
1537 );
1538 assert_eq!(
1539 m.range(35..40, &f, &()).collect::<Vec<_>>(),
1540 vec![(36, 36.0), (38, 38.0)],
1541 );
1542 }
1543
1544 #[test]
1545 fn range_next_back() {
1546 let mut f = MapForest::<u32, f32>::new();
1547 let mut m = Map::<u32, f32>::new();
1548
1549 for i in 10..20 {
1550 m.insert(i, i as f32, &mut f, &());
1551 }
1552
1553 assert_eq!(
1554 m.range(12..16, &f, &()).rev().collect::<Vec<_>>(),
1555 vec![(15, 15.0), (14, 14.0), (13, 13.0), (12, 12.0)],
1556 );
1557
1558 assert_eq!(
1559 m.range(18.., &f, &()).rev().collect::<Vec<_>>(),
1560 vec![(19, 19.0), (18, 18.0)],
1561 );
1562
1563 assert_eq!(
1564 m.range(..12, &f, &()).rev().collect::<Vec<_>>(),
1565 vec![(11, 11.0), (10, 10.0)],
1566 );
1567
1568 assert_eq!(
1569 m.range(11..=12, &f, &()).rev().collect::<Vec<_>>(),
1570 vec![(12, 12.0), (11, 11.0)],
1571 );
1572
1573 let mut iter = m.range(13..=16, &f, &());
1574 assert_eq!(iter.next(), Some((13, 13.0)));
1575 assert_eq!(iter.next_back(), Some((16, 16.0)));
1576 assert_eq!(iter.next(), Some((14, 14.0)));
1577 assert_eq!(iter.next_back(), Some((15, 15.0)));
1578 assert!(iter.next().is_none());
1579 assert!(iter.next_back().is_none());
1580
1581 let mut iter = m.range(13..=16, &f, &());
1582 assert_eq!(iter.next_back(), Some((16, 16.0)));
1583 assert_eq!(iter.next(), Some((13, 13.0)));
1584 assert_eq!(iter.next_back(), Some((15, 15.0)));
1585 assert_eq!(iter.next(), Some((14, 14.0)));
1586 assert!(iter.next_back().is_none());
1587 assert!(iter.next().is_none());
1588 }
1589}