From 762546c827046358b06cffbd82eb91d723b63047 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 1 Feb 2023 00:16:04 +0000 Subject: Add utilities for mutating, concatenating, creating vectors --- lib/collections.hpp | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'lib') diff --git a/lib/collections.hpp b/lib/collections.hpp index 5d39e79..47967b2 100644 --- a/lib/collections.hpp +++ b/lib/collections.hpp @@ -1,7 +1,10 @@ #pragma once +#include #include #include +#include +#include template concept SequentialCollection = requires(T c) { @@ -59,3 +62,43 @@ operator*=(std::span & in, auto && f) } return in; } + +template typename C, typename... T> +constexpr auto +operator*(const C & in, auto && f) +{ + C out; + + std::transform(in.begin(), in.end(), std::inserter(out, out.end()), f); + return out; +} + +template +constexpr auto & +operator+=(std::vector & in, std::vector && src) +{ + in.reserve(in.size() + src.size()); + std::move(src.begin(), src.end(), std::back_inserter(in)); + return in; +} + +template +constexpr auto +operator+(std::vector && in, std::vector && src) +{ + in.reserve(in.size() + src.size()); + std::move(src.begin(), src.end(), std::back_inserter(in)); + return in; +} + +template typename Direction = std::plus> +static auto +vectorOfN(std::integral auto N, unsigned int start = {}, unsigned int step = 1) +{ + std::vector v; + v.resize(N); + std::generate_n(v.begin(), N, [&start, step, adj = Direction {}]() { + return std::exchange(start, adj(start, step)); + }); + return v; +} -- cgit v1.2.3