array/darray: add resize calls

This commit is contained in:
Danny Robson 2020-08-13 13:33:20 +10:00
parent c6912618a4
commit 81a7b83dfe

View File

@ -65,13 +65,13 @@ namespace cruft {
} }
} }
ValueT& operator[] (std::size_t idx)& noexcept ValueT& operator[] (size_type idx)& noexcept
{ {
CHECK_LT (idx, m_size); CHECK_LT (idx, m_size);
return m_data.objects[idx]; 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); CHECK_LT (idx, m_size);
return m_data.objects[idx]; return m_data.objects[idx];
@ -87,10 +87,40 @@ namespace cruft {
decltype(auto) cbegin (void) const& { return begin (); } decltype(auto) cbegin (void) const& { return begin (); }
decltype(auto) cend (void) const& { return end (); } 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 capacity (void) const noexcept { return CapacityV; }
constexpr auto remain (void) const noexcept { return capacity () - size (); } 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 empty (void) const noexcept { return m_size == 0; }
constexpr bool full (void) const noexcept { return m_size == CapacityV; } constexpr bool full (void) const noexcept { return m_size == CapacityV; }
@ -154,7 +184,7 @@ namespace cruft {
ValueT objects[CapacityV]; ValueT objects[CapacityV];
} m_data; } m_data;
std::size_t m_size; size_type m_size;
}; };