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 #ifndef MLN_ACCU_MATH_COUNT_HH
00027 # define MLN_ACCU_MATH_COUNT_HH
00028 
00034 
00035 # include <mln/accu/internal/base.hh>
00036 # include <mln/core/concept/meta_accumulator.hh>
00037 
00038 
00039 namespace mln
00040 {
00041 
00042   
00043   namespace accu {
00044     namespace math {
00045       template <typename T> struct count;
00046     }
00047   }
00048 
00049 
00050   
00051 
00052   namespace trait
00053   {
00054 
00055     template <typename T>
00056     struct accumulator_< accu::math::count<T> >
00057     {
00058       typedef accumulator::has_untake::yes    has_untake;
00059       typedef accumulator::has_set_value::yes has_set_value;
00060       typedef accumulator::has_stop::no       has_stop;
00061       typedef accumulator::when_pix::use_none when_pix;
00062     };
00063 
00064   } 
00065 
00066 
00067   namespace accu
00068   {
00069 
00070     namespace meta
00071     {
00072 
00073       namespace math
00074       {
00075 
00077         struct count : public Meta_Accumulator< count >
00078         {
00079           template <typename T>
00080           struct with
00081           {
00082             typedef accu::math::count<T> ret;
00083           };
00084         };
00085 
00086       } 
00087 
00088     } 
00089 
00090 
00091     namespace math
00092     {
00093 
00098       
00099       template <typename T>
00100       struct count : public mln::accu::internal::base< unsigned , count<T> >
00101       {
00102         typedef T argument;
00103 
00104         count();
00105 
00108         void init();
00109         void take(const argument&);
00110         void take(const count<T>& other);
00111 
00112         void untake(const argument&);
00113         void untake(const count<T>& other);
00114 
00116         void set_value(unsigned c);
00118 
00120         unsigned to_result() const;
00121 
00124         bool is_valid() const;
00125 
00126       protected:
00128         unsigned count_;
00129       };
00130 
00131 
00132 
00133 # ifndef MLN_INCLUDE_ONLY
00134 
00135       template <typename T>
00136       inline
00137       count<T>::count()
00138       {
00139         init();
00140       }
00141 
00142       template <typename T>
00143       inline
00144       void
00145       count<T>::init()
00146       {
00147         count_ = 0;
00148       }
00149 
00150       template <typename T>
00151       inline
00152       void
00153       count<T>::take(const argument&)
00154       {
00155         ++count_;
00156       }
00157 
00158       template <typename T>
00159       inline
00160       void
00161       count<T>::untake(const argument&)
00162       {
00163         mln_precondition(count_ > 0);
00164         --count_;
00165       }
00166 
00167       template <typename T>
00168       inline
00169       void
00170       count<T>::take(const count<T>& other)
00171       {
00172         count_ += other.count_;
00173       }
00174 
00175       template <typename T>
00176       inline
00177       void
00178       count<T>::untake(const count<T>& other)
00179       {
00180         mln_precondition(other.count_ <= count_);
00181         count_ -= other.count_;
00182       }
00183 
00184       template <typename T>
00185       inline
00186       unsigned
00187       count<T>::to_result() const
00188       {
00189         return count_;
00190       }
00191 
00192       template <typename T>
00193       inline
00194       void
00195       count<T>::set_value(unsigned c)
00196       {
00197         count_ = c;
00198       }
00199 
00200       template <typename T>
00201       inline
00202       bool
00203       count<T>::is_valid() const
00204       {
00205         return true;
00206       }
00207 
00208 # endif // ! MLN_INCLUDE_ONLY
00209 
00210     } 
00211 
00212   } 
00213 
00214 } 
00215 
00216 
00217 #endif // ! MLN_ACCU_MATH_COUNT_HH