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 ++++++++++++++++++++++++++++++++++++++++++ test/Jamfile.jam | 1 + test/test-pack.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 lib/pack.h create mode 100644 test/test-pack.cpp 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(); + } +}; diff --git a/test/Jamfile.jam b/test/Jamfile.jam index fb9a996..3b4e891 100644 --- a/test/Jamfile.jam +++ b/test/Jamfile.jam @@ -59,6 +59,7 @@ run perf-persistence.cpp : -- : test-persistence : benchmark t run test-worker.cpp ; run test-instancing.cpp : : : test ; run test-glContainer.cpp : : : test ; +run test-pack.cpp : : : test ; compile test-static-enumDetails.cpp ; compile test-static-stream_support.cpp ; explicit perf-assetFactory ; diff --git a/test/test-pack.cpp b/test/test-pack.cpp new file mode 100644 index 0000000..1f9f061 --- /dev/null +++ b/test/test-pack.cpp @@ -0,0 +1,41 @@ +#define BOOST_TEST_MODULE pack + +#include +#include + +#include "pack.h" +#include + +using IntegerVectorPack = pack; + +BOOST_FIXTURE_TEST_SUITE(pint, IntegerVectorPack) + +BOOST_AUTO_TEST_CASE(basics) +{ + BOOST_CHECK_EQUAL(size(), 0); + BOOST_CHECK_NO_THROW(emplace(1)); + BOOST_CHECK_NO_THROW(emplace(2)); + BOOST_CHECK_NO_THROW(emplace(3)); + BOOST_CHECK_NO_THROW(emplace(4)); + BOOST_CHECK_EQUAL(size(), 4); + { + std::array expected1 {1, 2, 3, 4}; + BOOST_CHECK_EQUAL_COLLECTIONS(begin(), end(), expected1.begin(), expected1.end()); + } + + BOOST_CHECK_NO_THROW(erase(begin() + 1)); + BOOST_CHECK_EQUAL(size(), 3); + { + std::array expected1 {1, 4, 3}; + BOOST_CHECK_EQUAL_COLLECTIONS(begin(), end(), expected1.begin(), expected1.end()); + } + + BOOST_CHECK_NO_THROW(erase(--end())); + BOOST_CHECK_EQUAL(size(), 2); + { + std::array expected1 {1, 4}; + BOOST_CHECK_EQUAL_COLLECTIONS(begin(), end(), expected1.begin(), expected1.end()); + } +} + +BOOST_AUTO_TEST_SUITE_END() -- cgit v1.2.3