1
+ // Copyright 2013 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.
4
+ //
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.
10
+
1
11
//
2
12
// btree.rs
3
13
// Nif Ward
@@ -17,25 +27,25 @@ pub struct BTree<K, V>{
17
27
18
28
19
29
impl < K : Clone + TotalOrd , V : Clone > BTree < K , V > {
20
-
30
+
21
31
//Returns new BTree with root node (leaf) and user-supplied lower bound
22
32
fn new ( k : K , v : V , lb : uint ) -> BTree < K , V > {
23
33
BTree {
24
- root : Node :: new_leaf ( ~[ LeafElt :: new ( k, v) ] ) ,
25
- len : 1 ,
26
- lower_bound : lb,
27
- upper_bound : 2 * lb
34
+ root : Node :: new_leaf ( ~[ LeafElt :: new ( k, v) ] ) ,
35
+ len : 1 ,
36
+ lower_bound : lb,
37
+ upper_bound : 2 * lb
28
38
}
29
39
}
30
40
31
41
//Helper function for clone
32
42
fn new_with_node_len ( n : Node < K , V > , length : uint , lb : uint ) -> BTree < K , V > {
33
43
BTree {
34
- root : n,
35
- len : length,
36
- lower_bound : lb,
37
- upper_bound : 2 * lb
38
- }
44
+ root : n,
45
+ len : length,
46
+ lower_bound : lb,
47
+ upper_bound : 2 * lb
48
+ }
39
49
}
40
50
41
51
@@ -50,11 +60,11 @@ impl<K: Clone + TotalOrd, V: Clone> BTree<K, V>{
50
60
51
61
fn add ( self , k : K , v : V ) -> bool {
52
62
let is_get = & self . clone ( ) . get ( k. clone ( ) ) ;
53
- if is_get. is_some ( ) { return false ; }
54
- else {
55
- std:: util:: replace ( & mut self . root . clone ( ) , self . root . add ( k. clone ( ) , v) ) ;
56
- return true ;
57
- }
63
+ if is_get. is_some ( ) { return false ; }
64
+ else {
65
+ std:: util:: replace ( & mut self . root . clone ( ) , self . root . add ( k. clone ( ) , v) ) ;
66
+ return true ;
67
+ }
58
68
59
69
}
60
70
@@ -66,7 +76,7 @@ impl<K: ToStr + TotalOrd, V: ToStr> ToStr for BTree<K, V>{
66
76
//Returns a string representation of the BTree
67
77
fn to_str ( & self ) -> ~str {
68
78
let ret=self . root . to_str ( ) ;
69
- return ret;
79
+ return ret;
70
80
}
71
81
}
72
82
@@ -83,11 +93,11 @@ impl<K: Clone + TotalOrd, V: Clone> Node<K, V>{
83
93
//differentiates between leaf and branch nodes
84
94
fn is_leaf ( & self ) -> bool {
85
95
match self {
86
- & LeafNode ( * ) => true ,
87
- & BranchNode ( * ) => false
96
+ & LeafNode ( * ) => true ,
97
+ & BranchNode ( * ) => false
88
98
}
89
99
}
90
-
100
+
91
101
//Creates a new leaf or branch node
92
102
fn new_leaf ( vec : ~[ LeafElt < K , V > ] ) -> Node < K , V > {
93
103
LeafNode ( Leaf :: new ( vec) )
@@ -98,8 +108,8 @@ impl<K: Clone + TotalOrd, V: Clone> Node<K, V>{
98
108
99
109
fn get ( & self , k : K ) -> Option < V > {
100
110
match * self {
101
- LeafNode ( ref leaf) => return leaf. get ( k) ,
102
- BranchNode ( ref branch) => return branch. get ( k)
111
+ LeafNode ( ref leaf) => return leaf. get ( k) ,
112
+ BranchNode ( ref branch) => return branch. get ( k)
103
113
}
104
114
}
105
115
@@ -114,9 +124,10 @@ impl<K: Clone + TotalOrd, V: Clone> Node<K, V>{
114
124
impl < K : Clone + TotalOrd , V : Clone > Clone for Node < K , V > {
115
125
fn clone ( & self ) -> Node < K , V > {
116
126
match * self {
117
- LeafNode ( ref leaf) => return Node :: new_leaf ( leaf. elts . clone ( ) ) ,
118
- BranchNode ( ref branch) => return Node :: new_branch ( branch. elts . clone ( ) , branch. rightmost_child . clone ( ) )
119
- }
127
+ LeafNode ( ref leaf) => return Node :: new_leaf ( leaf. elts . clone ( ) ) ,
128
+ BranchNode ( ref branch) => return Node :: new_branch ( branch. elts . clone ( ) ,
129
+ branch. rightmost_child . clone ( ) )
130
+ }
120
131
}
121
132
}
122
133
@@ -125,10 +136,10 @@ impl<K: Clone + TotalOrd, V: Clone> TotalOrd for Node<K, V>{
125
136
fn cmp ( & self , other : & Node < K , V > ) -> Ordering {
126
137
//Requires a match statement--defer these procs to branch and leaf.
127
138
/* if self.elts[0].less_than(other.elts[0]) { return Less}
128
- if self.elts[0].greater_than(other.elts[0]) {return Greater}
129
- else {return Equal}
130
- */
131
- return Equal ;
139
+ if self.elts[0].greater_than(other.elts[0]) {return Greater}
140
+ else {return Equal}
141
+ */
142
+ return Equal ;
132
143
}
133
144
}
134
145
@@ -140,19 +151,19 @@ impl<K: Clone + TotalOrd, V: Clone> TotalEq for Node<K, V>{
140
151
141
152
let mut shorter = 0;
142
153
if self.elts.len() <= other.elts.len(){
143
- shorter = self.elts.len();
144
- }
145
- else{
146
- shorter = other.elts.len();
147
- }
148
- let mut i = 0;
149
- while i < shorter{
150
- if !self.elts[i].has_key(other.elts[i].key){
151
- return false;
152
- }
153
- i +=1;
154
+ shorter = self.elts.len();
155
+ }
156
+ else{
157
+ shorter = other.elts.len();
154
158
}
155
- return true;
159
+ let mut i = 0;
160
+ while i < shorter{
161
+ if !self.elts[i].has_key(other.elts[i].key){
162
+ return false;
163
+ }
164
+ i +=1;
165
+ }
166
+ return true;
156
167
*/
157
168
return true ;
158
169
}
@@ -163,7 +174,7 @@ impl<K: ToStr + TotalOrd, V: ToStr> ToStr for Node<K, V>{
163
174
fn to_str ( & self ) -> ~str {
164
175
match * self {
165
176
LeafNode ( ref leaf) => leaf. to_str ( ) ,
166
- BranchNode ( * ) => ~""
177
+ BranchNode ( * ) => ~""
167
178
}
168
179
}
169
180
}
@@ -192,13 +203,13 @@ impl<K: Clone + TotalOrd, V: Clone> Leaf<K, V>{
192
203
193
204
fn get ( & self , k : K ) -> Option < V > {
194
205
for s in self . elts . iter ( ) {
195
- let order=s. key . cmp ( & k) ;
196
- match order{
197
- Equal => return Some ( s. value . clone ( ) ) ,
198
- _ => { }
199
- }
200
- }
201
- return None ;
206
+ let order=s. key . cmp ( & k) ;
207
+ match order{
208
+ Equal => return Some ( s. value . clone ( ) ) ,
209
+ _ => { }
210
+ }
211
+ }
212
+ return None ;
202
213
}
203
214
204
215
//Add method in progress
@@ -224,22 +235,22 @@ impl<K: Clone + TotalOrd, V: Clone> Branch<K, V>{
224
235
//constructor takes a branch vector and a rightmost child
225
236
fn new ( vec : ~[ BranchElt < K , V > ] , right : ~Node < K , V > ) -> Branch < K , V > {
226
237
Branch {
227
- elts : vec,
228
- rightmost_child : right
238
+ elts : vec,
239
+ rightmost_child : right
229
240
}
230
241
}
231
242
232
243
fn get ( & self , k : K ) -> Option < V > {
233
244
for s in self . elts . iter ( ) {
234
- let order = s. key . cmp ( & k) ;
235
- match order{
236
- Less => return s. left . get ( k) ,
237
- Equal => return Some ( s. value . clone ( ) ) ,
238
- _ => { }
239
- }
240
- }
241
- return self . rightmost_child . get ( k) ;
242
- }
245
+ let order = s. key . cmp ( & k) ;
246
+ match order{
247
+ Less => return s. left . get ( k) ,
248
+ Equal => return Some ( s. value . clone ( ) ) ,
249
+ _ => { }
250
+ }
251
+ }
252
+ return self . rightmost_child . get ( k) ;
253
+ }
243
254
244
255
245
256
//Add method in progress
@@ -265,33 +276,33 @@ impl<K: Clone + TotalOrd, V> LeafElt<K, V>{
265
276
fn new ( k : K , v : V ) -> LeafElt < K , V > {
266
277
LeafElt {
267
278
key : k,
268
- value : v
269
- }
279
+ value : v
280
+ }
270
281
}
271
282
272
283
fn less_than ( & self , other : LeafElt < K , V > ) -> bool {
273
284
let order = self . key . cmp ( & other. key ) ;
274
- match order{
275
- Less => true ,
276
- _ => false
277
- }
285
+ match order{
286
+ Less => true ,
287
+ _ => false
288
+ }
278
289
}
279
290
280
291
fn greater_than ( & self , other : LeafElt < K , V > ) -> bool {
281
292
let order = self . key . cmp ( & other. key ) ;
282
- match order{
283
- Greater => true ,
284
- _ => false
285
- }
293
+ match order{
294
+ Greater => true ,
295
+ _ => false
296
+ }
286
297
}
287
298
288
299
289
300
fn has_key ( & self , other : K ) -> bool {
290
301
let order = self . key . cmp ( & other) ;
291
- match order{
292
- Equal => true ,
293
- _ => false
294
- }
302
+ match order{
303
+ Equal => true ,
304
+ _ => false
305
+ }
295
306
}
296
307
297
308
}
@@ -318,7 +329,7 @@ impl<K: Clone + TotalOrd, V: Clone> BranchElt<K, V>{
318
329
}
319
330
}
320
331
321
- //Add method in progress. Should it return a branch or a leaf elt? It will depend on implementation.
332
+ //Add method in progress. Should it return a branch or a leaf elt?
322
333
fn add ( & self , k : K , v : V ) -> LeafElt < K , V > {
323
334
return LeafElt :: new ( k, v) ;
324
335
}
@@ -330,43 +341,44 @@ impl<K: Clone + TotalOrd, V: Clone> Clone for BranchElt<K, V>{
330
341
}
331
342
}
332
343
333
- #[ test]
334
- fn add_test ( ) {
335
- let b = BTree :: new ( 1 , ~"abc", 2 ) ;
336
- let is_add = b. add ( 2 , ~"xyz") ;
337
- assert ! ( is_add) ;
338
-
339
- }
340
-
341
- #[ test]
342
- fn get_test ( ) {
343
- let b = BTree :: new ( 1 , ~"abc", 2 ) ;
344
- let val = b. get ( 1 ) ;
345
- assert_eq ! ( val, Some ( ~"abc"));
346
- }
344
+ #[ cfg( test) ]
345
+ mod test_btree{
347
346
348
- //Testing LeafElt<K, V> functions (less_than, greater_than, and has_key)
349
- #[test]
350
- fn leaf_lt(){
351
- let l1 = LeafElt::new(1, ~" abc");
352
- let l2 = LeafElt::new(2, ~" xyz");
353
- assert!(l1.less_than(l2));
354
- }
347
+ use super :: * ;
355
348
356
- #[test]
357
- fn leaf_gt (){
358
- let l1 = LeafElt ::new(1, ~" abc");
359
- let l2 = LeafElt::new (2, ~" xyz");
360
- assert!(l2.greater_than(l1) );
361
- }
349
+ #[ test]
350
+ fn add_test ( ) {
351
+ let b = BTree :: new ( 1 , ~"abc", 2 ) ;
352
+ let is_add = b . add ( 2 , ~"xyz") ;
353
+ assert ! ( is_add ) ;
354
+ }
362
355
363
- #[test]
364
- fn leaf_hk(){
365
- let l1 = LeafElt::new(1, ~" abc" ) ;
366
- assert!( l1. has_key( 1 ) ) ;
367
- }
356
+ #[ test]
357
+ fn get_test ( ) {
358
+ let b = BTree :: new ( 1 , ~"abc", 2 ) ;
359
+ let val = b. get ( 1 ) ;
360
+ assert_eq ! ( val, Some ( ~"abc"));
361
+ }
368
362
369
- fn main ( ) {
363
+ //Testing LeafElt<K, V> functions (less_than, greater_than, and has_key)
364
+ #[test]
365
+ fn leaf_lt(){
366
+ let l1 = LeafElt::new(1, ~" abc");
367
+ let l2 = LeafElt::new(2, ~" xyz");
368
+ assert!(l1.less_than(l2));
369
+ }
370
370
371
+ #[test]
372
+ fn leaf_gt(){
373
+ let l1 = LeafElt::new(1, ~" abc");
374
+ let l2 = LeafElt::new(2, ~" xyz");
375
+ assert!(l2.greater_than(l1));
376
+ }
371
377
378
+ #[test]
379
+ fn leaf_hk(){
380
+ let l1 = LeafElt::new(1, ~" abc" ) ;
381
+ assert!( l1. has_key( 1 ) ) ;
382
+ }
372
383
}
384
+
0 commit comments