2 #include <ossia/detail/config.hpp>
4 #include <ossia/detail/string_view.hpp>
22 template <
typename Vector>
23 using iterator_t =
typename std::remove_reference<Vector>::type::iterator;
25 template <
typename Vector,
typename Value>
26 auto find(Vector&& v,
const Value& val) noexcept
28 return std::find(std::begin(v), std::end(v), val);
31 template <
typename Vector,
typename Fun>
32 auto find_if(Vector&& v, Fun fun)
34 return std::find_if(std::begin(v), std::end(v), fun);
37 template <
typename Vector,
typename Value>
38 auto* ptr_find(Vector&& v,
const Value& val) noexcept
40 if constexpr(requires { v.find(val) != v.end(); })
42 auto it = v.find(val);
43 return it != v.end() ? &it->second :
nullptr;
47 auto it = std::find(std::begin(v), std::end(v), val);
48 return it != std::end(v) ? &*it :
nullptr;
52 template <
typename Vector,
typename Fun>
53 auto* ptr_find_if(Vector&& v, Fun fun)
55 auto it = std::find_if(std::begin(v), std::end(v), fun);
56 return it != std::end(v) ? &*it :
nullptr;
59 template <
typename Vector,
typename Value>
60 bool contains(Vector&& v,
const Value& val) noexcept
62 return find(v, val) != std::end(v);
65 template <
typename Vector,
typename Value>
66 void remove_one(Vector&& v,
const Value& val)
68 auto it = find(v, val);
75 template <
typename Vector,
typename Function>
76 void remove_one_if(Vector& v,
const Function& val)
78 auto it = find_if(v, val);
85 template <
typename Vector,
typename Value>
86 void remove_erase(Vector& v,
const Value& val)
88 v.erase(std::remove(v.begin(), v.end(), val), v.end());
91 template <
typename Vector,
typename Function>
92 void remove_erase_if(Vector& v,
const Function& val)
94 v.erase(std::remove_if(v.begin(), v.end(), val), v.end());
97 template <
typename Vector,
typename Fun>
98 void erase_if(Vector& r, Fun f)
100 for(
auto it = std::begin(r); it != std::end(r);)
102 it = f(*it) ? r.erase(it) : ++it;
106 template <
typename Vector,
typename Fun>
107 bool any_of(Vector&& v, Fun fun) noexcept
109 return std::any_of(std::begin(v), std::end(v), fun);
112 template <
typename Vector,
typename Fun>
113 auto all_of(Vector&& v, Fun fun) noexcept
115 return std::all_of(std::begin(v), std::end(v), fun);
118 template <
typename Vector,
typename Fun>
119 bool none_of(Vector&& v, Fun fun) noexcept
121 return std::none_of(std::begin(v), std::end(v), fun);
124 template <
typename Vector,
typename Fun>
125 auto remove_if(Vector&& v, Fun fun)
127 return std::remove_if(std::begin(v), std::end(v), fun);
130 template <
typename Vector,
typename Fun>
131 auto count_if(Vector&& v, Fun fun)
133 return std::count_if(std::begin(v), std::end(v), fun);
136 template <
typename Vector,
typename Fun>
137 auto max_element(Vector&& v, Fun fun)
139 return std::max_element(std::begin(v), std::end(v), fun);
142 template <
typename Vector>
143 auto sort(Vector&& v)
145 return std::sort(std::begin(v), std::end(v));
148 template <
typename Vector,
typename T>
149 auto fill(Vector&& v,
const T& val)
151 return std::fill(std::begin(v), std::end(v), val);
154 template <
typename Vector>
155 auto unique(Vector&& v)
157 return std::unique(std::begin(v), std::end(v));
160 template <
typename Vector,
typename Fun>
161 auto sort(Vector&& v, Fun fun)
163 return std::sort(std::begin(v), std::end(v), fun);
166 template <
typename Vector,
typename OutputIterator,
typename Fun>
167 auto transform(Vector&& v, OutputIterator it, Fun f)
169 return std::transform(v.begin(), v.end(), it, f);
172 template <
typename Array1,
typename Array2>
173 auto equal(
const Array1& v,
const Array2& v2) noexcept
175 return std::equal(std::begin(v), std::end(v), std::begin(v2));
178 template <
typename Vector1,
typename Vector2>
179 void copy(
const Vector1& source, Vector2& destination)
181 destination.reserve(destination.size() + source.size());
182 std::copy(source.begin(), source.end(), std::back_inserter(destination));
185 template <
typename Vector1,
typename Vector2,
typename Pred>
186 void copy_if(
const Vector1& source, Vector2& destination, Pred predicate)
188 std::copy_if(source.begin(), source.end(), std::back_inserter(destination), predicate);
191 template <
typename T,
typename K>
192 auto last_before(T&& container,
const K& k)
194 auto it = container.upper_bound(k);
195 if(it != container.begin())
197 std::advance(it, -1);
202 template <
typename T,
typename K>
203 auto find_key(T&& vec,
const K& key) noexcept
206 vec.begin(), vec.end(), [&](
const auto& elt) { return elt.first == key; });
209 template <std::
size_t N>
212 static const constexpr
auto value = N;
215 template <
class F, std::size_t... Is>
216 void for_each_in_range(F&& func, std::index_sequence<Is...>)
218 (std::forward<F>(func)(num<Is>{}), ...);
221 template <std::
size_t N,
typename F>
222 void for_each_in_range(F&& func)
224 for_each_in_range(std::forward<F>(func), std::make_index_sequence<N>());
229 template <
class T, std::size_t N, std::size_t... I>
230 constexpr std::array<std::remove_cv_t<T>, N>
231 to_array_impl(T (&a)[N], std::index_sequence<I...>) noexcept
237 template <
class T, std::
size_t N>
238 constexpr std::array<std::remove_cv_t<T>, N> to_array(T (&a)[N]) noexcept
240 return detail::to_array_impl(a, std::make_index_sequence<N>{});
243 template <
typename... Args>
244 constexpr std::array<
const char*,
sizeof...(Args)> make_array(Args&&... args) noexcept
249 template <
typename T>
250 void remove_duplicates(T& vec)
255 std::sort(vec.begin(), vec.end());
256 vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
259 template <
typename T,
typename Comp>
260 void remove_duplicates(T& vec, Comp comparator)
265 std::sort(vec.begin(), vec.end(), comparator);
266 vec.erase(std::unique(vec.begin(), vec.end(), comparator), vec.end());
269 template <
typename Container,
typename K,
typename Comp,
typename... Args>
270 auto emplace_sorted(Container& vec,
const K& k, Comp&& comp, Args&&... args)
273 auto it = std::lower_bound(vec.begin(), vec.end(), k, std::forward<Comp>(comp));
274 return vec.emplace(it, std::forward<Args>(args)...);
278 typename D,
template <
typename,
typename>
typename S,
typename T,
typename Alloc>
279 auto insert_at_end(D& dest, S<T, Alloc>&& src)
282 dest.end(), std::make_move_iterator(src.begin()),
283 std::make_move_iterator(src.end()));
287 template <
typename T>
288 void change_item_position(T& v,
size_t oldIndex,
size_t newIndex)
290 if(oldIndex > newIndex)
291 std::rotate(v.rend() - oldIndex - 1, v.rend() - oldIndex, v.rend() - newIndex);
294 v.begin() + oldIndex, v.begin() + oldIndex + 1, v.begin() + newIndex + 1);
297 template <
typename Container,
typename Item>
298 int index_in_container(Container& vec, Item i) noexcept
300 auto it = std::find(vec.begin(), vec.end(), i);
302 return std::distance(vec.begin(), it);