Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | c6f9510 | 2021-03-30 10:03:01 +0100 | [diff] [blame] | 2 | * Copyright (c) 2016-2021 Arm Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_IARRAY_H |
| 25 | #define ARM_COMPUTE_IARRAY_H |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 26 | |
| 27 | #include "arm_compute/core/Error.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 28 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 29 | #include <cstddef> |
| 30 | #include <cstdint> |
| 31 | |
| 32 | namespace arm_compute |
| 33 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 34 | /** Array of type T */ |
| 35 | template <class T> |
| 36 | class IArray |
| 37 | { |
| 38 | public: |
| 39 | /** Default constructor */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 40 | IArray() : _num_values(0), _max_size(0){}; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 41 | /** Constructor: initializes an array which can contain up to max_num_points values |
| 42 | * |
| 43 | * @param[in] max_num_values Maximum number of values the array will be able to stored |
| 44 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 45 | IArray(size_t max_num_values) : _num_values(0), _max_size(max_num_values) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 46 | { |
| 47 | } |
| 48 | /** Maximum number of values which can be stored in this array |
| 49 | * |
| 50 | * @return Maximum number of values |
| 51 | */ |
| 52 | size_t max_num_values() const |
| 53 | { |
| 54 | return _max_size; |
| 55 | } |
| 56 | /** Default virtual destructor */ |
| 57 | virtual ~IArray() = default; |
| 58 | /** Number of values currently stored in the array |
| 59 | * |
| 60 | * @return Number of values currently stored in the array or max_num_values + 1 if the array is overflowed. |
| 61 | */ |
| 62 | size_t num_values() const |
| 63 | { |
| 64 | return _num_values; |
| 65 | } |
| 66 | /** Append the passed argument to the end of the array if there is room. |
| 67 | * |
| 68 | * @param[in] val Value to add to the array. |
| 69 | * |
| 70 | * @return True if the point was successfully added to the array. False if the array is full and the point couldn't be added. |
| 71 | */ |
| 72 | bool push_back(const T &val) |
| 73 | { |
| 74 | ARM_COMPUTE_ERROR_ON(0 == _max_size); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 75 | if (_num_values >= max_num_values()) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 76 | { |
| 77 | _num_values = max_num_values() + 1; |
| 78 | return false; |
| 79 | } |
| 80 | at(_num_values) = val; |
| 81 | _num_values++; |
| 82 | return true; |
| 83 | } |
| 84 | /** Clear all the points from the array. */ |
| 85 | void clear() |
| 86 | { |
| 87 | _num_values = 0; |
| 88 | } |
| 89 | /** Did we lose some values because the array is too small? |
| 90 | * |
| 91 | * @return True if we tried to add a value using push_back() but there wasn't any room left to store it. |
| 92 | * False if all the values were successfully added to the array. |
| 93 | */ |
| 94 | bool overflow() const |
| 95 | { |
| 96 | return _num_values > max_num_values(); |
| 97 | } |
| 98 | /** Pointer to the first element of the array |
| 99 | * |
| 100 | * Other elements of the array can be accessed using buffer()[idx] for 0 <= idx < num_poins(). |
| 101 | * |
| 102 | * @return A pointer to the first element of the array |
| 103 | */ |
| 104 | virtual T *buffer() const = 0; |
| 105 | /** Reference to the element of the array located at the given index |
| 106 | * |
| 107 | * @param[in] index Index of the element |
| 108 | * |
| 109 | * @return A reference to the element of the array located at the given index. |
| 110 | */ |
| 111 | virtual T &at(size_t index) const |
| 112 | { |
| 113 | ARM_COMPUTE_ERROR_ON(buffer() == nullptr); |
| 114 | ARM_COMPUTE_ERROR_ON(index >= max_num_values()); |
| 115 | return buffer()[index]; |
| 116 | } |
| 117 | /** Resizes the array to contain "num" elements. If "num" is smaller than the maximum array size, the content is reduced to its first "num" elements, |
| 118 | * "num" elements can't be bigger than the maximum number of values which can be stored in this array. |
| 119 | * |
Anthony Barbier | f202e50 | 2017-11-23 18:02:04 +0000 | [diff] [blame] | 120 | * @param[in] num The new array size in number of elements |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 121 | */ |
| 122 | void resize(size_t num) |
| 123 | { |
| 124 | ARM_COMPUTE_ERROR_ON(num > max_num_values()); |
| 125 | _num_values = num; |
| 126 | }; |
| 127 | |
| 128 | private: |
| 129 | size_t _num_values; |
| 130 | size_t _max_size; |
| 131 | }; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 132 | /** Interface for Array of uint8s. */ |
| 133 | using IUInt8Array = IArray<uint8_t>; |
| 134 | /** Interface for Array of uint16s. */ |
| 135 | using IUInt16Array = IArray<uint16_t>; |
| 136 | /** Interface for Array of uint32s. */ |
| 137 | using IUInt32Array = IArray<uint32_t>; |
| 138 | /** Interface for Array of int16s. */ |
| 139 | using IInt16Array = IArray<int16_t>; |
| 140 | /** Interface for Array of int32s. */ |
| 141 | using IInt32Array = IArray<int32_t>; |
| 142 | /** Interface for Array of floats. */ |
| 143 | using IFloatArray = IArray<float>; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 144 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 145 | #endif /* ARM_COMPUTE_IARRAY_H */ |