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