Skip to content

Commit

Permalink
1. [feature] Add some imports and functions, preparing for potted::cu…
Browse files Browse the repository at this point in the history
…rsor mod.

2. [bug] Add potted::tests::test_grow() to produce a bug related to Vec reallocation. Need refactoring using indexed crate.
3. [bug] Add missing TreeData impls for i64 and u64.
4. [bug] Fix lots of imports for no_std.
  • Loading branch information
oooutlk committed Oct 25, 2018
1 parent e6311bd commit 6aafa33
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 18 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.*.swo
.*.swp
.swp
/target
**/*.rs.bk
Cargo.lock
18 changes: 11 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,32 +327,36 @@ mod rust {
#[cfg(not(feature="no_std"))] extern crate core;
#[cfg(not(feature="no_std"))] pub(crate) use std::borrow::{Borrow,BorrowMut};
#[cfg(not(feature="no_std"))] pub(crate) use std::boxed::Box;
#[cfg(not(feature="no_std"))] pub(crate) use std::cell::RefCell;
#[cfg(not(feature="no_std"))] pub(crate) use std::collections::VecDeque;
#[cfg(not(feature="no_std"))] pub(crate) use std::cmp::Ordering::{self,*};
#[cfg(not(feature="no_std"))] pub(crate) use std::fmt;
#[cfg(not(feature="no_std"))] pub(crate) use std::fmt::{Debug,Display,Formatter};
#[cfg(not(feature="no_std"))] pub(crate) use std::hash::{Hasher,Hash};
#[cfg(not(feature="no_std"))] pub(crate) use std::iter::{Iterator,FromIterator,IntoIterator,FusedIterator};
#[cfg(not(feature="no_std"))] pub(crate) use std::marker::PhantomData;
#[cfg(not(feature="no_std"))] pub(crate) use std::mem;
#[cfg(not(feature="no_std"))] pub(crate) use std::ops::{Deref,DerefMut,Div,Neg,Sub};
#[cfg(not(feature="no_std"))] pub(crate) use std::ptr::{self,null,null_mut};
#[cfg(not(feature="no_std"))] pub(crate) use std::mem::{self,transmute};
#[cfg(not(feature="no_std"))] pub(crate) use std::ops::{Add,AddAssign,Deref,DerefMut,Div,Neg,Sub,SubAssign};
#[cfg(not(feature="no_std"))] pub(crate) use std::ptr::{self,NonNull,null,null_mut};
#[cfg(not(feature="no_std"))] pub(crate) use std::vec::Vec;

#[cfg(feature="no_std")] extern crate alloc;
#[cfg(feature="no_std")] pub(crate) use self::alloc::borrow::{Borrow,BorrowMut,ToOwned};
#[cfg(feature="no_std")] pub(crate) use self::alloc::boxed::Box;
#[cfg(feature="no_std")] pub(crate) use self::alloc::string::{String,ToString};
#[cfg(feature="no_std")] pub(crate) use self::alloc::collections::VecDeque;
#[cfg(feature="no_std")] pub(crate) use self::alloc::format;
#[cfg(feature="no_std")] pub(crate) use self::alloc::vec::Vec;
#[cfg(feature="no_std")] pub(crate) use core::collections::VecDeque;
#[cfg(feature="no_std")] pub(crate) use core::cell::RefCell;
#[cfg(feature="no_std")] pub(crate) use core::cmp::Ordering::{self,*};
#[cfg(feature="no_std")] pub(crate) use core::fmt;
#[cfg(feature="no_std")] pub(crate) use core::fmt::{Debug,Display,Formatter};
#[cfg(feature="no_std")] pub(crate) use core::hash::{Hasher,Hash};
#[cfg(feature="no_std")] pub(crate) use core::iter::{Iterator,FromIterator,IntoIterator,FusedIterator};
#[cfg(feature="no_std")] pub(crate) use core::marker::PhantomData;
#[cfg(feature="no_std")] pub(crate) use core::mem;
#[cfg(feature="no_std")] pub(crate) use core::ops::{Deref,DerefMut,Div,Neg,Sub};
#[cfg(feature="no_std")] pub(crate) use core::ptr::{self,null,null_mut};
#[cfg(feature="no_std")] pub(crate) use core::mem::{self,transmute};
#[cfg(feature="no_std")] pub(crate) use core::ops::{Add,AddAssign,Deref,DerefMut,Div,Neg,Sub,SubAssign};
#[cfg(feature="no_std")] pub(crate) use core::ptr::{self,NonNull,null,null_mut};
}

pub mod linked;
Expand Down
2 changes: 1 addition & 1 deletion src/potted/forest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::bfs::{Bfs,BfsForest,Splitted,Moved,Visit};

use rust::*;

#[derive(Debug)]
#[derive(Debug,PartialEq,Eq)]
pub struct Forest<T> {
pub(crate) pot : Pot<T>,
}
Expand Down
18 changes: 18 additions & 0 deletions src/potted/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ pub use self::iter::{Iter,IterMut};
pub mod notation;
pub use self::notation::{TreeData,TupleTree,TupleForest,fr};

//pub mod cursor;
//pub use self::cursor::Cursor;

pub use super::bfs;
pub use super::Size;

Expand Down Expand Up @@ -86,4 +89,19 @@ mod tests {
tree.root_mut().drop_back();
assert_eq!( tree.root().to_string(), "_" );
}

#[should_panic]
#[test]
fn test_grow() {
let mut tree: Tree<_> = ( 0, 1, 2 ).into();
{
let mut iter = tree.iter_mut();
let mut first = iter.next().unwrap();
let mut second = iter.next().unwrap();
second.append_fr(( fr(), 3, 4, 5, 6, 7 ));
first.append_fr(( fr(), 8, 9 ));
}
let expected: Tree<_> = ( 0, (1,8,9), (2,3,4,5,6,7,) ).into();
assert_eq!( tree, expected );
}
}
4 changes: 2 additions & 2 deletions src/potted/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl Index for usize {
fn is_null( self ) -> bool { self == 0 }
}

#[derive(Debug)]
#[derive(Debug,PartialEq,Eq)]
pub(crate) struct Node<T> {
pub(crate) next : u32, // next sibling
pub(crate) child : u32, // last child
Expand Down Expand Up @@ -165,7 +165,7 @@ impl<'a, T:'a> NodeMut<'a,T> {

#[inline] pub(crate) fn node( &self ) -> &mut Node<T> { &mut self.pot_mut().nodes[ self.index ]}

#[inline] pub fn node_mut( &self, index: usize ) -> Self { Self{ index, ..*self }}
#[inline] pub(crate) fn node_mut( &self, index: usize ) -> Self { Self{ index, ..*self }}

#[inline] pub fn node_ref( &self ) -> NodeRef<'a,T> { NodeRef{ index: self.index, pot: self.pot, mark: PhantomData }}

Expand Down
4 changes: 2 additions & 2 deletions src/potted/notation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ macro_rules! primitive_impls {
($($name:ty),*) => { $(impl TreeData for $name {})* }
}

primitive_impls!{
bool, i8, u8, i16, u16, i32, u32, i128, u128, isize, usize, f32, f64
primitive_impls! {
bool, i8, u8, i16, u16, i32, u32, i64, u64, i128, u128, isize, usize, f32, f64
}

impl TreeData for &'static str {}
Expand Down
20 changes: 18 additions & 2 deletions src/potted/pot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::{Node,Size,Index};

use rust::*;

#[derive(PartialEq,Eq)]
pub struct Pot<T> {
pub(crate) nodes: Vec<Node<T>>
}
Expand Down Expand Up @@ -90,10 +91,25 @@ impl<T> Pot<T> {
}
}

#[inline] pub(crate) fn prev( &self, index: usize ) -> usize { self.nodes[ index ].prev as usize }
#[inline] pub(crate) fn next( &self, index: usize ) -> usize { self.nodes[ index ].next as usize }

// get the true next sib node, with "forest node" in mind.
#[allow(dead_code)]
// get the actual prev sib node, with "forest node" in mind.
#[inline]
pub(crate) fn prev_sib( &self, index: usize ) -> usize {
let parent = self.parent( index );
if parent.is_null() || !self.is_forest( parent ) { // it is inside a normal node
self.next( index )
} else { // it is inside a forest node
if index == self.head( parent ) {
self.prev( parent )
} else {
self.prev( index )
}
}
}

// get the actual next sib node, with "forest node" in mind.
#[inline]
pub(crate) fn next_sib( &self, index: usize ) -> usize {
let parent = self.parent( index );
Expand Down
2 changes: 1 addition & 1 deletion src/potted/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::bfs::{Bfs,BfsTree,Splitted,Visit,Moved};

use rust::*;

#[derive(Debug)]
#[derive(Debug,PartialEq,Eq)]
pub struct Tree<T> {
pub(crate) pot : Pot<T>,
}
Expand Down
3 changes: 0 additions & 3 deletions src/size.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#[cfg(feature="no_std")]
use rust::*;

/// A struct keeping the node's children count and all its descendants count for resource management purpose.
Expand All @@ -9,8 +8,6 @@ pub struct Size {
pub node_cnt : u32, // count of all nodes, including itself and all its descendants
}

use ::std::ops::{Add,AddAssign,Sub,SubAssign};

impl Add for Size {
type Output = Self;
fn add( self, rhs: Self ) -> Self { Size{ degree: self.degree+rhs.degree, node_cnt: self.node_cnt+rhs.node_cnt }}
Expand Down

0 comments on commit 6aafa33

Please sign in to comment.