2#include <ossia/detail/config.hpp>
4#include <ossia/detail/string_view.hpp>
22template <
typename Vector>
23using iterator_t =
typename std::remove_reference<Vector>::type::iterator;
25template <
typename Vector,
typename Value>
26auto find(Vector&& v,
const Value& val)
noexcept
28 return std::find(std::begin(v), std::end(v), val);
31template <
typename Vector,
typename Fun>
32auto find_if(Vector&& v, Fun fun)
34 return std::find_if(std::begin(v), std::end(v), fun);
37template <
typename Vector,
typename Value>
38auto* 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;
52template <
typename Vector,
typename Fun>
53auto* 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;
59template <
typename Vector,
typename Value>
60bool contains(Vector&& v,
const Value& val)
noexcept
62 return find(v, val) != std::end(v);
65template <
typename Vector,
typename Value>
66void remove_one(Vector&& v,
const Value& val)
68 auto it = find(v, val);
75template <
typename Vector,
typename Function>
76void remove_one_if(Vector& v,
const Function& val)
78 auto it = find_if(v, val);
85template <
typename Vector,
typename Value>
86void remove_erase(Vector& v,
const Value& val)
88 v.erase(std::remove(v.begin(), v.end(), val), v.end());
91template <
typename Vector,
typename Function>
92void remove_erase_if(Vector& v,
const Function& val)
94 v.erase(std::remove_if(v.begin(), v.end(), val), v.end());
97template <
typename Vector,
typename Fun>
98void 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;
106template <
typename Vector,
typename Fun>
107bool any_of(Vector&& v, Fun fun)
noexcept
109 return std::any_of(std::begin(v), std::end(v), fun);
112template <
typename Vector,
typename Fun>
113auto all_of(Vector&& v, Fun fun)
noexcept
115 return std::all_of(std::begin(v), std::end(v), fun);
118template <
typename Vector,
typename Fun>
119bool none_of(Vector&& v, Fun fun)
noexcept
121 return std::none_of(std::begin(v), std::end(v), fun);
124template <
typename Vector,
typename Fun>
125auto remove_if(Vector&& v, Fun fun)
127 return std::remove_if(std::begin(v), std::end(v), fun);
130template <
typename Vector,
typename Fun>
131auto count_if(Vector&& v, Fun fun)
133 return std::count_if(std::begin(v), std::end(v), fun);
136template <
typename Vector,
typename Fun>
137auto max_element(Vector&& v, Fun fun)
139 return std::max_element(std::begin(v), std::end(v), fun);
142template <
typename Vector>
145 return std::sort(std::begin(v), std::end(v));
148template <
typename Vector,
typename T>
149auto fill(Vector&& v,
const T& val)
151 return std::fill(std::begin(v), std::end(v), val);
154template <
typename Vector>
155auto unique(Vector&& v)
157 return std::unique(std::begin(v), std::end(v));
160template <
typename Vector,
typename Fun>
161auto sort(Vector&& v, Fun fun)
163 return std::sort(std::begin(v), std::end(v), fun);
166template <
typename Vector,
typename OutputIterator,
typename Fun>
167auto transform(Vector&& v, OutputIterator it, Fun f)
169 return std::transform(v.begin(), v.end(), it, f);
172template <
typename Array1,
typename Array2>
173auto equal(
const Array1& v,
const Array2& v2)
noexcept
175 return std::equal(std::begin(v), std::end(v), std::begin(v2));
178template <
typename Vector1,
typename Vector2>
179void copy(
const Vector1& source, Vector2& destination)
181 destination.reserve(destination.size() + source.size());
182 std::copy(source.begin(), source.end(), std::back_inserter(destination));
185template <
typename Vector1,
typename Vector2,
typename Pred>
186void copy_if(
const Vector1& source, Vector2& destination, Pred predicate)
188 std::copy_if(source.begin(), source.end(), std::back_inserter(destination), predicate);
191template <
typename T,
typename K>
192auto last_before(T&& container,
const K& k)
194 auto it = container.upper_bound(k);
195 if(it != container.begin())
197 std::advance(it, -1);
202template <
typename T,
typename K>
203auto find_key(T&& vec,
const K& key)
noexcept
206 vec.begin(), vec.end(), [&](
const auto& elt) { return elt.first == key; });
209template <std::
size_t N>
212 static const constexpr auto value = N;
215template <
class F, std::size_t... Is>
216void for_each_in_range(F&& func, std::index_sequence<Is...>)
218 (std::forward<F>(func)(num<Is>{}), ...);
221template <std::
size_t N,
typename F>
222void for_each_in_range(F&& func)
224 for_each_in_range(std::forward<F>(func), std::make_index_sequence<N>());
229template <
class T, std::size_t N, std::size_t... I>
230constexpr std::array<std::remove_cv_t<T>, N>
231to_array_impl(T (&a)[N], std::index_sequence<I...>)
noexcept
237template <
class T, std::
size_t N>
238constexpr 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>{});
243template <
typename... Args>
244constexpr std::array<
const char*,
sizeof...(Args)> make_array(Args&&... args)
noexcept
250void remove_duplicates(T& vec)
255 std::sort(vec.begin(), vec.end());
256 vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
259template <
typename T,
typename Comp>
260void 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());
269template <
typename Container,
typename K,
typename Comp,
typename... Args>
270auto 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>
279auto 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()));
288void 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);
297template <
typename Container,
typename Item>
298int 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);