Skip to content

Commit

Permalink
1. [refactoring] change default Tree/Forest implementation from s…
Browse files Browse the repository at this point in the history
…ingly-linked to fully-linked

2. [bug fix] assign correct `Size` in fully-linked `Forest::from()`
3. [refactoring] rename mod name `bottom_up` to `linked`, `basic`/`full` to `singly`/`fully`
  • Loading branch information
oooutlk committed Aug 24, 2018
1 parent 0bc1f9b commit fcc4b78
Show file tree
Hide file tree
Showing 21 changed files with 71 additions and 71 deletions.
6 changes: 0 additions & 6 deletions src/bottom_up/mod.rs

This file was deleted.

4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,5 +228,5 @@ mod rust {
#[cfg(feature="no_std")] pub(crate) use core::ptr::{null,null_mut};
}

pub mod bottom_up;
pub use bottom_up::{tr,fr,Tree,Forest,Node,Iter,IterMut,Subnode,OntoIter,Visit,TreeWalk,ForestWalk};
pub mod linked;
pub use linked::{tr,fr,Tree,Forest,Node,Iter,IterMut,Subnode,OntoIter,Visit,TreeWalk,ForestWalk};
26 changes: 13 additions & 13 deletions src/bottom_up/full/forest.rs → src/linked/fully/forest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ pub struct Forest<T> {

impl<T> Forest<T> {
/// Makes an empty `Forest`.
#[inline] pub fn new() -> Forest<T> { Self::from( null_mut() )}
#[inline] pub fn new() -> Forest<T> { Self::from( null_mut(), Size{ degree: 0, node_cnt: 0, })}

/// Returns the number of subtrees in `Forest`.
/// # Examples
///
/// ```
/// use trees::bottom_up::full::tr;
/// use trees::linked::fully::tr;
/// let forest = tr(0) - tr(1)/tr(2)/tr(3) - tr(4)/tr(5)/tr(6);
/// assert_eq!( forest.degree(), 3 );
/// ```
Expand All @@ -28,7 +28,7 @@ impl<T> Forest<T> {
/// # Examples
///
/// ```
/// use trees::bottom_up::full::tr;
/// use trees::linked::fully::tr;
/// let forest = tr(0) - tr(1)/tr(2)/tr(3) - tr(4)/tr(5)/tr(6);
/// assert_eq!( forest.node_count(), 7 );
/// ```
Expand All @@ -41,7 +41,7 @@ impl<T> Forest<T> {
/// # Examples
///
/// ```
/// use trees::bottom_up::full::{tr,fr};
/// use trees::linked::fully::{tr,fr};
/// let mut forest = fr();
/// assert!( forest.is_empty() );
/// forest.push_back( tr(1) );
Expand All @@ -52,7 +52,7 @@ impl<T> Forest<T> {
#[inline] pub(crate) fn set_parent( &mut self, parent: *mut Node<T> ) { for child in self.iter_mut() { child.parent = parent; }}

#[inline] pub(crate) fn set_child( &mut self, node: *mut Node<T> ) { self.child = node; }
#[inline] pub(crate) fn from( node: *mut Node<T> ) -> Self { Forest{ child: node, size: Size{ degree: 0, node_cnt: 0 }, mark: PhantomData } }
#[inline] pub(crate) fn from( node: *mut Node<T>, size: Size ) -> Self { Forest{ child: node, size, mark: PhantomData }}
#[inline] pub(crate) fn clear( &mut self ) { self.child = null_mut(); }

#[inline] pub(crate) unsafe fn set_sib( &mut self, prev: *mut Node<T>, next: *mut Node<T> ) {
Expand Down Expand Up @@ -119,7 +119,7 @@ impl<T> Forest<T> {
/// # Examples
///
/// ```
/// use trees::bottom_up::full::tr;
/// use trees::linked::fully::tr;
/// let mut forest = -tr(1)-tr(2);
/// forest.push_front( tr(3) );
/// assert_eq!( forest.to_string(), "( 3 1 2 )" );
Expand All @@ -141,7 +141,7 @@ impl<T> Forest<T> {
/// # Examples
///
/// ```
/// use trees::bottom_up::full::tr;
/// use trees::linked::fully::tr;
/// let mut forest = -tr(1)-tr(2);
/// forest.push_back( tr(3) );
/// assert_eq!( forest.to_string(), "( 1 2 3 )" );
Expand All @@ -164,7 +164,7 @@ impl<T> Forest<T> {
/// # Examples
///
/// ```
/// use trees::bottom_up::full::tr;
/// use trees::linked::fully::tr;
/// let mut forest = -tr(1)-tr(2);
/// assert_eq!( forest.pop_front(), Some( tr(1) ));
/// assert_eq!( forest.to_string(), "( 2 )" );
Expand Down Expand Up @@ -193,7 +193,7 @@ impl<T> Forest<T> {
/// # Examples
///
/// ```
/// use trees::bottom_up::full::tr;
/// use trees::linked::fully::tr;
/// let mut forest = -tr(1)-tr(2);
/// assert_eq!( forest.pop_back(), Some( tr(2) ));
/// assert_eq!( forest.to_string(), "( 1 )" );
Expand Down Expand Up @@ -224,7 +224,7 @@ impl<T> Forest<T> {
/// # Examples
///
/// ```
/// use trees::bottom_up::full::tr;
/// use trees::linked::fully::tr;
/// let mut forest1 = -tr(0)-tr(1);
/// let mut forest2 = -tr(2)-tr(3);
/// forest1.prepend( forest2 );
Expand All @@ -249,7 +249,7 @@ impl<T> Forest<T> {
/// # Examples
///
/// ```
/// use trees::bottom_up::full::tr;
/// use trees::linked::fully::tr;
/// let mut forest1 = -tr(0)-tr(1);
/// let mut forest2 = -tr(2)-tr(3);
/// forest1.append( forest2 );
Expand All @@ -273,7 +273,7 @@ impl<T> Forest<T> {
/// # Examples
///
/// ```
/// use trees::bottom_up::full::tr;
/// use trees::linked::fully::tr;
/// let forest = -tr(1)-tr(2);
/// let mut iter = forest.iter();
/// assert_eq!( iter.next(), Some( tr(1).root() ));
Expand All @@ -293,7 +293,7 @@ impl<T> Forest<T> {
/// # Examples
///
/// ```
/// use trees::bottom_up::full::tr;
/// use trees::linked::fully::tr;
/// let mut forest = -tr(1)-tr(2);
/// for child in forest.iter_mut() { child.data *= 10; }
/// assert_eq!( forest.to_string(), "( 10 20 )" );
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
20 changes: 10 additions & 10 deletions src/bottom_up/full/node.rs → src/linked/fully/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl<T> Node<T> {
/// # Examples
///
/// ```
/// use trees::bottom_up::full::tr;
/// use trees::linked::fully::tr;
/// let tree = tr(0) /( tr(1)/tr(2)/tr(3) ) /( tr(4)/tr(5)/tr(6) );
/// assert_eq!( tree.degree(), 2 );
/// ```
Expand All @@ -74,7 +74,7 @@ impl<T> Node<T> {
/// # Examples
///
/// ```
/// use trees::bottom_up::full::tr;
/// use trees::linked::fully::tr;
/// let tree = tr(0) /( tr(1)/tr(2)/tr(3) ) /( tr(4)/tr(5)/tr(6) );
/// assert_eq!( tree.node_count(), 7 );
/// ```
Expand Down Expand Up @@ -125,7 +125,7 @@ impl<T> Node<T> {
/// # Examples
///
/// ```
/// use trees::bottom_up::basic::tr;
/// use trees::linked::fully::tr;
/// let mut tree = tr(0) /tr(1);
/// tree.push_front( tr(2) );
/// assert_eq!( tree.to_string(), "0( 2 1 )" );
Expand All @@ -149,7 +149,7 @@ impl<T> Node<T> {
/// # Examples
///
/// ```
/// use trees::bottom_up::basic::tr;
/// use trees::linked::fully::tr;
/// let mut tree = tr(0) /tr(1);
/// tree.push_back( tr(2) );
/// assert_eq!( tree.to_string(), "0( 1 2 )" );
Expand All @@ -172,7 +172,7 @@ impl<T> Node<T> {
/// # Examples
///
/// ```
/// use trees::bottom_up::basic::tr;
/// use trees::linked::fully::tr;
/// let mut tree = tr(0) /tr(1)/tr(2);
/// assert_eq!( tree.pop_front(), Some( tr(1) ));
/// assert_eq!( tree.to_string(), "0( 2 )" );
Expand Down Expand Up @@ -201,7 +201,7 @@ impl<T> Node<T> {
/// # Examples
///
/// ```
/// use trees::bottom_up::full::tr;
/// use trees::linked::fully::tr;
/// let mut tree = tr(0) /tr(1)/tr(2);
/// assert_eq!( tree.pop_back(), Some( tr(2) ));
/// assert_eq!( tree.to_string(), "0( 1 )" );
Expand Down Expand Up @@ -231,7 +231,7 @@ impl<T> Node<T> {
/// # Examples
///
/// ```
/// use trees::bottom_up::basic::tr;
/// use trees::linked::fully::tr;
/// let mut tree = tr(0) /tr(1);
/// tree.prepend( -tr(2)-tr(3) );
/// assert_eq!( tree.to_string(), "0( 2 3 1 )" );
Expand All @@ -256,7 +256,7 @@ impl<T> Node<T> {
/// # Examples
///
/// ```
/// use trees::bottom_up::basic::tr;
/// use trees::linked::fully::tr;
/// let mut tree = tr(0) /tr(1);
/// tree.append( -tr(2)-tr(3) );
/// assert_eq!( tree.to_string(), "0( 1 2 3 )" );
Expand All @@ -283,7 +283,7 @@ impl<T> Node<T> {
/// # Examples
///
/// ```
/// use trees::bottom_up::basic::tr;
/// use trees::linked::fully::tr;
/// let tree = tr(0) /tr(1)/tr(2);
/// let mut iter = tree.iter();
/// assert_eq!( iter.next(), Some( tr(1).root() ));
Expand All @@ -303,7 +303,7 @@ impl<T> Node<T> {
/// # Examples
///
/// ```
/// use trees::bottom_up::basic::tr;
/// use trees::linked::fully::tr;
/// let mut tree = tr(0) /tr(1)/tr(2);
/// for child in tree.iter_mut() { child.data *= 10; }
/// assert_eq!( tree.to_string(), "0( 10 20 )" );
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl<'a, T:'a> Subnode<'a,T> {
/// # Examples
///
/// ```
/// use trees::bottom_up::full::tr;
/// use trees::linked::fully::tr;
/// let mut tree = tr(0) /tr(1)/tr(2);
/// for mut sub in tree.onto_iter() { sub.insert_before( tr(3) ); }
/// assert_eq!( tree.to_string(), "0( 3 1 3 2 )" );
Expand All @@ -40,7 +40,7 @@ impl<'a, T:'a> Subnode<'a,T> {
/// # Examples
///
/// ```
/// use trees::bottom_up::full::tr;
/// use trees::linked::fully::tr;
/// let mut tree = tr(0) /tr(1)/tr(2);
/// for mut sub in tree.onto_iter() { sub.insert_after( tr(3) ); }
/// assert_eq!( tree.to_string(), "0( 1 3 2 3 )" );
Expand All @@ -64,7 +64,7 @@ impl<'a, T:'a> Subnode<'a,T> {
///
/// # Examples
/// ```
/// use trees::bottom_up::full::{tr,fr};
/// use trees::linked::fully::{tr,fr};
///
/// let mut forest = -tr(1)-tr(2)-tr(3);
/// //for sub in forest.onto_iter() { sub.depart(); }
Expand Down
4 changes: 2 additions & 2 deletions src/bottom_up/full/tree.rs → src/linked/fully/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ impl<T> Tree<T> {
/// # Examples
///
/// ```
/// use trees::tr;
/// use trees::linked::fully::tr;
/// let mut tree = tr(0) /tr(1)/tr(2);
/// assert_eq!( tree.abandon().to_string(), "( 1 2 )" );
/// assert_eq!( tree, tr(0) );
/// ```
#[inline] pub fn abandon( &mut self ) -> Forest<T> {
let forest = Forest::<T>::from( self.child );
let forest = Forest::<T>::from( self.child, self.size );
self.reset_child();
self.size.degree = 0;
self.size.node_cnt = 1;
Expand Down
File renamed without changes.
6 changes: 6 additions & 0 deletions src/linked/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//! `Tree`/`Forest` for trees built bottom up.

pub mod singly;

pub mod fully;
pub use self::fully::{tr,fr,Tree,Forest,Node,Iter,IterMut,Subnode,OntoIter,Visit,TreeWalk,ForestWalk};
16 changes: 8 additions & 8 deletions src/bottom_up/basic/forest.rs → src/linked/singly/forest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl<T> Forest<T> {
/// # Examples
///
/// ```
/// use trees::bottom_up::basic::{tr,fr};
/// use trees::linked::singly::{tr,fr};
/// let mut forest = fr();
/// assert!( forest.is_empty() );
/// forest.push_back( tr(1) );
Expand Down Expand Up @@ -93,7 +93,7 @@ impl<T> Forest<T> {
/// # Examples
///
/// ```
/// use trees::bottom_up::basic::tr;
/// use trees::linked::singly::tr;
/// let mut forest = -tr(1)-tr(2);
/// forest.push_front( tr(3) );
/// assert_eq!( forest.to_string(), "( 3 1 2 )" );
Expand All @@ -113,7 +113,7 @@ impl<T> Forest<T> {
/// # Examples
///
/// ```
/// use trees::bottom_up::basic::tr;
/// use trees::linked::singly::tr;
/// let mut forest = -tr(1)-tr(2);
/// forest.push_back( tr(3) );
/// assert_eq!( forest.to_string(), "( 1 2 3 )" );
Expand All @@ -134,7 +134,7 @@ impl<T> Forest<T> {
/// # Examples
///
/// ```
/// use trees::bottom_up::basic::tr;
/// use trees::linked::singly::tr;
/// let mut forest = -tr(1)-tr(2);
/// assert_eq!( forest.pop_front(), Some( tr(1) ));
/// assert_eq!( forest.to_string(), "( 2 )" );
Expand All @@ -159,7 +159,7 @@ impl<T> Forest<T> {
/// # Examples
///
/// ```
/// use trees::bottom_up::basic::tr;
/// use trees::linked::singly::tr;
/// let mut forest1 = -tr(0)-tr(1);
/// let mut forest2 = -tr(2)-tr(3);
/// forest1.prepend( forest2 );
Expand All @@ -183,7 +183,7 @@ impl<T> Forest<T> {
/// # Examples
///
/// ```
/// use trees::bottom_up::basic::tr;
/// use trees::linked::singly::tr;
/// let mut forest1 = -tr(0)-tr(1);
/// let mut forest2 = -tr(2)-tr(3);
/// forest1.append( forest2 );
Expand All @@ -206,7 +206,7 @@ impl<T> Forest<T> {
/// # Examples
///
/// ```
/// use trees::bottom_up::basic::tr;
/// use trees::linked::singly::tr;
/// let forest = -tr(1)-tr(2);
/// let mut iter = forest.iter();
/// assert_eq!( iter.next(), Some( tr(1).root() ));
Expand All @@ -226,7 +226,7 @@ impl<T> Forest<T> {
/// # Examples
///
/// ```
/// use trees::bottom_up::basic::tr;
/// use trees::linked::singly::tr;
/// let mut forest = -tr(1)-tr(2);
/// for child in forest.iter_mut() { child.data *= 10; }
/// assert_eq!( forest.to_string(), "( 10 20 )" );
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit fcc4b78

Please sign in to comment.