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
|
|
|
|
2024-02-14 15:25:18 -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
|
|
|
|
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
|
|
|
|
*/
|
2024-02-14 15:25:18 -05:00
|
|
|
template<typename T>
|
|
|
|
class scoped_buffer<T, true>
|
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-14 15:21:59 -05:00
|
|
|
/**
|
|
|
|
* Resize the internal buffer. Nothing will occur if the sizes are equal.
|
|
|
|
* This function WILL NOT COPY ANY DATA. It is meant for use when creating a scoped buffer without size.
|
|
|
|
*/
|
|
|
|
constexpr void resize(size_t size)
|
|
|
|
{
|
|
|
|
if (size == 0)
|
|
|
|
return;
|
|
|
|
if (size == size_)
|
|
|
|
return;
|
|
|
|
delete[] buffer_;
|
|
|
|
buffer_ = new T[size];
|
|
|
|
size_ = size;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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-20 15:18:05 -05:00
|
|
|
constexpr inline reverse_iterator rbegin() noexcept
|
2024-02-13 17:06:33 -05:00
|
|
|
{
|
|
|
|
return reverse_iterator{end()};
|
|
|
|
}
|
|
|
|
|
2024-02-20 15:18:05 -05:00
|
|
|
constexpr inline 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-20 15:18:05 -05:00
|
|
|
constexpr inline const_iterator crbegin() const noexcept
|
2023-09-21 23:23:27 -04:00
|
|
|
{
|
2024-06-19 21:16:58 -04:00
|
|
|
return const_reverse_iterator{cend()};
|
2023-07-24 02:39:03 -04:00
|
|
|
}
|
|
|
|
|
2024-02-20 15:18:05 -05:00
|
|
|
constexpr inline reverse_iterator crend() const noexcept
|
2024-02-13 17:06:33 -05:00
|
|
|
{
|
|
|
|
return reverse_iterator{cbegin()};
|
|
|
|
}
|
2024-02-14 15:21:59 -05:00
|
|
|
|
2023-09-21 23:23:27 -04:00
|
|
|
~scoped_buffer()
|
|
|
|
{
|
2023-12-10 14:54:08 -05:00
|
|
|
delete[] buffer_;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-02-14 15:25:18 -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;
|
|
|
|
};
|
|
|
|
|
2024-06-19 21:16:58 -04:00
|
|
|
|
|
|
|
// TODO: might already have a version of this somewhere!
|
|
|
|
template<typename T, bool = std::is_copy_constructible_v<T> || std::is_copy_assignable_v<T>>
|
|
|
|
class expanding_buffer;
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class expanding_buffer<T, true>
|
|
|
|
{
|
|
|
|
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&;
|
|
|
|
using iterator = ptr_iterator<T>;
|
|
|
|
using const_iterator = ptr_iterator<const T>;
|
|
|
|
using reverse_iterator = std::reverse_iterator<iterator>;
|
|
|
|
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
|
|
|
|
private:
|
|
|
|
T* buffer_ = nullptr;
|
2024-06-24 13:52:15 -04:00
|
|
|
size_t size_ = 0;
|
2024-06-19 21:16:58 -04:00
|
|
|
public:
|
|
|
|
constexpr expanding_buffer(): buffer_(nullptr), size_(0)
|
|
|
|
{}
|
|
|
|
|
|
|
|
constexpr explicit expanding_buffer(size_t size): size_(size)
|
|
|
|
{
|
|
|
|
if (size > 0)
|
|
|
|
buffer_ = new T[size];
|
|
|
|
else
|
|
|
|
buffer_ = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr expanding_buffer(const expanding_buffer& copy)
|
|
|
|
{
|
|
|
|
if (copy.size() == 0)
|
|
|
|
{
|
|
|
|
buffer_ = nullptr;
|
|
|
|
size_ = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
buffer_ = new T[copy.size()];
|
|
|
|
size_ = copy.size_;
|
|
|
|
|
|
|
|
if constexpr (std::is_trivially_copyable_v<T>)
|
|
|
|
{
|
|
|
|
std::memcpy(buffer_, copy.buffer_, copy.size() * sizeof(T));
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
if constexpr (std::is_copy_constructible_v<T> && !std::is_copy_assignable_v<T>)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < this->size_; i++)
|
|
|
|
buffer_[i] = T(copy[i]);
|
|
|
|
} else
|
|
|
|
for (size_t i = 0; i < this->size_; i++)
|
|
|
|
buffer_[i] = copy[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr expanding_buffer& operator=(const expanding_buffer& copy)
|
|
|
|
{
|
|
|
|
if (© == this)
|
|
|
|
return *this;
|
|
|
|
|
|
|
|
if (copy.size() == 0)
|
|
|
|
{
|
|
|
|
buffer_ = nullptr;
|
|
|
|
size_ = 0;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete_this(buffer_, size());
|
|
|
|
buffer_ = new T[copy.size()];
|
|
|
|
size_ = copy.size_;
|
|
|
|
|
|
|
|
if constexpr (std::is_trivially_copyable_v<T>)
|
|
|
|
{
|
|
|
|
std::memcpy(buffer_, copy.buffer_, copy.size() * sizeof(T));
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
if constexpr (std::is_copy_constructible_v<T> && !std::is_copy_assignable_v<T>)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < this->size_; i++)
|
|
|
|
buffer_[i] = T(copy[i]);
|
|
|
|
} else
|
|
|
|
for (size_t i = 0; i < this->size_; i++)
|
|
|
|
buffer_[i] = copy[i];
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr expanding_buffer(expanding_buffer&& move) noexcept
|
|
|
|
{
|
|
|
|
delete_this(buffer_, size());
|
|
|
|
buffer_ = move.buffer_;
|
|
|
|
size_ = move.size();
|
|
|
|
move.buffer_ = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr expanding_buffer& operator=(expanding_buffer&& moveAssignment) noexcept
|
|
|
|
{
|
|
|
|
delete_this(buffer_, size());
|
|
|
|
buffer_ = moveAssignment.buffer_;
|
|
|
|
size_ = moveAssignment.size();
|
|
|
|
moveAssignment.buffer_ = nullptr;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resize the internal buffer. Nothing will occur if the sizes are equal.
|
|
|
|
* This function WILL NOT COPY ANY DATA. It is meant for use when creating a scoped buffer without size.
|
|
|
|
*/
|
|
|
|
constexpr void resize(size_t size)
|
|
|
|
{
|
|
|
|
if (size == 0)
|
|
|
|
return;
|
|
|
|
if (size == size_)
|
|
|
|
return;
|
|
|
|
delete_this(buffer_, this->size());
|
|
|
|
buffer_ = new T[size];
|
|
|
|
size_ = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr inline T& operator[](size_t index)
|
|
|
|
{
|
|
|
|
if (index >= size())
|
2024-07-29 19:34:03 -04:00
|
|
|
allocate_for(index);
|
2024-06-19 21:16:58 -04:00
|
|
|
return buffer_[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr inline const T& operator[](size_t index) const
|
|
|
|
{
|
|
|
|
if (index >= size())
|
2024-08-07 01:34:31 -04:00
|
|
|
BLT_ABORT("Index out of bounds");
|
2024-06-19 21:16:58 -04:00
|
|
|
return buffer_[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr inline T* operator*()
|
|
|
|
{
|
|
|
|
return buffer_;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr inline size_t size() const
|
|
|
|
{
|
|
|
|
return size_;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr inline T*& ptr()
|
|
|
|
{
|
|
|
|
return buffer_;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr inline const T* const& ptr() const
|
|
|
|
{
|
|
|
|
return buffer_;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr inline const T* const& data() const
|
|
|
|
{
|
|
|
|
return buffer_;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr inline T*& data()
|
|
|
|
{
|
|
|
|
return buffer_;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr iterator begin() noexcept
|
|
|
|
{
|
|
|
|
return iterator{data()};
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr iterator end() noexcept
|
|
|
|
{
|
|
|
|
return iterator{data() + size()};
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr const_iterator cbegin() const noexcept
|
|
|
|
{
|
|
|
|
return const_iterator{data()};
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr const_iterator cend() const noexcept
|
|
|
|
{
|
|
|
|
return const_iterator{data() + size()};
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr inline reverse_iterator rbegin() noexcept
|
|
|
|
{
|
|
|
|
return reverse_iterator{end()};
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr inline reverse_iterator rend() noexcept
|
|
|
|
{
|
|
|
|
return reverse_iterator{begin()};
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr inline const_iterator crbegin() const noexcept
|
|
|
|
{
|
|
|
|
return const_reverse_iterator{cend()};
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr inline reverse_iterator crend() const noexcept
|
|
|
|
{
|
|
|
|
return reverse_iterator{cbegin()};
|
|
|
|
}
|
|
|
|
|
|
|
|
~expanding_buffer()
|
|
|
|
{
|
|
|
|
delete_this(buffer_, size());
|
|
|
|
}
|
2024-07-29 19:34:03 -04:00
|
|
|
|
|
|
|
void expand(blt::size_t new_size)
|
2024-06-19 21:16:58 -04:00
|
|
|
{
|
2024-07-29 19:34:03 -04:00
|
|
|
T* new_buffer = new T[new_size];
|
2024-06-26 18:55:11 -04:00
|
|
|
if (buffer_ != nullptr)
|
2024-06-19 21:16:58 -04:00
|
|
|
{
|
2024-06-26 18:55:11 -04:00
|
|
|
if constexpr (std::is_trivially_copyable_v<T>)
|
2024-06-19 21:16:58 -04:00
|
|
|
{
|
2024-06-26 18:55:11 -04:00
|
|
|
std::memcpy(new_buffer, buffer_, size_ * sizeof(T));
|
2024-06-19 21:16:58 -04:00
|
|
|
} else
|
2024-06-26 18:55:11 -04:00
|
|
|
{
|
|
|
|
if constexpr (std::is_copy_constructible_v<T> && !std::is_move_constructible_v<T>)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < size_; i++)
|
|
|
|
new_buffer[i] = T(buffer_[i]);
|
|
|
|
} else
|
|
|
|
for (size_t i = 0; i < size_; i++)
|
|
|
|
new_buffer[i] = std::move(buffer_[i]);
|
|
|
|
}
|
|
|
|
delete[] buffer_;
|
2024-06-19 21:16:58 -04:00
|
|
|
}
|
|
|
|
buffer_ = new_buffer;
|
2024-07-29 19:34:03 -04:00
|
|
|
size_ = new_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void allocate_for(blt::size_t accessing_index)
|
|
|
|
{
|
|
|
|
accessing_index = std::max(size_, accessing_index);
|
|
|
|
accessing_index = blt::mem::next_byte_allocation(accessing_index);
|
|
|
|
expand(accessing_index);
|
2024-06-19 21:16:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void delete_this(T* buffer, blt::size_t)
|
|
|
|
{
|
|
|
|
// if constexpr (std::is_trivially_destructible_v<T>)
|
|
|
|
// return;
|
|
|
|
// if (buffer == nullptr)
|
|
|
|
// return;
|
|
|
|
// for (blt::size_t i = 0; i < size; i++)
|
|
|
|
// buffer[i]->~T();
|
|
|
|
// free(buffer);
|
|
|
|
delete[] buffer;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class expanding_buffer<T, false> : expanding_buffer<T, true>
|
|
|
|
{
|
|
|
|
using expanding_buffer<T, true>::expanding_buffer;
|
|
|
|
public:
|
|
|
|
expanding_buffer(const expanding_buffer& copy) = delete;
|
|
|
|
|
|
|
|
expanding_buffer operator=(expanding_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
|