Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016, 2017 ARM Limited. |
| 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 | */ |
| 24 | #ifndef __ARM_COMPUTE_IARRAY_H__ |
| 25 | #define __ARM_COMPUTE_IARRAY_H__ |
| 26 | |
| 27 | #include "arm_compute/core/Error.h" |
| 28 | #include <cstddef> |
| 29 | #include <cstdint> |
| 30 | |
| 31 | namespace arm_compute |
| 32 | { |
Rob Hughes | d59e5b0 | 2017-08-25 17:05:19 +0100 | [diff] [blame] | 33 | struct KeyPoint; |
| 34 | struct Coordinates2D; |
| 35 | struct DetectionWindow; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 36 | class Size2D; |
Rob Hughes | d59e5b0 | 2017-08-25 17:05:19 +0100 | [diff] [blame] | 37 | struct ROI; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 38 | |
| 39 | /** Array of type T */ |
| 40 | template <class T> |
| 41 | class IArray |
| 42 | { |
| 43 | public: |
| 44 | /** Default constructor */ |
| 45 | IArray() |
| 46 | : _num_values(0), _max_size(0) {}; |
| 47 | /** Constructor: initializes an array which can contain up to max_num_points values |
| 48 | * |
| 49 | * @param[in] max_num_values Maximum number of values the array will be able to stored |
| 50 | */ |
| 51 | IArray(size_t max_num_values) |
| 52 | : _num_values(0), _max_size(max_num_values) |
| 53 | { |
| 54 | } |
| 55 | /** Maximum number of values which can be stored in this array |
| 56 | * |
| 57 | * @return Maximum number of values |
| 58 | */ |
| 59 | size_t max_num_values() const |
| 60 | { |
| 61 | return _max_size; |
| 62 | } |
| 63 | /** Default virtual destructor */ |
| 64 | virtual ~IArray() = default; |
| 65 | /** Number of values currently stored in the array |
| 66 | * |
| 67 | * @return Number of values currently stored in the array or max_num_values + 1 if the array is overflowed. |
| 68 | */ |
| 69 | size_t num_values() const |
| 70 | { |
| 71 | return _num_values; |
| 72 | } |
| 73 | /** Append the passed argument to the end of the array if there is room. |
| 74 | * |
| 75 | * @param[in] val Value to add to the array. |
| 76 | * |
| 77 | * @return True if the point was successfully added to the array. False if the array is full and the point couldn't be added. |
| 78 | */ |
| 79 | bool push_back(const T &val) |
| 80 | { |
| 81 | ARM_COMPUTE_ERROR_ON(0 == _max_size); |
| 82 | if(_num_values >= max_num_values()) |
| 83 | { |
| 84 | _num_values = max_num_values() + 1; |
| 85 | return false; |
| 86 | } |
| 87 | at(_num_values) = val; |
| 88 | _num_values++; |
| 89 | return true; |
| 90 | } |
| 91 | /** Clear all the points from the array. */ |
| 92 | void clear() |
| 93 | { |
| 94 | _num_values = 0; |
| 95 | } |
| 96 | /** Did we lose some values because the array is too small? |
| 97 | * |
| 98 | * @return True if we tried to add a value using push_back() but there wasn't any room left to store it. |
| 99 | * False if all the values were successfully added to the array. |
| 100 | */ |
| 101 | bool overflow() const |
| 102 | { |
| 103 | return _num_values > max_num_values(); |
| 104 | } |
| 105 | /** Pointer to the first element of the array |
| 106 | * |
| 107 | * Other elements of the array can be accessed using buffer()[idx] for 0 <= idx < num_poins(). |
| 108 | * |
| 109 | * @return A pointer to the first element of the array |
| 110 | */ |
| 111 | virtual T *buffer() const = 0; |
| 112 | /** Reference to the element of the array located at the given index |
| 113 | * |
| 114 | * @param[in] index Index of the element |
| 115 | * |
| 116 | * @return A reference to the element of the array located at the given index. |
| 117 | */ |
| 118 | virtual T &at(size_t index) const |
| 119 | { |
| 120 | ARM_COMPUTE_ERROR_ON(buffer() == nullptr); |
| 121 | ARM_COMPUTE_ERROR_ON(index >= max_num_values()); |
| 122 | return buffer()[index]; |
| 123 | } |
| 124 | /** 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, |
| 125 | * "num" elements can't be bigger than the maximum number of values which can be stored in this array. |
| 126 | * |
Anthony Barbier | f202e50 | 2017-11-23 18:02:04 +0000 | [diff] [blame] | 127 | * @param[in] num The new array size in number of elements |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 128 | */ |
| 129 | void resize(size_t num) |
| 130 | { |
| 131 | ARM_COMPUTE_ERROR_ON(num > max_num_values()); |
| 132 | _num_values = num; |
| 133 | }; |
| 134 | |
| 135 | private: |
| 136 | size_t _num_values; |
| 137 | size_t _max_size; |
| 138 | }; |
| 139 | using IKeyPointArray = IArray<KeyPoint>; |
| 140 | using ICoordinates2DArray = IArray<Coordinates2D>; |
Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 141 | using IROIArray = IArray<ROI>; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 142 | using IDetectionWindowArray = IArray<DetectionWindow>; |
| 143 | using ISize2DArray = IArray<Size2D>; |
| 144 | using IUInt8Array = IArray<uint8_t>; |
| 145 | using IUInt16Array = IArray<uint16_t>; |
| 146 | using IUInt32Array = IArray<uint32_t>; |
| 147 | using IInt16Array = IArray<int16_t>; |
| 148 | using IInt32Array = IArray<int32_t>; |
| 149 | using IFloatArray = IArray<float>; |
| 150 | } |
| 151 | #endif /* __ARM_COMPUTE_IARRAY_H__ */ |