From f3451b57ab249fdcf5ccedd64cbd56b2a64e2de2 Mon Sep 17 00:00:00 2001 From: Brett Date: Sun, 30 Jun 2024 03:20:47 -0400 Subject: [PATCH] vector --- CMakeLists.txt | 2 +- include/blt/std/assert.h | 2 +- include/blt/std/vector.h | 145 ++++++++++++++++++++++++++++++++++----- 3 files changed, 130 insertions(+), 19 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index db2dad4..fa9d1ef 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.23) +set(BLT_VERSION 0.17.24) set(BLT_TEST_VERSION 0.0.1) set(BLT_TARGET BLT) diff --git a/include/blt/std/assert.h b/include/blt/std/assert.h index a55285e..1b51c5a 100644 --- a/include/blt/std/assert.h +++ b/include/blt/std/assert.h @@ -56,6 +56,6 @@ namespace blt #define BLT_THROW(throwable) do {blt::b_throw(throwable.what(), __FILE__, __LINE__); throw throwable;} while(0) -#define BLT_ABORT(message) do {blt::b_abort(message, __FILE__, __LINE__); } while (0) +#define BLT_ABORT(message) do {blt::b_abort(message, __FILE__, __LINE__); std::exit(1); } while (0) #endif //BLT_ASSERT_H diff --git a/include/blt/std/vector.h b/include/blt/std/vector.h index 9120c04..7987116 100644 --- a/include/blt/std/vector.h +++ b/include/blt/std/vector.h @@ -25,6 +25,7 @@ #include #include "ranges.h" #include +#include namespace blt { @@ -33,11 +34,16 @@ namespace blt class static_vector { private: - T buffer_[MAX_SIZE]; + std::array buffer_; size_t size_ = 0; - using iterator = T*; - using const_iterator = const T*; + using value_type = T; + using reference = T&; + using pointer = T*; + using const_reference = const T&; + using const_pointer = const T*; + using iterator = blt::ptr_iterator; + using const_iterator = blt::ptr_iterator; using reverse_iterator = std::reverse_iterator; using const_reverse_iterator = std::reverse_iterator; public: @@ -59,21 +65,19 @@ namespace blt return true; } - constexpr inline T& at(size_t index) + //TODO: replace with placement new / byte data + template + constexpr inline bool emplace_back(Args&& ... args) { - if (index >= MAX_SIZE) - throw std::runtime_error("Array index " + std::to_string(index) + " out of bounds! (Max size: " + std::to_string(MAX_SIZE) + ')'); - return buffer_[index]; + if (size_ >= MAX_SIZE) + return false; + buffer_[size_++] = T{std::forward(args)...}; + return true; } - constexpr inline T& operator[](size_t index) + constexpr inline void pop_back() { - return buffer_[index]; - } - - constexpr inline const T& operator[](size_t index) const - { - return buffer_[index]; + size_--; } constexpr inline void reserve(size_t size) @@ -83,6 +87,18 @@ namespace blt size_ = size; } + [[nodiscard]] constexpr inline bool empty() const + { + return size() == 0; + } + + constexpr inline void clear() + { + for (auto& v : *this) + v = {}; + size_ = 0; + } + [[nodiscard]] constexpr inline size_t size() const { return size_; @@ -93,21 +109,68 @@ namespace blt return MAX_SIZE; } - constexpr inline T* data() + [[nodiscard]] constexpr inline blt::size_t max_size() const + { + return MAX_SIZE; + } + + constexpr inline reference at(size_t index) + { + if (index >= MAX_SIZE) + throw std::runtime_error("Array index " + std::to_string(index) + " out of bounds! (Max size: " + std::to_string(MAX_SIZE) + ')'); + return buffer_[index]; + } + + constexpr inline reference operator[](size_t index) + { + return buffer_[index]; + } + + constexpr inline const_reference operator[](size_t index) const + { + return buffer_[index]; + } + + constexpr inline pointer operator*() { return buffer_; } - constexpr inline T* operator*() + constexpr inline const_pointer operator*() const { return buffer_; } - constexpr inline const T* data() const + constexpr inline pointer data() { return buffer_; } + constexpr inline const_pointer data() const + { + return buffer_; + } + + constexpr inline reference front() + { + return data()[0]; + } + + constexpr inline const_reference front() const + { + return data()[0]; + } + + constexpr inline reference back() + { + return data()[size() - 1]; + } + + constexpr inline const_reference back() const + { + return data()[size() - 1]; + } + constexpr inline iterator begin() noexcept { return data(); @@ -147,6 +210,54 @@ namespace blt { return reverse_iterator{cbegin()}; } + + template, bool> = true> + constexpr iterator insert(const_iterator pos, G&& ref) + { + blt::ptrdiff_t loc = pos - buffer_; + if (size_ + 1 >= capacity()) + { + BLT_ABORT("Inserting exceeds size of internal buffer!"); + } + for (auto insert = end() - 1; (insert - buffer_) != loc - 1; insert--) + { + auto new_pos = insert + 1; + *new_pos = *insert; + } + buffer_[loc] = ref; + size_++; + return buffer_ + loc; + } + + + constexpr iterator erase(const_iterator pos) + { + blt::ptrdiff_t loc = pos - buffer_; + + for (auto fetch = begin() + loc + 1; fetch != end(); fetch++) + { + auto insert = fetch - 1; + *insert = *fetch; + } + + size_--; + return buffer_ + loc + 1; + } + + constexpr iterator erase(const_iterator first, const_iterator last) + { + blt::ptrdiff_t first_pos = first - buffer_; + blt::ptrdiff_t last_pos = last - buffer_; + blt::ptrdiff_t remove_amount = last_pos - first_pos; + + for (auto fetch = begin() + last_pos, insert = begin() + first_pos; fetch != end(); fetch++, insert++) + { + *insert = *fetch; + } + + size_ -= remove_amount; + return buffer_ + first_pos + 1; + } }; template>