00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 # ifndef shape_node_hpp
00013 # define shape_node_hpp
00014
00015 # include <iostream>
00016 # include <vector>
00017 # define NODE node<CELL>
00018 # define TMPL template<class CELL>
00019
00020 namespace mmx { namespace shape {
00021
00022 template <class _CELL> class node
00023 {
00024 public:
00025 enum NODE_TYPE { LEFT, RIGHT } ;
00026
00027 typedef _CELL CELL;
00028 typedef _CELL Cell;
00029
00030 public:
00031 node(void) ;
00032 node(CELL cl) ;
00033 node(NODE* parent, CELL & cl, NODE_TYPE nodeType, int v=0) ;
00034 node(NODE* left, NODE * right, CELL cl, int v=0) ;
00035
00036 protected:
00037 node(NODE& node) ;
00038
00039 public:
00040 inline void set_cell(const CELL & c) { m_cell = c ; }
00041 inline void set_parent(NODE * n) { m_parent = n ; }
00042
00043 inline void set_leftchild(NODE * n) { m_left = n ; }
00044 inline void set_rightchild(NODE * n) { m_right = n ; }
00045
00046 inline const CELL& get_cell(void) const { return m_cell; }
00047 inline CELL& get_cell(void) { return m_cell ; }
00048
00049
00050
00051 inline NODE_TYPE type(void) const { return m_type ; }
00052 inline int split_dir(void) const { return m_var; }
00053
00054 inline NODE * left (void) { return m_left ; }
00055 inline NODE * right (void) { return m_right ; }
00056 inline NODE * parent(void) { return m_parent ; }
00057
00058 inline const NODE * left (void) const { return m_left ; }
00059 inline const NODE * right (void) const { return m_right ; }
00060 inline const NODE * parent(void) const { return m_parent ; }
00061
00062 int var(void) const {return this->m_var;}
00063 bool is_leaf(void) const;
00064 size_t leaf_distance() const;
00065
00066 public:
00067 CELL m_cell ;
00068
00069 NODE_TYPE m_type ;
00070 int m_var ;
00071
00072 NODE * m_parent ;
00073 NODE * m_left ;
00074 NODE * m_right ;
00075
00076 int depth ;
00077 int index ;
00078 } ;
00079
00080
00081 TMPL NODE::node(void)
00082 {
00083 m_type = LEFT ;
00084 m_var = 0 ;
00085
00086 m_parent = NULL ;
00087 m_left = NULL;
00088 m_right = NULL;
00089
00090 m_cell = NULL ;
00091
00092
00093 depth = 0;
00094 index = 0;
00095 }
00096
00097 TMPL NODE::node(CELL cl)
00098 {
00099 this->m_type = LEFT ;
00100 this->m_var = 0 ;
00101
00102 this->m_parent = NULL ;
00103 this->m_left = NULL;
00104 this->m_right = NULL;
00105
00106 this->m_cell = cl ;
00107
00108
00109 depth = 0;
00110 index = 0;
00111 }
00112
00113 TMPL NODE::node(NODE * left, NODE * right, CELL cl, int v) {
00114
00115 this->m_parent = NULL ;
00116 this->m_cell = cl ;
00117 this->m_var = v ;
00118
00119 left->type = LEFT ; left->parent = this ; m_left = left;
00120 right->type = RIGHT; right->parent = this ; m_right= right;
00121
00122 depth = left->depth-1 ;
00123 }
00124
00125 TMPL NODE::node(NODE * parent, CELL & cl, NODE_TYPE type, int v)
00126 {
00127 this->m_cell = cl ;
00128 this->m_type = type ;
00129 this->m_var = v ;
00130 this->m_parent = parent ;
00131
00132 this->m_left = NULL;
00133 this->m_right = NULL;
00134
00135 depth = parent->depth+1 ;
00136
00137 switch(type) {
00138 case LEFT : parent->set_leftchild(this) ; break ;
00139 case RIGHT: parent->set_rightchild(this) ; break ;
00140 default: std::cerr << "Error : the node's type isn't appropriate \n" ; break ;
00141 }
00142 }
00143
00144 TMPL bool NODE::is_leaf(void) const
00145 {
00146 if((m_left == NULL) && (m_right == NULL) )
00147 return true;
00148 return false;
00149 }
00150
00151 TMPL size_t NODE::leaf_distance(void) const
00152 {
00153 if ( this->is_leaf() )
00154 return 0;
00155
00156 struct inner {
00157 size_t operator()( const NODE* node ) {
00158 if ( node == 0 )
00159 return 0;
00160 else
00161 return node->leaf_distance();
00162 }
00163 } I;
00164
00165 size_t d = 0;
00166 d = std::min( d, I(m_left) );
00167 d = std::min( d, I(m_right) );
00168
00169 return d+1;
00170 }
00171
00172 TMPL
00173 bool is_adjacent(NODE* n1, NODE* n2) {
00174 return n1->get_cell()->is_adjacent(n2->get_cell());
00175 }
00176
00177 } ;
00178 } ;
00179
00180 # undef NODE
00181 # undef TMPL
00182 # endif // shape_node_hpp