37#ifndef ARRAY_2D_WRAPPER_H
38#define ARRAY_2D_WRAPPER_H
40#include "data/arrayWrapper.h"
41#include "data/dataHandler.h"
42#include "data/demangle.h"
85 mutable std::map<std::type_index, std::tuple<size_t, size_t, size_t>>
121 std::vector<T>* ptr =
nullptr);
139 const std::type_info& type)
const override;
143 const std::type_info& type,
const size_t address)
const override;
147 const size_t address)
const override;
149#ifdef CODE_GENERATION
173 template <
typename T>
175 const std::type_info& type,
const size_t address)
const
178 std::vector<size_t> result;
184 this->getAddressSpace(type, &arrayHeight, &arrayWidth);
185 if (space > address) {
187 if (type ==
typeid(T)) {
188 result.push_back(address);
192 size_t addressH = address / (this->width - arrayWidth + 1);
193 size_t addressW = address % (this->width - arrayWidth + 1);
194 size_t addressSrc = (addressH * this->width) + addressW;
196 for (
size_t idxHeight = 0; idxHeight < arrayHeight;
198 for (
size_t idxWidth = 0; idxWidth < arrayWidth;
200 size_t idxSrc = (idxHeight * this->width) + idxWidth;
201 result.push_back(addressSrc + idxSrc);
209 template <
typename T>
213 return this->getAddressSpace(type,
nullptr,
nullptr);
216 template <
typename T>
218 size_t* dim1,
size_t* dim2)
const
221 auto iter = this->cachedAddressSpace.find(type);
222 if (iter != this->cachedAddressSpace.end()) {
223 if (dim1 !=
nullptr) {
224 *dim1 = std::get<1>(iter->second);
226 if (dim2 !=
nullptr) {
227 *dim2 = std::get<2>(iter->second);
229 return std::get<0>(iter->second);
234 if (type ==
typeid(T)) {
235 this->cachedAddressSpace.emplace(
236 type, std::make_tuple(this->nbElements, 0, 0));
237 result = this->nbElements;
243 std::string typeName = DEMANGLE_TYPEID_NAME(type.name());
244 std::string regex{DEMANGLE_TYPEID_NAME(
typeid(T).name())};
245 regex.append(
"\\s*(const\\s*)?\\[([0-9]+)\\](\\[([0-9]+)\\])?");
246 std::regex arrayType(regex);
248 if (std::regex_match(typeName.c_str(), cm, arrayType)) {
251 if (cm[2].matched && !cm[4].matched) {
254 typeW = std::atoi(cm[2].str().c_str());
256 else if (cm[2].matched && cm[4].matched) {
258 typeH = std::atoi(cm[2].str().c_str());
259 typeW = std::atoi(cm[4].str().c_str());
266 if (typeH <= this->height && typeW <= this->width) {
267 result = (this->height - typeH + 1) *
268 ((this->width - typeW + 1));
269 if (dim1 !=
nullptr) {
272 if (dim2 !=
nullptr) {
275 this->cachedAddressSpace.emplace(
276 type, std::make_tuple(result, typeH, typeW));
284 template <
typename T>
286 const size_t address)
const
293 if (type ==
typeid(T)) {
295 &(this->containerPtr->at(address)),
296 UntypedSharedPtr::emptyDestructor<const T>());
304 size_t arrayHeight = 0;
305 size_t arrayWidth = 0;
306 size_t addressableSpace =
307 this->getAddressSpace(type, &arrayHeight, &arrayWidth);
311 auto array =
new T[arrayHeight * arrayWidth];
314 size_t addressH = address / (this->width - arrayWidth + 1);
315 size_t addressW = address % (this->width - arrayWidth + 1);
316 size_t addressSrc = (addressH * this->width) + addressW;
318 for (
size_t idxHeight = 0; idxHeight < arrayHeight; idxHeight++) {
319 for (
size_t idxWidth = 0; idxWidth < arrayWidth; idxWidth++) {
320 size_t idxSrc = (idxHeight * this->width) + idxWidth;
321 array[idxDst++] = this->containerPtr->at(addressSrc + idxSrc);
327 std::make_shared<UntypedSharedPtr::Model<const T[]>>(array)};
331#ifdef CODE_GENERATION
335 std::vector<size_t> sizes = {height, width};
343#include "data/primitiveTypeArray2D.h"
DataHandler for 2D arrays of primitive types.
Definition: array2DWrapper.h:68
size_t width
Number of columns of the 2D array.
Definition: array2DWrapper.h:90
virtual std::vector< size_t > getAddressesAccessed(const std::type_info &type, const size_t address) const override
Inherited from DataHandler.
Definition: array2DWrapper.h:174
size_t height
Number of lines of the 2D array.
Definition: array2DWrapper.h:93
virtual UntypedSharedPtr getDataAt(const std::type_info &type, const size_t address) const override
Inherited from DataHandler.
Definition: array2DWrapper.h:285
virtual ~Array2DWrapper()=default
Default destructor.
size_t getAddressSpace(const std::type_info &type, size_t *dim1, size_t *dim2) const
Utility function for the class.
Definition: array2DWrapper.h:217
virtual std::vector< size_t > getDimensionsSize() const override
Inherited from DataHandler.
Definition: array2DWrapper.h:333
virtual DataHandler * clone() const override
Return a PrimitiveTypeArray2D<T> where all data of the Array2DWrapper has been copied.
Definition: array2DWrapper.h:165
Array2DWrapper(const size_t w=2, const size_t h=4, std::vector< T > *ptr=nullptr)
Constructor for the 2D array.
Definition: array2DWrapper.h:156
Array2DWrapper(const Array2DWrapper &other)=default
Default copy constructor.
Definition: arrayWrapper.h:69
void checkAddressAndType(const std::type_info &type, const size_t &address) const
Definition: arrayWrapper.h:221
Base class for all sources of data to be accessed by a TPG Instruction executed within a Program.
Definition: dataHandler.h:54
DataHandler for 2D arrays of primitive types.
Definition: primitiveTypeArray2D.h:69
Class behaving as a std::shared_ptr whose type is not templated.
Definition: untypedSharedPtr.h:72
Definition: array2DWrapper.h:44