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 #ifndef MLN_MAKE_RELABELFUN_HH
00028 # define MLN_MAKE_RELABELFUN_HH
00029 
00033 
00034 # include <mln/core/concept/function.hh>
00035 # include <mln/fun/i2v/array.hh>
00036 # include <mln/value/next.hh>
00037 
00038 namespace mln
00039 {
00040 
00041   namespace make
00042   {
00043 
00054     template <typename V, typename F>
00055     fun::i2v::array<V>
00056     relabelfun(const Function_v2b<F>& fv2b,
00057                const V&               nlabels,
00058                V&                     new_nlabels);
00059 
00071     template <typename V, typename F>
00072     fun::i2v::array<V>
00073     relabelfun(const Function_v2v<F>& fv2v,
00074                const V&               nlabels,
00075                V&                     new_nlabels);
00076 
00077 
00078 # ifndef MLN_INCLUDE_ONLY
00079 
00080     template <typename V, typename F>
00081     inline
00082     fun::i2v::array<V>
00083     relabelfun(const Function_v2b<F>& fv2b_,
00084                const V&               nlabels,
00085                V&                     new_nlabels)
00086     {
00087       trace::entering("make::relabelfun");
00088 
00089       const F& fv2b = exact(fv2b_);
00090 
00091       unsigned nlabels_i = value::next(nlabels);
00092       V tmp_nlabels = literal::zero;
00093       fun::i2v::array<V> fi2v(nlabels_i, literal::zero);
00094       for (V i = 1; i < nlabels_i; ++i)
00095         if (fv2b(i))
00096           fi2v(i) = ++tmp_nlabels;
00097       new_nlabels = tmp_nlabels;
00098       trace::exiting("make::relabelfun");
00099       return fi2v;
00100     }
00101 
00102 
00103     template <typename V, typename F>
00104     inline
00105     fun::i2v::array<V>
00106     relabelfun(const Function_v2v<F>& fv2v_,
00107                const V&               nlabels,
00108                V&                     new_nlabels)
00109     {
00110       trace::entering("make::relabelfun");
00111 
00112       const F& fv2v = exact(fv2v_);
00113 
00114       unsigned nlabels_i = value::next(nlabels);
00115 
00116       util::array<V> new_labels(nlabels_i, mln_max(V));
00117       new_labels(0) = literal::zero;
00118       V tmp_nlabels = literal::zero;
00119       fun::i2v::array<V> fi2v(nlabels_i, mln_max(V));
00120       fi2v(0) = literal::zero;
00121 
00122       for (V i = 1; i < nlabels_i; ++i)
00123       {
00124         mln_assertion(fv2v(i) <= nlabels);
00125         if (new_labels(fv2v(i)) == mln_max(V))
00126         {
00127           new_labels(fv2v(i)) = ++tmp_nlabels;
00128           fi2v(i) = tmp_nlabels;
00129         }
00130         else
00131           fi2v(i) = new_labels(fv2v(i));
00132       }
00133       new_nlabels = tmp_nlabels;
00134       trace::exiting("make::relabelfun");
00135       return fi2v;
00136     }
00137 
00138 # endif // ! MLN_INCLUDE_ONLY
00139 
00140   } 
00141 
00142 } 
00143 
00144 
00145 #endif // ! MLN_MAKE_RELABELFUN_HH