From ba761a571ab9d62fa21e78d1b53f885f82b48446 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 24 Feb 2023 19:26:57 +0000 Subject: Fixed up vector operator+ element --- lib/collections.hpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'lib/collections.hpp') diff --git a/lib/collections.hpp b/lib/collections.hpp index 47967b2..31e5ab8 100644 --- a/lib/collections.hpp +++ b/lib/collections.hpp @@ -82,13 +82,13 @@ operator+=(std::vector & in, std::vector && src) return in; } -template +template constexpr auto -operator+(std::vector && in, std::vector && src) +operator+(const std::vector & in, Vn && vn) { - in.reserve(in.size() + src.size()); - std::move(src.begin(), src.end(), std::back_inserter(in)); - return in; + auto out(in); + out.emplace_back(std::forward(vn)); + return out; } template typename Direction = std::plus> -- cgit v1.2.3 From f25a47f022e45137149a0726a73cbd8b1fda4f04 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 25 Feb 2023 03:23:07 +0000 Subject: Helpers to create container instances from ranges --- lib/collections.hpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'lib/collections.hpp') diff --git a/lib/collections.hpp b/lib/collections.hpp index 31e5ab8..18e6147 100644 --- a/lib/collections.hpp +++ b/lib/collections.hpp @@ -15,6 +15,8 @@ concept SequentialCollection = requires(T c) { c.data() } -> std::same_as; }; +template +concept IterableCollection = std::is_same_v().begin()), decltype(std::declval().end())>; template constexpr std::array @@ -102,3 +104,17 @@ vectorOfN(std::integral auto N, unsigned int start = {}, unsigned int step = 1) }); return v; } + +template typename Rtn = std::vector, IterableCollection In> +auto +materializeRange(In && in) +{ + return Rtn(in.begin(), in.end()); +} + +template typename Rtn = std::vector, typename In> +auto +materializeRange(const std::pair & in) +{ + return Rtn(in.first, in.second); +} -- cgit v1.2.3 From aac5672de67798337618d0bceb93b2c799d3edc3 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 27 Feb 2023 01:05:11 +0000 Subject: Add nodiscard to many collections helpers --- lib/collections.hpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'lib/collections.hpp') diff --git a/lib/collections.hpp b/lib/collections.hpp index 18e6147..391647e 100644 --- a/lib/collections.hpp +++ b/lib/collections.hpp @@ -19,7 +19,7 @@ template concept IterableCollection = std::is_same_v().begin()), decltype(std::declval().end())>; template -constexpr std::array +[[nodiscard]] constexpr std::array operator+(const std::array & a, const std::array & b) { std::array r; @@ -30,7 +30,7 @@ operator+(const std::array & a, const std::array & b) } template -constexpr std::array, first * second> +[[nodiscard]] constexpr std::array, first * second> operator*(const std::array & a, const std::array & b) { std::array, first * second> r; @@ -44,7 +44,7 @@ operator*(const std::array & a, const std::array & b) } template -constexpr auto +[[nodiscard]] constexpr auto operator*(const std::array & in, auto && f) { std::array out; @@ -66,7 +66,7 @@ operator*=(std::span & in, auto && f) } template typename C, typename... T> -constexpr auto +[[nodiscard]] constexpr auto operator*(const C & in, auto && f) { C out; @@ -85,7 +85,7 @@ operator+=(std::vector & in, std::vector && src) } template -constexpr auto +[[nodiscard]] constexpr auto operator+(const std::vector & in, Vn && vn) { auto out(in); @@ -94,7 +94,7 @@ operator+(const std::vector & in, Vn && vn) } template typename Direction = std::plus> -static auto +[[nodiscard]] static auto vectorOfN(std::integral auto N, unsigned int start = {}, unsigned int step = 1) { std::vector v; @@ -106,14 +106,14 @@ vectorOfN(std::integral auto N, unsigned int start = {}, unsigned int step = 1) } template typename Rtn = std::vector, IterableCollection In> -auto +[[nodiscard]] auto materializeRange(In && in) { return Rtn(in.begin(), in.end()); } template typename Rtn = std::vector, typename In> -auto +[[nodiscard]] auto materializeRange(const std::pair & in) { return Rtn(in.first, in.second); -- cgit v1.2.3 From 90b4839af82e9c0188f4d37c0587e613756abcf9 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 27 Feb 2023 19:37:05 +0000 Subject: operator*= can work on any iterable collection --- lib/collections.hpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'lib/collections.hpp') diff --git a/lib/collections.hpp b/lib/collections.hpp index 391647e..16870be 100644 --- a/lib/collections.hpp +++ b/lib/collections.hpp @@ -55,9 +55,8 @@ operator*(const std::array & in, auto && f) return out; } -template constexpr auto & -operator*=(std::span & in, auto && f) +operator*=(IterableCollection auto & in, auto && f) { for (auto & v : in) { f(v); -- cgit v1.2.3