00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef OLENA_TOPO_COMBINATORIAL_MAP_INTERNAL_LEVEL_HH
00029 # define OLENA_TOPO_COMBINATORIAL_MAP_INTERNAL_LEVEL_HH
00030
00031 # include <iostream>
00032 # include <vector>
00033
00034 namespace oln {
00035
00036 namespace topo {
00037
00038 namespace combinatorial_map {
00039
00040 namespace internal {
00041
00042
00043
00044
00045
00046
00047
00048 template <class U>
00049 struct node
00050 {
00051 node() : fchild(0), lb(0), rb(0), father(0) {}
00052
00053 U fchild, lb, rb, father;
00054 };
00055
00056
00057
00058
00059
00060 template <class U>
00061 class level
00062 {
00063 public:
00064 level(unsigned n) : tree_(n+1)
00065 {
00066 assertion(n);
00067 }
00068
00069 U father(const U & child) const
00070 {
00071 assertion(child < tree_.size());
00072
00073 return tree_[child].father;
00074 }
00075
00076 U children(const U & father) const
00077 {
00078 static unsigned f = 0;
00079 static unsigned last = 0;
00080
00081 assertion(father < tree_.size());
00082
00083 if (f != father)
00084 {
00085 f = father;
00086 last = tree_[f].fchild;
00087 }
00088 else
00089 last = (!last || tree_[last].rb == tree_[f].fchild) ?
00090 0 : tree_[last].rb;
00091
00092 return last;
00093 }
00094
00095 void insert(const U & father, const U & child)
00096 {
00097 node<U> & f = tree_[father];
00098 node<U> & c = tree_[child];
00099
00100 if (!f.lb)
00101 f.lb = f.rb = father;
00102
00103 unsigned fchild = f.fchild;
00104
00105 if (fchild)
00106 {
00107 unsigned tail = tree_[fchild].lb;
00108
00109 tree_[tail].rb = child;
00110 c.lb = tail;
00111 tree_[fchild].lb = child;
00112 c.rb = fchild;
00113 }
00114 else
00115 f.fchild = c.lb = c.rb = child;
00116
00117 c.father = father;
00118 }
00119
00120 void merge(const U & l1, const U & l2)
00121 {
00122 node<U> & a = tree_[l1];
00123 node<U> & b = tree_[l2];
00124
00125
00126 if (tree_[b.father].fchild == l2)
00127 tree_[b.father].fchild = (l2 != b.rb) ? b.rb : 0;
00128
00129
00130 if (b.fchild)
00131 {
00132
00133 unsigned l = 0;
00134 while ((l = children(l2)))
00135 tree_[l].father = l1;
00136
00137 if (a.fchild)
00138 {
00139 tree_[tree_[a.fchild].lb].rb = b.fchild;
00140 tree_[b.fchild].lb = tree_[a.fchild].lb;
00141
00142 tree_[a.fchild].lb = tree_[b.fchild].lb;
00143 tree_[b.fchild].rb = a.fchild;
00144 }
00145 else
00146 a.fchild = b.fchild;
00147 }
00148
00149
00150 tree_[b.rb].lb = b.lb;
00151 tree_[b.lb].rb = b.rb;
00152
00153 b.fchild = b.lb = b.rb = b.father = 0;
00154 }
00155
00156 public:
00157 std::ostream & print(std::ostream & ostr) const
00158 {
00159 for (unsigned i = 1; i < tree_.size(); ++i)
00160 ostr << "father(" << i << ") = " << father(i) << std::endl;
00161
00162 ostr << std::endl;
00163
00164 for (unsigned i = 1; i < tree_.size(); ++i)
00165 {
00166 ostr << "children(" << i << ") = ";
00167
00168 unsigned l = children(i);
00169
00170 if (l)
00171 {
00172 ostr << l;
00173 while ((l = children(i)))
00174 ostr << ", " << l;
00175 ostr << std::endl;
00176 }
00177 else
00178 ostr << "none" << std::endl;
00179 }
00180
00181 return ostr;
00182 }
00183
00184 private:
00185 std::vector< node<U> > tree_;
00186 };
00187
00188 }
00189
00190 }
00191
00192 }
00193
00194 }
00195
00196 template<class U>
00197 inline std::ostream &
00198 operator<<(std::ostream & ostr,
00199 const oln::topo::combinatorial_map::internal::level<U> & l)
00200 {
00201 return l.print(ostr);
00202 }
00203
00204 #endif // ! OLENA_TOPO_COMBINATORIAL_MAP_INTERNAL_LEVEL_HH