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_LRDE_UFMT_AD_MAXTREE_HH
00029 # define OLENA_LRDE_UFMT_AD_MAXTREE_HH
00030
00031 # include <oln/lrde/ufmt/utils.hh>
00032
00033
00034 # define oln_lrde_ufmt_import_ad_maxtree_typedefs \
00035 typedef typename super::image image; \
00036 typedef typename super::point point; \
00037 typedef typename super::dpoint dpoint; \
00038 typedef typename super::dparent_t dparent_t; \
00039 typedef typename super::dpar_t dpar_t; \
00040 typedef typename super::value value
00041
00042
00043 namespace oln
00044 {
00045
00046 namespace lrde
00047 {
00048
00049 namespace ufmt
00050 {
00051
00052
00053 template <class T>
00054 point2d point_at(const image2d<T>& ima, int i)
00055 {
00056 int len = ima.ncols() + 2 * ima.border();
00057 i += (len + 1) * ima.border();
00058 return point2d((i / len) - ima.border(),
00059 (i % len) - ima.border());
00060 }
00061
00062
00063
00064
00065 template <class I>
00066 struct ad_maxtree
00067 {
00068 typedef I image;
00069 typedef int point;
00070 typedef int dpoint;
00071 typedef oln_value_type(I) value;
00072
00073 typedef oln_point_type(I) point_type;
00074
00075 template <class II>
00076 struct indexed_image
00077 {
00078 indexed_image()
00079 : ima_(0),
00080 buffer(0)
00081 {
00082 }
00083
00084 void init(const II& ima)
00085 {
00086 ima_ = const_cast<II*>(&ima);
00087 buffer = &((*ima_)[point_type()]);
00088 }
00089
00090 void memset_0()
00091 {
00092 T* ptr = ima_->impl()->buffer();
00093 memset(ptr, 0, ima_->impl()->len() * sizeof(int));
00094 }
00095
00096 typedef oln_value_type(II) T;
00097
00098 T& operator[](int i)
00099 {
00100 assert(buffer != 0);
00101 return *(buffer + i);
00102 }
00103
00104 T operator[](int i) const
00105 {
00106 assert(buffer != 0);
00107 return *(buffer + i);
00108 }
00109
00110 int index_of(const point_type& p) const
00111 {
00112 assert(buffer != 0);
00113 return int(&((*ima_)[p]) - buffer);
00114 }
00115
00116 bool hold(const oln_point_type(I)& p) const
00117 {
00118 return this->ima_->hold(p);
00119 }
00120
00121 bool hold(int i) const
00122 {
00123 point_type p = point_at(*ima_, i);
00124 if (index_of(p) != i)
00125 std::cout << p << ' ' << i << std::endl;
00126 assert(index_of(p) == i);
00127 return this->ima_->hold(p);
00128 }
00129
00130 size_t npoints() const
00131 {
00132 return this->ima_.npoints();
00133 }
00134
00135 const II& real() const { return *ima_; }
00136
00137 II* ima_;
00138 T* buffer;
00139 };
00140
00141
00142
00143
00144 typedef typename mute<I, int>::ret dparent_t;
00145 typedef indexed_image<dparent_t> dpar_t;
00146
00147 dparent_t dpar_;
00148 dpar_t dpar;
00149
00150 indexed_image<I> f;
00151
00152
00153
00154
00155 ad_maxtree(const abstract::image<I>& f_)
00156 :
00157 dpar_(f_.size())
00158 {
00159 f.init(f_.exact());
00160 dpar.init(dpar_);
00161 dpar.memset_0();
00162 }
00163
00164 void make_set(const point& x)
00165 {
00166 dpar[x] = 0;
00167 }
00168
00169 bool is_root(const point& x) const
00170 {
00171 return dpar[x] == 0;
00172 }
00173
00174 bool is_level_root(const point& x) const
00175 {
00176 return is_root(x) or f[x + dpar[x]] < f[x];
00177 }
00178
00179 point find_level_root(const point& x)
00180 {
00181 if (is_level_root(x))
00182 return x;
00183 else
00184 {
00185 point lr = find_level_root(x + dpar[x]);
00186 dpar[x] = lr - x;
00187 return lr;
00188 }
00189 }
00190
00191 point anc(point x, value h)
00192 {
00193 while (not is_root(x) and h <= f[x + dpar[x]])
00194 x = find_level_root(x + dpar[x]);
00195 return x;
00196 }
00197
00198 void swap(point& x, point& y)
00199 {
00200 point memo = x;
00201 x = y;
00202 y = memo;
00203 }
00204
00205
00206
00207
00208 const point_type& parent_(const point_type& p) const {
00209 int i = f.index_of(p);
00210 return point_at(i + dpar[i]);
00211 }
00212 const value& f_(const point_type& p) const {
00213 return f.real()[p];
00214 }
00215 const I& f_() const {
00216 return f.real();
00217 }
00218 bool is_root_(const point_type& p) const {
00219 int i = f.index_of(p);
00220 assert(i == dpar.index_of(p));
00221 return is_root(i);
00222 }
00223 bool is_level_root_(const point_type& p) const {
00224 int i = f.index_of(p);
00225 assert(i == dpar.index_of(p));
00226 return is_level_root(i);
00227 }
00228
00229
00230
00231
00232
00233 };
00234
00235
00236
00237 }
00238
00239 }
00240
00241 }
00242
00243
00244 #endif // ! OLENA_LRDE_UFMT_AD_MAXTREE_HH