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

array.hh

00001 // Copyright (C) 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_FUN_I2V_ARRAY_HH
00027 # define MLN_FUN_I2V_ARRAY_HH
00028 
00032 
00033 # include <vector>
00034 # include <algorithm>
00035 # include <mln/core/concept/function.hh>
00036 # include <mln/util/array.hh>
00037 # include <mln/metal/equal.hh>
00038 # include <mln/tag/init.hh>
00039 
00040 # include <mln/fun/internal/selector.hh>
00041 
00042 namespace mln
00043 {
00044 
00046   namespace fun {
00047     namespace i2v {
00048       template <typename T> class array;
00049     } // end of namespace mln::fun::i2v
00050   } // end of namespace mln::fun
00051 
00052 
00053 
00054   namespace convert
00055   {
00056 
00057     namespace over_load
00058     {
00059 
00060       template <typename T>
00061       inline
00062       void
00063       from_to_(const util::array<T>& from, fun::i2v::array<T>& to);
00064 
00065       template <typename T, typename U>
00066       inline
00067       void
00068       from_to_(const util::array<T>& from, fun::i2v::array<U>& to);
00069 
00070       template <typename T>
00071       inline
00072       void
00073       from_to_(const std::vector<T>& from, fun::i2v::array<T>& to);
00074 
00075       template <typename T, typename U>
00076       inline
00077       void
00078       from_to_(const std::vector<T>& from, fun::i2v::array<U>& to);
00079 
00080 
00081     } // end of namespace mln::convert::over_load
00082 
00083   } // end of namespace mln::convert
00084 
00085 
00086   namespace fun
00087   {
00088 
00089     namespace i2v
00090     {
00091 
00092       template <typename T>
00093       class array
00094         : public fun::internal::selector_from_result_<T, array<T> >::ret
00095       {
00096       public:
00097 
00100         typedef T result;
00101         typedef typename std::vector<T>::reference mutable_result;
00103 
00106 
00108         array();
00110         array(unsigned n);
00112         array(unsigned n, const T& val);
00113 
00116         array(const util::array<T>& from);
00119         array(const std::vector<T>& from);
00121 
00122 
00124         void reserve(unsigned n);
00125 
00127         void resize(unsigned n);
00130         void resize(unsigned n, const T& val);
00131 
00133         void append(const T& val);
00134 
00136         unsigned size() const;
00137 
00139         result operator()(unsigned i) const;
00141         mutable_result operator()(unsigned i);
00142 
00144         void init_(unsigned n);
00145 
00147         const std::vector<T>& std_vector() const;
00148 
00149       protected:
00150         std::vector<T> v_;
00151 
00152       };
00153 
00154 
00156       template <typename T>
00157       std::ostream& operator<<(std::ostream& ostr,
00158                                const array<T>& a);
00159 
00160 
00161     } // end of namespace mln::fun::i2v
00162 
00163   } // end of namespace mln::fun
00164 
00165 
00166 
00167 # ifndef MLN_INCLUDE_ONLY
00168 
00169   // Init.
00170 
00171   template <typename T1, typename T2>
00172   void init_(tag::function_t,
00173              fun::i2v::array<T1>&         f,
00174              const fun::i2v::array<T2>&   model)
00175   {
00176     f.init_(model.size());
00177   }
00178 
00179 
00180   // convert::from_to
00181 
00182   namespace convert
00183   {
00184 
00185     namespace over_load
00186     {
00187 
00188       template <typename T>
00189       inline
00190       void
00191       from_to_(const util::array<T>& from, fun::i2v::array<T>& to)
00192       {
00193         to = fun::i2v::array<T>(from);
00194       }
00195 
00196       template <typename T, typename U>
00197       inline
00198       void
00199       from_to_(const util::array<T>& from, fun::i2v::array<U>& to)
00200       {
00201         to.resize(from.nelements());
00202         for (unsigned i = 0; i < from.nelements(); ++i)
00203           to(i) = convert::to<U>(from[i]);
00204       }
00205 
00206       template <typename T>
00207       inline
00208       void
00209       from_to_(const std::vector<T>& from, fun::i2v::array<T>& to)
00210       {
00211         to = fun::i2v::array<T>(from);
00212       }
00213 
00214       template <typename T, typename U>
00215       inline
00216       void
00217       from_to_(const std::vector<T>& from, fun::i2v::array<U>& to)
00218       {
00219         to.resize(from.nelements());
00220         for (unsigned i = 0; i < from.size(); ++i)
00221           to(i) = convert::to<U>(from[i]);
00222       }
00223 
00224 
00225     } // end of namespace mln::convert::over_load
00226 
00227   } // end of namespace mln::convert
00228 
00229 
00230 
00232 
00233   namespace fun
00234   {
00235 
00236     namespace i2v
00237     {
00238 
00239       template <typename T>
00240       inline
00241       array<T>::array()
00242       {
00243       }
00244 
00245       template <typename T>
00246       inline
00247       array<T>::array(unsigned n)
00248         : v_(n)
00249       {
00250       }
00251 
00252       template <typename T>
00253       inline
00254       array<T>::array(unsigned n, const T& val)
00255         : v_(n, val)
00256       {
00257       }
00258 
00259       template <typename T>
00260       inline
00261       array<T>::array(const util::array<T>& from)
00262         : v_(from.std_vector())
00263       {
00264 
00265       }
00266 
00267       template <typename T>
00268       inline
00269       array<T>::array(const std::vector<T>& from)
00270         : v_(from)
00271       {
00272 
00273       }
00274 
00275       template <typename T>
00276       inline
00277       void
00278       array<T>::reserve(unsigned n)
00279       {
00280         v_.reserve(n);
00281       }
00282 
00283       template <typename T>
00284       inline
00285       void
00286       array<T>::resize(unsigned n)
00287       {
00288         v_.resize(n);
00289       }
00290 
00291       template <typename T>
00292       inline
00293       void
00294       array<T>::append(const T& val)
00295       {
00296         v_.push_back(val);
00297       }
00298 
00299       template <typename T>
00300       inline
00301       void
00302       array<T>::resize(unsigned n, const T& val)
00303       {
00304         v_.resize(n, val);
00305       }
00306 
00307       template <typename T>
00308       inline
00309       unsigned
00310       array<T>::size() const
00311       {
00312         return v_.size();
00313       }
00314 
00315       template <typename T>
00316       inline
00317       typename array<T>::result
00318       array<T>::operator()(unsigned i) const
00319       {
00320         mln_precondition(i < v_.size());
00321         return v_[i];
00322       }
00323 
00324       template <typename T>
00325       inline
00326       typename array<T>::mutable_result
00327       array<T>::operator()(unsigned i)
00328       {
00329         mln_precondition(i < v_.size());
00330         return v_[i];
00331       }
00332 
00333       template <typename T>
00334       inline
00335       void
00336       array<T>::init_(unsigned n)
00337       {
00338         v_.resize(n);
00339       }
00340 
00341       template <typename T>
00342       inline
00343       const std::vector<T>&
00344       array<T>::std_vector() const
00345       {
00346         return v_;
00347       }
00348 
00349 
00350       // Operator <<.
00351 
00352       template <typename T>
00353       std::ostream& operator<<(std::ostream& ostr,
00354                                const array<T>& a)
00355       {
00356         ostr << '(';
00357         const unsigned n = a.size();
00358         for (unsigned i = 0; i < n; ++i)
00359         {
00360           ostr << a(i);
00361           if (i != n - 1)
00362             ostr << ", ";
00363         }
00364         ostr << ')';
00365         return ostr;
00366       }
00367 
00368 
00369     } // end of namespace mln::fun::i2v
00370 
00371   } // end of namespace mln::fun
00372 
00373 
00374   template <typename T>
00375   inline
00376   fun::i2v::array<T> array(unsigned n, const T& t)
00377   {
00378     fun::i2v::array<T> tmp(n, t);
00379     return tmp;
00380   }
00381 
00382 # endif // ! MLN_INCLUDE_ONLY
00383 
00384 } // end of namespace mln
00385 
00386 
00387 #endif // ! MLN_FUN_I2V_ARRAY_HH

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