ivl 679
|
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_DETAILS_ARRAY_CORE_FUNCTIONS_HPP 00025 #define IVL_ARRAY_DETAILS_ARRAY_CORE_FUNCTIONS_HPP 00026 00027 namespace ivl { 00028 00029 //-------------------------------------------------------------- 00030 // Auxiliary functions for array creation 00031 00032 // array construction by element values 00033 00038 00039 template <class T> 00040 array<typename types::create_new<T>::type, fixed<1> > arr(const T& p1) 00041 { 00042 return array<typename types::create_new<T>::type, fixed<1> >(1, p1); 00043 } 00044 00046 template <class T, class T1> 00047 array<typename types::create_new<T>::type, fixed<2> > arr(const T& p1, 00048 const T1& p2) 00049 { 00050 array<typename types::create_new<T>::type, fixed<2> > a(2); 00051 a[0] = p1; a[1] = p2; 00052 return a; 00053 } 00054 00056 template <class T, class T1, class T2> 00057 array<typename types::create_new<T>::type, fixed<3> > arr(const T& p1, 00058 const T1& p2, const T2& p3) 00059 { 00060 array<typename types::create_new<T>::type, fixed<3> > a(3); 00061 a[0] = p1; a[1] = p2; a[2] = p3; 00062 return a; 00063 } 00064 00066 template <class T, class T1, class T2, class T3> 00067 array<typename types::create_new<T>::type, fixed<4> > arr(const T& p1, 00068 const T1& p2, const T2& p3, const T3& p4) 00069 { 00070 array<typename types::create_new<T>::type, fixed<4> > a(4); 00071 a[0] = p1; a[1] = p2; a[2] = p3; a[3] = p4; 00072 return a; 00073 } 00074 00076 template <class T, class T1, class T2, class T3, class T4> 00077 array<typename types::create_new<T>::type, fixed<5> > arr(const T& p1, 00078 const T1& p2, const T2& p3, const T3& p4, const T4& p5) 00079 { 00080 array<typename types::create_new<T>::type, fixed<5> > a(5); 00081 a[0] = p1; a[1] = p2; a[2] = p3; a[3] = p4; a[4] = p5; 00082 return a; 00083 } 00084 00086 template <class T, class T1, class T2, class T3, class T4, class T5> 00087 array<typename types::create_new<T>::type, fixed<6> > arr(const T& p1, 00088 const T1& p2, const T2& p3, const T3& p4, const T4& p5, const T5& p6) 00089 { 00090 array<typename types::create_new<T>::type, fixed<6> > a(6); 00091 a[0] = p1; a[1] = p2; a[2] = p3; a[3] = p4; a[4] = p5; a[5] = p6; 00092 return a; 00093 } 00094 00096 template <class T, class T1, class T2, class T3, class T4, class T5, class T6> 00097 array<typename types::create_new<T>::type, fixed<7> > arr(const T& p1, 00098 const T1& p2, const T2& p3, const T3& p4, const T4& p5, 00099 const T5& p6, const T6& p7) 00100 { 00101 array<typename types::create_new<T>::type, fixed<7> > a(7); 00102 a[0] = p1; a[1] = p2; a[2] = p3; a[3] = p4; a[4] = p5; a[5] = p6; a[6] = p7; 00103 return a; 00104 } 00111 /* 00113 inline 00114 size_array idx(size_t p1) 00115 { return arr<size_t>(p1); } 00116 00118 inline 00119 size_array idx(size_t p1, size_t p2) 00120 { return arr<size_t>(p1, p2); } 00121 00123 inline 00124 size_array idx(size_t p1, size_t p2, size_t p3) 00125 { return arr<size_t>(p1, p2, p3); } 00126 00128 inline 00129 size_array idx(size_t p1, size_t p2, size_t p3, size_t p4) 00130 { return arr<size_t>(p1, p2, p3, p4); } 00131 00133 inline 00134 size_array idx(size_t p1, size_t p2, size_t p3, size_t p4, size_t p5) 00135 { return arr<size_t>(p1, p2, p3, p4, p5); } 00136 00138 inline 00139 size_array idx(size_t p1, size_t p2, size_t p3, size_t p4, size_t p5, size_t p6) 00140 { return arr<size_t>(p1, p2, p3, p4, p5, p6); } 00141 */ 00142 00143 00145 inline 00146 array<size_t, fixed<1> > idx(size_t p1) 00147 { 00148 return array<size_t, fixed<1> >(1, p1); 00149 } 00150 00152 inline 00153 array<size_t, fixed<2> > idx(size_t p1, size_t p2) 00154 { 00155 array<size_t, fixed<2> > a; 00156 a[0] = p1; a[1] = p2; 00157 return a; 00158 } 00159 00161 inline 00162 array<size_t, fixed<3> > idx(size_t p1, size_t p2, size_t p3) 00163 { 00164 array<size_t, fixed<3> > a; 00165 a[0] = p1; a[1] = p2; a[2] = p3; 00166 return a; 00167 } 00168 00170 inline 00171 array<size_t, fixed<4> > idx(size_t p1, size_t p2, size_t p3, size_t p4) 00172 { 00173 array<size_t, fixed<4> > a; 00174 a[0] = p1; a[1] = p2; a[2] = p3; a[3] = p4; 00175 return a; 00176 } 00177 00179 inline 00180 array<size_t, fixed<5> > idx(size_t p1, size_t p2, size_t p3, size_t p4, 00181 size_t p5) 00182 { 00183 array<size_t, fixed<5> > a; 00184 a[0] = p1; a[1] = p2; a[2] = p3; a[3] = p4; a[4] = p5; 00185 return a; 00186 } 00187 00189 inline 00190 array<size_t, fixed<6> > idx(size_t p1, size_t p2, size_t p3, size_t p4, 00191 size_t p5, size_t p6) 00192 { 00193 array<size_t, fixed<6> > a; 00194 a[0] = p1; a[1] = p2; a[2] = p3; a[3] = p4; a[4] = p5; a[5] = p6; 00195 return a; 00196 } 00197 00199 inline 00200 array<size_t, fixed<7> > idx(size_t p1, size_t p2, size_t p3, size_t p4, 00201 size_t p5, size_t p6, size_t p7) 00202 { 00203 array<size_t, fixed<7> > a; 00204 a[0] = p1; a[1] = p2; a[2] = p3; a[3] = p4; a[4] = p5; a[5] = p6; a[6] = p7; 00205 return a; 00206 } 00207 00210 //-------------------------------------------------------------- 00211 // array MEMBER FUNCTIONS 00212 00220 template<class T, class S> 00221 typename types::create_new<T>::type 00222 array_common_base<array<T, S> >::min() const 00223 { 00224 typename array_type::const_iterator it = to_array().begin(); 00225 typename array_type::const_iterator end = to_array().end(); 00226 CHECK(it != end, eshape); 00227 T s = *it; 00228 for (; it != end; ++it) 00229 if (*it < s) 00230 s = *it; 00231 return (s); 00232 } 00233 00241 template<class T, class S> 00242 typename types::create_new<T>::type 00243 array_common_base<array<T, S> >::max() const 00244 // Note: developper's notice: if you see `function returning an array' error 00245 // here means that you are either explicitly or implicitly trying to 00246 // initialize an array which elements type is a c array, which is invalid. 00247 { 00248 typename array_type::const_iterator it = to_array().begin(); 00249 typename array_type::const_iterator end = to_array().end(); 00250 CHECK(it != end, eshape); 00251 T s = *it; 00252 for (; it != end; ++it) 00253 if (*it > s) 00254 s = *it; 00255 return (s); 00256 } 00257 00258 00259 //-------------------------------------------------------------- 00260 // array NON-MEMBER FUNCTIONS 00261 00267 00268 template<class T, class S> 00269 typename array<T, S>::create_similar shift(const array<T, S>& in, int count, T val = T()) 00270 { 00271 typename array<T, S>::create_similar out(in.derived().size()); 00272 size_t len = in.length(); 00273 00274 for (size_t i = 0; i < len; i++) { 00275 int j = int(i) - count; 00276 out[i] = (j >= 0 && j < int(len)) ? in[size_t(j)] : val; 00277 } 00278 00279 return out; 00280 } 00281 00283 template<class T, class S> 00284 typename array<T, S>::create_similar cumprod(const array<T, S>& in) 00285 { 00286 typename array<T, S>::create_similar out(in.derived().size()); 00287 size_t len = in.length(); 00288 00289 out[0] = in[0]; 00290 for(size_t i = 1; i < len; i++) 00291 out[i] = in[i] * out[i - 1]; 00292 00293 return out; 00294 } 00295 00297 template<class T, class S> 00298 typename types::promote<T>::type prod(const array<T, S>& in) 00299 { 00300 typedef typename types::promote<T>::type return_type; 00301 return_type s = 1; 00302 for (size_t i = 0; i < in.length(); ++i) 00303 s *= cast<return_type>(in[i]); 00304 return s; 00305 } 00306 00308 template<class S, class D> 00309 inline 00310 size_t sub2ind(const array<size_t, S>& stride, const array<size_t, D>& b) 00311 { 00312 size_t offs = b[0]; 00313 size_t ndims = stride.length(); 00314 00315 for(size_t i = 1; i < ndims; i++) 00316 offs += stride[i] * b[i]; 00317 00318 return offs; 00319 } 00320 00323 } /* namespace ivl */ 00324 00325 #endif // IVL_ARRAY_DETAILS_ARRAY_CORE_FUNCTIONS_HPP