src/SubField/SubFieldTraits.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 /***************************************************************************
00003  *
00004  * The IPPL Framework
00005  * 
00006  *
00007  * Visit http://people.web.psi.ch/adelmann/ for more details
00008  *
00009  ***************************************************************************/
00010 
00011 #ifndef SUB_FIELD_TRAITS_H
00012 #define SUB_FIELD_TRAITS_H
00013 
00014 
00016 // a templated traits class describing how to combine different subset
00017 // objects together.  The general case generates errors if you try to use
00018 // subset objects in pairs which are not supported; partial specializations
00019 // of this class indicate which combinations actually work and do the
00020 // right thing in those cases.
00021 //
00022 // This class will deal with two classes, call them A and B.  The class
00023 // tells how to construct B from A (A --> B), or how to combine A into B.
00024 // So you can always think of it as  A --> B.  The order of these classes in
00025 // the template parameter list is always Dim(B), Dim(A), B, A.
00026 //
00027 // There are two functions in this traits class:
00028 // a) construct:
00029 //    static method used to set up the subset object based on an input object.
00030 //    Arguments:
00031 //      Subset object (B) which is being constructed.  Assumed to be empty.
00032 //      Input subset object (A) to be used to construct B.
00033 //      BareField with data we are subsetting, if needed.
00034 //    Returns:
00035 //      Number of brackets which this construction adds.
00036 // b) combine:
00037 //    static methods to combine subset objects.
00038 //    Arguments:
00039 //      Existing subset object (B)
00040 //      The subset object (A) to add to the first argument.
00041 //      The return object, constructed from A and B (of type Return_t)
00042 //      The current number of brackets already used
00043 //      BareField with data we are subsetting, if needed.
00044 
00045 // include files
00046 #include "Index/NDIndex.h"
00047 #include "Index/SIndex.h"
00048 #include "Field/BareField.h"
00049 #include "Field/LField.h"
00050 
00051 
00052 template<class T, unsigned int Dim, class S1, class S2>
00053 struct SubFieldTraits {
00054   // static contruct method
00055   static int construct(S1&, const S2&, BareField<T,Dim>&) {
00056     PInsist(false,"Unsupported indexing attempted.");
00057     return 0;
00058   }
00059 
00060   // a typedef for the return type when you combine two subset objects
00061   typedef S2 Return_t;
00062 
00063   // an enum for how many brackets this combination will add
00064   enum { Brackets_u = 0 };
00065 
00066   // static combine method
00067   template<class S3>
00068   static void combine(const S1&, const S2&, S3&,
00069                       unsigned int&, BareField<T,Dim>&) {
00070     PInsist(false,"Unsupported indexing attempted.");
00071     return false;
00072   }
00073 };
00074 
00075 
00077 // NDIndex related specializations
00078 
00079 // construct NDIndex<Dim> from NDIndex<Dim2>
00080 // combine   [NDIndex<Dim>][NDIndex<Dim2>] --> NDIndex<Dim>
00081 template<class T, unsigned int Dim, unsigned int Dim2>
00082 struct SubFieldTraits<T, Dim, NDIndex<Dim>, NDIndex<Dim2> > {
00083   static int construct(NDIndex<Dim>& out, const NDIndex<Dim2>& s,
00084                        BareField<T,Dim>&) {
00085     CTAssert(Dim2 <= Dim);
00086     for (unsigned int d=0; d < Dim2; ++d)
00087       out[d] = s[d];
00088     return Dim2;
00089   }
00090   typedef NDIndex<Dim> Return_t;
00091   enum { Brackets_u = Dim2 };
00092   static void combine(const NDIndex<Dim>& s1, const NDIndex<Dim2>& s2,
00093                       Return_t& out, unsigned int B, BareField<T,Dim>&) {
00094     CTAssert(Dim2 <= Dim);
00095     unsigned int d;
00096     for (d=0; d < B; ++d)
00097       out[d]   = s1[d];
00098     for (d=0; d < Dim2; ++d)
00099       out[d+B] = s2[d];
00100   }
00101 };
00102 
00103 
00104 // construct NDIndex<Dim> from Index (B from A)
00105 // combine   [NDIndex<Dim>][Index] --> NDIndex<Dim>
00106 template<class T, unsigned int Dim>
00107 struct SubFieldTraits<T, Dim, NDIndex<Dim>, Index> {
00108   static int construct(NDIndex<Dim>& out, const Index& s,
00109                        BareField<T,Dim>&) {
00110     out[0] = s;
00111     return 1;
00112   }
00113   typedef NDIndex<Dim> Return_t;
00114   enum { Brackets_u = 1 };
00115   static void combine(const NDIndex<Dim>& s1, const Index& s2,
00116                       Return_t& out, unsigned int B, BareField<T,Dim>&) {
00117     unsigned int d;
00118     for (d=0; d < B; ++d)
00119       out[d] = s1[d];
00120     out[d] = s2;
00121   }
00122 };
00123 
00124 
00125 // construct NDIndex<Dim> from int (B from A)
00126 // combine   [NDIndex<Dim>][int] --> NDIndex<Dim>
00127 template<class T, unsigned int Dim>
00128 struct SubFieldTraits<T, Dim, NDIndex<Dim>, int> {
00129   static int construct(NDIndex<Dim>& out, const int& s,
00130                        BareField<T,Dim>&) {
00131     out[0] = Index(s,s);
00132     return 1;
00133   }
00134   typedef NDIndex<Dim> Return_t;
00135   enum { Brackets_u = 1 };
00136   static void combine(const NDIndex<Dim>& s1, const int& s2,
00137                       Return_t& out, unsigned int B, BareField<T,Dim>&) {
00138     unsigned int d;
00139     for (d=0; d < B; ++d)
00140       out[d] = s1[d];
00141     out[d] = Index(s2, s2);
00142   }
00143 };
00144 
00145 
00146 // construct NDIndex<Dim> from SOffset<Dim2> (B from A)
00147 // combine   [NDIndex<Dim>][SOffset<Dim2>] --> NDIndex<Dim>
00148 template<class T, unsigned int Dim, unsigned int Dim2>
00149 struct SubFieldTraits<T, Dim, NDIndex<Dim>, SOffset<Dim2> > {
00150   static int construct(NDIndex<Dim>& out, const SOffset<Dim2>& s,
00151                        BareField<T,Dim>&) {
00152     CTAssert(Dim2 <= Dim);
00153     for (unsigned int d=0; d < Dim2; ++d)
00154       out[d] = Index(s[d], s[d]);
00155     return Dim;
00156   }
00157   typedef NDIndex<Dim> Return_t;
00158   enum { Brackets_u = Dim2 };
00159   static void combine(const NDIndex<Dim>& s1, const SOffset<Dim2>& s2,
00160                       Return_t& out, unsigned int B, BareField<T,Dim>&) {
00161     CTAssert(Dim2 <= Dim);
00162     unsigned int d;
00163     for (d=0; d < B; ++d)
00164       out[d] = s1[d];
00165     for (d=0; d < Dim2; ++d)
00166       out[d+B] = Index(s2[d], s2[d]);
00167   }
00168 };
00169 
00170 
00172 // SIndex related specializations
00173 
00174 // construct SIndex<Dim> from SIndex<Dim> (B from A)
00175 // combine   [SIndex<Dim>][SIndex<Dim>] --> SIndex<Dim> (intersection)
00176 template<class T, unsigned int Dim>
00177 struct SubFieldTraits<T, Dim, SIndex<Dim>, SIndex<Dim> > {
00178   static int construct(SIndex<Dim>& out, const SIndex<Dim>& s,
00179                        BareField<T,Dim>&) {
00180     // assigning SIndex to SIndex also will initialize things if needed
00181     out = s;
00182     return Dim;
00183   }
00184   typedef SIndex<Dim> Return_t;
00185   enum { Brackets_u = 0 };
00186   static void combine(const SIndex<Dim>& s1, const SIndex<Dim>& s2,
00187                       Return_t& out, unsigned int, BareField<T,Dim>& A) {
00188     out  = s1;
00189     out &= s2;
00190   }
00191 };
00192 
00193 
00194 // construct SIndex<Dim> from NDIndex<Dim> (B from A)
00195 // combine   [SIndex<Dim>][NDIndex<Dim>] --> SIndex<Dim> (intersection)
00196 template<class T, unsigned int Dim>
00197 struct SubFieldTraits<T, Dim, SIndex<Dim>, NDIndex<Dim> > {
00198   static int construct(SIndex<Dim>& out, const NDIndex<Dim>& s,
00199                        BareField<T,Dim>& A) {
00200     if (out.needInitialize())
00201       out.initialize(A.getLayout());
00202     out = s;
00203     return Dim;
00204   }
00205   typedef SIndex<Dim> Return_t;
00206   enum { Brackets_u = 0 };
00207   static void combine(const SIndex<Dim>& s1, const NDIndex<Dim>& s2,
00208                       Return_t& out, unsigned int, BareField<T,Dim>& A) {
00209     out  = s1;
00210     out &= s2;
00211   }
00212 };
00213 
00214 
00215 // construct SIndex<Dim> from SOffset<Dim> (B from A)
00216 // combine   [SIndex<Dim>][SOffset<Dim>] --> SIndex<Dim> (intersection)
00217 template<class T, unsigned int Dim>
00218 struct SubFieldTraits<T, Dim, SIndex<Dim>, SOffset<Dim> > {
00219   static int construct(SIndex<Dim>& out, const SOffset<Dim>& s,
00220                        BareField<T,Dim>& A) {
00221     if (out.needInitialize())
00222       out.initialize(A.getLayout());
00223     out = s;
00224     return Dim;
00225   }
00226   typedef SIndex<Dim> Return_t;
00227   enum { Brackets_u = 0 };
00228   static void combine(const SIndex<Dim>& s1, const SOffset<Dim>& s2,
00229                       Return_t& out, unsigned int, BareField<T,Dim>& A) {
00230     out  = s1;
00231     out &= s2;
00232   }
00233 };
00234 
00235 
00237 // SOffset related specializations
00238 
00239 // construct SOffset<Dim> from SOffset<Dim2>
00240 // combine   [SOffset<Dim>][SOffset<Dim2>] --> SOffset<Dim>
00241 template<class T, unsigned int Dim, unsigned int Dim2>
00242 struct SubFieldTraits<T, Dim, SOffset<Dim>, SOffset<Dim2> > {
00243   static int construct(SOffset<Dim>& out, const SOffset<Dim2>& s,
00244                        BareField<T,Dim>&) {
00245     CTAssert(Dim2 <= Dim);
00246     for (unsigned int d=0; d < Dim2; ++d)
00247       out[d] = s[d];
00248     return Dim;
00249   }
00250   typedef SOffset<Dim> Return_t;
00251   enum { Brackets_u = Dim2 };
00252   static void combine(const SOffset<Dim>& s1, const SOffset<Dim2>& s2,
00253                       Return_t& out, unsigned int B, BareField<T,Dim>&) {
00254     CTAssert(Dim2 <= Dim);
00255     unsigned int d;
00256     for (d=0; d < B; ++d)
00257       out[d]   = s1[d];
00258     for (d=0; d < Dim2; ++d)
00259       out[d+B] = s2[d];
00260   }
00261 };
00262 
00263 
00264 // construct SOffset<Dim> from int
00265 // combine   [SOffset<Dim>][int] --> SOffset<Dim>
00266 template<class T, unsigned int Dim>
00267 struct SubFieldTraits<T, Dim, SOffset<Dim>, int> {
00268   static int construct(SOffset<Dim>& out, const int& s,
00269                        BareField<T,Dim>&) {
00270     out[0] = s;
00271     return 1;
00272   }
00273   typedef SOffset<Dim> Return_t;
00274   enum { Brackets_u = 1 };
00275   static void combine(const SOffset<Dim>& s1, const int& s2,
00276                       Return_t& out, unsigned int B, BareField<T,Dim>&) {
00277     unsigned int d;
00278     for (d=0; d < B; ++d)
00279       out[d] = s1[d];
00280     out[d] = s2;
00281   }
00282 };
00283 
00284 
00285 // combine   [SOffset<Dim>][Index] --> NDIndex<Dim>
00286 template<class T, unsigned int Dim>
00287 struct SubFieldTraits<T, Dim, SOffset<Dim>, Index> {
00288   typedef NDIndex<Dim> Return_t;
00289   enum { Brackets_u = 1 };
00290   static void combine(const SOffset<Dim>& s1, const Index& s2,
00291                       Return_t& out, unsigned int B, BareField<T,Dim>&) {
00292     unsigned int d;
00293     for (d=0; d < B; ++d)
00294       out[d] = Index(s1[d], s1[d]);
00295     out[d] = s2;
00296   }
00297 };
00298 
00299 
00300 // combine   [SOffset<Dim>][NDIndex<Dim2>] --> NDIndex<Dim>
00301 template<class T, unsigned int Dim, unsigned int Dim2>
00302 struct SubFieldTraits<T, Dim, SOffset<Dim>, NDIndex<Dim2> > {
00303   typedef NDIndex<Dim> Return_t;
00304   enum { Brackets_u = Dim2 };
00305   static void combine(const SOffset<Dim>& s1, const NDIndex<Dim2>& s2,
00306                       Return_t& out, unsigned int B, BareField<T,Dim>&) {
00307     CTAssert(Dim2 <= Dim);
00308     unsigned int d;
00309     for (d=0; d < B; ++d)
00310       out[d]   = Index(s1[d], s1[d]);
00311     for (d=0; d < Dim2; ++d)
00312       out[d+B] = s2[d];
00313   }
00314 };
00315 
00316 
00317 #endif // SUB_FIELD_TRAITS_H
00318 
00319 /***************************************************************************
00320  * $RCSfile: SubFieldTraits.h,v $   $Author: adelmann $
00321  * $Revision: 1.1.1.1 $   $Date: 2003/01/23 07:40:33 $
00322  * IPPL_VERSION_ID: $Id: SubFieldTraits.h,v 1.1.1.1 2003/01/23 07:40:33 adelmann Exp $ 
00323  ***************************************************************************/

Generated on Mon Jan 16 13:23:56 2006 for IPPL by  doxygen 1.4.6