BRICKS
Small, useful blocks of code, to build bigger things.
Loading...
Searching...
No Matches
algorithm.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <functional>
4#include <future>
5#include <string>
6#include <type_traits>
7#include <vector>
8
11
12namespace bricks {
13
24template <class Container>
25auto keys(const Container& input_map) -> std::vector<typename Container::key_type>
26{
27 std::vector<typename Container::key_type> retval;
28 retval.reserve(input_map.size());
29 std::transform(std::begin(input_map), std::end(input_map), std::back_inserter(retval),
30 [](auto&& pair) { return std::get<0>(std::forward<decltype(pair)>(pair)); });
31
32 return retval;
33}
34
45template <class Container>
46auto values(const Container& input_map) -> std::vector<typename Container::mapped_type>
47{
48 std::vector<typename Container::mapped_type> retval;
49 retval.reserve(input_map.size());
50 std::transform(std::begin(input_map), std::end(input_map), std::back_inserter(retval),
51 [](auto&& pair) { return std::get<1>(std::forward<decltype(pair)>(pair)); });
52
53 return retval;
54}
55
67template <typename F, typename... FrontArgs>
68constexpr auto bind_front(F&& f, FrontArgs&&... front_args)
69{
70 // front_args are copied because multiple invocations of this closure are possible
71 return [captured_f = std::forward<F>(f), front_args...](auto&&... back_args) {
72 return std::invoke(captured_f, front_args..., std::forward<decltype(back_args)>(back_args)...);
73 };
74}
75
94template <class Container, class Value>
95constexpr auto contains(const Container& container, const Value& value) noexcept -> bool
96{
97 return detail::contains(container, value);
98}
99
112template <class Container, class UnaryPredicate>
113constexpr auto contains_if(const Container& container, const UnaryPredicate& predicate) -> bool
114{
115 return std::find_if(std::begin(container), std::end(container), predicate) != std::end(container);
116}
117
135template <class Container, class Value>
136constexpr auto index_of(const Container& container, const Value& value) noexcept
137 -> std::optional<size_t>
138{
139 return detail::index_of(container, value);
140}
141
154template <class Container, class UnaryPredicate>
155constexpr auto index_of_if(const Container& container, const UnaryPredicate& predicate)
156 -> std::optional<size_t>
157{
158 const auto it = std::find_if(std::begin(container), std::end(container), predicate);
159 return it != std::end(container) ? std::make_optional(std::distance(std::begin(container), it))
160 : std::nullopt;
161}
162
174template <class T, class Rep, class Period>
175auto is_ready_after(const std::future<T>& future,
176 const std::chrono::duration<Rep, Period>& timeout) noexcept -> bool
177{
178 return future.wait_for(timeout) == std::future_status::ready;
179}
180
192template <class T, class Clock, class Duration>
193auto is_ready_at(const std::future<T>& future,
194 const std::chrono::time_point<Clock, Duration>& timeout) noexcept -> bool
195{
196 return future.wait_until(timeout) == std::future_status::ready;
197}
198
199} // namespace bricks
Definition algorithm.hpp:12
constexpr auto index_of_if(const Container &container, const UnaryPredicate &predicate) -> std::optional< size_t >
Get the index of the first element in a container for which a predicate is true.
Definition algorithm.hpp:155
constexpr auto contains(const Container &container, const Value &value) noexcept -> bool
Checks whether a container contains a specific value.
Definition algorithm.hpp:95
constexpr auto index_of(const Container &container, const Value &value) noexcept -> std::optional< size_t >
Get the index of the first occurence of a value in a container.
Definition algorithm.hpp:136
constexpr auto bind_front(F &&f, FrontArgs &&... front_args)
Bind arguments to the front of a function.
Definition algorithm.hpp:68
auto values(const Container &input_map) -> std::vector< typename Container::mapped_type >
Get the values of an associative container.
Definition algorithm.hpp:46
auto is_ready_at(const std::future< T > &future, const std::chrono::time_point< Clock, Duration > &timeout) noexcept -> bool
Check whether a future is ready at a specific time.
Definition algorithm.hpp:193
auto keys(const Container &input_map) -> std::vector< typename Container::key_type >
Get the keys of an associative container.
Definition algorithm.hpp:25
constexpr auto contains_if(const Container &container, const UnaryPredicate &predicate) -> bool
Checks if a container contains a value that satisfies a predicate.
Definition algorithm.hpp:113
auto is_ready_after(const std::future< T > &future, const std::chrono::duration< Rep, Period > &timeout) noexcept -> bool
Check whether a future is ready after a timeout.
Definition algorithm.hpp:175