Main Page | Namespace List | Class Hierarchy | Compound List | File List | Namespace Members | Compound Members

ix.h

00001 /* -*- c++ -*- */
00002 /*
00003  * Expresso, a C++ Array template class library
00004  * Copyright (C) 1998,2004 Oskar Enoksson (enok@lysator.liu.se)
00005  * 
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Library General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Library General Public
00017  * License along with this library; if not, write to the
00018  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019  * Boston, MA  02111-1307, USA.
00020  */
00021 #ifndef EXPRESSO_IX_HDR
00022 #define EXPRESSO_IX_HDR
00023 
00024 #include "debug.h"
00025 #include "tmpltool.h"
00026 #include "ixtype.h"
00027 
00028 namespace esso {
00029 
00030   template<int N, class T=IXTYPE>
00031   class Ix;
00032 
00033   namespace IndexInternal {
00034 
00035     template<int K, int M, bool Method=(M>K)>
00036     struct Recurse;
00037 
00038     template<int K, int M>
00039     struct Recurse<K,M,true> {
00040       template<int N, class T>
00041       inline static T Prod(const Ix<N,T> &d) { 
00042         return d[M-1]*Recurse<K,M-1,(M-1>K)>::Prod(d); }
00043       template<int N, class T>
00044       inline static T SumProd(const Ix<N,T> &s1, const Ix<N,T> &s2) { 
00045         return s1[M-1]*s2[M-1]+Recurse<K,M-1,(M-1>K)>::SumProd(s1,s2); }
00046     };
00047 
00048     template<int K, int M>
00049     struct Recurse<K,M,false> {
00050       template<int N, class T>
00051       inline static T Prod(const Ix<N,T> &d) { 
00052         return 1; }
00053       template<int N, class T>
00054       inline static T SumProd(const Ix<N,T> &s1, const Ix<N,T> &s2) { 
00055         return 0; }
00056     };
00057 
00058     template<class T0>
00059     struct Info {
00060       typedef T0 T;
00061       enum { len=1 };
00062       enum { totlen=1 };
00063       enum { rank=0 };
00064     };
00065 
00066     template<class T0, int N>
00067     struct Info<Ix<N,T0> > {
00068       typedef typename Info<T0>::T T;
00069       enum { len=N };
00070       enum { totlen=len*Info<T0>::totlen };
00071       enum { rank=1+Info<T0>::rank };
00072     };
00073   }
00074 
00076 
00083   template<int N, class T0>
00084   class Ix {
00085   public:
00086     typedef typename IndexInternal::Info<T0>::T T;
00087     template<int NN, class TT>
00088     friend class Ix;
00090 
00091 
00093     inline Ix() {}
00096     explicit inline Ix(T i0) {
00097       for(DIMT n=0; n<N; n++) d[n]=i0;
00098     }
00100     inline Ix(const Ix &i0) {
00101       for(DIMT n=0; n<N; n++) d[n]=i0[n];
00102     }
00104 
00105 
00106 
00107     inline Ix(T0 i0, T0 i1) {
00108       TmplTools::TAssert<(N==2)>();
00109       d[0]=i0, d[1]=i1;
00110     }
00112     inline Ix(T0 i0, T0 i1, T0 i2) {
00113       TmplTools::TAssert<(N==3)>();
00114       d[0]=i0, d[1]=i1, d[2]=i2;
00115     }  
00117     inline Ix(T0 i0, T0 i1, T0 i2, T0 i3) {
00118       TmplTools::TAssert<(N==4)>();
00119       d[0]=i0, d[1]=i1, d[2]=i2, d[3]=i3;
00120     }
00122     inline Ix(T0 i0, T0 i1, T0 i2, T0 i3, T0 i4) {
00123       TmplTools::TAssert<(N==5)>();
00124       d[0]=i0, d[1]=i1, d[2]=i2, d[3]=i3, d[4]=i4;
00125     }  
00127     inline Ix(T0 i0, T0 i1, T0 i2, T0 i3, T0 i4, T0 i5) {
00128       TmplTools::TAssert<(N==6)>();
00129       d[0]=i0, d[1]=i1, d[2]=i2, d[3]=i3, d[4]=i4, d[5]=i5;
00130     }
00132 
00133 
00134 
00135     inline T0 operator [](DIMT n) const { 
00136       EXPRESSO_DEBUG_CHECK_DIM(0<=n && n<N); 
00137       return d[n]; 
00138     }
00140     inline bool operator ==(const T &src) const {
00141       for(DIMT n=0; n<N; n++)
00142         if(!(d[n]==src))
00143           return false;
00144       return true;
00145     }
00147     inline bool operator ==(const Ix &src) const {
00148       for(DIMT n=0; n<N; n++)
00149         if(!(d[n]==src[n]))
00150           return false;
00151       return true;
00152     }
00154     inline bool operator !=(const T &src) const {
00155       for(DIMT n=0; n<N; n++)
00156         if(d[n]!=src)
00157           return true;
00158       return false;
00159     }
00161     inline bool operator !=(const Ix &src) const {
00162       for(DIMT n=0; n<N; n++)
00163         if(d[n]!=src[n])
00164           return true;
00165       return false;
00166     }
00169     template<DIMT LEN>
00170     inline Ix<LEN,T0> Sub(DIMT start=0) const {
00171       TmplTools::TAssert<(LEN<=N)>();
00172       EXPRESSO_DEBUG_CHECK_DIM(0<=start && LEN+start<=N); 
00173       Ix<LEN,T0> tmp;
00174       for(int n=0; n<LEN; n++)
00175         tmp[n] = d[start+n];
00176       return tmp;
00177     }
00179 
00180 
00181 
00182     inline T0 & operator [](DIMT n) { 
00183       EXPRESSO_DEBUG_CHECK_DIM(0<=n && n<N); 
00184       return d[n]; 
00185     }
00187     inline Ix & operator =(T src) {
00188       for(DIMT n=0; n<N; n++) d[n]=src;
00189       return *this;
00190     }
00192     template<class TT>
00193     inline Ix & operator +=(const Ix<N,TT> &src) {
00194       for(DIMT n=0; n<N; n++) d[n]+=src.d[n];
00195       return *this;
00196     }
00198     inline Ix & operator +=(const T &src) {
00199       for(DIMT n=0; n<N; n++) d[n]+=src;
00200       return *this;
00201     }
00203     template<class TT>
00204     inline Ix & operator -=(const Ix<N,TT> &src) {
00205       for(DIMT n=0; n<N; n++) d[n]-=src.d[n];
00206       return *this;
00207     }
00209     inline Ix & operator -=(const T &src) {
00210       for(DIMT n=0; n<N; n++) d[n]-=src;
00211       return *this;
00212     }
00214     inline Ix & operator *=(const T &src) {
00215       for(DIMT n=0; n<N; n++) d[n]*=src;
00216       return *this;
00217     }
00219     inline Ix & operator /=(const T &src) {
00220       for(DIMT n=0; n<N; n++) d[n]/=src;
00221       return *this;
00222     }
00228     template<int N1>
00229     inline Ix &Concat(const Ix<N1,T0> &p1, 
00230                       const Ix<N-N1,T0> &p2) {
00231       for(DIMT n=0; n<N1; n++)
00232         d[n] = p1[n];
00233       for(DIMT n=0; n<N-N1; n++)
00234         d[n+N1] = p2[n];
00235       return *this;
00236     }
00240     template<int M>
00241     inline Ix<N+M,T0> operator |(const Ix<M,T0> &p2) {
00242       return Ix<N+M,T0>().Concat(*this,p2);
00243     }
00245   protected:
00247     T d[N];
00248   };
00249 
00251 
00252 
00253   template<int N,class T> inline Ix<N,T> operator - (const Ix<N,T> &ix) { 
00254     Ix<N,T> ret; 
00255     for(DIMT n=0; n<N; n++) 
00256       ret[n]=-ix[n];
00257     return ret; 
00258   } 
00260   template<int N,class T> inline Ix<N,T> operator + (const Ix<N,T> &ix, T i) { 
00261     Ix<N,T> ret; 
00262     for(DIMT n=0; n<N; n++) 
00263       ret[n]=ix[n] + i; 
00264     return ret; 
00265   } 
00267   template<int N,class T> inline Ix<N,T> operator +(const Ix<N,T> &ix, const Ix<N,T> &ixx) { 
00268     Ix<N,T> ret; 
00269     for(DIMT n=0; n<N; n++) 
00270       ret[n]=ix[n] + ixx[n]; 
00271     return ret; 
00272   } 
00274   template<int N,class T> inline Ix<N,T> operator + (T i, const Ix<N,T> &ix) { 
00275     Ix<N,T> ret; 
00276     for(DIMT n=0; n<N; n++) 
00277       ret[n]=i + ix[n]; 
00278     return ret; 
00279   }
00281   template<int N,class T> inline Ix<N,T> operator - (const Ix<N,T> &ix, T i) { 
00282     Ix<N,T> ret; 
00283     for(DIMT n=0; n<N; n++) 
00284       ret[n]=ix[n] - i; 
00285     return ret; 
00286   } 
00288   template<int N,class T> inline Ix<N,T> operator -(const Ix<N,T> &ix, const Ix<N,T> &ixx) { 
00289     Ix<N,T> ret; 
00290     for(DIMT n=0; n<N; n++) 
00291       ret[n]=ix[n] - ixx[n]; 
00292     return ret; 
00293   } 
00295   template<int N,class T> inline Ix<N,T> operator - (T i, const Ix<N,T> &ix) { 
00296     Ix<N,T> ret; 
00297     for(DIMT n=0; n<N; n++) 
00298       ret[n]=i - ix[n]; 
00299     return ret; 
00300   }
00302   template<int N,class T> inline Ix<N,T> operator * (const Ix<N,T> &ix, T i) { 
00303     Ix<N,T> ret; 
00304     for(DIMT n=0; n<N; n++) 
00305       ret[n]=ix[n] * i; 
00306     return ret; 
00307   } 
00309   template<int N,class T> inline Ix<N,T> operator * (T i, const Ix<N,T> &ix) { 
00310     Ix<N,T> ret; 
00311     for(DIMT n=0; n<N; n++) 
00312       ret[n]=i * ix[n]; 
00313     return ret; 
00314   }
00316   template<int N,class T> inline Ix<N,T> operator / (const Ix<N,T> &ix, T i) { 
00317     Ix<N,T> ret; 
00318     for(DIMT n=0; n<N; n++) 
00319       ret[n]=ix[n] / i; 
00320     return ret; 
00321   } 
00323   template<int N,class T> inline Ix<N,T> operator / (T i, const Ix<N,T> &ix) { 
00324     Ix<N,T> ret; 
00325     for(DIMT n=0; n<N; n++) 
00326       ret[n]=i / ix[n]; 
00327     return ret; 
00328   }
00330 }
00331 
00332 #endif

Generated on Wed May 12 13:34:34 2004 for Expresso by doxygen 1.3.3