Compute Library  18.05
Utility.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2018 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_MISC_UTILITY_H__
25 #define __ARM_COMPUTE_MISC_UTILITY_H__
26 
27 #include <algorithm>
28 #include <array>
29 #include <limits>
30 #include <numeric>
31 #include <vector>
32 
33 namespace arm_compute
34 {
35 namespace utility
36 {
38 template <std::size_t...>
39 struct index_sequence
40 {
41 };
42 
43 template <std::size_t N, std::size_t... S>
44 struct index_sequence_generator : index_sequence_generator < N - 1, N - 1, S... >
45 {
46 };
47 
48 template <std::size_t... S>
49 struct index_sequence_generator<0u, S...> : index_sequence<S...>
50 {
51  using type = index_sequence<S...>;
52 };
53 
54 template <std::size_t N>
55 using index_sequence_t = typename index_sequence_generator<N>::type;
58 namespace detail
59 {
60 template <std::size_t... S,
61  typename Iterator,
62  typename T = std::array<typename std::iterator_traits<Iterator>::value_type, sizeof...(S)>>
63 T make_array(Iterator first, index_sequence<S...>)
64 {
65  return T{ { first[S]... } };
66 }
67 } // namespace detail
68 
69 template <std::size_t N, typename Iterator>
70 std::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 }
74 
83 template <typename T>
84 inline T clamp(const T &n, const T &lower, const T &upper)
85 {
86  return std::max(lower, std::min(n, upper));
87 }
88 
90 template <typename F>
91 inline void for_each(F &&)
92 {
93 }
94 
101 template <typename F, typename T, typename... Ts>
102 inline void for_each(F &&func, T &&arg, Ts &&... args)
103 {
104  func(std::forward<T>(arg));
105  for_each(std::forward<F>(func), std::forward<Ts>(args)...);
106 }
107 
112 template <typename F, typename T>
113 inline T &&foldl(F &&, T &&value)
114 {
115  return std::forward<T>(value);
116 }
117 
125 template <typename F, typename T, typename U, typename... Us>
126 inline auto foldl(F &&func, T &&initial, U &&value, Us &&... values) -> decltype(func(std::forward<T>(initial), std::forward<U>(value)))
127 {
128  return foldl(std::forward<F>(func), func(std::forward<T>(initial), std::forward<U>(value)), std::forward<Us>(values)...);
129 }
130 
139 template <typename T, typename U>
141 {
142  const auto low = static_cast<U>(std::numeric_limits<T>::lowest());
143  const auto high = static_cast<U>(std::numeric_limits<T>::max());
144  return static_cast<T>(clamp(val, low, high));
145 }
146 
153 template <typename T>
154 std::vector<size_t> sort_indices(const std::vector<T> &v)
155 {
156  std::vector<size_t> idx(v.size());
157  std::iota(idx.begin(), idx.end(), 0);
158 
159  std::sort(idx.begin(), idx.end(),
160  [&v](size_t i1, size_t i2)
161  {
162  return v[i1] < v[i2];
163  });
164 
165  return idx;
166 }
167 
168 inline bool endswith(const std::string &filename, const std::string &suffix)
169 {
170  if(filename.size() < suffix.size())
171  {
172  return false;
173  }
174  return std::equal(suffix.rbegin(), suffix.rend(), filename.rbegin());
175 }
176 
177 } // namespace utility
178 } // namespace arm_compute
179 #endif /* __ARM_COMPUTE_MISC_UTILITY_H__ */
fixed_point< T > min(fixed_point< T > x, fixed_point< T > y)
Definition: FixedPoint.h:897
T clamp(const T &n, const T &lower, const T &upper)
Performs clamping among a lower and upper value.
Definition: Utility.h:84
T saturate_cast(U val)
Type cast with saturation.
Definition: Utility.h:140
This file contains all available output stages for GEMMLowp on OpenCL.
T && foldl(F &&, T &&value)
Base case of foldl.
Definition: Utility.h:113
bool endswith(const std::string &filename, const std::string &suffix)
Definition: Utility.h:168
void for_each(F &&)
Base case of for_each.
Definition: Utility.h:91
T make_array(Iterator first, index_sequence< S... >)
Definition: Utility.h:63
fixed_point< T > max(fixed_point< T > x, fixed_point< T > y)
Definition: FixedPoint.h:902
Iterator updated by execute_window_loop for each window element.
Definition: Helpers.h:284
std::array< typename std::iterator_traits< Iterator >::value_type, N > make_array(Iterator first, Iterator last)
Definition: Utility.h:70
std::vector< size_t > sort_indices(const std::vector< T > &v)
Perform an index sort of a given vector.
Definition: Utility.h:154