Skip to content

Commit

Permalink
1. [refactoring] rename sib to next in Node definition.
Browse files Browse the repository at this point in the history
  • Loading branch information
oooutlk committed Aug 21, 2018
1 parent 8d52b37 commit bdd84f9
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 26 deletions.
10 changes: 5 additions & 5 deletions src/bottom_up/basic/forest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@ impl<T> Forest<T> {
#[inline] pub(crate) fn clear( &mut self ) { self.sub = null_mut(); }

#[inline] pub(crate) unsafe fn set_sib( &mut self, sib: *mut Node<T> ) {
(*self.tail()).sib = sib;
(*self.tail()).next = sib;
}

#[inline] pub(crate) unsafe fn head ( &self ) -> *mut Node<T> { (*self.sub).sib }
#[inline] pub(crate) unsafe fn head ( &self ) -> *mut Node<T> { (*self.sub).next }
#[inline] pub(crate) fn tail ( &self ) -> *mut Node<T> { self.sub }
#[inline] pub(crate) unsafe fn new_head( &self ) -> *mut Node<T> { (*self.head()).sib }
#[inline] pub(crate) unsafe fn new_head( &self ) -> *mut Node<T> { (*self.head()).next }

#[inline] pub(crate) unsafe fn has_only_one_child( &self ) -> bool { self.head() == self.tail() }

#[inline] pub(crate) fn adopt( &mut self, child: *mut Node<T> ) {
unsafe {
(*self.tail()).sib = child;
(*self.tail()).next = child;
}
}

Expand Down Expand Up @@ -147,7 +147,7 @@ impl<T> Forest<T> {
if self.has_only_one_child() {
self.clear();
} else {
(*self.tail()).sib = self.new_head();
(*self.tail()).next = self.new_head();
}
(*front).reset_sib();
Some( Tree::from( front ))
Expand Down
2 changes: 1 addition & 1 deletion src/bottom_up/basic/heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ pub type Phantom<T> = PhantomData<Box<Node<T>>>;

pub(crate) fn make_node<T>( data: T ) -> *mut Node<T> {
let mut node = Box::new( Node {
next : null_mut(),
sub : null_mut(),
sib : null_mut(),
data : data,
});
node.reset_sib();
Expand Down
4 changes: 2 additions & 2 deletions src/bottom_up/basic/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl<'a, T:'a> Iterator for Iter<'a, T> {
self.head = if self.head == self.tail {
null()
} else {
(*node).sib
(*node).next
};
Some( &*node )
}}
Expand Down Expand Up @@ -68,7 +68,7 @@ impl<'a, T:'a> Iterator for IterMut<'a, T> {
self.head = if self.head == self.tail {
null_mut()
} else {
(*node).sib
(*node).next
};
Some( &mut *node )
}}
Expand Down
16 changes: 8 additions & 8 deletions src/bottom_up/basic/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::{Tree,Forest,Iter,IterMut,OntoIter};
use rust::*;

