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

Generated on Thu Sep 8 2011 18:31:29 for Milena (Olena) by  doxygen 1.7.1