2 * Copyright (c) 2017-2018 ARM Limited.
4 * SPDX-License-Identifier: MIT
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:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
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
24 #ifndef __ARM_COMPUTE_DIMENSIONS_H__
25 #define __ARM_COMPUTE_DIMENSIONS_H__
27 #include "arm_compute/core/Error.h"
36 /** Constant value used to indicate maximum dimensions of a Window, TensorShape and Coordinates */
37 constexpr size_t MAX_DIMS = 6;
39 /** Dimensions with dimensionality */
44 /** Number of dimensions the tensor has */
45 static constexpr size_t num_max_dimensions = MAX_DIMS;
47 /** Constructor to initialize the tensor shape.
49 * @param[in] dims Values to initialize the dimensions.
51 template <typename... Ts>
52 explicit Dimensions(Ts... dims)
53 : _id{ { static_cast<T>(dims)... } }, _num_dimensions{ sizeof...(dims) }
57 /** Allow instances of this class to be copy constructed */
58 Dimensions(const Dimensions &) = default;
60 /** Allow instances of this class to be copied */
61 Dimensions &operator=(const Dimensions &) = default;
63 /** Allow instances of this class to be move constructed */
64 Dimensions(Dimensions &&) = default;
66 /** Allow instances of this class to be moved */
67 Dimensions &operator=(Dimensions &&) = default;
69 /** Accessor to set the value of one of the dimensions.
71 * @param[in] dimension Dimension for which the value is set.
72 * @param[in] value Value to be set for the dimension.
74 void set(size_t dimension, T value)
76 ARM_COMPUTE_ERROR_ON(dimension >= num_max_dimensions);
77 _id[dimension] = value;
78 _num_dimensions = std::max(_num_dimensions, dimension + 1);
80 /** Alias to access the size of the first dimension */
85 /** Alias to access the size of the second dimension */
90 /** Alias to access the size of the third dimension */
95 /** Generic accessor to get the size of any dimension
97 * @note Precondition: dimension < Dimensions::num_max_dimensions
99 * @param[in] dimension Dimension of the wanted size
101 * @return The size of the requested dimension.
103 const T &operator[](size_t dimension) const
105 ARM_COMPUTE_ERROR_ON(dimension >= num_max_dimensions);
106 return _id[dimension];
108 /** Generic accessor to get the size of any dimension
110 * @note Precondition: dimension < Dimensions::num_max_dimensions
112 * @param[in] dimension Dimension of the wanted size
114 * @return The size of the requested dimension.
116 T &operator[](size_t dimension)
118 ARM_COMPUTE_ERROR_ON(dimension >= num_max_dimensions);
119 return _id[dimension];
121 /** Returns the effective dimensionality of the tensor */
122 unsigned int num_dimensions() const
124 return _num_dimensions;
127 /** Set number of dimensions */
128 void set_num_dimensions(size_t num_dimensions)
130 _num_dimensions = num_dimensions;
133 /** Collapse dimensions.
135 * @param[in] n Number of dimensions to collapse into @p first.
136 * @param[in] first Dimensions into which the following @p n are collapsed.
138 void collapse(const size_t n, const size_t first = 0)
140 ARM_COMPUTE_ERROR_ON(first + n > _id.size());
142 const size_t last = std::min(_num_dimensions, first + n);
144 if(last > (first + 1))
146 // Collapse dimensions into the first
147 _id[first] = std::accumulate(&_id[first], &_id[last], 1, std::multiplies<T>());
148 // Shift the remaining dimensions down
149 std::copy(&_id[last], &_id[_num_dimensions], &_id[first + 1]);
150 // Reduce the number of dimensions
151 const size_t old_num_dimensions = _num_dimensions;
152 _num_dimensions -= last - first - 1;
153 // Fill the now empty dimensions with zero
154 std::fill(&_id[_num_dimensions], &_id[old_num_dimensions], 0);
158 /** Collapse dimensions starting from a given point
160 * @param[in] start Starting point of collapsing dimensions
162 void collapse_from(size_t start)
164 ARM_COMPUTE_ERROR_ON(start > num_dimensions());
166 collapse(num_dimensions() - start, start);
169 /** Returns a read/write iterator that points to the first element in the dimension array.
171 * @return an iterator.
173 typename std::array<T, num_max_dimensions>::iterator begin()
177 /** Returns a read-only (constant) iterator that points to the first element in the dimension array.
179 * @return an iterator.
181 typename std::array<T, num_max_dimensions>::const_iterator begin() const
185 /** Returns a read-only (constant) iterator that points to the first element in the dimension array.
187 * @return an iterator.
189 typename std::array<T, num_max_dimensions>::const_iterator cbegin() const
193 /** Returns a read/write iterator that points one past the last element in the dimension array.
195 * @return an iterator.
197 typename std::array<T, num_max_dimensions>::iterator end()
201 /** Returns a read-only (constant) iterator that points one past the last element in the dimension array.
203 * @return an iterator.
205 typename std::array<T, num_max_dimensions>::const_iterator end() const
209 /** Returns a read-only (constant) iterator that points one past the last element in the dimension array.
211 * @return an iterator.
213 typename std::array<T, num_max_dimensions>::const_iterator cend() const
219 /** Protected destructor. */
220 ~Dimensions() = default;
222 std::array<T, num_max_dimensions> _id;
223 size_t _num_dimensions{ 0 };
226 /** Check that given dimensions are equal.
228 * @param[in] lhs Left-hand side Dimensions.
229 * @param[in] rhs Right-hand side Dimensions.
231 * @return True if the given dimensions are equal.
233 template <typename T>
234 inline bool operator==(const Dimensions<T> &lhs, const Dimensions<T> &rhs)
236 return ((lhs.num_dimensions() == rhs.num_dimensions()) && std::equal(lhs.cbegin(), lhs.cend(), rhs.cbegin()));
238 /** Check that given dimensions are not equal.
240 * @param[in] lhs Left-hand side Dimensions.
241 * @param[in] rhs Right-hand side Dimensions.
243 * @return True if the given dimensions are not equal.
245 template <typename T>
246 inline bool operator!=(const Dimensions<T> &lhs, const Dimensions<T> &rhs)
248 return !(lhs == rhs);
251 #endif /*__ARM_COMPUTE_DIMENSIONS_H__*/