00001 #ifndef Impala_Core_Matrix_VirtualMatrixMemory_h
00002 #define Impala_Core_Matrix_VirtualMatrixMemory_h
00003
00004 #include "Core/Matrix/VirtualMatrix.h"
00005 #include "Core/Matrix/MatFunc.h"
00006 #include "Util/IOBuffer.h"
00007 #include "Core/Array/ReadRaw.h"
00008
00009 namespace Impala
00010 {
00011 namespace Core
00012 {
00013 namespace Matrix
00014 {
00015
00016
00017 template<class ArrayT>
00018 class VirtualMatrixMemory : public VirtualMatrix
00019 {
00020 public:
00021
00022 typedef typename ArrayT::StorType StorT;
00023
00024 VirtualMatrixMemory(ArrayT* mat)
00025 {
00026 mMat = mat;
00027 }
00028
00029 virtual
00030 ~VirtualMatrixMemory()
00031 {
00032 delete mMat;
00033 mMat = 0;
00034 }
00035
00036 virtual int
00037 NrRow()
00038 {
00039 return MatNrRow(mMat);
00040 }
00041
00042 virtual int
00043 NrCol()
00044 {
00045 return MatNrCol(mMat);
00046 }
00047
00048 virtual Mat*
00049 GetStorage()
00050 {
00051 return dynamic_cast<Mat*>(mMat);
00052 }
00053
00054 private:
00055
00056 virtual bool
00057 HasGetRowImplReal64()
00058 {
00059
00060 return (sizeof(StorT) == 8);
00061 }
00062
00063 virtual int
00064 GetRowImpl(int rowNr, Real64* buffer, int bufferSize)
00065 {
00066 return ReadRow(rowNr, buffer, bufferSize);
00067 }
00068
00069 virtual bool
00070 HasGetRowImplReal32()
00071 {
00072
00073 return (sizeof(StorT) == 4);
00074 }
00075
00076 virtual int
00077 GetRowImpl(int rowNr, Real32* buffer, int bufferSize)
00078 {
00079 return ReadRow(rowNr, buffer, bufferSize);
00080 }
00081
00082 template<class OutputT>
00083 int
00084 ReadRow(int rowNr, OutputT* buffer, int bufferSize)
00085 {
00086 if (MatNrCol(mMat) > bufferSize)
00087 {
00088 ILOG_ERROR("[ReadRow] Buffer too small");
00089 return 0;
00090 }
00091 StorT* row = MatE(mMat, rowNr, 0);
00092 for (int i=0 ; i<MatNrCol(mMat) ; i++)
00093 {
00094 buffer[i] = static_cast<OutputT>(row[i]);
00095 }
00096 return MatNrCol(mMat);
00097 }
00098
00099 virtual int
00100 GetDiagonalImpl(Real64* buffer, int bufferSize)
00101 {
00102 if (MatNrCol(mMat) > bufferSize)
00103 {
00104 ILOG_ERROR("[GetDiagonalImpl] Buffer too small");
00105 return 0;
00106 }
00107 for (int i=0 ; i<MatNrRow(mMat) ; i++)
00108 buffer[i] = static_cast<Real64>(*MatE(mMat, i, i));
00109 return MatNrCol(mMat);
00110 }
00111
00112 ArrayT* mMat;
00113
00114 ILOG_VAR_DEC;
00115 };
00116
00117 ILOG_VAR_INIT_TEMPL_1(VirtualMatrixMemory, ArrayT, Impala.Core.Matrix);
00118
00119 }
00120 }
00121 }
00122
00123 #endif