From 70a232cfcdd2742d5a8bde3a2b52ce2059f12432 Mon Sep 17 00:00:00 2001 From: apio Date: Tue, 22 Aug 2023 11:53:42 +0200 Subject: [PATCH] libluna: Make Vector grow exponentially --- libluna/include/luna/Vector.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/libluna/include/luna/Vector.h b/libluna/include/luna/Vector.h index 076e4ac2..b3a8e991 100644 --- a/libluna/include/luna/Vector.h +++ b/libluna/include/luna/Vector.h @@ -62,7 +62,7 @@ template class Vector Result try_append(T&& item) { - if (m_capacity == m_size) TRY(resize(m_capacity + 8)); + if (m_capacity == m_size) TRY(resize(m_capacity ? m_capacity * 2 : 8)); new (&m_data[m_size]) T(move(item)); @@ -252,6 +252,8 @@ template class Vector Result resize(usize new_capacity) { + if (new_capacity < m_capacity) return {}; + const usize new_byte_capacity = new_capacity * sizeof(T); void* const ptr = TRY(realloc_impl(m_data, new_byte_capacity));