Compute Library
 21.05
IArray.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2021 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 /** Array of type T */
34 template <class T>
35 class IArray
36 {
37 public:
38  /** Default constructor */
40  : _num_values(0), _max_size(0) {};
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  */
46  : _num_values(0), _max_size(max_num_values)
47  {
48  }
49  /** Maximum number of values which can be stored in this array
50  *
51  * @return Maximum number of values
52  */
53  size_t max_num_values() const
54  {
55  return _max_size;
56  }
57  /** Default virtual destructor */
58  virtual ~IArray() = default;
59  /** Number of values currently stored in the array
60  *
61  * @return Number of values currently stored in the array or max_num_values + 1 if the array is overflowed.
62  */
63  size_t num_values() const
64  {
65  return _num_values;
66  }
67  /** Append the passed argument to the end of the array if there is room.
68  *
69  * @param[in] val Value to add to the array.
70  *
71  * @return True if the point was successfully added to the array. False if the array is full and the point couldn't be added.
72  */
73  bool push_back(const T &val)
74  {
75  ARM_COMPUTE_ERROR_ON(0 == _max_size);
76  if(_num_values >= max_num_values())
77  {
78  _num_values = max_num_values() + 1;
79  return false;
80  }
81  at(_num_values) = val;
82  _num_values++;
83  return true;
84  }
85  /** Clear all the points from the array. */
86  void clear()
87  {
88  _num_values = 0;
89  }
90  /** Did we lose some values because the array is too small?
91  *
92  * @return True if we tried to add a value using push_back() but there wasn't any room left to store it.
93  * False if all the values were successfully added to the array.
94  */
95  bool overflow() const
96  {
97  return _num_values > max_num_values();
98  }
99  /** Pointer to the first element of the array
100  *
101  * Other elements of the array can be accessed using buffer()[idx] for 0 <= idx < num_poins().
102  *
103  * @return A pointer to the first element of the array
104  */
105  virtual T *buffer() const = 0;
106  /** Reference to the element of the array located at the given index
107  *
108  * @param[in] index Index of the element
109  *
110  * @return A reference to the element of the array located at the given index.
111  */
112  virtual T &at(size_t index) const
113  {
114  ARM_COMPUTE_ERROR_ON(buffer() == nullptr);
116  return buffer()[index];
117  }
118  /** 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,
119  * "num" elements can't be bigger than the maximum number of values which can be stored in this array.
120  *
121  * @param[in] num The new array size in number of elements
122  */
123  void resize(size_t num)
124  {
126  _num_values = num;
127  };
128 
129 private:
130  size_t _num_values;
131  size_t _max_size;
132 };
133 /** Interface for Array of uint8s. */
135 /** Interface for Array of uint16s. */
137 /** Interface for Array of uint32s. */
139 /** Interface for Array of int16s. */
141 /** Interface for Array of int32s. */
143 /** Interface for Array of floats. */
145 }
146 #endif /* ARM_COMPUTE_IARRAY_H */
void clear()
Clear all the points from the array.
Definition: IArray.h:86
Array of type T.
Definition: IArray.h:35
bool overflow() const
Did we lose some values because the array is too small?
Definition: IArray.h:95
void resize(size_t num)
Resizes the array to contain "num" elements.
Definition: IArray.h:123
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
Copyright (c) 2017-2021 Arm Limited.
IArray()
Default constructor.
Definition: IArray.h:39
virtual T * buffer() const =0
Pointer to the first element of the array.
size_t num_values() const
Number of values currently stored in the array.
Definition: IArray.h:63
IArray(size_t max_num_values)
Constructor: initializes an array which can contain up to max_num_points values.
Definition: IArray.h:45
size_t max_num_values() const
Maximum number of values which can be stored in this array.
Definition: IArray.h:53
bool push_back(const T &val)
Append the passed argument to the end of the array if there is room.
Definition: IArray.h:73
virtual ~IArray()=default
Default virtual destructor.
virtual T & at(size_t index) const
Reference to the element of the array located at the given index.
Definition: IArray.h:112