diff options
Diffstat (limited to '')
-rw-r--r-- | src/base/itertools.hh | 96 |
1 files changed, 88 insertions, 8 deletions
diff --git a/src/base/itertools.hh b/src/base/itertools.hh index 058ceb8..70286fb 100644 --- a/src/base/itertools.hh +++ b/src/base/itertools.hh @@ -31,6 +31,8 @@ #define lnav_itertools_hh #include <algorithm> +#include <deque> +#include <map> #include <memory> #include <set> #include <type_traits> @@ -138,8 +140,16 @@ struct max_with_init { struct sum {}; +struct to_vector {}; + } // namespace details +inline details::to_vector +to_vector() +{ + return details::to_vector{}; +} + template<typename T> inline details::unwrap_or<T> unwrap_or(T value) @@ -619,12 +629,32 @@ operator|(nonstd::optional<T> in, template<typename T, typename F> auto -operator|(const T& in, const lnav::itertools::details::mapper<F>& mapper) - -> std::vector<std::remove_const_t<std::remove_reference_t< - decltype(mapper.m_func(std::declval<typename T::value_type>()))>>> +operator|(const std::set<T>& in, + const lnav::itertools::details::mapper<F>& mapper) + -> std::set<std::remove_const_t< + std::remove_reference_t<decltype(mapper.m_func(std::declval<T>()))>>> { - using return_type = std::vector<std::remove_const_t<std::remove_reference_t< - decltype(mapper.m_func(std::declval<typename T::value_type>()))>>>; + using return_type = std::set<std::remove_const_t< + std::remove_reference_t<decltype(mapper.m_func(std::declval<T>()))>>>; + return_type retval; + + std::transform(in.begin(), + in.end(), + std::inserter(retval, retval.begin()), + mapper.m_func); + + return retval; +} + +template<typename T, typename F> +auto +operator|(const std::vector<T>& in, + const lnav::itertools::details::mapper<F>& mapper) + -> std::vector<std::remove_const_t< + std::remove_reference_t<decltype(mapper.m_func(std::declval<T>()))>>> +{ + using return_type = std::vector<std::remove_const_t< + std::remove_reference_t<decltype(mapper.m_func(std::declval<T>()))>>>; return_type retval; retval.reserve(in.size()); @@ -636,12 +666,50 @@ operator|(const T& in, const lnav::itertools::details::mapper<F>& mapper) template<typename T, typename F> auto -operator|(const T& in, const lnav::itertools::details::mapper<F>& mapper) +operator|(const std::deque<T>& in, + const lnav::itertools::details::mapper<F>& mapper) + -> std::vector<std::remove_const_t< + std::remove_reference_t<decltype(mapper.m_func(std::declval<T>()))>>> +{ + using return_type = std::vector<std::remove_const_t< + std::remove_reference_t<decltype(mapper.m_func(std::declval<T>()))>>>; + return_type retval; + + retval.reserve(in.size()); + std::transform( + in.begin(), in.end(), std::back_inserter(retval), mapper.m_func); + + return retval; +} + +template<typename K, typename V, typename F> +auto +operator|(const std::map<K, V>& in, + const lnav::itertools::details::mapper<F>& mapper) -> std::vector< - std::remove_const_t<decltype(((*in.begin()).*mapper.m_func)())>> + std::remove_const_t<std::remove_reference_t<decltype(mapper.m_func( + std::declval<typename std::map<K, V>::value_type>()))>>> { using return_type = std::vector< - std::remove_const_t<decltype(((*in.begin()).*mapper.m_func)())>>; + std::remove_const_t<std::remove_reference_t<decltype(mapper.m_func( + std::declval<typename std::map<K, V>::value_type>()))>>>; + return_type retval; + + retval.reserve(in.size()); + std::transform( + in.begin(), in.end(), std::back_inserter(retval), mapper.m_func); + + return retval; +} + +template<typename T, typename F> +auto +operator|(const T& in, const lnav::itertools::details::mapper<F>& mapper) + -> std::vector<std::remove_const_t< + std::remove_reference_t<decltype(((*in.begin()).*mapper.m_func)())>>> +{ + using return_type = std::vector<std::remove_const_t< + std::remove_reference_t<decltype(((*in.begin()).*mapper.m_func)())>>>; return_type retval; retval.reserve(in.size()); @@ -782,4 +850,16 @@ operator|(nonstd::optional<T> in, return in.value_or(unwrapper.uo_value); } +template<typename T> +std::vector<T> +operator|(std::set<T>&& in, lnav::itertools::details::to_vector tv) +{ + std::vector<T> retval; + + retval.reserve(in.size()); + std::copy(in.begin(), in.end(), std::back_inserter(retval)); + + return retval; +} + #endif |