Go to the documentation of this file.00001 #ifndef DENSEIWRAPPER_HH
00002 #define DENSEIWRAPPER_HH
00003
00004 #include <complex>
00005 #include <string>
00006 #include <algorithm>
00007
00008 using namespace matfile;
00009 namespace concepts {
00010
00029 template<class Scalar>
00030 class DenseIWrapper
00031 {
00032
00033 public:
00034 typedef ArrayDenseWrapper Type;
00035
00036 public:
00040 typedef concepts::DenseMatrix<Scalar> MatrixType;
00041
00042 private:
00043 typedef typename NumTraits<Scalar>::Real RealScalar;
00044 typedef std::complex<RealScalar> ComplexScalar;
00045
00046 private:
00047 bool isComplex_;
00048 MATtype realType_, imagType_;
00049 miINT32_t rows_, cols_;
00050
00051 private:
00052 MatrixType& matrix_;
00053 bool matrixPrepared;
00054
00055 private:
00056 std::string prCopy_, piCopy_;
00057
00058 public:
00070 DenseIWrapper(MatrixType& matrix): matrix_(matrix) { }
00071
00072 void isComplex(bool isComplex) {
00073 isComplex_ = isComplex;
00074 }
00075
00076 void size(const miINT32_t& rows, const miINT32_t& cols) {
00077 rows_ = rows; cols_ = cols;
00078
00079
00080
00081
00082
00083
00084
00085 }
00086
00087 void name(const std::string&) const { }
00088
00089 void realType(const MATtype& type) {
00090 realType_ = type;
00091 }
00092
00093 byte *pr() {
00094 if (realType_ == NumTraits<Scalar>::MatType && NumTraits<Scalar>::isComplex == 0) {
00095 matrixPrepared = true;
00096 return reinterpret_cast<byte*>(&matrix_(0,0));
00097 }
00098 matrixPrepared = false;
00099 prCopy_.reserve(rows_*cols_*typeSize(realType_));
00100 return &prCopy_[0];
00101 }
00102
00103 void imagType(const MATtype& type) {
00104 imagType_ = type;
00105 }
00106
00107 byte *pi() {
00108 if (NumTraits<Scalar>::isComplex == 0)
00109 return NULL;
00110 piCopy_.reserve(rows_*cols_*typeSize(imagType_));
00111 return &piCopy_[0];
00112 }
00113
00114 private:
00115 template<class Other> void copy(bool real = true) {
00116 if (NumTraits<Scalar>::isComplex != 0) {
00117 if (real) {
00118 Other *pr = reinterpret_cast<Other*>(&prCopy_[0]);
00119 ComplexScalar *v = reinterpret_cast<ComplexScalar*>(&matrix_(0,0));
00120 for (miINT32_t i = 0; i < rows_*cols_; ++i)
00121 *v++ = *pr++;
00122 } else if (!real && isComplex_) {
00123 Other *pi = reinterpret_cast<Other*>(&piCopy_[0]);
00124 ComplexScalar *v = reinterpret_cast<ComplexScalar*>(&matrix_(0,0));
00125 for (miINT32_t i = 0; i < rows_*cols_; ++i)
00126 *v++ += ComplexScalar(0, *pi++);
00127 }
00128 return;
00129 }
00130
00131 Other *pr = reinterpret_cast<Other*>(&prCopy_[0]);
00132 Scalar *v = reinterpret_cast<Scalar*>(&matrix_(0,0));
00133 for (miINT32_t i = 0; i < rows_*cols_; ++i)
00134 *v++ = *pr++;
00135 }
00136
00137 public:
00150 void get_matrix() {
00151 if (matrixPrepared)
00152 return ;
00153
00154 MATtype *nowCopy = &realType_;
00155 while (true) {
00156 bool real;
00157 if (nowCopy == &realType_)
00158 real = true;
00159 else
00160 real = false;
00161
00162 switch(*nowCopy) {
00163 case miINT8:
00164 copy<miINT8_t>(real);
00165 break;
00166 case miUINT8:
00167 copy<miUINT8_t>(real);
00168 break;
00169 case miINT16:
00170 copy<miINT16_t>(real);
00171 break;
00172 case miUINT16:
00173 copy<miUINT16_t>(real);
00174 break;
00175 case miINT32:
00176 copy<miINT32_t>(real);
00177 break;
00178 case miUINT32:
00179 copy<miUINT32_t>(real);
00180 break;
00181 case miINT64:
00182 copy<miINT64_t>(real);
00183 break;
00184 case miUINT64:
00185 copy<miUINT64_t>(real);
00186 break;
00187 case miSINGLE:
00188 copy<miSINGLE_t>(real);
00189 break;
00190 case miDOUBLE:
00191 copy<miDOUBLE_t>(real);
00192 break;
00193 }
00194
00195 if (real && isComplex_)
00196 nowCopy = &imagType_;
00197 else
00198 break;
00199 }
00200 return;
00201 }
00202 };
00203
00204
00205 }
00206
00207
00208 #endif //DENSEIWRAPPER_HH