blob: 8dd9afd5cd5e911851ebcebb51a1cf1c411f8e6b [file] [log] [blame]
Georgios Pinitas8795ffb2017-12-01 16:13:40 +00001/*
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +00002 * Copyright (c) 2017-2018 ARM Limited.
Georgios Pinitas8795ffb2017-12-01 16:13:40 +00003 *
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_MISC_UTILITY_H__
25#define __ARM_COMPUTE_MISC_UTILITY_H__
26
Michalis Spyrou40df1e92018-02-06 16:53:47 +000027#include <algorithm>
Georgios Pinitas8795ffb2017-12-01 16:13:40 +000028#include <array>
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +000029#include <limits>
Michalis Spyrou40df1e92018-02-06 16:53:47 +000030#include <numeric>
31#include <vector>
Georgios Pinitas8795ffb2017-12-01 16:13:40 +000032
33namespace arm_compute
34{
35namespace utility
36{
37/** @cond */
38template <std::size_t...>
39struct index_sequence
40{
41};
42
43template <std::size_t N, std::size_t... S>
44struct index_sequence_generator : index_sequence_generator < N - 1, N - 1, S... >
45{
46};
47
48template <std::size_t... S>
49struct index_sequence_generator<0u, S...> : index_sequence<S...>
50{
51 using type = index_sequence<S...>;
52};
53
54template <std::size_t N>
55using index_sequence_t = typename index_sequence_generator<N>::type;
56/** @endcond */
57
58namespace detail
59{
60template <std::size_t... S,
61 typename Iterator,
62 typename T = std::array<typename std::iterator_traits<Iterator>::value_type, sizeof...(S)>>
63T make_array(Iterator first, index_sequence<S...>)
64{
65 return T{ { first[S]... } };
66}
67} // namespace detail
68
69template <std::size_t N, typename Iterator>
70std::array<typename std::iterator_traits<Iterator>::value_type, N> make_array(Iterator first, Iterator last)
71{
72 return detail::make_array(first, index_sequence_t<N> {});
73}
Diego Lopez Recas490b3d82017-12-19 15:42:25 +000074
75/** Performs clamping among a lower and upper value.
76 *
77 * @param[in] n Value to clamp.
78 * @param[in] lower Lower threshold.
79 * @param[in] upper Upper threshold.
80 *
81 * @return Clamped value.
82 */
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +000083template <typename DataType, typename RangeType = DataType>
84inline DataType clamp(const DataType &n,
85 const DataType &lower = std::numeric_limits<RangeType>::lowest(),
86 const DataType &upper = std::numeric_limits<RangeType>::max())
Diego Lopez Recas490b3d82017-12-19 15:42:25 +000087{
88 return std::max(lower, std::min(n, upper));
89}
90
91/** Base case of for_each. Does nothing. */
92template <typename F>
93inline void for_each(F &&)
94{
95}
96
97/** Call the function for each of the arguments
98 *
99 * @param[in] func Function to be called
100 * @param[in] arg Argument passed to the function
101 * @param[in] args Remaining arguments
102 */
103template <typename F, typename T, typename... Ts>
104inline void for_each(F &&func, T &&arg, Ts &&... args)
105{
106 func(std::forward<T>(arg));
107 for_each(std::forward<F>(func), std::forward<Ts>(args)...);
108}
109
110/** Base case of foldl.
111 *
112 * @return value.
113 */
114template <typename F, typename T>
115inline T &&foldl(F &&, T &&value)
116{
117 return std::forward<T>(value);
118}
119
120/** Fold left.
121 *
122 * @param[in] func Function to be called
123 * @param[in] initial Initial value
124 * @param[in] value Argument passed to the function
125 * @param[in] values Remaining arguments
126 */
127template <typename F, typename T, typename U, typename... Us>
128inline auto foldl(F &&func, T &&initial, U &&value, Us &&... values) -> decltype(func(std::forward<T>(initial), std::forward<U>(value)))
129{
130 return foldl(std::forward<F>(func), func(std::forward<T>(initial), std::forward<U>(value)), std::forward<Us>(values)...);
131}
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000132
Michalis Spyrou40df1e92018-02-06 16:53:47 +0000133/** Perform an index sort of a given vector.
134 *
135 * @param[in] v Vector to sort
136 *
137 * @return Sorted index vector.
138 */
139template <typename T>
140std::vector<size_t> sort_indices(const std::vector<T> &v)
141{
142 std::vector<size_t> idx(v.size());
143 std::iota(idx.begin(), idx.end(), 0);
144
145 std::sort(idx.begin(), idx.end(),
146 [&v](size_t i1, size_t i2)
147 {
148 return v[i1] < v[i2];
149 });
150
151 return idx;
152}
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100153
Georgios Pinitas17b12302018-06-18 18:13:51 +0100154/** Checks if a string contains a given suffix
155 *
156 * @param[in] str Input string
157 * @param[in] suffix Suffix to check for
158 *
159 * @return True if the string ends with the given suffix else false
160 */
161inline bool endswith(const std::string &str, const std::string &suffix)
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100162{
Georgios Pinitas17b12302018-06-18 18:13:51 +0100163 if(str.size() < suffix.size())
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100164 {
165 return false;
166 }
Georgios Pinitas17b12302018-06-18 18:13:51 +0100167 return std::equal(suffix.rbegin(), suffix.rend(), str.rbegin());
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100168}
169
Georgios Pinitas17b12302018-06-18 18:13:51 +0100170/** Checks if a pointer complies with a given alignment
171 *
172 * @param[in] ptr Pointer to check
173 * @param[in] alignment Alignment value
174 *
175 * @return True if the pointer is aligned else false
176 */
177inline bool check_aligned(void *ptr, const size_t alignment)
178{
179 return (reinterpret_cast<std::uintptr_t>(ptr) % alignment) == 0;
180}
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100181
182/** Convert string to lower case.
183 *
184 * @param[in] string To be converted string.
185 *
186 * @return Lower case string.
187 */
188inline std::string tolower(std::string string)
189{
190 std::transform(string.begin(), string.end(), string.begin(), [](unsigned char c)
191 {
192 return std::tolower(c);
193 });
194 return string;
195}
Diego Lopez Recas490b3d82017-12-19 15:42:25 +0000196} // namespace utility
Georgios Pinitas8795ffb2017-12-01 16:13:40 +0000197} // namespace arm_compute
198#endif /* __ARM_COMPUTE_MISC_UTILITY_H__ */