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 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 20 | #include <iterator> |
| 21 | |
| 22 | namespace arm { |
| 23 | namespace app { |
| 24 | |
| 25 | /** |
| 26 | * Class Array2d is a data structure that represents a two dimensional array. |
| 27 | * The data is allocated in contiguous memory, arranged row-wise |
| 28 | * and individual elements can be accessed with the () operator. |
| 29 | * For example a two dimensional array D of size (M, N) can be accessed: |
| 30 | * |
| 31 | * _|<------------- col size = N -------->| |
| 32 | * | D(r=0, c=0) D(r=0, c=1)... D(r=0, c=N) |
| 33 | * | D(r=1, c=0) D(r=1, c=1)... D(r=1, c=N) |
| 34 | * | ... |
| 35 | * row size = M ... |
| 36 | * | ... |
| 37 | * _ D(r=M, c=0) D(r=M, c=1)... D(r=M, c=N) |
| 38 | * |
| 39 | */ |
| 40 | template<typename T> |
| 41 | class Array2d { |
| 42 | public: |
| 43 | /** |
| 44 | * @brief Creates the array2d with the given sizes. |
| 45 | * @param[in] rows Number of rows. |
| 46 | * @param[in] cols Number of columns. |
| 47 | */ |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 48 | Array2d(unsigned rows, unsigned cols): m_rows(rows), m_cols(cols) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 49 | { |
| 50 | if (rows == 0 || cols == 0) { |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame^] | 51 | printf("Array2d constructor has 0 size.\n"); |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 52 | m_data = nullptr; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 53 | return; |
| 54 | } |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 55 | m_data = new T[rows * cols]; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | ~Array2d() |
| 59 | { |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 60 | delete[] m_data; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | T& operator() (unsigned int row, unsigned int col) |
| 64 | { |
| 65 | #if defined(DEBUG) |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 66 | if (row >= m_rows || col >= m_cols || m_data == nullptr) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 67 | printf_err("Array2d subscript out of bounds.\n"); |
| 68 | } |
| 69 | #endif /* defined(DEBUG) */ |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 70 | return m_data[m_cols * row + col]; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | T operator() (unsigned int row, unsigned int col) const |
| 74 | { |
| 75 | #if defined(DEBUG) |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 76 | if (row >= m_rows || col >= m_cols || m_data == nullptr) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 77 | printf_err("const Array2d subscript out of bounds.\n"); |
| 78 | } |
| 79 | #endif /* defined(DEBUG) */ |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 80 | return m_data[m_cols * row + col]; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /** |
| 84 | * @brief Gets rows number of the current array2d. |
| 85 | * @return Number of rows. |
| 86 | */ |
| 87 | size_t size(size_t dim) |
| 88 | { |
| 89 | switch (dim) |
| 90 | { |
| 91 | case 0: |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 92 | return m_rows; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 93 | case 1: |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 94 | return m_cols; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 95 | default: |
| 96 | return 0; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * @brief Gets the array2d total size. |
| 102 | */ |
| 103 | size_t totalSize() |
| 104 | { |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 105 | return m_rows * m_cols; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /** |
| 109 | * array2d iterator. |
| 110 | */ |
| 111 | using iterator=T*; |
| 112 | using const_iterator=T const*; |
| 113 | |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 114 | iterator begin() { return m_data; } |
| 115 | iterator end() { return m_data + totalSize(); } |
| 116 | const_iterator begin() const { return m_data; } |
| 117 | const_iterator end() const { return m_data + totalSize(); }; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 118 | |
| 119 | private: |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 120 | size_t m_rows; |
| 121 | size_t m_cols; |
| 122 | T* m_data; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | } /* namespace app */ |
| 126 | } /* namespace arm */ |
| 127 | |
| 128 | #endif /* DATA_STRUCTURES_HPP */ |