ivl 679
ivl/details/array_nd/impl/common/array_nd_common_base.hpp
00001 /* This file is part of the ivl C++ library <http://image.ntua.gr/ivl>.
00002    A C++ template library extending syntax towards mathematical notation.
00003 
00004    Copyright (C) 2012 Yannis Avrithis <iavr@image.ntua.gr>
00005    Copyright (C) 2012 Kimon Kontosis <kimonas@image.ntua.gr>
00006 
00007    ivl is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU Lesser General Public License 
00009    version 3 as published by the Free Software Foundation.
00010 
00011    Alternatively, you can redistribute it and/or modify it under the terms 
00012    of the GNU General Public License version 2 as published by the Free 
00013    Software Foundation.
00014 
00015    ivl is distributed in the hope that it will be useful,
00016    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
00018    See the GNU General Public License for more details.
00019 
00020    You should have received a copy of the GNU General Public License 
00021    and a copy of the GNU Lesser General Public License along 
00022    with ivl. If not, see <http://www.gnu.org/licenses/>. */
00023 
00024 #ifndef IVL_ARRAY_ND_DETAILS_ARRAY_ND_COMMON_BASE_HPP
00025 #define IVL_ARRAY_ND_DETAILS_ARRAY_ND_COMMON_BASE_HPP
00026 
00027 namespace ivl {
00028 
00029 template<class T, class OPTS>
00030 class array_common_base<array_nd<T, OPTS> >
00031         :
00032         public array<T, typename types::derive_opts<
00033                 array_nd<T, OPTS> >::type>,
00034 
00035         public array_common_base<common_container_base<T,
00036                 array_nd<T, OPTS> > >
00037 {
00038 private:
00039         typedef array_nd<T, OPTS> t;
00040 
00041         typedef array<T, typename types::
00042                 derive_opts<array_nd<T, OPTS> >::type> prv_base_class;
00043 
00044         typedef array_common_base<common_container_base<T,
00045                 array_nd<T, OPTS> > > prv_common;
00046 
00047         typedef array_common_base this_type;
00048 
00049 protected:
00050         typedef this_type common_base_class;
00051 
00052 public:
00053         typedef t array_nd_type;
00054         typedef t current_type;
00055         typedef t container_type;
00056         typedef t array_nd_or_2d_type;
00057         typedef typename this_type::elem_type elem_type;
00058         typedef prv_base_class base_class;
00059         typedef typename this_type::derived_type derived_type;
00060 
00061         // define _arg_base to solve weird msvc2005 bug
00062         typedef typename prv_base_class::all_init_arg all_init_arg_base;
00063         typedef typename prv_base_class::data_init_arg data_init_arg_base;
00064         typedef all_init_arg_base all_init_arg;
00065         typedef data_init_arg_base data_init_arg;
00066 
00067         t& to_array_nd() { return static_cast<t&>(*this); }
00068         const t& to_array_nd() const { return static_cast<const t&>(*this); }
00069         t& to_current() { return static_cast<t&>(*this); }
00070         const t& to_current() const { return static_cast<const t&>(*this); }
00071 
00072 
00073         t& derived() { return static_cast<derived_type&>(to_array_nd()); }
00074         const t& derived() const { return static_cast<const derived_type&>(to_array_nd()); }
00075 
00076 
00078         array_common_base() { }
00079 
00081         array_common_base(const array_common_base& a) : base_class(a) { }
00082 
00084         array_common_base(all_init_arg y) : base_class(y) { }
00085 
00087         array_common_base(size_t x) : base_class(x) { }
00088 
00090         array_common_base(size_t x, const T& y) : base_class(x, y) { }
00091 
00093         array_common_base(size_t x, const T* y) : base_class(x, y) { }
00094 
00096         array_common_base(size_t x, data_init_arg y) : base_class(x, y) { }
00097 
00099         template<class J, class S>
00100         array_common_base(size_t x, const array<J, S>& y)
00101                 : base_class(x, y) { }
00102 
00104         //
00105         template<class J, class S>
00106         array_common_base(size_t x, array<J, S>& y)
00107                 : base_class(x, y) { }
00108 
00110         template<class J, class S>
00111         array_common_base(const array<J, S>& x) : base_class(x) { }
00112 
00114         template<class J, class S>
00115         array_common_base(array<J, S>& x) : base_class(x) { }
00116 
00117         // Basic array_nd functions
00118 
00119         /*template <class S, class K>
00120         bool isequal(const array_nd<T, S, K>& o)
00121         {
00122                 return ivl::isequal(to_array_nd(), o);
00123         }*/
00124 
00125         // indexing with an array of int instead of size_t!
00126         template<class S>
00127         typename std::iterator_traits<typename types::best_iterator<array_common_base>::type>::reference operator()(
00128                         const array<int, S>& indx)
00129         {
00130                 return derived()(cast<size_t>(indx));
00131         }
00132         template<class S>
00133         typename std::iterator_traits<typename array_common_base::const_iterator>::reference operator() (
00134                         const array<int, S>& indx) const
00135         {
00136                 return derived()(cast<size_t>(indx));
00137         }
00138 
00139 
00140         // group of const subarrays :)
00141         template <class S>
00142         ivl::subarray<const t, array<index_array, S> > operator()
00143                 (const array<index_array, S>& indx) const
00144         {
00145                 return ivl::subarray<const t, array<index_array, S> >(to_array_nd(), indx);
00146         }
00147 
00148         ivl::subarray<const t, array<index_array, fixed<2> > > operator()
00149                 (const index_array& idxn, const index_array& idx2) const
00150         {
00151                 return ivl::subarray<const t, array<index_array, fixed<2> > >(
00152                         to_array_nd(), ivl::arr<index_array>(idxn, idx2));
00153         }
00154 
00155         ivl::subarray<const t, array<index_array, fixed<3> > > operator()
00156                 (const index_array& idxn, const index_array& idx2,
00157                         const index_array& idx3) const
00158         {
00159                 return ivl::subarray<const t, array<index_array, fixed<3> > >(
00160                         to_array_nd(), ivl::arr<index_array>(idxn, idx2, idx3));
00161         }
00162 
00163         ivl::subarray<const t, array<index_array, fixed<4> > > operator()
00164                 (const index_array& idxn, const index_array& idx2,
00165                         const index_array& idx3, const index_array& idx4) const
00166         {
00167                 return ivl::subarray<const t, array<index_array, fixed<4> > >(
00168                         to_array_nd(), ivl::arr<index_array>(idxn, idx2, idx3, idx4));
00169         }
00170 
00171         ivl::subarray<const t, array<index_array, fixed<5> > > operator()
00172                 (const index_array& idxn, const index_array& idx2,
00173                         const index_array& idx3, const index_array& idx4,
00174                         const index_array& idx5) const
00175         {
00176                 return ivl::subarray<const t, array<index_array, fixed<5> > >(
00177                         to_array_nd(), ivl::arr<index_array>(idxn, idx2, idx3, idx4, idx5));
00178         }
00179 
00180         ivl::subarray<const t, array<index_array, fixed<6> > > operator()
00181                 (const index_array& idxn, const index_array& idx2,
00182                         const index_array& idx3, const index_array& idx4,
00183                         const index_array& idx5, const index_array& idx6) const
00184         {
00185                 return ivl::subarray<const t, array<index_array, fixed<6> > >(
00186                         to_array_nd(),
00187                                 ivl::arr<index_array>(idxn, idx2, idx3, idx4, idx5, idx6));
00188         }
00189 
00190         ivl::subarray<const t, array<index_array, fixed<7> > > operator()
00191                 (const index_array& idxn, const index_array& idx2,
00192                         const index_array& idx3, const index_array& idx4,
00193                         const index_array& idx5, const index_array& idx6,
00194                         const index_array& idx7) const
00195         {
00196                 return ivl::subarray<const t, array<index_array, fixed<7> > >(
00197                         to_array_nd(), ivl::arr<index_array>(idxn, idx2, idx3, idx4, idx5,
00198                                                                                                  idx6, idx7));
00199         }
00200 
00201 
00202         // group of non-const subarrays !
00203 
00204         template <class S>
00205         ivl::subarray<t, array<index_array, S> > operator()
00206                 (const array<index_array, S>& indx)
00207         {
00208                 return ivl::subarray<t, array<index_array, S> >(to_array_nd(), indx);
00209         }
00210 
00211         ivl::subarray<t, array<index_array, fixed<2> > > operator()
00212                 (const index_array& idxn, const index_array& idx2)
00213         {
00214                 return ivl::subarray<t, array<index_array, fixed<2> > >(
00215                         to_array_nd(), ivl::arr<index_array>(idxn, idx2));
00216         }
00217 
00218         ivl::subarray<t, array<index_array, fixed<3> > > operator()
00219                 (const index_array& idxn, const index_array& idx2,
00220                         const index_array& idx3)
00221         {
00222                 return ivl::subarray<t, array<index_array, fixed<3> > >(
00223                         to_array_nd(), ivl::arr<index_array>(idxn, idx2, idx3));
00224         }
00225 
00226         ivl::subarray<t, array<index_array, fixed<4> > > operator()
00227                 (const index_array& idxn, const index_array& idx2,
00228                         const index_array& idx3, const index_array& idx4)
00229         {
00230                 return ivl::subarray<t, array<index_array, fixed<4> > >(
00231                         to_array_nd(), ivl::arr<index_array>(idxn, idx2, idx3, idx4));
00232         }
00233 
00234         ivl::subarray<t, array<index_array, fixed<5> > > operator()
00235                 (const index_array& idxn, const index_array& idx2,
00236                         const index_array& idx3, const index_array& idx4,
00237                         const index_array& idx5)
00238         {
00239                 return ivl::subarray<t, array<index_array, fixed<5> > >(
00240                         to_array_nd(), ivl::arr<index_array>(idxn, idx2, idx3, idx4, idx5));
00241         }
00242 
00243         ivl::subarray<t, array<index_array, fixed<6> > > operator()
00244                 (const index_array& idxn, const index_array& idx2,
00245                         const index_array& idx3, const index_array& idx4,
00246                         const index_array& idx5, const index_array& idx6)
00247         {
00248                 return ivl::subarray<t, array<index_array, fixed<6> > >(
00249                         to_array_nd(),
00250                                 ivl::arr<index_array>(idxn, idx2, idx3, idx4, idx5, idx6));
00251         }
00252 
00253         ivl::subarray<t, array<index_array, fixed<7> > > operator()
00254                 (const index_array& idxn, const index_array& idx2,
00255                         const index_array& idx3, const index_array& idx4,
00256                         const index_array& idx5, const index_array& idx6,
00257                         const index_array& idx7)
00258         {
00259                 return ivl::subarray<t, array<index_array, fixed<7> > >(
00260                         to_array_nd(), ivl::arr<index_array>(idxn, idx2, idx3, idx4, idx5,
00261                                                                                                  idx6, idx7));
00262         }
00263         // end of group of subarrays
00264 
00265         // TODO: deprecated
00266         typedef ivl::subarray<t, array<index_array, data::stack<3> > > subarray_type;
00267         typedef ivl::subarray<const t, array<index_array, data::stack<3> > > const_subarray_type;
00268 
00269         typedef subarray_type subbox_type;
00270         typedef const_subarray_type const_subbox_type;
00271 
00272         //
00273         typedef ivl::subarray<t, array<index_array, data::stack<3> > > subarray;
00274         typedef ivl::subarray<const t, array<index_array, data::stack<3> > > const_subarray;
00275 
00276         typedef subarray_type box;
00277         typedef const_subarray_type const_box;
00278         //
00279 
00280 
00281         // -----------------------------------------------------------------------
00282 /*      template<class T2, class S2>
00283         ivl::concat<const derived_type, const array_nd<T2, S2>, 1>
00284         operator|(const array_nd<T2, S2>* r)
00285         {
00286                 return ivl::concat<const derived_type, const array_nd<T2, S2>, 1>
00287                 (derived(), *r);
00288         }
00289 */
00290 #if 0
00291         template<class A2>
00292         ivl::concat<const derived_type, const A2, 1>
00293         operator|(const A2* r)
00294         {
00295                 return ivl::concat<const derived_type, const A2, 1>
00296                 (derived(), *r);
00297         }
00298 
00299         template<class A2>
00300         ivl::concat<const derived_type, const A2, 0>
00301         operator-(const A2* r)
00302         {
00303                 return ivl::concat<const derived_type, const A2, 0>
00304                 (derived(), *r);
00305         }
00306 #endif
00307         /*
00308         template<class T2, class S2>
00309         ivl::concat<const derived_type, const array_nd<T2, S2>, 0>
00310         operator-(const array_nd<T2, S2>* r)
00311         {
00312                 return ivl::concat<const derived_type, const array_nd<T2, S2>, 0>
00313                 (derived(), *r);
00314         }*/
00315 
00316         //propagation of operator =
00317         using base_class::operator=;
00318         array_common_base& operator=(const array_common_base& a)
00319         {
00320                 base_class::operator=(a);
00321                 return *this;
00322         }
00323 
00324         using prv_common::operator-;
00325         using prv_common::operator+;
00326         using prv_common::operator|;
00327         using prv_common::operator~;
00328         using prv_common::operator!;
00329         using prv_common::operator*;
00330         using prv_common::operator->*;
00331 
00332         //note: should not really be implemented for readonly arrays...
00333         template<class S>
00334         void swap(array_nd<T, S>& a)
00335         {
00336                 typedef typename array_nd<T, S>::derived_type::size_type sz_t;
00337                 sz_t sz = a.derived().size_nd();
00338                 types::r_resize(a.derived(), to_array_nd().derived().size_nd());
00339                 types::r_resize(to_array_nd().derived(), sz);
00340         }
00341 
00342 #if 0
00343 // TODO: have to implement
00344 
00345         NewDerived operator()(const gslice& gsl) const;
00346         gslice_array<T> operator()(const gslice& gsl);
00347 
00348         NewDerived operator()(const array<slice>& as) const;
00349         gslice_array<T> operator()(const array<slice>& as);
00350 #endif
00351 
00352 
00353 #ifdef IVL_MATLAB
00354 
00364         derived_type& operator=(const mxArray* mx);
00365 #endif
00366 
00372         std::ostream& print(std::ostream& os) const;
00377 #ifdef IVL_MATLAB
00378 
00379         mxArray* mx() const;
00380 #endif
00381 
00382 };
00383 
00384 } /* namespace ivl */
00385 
00386 #endif // IVL_ARRAY_ND_DETAILS_ARRAY_ND_COMMON_BASE_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations