1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use core
::cmp
::Ordering
;
15 use super::node
::{Handle, NodeRef, marker}
;
17 use super::node
::ForceResult
::*;
18 use self::SearchResult
::*;
20 pub enum SearchResult
<BorrowType
, K
, V
, FoundType
, GoDownType
> {
21 Found(Handle
<NodeRef
<BorrowType
, K
, V
, FoundType
>, marker
::KV
>),
22 GoDown(Handle
<NodeRef
<BorrowType
, K
, V
, GoDownType
>, marker
::Edge
>)
25 pub fn search_tree
<BorrowType
, K
, V
, Q
: ?Sized
>(
26 mut node
: NodeRef
<BorrowType
, K
, V
, marker
::LeafOrInternal
>,
28 ) -> SearchResult
<BorrowType
, K
, V
, marker
::LeafOrInternal
, marker
::Leaf
>
29 where Q
: Ord
, K
: Borrow
<Q
> {
32 match search_node(node
, key
) {
33 Found(handle
) => return Found(handle
),
34 GoDown(handle
) => match handle
.force() {
35 Leaf(leaf
) => return GoDown(leaf
),
36 Internal(internal
) => {
37 node
= internal
.descend();
45 pub fn search_node
<BorrowType
, K
, V
, Type
, Q
: ?Sized
>(
46 node
: NodeRef
<BorrowType
, K
, V
, Type
>,
48 ) -> SearchResult
<BorrowType
, K
, V
, Type
, Type
>
49 where Q
: Ord
, K
: Borrow
<Q
> {
51 match search_linear(&node
, key
) {
53 Handle
::new_kv(node
, idx
)
55 (idx
, false) => SearchResult
::GoDown(
56 Handle
::new_edge(node
, idx
)
61 pub fn search_linear
<BorrowType
, K
, V
, Type
, Q
: ?Sized
>(
62 node
: &NodeRef
<BorrowType
, K
, V
, Type
>,
65 where Q
: Ord
, K
: Borrow
<Q
> {
67 for (i
, k
) in node
.keys().iter().enumerate() {
68 match key
.cmp(k
.borrow()) {
69 Ordering
::Greater
=> {}
,
70 Ordering
::Equal
=> return (i
, true),
71 Ordering
::Less
=> return (i
, false)
74 (node
.keys().len(), false)