• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List

sum.hh

00001 // Copyright (C) 2007, 2008, 2009 EPITA Research and Development
00002 // Laboratory (LRDE)
00003 //
00004 // This file is part of Olena.
00005 //
00006 // Olena is free software: you can redistribute it and/or modify it under
00007 // the terms of the GNU General Public License as published by the Free
00008 // Software Foundation, version 2 of the License.
00009 //
00010 // Olena is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Olena.  If not, see <http://www.gnu.org/licenses/>.
00017 //
00018 // As a special exception, you may use this file as part of a free
00019 // software project 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 produce
00022 // an executable, this file does not by itself cause the resulting
00023 // executable to be covered by the GNU General Public License.  This
00024 // exception does not however invalidate any other reasons why the
00025 // executable file might be covered by the GNU General Public License.
00026 
00027 #ifndef MLN_ACCU_MATH_SUM_HH
00028 # define MLN_ACCU_MATH_SUM_HH
00029 
00033 
00034 # include <mln/core/concept/meta_accumulator.hh>
00035 # include <mln/accu/internal/base.hh>
00036 
00037 # include <mln/util/pix.hh> // To prevent accu::math::sum to work on pixels (ambiguous).
00038 
00039 # include <mln/trait/value_.hh>      // For mln_sum.
00040 # include <mln/value/builtin/all.hh> // In the case of summing builtin values.
00041 # include <mln/literal/zero.hh>      // For initialization.
00042 
00043 
00044 namespace mln
00045 {
00046 
00047   namespace accu
00048   {
00049 
00050     namespace math
00051     {
00052 
00053       // Forward declaration.
00054       template <typename T, typename S>
00055       struct sum;
00056 
00057     } // end of namespace mln::accu::math
00058 
00059     namespace meta
00060     {
00061 
00062       namespace math
00063       {
00064 
00066         struct sum : public Meta_Accumulator< sum >
00067         {
00068           template <typename T, typename S = mln_sum(T)>
00069           struct with
00070           {
00071             typedef accu::math::sum<T, S> ret;
00072           };
00073         };
00074 
00075       } // end of namespace mln::accu::meta::math
00076 
00077     } // end of namespace mln::accu::meta
00078 
00079   } // end of namespace mln::accu
00080 
00081   // Traits.
00082   namespace trait
00083   {
00084 
00085     template <typename T, typename S>
00086     struct accumulator_< accu::math::sum<T,S> >
00087     {
00088       typedef accumulator::has_untake::yes    has_untake;
00089       typedef accumulator::has_set_value::yes has_set_value;
00090       typedef accumulator::has_stop::no       has_stop;
00091       typedef accumulator::when_pix::not_ok   when_pix;
00092     };
00093 
00094   } // end of namespace mln::trait
00095 
00096 
00097   namespace accu
00098   {
00099 
00100     namespace math
00101     {
00102 
00110       //
00111       template <typename T, typename S = mln_sum(T)>
00112       struct sum : public mln::accu::internal::base< const S&, sum<T,S> >
00113       {
00114         typedef T argument;
00115 
00116         sum();
00117 
00120         void init();
00121         void take(const argument& t);
00122         void take_as_init_(const argument& t);
00123         void take(const sum<T,S>& other);
00124 
00125         void untake(const argument& t);
00126         void untake(const sum<T,S>& other);
00127         void set_value(const S& s);
00129 
00131         const S& to_result() const;
00132 
00135         bool is_valid() const;
00136 
00137       protected:
00138 
00139         S s_;
00140       };
00141 
00142 
00143       template <typename I, typename S>
00144       struct sum< util::pix<I>, S >;
00145 
00146 
00147 # ifndef MLN_INCLUDE_ONLY
00148 
00149 
00150 
00151       template <typename T, typename S>
00152       inline
00153       sum<T,S>::sum()
00154       {
00155         init();
00156       }
00157 
00158       template <typename T, typename S>
00159       inline
00160       void
00161       sum<T,S>::init()
00162       {
00163         s_ = literal::zero;
00164       }
00165 
00166       template <typename T, typename S>
00167       inline
00168       void sum<T,S>::take(const argument& t)
00169       {
00170         s_ += static_cast<S>(t);
00171       }
00172 
00173       template <typename T, typename S>
00174       inline
00175       void sum<T,S>::untake(const argument& t)
00176       {
00177         s_ -= static_cast<S>(t);
00178       }
00179 
00180       template <typename T, typename S>
00181       inline
00182       void sum<T,S>::take_as_init_(const argument& t)
00183       {
00184         s_ = static_cast<S>(t);
00185       }
00186 
00187       template <typename T, typename S>
00188       inline
00189       void
00190       sum<T,S>::take(const sum<T,S>& other)
00191       {
00192         s_ += other.s_;
00193       }
00194 
00195       template <typename T, typename S>
00196       inline
00197       void
00198       sum<T,S>::untake(const sum<T,S>& other)
00199       {
00200         s_ -= other.s_;
00201       }
00202 
00203       template <typename T, typename S>
00204       inline
00205       const S&
00206       sum<T,S>::to_result() const
00207       {
00208         return s_;
00209       }
00210 
00211       template <typename T, typename S>
00212       inline
00213       void
00214       sum<T,S>::set_value(const S& s)
00215       {
00216         s_ = s;
00217       }
00218 
00219       template <typename T, typename S>
00220       inline
00221       bool
00222       sum<T,S>::is_valid() const
00223       {
00224         return true;
00225       }
00226 
00227 # endif // ! MLN_INCLUDE_ONLY
00228 
00229     } // end of namespace mln::accu::math
00230 
00231   } // end of namespace mln::accu
00232 
00233 } // end of namespace mln
00234 
00235 
00236 #endif // ! MLN_ACCU_MATH_SUM_HH

Generated on Tue Oct 4 2011 15:24:41 for Milena (Olena) by  doxygen 1.7.1