diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2023-02-01 00:16:04 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2023-02-01 00:16:07 +0000 |
commit | 762546c827046358b06cffbd82eb91d723b63047 (patch) | |
tree | a68180e4000b886edf3cbb154d9f8b01b478b87e /lib | |
parent | Add missing braces (diff) | |
download | ilt-762546c827046358b06cffbd82eb91d723b63047.tar.bz2 ilt-762546c827046358b06cffbd82eb91d723b63047.tar.xz ilt-762546c827046358b06cffbd82eb91d723b63047.zip |
Add utilities for mutating, concatenating, creating vectors
Diffstat (limited to 'lib')
-rw-r--r-- | lib/collections.hpp | 43 |
1 files changed, 43 insertions, 0 deletions
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 <algorithm> #include <array> #include <span> +#include <utility> +#include <vector> template<typename T, typename E> concept SequentialCollection = requires(T c) { @@ -59,3 +62,43 @@ operator*=(std::span<T> & in, auto && f) } return in; } + +template<template<typename...> typename C, typename... T> +constexpr auto +operator*(const C<T...> & in, auto && f) +{ + C<decltype(f(in[0]))> out; + + std::transform(in.begin(), in.end(), std::inserter(out, out.end()), f); + return out; +} + +template<typename... T> +constexpr auto & +operator+=(std::vector<T...> & in, std::vector<T...> && src) +{ + in.reserve(in.size() + src.size()); + std::move(src.begin(), src.end(), std::back_inserter(in)); + return in; +} + +template<typename... T> +constexpr auto +operator+(std::vector<T...> && in, std::vector<T...> && src) +{ + in.reserve(in.size() + src.size()); + std::move(src.begin(), src.end(), std::back_inserter(in)); + return in; +} + +template<template<typename> typename Direction = std::plus> +static auto +vectorOfN(std::integral auto N, unsigned int start = {}, unsigned int step = 1) +{ + std::vector<unsigned int> v; + v.resize(N); + std::generate_n(v.begin(), N, [&start, step, adj = Direction {}]() { + return std::exchange(start, adj(start, step)); + }); + return v; +} |