5#ifndef BITCOIN_PREVECTOR_H
6#define BITCOIN_PREVECTOR_H
36template <
unsigned int N,
typename T,
typename Size = uint32_t,
37 typename Diff = int32_t>
82 return (&(*a) - &(*b));
173 return (&(*a) - &(*b));
246 static_assert(
alignof(
char *) %
alignof(
size_type) == 0 &&
247 sizeof(
char *) %
alignof(
size_type) == 0,
248 "size_type cannot have more restrictive alignment "
249 "requirement than pointer");
250 static_assert(
alignof(
char *) %
alignof(T) == 0,
251 "value_type T cannot have more restrictive alignment "
252 "requirement than pointer");
258 return reinterpret_cast<const T *
>(
_union.
direct) + pos;
270 if (new_capacity <= N) {
275 memcpy(dst, src,
size() *
sizeof(T));
288 ((
size_t)
sizeof(T)) * new_capacity));
292 char *new_indirect =
static_cast<char *
>(
293 malloc(((
size_t)
sizeof(T)) * new_capacity));
296 T *dst =
reinterpret_cast<T *
>(new_indirect);
297 memcpy(dst, src,
size() *
sizeof(T));
312 void fill(T *dst, ptrdiff_t
count,
const T &value = T{}) {
313 std::fill_n(dst,
count, value);
316 template <
typename InputIterator>
317 void fill(T *dst, InputIterator first, InputIterator last) {
318 while (first != last) {
319 new (
static_cast<void *
>(dst)) T(*first);
335 template <
typename InputIterator>
336 void assign(InputIterator first, InputIterator last) {
356 template <
typename InputIterator>
372 :
_union(std::move(other._union)),
_size(other._size) {
377 if (&other ==
this) {
388 _union = std::move(other._union);
426 if (cur_size == new_size) {
429 if (cur_size > new_size) {
436 ptrdiff_t increase = new_size - cur_size;
458 memmove(ptr + 1, ptr, (
size() - p) *
sizeof(T));
460 new (
static_cast<void *
>(ptr)) T(value);
471 memmove(ptr +
count, ptr, (
size() - p) *
sizeof(T));
476 template <
typename InputIterator>
485 memmove(ptr +
count, ptr, (
size() - p) *
sizeof(T));
487 fill(ptr, first, last);
499 if (new_size <
size()) {
516 char *endp = (
char *)&(*
end());
517 if (!std::is_trivially_destructible<T>::value) {
526 memmove(&(*first), &(*last), endp - ((
char *)(&(*last))));
552 std::swap(
_union, other._union);
553 std::swap(
_size, other._size);
557 if (!std::is_trivially_destructible<T>::value) {
574 if ((*b1) != (*b2)) {
584 return !(*
this == other);
bool operator==(const_iterator x) const
const_iterator & operator++()
const_iterator & operator+=(size_type n)
bool operator<(const_iterator x) const
const_iterator & operator-=(size_type n)
std::random_access_iterator_tag iterator_category
const_iterator operator--(int)
bool operator<=(const_iterator x) const
const_iterator operator++(int)
const_iterator(const T *ptr_)
const T * operator->() const
difference_type friend operator-(const_iterator a, const_iterator b)
bool operator!=(const_iterator x) const
const_iterator operator+(size_type n)
bool operator>=(const_iterator x) const
const_iterator & operator--()
const T & operator*() const
const_iterator operator-(size_type n)
const T & operator[](size_type pos) const
bool operator>(const_iterator x) const
const_iterator(iterator x)
const_reverse_iterator(const T *ptr_)
const_reverse_iterator & operator--()
const_reverse_iterator operator--(int)
const_reverse_iterator operator++(int)
std::bidirectional_iterator_tag iterator_category
const_reverse_iterator(reverse_iterator x)
const T * operator->() const
bool operator!=(const_reverse_iterator x) const
const_reverse_iterator & operator++()
const T & operator*() const
bool operator==(const_reverse_iterator x) const
bool operator<(iterator x) const
bool operator==(iterator x) const
difference_type friend operator-(iterator a, iterator b)
bool operator!=(iterator x) const
iterator operator-(size_type n)
bool operator<=(iterator x) const
bool operator>=(iterator x) const
T & operator[](size_type pos)
iterator & operator+=(size_type n)
iterator & operator-=(size_type n)
bool operator>(iterator x) const
const T & operator[](size_type pos) const
std::random_access_iterator_tag iterator_category
iterator operator+(size_type n)
std::bidirectional_iterator_tag iterator_category
reverse_iterator operator++(int)
reverse_iterator & operator--()
const T & operator*() const
bool operator!=(reverse_iterator x) const
reverse_iterator(T *ptr_)
const T * operator->() const
bool operator==(reverse_iterator x) const
reverse_iterator operator--(int)
reverse_iterator & operator++()
Implements a drop-in replacement for std::vector<T> which stores up to N elements directly (without h...
void change_capacity(size_type new_capacity)
void fill(T *dst, ptrdiff_t count, const T &value=T{})
iterator erase(iterator first, iterator last)
prevector(InputIterator first, InputIterator last)
void swap(prevector< N, T, Size, Diff > &other) noexcept
prevector & operator=(prevector< N, T, Size, Diff > &&other) noexcept
const value_type & const_reference
void emplace_back(Args &&...args)
bool operator==(const prevector< N, T, Size, Diff > &other) const
iterator erase(iterator pos)
prevector(size_type n, const T &val)
void fill(T *dst, InputIterator first, InputIterator last)
const T * indirect_ptr(difference_type pos) const
T * direct_ptr(difference_type pos)
const_iterator end() const
direct_or_indirect _union
void assign(InputIterator first, InputIterator last)
void insert(iterator pos, InputIterator first, InputIterator last)
const T * item_ptr(difference_type pos) const
bool operator<(const prevector< N, T, Size, Diff > &other) const
const value_type * data() const
bool operator!=(const prevector< N, T, Size, Diff > &other) const
void reserve(size_type new_capacity)
prevector(const prevector< N, T, Size, Diff > &other)
const T & operator[](size_type pos) const
const T * direct_ptr(difference_type pos) const
void resize_uninitialized(size_type new_size)
const_reverse_iterator rbegin() const
T * item_ptr(difference_type pos)
T * indirect_ptr(difference_type pos)
prevector & operator=(const prevector< N, T, Size, Diff > &other)
void resize(size_type new_size)
size_t allocated_memory() const
iterator insert(iterator pos, const T &value)
reverse_iterator rbegin()
const_reverse_iterator rend() const
prevector(prevector< N, T, Size, Diff > &&other) noexcept
const value_type * const_pointer
void assign(size_type n, const T &val)
void insert(iterator pos, size_type count, const T &value)
void push_back(const T &value)
const_iterator begin() const
T & operator[](size_type pos)
struct prevector::direct_or_indirect::@2 indirect_contents
char direct[sizeof(T) *N]