From 38fc122b84fce3a0e6299069524e80a73818d928 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 10 Apr 2023 13:19:19 +0100 Subject: Constraint operator* collection helper to IterableCollections --- lib/collections.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib/collections.hpp') diff --git a/lib/collections.hpp b/lib/collections.hpp index 59cec6f..4df622e 100644 --- a/lib/collections.hpp +++ b/lib/collections.hpp @@ -65,10 +65,11 @@ operator*=(IterableCollection auto & in, auto && f) } template typename C, typename... T> + requires IterableCollection> [[nodiscard]] constexpr auto operator*(const C & in, auto && f) { - C out; + C out; std::transform(in.begin(), in.end(), std::inserter(out, out.end()), f); return out; -- cgit v1.2.3 From a5a0ad3634c512afb1686a2ec8940843e7f0eb63 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 10 Apr 2023 13:20:49 +0100 Subject: operator* collection helper reserves target space when possible --- lib/collections.hpp | 3 +++ 1 file changed, 3 insertions(+) (limited to 'lib/collections.hpp') diff --git a/lib/collections.hpp b/lib/collections.hpp index 4df622e..b51d18e 100644 --- a/lib/collections.hpp +++ b/lib/collections.hpp @@ -70,6 +70,9 @@ template typename C, typename... T> operator*(const C & in, auto && f) { C out; + if constexpr (requires { out.reserve(in.size()); }) { + out.reserve(in.size()); + } std::transform(in.begin(), in.end(), std::inserter(out, out.end()), f); return out; -- cgit v1.2.3 From e7d946aadebfdf49cc96e6a518c1af1ef7f5c71c Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 10 Apr 2023 14:34:56 +0100 Subject: Add operator* helper specialised for std::span --- lib/collections.hpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'lib/collections.hpp') diff --git a/lib/collections.hpp b/lib/collections.hpp index b51d18e..8f5a43d 100644 --- a/lib/collections.hpp +++ b/lib/collections.hpp @@ -78,6 +78,27 @@ operator*(const C & in, auto && f) return out; } +template +[[nodiscard]] constexpr auto +operator*(const std::span in, auto && f) +{ + std::array())), N> out; + + std::transform(in.begin(), in.end(), out.begin(), f); + return out; +} + +template +[[nodiscard]] constexpr auto +operator*(const std::span in, auto && f) +{ + std::vector()))> out; + + out.reserve(in.size()); + std::transform(in.begin(), in.end(), std::inserter(out, out.end()), f); + return out; +} + template constexpr auto & operator+=(std::vector & in, std::vector && src) -- cgit v1.2.3