pub struct Node<T> {
pub(crate) sib : *mut Node<T>, // next sibling
pub(crate) next : *mut Node<T>, // next sibling
pub(crate) sub : *mut Node<T>, // last child
pub data : T,
}
Expand All @@ -15,15 +15,15 @@ impl<T> Node<T> {
#[inline] pub fn is_leaf( &self ) -> bool { self.sub.is_null() }
#[inline] pub(crate) unsafe fn has_only_one_child( &self ) -> bool { self.head() == self.tail() }

#[inline] pub(crate) fn set_sib( &mut self, sib: *mut Self ) { self.sib = sib; }
#[inline] pub(crate) fn reset_sib( &mut self ) { self.sib = self as *mut Self; }
#[inline] pub(crate) fn has_no_sib( &self ) -> bool { self.sib as *const Self == self as *const Self }
#[inline] pub(crate) fn set_sib( &mut self, sib: *mut Self ) { self.next = sib; }
#[inline] pub(crate) fn reset_sib( &mut self ) { self.next = self as *mut Self; }
#[inline] pub(crate) fn has_no_sib( &self ) -> bool { self.next as *const Self == self as *const Self }

#[inline] pub(crate) unsafe fn head( &self ) -> *mut Self { (*self.sub).sib }
#[inline] pub(crate) unsafe fn head( &self ) -> *mut Self { (*self.sub).next }
#[inline] pub(crate) fn tail( &self ) -> *mut Self { self.sub }
#[inline] pub(crate) unsafe fn new_head( &self ) -> *mut Node<T> { (*self.head()).sib }
#[inline] pub(crate) unsafe fn new_head( &self ) -> *mut Node<T> { (*self.head()).next }

#[inline] pub(crate) unsafe fn adopt( &mut self, child: *mut Node<T> ) { (*self.tail()).sib = child; }
#[inline] pub(crate) unsafe fn adopt( &mut self, child: *mut Node<T> ) { (*self.tail()).next = child; }

/// Returns the first child of the forest,
/// or None if it is empty.
Expand Down Expand Up @@ -126,7 +126,7 @@ impl<T> Node<T> {
if self.has_only_one_child() {
self.reset_child();
} else {
(*self.tail()).sib = self.new_head();
(*self.tail()).next = self.new_head();
}
(*front).reset_sib();
Some( Tree::from( front ))
Expand Down
14 changes: 7 additions & 7 deletions src/bottom_up/basic/onto_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ impl<'a, T:'a> Subnode<'a,T> {
/// ```
#[inline] pub fn insert_before( &mut self, mut sib: Tree<T> ) {
unsafe {
sib.root_mut().sib = self.node as *mut Node<T>;
(*self.prev).sib = sib.root_mut();
sib.root_mut().next = self.node as *mut Node<T>;
(*self.prev).next = sib.root_mut();
}
sib.clear();
}
Expand All @@ -44,8 +44,8 @@ impl<'a, T:'a> Subnode<'a,T> {
/// ```
#[inline] pub fn insert_after( &mut self, mut sib: Tree<T> ) {
unsafe {
(*sib.root_mut()).sib = self.node.sib;
self.node.sib = sib.root_mut();
(*sib.root_mut()).next = self.node.next;
self.node.next = sib.root_mut();
if (*self.psub) == self.node as *mut Node<T> {
*self.psub = sib.root_mut();
}
Expand Down Expand Up @@ -73,7 +73,7 @@ impl<'a, T:'a> Subnode<'a,T> {
self.prev
}
}
(*self.prev).sib = self.node.sib;
(*self.prev).next = self.node.next;
self.node.reset_sib();
Tree::from( self.node as *mut Node<T> )
}
Expand Down Expand Up @@ -107,7 +107,7 @@ impl<'a, T:'a> Iterator for OntoIter<'a,T> {
return None;
}
unsafe {
if (*self.prev).sib != self.next {
if (*self.prev).next != self.next {
self.prev = self.curr; // curr did not depart()-ed
}
}
Expand All @@ -116,7 +116,7 @@ impl<'a, T:'a> Iterator for OntoIter<'a,T> {
if !self.next.is_null() {
let curr = self.next;
unsafe {
self.next = (*curr).sib;
self.next = (*curr).next;
return Some( Subnode{ node: &mut *curr, prev: self.prev, psub: self.psub });
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/bottom_up/basic/walk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct Nodes<T> {

impl<T> Nodes<T> {
/// Only the given node will be visited.
#[inline] fn this( node: *const Node<T> ) -> Self { Nodes{ node, sentinel: unsafe{ (*node).sib }}}
#[inline] fn this( node: *const Node<T> ) -> Self { Nodes{ node, sentinel: unsafe{ (*node).next }}}

/// The given node and all its siblings will be visited.
#[inline] fn sibs( node: *const Node<T> ) -> Self { Nodes{ node, sentinel: node }}
Expand Down Expand Up @@ -151,7 +151,7 @@ impl<T> Walk<T> {
}
Direction::Right => {
if let Some( nodes ) = self.path.last_mut() {
nodes.node = unsafe{ (*nodes.node).sib };
nodes.node = unsafe{ (*nodes.node).next };
if nodes.node == nodes.sentinel {
self.direction = Direction::Up;
continue;
Expand Down Expand Up @@ -207,7 +207,7 @@ impl<T> Walk<T> {
#[inline] fn to_sib( &mut self, n: usize ) -> Option<Visit<T>> {
if let Some( nodes ) = self.path.last_mut() {
for _ in 0..n {
nodes.node = unsafe{ (*nodes.node).sib };
nodes.node = unsafe{ (*nodes.node).next };
if nodes.node == nodes.sentinel {
self.direction = Direction::Up;
return None;
Expand Down

0 comments on commit bdd84f9

Please sign in to comment.