From 81a7b83dfed346579ed6709cb9334abf4561ddf1 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Thu, 13 Aug 2020 13:33:20 +1000 Subject: [PATCH] array/darray: add resize calls --- array/darray.hpp | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/array/darray.hpp b/array/darray.hpp index 00dec8a6..a32a0037 100644 --- a/array/darray.hpp +++ b/array/darray.hpp @@ -65,13 +65,13 @@ namespace cruft { } } - ValueT& operator[] (std::size_t idx)& noexcept + ValueT& operator[] (size_type idx)& noexcept { CHECK_LT (idx, m_size); return m_data.objects[idx]; } - ValueT const& operator[] (std::size_t idx) const& noexcept + ValueT const& operator[] (size_type idx) const& noexcept { CHECK_LT (idx, m_size); return m_data.objects[idx]; @@ -87,10 +87,40 @@ namespace cruft { decltype(auto) cbegin (void) const& { return begin (); } decltype(auto) cend (void) const& { return end (); } - std::size_t size (void) const noexcept { return m_size; } + constexpr value_type* data (void)& noexcept + { + return m_data.objects + 0; + } + + constexpr value_type const* data (void) const& noexcept + { + return m_data.objects + 0; + } + + size_type size (void) const noexcept { return m_size; } constexpr auto capacity (void) const noexcept { return CapacityV; } constexpr auto remain (void) const noexcept { return capacity () - size (); } + constexpr void + resize (size_type const count, value_type const &value) + { + if (count < m_size) { + while (m_size > count) { + --m_size; + m_data.objects[m_size].~ValueT (); + } + return; + } + + if (count > m_size) { + for ( ; m_size < count; ++m_size) + new (m_data.objects + m_size) ValueT (value); + return; + } + } + + constexpr void resize (size_type count) { return resize (count, value_type {}); } + constexpr bool empty (void) const noexcept { return m_size == 0; } constexpr bool full (void) const noexcept { return m_size == CapacityV; } @@ -154,7 +184,7 @@ namespace cruft { ValueT objects[CapacityV]; } m_data; - std::size_t m_size; + size_type m_size; };