ad_maxtree.hh

00001 // Copyright (C) 2006  EPITA Research and Development Laboratory
00002 //
00003 // This file is part of the Olena Library.  This library is free
00004 // software; you can redistribute it and/or modify it under the terms
00005 // of the GNU General Public License version 2 as published by the
00006 // Free Software Foundation.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this library; see the file COPYING.  If not, write to
00015 // the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00016 // Boston, MA 02110-1301, USA.
00017 //
00018 // As a special exception, you may use this file as part of a free
00019 // software library without restriction.  Specifically, if other files
00020 // instantiate templates or use macros or inline functions from this
00021 // file, or you compile this file and link it with other files to
00022 // produce an executable, this file does not by itself cause the
00023 // resulting executable to be covered by the GNU General Public
00024 // License.  This exception does not however invalidate any other
00025 // reasons why the executable file might be covered by the GNU General
00026 // Public License.
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       // FIXME: doc.
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         // aux data
00144         typedef typename mute<I, int>::ret dparent_t;
00145         typedef indexed_image<dparent_t> dpar_t;
00146 
00147         dparent_t dpar_; // proxied
00148         dpar_t dpar;     // proxy
00149         // input
00150         indexed_image<I> f;
00151 
00152 
00153         // ctor
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         // uniformized interface
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         // end of uniformized interface
00230 
00231 
00232 
00233       }; // end of class ad_maxtree
00234 
00235 
00236 
00237     } // end of namespace oln::lrde::ufmt
00238 
00239   } // end of namespace oln::lrde
00240 
00241 } // end of namespace oln
00242 
00243 
00244 #endif // ! OLENA_LRDE_UFMT_AD_MAXTREE_HH

Generated on Tue Feb 20 20:17:41 2007 for Olena by  doxygen 1.5.1