2012-06-24 10:44:24 +00:00
< ? php
abstract class Parentum {
2012-06-25 13:03:14 +00:00
/*---------------------------
This class allows external access to the child - node list .
Use the NodeIterator to iterate through the list since
the numeric array keys might have gaps due to node removals !
2012-07-01 18:04:01 +00:00
Once init () has been called , an AVLTree is used to support the
functions removeChild () and findChild () . init () must be called
before adding any nodes !
2012-06-25 13:03:14 +00:00
---------------------------*/
protected $nodes = array ();
2012-07-01 18:04:01 +00:00
protected $avl = null ;
protected function init () {
#echo "init()";
$this -> nodes = array ();
$this -> avl = new AVLTree ();
}
2012-06-25 13:03:14 +00:00
2012-07-01 18:04:01 +00:00
abstract protected function makeChild ( $args ); // overwriteable child generator; allows to define child type (eg.: admin classes that inherit from base class)
2012-06-25 13:03:14 +00:00
final function getSize () {
return sizeof ( $this -> nodes );
}
final function isEmpty () {
return ( sizeof ( $this -> nodes ) == 0 );
}
final function getIterator () {
return new NodeIterator ( $this -> nodes );
}
final function addChild ( $n ) {
$tmp = sizeof ( $this -> nodes );
2012-07-01 18:04:01 +00:00
$n -> setIdx ( $tmp );
2012-06-25 13:03:14 +00:00
$this -> nodes [] = $n ;
2012-07-01 18:04:01 +00:00
if ( $this -> avl != null ) {
$this -> avl -> insert ( $n );
}
2012-06-25 13:03:14 +00:00
return $tmp ;
}
2012-07-01 18:04:01 +00:00
function removeChild ( $id ) {
if ( $this -> isEmpty ()) {
return null ;
}
2012-06-25 13:03:14 +00:00
2012-07-01 18:04:01 +00:00
if ( $this -> avl == null ) {
return false ;
2012-06-25 13:03:14 +00:00
}
2012-07-01 18:04:01 +00:00
$n = $this -> avl -> remove ( $id );
2012-06-25 13:03:14 +00:00
2012-07-01 18:04:01 +00:00
#echo var_export($n,true);
if ( $n != null ) {
if ( $n -> getIdx () != null ) {
unset ( $this -> nodes [ $n -> getIdx ()]);
2012-06-25 13:03:14 +00:00
}
2012-07-01 18:04:01 +00:00
return $n ;
2012-06-25 13:03:14 +00:00
}
return null ;
}
2012-07-01 18:04:01 +00:00
final function getChildByID ( $id ) {
if ( $this -> isEmpty ()) {
return null ;
}
2012-06-25 13:03:14 +00:00
2012-07-01 18:04:01 +00:00
if ( $this -> avl == null ) {
return false ;
2012-06-25 13:03:14 +00:00
}
2012-07-01 18:04:01 +00:00
#$this->avl->inorder();
return $this -> avl -> find ( $id );
}
final function getChildByIdx ( $idx ) {
if ( $this -> isEmpty ()) {
return null ;
}
return $this -> nodes [ $idx ];
2012-06-25 13:03:14 +00:00
}
2012-06-24 10:44:24 +00:00
}
?>