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+
111//
212// btree.rs
313// Nif Ward
@@ -17,25 +27,25 @@ pub struct BTree<K, V>{
1727
1828
1929impl < K : Clone + TotalOrd , V : Clone > BTree < K , V > {
20-
30+
2131 //Returns new BTree with root node (leaf) and user-supplied lower bound
2232 fn new ( k : K , v : V , lb : uint ) -> BTree < K , V > {
2333 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
2838 }
2939 }
3040
3141 //Helper function for clone
3242 fn new_with_node_len ( n : Node < K , V > , length : uint , lb : uint ) -> BTree < K , V > {
3343 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+ }
3949 }
4050
4151
@@ -50,11 +60,11 @@ impl<K: Clone + TotalOrd, V: Clone> BTree<K, V>{
5060
5161 fn add ( self , k : K , v : V ) -> bool {
5262 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+ }
5868
5969 }
6070
@@ -66,7 +76,7 @@ impl<K: ToStr + TotalOrd, V: ToStr> ToStr for BTree<K, V>{
6676 //Returns a string representation of the BTree
6777 fn to_str ( & self ) -> ~str {
6878 let ret=self . root . to_str ( ) ;
69- return ret;
79+ return ret;
7080 }
7181}
7282
@@ -83,11 +93,11 @@ impl<K: Clone + TotalOrd, V: Clone> Node<K, V>{
8393 //differentiates between leaf and branch nodes
8494 fn is_leaf ( & self ) -> bool {
8595 match self {
86- & LeafNode ( * ) => true ,
87- & BranchNode ( * ) => false
96+ & LeafNode ( * ) => true ,
97+ & BranchNode ( * ) => false
8898 }
8999 }
90-
100+
91101 //Creates a new leaf or branch node
92102 fn new_leaf ( vec : ~[ LeafElt < K , V > ] ) -> Node < K , V > {
93103 LeafNode ( Leaf :: new ( vec) )
@@ -98,8 +108,8 @@ impl<K: Clone + TotalOrd, V: Clone> Node<K, V>{
98108
99109 fn get ( & self , k : K ) -> Option < V > {
100110 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)
103113 }
104114 }
105115
@@ -114,9 +124,10 @@ impl<K: Clone + TotalOrd, V: Clone> Node<K, V>{
114124impl < K : Clone + TotalOrd , V : Clone > Clone for Node < K , V > {
115125 fn clone ( & self ) -> Node < K , V > {
116126 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+ }
120131 }
121132}
122133
@@ -125,10 +136,10 @@ impl<K: Clone + TotalOrd, V: Clone> TotalOrd for Node<K, V>{
125136 fn cmp ( & self , other : & Node < K , V > ) -> Ordering {
126137 //Requires a match statement--defer these procs to branch and leaf.
127138 /* 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 ;
132143 }
133144}
134145
@@ -140,19 +151,19 @@ impl<K: Clone + TotalOrd, V: Clone> TotalEq for Node<K, V>{
140151
141152 let mut shorter = 0;
142153 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();
154158 }
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;
156167 */
157168 return true ;
158169 }
@@ -163,7 +174,7 @@ impl<K: ToStr + TotalOrd, V: ToStr> ToStr for Node<K, V>{
163174 fn to_str ( & self ) -> ~str {
164175 match * self {
165176 LeafNode ( ref leaf) => leaf. to_str ( ) ,
166- BranchNode ( * ) => ~""
177+ BranchNode ( * ) => ~""
167178 }
168179 }
169180}
@@ -192,13 +203,13 @@ impl<K: Clone + TotalOrd, V: Clone> Leaf<K, V>{
192203
193204 fn get ( & self , k : K ) -> Option < V > {
194205 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 ;
202213 }
203214
204215 //Add method in progress
@@ -224,22 +235,22 @@ impl<K: Clone + TotalOrd, V: Clone> Branch<K, V>{
224235 //constructor takes a branch vector and a rightmost child
225236 fn new ( vec : ~[ BranchElt < K , V > ] , right : ~Node < K , V > ) -> Branch < K , V > {
226237 Branch {
227- elts : vec,
228- rightmost_child : right
238+ elts : vec,
239+ rightmost_child : right
229240 }
230241 }
231242
232243 fn get ( & self , k : K ) -> Option < V > {
233244 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+ }
243254
244255
245256 //Add method in progress
@@ -265,33 +276,33 @@ impl<K: Clone + TotalOrd, V> LeafElt<K, V>{
265276 fn new ( k : K , v : V ) -> LeafElt < K , V > {
266277 LeafElt {
267278 key : k,
268- value : v
269- }
279+ value : v
280+ }
270281 }
271282
272283 fn less_than ( & self , other : LeafElt < K , V > ) -> bool {
273284 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+ }
278289 }
279290
280291 fn greater_than ( & self , other : LeafElt < K , V > ) -> bool {
281292 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+ }
286297 }
287298
288299
289300 fn has_key ( & self , other : K ) -> bool {
290301 let order = self . key . cmp ( & other) ;
291- match order{
292- Equal => true ,
293- _ => false
294- }
302+ match order{
303+ Equal => true ,
304+ _ => false
305+ }
295306 }
296307
297308}
@@ -318,7 +329,7 @@ impl<K: Clone + TotalOrd, V: Clone> BranchElt<K, V>{
318329 }
319330 }
320331
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?
322333 fn add ( & self , k : K , v : V ) -> LeafElt < K , V > {
323334 return LeafElt :: new ( k, v) ;
324335 }
@@ -330,43 +341,44 @@ impl<K: Clone + TotalOrd, V: Clone> Clone for BranchElt<K, V>{
330341 }
331342}
332343
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{
347346
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 :: * ;
355348
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+ }
362355
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+ }
368362
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+ }
370370
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+ }
371377
378+ #[test]
379+ fn leaf_hk(){
380+ let l1 = LeafElt::new(1, ~" abc" ) ;
381+ assert!( l1. has_key( 1 ) ) ;
382+ }
372383}
384+
0 commit comments