1 /* 2 * Copyright (c) 2016-2019 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 { 33 struct KeyPoint; 34 struct Coordinates2D; 35 struct DetectionWindow; 36 class Size2D; 37 38 /** Array of type T */ 39 template <class T> 40 class IArray 41 { 42 public: 43 /** Default constructor */ IArray()44 IArray() 45 : _num_values(0), _max_size(0) {}; 46 /** Constructor: initializes an array which can contain up to max_num_points values 47 * 48 * @param[in] max_num_values Maximum number of values the array will be able to stored 49 */ IArray(size_t max_num_values)50 IArray(size_t max_num_values) 51 : _num_values(0), _max_size(max_num_values) 52 { 53 } 54 /** Maximum number of values which can be stored in this array 55 * 56 * @return Maximum number of values 57 */ max_num_values()58 size_t max_num_values() const 59 { 60 return _max_size; 61 } 62 /** Default virtual destructor */ 63 virtual ~IArray() = default; 64 /** Number of values currently stored in the array 65 * 66 * @return Number of values currently stored in the array or max_num_values + 1 if the array is overflowed. 67 */ num_values()68 size_t num_values() const 69 { 70 return _num_values; 71 } 72 /** Append the passed argument to the end of the array if there is room. 73 * 74 * @param[in] val Value to add to the array. 75 * 76 * @return True if the point was successfully added to the array. False if the array is full and the point couldn't be added. 77 */ push_back(const T & val)78 bool push_back(const T &val) 79 { 80 ARM_COMPUTE_ERROR_ON(0 == _max_size); 81 if(_num_values >= max_num_values()) 82 { 83 _num_values = max_num_values() + 1; 84 return false; 85 } 86 at(_num_values) = val; 87 _num_values++; 88 return true; 89 } 90 /** Clear all the points from the array. */ clear()91 void clear() 92 { 93 _num_values = 0; 94 } 95 /** Did we lose some values because the array is too small? 96 * 97 * @return True if we tried to add a value using push_back() but there wasn't any room left to store it. 98 * False if all the values were successfully added to the array. 99 */ overflow()100 bool overflow() const 101 { 102 return _num_values > max_num_values(); 103 } 104 /** Pointer to the first element of the array 105 * 106 * Other elements of the array can be accessed using buffer()[idx] for 0 <= idx < num_poins(). 107 * 108 * @return A pointer to the first element of the array 109 */ 110 virtual T *buffer() const = 0; 111 /** Reference to the element of the array located at the given index 112 * 113 * @param[in] index Index of the element 114 * 115 * @return A reference to the element of the array located at the given index. 116 */ at(size_t index)117 virtual T &at(size_t index) const 118 { 119 ARM_COMPUTE_ERROR_ON(buffer() == nullptr); 120 ARM_COMPUTE_ERROR_ON(index >= max_num_values()); 121 return buffer()[index]; 122 } 123 /** 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, 124 * "num" elements can't be bigger than the maximum number of values which can be stored in this array. 125 * 126 * @param[in] num The new array size in number of elements 127 */ resize(size_t num)128 void resize(size_t num) 129 { 130 ARM_COMPUTE_ERROR_ON(num > max_num_values()); 131 _num_values = num; 132 }; 133 134 private: 135 size_t _num_values; 136 size_t _max_size; 137 }; 138 /** Interface for Array of Key Points. */ 139 using IKeyPointArray = IArray<KeyPoint>; 140 /** Interface for Array of 2D Coordinates. */ 141 using ICoordinates2DArray = IArray<Coordinates2D>; 142 /** Interface for Array of Detection Windows. */ 143 using IDetectionWindowArray = IArray<DetectionWindow>; 144 /** Interface for Array of 2D Sizes. */ 145 using ISize2DArray = IArray<Size2D>; 146 /** Interface for Array of uint8s. */ 147 using IUInt8Array = IArray<uint8_t>; 148 /** Interface for Array of uint16s. */ 149 using IUInt16Array = IArray<uint16_t>; 150 /** Interface for Array of uint32s. */ 151 using IUInt32Array = IArray<uint32_t>; 152 /** Interface for Array of int16s. */ 153 using IInt16Array = IArray<int16_t>; 154 /** Interface for Array of int32s. */ 155 using IInt32Array = IArray<int32_t>; 156 /** Interface for Array of floats. */ 157 using IFloatArray = IArray<float>; 158 } 159 #endif /* ARM_COMPUTE_IARRAY_H */ 160