MemArray.hpp

00001 #ifndef __FIBER_MEMARRAY_HPP
00002 #define __FIBER_MEMARRAY_HPP "Created 4.07.2004 14:31:21 by bzfbenge"
00003 
00004 #include "ReferencingMemArray.hpp"
00005 #include <vector/MultiCopy.hpp>
00006 
00007 namespace Fiber
00008 {
00009         using std::type_info;
00010 
00011         using MemCore::RefPtr;
00012         using MemCore::ReferenceBase;
00013         using MemCore::Chunk;
00014 
00015 
00016 template <int N>
00017         class MemArrayGetSlice;
00018 
00019 template <int N, class T>
00020 class   MemArray;
00021 
00027 template <int N, class T>
00028 class   MemArray : public ReferencingMemArray<N, T>
00029 {
00030         typedef RefPtr<MemCore::TypedChunk<T> > MemChunk;
00031 
00032 static  bool    TypeIsRegistered;
00033 
00034 public:
00035         typedef typename MetaInfo<T>::element_t element_t;
00036 
00039         typedef MemArray<N, element_t> ComponentArray_t;
00040 
00042         typedef T value_type;
00043 
00047         typedef typename META::BaseClass<T>::result value_base_type;
00048 
00053         typedef MemArray<N, value_base_type> MemArrayOfBaseValues_t;
00054 
00055         enum
00056         {
00058                 Dims = N 
00059         };
00060 
00061 static  bool reg()
00062         {
00063                 if (TypeIsRegistered)
00064                         MemBase::RegisteredTypes++;
00065 
00066                 return TypeIsRegistered;
00067         }
00068 
00069         MemArray( const Iterator<T>&data, const MultiIndex<N>&Sz,
00070                   const RefPtr<MemCore::ChunkBase>&MemStorage,
00071                   const MemBase::Creator_t&C )
00072         : MemBase(C)
00073         , ReferencingMemArray<N,T>(data, Sz, MemStorage, C)
00074         {}
00075 
00076 public:
00078         typedef MultiArray<N, T> MultiArray_t;
00079 
00081         using MultiArray_t::ptr;
00082 
00083         typedef MemCore::NullPtr NullPtr;
00084 
00085         typedef typename MultiArray_t::component_t  component_t;
00086         typedef typename MultiArray_t::ValueBase_t  ValueBase_t;
00087 
00088         typedef typename MultiArray_t::MultiArrayBase_t         MultiArrayBase_t;
00089         typedef typename MultiArray_t::MultiArrayComponent_t    MultiArrayComponent_t;
00090 
00091         typedef MemArray<N,ValueBase_t> MemArrayBase_t;
00092         typedef MemArray<N,component_t > MemArrayComponent_t;
00093 
00095         MemArray(const MultiIndex<N>&M, const MemBase::Creator_t&C = NullPtr() )
00096         : MemBase(C)
00097         , ReferencingMemArray<N, T>( new Chunk<T>( M.size() ), M, C)
00098         {
00099                 reg();
00100         }
00101 
00105         MemArray(const RefPtr<Chunk<T> >&Data, const MultiIndex<N>&M, const MemBase::Creator_t&C = NullPtr() )
00106         : MemBase(C)
00107         , ReferencingMemArray<N, T>( Data, M, C)
00108         {
00109                 reg();
00110         }
00111 
00115         MemArray(const MemCore::MemVector<T>&Data, const MultiIndex<N>&M, const MemBase::Creator_t&C = NullPtr() )
00116         : MemBase(C)
00117         , ReferencingMemArray<N, T>( Data, M, C)
00118         {
00119                 reg();
00120         }
00121 
00123         MemArray(const RefPtr<MemCore::ChunkBase> &Data, const MultiIndex<N>&Sz, const MemBase::Creator_t&C = NullPtr() )
00124         : MemBase(C)
00125         , ReferencingMemArray<N, T>( Data, Sz, C)
00126         {
00127                 reg();
00128         }
00129 
00130         MemArray(const RefPtr<MemCore::ChunkBase>&Storage, const MultiArray<N,T>&M, const MemBase::Creator_t&C = NullPtr() )
00131         : MemBase(C)
00132         , ReferencingMemArray<N,T>(Storage, M, C)
00133         {
00134                 reg();
00135         }
00136 
00137 /*
00138         RefPtr<MemArrayOfBaseValues_t> createSharedBaseArray() const
00139         {
00140                 return new MemArrayOfBaseValues_t( this->StoragePtr(), Size() );
00141         }
00142 
00143         RefPtr<MemBase> createBaseArray() const
00144         {
00145                 return createSharedBaseArray();
00146         }
00147 */
00148 
00149 private:
00150 
00151         friend class MemArrayGetSlice<N>;
00152         friend class MemArrayGetSlice<N+1>;
00153 public:
00154 
00155 /*
00156         override void DeferredConstructor()
00157         {
00158                 MemCore::SaveRegistry<MemArray>::addInterfaces(this);
00159 //              addInterface( new StorageInterpolator( this ) );
00160         }
00161 */
00162 
00163         ~MemArray()
00164         {
00165 //              printf("MemArray::~MemArray(): releasing %ld bytes\n", long(memsize()) ); 
00166 //              this->MemChunk::speak("MemArray::~MemArray() chunk");
00167 
00168 //              MemArrayBaseCreator::CreatorNumber += myCreatorNumber;
00169         }
00170 
00171         override RefPtr<MemBase> getSlice(index_t i, const MemBase::Creator_t&C) const;
00172 
00174         override MultiIndex<N> Size() const
00175         {
00176                 return MultiArray_t::Size();
00177         }
00178 
00184         override RefPtr<MemBase> makeMemArray(const MemBase::Creator_t&) const
00185         {
00186                 return this->self();
00187         }
00188 
00189         override RefPtr<MemBase> createMemArray(const MultiIndex<N>&NewSize, const MemBase::Creator_t&C) const
00190         {
00191                 return new MemArray( NewSize, C );
00192         }
00193 
00199         RefPtr<MemArray> createMemArray(const MemCore::MemVector<T>&Data, const MemBase::Creator_t&C) const
00200         {
00201                 if (!Data) return MemCore::NullPtr(); 
00202                 if (Data->size() != Size().size() )
00203                         return MemCore::NullPtr(); 
00204 
00205                 return new MemArray(Data, Size(), C);
00206         }
00207 
00213         RefPtr<MemArray> createMemArray(const RefPtr<MemCore::ChunkBase>&Data, const MemBase::Creator_t&C) const
00214         {
00215                 if (!Data) return MemCore::NullPtr(); 
00216                 if (Data->elements() != Size().size() )
00217                         return MemCore::NullPtr(); 
00218 
00219                 return new MemArray(Data, Size(), C );
00220         }
00221 
00228         override RefPtr<MemBase> createEquallySizedMemArray(const RefPtr<MemCore::ChunkBase>&Storage, const MemBase::Creator_t&C) const
00229         {
00230                 return createMemArray(Storage, C);
00231         }
00232 
00233 
00234         override RefPtr<MemBase> createSubMemArray(const MultiIndex<N>&Offset, const MultiIndex<N>&CopySize, const MemBase::Creator_t&C) const
00235         {
00236         RefPtr<MemArray> Result = new MemArray( CopySize, C);
00237         MultiArray_t&Dest = *Result; 
00238 
00239                 CopySubset(CopySize, Dest, MultiIndex<N>(0), *this, Offset, ::Fiber::PlainCopyConvert() ); 
00240 
00241                 return Result;
00242         }
00243 
00244         override RefPtr<MemBase> getComponentArray(int member, const MemBase::Creator_t&C)
00245         {
00246                 if (member >= MetaInfo<T>::MULTIPLICITY)
00247                         return MemCore::NullPtr();
00248 
00249         Iterator<T>         &me = *this;
00250         Iterator<element_t> component_ptr = me.getComponent(member); 
00251 
00252                 return new ComponentArray_t( component_ptr, MultiArray_t::Size(), this->Storage, C );
00253         }
00254 
00256         override const type_info&getType() const
00257         {
00258                 return typeid(T);
00259         }
00260 
00261         override const FiberTypeBase&getFiberType() const
00262         {
00263                 return MemArrayAllocator<T>::getFiberType();
00264         }
00265 
00266 /*
00267         RefPtr<MemBase> computeNthDerivative(int N)
00268         {
00269                 DeriveArray<Interpol, Dims>::computeNthDerivative(result, this, N);
00270         }
00271 */
00272 };
00273 
00274 
00275 
00276 template <class T>
00277 inline RefPtr<MemBase> MemArrayAllocator<T>::create(int rank, const index_t dims[], const WeakPtr<CreativeArrayBase>&theCreator) const
00278 {
00279         switch(rank)
00280         {
00281         default: return MemCore::NullPtr(); 
00282 
00283         case 1:  return new MemArray<1, T>(MIndex(dims[0]), theCreator ) ;
00284                         
00285         case 2:  return new MemArray<2, T>(MIndex(dims[0], dims[1]),theCreator ) ; 
00286                         
00287         case 3:  return new MemArray<3, T>(MIndex(dims[0], dims[1], dims[2]), theCreator ) ;
00288 
00289         case 4:  return new MemArray<4, T>(MIndex(dims[0], dims[1], dims[2], dims[3]),theCreator ) ;
00290         }
00291 }
00292 
00293 template <int N, class T>
00294 bool    MemArray<N,T>::TypeIsRegistered = MemArrayAllocator<T>::isRegistered();
00295 
00296 
00297 
00298 
00299 
00300 
00301 template <int N>
00302 class MemArrayGetSlice
00303 {
00304 public:
00305         template <class T>
00306 static  RefPtr<MemArray<N-1, T> > getSlice(const MemArray<N,T>&M, index_t i, const MemBase::Creator_t&C)
00307         {
00308                 return new MemArray<N-1,T>(M.StoragePtr(), M[i], C );
00309         }
00310 };
00311 
00312 
00313 template <>
00314 class MemArrayGetSlice<1>
00315 {
00316 public:
00317         template <class T>
00318 static  MemCore::NullPtr getSlice(const MemArray<1,T>&, index_t i, const MemBase::Creator_t&C)
00319         {
00320                 return MemCore::NullPtr();
00321         }
00322 };
00323 
00324 
00325 template <int N, class T>
00326 inline RefPtr<MemBase> MemArray<N,T>::getSlice(index_t i, const MemBase::Creator_t&C) const
00327 {
00328         return MemArrayGetSlice<N>::getSlice(*this, i, C);
00329 }
00330 
00331 template <class T>
00332 inline RefPtr<MemBase> makeMemArray1D(const RefPtr<MemCore::TypedChunk<T> >&DataChunk, const MemBase::Creator_t&C)
00333 {
00334         if (!DataChunk)
00335                 return ::MemCore::NullPtr(); 
00336 
00337         return new MemArray<1, T>( DataChunk, MIndex(DataChunk->size() ), C);
00338 }
00339 
00340 template <class T>
00341 inline RefPtr<MemBase> makeMemArray1D(const RefPtr<MemCore::Chunk<T> >&DataChunk, const MemBase::Creator_t&C)
00342 {
00343         if (!DataChunk)
00344                 return ::MemCore::NullPtr(); 
00345 
00346         return new MemArray<1, T>( DataChunk, MIndex(DataChunk->size() ), C);
00347 }
00348 
00349 
00350 } /* namespace Fiber */ 
00351 
00352 #endif /* __FIBER_MEMARRAY_HPP */
00353