[213] | 1 | // %flair:license{
|
---|
| 2 | // This file is part of the Flair framework distributed under the
|
---|
| 3 | // CECILL-C License, Version 1.0.
|
---|
| 4 | // %flair:license}
|
---|
| 5 | // created: 2018/01/30
|
---|
| 6 | // filename: Matrix_impl.cpp
|
---|
| 7 | //
|
---|
| 8 | // author: Guillaume Sanahuja
|
---|
| 9 | // Copyright Heudiasyc UMR UTC/CNRS 7253
|
---|
| 10 | //
|
---|
| 11 | // version: $Id: $
|
---|
| 12 | //
|
---|
| 13 | // purpose: classe definissant le type de données Matrix
|
---|
| 14 | //
|
---|
| 15 | /*********************************************************************/
|
---|
| 16 |
|
---|
| 17 | #include "Matrix.h"
|
---|
| 18 | #include "Matrix_impl.h"
|
---|
| 19 | #include <typeinfo>
|
---|
| 20 | #include <sstream>
|
---|
| 21 |
|
---|
| 22 | using std::string;
|
---|
| 23 | using namespace flair::core;
|
---|
| 24 |
|
---|
| 25 | Matrix_impl::Matrix_impl(Matrix *self, int rows, int cols,
|
---|
| 26 | flair::core::ScalarType const &_elementDataType,
|
---|
| 27 | int n)
|
---|
| 28 | : elementDataType(_elementDataType) {
|
---|
[318] | 29 | descriptor = new MatrixDescriptor(rows, cols);
|
---|
[213] | 30 | Init(self, n);
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | Matrix_impl::Matrix_impl(Matrix *self,
|
---|
[318] | 34 | const MatrixDescriptor *inDescriptor,
|
---|
[213] | 35 | flair::core::ScalarType const &_elementDataType,
|
---|
| 36 | int n)
|
---|
| 37 | : elementDataType(_elementDataType) {
|
---|
[318] | 38 | descriptor = new MatrixDescriptor(inDescriptor->Rows(), inDescriptor->Cols());
|
---|
[213] | 39 |
|
---|
| 40 | for (uint32_t i = 0; i < descriptor->Rows(); i++) {
|
---|
| 41 | for (uint32_t j = 0; j < descriptor->Cols(); j++) {
|
---|
| 42 | descriptor->SetElementName(i, j, inDescriptor->ElementName(i, j));
|
---|
| 43 | }
|
---|
| 44 | }
|
---|
| 45 | Init(self, n);
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | void Matrix_impl::Init(Matrix *self, int n) {
|
---|
| 49 | this->self = self;
|
---|
| 50 |
|
---|
| 51 | datas = nullptr;
|
---|
| 52 | try {
|
---|
| 53 | ScalarType const &scalarType =
|
---|
| 54 | dynamic_cast<ScalarType const &>(elementDataType);
|
---|
| 55 | if (typeid(scalarType) == typeid(FloatType)) {
|
---|
[214] | 56 | datas =malloc(descriptor->Rows() * descriptor->Cols() * sizeof(float));
|
---|
| 57 | } else if (typeid(scalarType) == typeid(SignedIntegerType) || typeid(scalarType) == typeid(UnsignedIntegerType) ) {
|
---|
[213] | 58 | switch (elementDataType.GetSize()) {
|
---|
| 59 | case 1:
|
---|
[214] | 60 | datas=malloc(descriptor->Rows() * descriptor->Cols() * sizeof(uint8_t));
|
---|
[213] | 61 | break;
|
---|
| 62 | case 2:
|
---|
[214] | 63 | datas=malloc(descriptor->Rows() * descriptor->Cols() * sizeof(uint16_t));
|
---|
[213] | 64 | break;
|
---|
| 65 | default:
|
---|
| 66 | self->Err("unsupported integer scalar type\n");
|
---|
[214] | 67 | }
|
---|
[213] | 68 | } else {
|
---|
| 69 | self->Err("unsupported scalar type\n");
|
---|
| 70 | }
|
---|
| 71 | } catch (std::bad_cast e) {
|
---|
| 72 | self->Err("type is not a scalar\n");
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | if (datas == nullptr)
|
---|
| 76 | self->Err("allocating matrix failed\n");
|
---|
| 77 | if (n > 1)
|
---|
| 78 | self->Warn("n>1 not supported\n");
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | Matrix_impl::~Matrix_impl() {
|
---|
| 82 | free(datas);
|
---|
| 83 | delete descriptor;
|
---|
| 84 | }
|
---|
[214] | 85 |
|
---|
| 86 | float Matrix_impl::ValueNoMutex(uint32_t row, uint32_t col) const {
|
---|
| 87 | ScalarType const &scalarType =
|
---|
| 88 | dynamic_cast<ScalarType const &>(elementDataType);
|
---|
| 89 | if (typeid(scalarType) == typeid(FloatType)) {
|
---|
| 90 | return ((float*)datas)[row*descriptor->Cols()+col];
|
---|
| 91 | } else if (typeid(scalarType) == typeid(SignedIntegerType)) {
|
---|
| 92 | switch (elementDataType.GetSize()) {
|
---|
| 93 | case 1:
|
---|
| 94 | return ((int8_t*)datas)[row*descriptor->Cols()+col];
|
---|
| 95 | break;
|
---|
| 96 | case 2:
|
---|
| 97 | return ((int16_t*)datas)[row*descriptor->Cols()+col];
|
---|
| 98 | break;
|
---|
| 99 | default:
|
---|
| 100 | self->Err("unsupported signed integer scalar type\n");
|
---|
| 101 | }
|
---|
| 102 | } else if (typeid(scalarType) == typeid(UnsignedIntegerType)) {
|
---|
| 103 | switch (elementDataType.GetSize()) {
|
---|
| 104 | case 1:
|
---|
| 105 | return ((uint8_t*)datas)[row*descriptor->Cols()+col];
|
---|
| 106 | break;
|
---|
| 107 | case 2:
|
---|
| 108 | return ((uint16_t*)datas)[row*descriptor->Cols()+col];
|
---|
| 109 | break;
|
---|
| 110 | default:
|
---|
| 111 | self->Err("unsupported unsigned integer scalar type\n");
|
---|
| 112 | }
|
---|
| 113 | } else {
|
---|
| 114 | self->Err("unsupported scalar type\n");
|
---|
| 115 | }
|
---|
| 116 | return 0;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | void Matrix_impl::SetValueNoMutex(uint32_t row, uint32_t col, float value) {
|
---|
| 120 | ScalarType const &scalarType =
|
---|
| 121 | dynamic_cast<ScalarType const &>(elementDataType);
|
---|
| 122 | if (typeid(scalarType) == typeid(FloatType)) {
|
---|
| 123 | ((float*)datas)[row*descriptor->Cols()+col]=value;
|
---|
| 124 | } else if (typeid(scalarType) == typeid(SignedIntegerType)) {
|
---|
| 125 | switch (elementDataType.GetSize()) {
|
---|
| 126 | case 1:
|
---|
| 127 | ((int8_t*)datas)[row*descriptor->Cols()+col]=value;
|
---|
| 128 | break;
|
---|
| 129 | case 2:
|
---|
| 130 | ((int16_t*)datas)[row*descriptor->Cols()+col]=value;
|
---|
| 131 | break;
|
---|
| 132 | default:
|
---|
| 133 | self->Err("unsupported signed integer scalar type\n");
|
---|
| 134 | }
|
---|
| 135 | } else if (typeid(scalarType) == typeid(UnsignedIntegerType)) {
|
---|
| 136 | switch (elementDataType.GetSize()) {
|
---|
| 137 | case 1:
|
---|
| 138 | ((uint8_t*)datas)[row*descriptor->Cols()+col]=value;
|
---|
| 139 | break;
|
---|
| 140 | case 2:
|
---|
| 141 | ((uint16_t*)datas)[row*descriptor->Cols()+col]=value;
|
---|
| 142 | break;
|
---|
| 143 | default:
|
---|
| 144 | self->Err("unsupported unsigned integer scalar type\n");
|
---|
| 145 | }
|
---|
| 146 | } else {
|
---|
| 147 | self->Err("unsupported scalar type\n");
|
---|
| 148 | }
|
---|
| 149 | } |
---|