2023-02-08 13:40:28 -05:00
|
|
|
/*
|
|
|
|
* Created by Brett on 08/02/23.
|
|
|
|
* Licensed under GNU General Public License V3.0
|
|
|
|
* See LICENSE file for license detail
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef BLT_TESTS_MEMORY_H
|
|
|
|
#define BLT_TESTS_MEMORY_H
|
|
|
|
|
2023-12-18 23:13:44 -05:00
|
|
|
#include <blt/std/memory_util.h>
|
2023-07-20 21:44:52 -04:00
|
|
|
#include <initializer_list>
|
|
|
|
#include <iterator>
|
2023-09-21 23:23:27 -04:00
|
|
|
#include <cstring>
|
|
|
|
#include "queue.h"
|
2023-12-18 19:22:29 -05:00
|
|
|
#include "utility.h"
|
2023-11-06 18:35:09 -05:00
|
|
|
#include <blt/std/assert.h>
|
2023-10-24 21:43:22 -04:00
|
|
|
#include <cstdint>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <utility>
|
|
|
|
#include <cstring>
|
2023-07-20 21:44:52 -04:00
|
|
|
|
2023-09-21 23:23:27 -04:00
|
|
|
namespace blt
|
|
|
|
{
|
2023-07-20 21:44:52 -04:00
|
|
|
|
|
|
|
template<typename V>
|
2023-09-21 23:23:27 -04:00
|
|
|
struct ptr_iterator
|
|
|
|
{
|
2023-07-20 21:44:52 -04:00
|
|
|
public:
|
|
|
|
using iterator_category = std::random_access_iterator_tag;
|
2023-09-21 23:23:27 -04:00
|
|
|
using difference_type = std::ptrdiff_t;
|
|
|
|
using value_type = V;
|
|
|
|
using pointer = value_type*;
|
|
|
|
using reference = value_type&;
|
2023-07-20 21:44:52 -04:00
|
|
|
|
2023-09-21 23:23:27 -04:00
|
|
|
explicit ptr_iterator(V* v): _v(v)
|
|
|
|
{}
|
2023-07-20 21:44:52 -04:00
|
|
|
|
2023-09-21 23:23:27 -04:00
|
|
|
reference operator*() const
|
|
|
|
{ return *_v; }
|
|
|
|
|
|
|
|
pointer operator->()
|
|
|
|
{ return _v; }
|
|
|
|
|
|
|
|
ptr_iterator& operator++()
|
|
|
|
{
|
2023-07-20 21:44:52 -04:00
|
|
|
_v++;
|
|
|
|
return *this;
|
|
|
|
}
|
2023-09-21 23:23:27 -04:00
|
|
|
|
|
|
|
ptr_iterator& operator--()
|
|
|
|
{
|
2023-07-20 21:44:52 -04:00
|
|
|
_v--;
|
|
|
|
return *this;
|
|
|
|
}
|
2023-09-21 23:23:27 -04:00
|
|
|
|
|
|
|
ptr_iterator operator++(int)
|
|
|
|
{
|
2023-07-20 21:44:52 -04:00
|
|
|
auto tmp = *this;
|
|
|
|
++(*this);
|
|
|
|
return tmp;
|
|
|
|
}
|
2023-09-21 23:23:27 -04:00
|
|
|
|
|
|
|
ptr_iterator operator--(int)
|
|
|
|
{
|
2023-07-20 21:44:52 -04:00
|
|
|
auto tmp = *this;
|
|
|
|
--(*this);
|
|
|
|
return tmp;
|
|
|
|
}
|
2023-09-21 23:23:27 -04:00
|
|
|
|
|
|
|
friend bool operator==(const ptr_iterator& a, const ptr_iterator& b)
|
|
|
|
{
|
2023-07-20 21:44:52 -04:00
|
|
|
return a._v == b._v;
|
|
|
|
}
|
2023-09-21 23:23:27 -04:00
|
|
|
|
|
|
|
friend bool operator!=(const ptr_iterator& a, const ptr_iterator& b)
|
|
|
|
{
|
2023-07-20 21:44:52 -04:00
|
|
|
return a._v != b._v;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
V* _v;
|
|
|
|
};
|
2023-09-21 23:23:27 -04:00
|
|
|
|
2023-02-08 13:40:28 -05:00
|
|
|
/**
|
|
|
|
* Creates an encapsulation of a T array which will be automatically deleted when this object goes out of scope.
|
2023-10-27 14:26:31 -04:00
|
|
|
* This is a simple buffer meant to be used only inside of a function and not copied around.
|
2023-02-08 13:40:28 -05:00
|
|
|
* The internal buffer is allocated on the heap.
|
2023-07-24 02:55:03 -04:00
|
|
|
* The operator * has been overloaded to return the internal buffer.
|
2023-02-08 13:40:28 -05:00
|
|
|
* @tparam T type that is stored in buffer eg char
|
|
|
|
*/
|
2023-11-05 19:05:32 -05:00
|
|
|
template<typename T, bool = std::is_copy_constructible_v<T> || std::is_copy_assignable_v<T>>
|
|
|
|
class scoped_buffer
|
2023-09-21 23:23:27 -04:00
|
|
|
{
|
2024-02-13 17:06:33 -05:00
|
|
|
public:
|
|
|
|
using element_type = T;
|
|
|
|
using value_type = std::remove_cv_t<T>;
|
|
|
|
using pointer = T*;
|
|
|
|
using const_pointer = const T*;
|
|
|
|
using reference = T&;
|
|
|
|
using const_reference = const T&;
|
2024-02-13 17:26:05 -05:00
|
|
|
using iterator = ptr_iterator<T>;
|
|
|
|
using const_iterator = ptr_iterator<const T>;
|
2024-02-13 17:06:33 -05:00
|
|
|
using reverse_iterator = std::reverse_iterator<iterator>;
|
|
|
|
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
|
2023-07-24 02:39:03 -04:00
|
|
|
private:
|
2023-12-10 14:54:08 -05:00
|
|
|
T* buffer_ = nullptr;
|
|
|
|
size_t size_;
|
2023-07-24 02:39:03 -04:00
|
|
|
public:
|
2024-02-13 17:26:05 -05:00
|
|
|
constexpr scoped_buffer(): buffer_(nullptr), size_(0)
|
2023-10-27 14:26:31 -04:00
|
|
|
{}
|
|
|
|
|
2024-02-13 17:26:05 -05:00
|
|
|
constexpr explicit scoped_buffer(size_t size): size_(size)
|
2023-09-21 23:23:27 -04:00
|
|
|
{
|
2023-11-06 18:35:09 -05:00
|
|
|
if (size > 0)
|
2023-12-10 14:54:08 -05:00
|
|
|
buffer_ = new T[size];
|
2023-11-06 18:35:09 -05:00
|
|
|
else
|
2023-12-10 14:54:08 -05:00
|
|
|
buffer_ = nullptr;
|
2023-07-24 02:39:03 -04:00
|
|
|
}
|
|
|
|
|
2024-02-13 17:26:05 -05:00
|
|
|
constexpr scoped_buffer(const scoped_buffer& copy)
|
2023-11-05 19:05:32 -05:00
|
|
|
{
|
2023-11-09 19:07:24 -05:00
|
|
|
if (copy.size() == 0)
|
|
|
|
{
|
2023-12-10 14:54:08 -05:00
|
|
|
buffer_ = nullptr;
|
|
|
|
size_ = 0;
|
2023-11-08 20:25:47 -05:00
|
|
|
return;
|
|
|
|
}
|
2023-12-10 14:54:08 -05:00
|
|
|
buffer_ = new T[copy.size()];
|
|
|
|
size_ = copy.size_;
|
2023-11-05 19:05:32 -05:00
|
|
|
|
|
|
|
if constexpr (std::is_trivially_copyable_v<T>)
|
|
|
|
{
|
2023-12-10 14:54:08 -05:00
|
|
|
std::memcpy(buffer_, copy.buffer_, copy.size() * sizeof(T));
|
2023-11-05 19:05:32 -05:00
|
|
|
} else
|
|
|
|
{
|
|
|
|
if constexpr (std::is_copy_constructible_v<T> && !std::is_copy_assignable_v<T>)
|
|
|
|
{
|
2023-12-10 14:54:08 -05:00
|
|
|
for (size_t i = 0; i < this->size_; i++)
|
|
|
|
buffer_[i] = T(copy[i]);
|
2023-11-05 19:05:32 -05:00
|
|
|
} else
|
2023-12-10 14:54:08 -05:00
|
|
|
for (size_t i = 0; i < this->size_; i++)
|
|
|
|
buffer_[i] = copy[i];
|
2023-11-05 19:05:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-13 17:26:05 -05:00
|
|
|
constexpr scoped_buffer& operator=(const scoped_buffer& copy)
|
2023-11-05 19:05:32 -05:00
|
|
|
{
|
|
|
|
if (© == this)
|
|
|
|
return *this;
|
|
|
|
|
2023-11-09 19:07:24 -05:00
|
|
|
if (copy.size() == 0)
|
|
|
|
{
|
2023-12-10 14:54:08 -05:00
|
|
|
buffer_ = nullptr;
|
|
|
|
size_ = 0;
|
2023-11-08 20:25:47 -05:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2023-12-10 14:54:08 -05:00
|
|
|
delete[] this->buffer_;
|
|
|
|
buffer_ = new T[copy.size()];
|
|
|
|
size_ = copy.size_;
|
2023-11-05 19:05:32 -05:00
|
|
|
|
|
|
|
if constexpr (std::is_trivially_copyable_v<T>)
|
|
|
|
{
|
2023-12-10 14:54:08 -05:00
|
|
|
std::memcpy(buffer_, copy.buffer_, copy.size() * sizeof(T));
|
2023-11-05 19:05:32 -05:00
|
|
|
} else
|
|
|
|
{
|
|
|
|
if constexpr (std::is_copy_constructible_v<T> && !std::is_copy_assignable_v<T>)
|
|
|
|
{
|
2023-12-10 14:54:08 -05:00
|
|
|
for (size_t i = 0; i < this->size_; i++)
|
|
|
|
buffer_[i] = T(copy[i]);
|
2023-11-05 19:05:32 -05:00
|
|
|
} else
|
2023-12-10 14:54:08 -05:00
|
|
|
for (size_t i = 0; i < this->size_; i++)
|
|
|
|
buffer_[i] = copy[i];
|
2023-11-05 19:05:32 -05:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
2023-07-24 02:39:03 -04:00
|
|
|
|
2024-02-13 17:26:05 -05:00
|
|
|
constexpr scoped_buffer(scoped_buffer&& move) noexcept
|
2023-09-21 23:23:27 -04:00
|
|
|
{
|
2023-12-10 14:54:08 -05:00
|
|
|
delete[] buffer_;
|
|
|
|
buffer_ = move.buffer_;
|
|
|
|
size_ = move.size();
|
|
|
|
move.buffer_ = nullptr;
|
2023-07-24 02:52:11 -04:00
|
|
|
}
|
2023-07-24 02:39:03 -04:00
|
|
|
|
2024-02-13 17:26:05 -05:00
|
|
|
constexpr scoped_buffer& operator=(scoped_buffer&& moveAssignment) noexcept
|
2023-09-21 23:23:27 -04:00
|
|
|
{
|
2023-12-10 14:54:08 -05:00
|
|
|
delete[] buffer_;
|
|
|
|
buffer_ = moveAssignment.buffer_;
|
|
|
|
size_ = moveAssignment.size();
|
|
|
|
moveAssignment.buffer_ = nullptr;
|
2023-07-24 02:52:11 -04:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
2023-07-24 02:39:03 -04:00
|
|
|
|
2024-02-13 17:26:05 -05:00
|
|
|
constexpr inline T& operator[](size_t index)
|
2023-09-21 23:23:27 -04:00
|
|
|
{
|
2023-12-10 14:54:08 -05:00
|
|
|
return buffer_[index];
|
2023-07-24 02:39:03 -04:00
|
|
|
}
|
|
|
|
|
2024-02-13 17:26:05 -05:00
|
|
|
constexpr inline const T& operator[](size_t index) const
|
2023-09-21 23:23:27 -04:00
|
|
|
{
|
2023-12-10 14:54:08 -05:00
|
|
|
return buffer_[index];
|
2023-07-24 02:39:03 -04:00
|
|
|
}
|
|
|
|
|
2024-02-13 17:26:05 -05:00
|
|
|
constexpr inline T* operator*()
|
2023-09-21 23:23:27 -04:00
|
|
|
{
|
2023-12-10 14:54:08 -05:00
|
|
|
return buffer_;
|
2023-07-24 02:39:03 -04:00
|
|
|
}
|
|
|
|
|
2024-02-13 17:26:05 -05:00
|
|
|
[[nodiscard]] constexpr inline size_t size() const
|
2023-09-21 23:23:27 -04:00
|
|
|
{
|
2023-12-10 14:54:08 -05:00
|
|
|
return size_;
|
2023-07-24 02:39:03 -04:00
|
|
|
}
|
|
|
|
|
2024-02-13 17:26:05 -05:00
|
|
|
constexpr inline T*& ptr()
|
2023-09-21 23:23:27 -04:00
|
|
|
{
|
2023-12-10 14:54:08 -05:00
|
|
|
return buffer_;
|
2023-07-24 02:39:03 -04:00
|
|
|
}
|
|
|
|
|
2024-02-13 17:26:05 -05:00
|
|
|
constexpr inline const T* const& ptr() const
|
2023-10-25 01:23:16 -04:00
|
|
|
{
|
2023-12-10 14:54:08 -05:00
|
|
|
return buffer_;
|
2023-10-25 01:23:16 -04:00
|
|
|
}
|
|
|
|
|
2024-02-13 17:26:05 -05:00
|
|
|
constexpr inline const T* const& data() const
|
2023-10-25 01:23:33 -04:00
|
|
|
{
|
2023-12-10 14:54:08 -05:00
|
|
|
return buffer_;
|
2023-10-25 01:23:33 -04:00
|
|
|
}
|
|
|
|
|
2024-02-13 17:26:05 -05:00
|
|
|
constexpr inline T*& data()
|
2023-10-25 01:23:33 -04:00
|
|
|
{
|
2023-12-10 14:54:08 -05:00
|
|
|
return buffer_;
|
2023-10-25 01:23:33 -04:00
|
|
|
}
|
|
|
|
|
2024-02-13 17:06:33 -05:00
|
|
|
// inline auto begin()
|
|
|
|
// {
|
|
|
|
// return ptr_iterator{buffer_};
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// inline ptr_iterator<T> end()
|
|
|
|
// {
|
|
|
|
// return ptr_iterator{&buffer_[size_]};
|
|
|
|
// }
|
|
|
|
|
2024-02-13 17:26:05 -05:00
|
|
|
constexpr iterator begin() noexcept
|
2024-02-13 17:06:33 -05:00
|
|
|
{
|
2024-02-13 17:26:05 -05:00
|
|
|
return iterator{data()};
|
2024-02-13 17:06:33 -05:00
|
|
|
}
|
|
|
|
|
2024-02-13 17:26:05 -05:00
|
|
|
constexpr iterator end() noexcept
|
2024-02-13 17:06:33 -05:00
|
|
|
{
|
2024-02-13 17:26:05 -05:00
|
|
|
return iterator{data() + size()};
|
2024-02-13 17:06:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr const_iterator cbegin() const noexcept
|
|
|
|
{
|
2024-02-13 17:26:05 -05:00
|
|
|
return const_iterator{data()};
|
2024-02-13 17:06:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr const_iterator cend() const noexcept
|
|
|
|
{
|
2024-02-13 17:26:05 -05:00
|
|
|
return const_iterator{data() + size()};
|
2024-02-13 17:06:33 -05:00
|
|
|
}
|
|
|
|
|
2024-02-13 17:26:05 -05:00
|
|
|
constexpr reverse_iterator rbegin() noexcept
|
2024-02-13 17:06:33 -05:00
|
|
|
{
|
|
|
|
return reverse_iterator{end()};
|
|
|
|
}
|
|
|
|
|
2024-02-13 17:26:05 -05:00
|
|
|
constexpr reverse_iterator rend() noexcept
|
2023-10-25 01:26:44 -04:00
|
|
|
{
|
2024-02-13 17:06:33 -05:00
|
|
|
return reverse_iterator{begin()};
|
2023-10-25 01:26:44 -04:00
|
|
|
}
|
|
|
|
|
2024-02-13 17:06:33 -05:00
|
|
|
constexpr const_reverse_iterator crbegin() const noexcept
|
2023-09-21 23:23:27 -04:00
|
|
|
{
|
2024-02-13 17:06:33 -05:00
|
|
|
return reverse_iterator{cend()};
|
2023-07-24 02:39:03 -04:00
|
|
|
}
|
|
|
|
|
2024-02-13 17:06:33 -05:00
|
|
|
constexpr const_reverse_iterator crend() const noexcept
|
|
|
|
{
|
|
|
|
return reverse_iterator{cbegin()};
|
|
|
|
}
|
|
|
|
|
2023-09-21 23:23:27 -04:00
|
|
|
~scoped_buffer()
|
|
|
|
{
|
2023-12-10 14:54:08 -05:00
|
|
|
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;
|
|
|
|
|
2023-12-10 15:15:23 -05:00
|
|
|
inline bool push_back(const T& copy)
|
2023-12-10 14:54:08 -05:00
|
|
|
{
|
|
|
|
if (size_ >= MAX_SIZE)
|
|
|
|
return false;
|
2023-12-10 15:15:23 -05:00
|
|
|
buffer_[size_++] = copy;
|
2023-12-10 14:54:08 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool push_back(T&& move)
|
|
|
|
{
|
|
|
|
if (size_ >= MAX_SIZE)
|
|
|
|
return false;
|
2023-12-10 15:15:23 -05:00
|
|
|
buffer_[size_++] = std::move(move);
|
2023-12-10 14:54:08 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-12-10 15:15:23 -05:00
|
|
|
inline T& 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) + ')');
|
|
|
|
}
|
|
|
|
|
2023-12-10 14:54:08 -05:00
|
|
|
inline T& operator[](size_t index)
|
|
|
|
{
|
|
|
|
return buffer_[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
inline const T& operator[](size_t index) const
|
|
|
|
{
|
|
|
|
return buffer_[index];
|
|
|
|
}
|
|
|
|
|
2023-12-10 15:15:23 -05:00
|
|
|
inline void reserve(size_t size)
|
|
|
|
{
|
|
|
|
if (size > MAX_SIZE)
|
|
|
|
size = MAX_SIZE;
|
|
|
|
size_ = size;
|
|
|
|
}
|
|
|
|
|
2023-12-10 14:54:08 -05:00
|
|
|
[[nodiscard]] inline size_t size() const
|
|
|
|
{
|
|
|
|
return size_;
|
|
|
|
}
|
|
|
|
|
2023-12-10 15:15:23 -05:00
|
|
|
[[nodiscard]] inline size_t capacity() const
|
|
|
|
{
|
|
|
|
return MAX_SIZE;
|
|
|
|
}
|
|
|
|
|
2023-12-10 14:54:08 -05:00
|
|
|
inline T* data()
|
|
|
|
{
|
|
|
|
return buffer_;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline T* operator*()
|
|
|
|
{
|
|
|
|
return buffer_;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline T* data() const
|
|
|
|
{
|
|
|
|
return buffer_;
|
|
|
|
}
|
|
|
|
|
2023-12-10 15:15:23 -05:00
|
|
|
inline T* begin()
|
2023-12-10 14:54:08 -05:00
|
|
|
{
|
2023-12-10 15:15:23 -05:00
|
|
|
return buffer_;
|
2023-12-10 14:54:08 -05:00
|
|
|
}
|
|
|
|
|
2023-12-10 15:15:23 -05:00
|
|
|
inline T* end()
|
2023-12-10 14:54:08 -05:00
|
|
|
{
|
2023-12-10 15:15:23 -05:00
|
|
|
return &buffer_[size_];
|
2023-07-24 02:39:03 -04:00
|
|
|
}
|
2023-02-08 13:40:28 -05:00
|
|
|
};
|
2023-03-05 12:57:57 -05:00
|
|
|
|
2023-11-05 19:05:32 -05:00
|
|
|
template<typename T>
|
|
|
|
class scoped_buffer<T, false> : scoped_buffer<T, true>
|
|
|
|
{
|
|
|
|
using scoped_buffer<T, true>::scoped_buffer;
|
|
|
|
public:
|
|
|
|
scoped_buffer(const scoped_buffer& copy) = delete;
|
|
|
|
|
|
|
|
scoped_buffer operator=(scoped_buffer& copyAssignment) = delete;
|
|
|
|
};
|
|
|
|
|
2023-03-05 12:57:57 -05:00
|
|
|
template<typename T>
|
2023-09-21 23:23:27 -04:00
|
|
|
struct nullptr_initializer
|
|
|
|
{
|
2023-03-05 12:57:57 -05:00
|
|
|
private:
|
|
|
|
T* m_ptr = nullptr;
|
|
|
|
public:
|
|
|
|
nullptr_initializer() = default;
|
|
|
|
|
2023-09-21 23:23:27 -04:00
|
|
|
explicit nullptr_initializer(T* ptr): m_ptr(ptr)
|
|
|
|
{}
|
|
|
|
|
|
|
|
nullptr_initializer(const nullptr_initializer<T>& ptr): m_ptr(ptr.m_ptr)
|
|
|
|
{}
|
|
|
|
|
|
|
|
nullptr_initializer(nullptr_initializer<T>&& ptr) noexcept: m_ptr(ptr.m_ptr)
|
|
|
|
{}
|
|
|
|
|
|
|
|
nullptr_initializer<T>& operator=(const nullptr_initializer<T>& ptr)
|
|
|
|
{
|
2023-03-05 12:57:57 -05:00
|
|
|
if (&ptr == this)
|
|
|
|
return *this;
|
|
|
|
this->m_ptr = ptr.m_ptr;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2023-09-21 23:23:27 -04:00
|
|
|
nullptr_initializer<T>& operator=(nullptr_initializer<T>&& ptr) noexcept
|
|
|
|
{
|
2023-03-05 12:57:57 -05:00
|
|
|
if (&ptr == this)
|
|
|
|
return *this;
|
|
|
|
this->m_ptr = ptr.m_ptr;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2023-09-21 23:23:27 -04:00
|
|
|
inline T* operator->()
|
|
|
|
{
|
2023-03-05 12:57:57 -05:00
|
|
|
return m_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
~nullptr_initializer() = default;
|
|
|
|
};
|
2023-07-20 21:44:52 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a hash-map like association between an enum key and any arbitrary value.
|
|
|
|
* The storage is backed by a contiguous array for faster access.
|
|
|
|
* @tparam K enum value
|
|
|
|
* @tparam V associated value
|
|
|
|
*/
|
|
|
|
template<typename K, typename V>
|
2023-09-21 23:23:27 -04:00
|
|
|
class enum_storage
|
|
|
|
{
|
2023-07-20 21:44:52 -04:00
|
|
|
private:
|
2023-09-21 23:23:27 -04:00
|
|
|
V* m_values;
|
|
|
|
size_t m_size = 0;
|
2023-07-20 21:44:52 -04:00
|
|
|
public:
|
2023-09-21 23:23:27 -04:00
|
|
|
enum_storage(std::initializer_list<std::pair<K, V>> init)
|
|
|
|
{
|
2023-07-20 21:44:52 -04:00
|
|
|
for (auto& i : init)
|
2023-09-21 23:23:27 -04:00
|
|
|
m_size = std::max((size_t) i.first, m_size);
|
|
|
|
m_values = new V[m_size];
|
2023-07-21 03:32:42 -04:00
|
|
|
for (auto& v : init)
|
2023-09-21 23:23:27 -04:00
|
|
|
m_values[(size_t) v.first] = v.second;
|
2023-07-20 21:44:52 -04:00
|
|
|
}
|
|
|
|
|
2023-09-21 23:23:27 -04:00
|
|
|
inline V& operator[](size_t index)
|
|
|
|
{
|
|
|
|
return m_values[index];
|
2023-07-20 21:44:52 -04:00
|
|
|
}
|
|
|
|
|
2023-09-21 23:23:27 -04:00
|
|
|
inline const V& operator[](size_t index) const
|
|
|
|
{
|
|
|
|
return m_values[index];
|
2023-07-20 21:44:52 -04:00
|
|
|
}
|
|
|
|
|
2023-09-21 23:23:27 -04:00
|
|
|
[[nodiscard]] inline size_t size() const
|
|
|
|
{
|
|
|
|
return m_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr_iterator<V> begin()
|
|
|
|
{
|
|
|
|
return ptr_iterator{m_values};
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr_iterator<V> end()
|
|
|
|
{
|
|
|
|
return ptr_iterator{&m_values[m_size]};
|
2023-07-20 21:44:52 -04:00
|
|
|
}
|
|
|
|
|
2023-09-21 23:23:27 -04:00
|
|
|
~enum_storage()
|
|
|
|
{
|
|
|
|
delete[] m_values;
|
2023-07-20 21:44:52 -04:00
|
|
|
}
|
2023-09-21 23:23:27 -04:00
|
|
|
};
|
|
|
|
|
2023-02-08 13:40:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif //BLT_TESTS_MEMORY_H
|