Skip to content

Commit

Permalink
1. [refactoring] rename children()/children_mut() to iter()/iter_mut().
Browse files Browse the repository at this point in the history
  • Loading branch information
oooutlk committed Aug 17, 2018
1 parent cf170e0 commit 1d5705f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 40 deletions.
20 changes: 10 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
//! );
//! ```
//!
//! 3. `Tree` traversal, using `Node::children()` recursively
//! 3. `Tree` traversal, using `Node::iter()` recursively
//!
//! ```rust
//! use std::string::{String,ToString};
Expand All @@ -73,7 +73,7 @@
//! } else {
//! node.data.to_string()
//! + &"( "
//! + &node.children()
//! + &node.iter()
//! .fold( String::new(),
//! |s,c| s + &tree_to_string(c) + &" " )
//! + &")"
Expand Down Expand Up @@ -136,9 +136,9 @@
//! let mut tree: Tree<i32> = tr(0) /tr(1)/tr(2)/tr(3);
//! {
//! let root: &Node<i32> = tree.borrow(); // you can also use tree.root()
//! let first_child : &Node<i32> = tree.children().next().unwrap();
//! let second_child: &Node<i32> = tree.children().nth(1).unwrap();
//! let third_child : &Node<i32> = tree.children().last().unwrap();
//! let first_child : &Node<i32> = tree.iter().next().unwrap();
//! let second_child: &Node<i32> = tree.iter().nth(1).unwrap();
//! let third_child : &Node<i32> = tree.iter().last().unwrap();
//! }
//! let first_child: Tree<i32> = tree.pop_front().unwrap();
//! ```
Expand All @@ -147,23 +147,23 @@
//!
//! The children nodes of a node, or a forest, is conceptually a forward list.
//!
//! 1. Using `children()` to iterate over referenced child `Node`s, you can:
//! 1. Using `iter()` to iterate over referenced child `Node`s, you can:
//!
//! 1.1 read the data associated with each node.
//!
//! 1.2 use `children()` to iterate over children's children, etc.
//! 1.2 use `iter()` to iterate over children's children, etc.
//!
//! 2. Using `children_mut()` to iterate over referenced child `Node`s, you can:
//! 2. Using `iter_mut()` to iterate over referenced child `Node`s, you can:
//!
//! 2.1 read/write the data associated with each node, or `prepend()`, `append`, `abandon()`, `push_front()`, `pop_front()`, `push_back()` child node(s) in constant time.
//!
//! 2.2 use `children_mut()` to iterate over children's children, etc.
//! 2.2 use `iter_mut()` to iterate over children's children, etc.
//!
//! 3. Using `subtrees()` to iterate over `Subtree`s, you can:
//!
//! 3.1 `insert_before`, `insert_after()`, `depart()` node(s) at any position.
//!
//! 3.2 do whatever `children()` or `children_mut()` can do.
//! 3.2 do whatever `iter()` or `iter_mut()` can do.
//!
//! 4. Using `Forest::<T>::into_iter()` to iterate over `Tree`s, you can:
//!
Expand Down
24 changes: 12 additions & 12 deletions src/sib/forest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,12 @@ impl<T> Forest<T> {
/// ```
/// use trees::tr;
/// let forest = -tr(1)-tr(2);
/// let mut iter = forest.children();
/// let mut iter = forest.iter();
/// assert_eq!( iter.next(), Some( tr(1).root() ));
/// assert_eq!( iter.next(), Some( tr(2).root() ));
/// assert_eq!( iter.next(), None );
/// ```
#[inline] pub fn children<'a>( &self ) -> Iter<'a,T> {
#[inline] pub fn iter<'a>( &self ) -> Iter<'a,T> {
if self.is_empty() {
Iter::new( null(), null() )
} else { unsafe {
Expand All @@ -228,10 +228,10 @@ impl<T> Forest<T> {
/// ```
/// use trees::tr;
/// let mut forest = -tr(1)-tr(2);
/// for child in forest.children_mut() { child.data *= 10; }
/// for child in forest.iter_mut() { child.data *= 10; }
/// assert_eq!( forest.to_string(), "( 10 20 )" );
/// ```
#[inline] pub fn children_mut<'a>( &mut self ) -> IterMut<'a,T> {
#[inline] pub fn iter_mut<'a>( &mut self ) -> IterMut<'a,T> {
if self.is_empty() {
IterMut::new( null_mut(), null_mut() )
} else { unsafe {
Expand Down Expand Up @@ -266,7 +266,7 @@ impl<T> Forest<T> {
impl<T:Clone> Clone for Forest<T> {
fn clone( &self ) -> Self {
let mut forest = Forest::<T>::new();
for child in self.children() {
for child in self.iter() {
forest.push_back( child.to_owned() );
}
forest
Expand Down Expand Up @@ -323,7 +323,7 @@ impl<T:Debug> Debug for Forest<T> { fn fmt( &self, f: &mut Formatter ) -> fmt::R
write!( f, "()" )
} else {
write!( f, "( " )?;
for child in self.children() {
for child in self.iter() {
write!( f, "{:?} ", child )?;
}
write!( f, ")" )
Expand All @@ -337,7 +337,7 @@ impl<T:Display> Display for Forest<T> {
write!( f, "()" )
} else {
write!( f, "( " )?;
for child in self.children() {
for child in self.iter() {
write!( f, "{} ", child )?;
}
write!( f, ")" )
Expand All @@ -346,27 +346,27 @@ impl<T:Display> Display for Forest<T> {
}

impl<T:PartialEq> PartialEq for Forest<T> {
fn eq( &self, other: &Self ) -> bool { self.children().eq( other.children() )}
fn ne( &self, other: &Self ) -> bool { self.children().ne( other.children() )}
fn eq( &self, other: &Self ) -> bool { self.iter().eq( other.iter() )}
fn ne( &self, other: &Self ) -> bool { self.iter().ne( other.iter() )}
}

impl<T:Eq> Eq for Forest<T> {}

impl<T:PartialOrd> PartialOrd for Forest<T> {
fn partial_cmp( &self, other: &Self ) -> Option<Ordering> {
self.children().partial_cmp( other.children() )
self.iter().partial_cmp( other.iter() )
}
}

impl<T:Ord> Ord for Forest<T> {
#[inline] fn cmp( &self, other: &Self ) -> Ordering {
self.children().cmp( other.children() )
self.iter().cmp( other.iter() )
}
}

impl<T:Hash> Hash for Forest<T> {
fn hash<H:Hasher>( &self, state: &mut H ) {
for child in self.children() {
for child in self.iter() {
child.hash( state );
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/sib/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use rust::*;

/// An iterator over the sub `Node`s of a `Node` or `Forest`.
///
/// This `struct` is created by [`Node::children`] and [`Forest::children`].
/// This `struct` is created by [`Node::iter`] and [`Forest::iter`].
/// See its document for more.
///
/// [`Node::children`]: struct.Node.html#method.children
/// [`Forest::children`]: struct.Forest.html#method.children
/// [`Node::iter`]: struct.Node.html#method.iter
/// [`Forest::iter`]: struct.Forest.html#method.iter
pub struct Iter<'a, T:'a> {
head : *const Node<T>,
tail : *const Node<T>,
Expand Down Expand Up @@ -46,11 +46,11 @@ impl<'a, T> Clone for Iter<'a, T> {

/// A mutable iterator over the sub `Node`s of a `Node` or `Forest`.
///
/// This `struct` is created by [`Node::children_mut`] and [`Forest::children_mut`].
/// This `struct` is created by [`Node::iter_mut`] and [`Forest::iter_mut`].
/// See its document for more.
///
/// [`Node::children`]: struct.Node.html#method.children_mut
/// [`Forest::children`]: struct.Forest.html#method.children_mut
/// [`Node::iter`]: struct.Node.html#method.iter_mut
/// [`Forest::iter`]: struct.Forest.html#method.iter_mut
pub struct IterMut<'a, T:'a> {
head : *mut Node<T>,
tail : *mut Node<T>,
Expand Down
24 changes: 12 additions & 12 deletions src/sib/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,12 @@ impl<T> Node<T> {
/// ```
/// use trees::sib::tr;
/// let tree = tr(0) /tr(1)/tr(2);
/// let mut iter = tree.children();
/// let mut iter = tree.iter();
/// assert_eq!( iter.next(), Some( tr(1).root() ));
/// assert_eq!( iter.next(), Some( tr(2).root() ));
/// assert_eq!( iter.next(), None );
/// ```
#[inline] pub fn children<'a>( &self ) -> Iter<'a,T> {
#[inline] pub fn iter<'a>( &self ) -> Iter<'a,T> {
if self.is_leaf() {
Iter::new( null(), null() )
} else { unsafe {
Expand All @@ -209,10 +209,10 @@ impl<T> Node<T> {
/// ```
/// use trees::sib::tr;
/// let mut tree = tr(0) /tr(1)/tr(2);
/// for child in tree.children_mut() { child.data *= 10; }
/// for child in tree.iter_mut() { child.data *= 10; }
/// assert_eq!( tree.to_string(), "0( 10 20 )" );
/// ```
#[inline] pub fn children_mut<'a>( &mut self ) -> IterMut<'a,T> {
#[inline] pub fn iter_mut<'a>( &mut self ) -> IterMut<'a,T> {
if self.is_leaf() {
IterMut::new( null_mut(), null_mut() )
} else { unsafe {
Expand Down Expand Up @@ -248,7 +248,7 @@ impl<T:Clone> ToOwned for Node<T> {
type Owned = Tree<T>;
fn to_owned( &self ) -> Self::Owned {
let mut tree = Tree::new( self.data.clone() );
for child in self.children() {
for child in self.iter() {
tree.push_back( child.to_owned() );
}
tree
Expand All @@ -270,7 +270,7 @@ impl<T:Debug> Debug for Node<T> {
} else {
self.data.fmt(f)?;
write!( f, "( " )?;
for child in self.children() {
for child in self.iter() {
write!( f, "{:?} ", child )?;
}
write!( f, ")" )
Expand All @@ -285,7 +285,7 @@ impl<T:Display> Display for Node<T> {
} else {
self.data.fmt(f)?;
write!( f, "( " )?;
for child in self.children() {
for child in self.iter() {
write!( f, "{} ", child )?;
}
write!( f, ")" )
Expand All @@ -294,8 +294,8 @@ impl<T:Display> Display for Node<T> {
}

impl<T:PartialEq> PartialEq for Node<T> {
fn eq( &self, other: &Self ) -> bool { self.data == other.data && self.children().eq( other.children() )}
fn ne( &self, other: &Self ) -> bool { self.data != other.data || self.children().ne( other.children() )}
fn eq( &self, other: &Self ) -> bool { self.data == other.data && self.iter().eq( other.iter() )}
fn ne( &self, other: &Self ) -> bool { self.data != other.data || self.iter().ne( other.iter() )}
}

impl<T:Eq> Eq for Node<T> {}
Expand All @@ -307,7 +307,7 @@ impl<T:PartialOrd> PartialOrd for Node<T> {
Some( order ) => match order {
Less => Some( Less ),
Greater => Some( Greater ),
Equal => self.children().partial_cmp( other.children() ),
Equal => self.iter().partial_cmp( other.iter() ),
},
}
}
Expand All @@ -318,15 +318,15 @@ impl<T:Ord> Ord for Node<T> {
match self.data.cmp( &other.data ) {
Less => Less,
Greater => Greater,
Equal => self.children().cmp( other.children() ),
Equal => self.iter().cmp( other.iter() ),
}
}
}

impl<T:Hash> Hash for Node<T> {
fn hash<H:Hasher>( &self, state: &mut H ) {
self.data.hash( state );
for child in self.children() {
for child in self.iter() {
child.hash( state );
}
}
Expand Down

0 comments on commit 1d5705f

Please sign in to comment.