|
18 | 18 | ///a length (the height of the tree), and lower and upper bounds on the |
19 | 19 | ///number of elements that a given node can contain. |
20 | 20 |
|
21 | | -use std::vec::OwnedVector; |
| 21 | +use std::fmt; |
| 22 | +use std::fmt::Show; |
22 | 23 |
|
23 | 24 | #[allow(missing_doc)] |
24 | 25 | pub struct BTree<K, V> { |
@@ -106,11 +107,10 @@ impl<K: TotalOrd, V: TotalEq> TotalOrd for BTree<K, V> { |
106 | 107 | } |
107 | 108 | } |
108 | 109 |
|
109 | | -impl<K: ToStr + TotalOrd, V: ToStr> ToStr for BTree<K, V> { |
| 110 | +impl<K: fmt::Show + TotalOrd, V: fmt::Show> fmt::Show for BTree<K, V> { |
110 | 111 | ///Returns a string representation of the BTree |
111 | | - fn to_str(&self) -> ~str { |
112 | | - let ret = self.root.to_str(); |
113 | | - ret |
| 112 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 113 | + self.root.fmt(f) |
114 | 114 | } |
115 | 115 | } |
116 | 116 |
|
@@ -235,15 +235,15 @@ impl<K: TotalOrd, V: TotalEq> TotalOrd for Node<K, V> { |
235 | 235 | } |
236 | 236 | } |
237 | 237 |
|
238 | | -impl<K: ToStr + TotalOrd, V: ToStr> ToStr for Node<K, V> { |
| 238 | +impl<K: fmt::Show + TotalOrd, V: fmt::Show> fmt::Show for Node<K, V> { |
239 | 239 | ///Returns a string representation of a Node. |
240 | 240 | ///Will iterate over the Node and show "Key: x, value: y, child: () // " |
241 | 241 | ///for all elements in the Node. "Child" only exists if the Node contains |
242 | 242 | ///a branch. |
243 | | - fn to_str(&self) -> ~str { |
| 243 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
244 | 244 | match *self { |
245 | | - LeafNode(ref leaf) => leaf.to_str(), |
246 | | - BranchNode(ref branch) => branch.to_str() |
| 245 | + LeafNode(ref leaf) => leaf.fmt(f), |
| 246 | + BranchNode(ref branch) => branch.fmt(f), |
247 | 247 | } |
248 | 248 | } |
249 | 249 | } |
@@ -401,10 +401,14 @@ impl<K: TotalOrd, V: TotalEq> TotalOrd for Leaf<K, V> { |
401 | 401 | } |
402 | 402 |
|
403 | 403 |
|
404 | | -impl<K: ToStr + TotalOrd, V: ToStr> ToStr for Leaf<K, V> { |
| 404 | +impl<K: fmt::Show + TotalOrd, V: fmt::Show> fmt::Show for Leaf<K, V> { |
405 | 405 | ///Returns a string representation of a Leaf. |
406 | | - fn to_str(&self) -> ~str { |
407 | | - self.elts.iter().map(|s| s.to_str()).to_owned_vec().connect(" // ") |
| 406 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 407 | + for (i, s) in self.elts.iter().enumerate() { |
| 408 | + if i != 0 { try!(write!(f.buf, " // ")) } |
| 409 | + try!(write!(f.buf, "{}", *s)) |
| 410 | + } |
| 411 | + Ok(()) |
408 | 412 | } |
409 | 413 | } |
410 | 414 |
|
@@ -618,13 +622,14 @@ impl<K: TotalOrd, V: TotalEq> TotalOrd for Branch<K, V> { |
618 | 622 | } |
619 | 623 | } |
620 | 624 |
|
621 | | -impl<K: ToStr + TotalOrd, V: ToStr> ToStr for Branch<K, V> { |
| 625 | +impl<K: fmt::Show + TotalOrd, V: fmt::Show> fmt::Show for Branch<K, V> { |
622 | 626 | ///Returns a string representation of a Branch. |
623 | | - fn to_str(&self) -> ~str { |
624 | | - let mut ret = self.elts.iter().map(|s| s.to_str()).to_owned_vec().connect(" // "); |
625 | | - ret.push_str(" // "); |
626 | | - ret.push_str("rightmost child: ("+ self.rightmost_child.to_str() +") "); |
627 | | - ret |
| 627 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 628 | + for (i, s) in self.elts.iter().enumerate() { |
| 629 | + if i != 0 { try!(write!(f.buf, " // ")) } |
| 630 | + try!(write!(f.buf, "{}", *s)) |
| 631 | + } |
| 632 | + write!(f.buf, " // rightmost child: ({}) ", *self.rightmost_child) |
628 | 633 | } |
629 | 634 | } |
630 | 635 |
|
@@ -672,11 +677,10 @@ impl<K: TotalOrd, V: TotalEq> TotalOrd for LeafElt<K, V> { |
672 | 677 | } |
673 | 678 | } |
674 | 679 |
|
675 | | -impl<K: ToStr + TotalOrd, V: ToStr> ToStr for LeafElt<K, V> { |
| 680 | +impl<K: fmt::Show + TotalOrd, V: fmt::Show> fmt::Show for LeafElt<K, V> { |
676 | 681 | ///Returns a string representation of a LeafElt. |
677 | | - fn to_str(&self) -> ~str { |
678 | | - format!("Key: {}, value: {};", |
679 | | - self.key.to_str(), self.value.to_str()) |
| 682 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 683 | + write!(f.buf, "Key: {}, value: {};", self.key, self.value) |
680 | 684 | } |
681 | 685 | } |
682 | 686 |
|
@@ -715,12 +719,12 @@ impl<K: TotalOrd, V: TotalEq> TotalOrd for BranchElt<K, V> { |
715 | 719 | } |
716 | 720 | } |
717 | 721 |
|
718 | | -impl<K: ToStr + TotalOrd, V: ToStr> ToStr for BranchElt<K, V> { |
719 | | - ///Returns string containing key, value, and child (which should recur to a leaf) |
720 | | - ///Consider changing in future to be more readable. |
721 | | - fn to_str(&self) -> ~str { |
722 | | - format!("Key: {}, value: {}, (child: {})", |
723 | | - self.key.to_str(), self.value.to_str(), self.left.to_str()) |
| 722 | +impl<K: fmt::Show + TotalOrd, V: fmt::Show> fmt::Show for BranchElt<K, V> { |
| 723 | + /// Returns string containing key, value, and child (which should recur to a |
| 724 | + /// leaf) Consider changing in future to be more readable. |
| 725 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 726 | + write!(f.buf, "Key: {}, value: {}, (child: {})", |
| 727 | + self.key, self.value, *self.left) |
724 | 728 | } |
725 | 729 | } |
726 | 730 |
|
|
0 commit comments