alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2021 Arm Limited. All rights reserved. |
| 3 | * SPDX-License-Identifier: Apache-2.0 |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | #ifndef DATA_STRUCTURES_HPP |
| 18 | #define DATA_STRUCTURES_HPP |
| 19 | |
| 20 | #include "hal.h" |
| 21 | |
| 22 | #include <iterator> |
| 23 | |
| 24 | namespace arm { |
| 25 | namespace app { |
| 26 | |
| 27 | /** |
| 28 | * Class Array2d is a data structure that represents a two dimensional array. |
| 29 | * The data is allocated in contiguous memory, arranged row-wise |
| 30 | * and individual elements can be accessed with the () operator. |
| 31 | * For example a two dimensional array D of size (M, N) can be accessed: |
| 32 | * |
| 33 | * _|<------------- col size = N -------->| |
| 34 | * | D(r=0, c=0) D(r=0, c=1)... D(r=0, c=N) |
| 35 | * | D(r=1, c=0) D(r=1, c=1)... D(r=1, c=N) |
| 36 | * | ... |
| 37 | * row size = M ... |
| 38 | * | ... |
| 39 | * _ D(r=M, c=0) D(r=M, c=1)... D(r=M, c=N) |
| 40 | * |
| 41 | */ |
| 42 | template<typename T> |
| 43 | class Array2d { |
| 44 | public: |
| 45 | /** |
| 46 | * @brief Creates the array2d with the given sizes. |
| 47 | * @param[in] rows Number of rows. |
| 48 | * @param[in] cols Number of columns. |
| 49 | */ |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 50 | Array2d(unsigned rows, unsigned cols): m_rows(rows), m_cols(cols) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 51 | { |
| 52 | if (rows == 0 || cols == 0) { |
| 53 | printf_err("Array2d constructor has 0 size.\n"); |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 54 | m_data = nullptr; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 55 | return; |
| 56 | } |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 57 | m_data = new T[rows * cols]; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | ~Array2d() |
| 61 | { |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 62 | delete[] m_data; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | T& operator() (unsigned int row, unsigned int col) |
| 66 | { |
| 67 | #if defined(DEBUG) |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 68 | if (row >= m_rows || col >= m_cols || m_data == nullptr) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 69 | printf_err("Array2d subscript out of bounds.\n"); |
| 70 | } |
| 71 | #endif /* defined(DEBUG) */ |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 72 | return m_data[m_cols * row + col]; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | T operator() (unsigned int row, unsigned int col) const |
| 76 | { |
| 77 | #if defined(DEBUG) |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 78 | if (row >= m_rows || col >= m_cols || m_data == nullptr) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 79 | printf_err("const Array2d subscript out of bounds.\n"); |
| 80 | } |
| 81 | #endif /* defined(DEBUG) */ |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 82 | return m_data[m_cols * row + col]; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /** |
| 86 | * @brief Gets rows number of the current array2d. |
| 87 | * @return Number of rows. |
| 88 | */ |
| 89 | size_t size(size_t dim) |
| 90 | { |
| 91 | switch (dim) |
| 92 | { |
| 93 | case 0: |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 94 | return m_rows; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 95 | case 1: |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 96 | return m_cols; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 97 | default: |
| 98 | return 0; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * @brief Gets the array2d total size. |
| 104 | */ |
| 105 | size_t totalSize() |
| 106 | { |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 107 | return m_rows * m_cols; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /** |
| 111 | * array2d iterator. |
| 112 | */ |
| 113 | using iterator=T*; |
| 114 | using const_iterator=T const*; |
| 115 | |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 116 | iterator begin() { return m_data; } |
| 117 | iterator end() { return m_data + totalSize(); } |
| 118 | const_iterator begin() const { return m_data; } |
| 119 | const_iterator end() const { return m_data + totalSize(); }; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 120 | |
| 121 | private: |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 122 | size_t m_rows; |
| 123 | size_t m_cols; |
| 124 | T* m_data; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | } /* namespace app */ |
| 128 | } /* namespace arm */ |
| 129 | |
| 130 | #endif /* DATA_STRUCTURES_HPP */ |