static vector

v1
Brett 2023-12-10 14:54:08 -05:00
parent e6d096a486
commit 319f7a3771
2 changed files with 116 additions and 48 deletions

View File

@ -309,6 +309,8 @@ namespace blt::string
}; };
struct tree_node { struct tree_node {
std::string data;
std::string title;
std::vector<tree_node*> children; std::vector<tree_node*> children;
}; };

View File

@ -172,43 +172,43 @@ namespace blt
class scoped_buffer class scoped_buffer
{ {
private: private:
T* _buffer = nullptr; T* buffer_ = nullptr;
size_t _size; size_t size_;
public: public:
scoped_buffer(): _buffer(nullptr), _size(0) scoped_buffer(): buffer_(nullptr), size_(0)
{} {}
explicit scoped_buffer(size_t size): _size(size) explicit scoped_buffer(size_t size): size_(size)
{ {
if (size > 0) if (size > 0)
_buffer = new T[size]; buffer_ = new T[size];
else else
_buffer = nullptr; buffer_ = nullptr;
} }
scoped_buffer(const scoped_buffer& copy) scoped_buffer(const scoped_buffer& copy)
{ {
if (copy.size() == 0) if (copy.size() == 0)
{ {
_buffer = nullptr; buffer_ = nullptr;
_size = 0; size_ = 0;
return; return;
} }
_buffer = new T[copy.size()]; buffer_ = new T[copy.size()];
_size = copy._size; size_ = copy.size_;
if constexpr (std::is_trivially_copyable_v<T>) if constexpr (std::is_trivially_copyable_v<T>)
{ {
std::memcpy(_buffer, copy._buffer, copy.size() * sizeof(T)); std::memcpy(buffer_, copy.buffer_, copy.size() * sizeof(T));
} else } else
{ {
if constexpr (std::is_copy_constructible_v<T> && !std::is_copy_assignable_v<T>) if constexpr (std::is_copy_constructible_v<T> && !std::is_copy_assignable_v<T>)
{ {
for (size_t i = 0; i < this->_size; i++) for (size_t i = 0; i < this->size_; i++)
_buffer[i] = T(copy[i]); buffer_[i] = T(copy[i]);
} else } else
for (size_t i = 0; i < this->_size; i++) for (size_t i = 0; i < this->size_; i++)
_buffer[i] = copy[i]; buffer_[i] = copy[i];
} }
} }
@ -219,102 +219,168 @@ namespace blt
if (copy.size() == 0) if (copy.size() == 0)
{ {
_buffer = nullptr; buffer_ = nullptr;
_size = 0; size_ = 0;
return *this; return *this;
} }
delete[] this->_buffer; delete[] this->buffer_;
_buffer = new T[copy.size()]; buffer_ = new T[copy.size()];
_size = copy._size; size_ = copy.size_;
if constexpr (std::is_trivially_copyable_v<T>) if constexpr (std::is_trivially_copyable_v<T>)
{ {
std::memcpy(_buffer, copy._buffer, copy.size() * sizeof(T)); std::memcpy(buffer_, copy.buffer_, copy.size() * sizeof(T));
} else } else
{ {
if constexpr (std::is_copy_constructible_v<T> && !std::is_copy_assignable_v<T>) if constexpr (std::is_copy_constructible_v<T> && !std::is_copy_assignable_v<T>)
{ {
for (size_t i = 0; i < this->_size; i++) for (size_t i = 0; i < this->size_; i++)
_buffer[i] = T(copy[i]); buffer_[i] = T(copy[i]);
} else } else
for (size_t i = 0; i < this->_size; i++) for (size_t i = 0; i < this->size_; i++)
_buffer[i] = copy[i]; buffer_[i] = copy[i];
} }
return *this; return *this;
} }
scoped_buffer(scoped_buffer&& move) noexcept scoped_buffer(scoped_buffer&& move) noexcept
{ {
delete[] _buffer; delete[] buffer_;
_buffer = move._buffer; buffer_ = move.buffer_;
_size = move.size(); size_ = move.size();
move._buffer = nullptr; move.buffer_ = nullptr;
} }
scoped_buffer& operator=(scoped_buffer&& moveAssignment) noexcept scoped_buffer& operator=(scoped_buffer&& moveAssignment) noexcept
{ {
delete[] _buffer; delete[] buffer_;
_buffer = moveAssignment._buffer; buffer_ = moveAssignment.buffer_;
_size = moveAssignment.size(); size_ = moveAssignment.size();
moveAssignment._buffer = nullptr; moveAssignment.buffer_ = nullptr;
return *this; return *this;
} }
inline T& operator[](unsigned long index) inline T& operator[](size_t index)
{ {
return _buffer[index]; return buffer_[index];
} }
inline const T& operator[](unsigned long index) const inline const T& operator[](size_t index) const
{ {
return _buffer[index]; return buffer_[index];
} }
inline T* operator*() inline T* operator*()
{ {
return _buffer; return buffer_;
} }
[[nodiscard]] inline size_t size() const [[nodiscard]] inline size_t size() const
{ {
return _size; return size_;
} }
inline T*& ptr() inline T*& ptr()
{ {
return _buffer; return buffer_;
} }
inline const T* const& ptr() const inline const T* const& ptr() const
{ {
return _buffer; return buffer_;
} }
inline const T* const& data() const inline const T* const& data() const
{ {
return _buffer; return buffer_;
} }
inline T*& data() inline T*& data()
{ {
return _buffer; return buffer_;
} }
ptr_iterator<T> begin() inline ptr_iterator<T> begin()
{ {
return ptr_iterator{_buffer}; return ptr_iterator{buffer_};
} }
ptr_iterator<T> end() inline ptr_iterator<T> end()
{ {
return ptr_iterator{&_buffer[_size]}; return ptr_iterator{&buffer_[size_]};
} }
~scoped_buffer() ~scoped_buffer()
{ {
delete[] _buffer; delete[] buffer_;
}
};
template<typename T, size_t MAX_SIZE>
class static_vector
{
private:
T buffer_[MAX_SIZE];
size_t size_ = 0;
public:
static_vector() = default;
inline bool push_back(T copy)
{
if (size_ >= MAX_SIZE)
return false;
buffer_[size_++] = std::move(copy);
return true;
}
inline bool push_back(T&& move)
{
if (size_ >= MAX_SIZE)
return false;
buffer_[size_++] = std::forward(move);
return true;
}
inline T& operator[](size_t index)
{
return buffer_[index];
}
inline const T& operator[](size_t index) const
{
return buffer_[index];
}
[[nodiscard]] inline size_t size() const
{
return size_;
}
inline T* data()
{
return buffer_;
}
inline T* operator*()
{
return buffer_;
}
inline T* data() const
{
return buffer_;
}
inline ptr_iterator<T> begin()
{
return ptr_iterator<T>{buffer_};
}
inline ptr_iterator<T> end()
{
return ptr_iterator<T>{&buffer_[size_]};
} }
}; };