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

array.hh

00001 // Copyright (C) 2007, 2008, 2009 EPITA Research and Development Laboratory (LRDE)
00002 //
00003 // This file is part of Olena.
00004 //
00005 // Olena is free software: you can redistribute it and/or modify it under
00006 // the terms of the GNU General Public License as published by the Free
00007 // Software Foundation, version 2 of the License.
00008 //
00009 // Olena is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 // General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Olena.  If not, see <http://www.gnu.org/licenses/>.
00016 //
00017 // As a special exception, you may use this file as part of a free
00018 // software project without restriction.  Specifically, if other files
00019 // instantiate templates or use macros or inline functions from this
00020 // file, or you compile this file and link it with other files to produce
00021 // an executable, this file does not by itself cause the resulting
00022 // executable to be covered by the GNU General Public License.  This
00023 // exception does not however invalidate any other reasons why the
00024 // executable file might be covered by the GNU General Public License.
00025 
00026 #ifndef MLN_HISTO_ARRAY_HH
00027 # define MLN_HISTO_ARRAY_HH
00028 
00032 
00033 # include <vector>
00034 # include <algorithm>
00035 
00036 # include <mln/value/set.hh>
00037 
00038 
00039 namespace mln
00040 {
00041 
00042   namespace histo
00043   {
00044 
00045 
00047     template <typename T>
00048     struct array
00049     {
00050       typedef T value;
00051 
00052       array();
00053 
00054       array(const array& other);
00055       array& operator=(const array& other);
00056 
00057       void clear();
00058 
00059       unsigned operator()(const T& v) const;
00060       unsigned& operator()(const T& v);
00061 
00062       const std::vector<unsigned>& vect() const;
00063       const mln::value::set<T>& vset() const;
00064       unsigned operator[](unsigned i) const;
00065       unsigned& operator[](unsigned i);
00066 
00067       unsigned nvalues() const;
00068 
00069     protected:
00070 
00071       const mln::value::set<T>& s_;
00072       std::vector<unsigned> h_;
00073     };
00074 
00075 
00076     template <typename T>
00077     std::ostream& operator<<(std::ostream& ostr, const array<T>& h);
00078 
00079 
00080 
00081 
00082 # ifndef MLN_INCLUDE_ONLY
00083 
00084     template <typename T>
00085     inline
00086     array<T>::array()
00087       : s_(mln::value::set<T>::the()),
00088         h_(s_.nvalues(), 0)
00089     {
00090       clear();
00091     }
00092 
00093     template <typename T>
00094     inline
00095     array<T>::array(const array& other)
00096       : s_(other.s_),
00097         h_(other.h_)
00098     {
00099     }
00100 
00101     template <typename T>
00102     inline
00103     array<T>&
00104     array<T>::operator=(const array& other)
00105     {
00106       if (&other == this)
00107         return *this;
00108       h_ = other.h_;
00109       return *this;
00110     }
00111 
00112     template <typename T>
00113     inline
00114     void
00115     array<T>::clear()
00116     {
00117       std::fill(h_.begin(), h_.end(), 0);
00118     }
00119 
00120     template <typename T>
00121     inline
00122     unsigned
00123     array<T>::operator()(const T& v) const
00124     {
00125       return h_[s_.index_of(v)];
00126     }
00127 
00128     template <typename T>
00129     inline
00130     unsigned&
00131     array<T>::operator()(const T& v)
00132     {
00133       return h_[s_.index_of(v)];
00134     }
00135 
00136     template <typename T>
00137     inline
00138     const mln::value::set<T>&
00139     array<T>::vset() const
00140     {
00141       return s_;
00142     }
00143 
00144     template <typename T>
00145     inline
00146     unsigned
00147     array<T>::operator[](unsigned i) const
00148     {
00149       mln_precondition(i < s_.nvalues());
00150       return h_[i];
00151     }
00152 
00153     template <typename T>
00154     inline
00155     unsigned&
00156     array<T>::operator[](unsigned i)
00157     {
00158       mln_precondition(i < s_.nvalues());
00159       return h_[i];
00160     }
00161 
00162     template <typename T>
00163     inline
00164     const std::vector<unsigned>&
00165     array<T>::vect() const
00166     {
00167       return h_;
00168     }
00169 
00170     template <typename T>
00171     inline
00172     unsigned array<T>::nvalues() const
00173     {
00174       return h_.size();
00175     }
00176 
00177     template <typename T>
00178     inline
00179     std::ostream& operator<<(std::ostream& ostr, const array<T>& h)
00180     {
00181       mln_viter(mln::value::set<T>) v(h.vset());
00182       for_all(v)
00183         if (h(v) != 0)
00184           ostr << v << ':' << h(v) << ' ';
00185       return ostr;
00186     }
00187 
00188 # endif // ! MLN_INCLUDE_ONLY
00189 
00190   } // end of namespace mln::histo
00191 
00192 } // end of namespace mln
00193 
00194 
00195 #endif // ! MLN_HISTO_ARRAY_HH

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