00001 #ifdef CH_LANG_CC
00002
00003
00004
00005
00006
00007
00008
00009 #endif
00010
00011 #ifndef _INDEXTMI_H_
00012 #define _INDEXTMI_H_
00013
00014 #include <iostream>
00015 using std::ostream;
00016 using std::istream;
00017 using std::ws;
00018
00019 #include "MayDay.H"
00020 #include "Misc.H"
00021 #include "IndexTM.H"
00022 #include "parstream.H"
00023 #include <cmath>
00024 #include <algorithm>
00025 #include <functional>
00026 #include <numeric>
00027 #include "Metaprograms.H"
00028
00029 #include "BaseNamespaceHeader.H"
00030
00031 template<typename T, int N> ostream& operator<< (ostream & a_os,
00032 const IndexTM<T,N> & a_p)
00033 {
00034 a_os << '(';
00035
00036 for (int i = 0; i < N; ++i)
00037 {
00038 a_os << a_p[i];
00039
00040 if (i < N-1)
00041 {
00042 a_os << ',';
00043 }
00044 }
00045
00046 a_os << ')';
00047
00048 if (a_os.fail())
00049 {
00050 MayDay::Abort("operator<<(ostream&,Index&) failed");
00051 }
00052
00053 return a_os;
00054 }
00055
00056
00057
00058
00059 #define CH_IGNORE_MAX 100000
00060
00061 template<typename T, int N> istream& operator>> (istream & a_is,
00062 IndexTM<T,N> & a_p)
00063 {
00064 a_is >> ws;
00065
00066 char c;
00067 a_is >> c;
00068 a_is.putback(c);
00069
00070 if (c == '(')
00071 {
00072 a_is.ignore(CH_IGNORE_MAX, '(') >> a_p[0];
00073
00074 for (int i = 1; i < N; ++i)
00075 {
00076 a_is.ignore(CH_IGNORE_MAX, ',') >> a_p[i];
00077 }
00078
00079 a_is.ignore(CH_IGNORE_MAX, ')');
00080 }
00081 else if (c == '<')
00082 {
00083 a_is.ignore(CH_IGNORE_MAX, '<') >> a_p[0];
00084
00085 for (int i = 1; i < N; ++i)
00086 {
00087 a_is.ignore(CH_IGNORE_MAX, ',') >> a_p[1];
00088 }
00089 a_is.ignore(CH_IGNORE_MAX, '>');
00090 }
00091 else
00092 {
00093 MayDay::Abort("operator>>(istream&,Index&): expected \'(\' or \'<\'");
00094 }
00095
00096 if (a_is.fail())
00097 {
00098 MayDay::Abort("operator>>(istream&,Index&) failed");
00099 }
00100
00101 return a_is;
00102 }
00103
00104 template<typename T, int N> void IndexTM<T,N>::printOn (ostream & a_os) const
00105 {
00106 a_os << "Index: " << *this << "\n";
00107 }
00108
00109 template<typename T, int N> void IndexTM<T,N>::p() const
00110 {
00111 pout() << *this << "\n";
00112 }
00113
00114 template<typename T, int N> void IndexTM<T,N>::dumpOn (ostream & a_os) const
00115 {
00116 a_os << "Index " << *this << "\n";
00117 }
00118
00119
00120
00121
00122 template<typename T, int N> int IndexTM<T,N>::InitStatics()
00123 {
00124 IndexTM<T,N>& pz = const_cast<IndexTM<T,N>&>(IndexTM<T,N>::Zero);
00125
00126 pz.setAll(0);
00127
00128 IndexTM<T,N>& pu = const_cast<IndexTM<T,N>&>(IndexTM<T,N>::Unit);
00129
00130 pu.setAll(1);
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140 return 0;
00141 }
00142
00143
00144
00145
00146 template<typename T, int N> inline IndexTM<T,N>::IndexTM(const T *a_a)
00147 : GenericArithmeticable< T, IndexTM<T,N> >(this)
00148 {
00149 memcpy(m_vect, a_a, N*sizeof(T));
00150 }
00151
00152 template<typename T, int N> inline IndexTM<T,N>::IndexTM(const IndexTM<T,N> & a_iv)
00153 : GenericArithmeticable< T, IndexTM<T,N> >(this)
00154 {
00155 memcpy(m_vect, a_iv.m_vect, N*sizeof(T));
00156 }
00157
00158 template<typename T, int N> inline IndexTM<T,N>& IndexTM<T,N>::operator=(const IndexTM<T,N> & a_iv)
00159 {
00160 memcpy(m_vect, a_iv.m_vect, N*sizeof(T));
00161 return *this;
00162 }
00163
00164 template<typename T, int N> inline T& IndexTM<T,N>::operator[] (int a_i)
00165 {
00166 CH_assert(a_i >= 0 && a_i < N);
00167 return m_vect[a_i];
00168 }
00169
00170 template<typename T, int N> inline T IndexTM<T,N>::operator[] (int a_i) const
00171 {
00172 CH_assert(a_i >= 0 && a_i < N);
00173 return m_vect[a_i];
00174 }
00175
00176 template<typename T, int N> inline void IndexTM<T,N>::setVal(int a_i,
00177 T a_val)
00178 {
00179 CH_assert(a_i >= 0 && a_i < N);
00180 m_vect[a_i] = a_val;
00181 }
00182
00183 template<typename T, int N> inline void IndexTM<T,N>::setAll(T a_val)
00184 {
00185 for (int i = 0; i < N; i++)
00186 {
00187 m_vect[i] = a_val;
00188 }
00189 }
00190
00191 template<typename T, int N> inline const T* IndexTM<T,N>::dataPtr() const
00192 {
00193 return m_vect;
00194 }
00195
00196 template<typename T, int N> inline T* IndexTM<T,N>::dataPtr()
00197 {
00198 return m_vect;
00199 }
00200
00201 template<typename T, int N> inline const T* IndexTM<T,N>::getVect() const
00202 {
00203 return m_vect;
00204 }
00205
00206 template<typename T, int N> inline bool IndexTM<T,N>::operator== (const IndexTM & a_p) const
00207 {
00208 return Metaprograms::pointwiseCompare<N,T,std::equal_to<T> >(m_vect,a_p.m_vect);
00209 }
00210
00211 template<typename T, int N> inline bool IndexTM<T,N>::operator!= (const IndexTM & a_p) const
00212 {
00213 return !(operator==(a_p));
00214 }
00215
00216 template<typename T, int N> inline bool IndexTM<T,N>::lexLT(const IndexTM & a_s) const
00217 {
00218 return Metaprograms::LexLT<N,T>()(m_vect,a_s.m_vect);
00219 }
00220
00221 template<typename T, int N> inline bool IndexTM<T,N>::lexGT(const IndexTM & a_s) const
00222 {
00223 return ! Metaprograms::LexLT<N,T>()(m_vect,a_s.m_vect);
00224 }
00225
00226 template<typename T, int N> inline IndexTM<T,N> IndexTM<T,N>::operator+ () const
00227 {
00228 return IndexTM<T,N>(*this);
00229 }
00230
00231 template<typename T, int N> inline IndexTM<T,N> IndexTM<T,N>::operator- () const
00232 {
00233 IndexTM<T,N> result(*this);
00234 for (int i = 0; i < N; ++i)
00235 {
00236 result.m_vect[i] *= -1;
00237 }
00238 return result;
00239 }
00240
00241 template<typename T, int N> inline T IndexTM<T,N>::dotProduct(const IndexTM<T,N> & a_rhs ) const
00242 {
00243 return Metaprograms::InnerProduct<N,T,T,
00244 std::plus<T>,
00245 std::multiplies<T> >()(m_vect,
00246 a_rhs.m_vect);
00247 }
00248
00249 template<typename T, int N> inline T IndexTM<T,N>::sum() const
00250 {
00251 return Metaprograms::Accum<N,T,std::plus<T> >()(m_vect);
00252 }
00253
00254 template<typename T, int N> inline T IndexTM<T,N>::product () const
00255 {
00256 return Metaprograms::Accum<N,T,std::multiplies<T> >()(m_vect);
00257 }
00258
00259 template<typename T, int N> template<typename OP> bool IndexTM<T,N>::operatorCompare(const IndexTM<T,N> & a_p,
00260 const OP & a_op) const
00261 {
00262 return Metaprograms::pointwiseCompare<N,T,OP>(m_vect,a_p.m_vect);
00263 }
00264
00265
00266 template<typename T, int N> template<typename OP> inline IndexTM<T,N>& IndexTM<T,N>::operatorOpEquals(const T & a_s,
00267 const OP & a_op)
00268 {
00269 Metaprograms::Transform<N,T,OP>()(m_vect,a_s);
00270 return *this;
00271 }
00272
00273 template<typename T, int N> template<typename OP> inline IndexTM<T,N>& IndexTM<T,N>::operatorOpEquals(const IndexTM<T,N> & a_p,
00274 const OP & a_op)
00275 {
00276 Metaprograms::Transform<N,T,OP>()(m_vect,a_p.m_vect);
00277 return *this;
00278 }
00279
00280 template<typename T, int N> inline IndexTM<T,N>& IndexTM<T,N>::reciprocal()
00281 {
00282 std::transform(m_vect, m_vect+N, m_vect, bind1st(std::divides<T>(),T(1)));
00283 return *this;
00284 }
00285
00286 template<typename T> static bool abscompare(const T & a_a,
00287 const T & a_b)
00288 {
00289 return ::fabs(a_a) < ::fabs(a_b);
00290 }
00291 template<typename T, int N> inline int IndexTM<T,N>::minDir(bool a_doAbs) const
00292 {
00293 if (a_doAbs)
00294 {
00295 return std::min_element(m_vect, m_vect+N, std::ptr_fun(abscompare<T>)) - m_vect;
00296 }
00297 else
00298 {
00299 return std::min_element(m_vect, m_vect+N) - m_vect;
00300 }
00301 }
00302
00303 template<typename T, int N> inline int IndexTM<T,N>::maxDir(bool a_doAbs) const
00304 {
00305 if (a_doAbs)
00306 {
00307 return std::max_element(m_vect, m_vect+N, std::ptr_fun(abscompare<T>)) - m_vect;
00308 }
00309 else
00310 {
00311 return std::max_element(m_vect, m_vect+N) - m_vect;
00312 }
00313 }
00314
00315 template<typename T> static T ourmin(T a_a,
00316 T a_b)
00317 {
00318 return ((a_a < a_b) ? a_a : a_b);
00319 }
00320
00321 template<typename T> static T ourmax(T a_a,
00322 T a_b)
00323 {
00324 return (a_a > a_b ? a_a : a_b);
00325 }
00326
00327 template<typename T, int N> inline IndexTM<T,N>& IndexTM<T,N>::min(const IndexTM<T,N> & a_p)
00328 {
00329 std::transform(m_vect, m_vect+N, a_p.m_vect, m_vect,
00330 std::ptr_fun(ourmin<T>));
00331 return *this;
00332 }
00333
00334 template<typename T, int N> inline IndexTM<T,N>& IndexTM<T,N>::max(const IndexTM<T,N> & a_p)
00335 {
00336 std::transform(m_vect, m_vect+N, a_p.m_vect, m_vect,
00337 std::ptr_fun(ourmax<T>));
00338 return *this;
00339 }
00340
00341
00342 template<typename T, int N> inline IndexTM<T,N>& IndexTM<T,N>::scale(T a_s)
00343 {
00344 return (*this) *= a_s;
00345 }
00346
00347 template<typename T, int N> inline IndexTM<T,N>& IndexTM<T,N>::reflect (T a_refIx,
00348 int a_idir)
00349 {
00350 CH_assert(a_idir >= 0 && a_idir < N);
00351 m_vect[a_idir] = -m_vect[a_idir] + 2*a_refIx;
00352 return *this;
00353 }
00354
00355 template<typename T, int N> inline IndexTM<T,N>& IndexTM<T,N>::shift(int a_coord,
00356 T a_s)
00357 {
00358 CH_assert(a_coord >= 0 && a_coord < N);
00359 m_vect[a_coord] += a_s;
00360 return *this;
00361 }
00362
00363 template<typename T, int N> inline IndexTM<T,N>& IndexTM<T,N>::shift(const IndexTM<T,N> & a_iv)
00364 {
00365 return (*this) += a_iv;
00366 }
00367
00368 template<typename T, int N> inline IndexTM<T,N>& IndexTM<T,N>::diagShift(T a_s)
00369 {
00370 return (*this) += a_s;
00371 }
00372
00373 template<typename T, int N> inline IndexTM<T,N> scale (const IndexTM<T,N> & a_p,
00374 T a_s)
00375 {
00376 return a_p * a_s;
00377 }
00378
00379 template<typename T, int N> inline IndexTM<T,N> diagShift(const IndexTM<T,N> & a_p,
00380 T a_s)
00381 {
00382 return a_p + a_s;
00383 }
00384
00385 template<typename T, int N> inline IndexTM<T,N> min(const IndexTM<T,N> & a_p1,
00386 const IndexTM<T,N> & a_p2)
00387 {
00388 IndexTM<T,N> result(a_p1);
00389 return result.min(a_p2);
00390 }
00391
00392 template<typename T, int N> inline IndexTM<T,N> max(const IndexTM<T,N> & a_p1,
00393 const IndexTM<T,N> & a_p2)
00394 {
00395 IndexTM<T,N> result(a_p1);
00396 return result.max(a_p2);
00397 }
00398
00399 template<typename T, int N> inline IndexTM<T,N> BASISV_TM(int a_dir)
00400 {
00401 CH_assert(a_dir >= 0 && a_dir < N);
00402 IndexTM<T,N> tmp = IndexTM<T,N>::Zero ;
00403 tmp.dataPtr()[a_dir] = T(1);
00404 return tmp;
00405 }
00406
00407 template<typename T, int N> inline IndexTM<T,N> reflect(const IndexTM<T,N> & a_a,
00408 T a_refIx,
00409 int a_idir)
00410 {
00411 IndexTM<T,N> result(a_a);
00412 return result.reflect(a_refIx, a_idir);
00413 }
00414
00415 template<typename T> static T ourcoarsen(T a_a,
00416 T a_b)
00417 {
00418 return (a_a < 0 ? T(-::fabs(a_a+1))/a_b-1 : a_a/a_b);
00419 }
00420
00421 template<typename T, int N> inline IndexTM<T,N> coarsen(const IndexTM<T,N> & a_p,
00422 T a_s)
00423 {
00424 IndexTM<T,N> result(a_p);
00425 return result.coarsen(a_s);
00426 }
00427
00428 template<typename T, int N> inline IndexTM<T,N> coarsen(const IndexTM<T,N> & a_p1,
00429 const IndexTM<T,N> & a_p2)
00430 {
00431 IndexTM<T,N> result(a_p1);
00432 return result.coarsen(a_p2);
00433 }
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447 template<typename T, int N> inline IndexTM<T,N>& IndexTM<T,N>::coarsen(T a_s)
00448 {
00449 CH_assert(a_s > 0);
00450 std::transform(m_vect, m_vect+N, m_vect,
00451 std::bind2nd(std::ptr_fun(ourcoarsen<T>),a_s));
00452 return *this;
00453 }
00454
00455 template<typename T, int N> inline IndexTM<T,N>& IndexTM<T,N>::coarsen(const IndexTM<T,N> & a_p)
00456 {
00457 CH_assert(p > (IndexTM<T,N>::Zero));
00458 std::transform(m_vect, m_vect+N, a_p.m_vect, m_vect, std::ptr_fun(ourcoarsen<T>));
00459 return *this;
00460 }
00461
00462 template<typename T, int N> void IndexTM<T,N>::linearIn(const void* a_inBuf)
00463 {
00464 memcpy(m_vect, (T*)a_inBuf, N*sizeof(T));
00465 }
00466
00467 template<typename T, int N> void IndexTM<T,N>::linearOut(void* a_outBuf) const
00468 {
00469 memcpy((T*)a_outBuf, m_vect, N*sizeof(T));
00470 }
00471
00472 template<typename T, int N> IndexTM<T,N>::IndexTM(T a_i)
00473 : GenericArithmeticable< T, IndexTM<T,N> >(this)
00474 {
00475 STATIC_ASSERT(N == 1);
00476 m_vect[0] = a_i;
00477 }
00478
00479 template<typename T, int N> IndexTM<T,N>::IndexTM(T a_i,
00480 T a_j)
00481 : GenericArithmeticable< T, IndexTM<T,N> >(this)
00482 {
00483 STATIC_ASSERT(N == 2);
00484 m_vect[0] = a_i;
00485 m_vect[1] = a_j;
00486 }
00487
00488 template<typename T, int N> IndexTM<T,N>::IndexTM(T a_i,
00489 T a_j,
00490 T a_k)
00491 : GenericArithmeticable< T, IndexTM<T,N> >(this)
00492 {
00493 STATIC_ASSERT(N == 3);
00494 m_vect[0] = a_i;
00495 m_vect[1] = a_j;
00496 m_vect[2] = a_k;
00497 }
00498
00499 template<typename T, int N> IndexTM<T,N>::IndexTM(T a_i,
00500 T a_j,
00501 T a_k,
00502 T a_l)
00503 : GenericArithmeticable< T, IndexTM<T,N> >(this)
00504 {
00505 STATIC_ASSERT(N == 4);
00506 m_vect[0] = a_i;
00507 m_vect[1] = a_j;
00508 m_vect[2] = a_k;
00509 m_vect[3] = a_l;
00510 }
00511
00512 template<typename T, int N> IndexTM<T,N>::IndexTM(T a_i,
00513 T a_j,
00514 T a_k,
00515 T a_l,
00516 T a_m)
00517 : GenericArithmeticable< T, IndexTM<T,N> >(this)
00518 {
00519 STATIC_ASSERT(N == 5);
00520 m_vect[0] = a_i;
00521 m_vect[1] = a_j;
00522 m_vect[2] = a_k;
00523 m_vect[3] = a_l;
00524 m_vect[4] = a_m;
00525 }
00526
00527 template<typename T, int N> IndexTM<T,N>::IndexTM(T a_i,
00528 T a_j,
00529 T a_k,
00530 T a_l,
00531 T a_m,
00532 T a_n)
00533 : GenericArithmeticable< T, IndexTM<T,N> >(this)
00534 {
00535 STATIC_ASSERT(N == 6);
00536 m_vect[0] = a_i;
00537 m_vect[1] = a_j;
00538 m_vect[2] = a_k;
00539 m_vect[3] = a_l;
00540 m_vect[4] = a_m;
00541 m_vect[5] = a_n;
00542 }
00543
00544 template<typename T, int N> const IndexTM<T,N> IndexTM<T,N>::Zero;
00545 template<typename T, int N> const IndexTM<T,N> IndexTM<T,N>::Unit;
00546
00547 static int s_dummyForIntVectCpp1 (IndexTM<int ,1>::InitStatics());
00548 static int s_dummyForIntVectCpp2 (IndexTM<int ,2>::InitStatics());
00549 static int s_dummyForIntVectCpp3 (IndexTM<int ,3>::InitStatics());
00550 static int s_dummyForIntVectCpp4 (IndexTM<int ,4>::InitStatics());
00551 static int s_dummyForIntVectCpp5 (IndexTM<int ,5>::InitStatics());
00552 static int s_dummyForIntVectCpp6 (IndexTM<int ,6>::InitStatics());
00553 static int s_dummyForRealVectCpp1(IndexTM<Real,1>::InitStatics());
00554 static int s_dummyForRealVectCpp2(IndexTM<Real,2>::InitStatics());
00555 static int s_dummyForRealVectCpp3(IndexTM<Real,3>::InitStatics());
00556 static int s_dummyForRealVectCpp4(IndexTM<Real,4>::InitStatics());
00557 static int s_dummyForRealVectCpp5(IndexTM<Real,5>::InitStatics());
00558 static int s_dummyForRealVectCpp6(IndexTM<Real,6>::InitStatics());
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569 #include "BaseNamespaceFooter.H"
00570
00571 #endif // include guard