@@ -10,79 +10,79 @@ pub enum SearchResult<BorrowType, K, V, FoundType, GoDownType> {
10
10
GoDown ( Handle < NodeRef < BorrowType , K , V , GoDownType > , marker:: Edge > ) ,
11
11
}
12
12
13
- /// Looks up a given key in a (sub)tree headed by the given node, recursively.
14
- /// Returns a `Found` with the handle of the matching KV, if any. Otherwise,
15
- /// returns a `GoDown` with the handle of the leaf edge where the key belongs.
16
- ///
17
- /// The result is meaningful only if the tree is ordered by key, like the tree
18
- /// in a `BTreeMap` is.
19
- pub fn search_tree < BorrowType , K , V , Q : ?Sized > (
20
- mut node : NodeRef < BorrowType , K , V , marker:: LeafOrInternal > ,
21
- key : & Q ,
22
- ) -> SearchResult < BorrowType , K , V , marker:: LeafOrInternal , marker:: Leaf >
23
- where
24
- Q : Ord ,
25
- K : Borrow < Q > ,
26
- {
27
- loop {
28
- match search_node ( node, key) {
29
- Found ( handle) => return Found ( handle) ,
30
- GoDown ( handle) => match handle. force ( ) {
31
- Leaf ( leaf) => return GoDown ( leaf) ,
32
- Internal ( internal) => {
33
- node = internal. descend ( ) ;
34
- continue ;
35
- }
36
- } ,
13
+ pub enum IndexResult {
14
+ KV ( usize ) ,
15
+ Edge ( usize ) ,
16
+ }
17
+
18
+ impl < BorrowType , K , V > NodeRef < BorrowType , K , V , marker:: LeafOrInternal > {
19
+ /// Looks up a given key in a (sub)tree headed by the node, recursively.
20
+ /// Returns a `Found` with the handle of the matching KV, if any. Otherwise,
21
+ /// returns a `GoDown` with the handle of the leaf edge where the key belongs.
22
+ ///
23
+ /// The result is meaningful only if the tree is ordered by key, like the tree
24
+ /// in a `BTreeMap` is.
25
+ pub fn search_tree < Q : ?Sized > (
26
+ mut self ,
27
+ key : & Q ,
28
+ ) -> SearchResult < BorrowType , K , V , marker:: LeafOrInternal , marker:: Leaf >
29
+ where
30
+ Q : Ord ,
31
+ K : Borrow < Q > ,
32
+ {
33
+ loop {
34
+ self = match self . search_node ( key) {
35
+ Found ( handle) => return Found ( handle) ,
36
+ GoDown ( handle) => match handle. force ( ) {
37
+ Leaf ( leaf) => return GoDown ( leaf) ,
38
+ Internal ( internal) => internal. descend ( ) ,
39
+ } ,
40
+ }
37
41
}
38
42
}
39
43
}
40
44
41
- /// Looks up a given key in a given node, without recursion.
42
- /// Returns a `Found` with the handle of the matching KV, if any. Otherwise,
43
- /// returns a `GoDown` with the handle of the edge where the key might be found
44
- /// (if the node is internal) or where the key can be inserted.
45
- ///
46
- /// The result is meaningful only if the tree is ordered by key, like the tree
47
- /// in a `BTreeMap` is.
48
- pub fn search_node < BorrowType , K , V , Type , Q : ?Sized > (
49
- node : NodeRef < BorrowType , K , V , Type > ,
50
- key : & Q ,
51
- ) -> SearchResult < BorrowType , K , V , Type , Type >
52
- where
53
- Q : Ord ,
54
- K : Borrow < Q > ,
55
- {
56
- match search_linear ( & node, key) {
57
- ( idx, true ) => Found ( unsafe { Handle :: new_kv ( node, idx) } ) ,
58
- ( idx, false ) => GoDown ( unsafe { Handle :: new_edge ( node, idx) } ) ,
45
+ impl < BorrowType , K , V , Type > NodeRef < BorrowType , K , V , Type > {
46
+ /// Looks up a given key in the node, without recursion.
47
+ /// Returns a `Found` with the handle of the matching KV, if any. Otherwise,
48
+ /// returns a `GoDown` with the handle of the edge where the key might be found
49
+ /// (if the node is internal) or where the key can be inserted.
50
+ ///
51
+ /// The result is meaningful only if the tree is ordered by key, like the tree
52
+ /// in a `BTreeMap` is.
53
+ pub fn search_node < Q : ?Sized > ( self , key : & Q ) -> SearchResult < BorrowType , K , V , Type , Type >
54
+ where
55
+ Q : Ord ,
56
+ K : Borrow < Q > ,
57
+ {
58
+ match self . find_index ( key) {
59
+ IndexResult :: KV ( idx) => Found ( unsafe { Handle :: new_kv ( self , idx) } ) ,
60
+ IndexResult :: Edge ( idx) => GoDown ( unsafe { Handle :: new_edge ( self , idx) } ) ,
61
+ }
59
62
}
60
- }
61
63
62
- /// Returns either the KV index in the node at which the key (or an equivalent)
63
- /// exists and `true`, or the edge index where the key belongs and `false`.
64
- ///
65
- /// The result is meaningful only if the tree is ordered by key, like the tree
66
- /// in a `BTreeMap` is.
67
- fn search_linear < BorrowType , K , V , Type , Q : ?Sized > (
68
- node : & NodeRef < BorrowType , K , V , Type > ,
69
- key : & Q ,
70
- ) -> ( usize , bool )
71
- where
72
- Q : Ord ,
73
- K : Borrow < Q > ,
74
- {
75
- // This function is defined over all borrow types (immutable, mutable, owned).
76
- // Using `keys_at()` is fine here even if BorrowType is mutable, as all we return
77
- // is an index -- not a reference.
78
- let len = node. len ( ) ;
79
- for i in 0 ..len {
80
- let k = unsafe { node. reborrow ( ) . key_at ( i) } ;
81
- match key. cmp ( k. borrow ( ) ) {
82
- Ordering :: Greater => { }
83
- Ordering :: Equal => return ( i, true ) ,
84
- Ordering :: Less => return ( i, false ) ,
64
+ /// Returns either the KV index in the node at which the key (or an equivalent)
65
+ /// exists, or the edge index where the key belongs.
66
+ ///
67
+ /// The result is meaningful only if the tree is ordered by key, like the tree
68
+ /// in a `BTreeMap` is.
69
+ fn find_index < Q : ?Sized > ( & self , key : & Q ) -> IndexResult
70
+ where
71
+ Q : Ord ,
72
+ K : Borrow < Q > ,
73
+ {
74
+ // This function is defined over all borrow types (immutable, mutable, owned).
75
+ // Using `keys_at()` is fine here even if BorrowType is mutable, as all we return
76
+ // is an index -- not a reference.
77
+ let len = self . len ( ) ;
78
+ for i in 0 ..len {
79
+ let k = unsafe { self . reborrow ( ) . key_at ( i) } ;
80
+ match key. cmp ( k. borrow ( ) ) {
81
+ Ordering :: Greater => { }
82
+ Ordering :: Equal => return IndexResult :: KV ( i) ,
83
+ Ordering :: Less => return IndexResult :: Edge ( i) ,
84
+ }
85
85
}
86
+ IndexResult :: Edge ( len)
86
87
}
87
- ( len, false )
88
88
}
0 commit comments