23#ifndef GUL17_SMALLVECTOR_H_
24#define GUL17_SMALLVECTOR_H_
28#include <initializer_list>
268template <
typename ElementT,
size_t in_capacity>
327 static_assert(std::is_default_constructible<ValueType>::value,
328 "SmallVector: Element type is not default-constructible");
365 typename = std::enable_if_t<not std::is_integral<InputIterator>::value>>
368 fill_empty_vector_with_copied_range(first, last);
380 noexcept(std::is_nothrow_copy_constructible<ValueType>::value)
382 static_assert(std::is_copy_constructible<ValueType>::value ==
true,
383 "SmallVector: Element type is not copy-constructible");
384 fill_empty_vector_with_copied_range(
other.cbegin(),
other.cend());
403 noexcept(std::is_nothrow_move_constructible<ValueType>::value)
405 move_or_copy_all_elements_from(std::move(
other));
417 fill_empty_vector_with_copied_range(
init.begin(),
init.end());
425 if (is_storage_allocated())
462 typename = std::enable_if_t<not std::is_integral<InputIterator>::value>>
466 fill_empty_vector_with_copied_range(first, last);
482 std::uninitialized_copy(
init.begin(),
init.end(),
data());
493 throw std::out_of_range(
cat(
"Index out of range: ",
idx,
" >= ", size_));
501 throw std::out_of_range(
cat(
"Index out of range: ",
idx,
" >= ", size_));
511 return *(data_end() - 1);
520 return *(data_end() - 1);
571 destroy_range(
data(), data_end());
583 return std::make_reverse_iterator(
end());
594 return std::make_reverse_iterator(
begin());
603 return reinterpret_cast<ValueType*
>(data_ptr_);
612 return reinterpret_cast<const ValueType*
>(data_ptr_);
646 return data_end() - 1;
651 return insert_single_value(
pos, std::move(
v));
674 if (size_ == capacity_)
677 ::new(
static_cast<void*
>(data_end()))
784 return insert_single_value(
pos,
value);
798 return insert_single_value(
pos, std::move(
value));
850 typename = std::enable_if_t<not std::is_integral<InputIterator>::value>>
865 copy_range_into_uninitialized_cells(size_, first +
num_assignable, last);
898 return std::numeric_limits<SizeType>::max();
909 noexcept(std::is_nothrow_copy_assignable<ValueType>::value)
916 size_ =
other.size();
936 noexcept(std::is_nothrow_move_constructible<ValueType>::value)
941 if (is_storage_allocated())
943 move_or_copy_all_elements_from(std::move(
other));
968 return std::equal(
lhs.cbegin(),
lhs.cend(),
rhs.cbegin(),
rhs.cend());
1002 auto p = data_end();
1017 if (size_ == capacity_)
1035 if (size_ == capacity_)
1051 return std::make_reverse_iterator(
end());
1061 return std::make_reverse_iterator(
begin());
1079 const auto d_end = data_end();
1084 if (is_storage_allocated())
1108 static_assert(std::is_default_constructible<ValueType>::value,
1109 "SmallVector: For using resize(), element type must be default-constructible");
1118 fill_uninitialized_cells_with_default_constructed_elements(size_,
1164 auto new_memory = std::unique_ptr<AlignedStorage[]>{};
1173 const auto d_end = data_end();
1178 if (is_storage_allocated())
1197 if (is_storage_allocated())
1199 if (
other.is_storage_allocated())
1200 swap_heap_with_heap(*
this,
other);
1202 swap_heap_with_internal(*
this,
other);
1206 if (
other.is_storage_allocated())
1207 swap_heap_with_internal(
other, *
this);
1209 swap_internal_with_internal(*
this,
other);
1214 using AlignedStorage =
1221 std::array<AlignedStorage, in_capacity> internal_array_;
1224 AlignedStorage* data_ptr_{ internal_array_.data() };
1244 template<
class InputIterator>
1252 for (
auto it = first;
it != last; ++
it)
1285 return reinterpret_cast<ValueType*
>(data_ptr_) + size_;
1291 return reinterpret_cast<const ValueType*
>(data_ptr_) + size_;
1322 template<
typename InputIterator>
1328 copy_range_into_uninitialized_cells(0
u, first, last);
1355 void fill_uninitialized_cells_with_default_constructed_elements(
SizeType pos,
1385 const auto remaining_space = std::numeric_limits<SizeType>::max() - capacity_;
1388 throw std::length_error(
"Max. capacity reached");
1400 template <
typename T>
1406 return begin() + size_ - 1;
1411 if (size_ == capacity_)
1432 return data_ptr_ != internal_array_.data();
1492 noexcept(std::is_nothrow_move_constructible<ValueType>::value)
1495 if (
other.is_storage_allocated())
1497 data_ptr_ =
other.data_ptr_;
other.data_ptr_ =
other.internal_array_.data();
1498 capacity_ =
other.capacity_;
other.capacity_ =
other.inner_capacity();
1503 data_ptr_ = internal_array_.data();
1506 size_ =
other.size();
1518 std::swap(
a.data_ptr_,
b.data_ptr_);
1519 std::swap(
a.capacity_,
b.capacity_);
1520 std::swap(
a.size_,
b.size_);
1531 uninitialized_move_or_copy(
b.begin(),
b.end(),
1532 reinterpret_cast<ValueType*
>(
a.internal_array_.data()));
1533 destroy_range(
b.begin(),
b.end());
1535 b.data_ptr_ =
a.data_ptr_;
1536 a.data_ptr_ =
a.internal_array_.data();
1538 std::swap(
a.capacity_,
b.capacity_);
1539 std::swap(
a.size_,
b.size_);
1549 if (
a.size_ <=
b.size_)
1552 std::swap(
a.data()[
i],
b.data()[
i]);
1554 uninitialized_move_or_copy(
b.begin() +
a.size_,
b.end(),
a.begin() +
a.size_);
1555 destroy_range(
b.begin() +
a.size_,
b.end());
1560 std::swap(
a.data()[
i],
b.data()[
i]);
1562 uninitialized_move_or_copy(
a.begin() +
b.size_,
a.end(),
b.begin() +
b.size_);
1563 destroy_range(
a.begin() +
b.size_,
a.end());
1566 std::swap(
a.size_,
b.size_);
1573 template <
typename T>
1574 static typename std::enable_if_t<not std::is_nothrow_move_constructible<T>::value>
1602 template <
typename T>
1603 static typename std::enable_if_t<std::is_nothrow_move_constructible<T>::value>
1619 template <
typename T>
1620 static typename std::enable_if_t<std::is_nothrow_move_constructible<T>::value>
1628 template <
typename T>
1629 static typename std::enable_if_t<not std::is_nothrow_move_constructible<T>::value
and
1630 std::is_copy_constructible<T>::value>
1638 template <
typename T>
1639 static typename std::enable_if_t<not std::is_nothrow_move_constructible<T>::value
1640 and not std::is_copy_constructible<T>::value>
1655template<
typename ElementT,
size_t in_capacity>
Declaration of the overload set for cat() and of the associated class ConvertingStringView.
A resizable container with contiguous storage that can hold a specified number of elements without al...
Definition SmallVector.h:270
constexpr Iterator begin() noexcept
Return an iterator to the first element of the vector.
Definition SmallVector.h:527
void assign(SizeType num_elements, const ValueType &value)
Fill the vector with a certain number of copies of the given value after clearing all previous conten...
Definition SmallVector.h:436
Iterator emplace(ConstIterator pos, ArgumentTypes &&... arguments)
Construct an additional element at an arbitrary position in the vector.
Definition SmallVector.h:641
SmallVector & operator=(const SmallVector &other) noexcept(std::is_nothrow_copy_assignable< ValueType >::value)
Copy assignment operator: Copy all elements from another SmallVector after clearing all previous cont...
Definition SmallVector.h:908
std::reverse_iterator< ConstIterator > ConstReverseIterator
Iterator to a const element in reversed container.
Definition SmallVector.h:305
SmallVector(std::initializer_list< ValueType > init)
Construct a SmallVector that is filled with copies of the elements from a given initializer list.
Definition SmallVector.h:415
constexpr Reference operator[](SizeType idx)
Return a reference to the element at the specified index.
Definition SmallVector.h:984
constexpr ConstIterator cbegin() const noexcept
Return a const iterator to the first element of the vector.
Definition SmallVector.h:551
friend bool operator!=(const SmallVector &lhs, const SmallVector &rhs)
Inequality operator: Return true if both vectors have a different size() or at least one different el...
Definition SmallVector.h:978
uint32_t SizeType
Unsigned integer type for indexing, number of elements, capacity.
Definition SmallVector.h:277
void clear() noexcept
Erase all elements from the container without changing its capacity.
Definition SmallVector.h:569
SmallVector(SizeType num_elements, const ValueType &value)
Construct a SmallVector that is filled with a certain number of copies of the given value.
Definition SmallVector.h:343
ReverseIterator rbegin() noexcept
Return a reverse iterator to the first element of the reversed vector (which is the last element of t...
Definition SmallVector.h:1049
constexpr Reference at(SizeType idx)
Return a reference to the element at the specified index with bounds-checking.
Definition SmallVector.h:490
Reference reference
Reference to an element.
Definition SmallVector.h:287
constexpr ConstReference operator[](SizeType idx) const
Return a const reference to the element at the specified index.
Definition SmallVector.h:990
constexpr Reference back() noexcept
Return a reference to the last element in the vector.
Definition SmallVector.h:509
ValueType value_type
Type of the elements in the underlying container.
Definition SmallVector.h:275
Iterator insert(ConstIterator pos, SizeType num_elements, const ValueType &value)
Insert a number of copies of the given value before the indicated position.
Definition SmallVector.h:810
ConstReverseIterator crbegin() noexcept
Return a const reverse iterator to the first element of the reversed vector (which is the last elemen...
Definition SmallVector.h:581
friend bool operator==(const SmallVector &lhs, const SmallVector &rhs)
Equality operator: Return true if both vectors have the same size() and the same elements.
Definition SmallVector.h:966
void assign(InputIterator first, InputIterator last)
Fill the vector with copies of elements from the given range.
Definition SmallVector.h:463
ValueType * Iterator
Iterator to an element.
Definition SmallVector.h:293
void assign(std::initializer_list< ValueType > init)
Assign the elements of an initializer list to this vector after clearing all previous contents.
Definition SmallVector.h:476
Iterator insert(ConstIterator pos, std::initializer_list< ValueType > init)
Insert elements from an initializer list before the indicated position.
Definition SmallVector.h:885
void shrink_to_fit()
Reduce the capacity as far as possible while retaining all stored elements.
Definition SmallVector.h:1155
ElementT ValueType
Type of the elements in the underlying container.
Definition SmallVector.h:273
constexpr ConstIterator cend() const noexcept
Return a const iterator pointing past the last element of the vector.
Definition SmallVector.h:560
const ValueType & ConstReference
Reference to a const element.
Definition SmallVector.h:289
Iterator iterator
Iterator to an element.
Definition SmallVector.h:295
SmallVector(SmallVector &&other) noexcept(std::is_nothrow_move_constructible< ValueType >::value)
Move constructor: Create a SmallVector from the contents of another one with the same inner capacity ...
Definition SmallVector.h:402
~SmallVector()
Destructor: Destroys all stored elements and frees all allocated memory.
Definition SmallVector.h:421
void swap(SmallVector &other)
Exchange the contents of this SmallVector with those of another one.
Definition SmallVector.h:1195
void reserve(SizeType new_capacity)
Increase the capacity of the vector to the specified size.
Definition SmallVector.h:1072
std::ptrdiff_t DifferenceType
Signed integer type for the difference of two iterators.
Definition SmallVector.h:281
constexpr ConstReference front() const noexcept
Return a const reference to the first element in the vector.
Definition SmallVector.h:762
constexpr ConstIterator begin() const noexcept
Return a const iterator to the first element of the vector.
Definition SmallVector.h:536
SmallVector() noexcept=default
Construct an empty SmallVector.
ConstReference const_reference
Reference to a const element.
Definition SmallVector.h:291
Iterator erase(ConstIterator pos)
Erase a single element from the vector, moving elements behind it forward.
Definition SmallVector.h:717
constexpr ValueType * data() noexcept
Return a pointer to the contiguous data storage of the vector.
Definition SmallVector.h:601
SmallVector & operator=(std::initializer_list< ValueType > init)
Assign the elements of an initializer list to this vector after clearing all previous contents.
Definition SmallVector.h:953
SmallVector(InputIterator first, InputIterator last)
Construct a SmallVector that is filled with copies of elements from the given range.
Definition SmallVector.h:366
ReverseIterator rend() noexcept
Return a reverse iterator pointing past the last element of the reversed vector.
Definition SmallVector.h:1059
ValueType & Reference
Reference to an element.
Definition SmallVector.h:285
SizeType size_type
Unsigned integer type for indexing, number of elements, capacity.
Definition SmallVector.h:279
constexpr ConstReference back() const noexcept
Return a const reference to the last element in the vector.
Definition SmallVector.h:518
constexpr Reference front() noexcept
Return a reference to the first element in the vector.
Definition SmallVector.h:753
constexpr const ValueType * data() const noexcept
Return a pointer to the contiguous data storage of the vector.
Definition SmallVector.h:610
Iterator insert(ConstIterator pos, ValueType &&value)
Insert a single element before the indicated position.
Definition SmallVector.h:796
SmallVector(const SmallVector &other) noexcept(std::is_nothrow_copy_constructible< ValueType >::value)
Create a copy of another SmallVector with the same inner capacity.
Definition SmallVector.h:379
constexpr bool empty() const noexcept
Determine if the vector is empty.
Definition SmallVector.h:686
Iterator insert(ConstIterator pos, const ValueType &value)
Insert a single element before the indicated position.
Definition SmallVector.h:782
void resize(SizeType num_elements)
Change the number of elements in the container.
Definition SmallVector.h:1106
constexpr SizeType capacity() const noexcept
Return the number of elements that can currently be stored in this vector without having to allocate ...
Definition SmallVector.h:545
SmallVector & operator=(SmallVector &&other) noexcept(std::is_nothrow_move_constructible< ValueType >::value)
Move assignment operator: Assign all of the elements from another vector to this one using move seman...
Definition SmallVector.h:935
constexpr ConstIterator end() const noexcept
Return a const iterator pointing past the last element of the vector.
Definition SmallVector.h:701
constexpr SizeType inner_capacity() const noexcept
Return the number of elements this SmallVector can hold internally without having to allocate storage...
Definition SmallVector.h:771
Reference emplace_back(ArgumentTypes &&... arguments)
Construct an additional element at the end of the buffer.
Definition SmallVector.h:672
void push_back(ValueType &&value)
Move one element to the end of the buffer.
Definition SmallVector.h:1033
Iterator erase(ConstIterator first, ConstIterator last)
Erase a range of elements from the vector, moving elements behind the range forward.
Definition SmallVector.h:735
Iterator insert(ConstIterator pos, InputIterator first, InputIterator last)
Insert a range of values before the indicated position.
Definition SmallVector.h:851
constexpr SizeType max_size() const noexcept
Return the maximum number of elements that this vector can theoretically hold.
Definition SmallVector.h:896
ConstReverseIterator const_reverse_iterator
Iterator to a const element in reversed container.
Definition SmallVector.h:307
ConstIterator const_iterator
Iterator to a const element.
Definition SmallVector.h:299
constexpr ConstReference at(SizeType idx) const
Return a const reference to the element at the specified index.
Definition SmallVector.h:498
void push_back(const ValueType &value)
Copy one element to the end of the buffer.
Definition SmallVector.h:1015
void resize(SizeType num_elements, const ValueType &element)
Change the number of elements in the container.
Definition SmallVector.h:1136
constexpr SizeType size() const noexcept
Return the number of elements that are currently stored.
Definition SmallVector.h:1186
void pop_back()
Remove the last element from the vector.
Definition SmallVector.h:999
DifferenceType difference_type
Signed integer type for the difference of two iterators.
Definition SmallVector.h:283
ReverseIterator reverse_iterator
Iterator to an element in reversed container.
Definition SmallVector.h:303
std::reverse_iterator< Iterator > ReverseIterator
Iterator to an element in reversed container.
Definition SmallVector.h:301
ConstReverseIterator crend() noexcept
Return a const reverse iterator pointing past the last element of the reversed vector.
Definition SmallVector.h:592
const ValueType * ConstIterator
Iterator to a const element.
Definition SmallVector.h:297
constexpr Iterator end() noexcept
Return an iterator pointing past the last element of the vector.
Definition SmallVector.h:692
void swap(SmallVector< ElementT, in_capacity > &a, SmallVector< ElementT, in_capacity > &b)
Exchange the contents of one SmallVector with those of another one.
Definition SmallVector.h:1656
auto constexpr bit_set(unsigned bit) noexcept -> ReturnT
Set a bit in an integral type.
Definition bit_manip.h:121
std::string cat()
Efficiently concatenate an arbitrary number of strings and numbers.
Definition cat.h:97
Definition of macros used internally by GUL.
Namespace gul17 contains all functions and classes of the General Utility Library.
Definition doxygen.h:26