2018-10-07 03:42:33 +00:00
|
|
|
#pragma once
|
2015-08-22 01:58:41 +00:00
|
|
|
|
2017-07-15 14:13:36 +00:00
|
|
|
#include <algorithm>
|
2017-12-29 08:08:12 +00:00
|
|
|
#include <cstdlib>
|
2019-06-12 02:05:17 +00:00
|
|
|
#include <optional>
|
2019-10-04 04:08:43 +00:00
|
|
|
#include <type_traits>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
#include <logvisor/logvisor.hpp>
|
|
|
|
#endif
|
2015-08-22 01:58:41 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
namespace rstl {
|
2015-08-22 01:58:41 +00:00
|
|
|
|
2017-09-11 07:08:30 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
static logvisor::Module Log("rstl");
|
|
|
|
#endif
|
|
|
|
|
2015-08-22 01:58:41 +00:00
|
|
|
/**
|
2017-09-11 07:08:30 +00:00
|
|
|
* @brief Base vector backed by statically-allocated array
|
2015-08-22 01:58:41 +00:00
|
|
|
*/
|
2018-02-15 04:37:21 +00:00
|
|
|
template <class T>
|
2018-12-08 05:30:43 +00:00
|
|
|
class _reserved_vector_base {
|
2015-08-22 01:58:41 +00:00
|
|
|
public:
|
2018-12-08 05:30:43 +00:00
|
|
|
class const_iterator {
|
|
|
|
friend class _reserved_vector_base;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
const T* m_val;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit const_iterator(const T* val) : m_val(val) {}
|
|
|
|
using value_type = T;
|
|
|
|
using difference_type = std::ptrdiff_t;
|
|
|
|
using pointer = T*;
|
|
|
|
using reference = T&;
|
|
|
|
using iterator_category = std::random_access_iterator_tag;
|
|
|
|
|
|
|
|
const T& operator*() const { return *m_val; }
|
|
|
|
const T* operator->() const { return m_val; }
|
|
|
|
const_iterator& operator++() {
|
|
|
|
++m_val;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
const_iterator& operator--() {
|
|
|
|
--m_val;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
const_iterator operator++(int) {
|
|
|
|
auto ret = *this;
|
|
|
|
++m_val;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
const_iterator operator--(int) {
|
|
|
|
auto ret = *this;
|
|
|
|
--m_val;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
bool operator!=(const const_iterator& other) const { return m_val != other.m_val; }
|
|
|
|
bool operator==(const const_iterator& other) const { return m_val == other.m_val; }
|
|
|
|
const_iterator operator+(std::ptrdiff_t i) const { return const_iterator(m_val + i); }
|
|
|
|
const_iterator operator-(std::ptrdiff_t i) const { return const_iterator(m_val - i); }
|
|
|
|
const_iterator& operator+=(std::ptrdiff_t i) {
|
|
|
|
m_val += i;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
const_iterator& operator-=(std::ptrdiff_t i) {
|
|
|
|
m_val -= i;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
std::ptrdiff_t operator-(const const_iterator& it) const { return m_val - it.m_val; }
|
|
|
|
bool operator>(const const_iterator& it) const { return m_val > it.m_val; }
|
|
|
|
bool operator<(const const_iterator& it) const { return m_val < it.m_val; }
|
|
|
|
bool operator>=(const const_iterator& it) const { return m_val >= it.m_val; }
|
|
|
|
bool operator<=(const const_iterator& it) const { return m_val <= it.m_val; }
|
|
|
|
const T& operator[](std::ptrdiff_t i) const { return m_val[i]; }
|
|
|
|
};
|
|
|
|
|
|
|
|
class iterator : public const_iterator {
|
|
|
|
friend class _reserved_vector_base;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit iterator(T* val) : const_iterator(val) {}
|
|
|
|
T& operator*() const { return *const_cast<T*>(const_iterator::m_val); }
|
|
|
|
T* operator->() const { return const_cast<T*>(const_iterator::m_val); }
|
|
|
|
iterator& operator++() {
|
|
|
|
++const_iterator::m_val;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
iterator& operator--() {
|
|
|
|
--const_iterator::m_val;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
iterator operator++(int) {
|
|
|
|
auto ret = *this;
|
|
|
|
++const_iterator::m_val;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
iterator operator--(int) {
|
|
|
|
auto ret = *this;
|
|
|
|
--const_iterator::m_val;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
iterator operator+(std::ptrdiff_t i) const { return iterator(const_cast<T*>(const_iterator::m_val) + i); }
|
|
|
|
iterator operator-(std::ptrdiff_t i) const { return iterator(const_cast<T*>(const_iterator::m_val) - i); }
|
|
|
|
iterator& operator+=(std::ptrdiff_t i) {
|
|
|
|
const_iterator::m_val += i;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
iterator& operator-=(std::ptrdiff_t i) {
|
|
|
|
const_iterator::m_val -= i;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
std::ptrdiff_t operator-(const iterator& it) const { return const_iterator::m_val - it.m_val; }
|
|
|
|
bool operator>(const iterator& it) const { return const_iterator::m_val > it.m_val; }
|
|
|
|
bool operator<(const iterator& it) const { return const_iterator::m_val < it.m_val; }
|
|
|
|
bool operator>=(const iterator& it) const { return const_iterator::m_val >= it.m_val; }
|
|
|
|
bool operator<=(const iterator& it) const { return const_iterator::m_val <= it.m_val; }
|
|
|
|
T& operator[](std::ptrdiff_t i) const { return const_cast<T*>(const_iterator::m_val)[i]; }
|
|
|
|
};
|
|
|
|
|
|
|
|
class const_reverse_iterator {
|
|
|
|
friend class _reserved_vector_base;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
const T* m_val;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit const_reverse_iterator(const T* val) : m_val(val) {}
|
|
|
|
using value_type = T;
|
|
|
|
using difference_type = std::ptrdiff_t;
|
|
|
|
using pointer = T*;
|
|
|
|
using reference = T&;
|
|
|
|
using iterator_category = std::random_access_iterator_tag;
|
|
|
|
|
|
|
|
const T& operator*() const { return *m_val; }
|
|
|
|
const T* operator->() const { return m_val; }
|
|
|
|
const_reverse_iterator& operator++() {
|
|
|
|
--m_val;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
const_reverse_iterator& operator--() {
|
|
|
|
++m_val;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
const_reverse_iterator operator++(int) {
|
|
|
|
auto ret = *this;
|
|
|
|
--m_val;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
const_reverse_iterator operator--(int) {
|
|
|
|
auto ret = *this;
|
|
|
|
++m_val;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
bool operator!=(const const_reverse_iterator& other) const { return m_val != other.m_val; }
|
|
|
|
bool operator==(const const_reverse_iterator& other) const { return m_val == other.m_val; }
|
|
|
|
const_reverse_iterator operator+(std::ptrdiff_t i) const { return const_reverse_iterator(m_val - i); }
|
|
|
|
const_reverse_iterator operator-(std::ptrdiff_t i) const { return const_reverse_iterator(m_val + i); }
|
|
|
|
const_reverse_iterator& operator+=(std::ptrdiff_t i) {
|
|
|
|
m_val -= i;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
const_reverse_iterator& operator-=(std::ptrdiff_t i) {
|
|
|
|
m_val += i;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
std::ptrdiff_t operator-(const const_reverse_iterator& it) const { return it.m_val - m_val; }
|
|
|
|
bool operator>(const const_iterator& it) const { return it.m_val > m_val; }
|
|
|
|
bool operator<(const const_iterator& it) const { return it.m_val < m_val; }
|
|
|
|
bool operator>=(const const_iterator& it) const { return it.m_val >= m_val; }
|
|
|
|
bool operator<=(const const_iterator& it) const { return it.m_val <= m_val; }
|
|
|
|
const T& operator[](std::ptrdiff_t i) const { return m_val[-i]; }
|
|
|
|
};
|
|
|
|
|
|
|
|
class reverse_iterator : public const_reverse_iterator {
|
|
|
|
friend class _reserved_vector_base;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit reverse_iterator(T* val) : const_reverse_iterator(val) {}
|
|
|
|
T& operator*() const { return *const_cast<T*>(const_reverse_iterator::m_val); }
|
|
|
|
T* operator->() const { return const_cast<T*>(const_reverse_iterator::m_val); }
|
|
|
|
reverse_iterator& operator++() {
|
|
|
|
--const_reverse_iterator::m_val;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
reverse_iterator& operator--() {
|
|
|
|
++const_reverse_iterator::m_val;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
reverse_iterator operator++(int) {
|
|
|
|
auto ret = *this;
|
|
|
|
--const_reverse_iterator::m_val;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
reverse_iterator operator--(int) {
|
|
|
|
auto ret = *this;
|
|
|
|
++const_reverse_iterator::m_val;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
reverse_iterator operator+(std::ptrdiff_t i) const {
|
|
|
|
return reverse_iterator(const_cast<T*>(const_reverse_iterator::m_val) - i);
|
|
|
|
}
|
|
|
|
reverse_iterator operator-(std::ptrdiff_t i) const {
|
|
|
|
return reverse_iterator(const_cast<T*>(const_reverse_iterator::m_val) + i);
|
|
|
|
}
|
|
|
|
reverse_iterator& operator+=(std::ptrdiff_t i) {
|
|
|
|
const_reverse_iterator::m_val -= i;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
reverse_iterator& operator-=(std::ptrdiff_t i) {
|
|
|
|
const_reverse_iterator::m_val += i;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
std::ptrdiff_t operator-(const reverse_iterator& it) const { return it.m_val - const_reverse_iterator::m_val; }
|
|
|
|
bool operator>(const const_reverse_iterator& it) const { return it.m_val > const_reverse_iterator::m_val; }
|
|
|
|
bool operator<(const const_reverse_iterator& it) const { return it.m_val < const_reverse_iterator::m_val; }
|
|
|
|
bool operator>=(const const_reverse_iterator& it) const { return it.m_val >= const_reverse_iterator::m_val; }
|
|
|
|
bool operator<=(const const_reverse_iterator& it) const { return it.m_val <= const_reverse_iterator::m_val; }
|
|
|
|
T& operator[](std::ptrdiff_t i) const { return const_cast<T*>(const_reverse_iterator::m_val)[-i]; }
|
|
|
|
};
|
2017-09-11 07:08:30 +00:00
|
|
|
|
|
|
|
protected:
|
2018-12-08 05:30:43 +00:00
|
|
|
static iterator _const_cast_iterator(const const_iterator& it) { return iterator(const_cast<T*>(it.m_val)); }
|
2017-09-11 07:08:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Vector backed by statically-allocated array with uninitialized storage
|
|
|
|
*/
|
|
|
|
template <class T, size_t N>
|
2018-12-08 05:30:43 +00:00
|
|
|
class reserved_vector : public _reserved_vector_base<T> {
|
2019-10-04 04:28:13 +00:00
|
|
|
using base = _reserved_vector_base<T>;
|
|
|
|
|
|
|
|
public:
|
|
|
|
using value_type = T;
|
|
|
|
|
|
|
|
using pointer = value_type*;
|
|
|
|
using const_pointer = const value_type*;
|
|
|
|
|
|
|
|
using reference = value_type&;
|
|
|
|
using const_reference = const value_type&;
|
|
|
|
|
|
|
|
using difference_type = std::ptrdiff_t;
|
|
|
|
using size_type = std::size_t;
|
|
|
|
|
|
|
|
using iterator = typename base::iterator;
|
|
|
|
using const_iterator = typename base::const_iterator;
|
|
|
|
using reverse_iterator = typename base::reverse_iterator;
|
|
|
|
using const_reverse_iterator = typename base::const_reverse_iterator;
|
|
|
|
|
|
|
|
private:
|
2018-12-08 05:30:43 +00:00
|
|
|
union alignas(T) storage_t {
|
|
|
|
struct {
|
|
|
|
} _dummy;
|
|
|
|
T _value;
|
|
|
|
storage_t() : _dummy() {}
|
|
|
|
~storage_t() {}
|
|
|
|
};
|
|
|
|
size_t x0_size;
|
|
|
|
storage_t x4_data[N];
|
|
|
|
T& _value(std::ptrdiff_t idx) { return x4_data[idx]._value; }
|
|
|
|
const T& _value(std::ptrdiff_t idx) const { return x4_data[idx]._value; }
|
|
|
|
template <typename Tp>
|
|
|
|
static void
|
2019-10-04 04:14:10 +00:00
|
|
|
destroy(Tp& t, std::enable_if_t<std::is_destructible_v<Tp> && !std::is_trivially_destructible_v<Tp>>* = nullptr) {
|
2018-12-08 05:30:43 +00:00
|
|
|
t.Tp::~Tp();
|
|
|
|
}
|
|
|
|
template <typename Tp>
|
|
|
|
static void
|
2019-10-04 04:14:10 +00:00
|
|
|
destroy(Tp& t, std::enable_if_t<!std::is_destructible_v<Tp> || std::is_trivially_destructible_v<Tp>>* = nullptr) {
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
2018-02-15 04:37:21 +00:00
|
|
|
|
2017-09-11 07:08:30 +00:00
|
|
|
public:
|
2019-10-04 04:53:12 +00:00
|
|
|
reserved_vector() noexcept(std::is_nothrow_constructible_v<T>) : x0_size(0) {}
|
2018-12-08 05:30:43 +00:00
|
|
|
|
2019-01-03 03:47:28 +00:00
|
|
|
template <size_t LN>
|
2019-10-04 04:53:12 +00:00
|
|
|
reserved_vector(const T (&l)[LN]) noexcept(std::is_nothrow_copy_constructible_v<T>)
|
2019-01-03 03:47:28 +00:00
|
|
|
: x0_size(LN) {
|
|
|
|
static_assert(LN <= N, "initializer array too large for reserved_vector");
|
2019-10-04 04:14:10 +00:00
|
|
|
for (size_t i = 0; i < LN; ++i) {
|
2019-01-03 03:47:28 +00:00
|
|
|
::new (static_cast<void*>(std::addressof(_value(i)))) T(l[i]);
|
2019-10-04 04:14:10 +00:00
|
|
|
}
|
2019-01-03 03:47:28 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 04:53:12 +00:00
|
|
|
reserved_vector(const reserved_vector& other) noexcept(std::is_nothrow_copy_constructible_v<T>)
|
|
|
|
: x0_size(other.x0_size) {
|
2019-10-04 04:14:10 +00:00
|
|
|
for (size_t i = 0; i < x0_size; ++i) {
|
2018-12-08 05:30:43 +00:00
|
|
|
::new (static_cast<void*>(std::addressof(_value(i)))) T(other._value(i));
|
2019-10-04 04:14:10 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 04:53:12 +00:00
|
|
|
reserved_vector& operator=(const reserved_vector& other) noexcept(std::is_nothrow_copy_assignable_v<T>) {
|
2018-12-08 05:30:43 +00:00
|
|
|
size_t i = 0;
|
|
|
|
if (other.x0_size > x0_size) {
|
2019-10-04 04:14:10 +00:00
|
|
|
for (; i < x0_size; ++i) {
|
2018-12-08 05:30:43 +00:00
|
|
|
_value(i) = other._value(i);
|
2019-10-04 04:14:10 +00:00
|
|
|
}
|
|
|
|
for (; i < other.x0_size; ++i) {
|
2018-12-08 05:30:43 +00:00
|
|
|
::new (static_cast<void*>(std::addressof(_value(i)))) T(other._value(i));
|
2019-10-04 04:14:10 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
} else if (other.x0_size < x0_size) {
|
2019-10-04 04:14:10 +00:00
|
|
|
for (; i < other.x0_size; ++i) {
|
2018-12-08 05:30:43 +00:00
|
|
|
_value(i) = other._value(i);
|
2019-10-04 04:14:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if constexpr (std::is_destructible_v<T> && !std::is_trivially_destructible_v<T>) {
|
|
|
|
for (; i < x0_size; ++i) {
|
2018-12-08 05:30:43 +00:00
|
|
|
destroy(_value(i));
|
2019-10-04 04:14:10 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
} else {
|
2019-10-04 04:14:10 +00:00
|
|
|
for (; i < other.x0_size; ++i) {
|
2018-12-08 05:30:43 +00:00
|
|
|
_value(i) = other._value(i);
|
2019-10-04 04:14:10 +00:00
|
|
|
}
|
2017-09-18 20:29:59 +00:00
|
|
|
}
|
2019-10-04 04:14:10 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
x0_size = other.x0_size;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-10-04 04:53:12 +00:00
|
|
|
reserved_vector(reserved_vector&& other) noexcept(std::is_nothrow_move_constructible_v<T>) : x0_size(other.x0_size) {
|
2019-10-04 04:14:10 +00:00
|
|
|
for (size_t i = 0; i < x0_size; ++i) {
|
2018-12-08 05:30:43 +00:00
|
|
|
::new (static_cast<void*>(std::addressof(_value(i)))) T(std::forward<T>(other._value(i)));
|
2019-10-04 04:14:10 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 04:53:12 +00:00
|
|
|
reserved_vector& operator=(reserved_vector&& other) noexcept(
|
|
|
|
std::is_nothrow_move_assignable_v<T>&& std::is_nothrow_move_constructible_v<T>) {
|
2018-12-08 05:30:43 +00:00
|
|
|
size_t i = 0;
|
|
|
|
if (other.x0_size > x0_size) {
|
2019-10-04 04:14:10 +00:00
|
|
|
for (; i < x0_size; ++i) {
|
2018-12-08 05:30:43 +00:00
|
|
|
_value(i) = std::forward<T>(other._value(i));
|
2019-10-04 04:14:10 +00:00
|
|
|
}
|
|
|
|
for (; i < other.x0_size; ++i) {
|
2018-12-08 05:30:43 +00:00
|
|
|
::new (static_cast<void*>(std::addressof(_value(i)))) T(std::forward<T>(other._value(i)));
|
2019-10-04 04:14:10 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
} else if (other.x0_size < x0_size) {
|
2019-10-04 04:14:10 +00:00
|
|
|
for (; i < other.x0_size; ++i) {
|
2018-12-08 05:30:43 +00:00
|
|
|
_value(i) = std::forward<T>(other._value(i));
|
2019-10-04 04:14:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if constexpr (std::is_destructible_v<T> && !std::is_trivially_destructible_v<T>) {
|
|
|
|
for (; i < x0_size; ++i) {
|
2018-12-08 05:30:43 +00:00
|
|
|
destroy(_value(i));
|
2019-10-04 04:14:10 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
} else {
|
2019-10-04 04:14:10 +00:00
|
|
|
for (; i < other.x0_size; ++i) {
|
2018-12-08 05:30:43 +00:00
|
|
|
_value(i) = std::forward<T>(other._value(i));
|
2019-10-04 04:14:10 +00:00
|
|
|
}
|
2017-09-18 20:29:59 +00:00
|
|
|
}
|
2019-10-04 04:14:10 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
x0_size = other.x0_size;
|
|
|
|
return *this;
|
|
|
|
}
|
2017-09-18 20:29:59 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
~reserved_vector() {
|
2019-10-04 04:14:10 +00:00
|
|
|
if constexpr (std::is_destructible_v<T> && !std::is_trivially_destructible_v<T>) {
|
|
|
|
for (size_t i = 0; i < x0_size; ++i) {
|
2018-12-08 05:30:43 +00:00
|
|
|
destroy(_value(i));
|
2019-10-04 04:14:10 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
2017-09-18 20:29:59 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
void push_back(const T& d) {
|
2017-09-11 07:08:30 +00:00
|
|
|
#ifndef NDEBUG
|
2019-10-04 04:14:10 +00:00
|
|
|
if (x0_size == N) {
|
2019-07-20 04:27:21 +00:00
|
|
|
Log.report(logvisor::Fatal, fmt("push_back() called on full rstl::reserved_vector."));
|
2019-10-04 04:14:10 +00:00
|
|
|
}
|
2017-09-11 07:08:30 +00:00
|
|
|
#endif
|
2019-10-04 04:14:10 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
::new (static_cast<void*>(std::addressof(_value(x0_size)))) T(d);
|
|
|
|
++x0_size;
|
|
|
|
}
|
2017-01-07 01:58:05 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
void push_back(T&& d) {
|
2017-09-11 07:08:30 +00:00
|
|
|
#ifndef NDEBUG
|
2019-10-04 04:14:10 +00:00
|
|
|
if (x0_size == N) {
|
2019-07-20 04:27:21 +00:00
|
|
|
Log.report(logvisor::Fatal, fmt("push_back() called on full rstl::reserved_vector."));
|
2019-10-04 04:14:10 +00:00
|
|
|
}
|
2017-09-11 07:08:30 +00:00
|
|
|
#endif
|
2019-10-04 04:14:10 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
::new (static_cast<void*>(std::addressof(_value(x0_size)))) T(std::forward<T>(d));
|
|
|
|
++x0_size;
|
|
|
|
}
|
2017-01-07 01:58:05 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
template <class... _Args>
|
|
|
|
void emplace_back(_Args&&... args) {
|
2017-09-11 07:08:30 +00:00
|
|
|
#ifndef NDEBUG
|
2019-10-04 04:14:10 +00:00
|
|
|
if (x0_size == N) {
|
2019-07-20 04:27:21 +00:00
|
|
|
Log.report(logvisor::Fatal, fmt("emplace_back() called on full rstl::reserved_vector."));
|
2019-10-04 04:14:10 +00:00
|
|
|
}
|
2017-09-11 07:08:30 +00:00
|
|
|
#endif
|
2019-10-04 04:14:10 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
::new (static_cast<void*>(std::addressof(_value(x0_size)))) T(std::forward<_Args>(args)...);
|
|
|
|
++x0_size;
|
|
|
|
}
|
2017-01-07 01:58:05 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
void pop_back() {
|
2017-09-11 07:08:30 +00:00
|
|
|
#ifndef NDEBUG
|
2019-10-04 04:14:10 +00:00
|
|
|
if (x0_size == 0) {
|
2019-07-20 04:27:21 +00:00
|
|
|
Log.report(logvisor::Fatal, fmt("pop_back() called on empty rstl::reserved_vector."));
|
2019-10-04 04:14:10 +00:00
|
|
|
}
|
2017-09-11 07:08:30 +00:00
|
|
|
#endif
|
2019-10-04 04:14:10 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
--x0_size;
|
|
|
|
destroy(_value(x0_size));
|
|
|
|
}
|
2017-09-11 07:08:30 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
iterator insert(const_iterator pos, const T& value) {
|
2017-09-11 07:08:30 +00:00
|
|
|
#ifndef NDEBUG
|
2019-10-04 04:14:10 +00:00
|
|
|
if (x0_size == N) {
|
2019-07-20 04:27:21 +00:00
|
|
|
Log.report(logvisor::Fatal, fmt("insert() called on full rstl::reserved_vector."));
|
2019-10-04 04:14:10 +00:00
|
|
|
}
|
2017-09-11 07:08:30 +00:00
|
|
|
#endif
|
2019-10-04 04:14:10 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
auto target_it = base::_const_cast_iterator(pos);
|
|
|
|
if (pos == cend()) {
|
|
|
|
::new (static_cast<void*>(std::addressof(_value(x0_size)))) T(value);
|
|
|
|
} else {
|
|
|
|
::new (static_cast<void*>(std::addressof(_value(x0_size)))) T(std::forward<T>(_value(x0_size - 1)));
|
2019-10-04 04:14:10 +00:00
|
|
|
for (auto it = end() - 1; it != target_it; --it) {
|
2018-12-08 05:30:43 +00:00
|
|
|
*it = std::forward<T>(*(it - 1));
|
2019-10-04 04:14:10 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
*target_it = value;
|
2017-09-11 07:08:30 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
++x0_size;
|
|
|
|
return target_it;
|
|
|
|
}
|
2017-09-11 07:08:30 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
iterator insert(const_iterator pos, T&& value) {
|
2017-09-11 07:08:30 +00:00
|
|
|
#ifndef NDEBUG
|
2018-12-08 05:30:43 +00:00
|
|
|
if (x0_size == N)
|
2019-07-20 04:27:21 +00:00
|
|
|
Log.report(logvisor::Fatal, fmt("insert() called on full rstl::reserved_vector."));
|
2017-09-11 07:08:30 +00:00
|
|
|
#endif
|
2018-12-08 05:30:43 +00:00
|
|
|
auto target_it = base::_const_cast_iterator(pos);
|
|
|
|
if (pos == cend()) {
|
|
|
|
::new (static_cast<void*>(std::addressof(_value(x0_size)))) T(std::forward<T>(value));
|
|
|
|
} else {
|
|
|
|
::new (static_cast<void*>(std::addressof(_value(x0_size)))) T(std::forward<T>(_value(x0_size - 1)));
|
2019-10-04 04:14:10 +00:00
|
|
|
for (auto it = end() - 1; it != target_it; --it) {
|
2018-12-08 05:30:43 +00:00
|
|
|
*it = std::forward<T>(*(it - 1));
|
2019-10-04 04:14:10 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
*target_it = std::forward<T>(value);
|
2017-09-11 07:08:30 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
++x0_size;
|
|
|
|
return target_it;
|
|
|
|
}
|
2017-09-11 07:08:30 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
void resize(size_t size) {
|
2017-09-11 07:08:30 +00:00
|
|
|
#ifndef NDEBUG
|
2019-10-04 04:14:10 +00:00
|
|
|
if (size > N) {
|
2019-07-20 04:27:21 +00:00
|
|
|
Log.report(logvisor::Fatal, fmt("resize() call overflows rstl::reserved_vector."));
|
2019-10-04 04:14:10 +00:00
|
|
|
}
|
2017-09-11 07:08:30 +00:00
|
|
|
#endif
|
2019-10-04 04:14:10 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
if (size > x0_size) {
|
2019-10-04 04:14:10 +00:00
|
|
|
for (size_t i = x0_size; i < size; ++i) {
|
2018-12-08 05:30:43 +00:00
|
|
|
::new (static_cast<void*>(std::addressof(_value(i)))) T();
|
2019-10-04 04:14:10 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
x0_size = size;
|
|
|
|
} else if (size < x0_size) {
|
2019-10-04 04:14:10 +00:00
|
|
|
if constexpr (std::is_destructible_v<T> && !std::is_trivially_destructible_v<T>) {
|
|
|
|
for (size_t i = size; i < x0_size; ++i) {
|
2018-12-08 05:30:43 +00:00
|
|
|
destroy(_value(i));
|
2019-10-04 04:14:10 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
x0_size = size;
|
2017-09-11 07:08:30 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
2017-09-11 07:08:30 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
void resize(size_t size, const T& value) {
|
2017-09-11 07:08:30 +00:00
|
|
|
#ifndef NDEBUG
|
2019-10-04 04:14:10 +00:00
|
|
|
if (size > N) {
|
2019-07-20 04:27:21 +00:00
|
|
|
Log.report(logvisor::Fatal, fmt("resize() call overflows rstl::reserved_vector."));
|
2019-10-04 04:14:10 +00:00
|
|
|
}
|
2017-09-11 07:08:30 +00:00
|
|
|
#endif
|
2019-10-04 04:14:10 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
if (size > x0_size) {
|
2019-10-04 04:14:10 +00:00
|
|
|
for (size_t i = x0_size; i < size; ++i) {
|
2018-12-08 05:30:43 +00:00
|
|
|
::new (static_cast<void*>(std::addressof(_value(i)))) T(value);
|
2019-10-04 04:14:10 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
x0_size = size;
|
|
|
|
} else if (size < x0_size) {
|
2019-10-04 04:14:10 +00:00
|
|
|
if constexpr (std::is_destructible_v<T> && !std::is_trivially_destructible_v<T>) {
|
|
|
|
for (size_t i = size; i < x0_size; ++i) {
|
2018-12-08 05:30:43 +00:00
|
|
|
destroy(_value(i));
|
2019-10-04 04:14:10 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
x0_size = size;
|
2017-09-11 07:08:30 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
2017-09-11 07:08:30 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
iterator erase(const_iterator pos) {
|
2017-09-11 07:08:30 +00:00
|
|
|
#ifndef NDEBUG
|
2019-10-04 04:14:10 +00:00
|
|
|
if (x0_size == 0) {
|
2019-07-20 04:27:21 +00:00
|
|
|
Log.report(logvisor::Fatal, fmt("erase() called on empty rstl::reserved_vector."));
|
2019-10-04 04:14:10 +00:00
|
|
|
}
|
2017-09-11 07:08:30 +00:00
|
|
|
#endif
|
2019-10-04 04:14:10 +00:00
|
|
|
|
|
|
|
for (auto it = base::_const_cast_iterator(pos) + 1; it != end(); ++it) {
|
2018-12-08 05:30:43 +00:00
|
|
|
*(it - 1) = std::forward<T>(*it);
|
2019-10-04 04:14:10 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
--x0_size;
|
|
|
|
destroy(_value(x0_size));
|
|
|
|
return base::_const_cast_iterator(pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
void pop_front() {
|
2019-10-04 04:14:10 +00:00
|
|
|
if (x0_size != 0) {
|
2018-12-08 05:30:43 +00:00
|
|
|
erase(begin());
|
2019-10-04 04:14:10 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void clear() {
|
2019-10-04 04:14:10 +00:00
|
|
|
if constexpr (std::is_destructible_v<T> && !std::is_trivially_destructible_v<T>) {
|
|
|
|
for (auto it = begin(); it != end(); ++it) {
|
2018-12-08 05:30:43 +00:00
|
|
|
destroy(*it);
|
2019-10-04 04:14:10 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
x0_size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t size() const noexcept { return x0_size; }
|
|
|
|
bool empty() const noexcept { return x0_size == 0; }
|
|
|
|
constexpr size_t capacity() const noexcept { return N; }
|
|
|
|
const T* data() const noexcept { return std::addressof(_value(0)); }
|
|
|
|
T* data() noexcept { return std::addressof(_value(0)); }
|
|
|
|
|
|
|
|
T& back() { return _value(x0_size - 1); }
|
|
|
|
T& front() { return _value(0); }
|
|
|
|
const T& back() const { return _value(x0_size - 1); }
|
|
|
|
const T& front() const { return _value(0); }
|
|
|
|
|
|
|
|
const_iterator begin() const noexcept { return const_iterator(std::addressof(_value(0))); }
|
|
|
|
const_iterator end() const noexcept { return const_iterator(std::addressof(_value(x0_size))); }
|
|
|
|
iterator begin() noexcept { return iterator(std::addressof(_value(0))); }
|
|
|
|
iterator end() noexcept { return iterator(std::addressof(_value(x0_size))); }
|
|
|
|
const_iterator cbegin() const noexcept { return begin(); }
|
|
|
|
const_iterator cend() const noexcept { return end(); }
|
|
|
|
|
|
|
|
const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(std::addressof(_value(x0_size - 1))); }
|
|
|
|
const_reverse_iterator rend() const noexcept { return const_reverse_iterator(std::addressof(_value(-1))); }
|
|
|
|
reverse_iterator rbegin() noexcept { return reverse_iterator(std::addressof(_value(x0_size - 1))); }
|
|
|
|
reverse_iterator rend() noexcept { return reverse_iterator(std::addressof(_value(-1))); }
|
|
|
|
const_reverse_iterator crbegin() const noexcept { return rbegin(); }
|
|
|
|
const_reverse_iterator crend() const noexcept { return rend(); }
|
|
|
|
|
2019-04-07 05:14:48 +00:00
|
|
|
T& operator[](size_t idx) {
|
|
|
|
#ifndef NDEBUG
|
2019-10-04 04:14:10 +00:00
|
|
|
if (idx >= x0_size) {
|
2019-07-20 04:27:21 +00:00
|
|
|
Log.report(logvisor::Fatal, fmt("out of bounds access on reserved_vector."));
|
2019-10-04 04:14:10 +00:00
|
|
|
}
|
2019-04-07 05:14:48 +00:00
|
|
|
#endif
|
|
|
|
return _value(idx);
|
|
|
|
}
|
|
|
|
const T& operator[](size_t idx) const {
|
|
|
|
#ifndef NDEBUG
|
2019-10-04 04:14:10 +00:00
|
|
|
if (idx >= x0_size) {
|
2019-07-20 04:27:21 +00:00
|
|
|
Log.report(logvisor::Fatal, fmt("out of bounds access on reserved_vector."));
|
2019-10-04 04:14:10 +00:00
|
|
|
}
|
2019-04-07 05:14:48 +00:00
|
|
|
#endif
|
|
|
|
return _value(idx);
|
|
|
|
}
|
2017-09-11 07:08:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2018-02-15 04:37:21 +00:00
|
|
|
* @brief Vector-style view backed by externally-allocated storage
|
2017-09-11 07:08:30 +00:00
|
|
|
*/
|
2018-02-15 04:37:21 +00:00
|
|
|
template <class T>
|
2018-12-08 05:30:43 +00:00
|
|
|
class prereserved_vector : public _reserved_vector_base<T> {
|
|
|
|
size_t x0_size;
|
|
|
|
T* x4_data;
|
|
|
|
T& _value(std::ptrdiff_t idx) { return x4_data[idx]; }
|
|
|
|
const T& _value(std::ptrdiff_t idx) const { return x4_data[idx]; }
|
2018-02-15 04:37:21 +00:00
|
|
|
|
2017-09-11 07:08:30 +00:00
|
|
|
public:
|
2018-12-08 05:30:43 +00:00
|
|
|
using base = _reserved_vector_base<T>;
|
|
|
|
using iterator = typename base::iterator;
|
|
|
|
using const_iterator = typename base::const_iterator;
|
|
|
|
using reverse_iterator = typename base::reverse_iterator;
|
|
|
|
using const_reverse_iterator = typename base::const_reverse_iterator;
|
|
|
|
prereserved_vector() : x0_size(0), x4_data(nullptr) {}
|
|
|
|
prereserved_vector(size_t size, T* data) : x0_size(size), x4_data(data) {}
|
|
|
|
|
|
|
|
void set_size(size_t n) { x0_size = n; }
|
|
|
|
void set_data(T* data) { x4_data = data; }
|
|
|
|
|
|
|
|
size_t size() const noexcept { return x0_size; }
|
|
|
|
bool empty() const noexcept { return x0_size == 0; }
|
|
|
|
const T* data() const noexcept { return x4_data; }
|
|
|
|
T* data() noexcept { return x4_data; }
|
|
|
|
|
|
|
|
T& back() { return _value(x0_size - 1); }
|
|
|
|
T& front() { return _value(0); }
|
|
|
|
const T& back() const { return _value(x0_size - 1); }
|
|
|
|
const T& front() const { return _value(0); }
|
|
|
|
|
|
|
|
const_iterator begin() const noexcept { return const_iterator(std::addressof(_value(0))); }
|
|
|
|
const_iterator end() const noexcept { return const_iterator(std::addressof(_value(x0_size))); }
|
|
|
|
iterator begin() noexcept { return iterator(std::addressof(_value(0))); }
|
|
|
|
iterator end() noexcept { return iterator(std::addressof(_value(x0_size))); }
|
|
|
|
const_iterator cbegin() const noexcept { return begin(); }
|
|
|
|
const_iterator cend() const noexcept { return end(); }
|
|
|
|
|
|
|
|
const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(std::addressof(_value(x0_size - 1))); }
|
|
|
|
const_reverse_iterator rend() const noexcept { return const_reverse_iterator(std::addressof(_value(-1))); }
|
|
|
|
reverse_iterator rbegin() noexcept { return reverse_iterator(std::addressof(_value(x0_size - 1))); }
|
|
|
|
reverse_iterator rend() noexcept { return reverse_iterator(std::addressof(_value(-1))); }
|
|
|
|
const_reverse_iterator crbegin() const noexcept { return rbegin(); }
|
|
|
|
const_reverse_iterator crend() const noexcept { return rend(); }
|
|
|
|
|
|
|
|
T& operator[](size_t idx) { return _value(idx); }
|
|
|
|
const T& operator[](size_t idx) const { return _value(idx); }
|
2017-01-07 01:58:05 +00:00
|
|
|
};
|
2017-07-11 07:17:03 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
template <class ForwardIt, class T>
|
|
|
|
ForwardIt binary_find(ForwardIt first, ForwardIt last, const T& value) {
|
|
|
|
first = std::lower_bound(first, last, value);
|
|
|
|
return (!(first == last) && !(value < *first)) ? first : last;
|
2017-07-11 07:17:03 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
template <class ForwardIt, class T, class GetKey>
|
|
|
|
ForwardIt binary_find(ForwardIt first, ForwardIt last, const T& value, GetKey getkey) {
|
|
|
|
auto comp = [&](const auto& left, const T& right) { return getkey(left) < right; };
|
|
|
|
first = std::lower_bound(first, last, value, comp);
|
|
|
|
return (!(first == last) && !(value < getkey(*first))) ? first : last;
|
2017-07-11 07:17:03 +00:00
|
|
|
}
|
2017-07-23 23:45:04 +00:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
template <typename _CharTp>
|
|
|
|
class basic_string
|
|
|
|
{
|
|
|
|
struct COWData
|
|
|
|
{
|
|
|
|
uint32_t x0_capacity;
|
|
|
|
uint32_t x4_refCount;
|
|
|
|
_CharTp x8_data[];
|
|
|
|
};
|
|
|
|
|
|
|
|
const _CharTp* x0_ptr;
|
|
|
|
COWData* x4_cow;
|
|
|
|
uint32_t x8_size;
|
|
|
|
|
|
|
|
void internal_allocate(int size)
|
|
|
|
{
|
|
|
|
x4_cow = reinterpret_cast<COWData*>(new uint8_t[size * sizeof(_CharTp) + 8]);
|
|
|
|
x0_ptr = x4_cow->x8_data;
|
|
|
|
x4_cow->x0_capacity = uint32_t(size);
|
|
|
|
x4_cow->x4_refCount = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const _CharTp _EmptyString;
|
|
|
|
|
|
|
|
public:
|
|
|
|
struct literal_t {};
|
|
|
|
|
|
|
|
basic_string(literal_t, const _CharTp* data)
|
|
|
|
{
|
|
|
|
x0_ptr = data;
|
|
|
|
x4_cow = nullptr;
|
|
|
|
|
|
|
|
const _CharTp* it = data;
|
|
|
|
while (*it)
|
|
|
|
++it;
|
|
|
|
|
|
|
|
x8_size = uint32_t((it - data) / sizeof(_CharTp));
|
|
|
|
}
|
|
|
|
|
|
|
|
basic_string(const basic_string& str)
|
|
|
|
{
|
|
|
|
x0_ptr = str.x0_ptr;
|
|
|
|
x4_cow = str.x4_cow;
|
|
|
|
x8_size = str.x8_size;
|
|
|
|
if (x4_cow)
|
|
|
|
++x4_cow->x4_refCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
basic_string(const _CharTp* data, int size)
|
|
|
|
{
|
|
|
|
if (size <= 0 && !data)
|
|
|
|
{
|
|
|
|
x0_ptr = &_EmptyString;
|
|
|
|
x4_cow = nullptr;
|
|
|
|
x8_size = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const _CharTp* it = data;
|
|
|
|
uint32_t len = 0;
|
|
|
|
while (*it)
|
|
|
|
{
|
|
|
|
if (size != -1 && len >= size)
|
|
|
|
break;
|
|
|
|
++it;
|
|
|
|
++len;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal_allocate(len + 1);
|
|
|
|
x8_size = len;
|
|
|
|
for (int i = 0; i < len; ++i)
|
|
|
|
x4_cow->x8_data[i] = data[i];
|
|
|
|
x4_cow->x8_data[len] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
~basic_string()
|
|
|
|
{
|
|
|
|
if (x4_cow && --x4_cow->x4_refCount == 0)
|
|
|
|
delete[] x4_cow;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
const char basic_string<char>::_EmptyString = 0;
|
|
|
|
template <>
|
|
|
|
const wchar_t basic_string<wchar_t>::_EmptyString = 0;
|
|
|
|
|
|
|
|
typedef basic_string<wchar_t> wstring;
|
|
|
|
typedef basic_string<char> string;
|
|
|
|
|
|
|
|
wstring wstring_l(const wchar_t* data)
|
|
|
|
{
|
|
|
|
return wstring(wstring::literal_t(), data);
|
|
|
|
}
|
|
|
|
|
|
|
|
string string_l(const char* data)
|
|
|
|
{
|
|
|
|
return string(string::literal_t(), data);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
} // namespace rstl
|