提交 4a00d4e6 编写于 作者: V Valerii Hiora

TreeMap examples fixes

1. Removed obsolete comment regarding recursive/iteration implementations of tree_find_with/tree_find_mut_with
2. Replaced easy breakable find_with example with simpler one (which only removes redundant allocation during search)
上级 92b5bf86
...@@ -190,33 +190,38 @@ pub fn move_iter(self) -> MoveEntries<K, V> { ...@@ -190,33 +190,38 @@ pub fn move_iter(self) -> MoveEntries<K, V> {
} }
impl<K, V> TreeMap<K, V> { impl<K, V> TreeMap<K, V> {
/// Return the value for which f(key) returns Equal. f is invoked /// Return the value for which `f(key)` returns `Equal`. `f` is invoked
/// with current key and helps to navigate the tree /// with current key and guides tree navigation. That means `f` should
/// be aware of natural ordering of the tree.
/// ///
/// # Example /// # Example
/// ///
/// ``` /// ```
/// use std::ascii::StrAsciiExt; /// use collections::treemap::TreeMap;
/// ///
/// let mut t = collections::treemap::TreeMap::new(); /// fn get_headers() -> TreeMap<String, String> {
/// t.insert("Content-Type", "application/xml"); /// let mut result = TreeMap::new();
/// t.insert("User-Agent", "Curl-Rust/0.1"); /// result.insert("Content-Type".to_string(), "application/xml".to_string());
/// result.insert("User-Agent".to_string(), "Curl-Rust/0.1".to_string());
/// result
/// }
/// ///
/// let ua_key = "user-agent"; /// let headers = get_headers();
/// let ua = t.find_with(|&k| { /// let ua_key = "User-Agent";
/// ua_key.cmp(&k.to_ascii_lower().as_slice()) /// let ua = headers.find_with(|k| {
/// ua_key.cmp(&k.as_slice())
/// }); /// });
/// ///
/// assert_eq!(*ua.unwrap(), "Curl-Rust/0.1"); /// assert_eq!((*ua.unwrap()).as_slice(), "Curl-Rust/0.1");
/// ``` /// ```
#[inline] #[inline]
pub fn find_with<'a>(&'a self, f:|&K| -> Ordering) -> Option<&'a V> { pub fn find_with<'a>(&'a self, f:|&K| -> Ordering) -> Option<&'a V> {
tree_find_with(&self.root, f) tree_find_with(&self.root, f)
} }
/// Return the value for which f(key) returns Equal. f is invoked /// Return the value for which `f(key)` returns `Equal`. `f` is invoked
/// with current key and helps to navigate the tree /// with current key and guides tree navigation. That means `f` should
/// /// be aware of natural ordering of the tree.
/// # Example /// # Example
/// ///
/// ``` /// ```
...@@ -913,14 +918,9 @@ fn split<K: Ord, V>(node: &mut Box<TreeNode<K, V>>) { ...@@ -913,14 +918,9 @@ fn split<K: Ord, V>(node: &mut Box<TreeNode<K, V>>) {
} }
} }
// Next 2 functions have the same conventions // Next 2 functions have the same convention: comparator gets
// // at input current key and returns search_key cmp cur_key
// The only difference is that non-mutable version uses loop instead // (i.e. search_key.cmp(&cur_key))
// of recursion (performance considerations)
// It seems to be impossible to avoid recursion with mutability
//
// So convention is that comparator is gets at input current key
// and returns search_key cmp cur_key (i.e. search_key.cmp(cur_key))
fn tree_find_with<'r, K, V>(node: &'r Option<Box<TreeNode<K, V>>>, fn tree_find_with<'r, K, V>(node: &'r Option<Box<TreeNode<K, V>>>,
f: |&K| -> Ordering) -> Option<&'r V> { f: |&K| -> Ordering) -> Option<&'r V> {
let mut current: &'r Option<Box<TreeNode<K, V>>> = node; let mut current: &'r Option<Box<TreeNode<K, V>>> = node;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册