CopyMemArray.hpp

00001 // $Id: CopyMemArray.hpp,v 1.4 2006/05/09 12:30:28 werner Exp $
00002 // $Log: CopyMemArray.hpp,v $
00003 // Revision 1.4  2006/05/09 12:30:28  werner
00004 // Adjustments and fixes for MSYS/MINGW32
00005 //
00006 // Revision 1.3  2004/09/30 11:18:40  werner
00007 // Bugfixes and increased functionality for generic field reading.
00008 //
00009 // Revision 1.2  2004/08/12 15:50:54  werner
00010 // Improvements.
00011 //
00012 // Revision 1.1  2004/07/12 13:30:44  werner
00013 // Outsourcing of the MemCore/VecAl interface into the Field library.
00014 //
00015 // Revision 1.4  2004/07/09 20:15:27  werner
00016 // Added local alignment capability when interpolating vector fields and
00017 // option to compute the interpolated derivative.
00018 //
00019 // Revision 1.3  2004/07/06 17:13:59  werner
00020 // Adjustments for GCC 3.4.0
00021 //
00022 // Revision 1.2  2004/07/06 11:56:04  werner
00023 // The Xerox copier is functioning now for all types and all targets.
00024 //
00025 // Revision 1.1  2004/07/04 17:24:57  werner
00026 // Interfacing MemCore library from VecAl and
00027 // template reduction to runtime interface. Uses the META library.
00028 //
00029 
00030 #include "MemArray.hpp"
00031 #include <vecal/ipol/CopyMultiArray.hpp>
00032 #include <vecal/ipol/ViewMultiArray.hpp>
00033 #include <vecal/ipol/STLMultiArray.hpp>
00034 
00035 #include <meta/LIST.hpp>
00036 #include <meta/NativeTypeList.hpp>
00037 
00038 using namespace MemCore;
00039 using namespace META;
00040 
00041 #ifndef NDEBUG
00042 #define FAST_COMPILATION
00043 #endif
00044 
00045 namespace Fiber
00046 {
00047 using namespace VecAl;
00048 
00049 template <class SrcMemArrayType>
00050 struct  CopyMemArraySourceTypeIterator
00051 {
00052         SrcMemArrayType&Src;
00053         
00054         enum { Dims = SrcMemArrayType::Dims };
00055 
00056         const MultiIndex<Dims>        &DstOffset;
00057         const RefPtr<MemArray<Dims> > &Dst;
00058         const MultiIndex<Dims>        &SrcOffset;
00059 
00060         CopyMemArraySourceTypeIterator(const RefPtr<MemArray<Dims> > &pDst,
00061                                        const MultiIndex<Dims>        &pDstOffset,
00062                                        SrcMemArrayType&S,
00063                                        const MultiIndex<Dims>        &pSrcOffset)
00064         : Src(S), Dst(pDst)
00065         , DstOffset(pDstOffset), SrcOffset(pSrcOffset)
00066         {}
00067 
00068         template <typename T>
00069                 struct exec
00070                 {
00071                         template <typename Storage>
00072                                 struct  DstStorage
00073                         {
00074                         static  bool CouldCopy(CopyMemArraySourceTypeIterator&It)
00075                                 {
00076                                         if (RefPtr<MemArrayStorage<Dims, T, Storage> > D = It.Dst)
00077                                         {
00078                                                 Xerox<MultiArray<Dims, T, Storage>, SrcMemArrayType>::
00079 							Copy( D->myArray(), It.DstOffset, It.Src, It.SrcOffset);
00080                                                 return true; // copy done.
00081                                         }
00082                                         return false; // continue traversal
00083                                 }
00084                         };
00085 
00086                         // return true if the traversal of the list 
00087                         // should be stopped
00088                 static inline bool Break(CopyMemArraySourceTypeIterator&It)
00089                         {
00090                                 if (DstStorage<        T*  >::CouldCopy(It) ) return true;
00091                                 if (DstStorage<ViewPtr<T>  >::CouldCopy(It) ) return true;
00092                         //      if (DstStorage<vector <T>  >::CouldCopy(It) ) return true;
00093                                 if (DstStorage<vector <T*> >::CouldCopy(It) ) return true;
00094 
00095 
00096                         /*
00097                                 if (RefPtr<MemArrayStorage<Dims, T, T*> > D = It.Dst)
00098                                 {
00099                                         Xerox<MultiArray<Dims, T, T*>, SrcMemArrayType, Dims>::
00100                                                 Copy( D->myArray(), It.DstOffset, It.Src, It.SrcOffset);
00101                                         return true; // copy done.
00102                                 }
00103                         */
00104                                 return false; // continue traversal
00105                         }
00106                 };
00107 };
00108 
00109 
00110 
00111 template <int Dims>
00112 struct  CopyMemArrayIterator
00113 {
00114         const RefPtr<MemArray<Dims> >   &Dst;
00115         const MultiIndex<Dims>          &DstOffset;
00116         const RefPtr<MemArray<Dims> >   &Src; 
00117         const MultiIndex<Dims>          &SrcOffset;
00118 
00119 
00120         CopyMemArrayIterator(const RefPtr<MemArray<Dims> >&pDst, const MultiIndex<Dims>&pDstOffset,
00121                              const RefPtr<MemArray<Dims> >&pSrc, const MultiIndex<Dims>&pSrcOffset)
00122         : Dst(pDst), DstOffset(pDstOffset)
00123         , Src(pSrc), SrcOffset(pSrcOffset)
00124         {}
00125 
00126         template <typename T>
00127                 struct exec
00128                 {
00129                         template <typename Storage>
00130                                 struct  SrcStorage
00131                         {
00132                         static  bool CouldCopy(CopyMemArrayIterator&It)
00133                                 {
00134                                         if (RefPtr<MemArrayStorage<Dims, T, Storage> > Src = It.Src)
00135                                         {
00136                                         typedef MultiArray<Dims, T, Storage> SrcMultiArray_t;
00137 
00138                                         CopyMemArraySourceTypeIterator<SrcMultiArray_t> 
00139                                                 cm( It.Dst, It.DstOffset, Src->myArray(), It.SrcOffset );
00140 
00141                                         bool    ok = FOREACH<CopyMemArraySourceTypeIterator<SrcMultiArray_t>,
00142                                                              FloatTypeList>::exec( cm );
00143 
00144                                                 return ok;
00145                                         }
00146                                         return false;
00147                                 }
00148                         };
00149 
00150                         // return true if the traversal of the list 
00151                         // should be stopped
00152                 static inline bool Break(CopyMemArrayIterator&It)
00153                         {
00154                                 if (SrcStorage<const T*		>::CouldCopy(It) ) return true;
00155                                 if (SrcStorage<      T*		>::CouldCopy(It) ) return true;
00156                                 if (SrcStorage<ViewPtr<const T> >::CouldCopy(It) ) return true;
00157                                 if (SrcStorage<ViewPtr<      T> >::CouldCopy(It) ) return true;
00158                                 if (SrcStorage<vector <      T> >::CouldCopy(It) ) return true;
00159                                 if (SrcStorage<vector <     T*> >::CouldCopy(It) ) return true;
00160 
00161 
00162 
00163 /*                      The above code is identical to the explicit version as here,
00164                         but leaving the storage type open for iteration.
00165 
00166                                 if (RefPtr<MemArrayStorage<Dims, T, T*> > Src = It.Src)
00167                                 {
00168                                 CopyMemArraySourceTypeIterator<MultiArray<Dims, T, T*> > 
00169                                         cm( It.Dst, It.DstOffset, Src->myArray(), It.SrcOffset );
00170 
00171                                 bool    ok = FOREACH<CopyMemArraySourceTypeIterator<MultiArray<Dims, T, T*> >,
00172                                                      FloatTypeList>::exec( cm );
00173 
00174                                         return ok;
00175                                 }
00176 */
00177                                 return false; // continue traversal
00178                         }
00179                 };
00180 };
00181 
00182 
00183 } /* namespace VecAl */ 
00184