Skip to content

Commit

Permalink
to_vec: using std::begin/end
Browse files Browse the repository at this point in the history
  • Loading branch information
felixguendling committed Sep 18, 2024
1 parent c211f64 commit f08f454
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions include/utl/to_vec.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@ namespace utl {

template <typename Output, typename Container, typename UnaryOperation>
inline void transform_to(Container&& c, Output& out, UnaryOperation&& op) {
out.reserve(
static_cast<std::size_t>(std::distance(std::begin(c), std::end(c))));
std::transform(std::begin(c), std::end(c), std::back_inserter(out),
using std::begin;
using std::end;
out.reserve(static_cast<std::size_t>(std::distance(begin(c), end(c))));
std::transform(begin(c), end(c), std::back_inserter(out),
std::forward<UnaryOperation>(op));
}

template <typename Output, typename Container, typename UnaryOperation>
inline auto transform_to(Container&& c, UnaryOperation&& op) -> Output {
using std::begin;
using std::end;
Output v;
v.reserve(
static_cast<std::size_t>(std::distance(std::begin(c), std::end(c))));
std::transform(std::begin(c), std::end(c), std::back_inserter(v),
v.reserve(static_cast<std::size_t>(std::distance(begin(c), end(c))));
std::transform(begin(c), end(c), std::back_inserter(v),
std::forward<UnaryOperation>(op));
return v;
}
Expand All @@ -36,21 +38,23 @@ inline auto to_vec(It s, It e, UnaryOperation&& op)
template <typename Container, typename UnaryOperation>
inline auto to_vec(Container&& c, UnaryOperation&& op)
-> std::vector<std::decay_t<decltype(op(*std::begin(c)))>> {
using std::begin;
using std::end;
auto v = std::vector<std::decay_t<decltype(op(*std::begin(c)))>>{};
v.reserve(
static_cast<std::size_t>(std::distance(std::begin(c), std::end(c))));
std::transform(std::begin(c), std::end(c), std::back_inserter(v),
v.reserve(static_cast<std::size_t>(std::distance(begin(c), end(c))));
std::transform(begin(c), end(c), std::back_inserter(v),
std::forward<UnaryOperation>(op));
return v;
}

template <typename Container>
inline auto to_vec(Container&& c)
-> std::vector<std::decay_t<decltype(*std::begin(c))>> {
auto v = std::vector<std::decay_t<decltype(*std::begin(c))>>{};
v.reserve(
static_cast<std::size_t>(std::distance(std::begin(c), std::end(c))));
std::copy(std::begin(c), std::end(c), std::back_inserter(v));
using std::begin;
using std::end;
auto v = std::vector<std::decay_t<decltype(*begin(c))>>{};
v.reserve(static_cast<std::size_t>(std::distance(begin(c), end(c))));
std::copy(begin(c), end(c), std::back_inserter(v));
return v;
}

Expand Down

0 comments on commit f08f454

Please sign in to comment.