From 57ddcfca1e72245b8e7e59a2f6ccf2efda1b29b3 Mon Sep 17 00:00:00 2001 From: Brett Date: Sun, 30 Jun 2024 13:54:10 -0400 Subject: [PATCH] svo vector --- CMakeLists.txt | 2 +- include/blt/std/vector.h | 64 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 63 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 01ab606..98ba949 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.20) include(cmake/color.cmake) -set(BLT_VERSION 0.17.26) +set(BLT_VERSION 0.17.27) set(BLT_TEST_VERSION 0.0.1) set(BLT_TARGET BLT) diff --git a/include/blt/std/vector.h b/include/blt/std/vector.h index d2cc209..5ed4d12 100644 --- a/include/blt/std/vector.h +++ b/include/blt/std/vector.h @@ -539,12 +539,72 @@ namespace blt class svo_vector { public: + constexpr svo_vector() = default; + + template, bool> = true> + constexpr svo_vector(std::initializer_list list) + { + if (list.size() > BUFFER_SIZE) + { + // TODO: how to avoid copy here? + data.buffer_pointer = alloc.allocate(list.size()); + for (const auto& v : blt::enumerate(list)) + new (&data.buffer_pointer[v.first]) T(v.second); + capacity = list.size(); + used = list.size(); + } else + { + for (const auto& v : blt::enumerate(list)) + new (&data.buffer[v.first]) T(v.second); + used = list.size(); + } + } + + template, G> || std::is_same_v, G>, bool> = true> + constexpr explicit svo_vector(G&& universal) + { + if (universal.size() > BUFFER_SIZE) + { + + } else + { + + } + } + + [[nodiscard]] constexpr blt::size_t size() const + { + return used; + } + + [[nodiscard]] constexpr bool is_heap() const + { + return used > BUFFER_SIZE; + } + + BLT_CPP20_CONSTEXPR ~svo_vector() + { + if (is_heap()) + { + for (blt::size_t i = 0; i < used; i++) + data.buffer_pointer[i].~T(); + alloc.deallocate(data.buffer_pointer, capacity); + } else + { + for (blt::size_t i = 0; i < used; i++) + data.buffer[i].~T(); + } + } private: - union buffer_data { + union buffer_data + { T* buffer_pointer; T buffer[BUFFER_SIZE]; - }; + } data; + ALLOC alloc; + blt::size_t used = 0; + blt::size_t capacity = BUFFER_SIZE; }; }