59#ifndef TPL_SMALL_VECTOR_H
60#define TPL_SMALL_VECTOR_H
63#include <initializer_list>
117template <
typename T,
size_t N = 8>
120 static_assert(
N > 0,
"SmallVector requires a positive inline capacity");
121 static_assert(std::is_move_constructible_v<T>,
122 "SmallVector requires a move-constructible element type");
138 return reinterpret_cast<const T *
>(
storage_);
143 return std::allocator<T>().allocate(
m);
148 std::allocator<T>().deallocate(p,
m);
160 ::new (
static_cast<void *
>(np + i))
T(std::move_if_noexcept(
ptr_[i]));
164 std::destroy(np, np + i);
176 void grow(
const size_t min_cap)
178 size_t new_cap =
cap_;
179 while (new_cap < min_cap)
182 <<
"SmallVector::grow(): capacity overflow";
193 if constexpr (std::is_nothrow_move_constructible_v<T>)
195 for (;
n_ < o.n_; ++
n_)
196 ::new (
static_cast<void *
>(
ptr_ +
n_))
T(std::move(o.ptr_[
n_]));
202 for (;
n_ < o.n_; ++
n_)
203 ::new (
static_cast<void *
>(
ptr_ +
n_))
T(std::move(o.ptr_[
n_]));
211 std::destroy(o.ptr_, o.ptr_ + o.n_);
219 o.ptr_ = o.inline_ptr();
253 requires std::is_copy_constructible_v<T>
275 requires std::is_copy_constructible_v<T>
284 ::new (
static_cast<void *
>(
ptr_ +
n_))
T(x);
301 template <std::input_iterator It>
306 for (; first != last; ++first)
318 requires std::is_copy_constructible_v<T>
325 for (;
n_ < v.n_; ++
n_)
326 ::new (
static_cast<void *
>(
ptr_ +
n_))
T(v.ptr_[
n_]);
348 requires std::is_copy_constructible_v<T>
355 for (;
n_ < v.n_; ++
n_)
356 ::new (
static_cast<void *
>(
ptr_ +
n_))
T(v.ptr_[
n_]);
386 std::swap(
ptr_, v.ptr_);
388 std::swap(
cap_, v.cap_);
392 *
this = std::move(v);
399 [[nodiscard]]
size_t size() const noexcept
521 [[nodiscard]]
const T *
data() const noexcept
536 template <
class... Args>
541 T tmp(std::forward<Args>(args)...);
543 ::new (
static_cast<void *
>(
ptr_ +
n_))
T(std::move(tmp));
546 ::new (
static_cast<void *
>(
ptr_ +
n_))
T(std::forward<Args>(args)...);
557 requires std::is_copy_constructible_v<T>
615 requires std::is_copy_constructible_v<T>
620 <<
"SmallVector::append_range(): null source pointer";
627 <<
"SmallVector::append_range(): count overflows size_t when added "
628 "to the current size";
631 if constexpr (std::is_trivially_copyable_v<T>)
643 std::uninitialized_copy_n(first,
count,
ptr_ +
n_);
651 for (; i <
count; ++i)
652 ::new (
static_cast<void *
>(
ptr_ +
n_ + i))
T(first[i]);
665 requires std::is_copy_constructible_v<T>
673 return append(std::move(item));
684 T ret = std::move(
ptr_[
n_ - 1]);
685 std::destroy_at(
ptr_ + --
n_);
696 std::destroy_at(
ptr_ + --
n_);
708 requires std::is_move_assignable_v<T>
715 ::new (
static_cast<void *
>(
ptr_ +
n_))
T(std::move(item));
718 ::new (
static_cast<void *
>(
ptr_ +
n_))
T(std::move(
ptr_[
n_ - 1]));
719 for (
size_t i =
n_ - 1; i > pos; --i)
721 ptr_[pos] = std::move(item);
732 requires std::is_move_assignable_v<T>
735 for (
size_t i = pos; i + 1 <
n_; ++i)
737 std::destroy_at(
ptr_ + --
n_);
787 template <
class Operation>
791 for (
size_t i = 0; i <
n_; ++i)
792 if (not operation(
ptr_[i]))
802 template <
class Operation>
806 for (
size_t i = 0; i <
n_; ++i)
807 if (not operation(std::as_const(
ptr_[i])))
824 for (
size_t i = 0; i <
n_; ++i)
825 if (not (
ptr_[i] == v(i)))
834 return not (*
this == v);
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
Exception handling system with formatted messages for Aleph-w.
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
#define ah_underflow_error_if(C)
Throws std::underflow_error if condition holds.
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
size_t size_t int32_t value
size_t size() const noexcept
Return the number of items of the list, in constant time.
Number Theoretic Transform over Z / MOD Z.
Contiguous dynamic array with N elements of inline storage.
const_iterator end() const noexcept
Const iterator past the last element. O(1).
void relocate(const size_t new_cap)
static void deallocate(T *p, size_t m) noexcept
T & push_back(const T &item)
STL-style alias of append(const T &).
size_t size_type
STL convention: size type.
size_t capacity() const noexcept
Return the current capacity (inline or heap). O(1).
size_t size() const noexcept
Return the number of stored elements. O(1).
bool traverse(Operation operation)
Traverse elements in order while operation returns true.
void reserve(const size_t cap)
Reserve capacity for at least cap elements.
const T * const_iterator
Random-access const iterator.
SmallVector(It first, It last)
Construct from an iterator range.
SmallVector(const SmallVector &v)
Copy constructor (requires copyable T).
T & emplace_back(Args &&...args)
Construct an element in place at the end.
const_iterator cend() const noexcept
Const iterator past the last element. O(1).
T & get_last()
Last element (checked).
const_iterator begin() const noexcept
Const iterator to the first element. O(1).
void append_range(const T *first, const size_t count)
Append count copies from [first, first + count), in order.
T & append(const T &item)
Append a copy of item.
SmallVector() noexcept
Construct an empty vector using the inline storage. Never allocates.
void empty() noexcept
Destroy all elements (Aleph convention).
iterator end() noexcept
Iterator past the last element. O(1).
T * data() noexcept
Pointer to the contiguous element storage. O(1).
void steal(SmallVector &&o) noexcept(std::is_nothrow_move_constructible_v< T >)
SmallVector(const size_t n, const T &value)
Construct with n copies of value.
static T * allocate(size_t m)
T & get_first()
First element (checked).
T & operator()(size_t i) noexcept
Unchecked access to the i-th element (must be < size()).
void swap(SmallVector &v) noexcept(std::is_nothrow_move_constructible_v< T >)
Swap contents with v.
const T & get_first() const
See get_first().
bool is_small() const noexcept
Return true while the elements still live in the inline buffer. O(1).
T & append(T &&item)
Append item by moving.
T * inline_ptr() noexcept
T remove_last()
Remove and return the last element.
T value_type
STL convention: value type.
bool operator==(const SmallVector< T, M > &v) const
Equality: same size and pairwise equal elements.
std::byte storage_[N *sizeof(T)]
SmallVector & operator=(const SmallVector &v)
Copy assignment (requires copyable T).
bool traverse(Operation operation) const
Const traversal in order while operation returns true.
T & insert(size_t pos, T item)
Insert an element at position pos, shifting the tail right.
void pop_back()
Remove the last element (STL style).
const T * data() const noexcept
See data().
SmallVector(SmallVector &&v) noexcept(std::is_nothrow_move_constructible_v< T >)
Move constructor.
iterator begin() noexcept
Iterator to the first element. O(1).
T & operator[](size_t i)
Checked access to the i-th element.
bool operator!=(const SmallVector< T, M > &v) const
Inequality: negation of operator==.
const T * inline_ptr() const noexcept
void erase(const size_t pos)
Remove the element at position pos, shifting the tail left.
bool is_empty() const noexcept
Return true if no elements are stored. O(1).
SmallVector(std::initializer_list< T > l)
Construct from an initializer list.
T * iterator
Random-access iterator.
const_iterator cbegin() const noexcept
Const iterator to the first element. O(1).
T & push_back(T &&item)
STL-style alias of append(T &&).
void grow(const size_t min_cap)
void clear() noexcept
Destroy all elements. Alias of empty(). Capacity is kept.
T Item_Type
Aleph convention: element type.
const T & get_last() const
See get_last().
f(args...) is a valid call whose result is usable as a condition.
Main namespace for Aleph-w library functions.
std::decay_t< typename HeadC::Item_Type > T
and
Check uniqueness with explicit hash + equality functors.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)