From a6457aea04d1705f5b03f9c9e628bebbefdcf64c Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 1 May 2023 18:14:49 +0100 Subject: Add the pack container Keeps its elements densely packed together without any interest in order --- lib/pack.h | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 lib/pack.h (limited to 'lib/pack.h') diff --git a/lib/pack.h b/lib/pack.h new file mode 100644 index 0000000..92c8b20 --- /dev/null +++ b/lib/pack.h @@ -0,0 +1,42 @@ +#pragma once + +#include + +template typename Container> class pack : protected Container { +public: + using Container::Container; + + using Container::begin; + using Container::end; + using Container::rbegin; + using Container::rend; + using Container::cbegin; + using Container::cend; + using Container::crbegin; + using Container::crend; + using Container::clear; + using Container::empty; + using Container::size; + using Container::capacity; + using Container::shrink_to_fit; + using Container::at; + using Container::data; + using Container::operator[]; + + template + decltype(auto) + emplace(Ps &&... ps) + { + return Container::emplace_back(std::forward(ps)...); + } + + void + erase(typename Container::iterator pos) + { + pos->~T(); + if (&*pos != &Container::back()) { + new (&*pos) T(std::move(Container::back())); + } + Container::pop_back(); + } +}; -- cgit v1.2.3 From ca05c4a3c1e536cdf300b49c49004e4e0e54dfea Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 1 May 2023 20:31:50 +0100 Subject: Don't explictly call destructor of contained element --- lib/pack.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'lib/pack.h') diff --git a/lib/pack.h b/lib/pack.h index 92c8b20..e63d7cc 100644 --- a/lib/pack.h +++ b/lib/pack.h @@ -33,9 +33,8 @@ public: void erase(typename Container::iterator pos) { - pos->~T(); if (&*pos != &Container::back()) { - new (&*pos) T(std::move(Container::back())); + *pos = std::move(Container::back()); } Container::pop_back(); } -- cgit v1.2.3