From 2d6cc0522cbd45f20469a2d9e40a02c480db1cc8 Mon Sep 17 00:00:00 2001 From: PhilipDeegan Date: Sat, 5 Oct 2024 20:41:14 +0200 Subject: [PATCH] ++ --- inc/mkn/kul/vector.hpp | 4 ++++ test/test/no_construct_alloc.cpp | 20 +++++++++++++++----- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/inc/mkn/kul/vector.hpp b/inc/mkn/kul/vector.hpp index 2712ed7..824927e 100644 --- a/inc/mkn/kul/vector.hpp +++ b/inc/mkn/kul/vector.hpp @@ -44,6 +44,10 @@ template std::vector>& as_super(std::vector>& v) { return *reinterpret_cast>*>(&v); } +template +std::vector> const& as_super(std::vector> const& v) { + return *reinterpret_cast> const*>(&v); +} } // namespace mkn::kul diff --git a/test/test/no_construct_alloc.cpp b/test/test/no_construct_alloc.cpp index b9f9b36..8c57bfe 100644 --- a/test/test/no_construct_alloc.cpp +++ b/test/test/no_construct_alloc.cpp @@ -8,6 +8,7 @@ #include #include +#include #include template @@ -17,9 +18,7 @@ struct S { } bool operator==(S const& that) const { - for (std::size_t i = 0; i < size; i++) - if (vars[i] != that.vars[i]) return false; - return true; + return std::equal(vars.begin(), vars.end(), that.vars.begin()); } bool operator!=(S const& that) const { return !(*this == that); } @@ -43,6 +42,15 @@ auto copy_operator_equal(V const& v) { return out; } +template +auto copy_operator_equal_super(V const& v) { + KUL_DBG_FUNC_ENTER; + V out; + out.reserve(v.capacity()); + mkn::kul::as_super(out) = mkn::kul::as_super(v); + return out; +} + template auto copy_manual(V const& v) { KUL_DBG_FUNC_ENTER; @@ -56,7 +64,7 @@ auto copy_manual(V const& v) { template auto make_vector(std::size_t const& size) { KUL_DBG_FUNC_ENTER; - return V{size}; + return V(size); } template auto make_vector_from(V1 const& v1) { @@ -68,7 +76,7 @@ auto make_vector_from(V1 const& v1) { template void do_compare() { - constexpr static std::size_t N = 2e6; + constexpr static std::size_t N = 2000000; auto const std_vec = make_vector>(N); auto const std_vec2 = make_vector_from>(std_vec); auto const no_construct_vec = make_vector_from>(std_vec); @@ -78,11 +86,13 @@ void do_compare() { auto const v1 = copy_construct(no_construct_vec); auto const v2 = copy_manual(std_vec); auto const v3 = copy_manual(no_construct_vec); + auto const v4 = copy_operator_equal_super(no_construct_vec); if (v0 != std_vec) throw std::runtime_error("FAIL 0"); if (v0 == v1) throw std::runtime_error("FAIL 1"); // :( if (v0 != v2) throw std::runtime_error("FAIL 2"); if (v0 != v3) throw std::runtime_error("FAIL 3"); + if (v0 != v4) throw std::runtime_error("FAIL 4"); } TEST(NoConstructAllocator, copies) { do_compare>(); }