Created
October 25, 2025 21:42
-
-
Save dgodfrey206/8e549d8a0e9f99e80b25dc6f6c7b2bce to your computer and use it in GitHub Desktop.
SmallVector is a static length-optimized dynamic buffer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <cmath> | |
| #include <cstdio> | |
| #include <vector> | |
| #include <iostream> | |
| #include <algorithm> | |
| #include <new> | |
| using namespace std; | |
| template <typename T, size_t SmallSizeThreshold> | |
| class SmallVector { | |
| public: | |
| SmallVector() = default; | |
| template <typename... Args> | |
| T& emplace_back(Args&&... args) { | |
| if (size() < SmallSizeThreshold) | |
| return *new (&stack[length++]) T(std::forward<Args>(args)...); | |
| if (!dyArray) { | |
| capacity = SmallSizeThreshold; | |
| dyArray = new std::aligned_storage_t<sizeof(T), alignof(T)>[capacity]; | |
| for (size_t i = 0; i < length; i++) | |
| *std::launder(reinterpret_cast<T*>(&dyArray[i])) = std::move(*std::launder(reinterpret_cast<T*>(&stack[i]))); | |
| } | |
| if (size() >= capacity) { | |
| capacity *= 2; | |
| auto temp = new std::aligned_storage_t<sizeof(T), alignof(T)>[capacity]; | |
| for (size_t i = 0; i < length; i++) | |
| *std::launder(reinterpret_cast<T*>(&temp[i])) = std::move(*std::launder(reinterpret_cast<T*>(&dyArray[i]))); | |
| delete[] dyArray; | |
| dyArray = temp; | |
| } | |
| return *new (&dyArray[length++]) T(std::forward<Args>(args)...); | |
| } | |
| void pop_back() { | |
| if (dyArray) | |
| std::destroy_at(std::launder(reinterpret_cast<T*>(&dyArray[--length]))); | |
| else | |
| std::destroy_at(std::launder(reinterpret_cast<T*>(&stack[--length]))); | |
| } | |
| T& operator[](size_t idx) { | |
| if (dyArray) | |
| return *std::launder(reinterpret_cast<T*>(&dyArray[idx])); | |
| return *std::launder(reinterpret_cast<T*>(&stack[idx])); | |
| } | |
| size_t size() const { | |
| return length; | |
| } | |
| private: | |
| std::aligned_storage_t<sizeof(T), alignof(T)> stack[SmallSizeThreshold]; | |
| std::aligned_storage_t<sizeof(T), alignof(T)>* dyArray = nullptr; | |
| size_t length = 0; | |
| size_t capacity = SmallSizeThreshold; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment