26#ifndef GUL17_SMALLVECTOR_H_
27#define GUL17_SMALLVECTOR_H_
33#include <initializer_list>
276template <
typename ElementT,
size_t in_capacity>
335 static_assert(std::is_default_constructible<ValueType>::value,
336 "SmallVector: Element type is not default-constructible");
373 typename = std::enable_if_t<not std::is_integral<InputIterator>::value>>
376 fill_empty_vector_with_copied_range(first, last);
388 noexcept(std::is_nothrow_copy_constructible<ValueType>::value)
390 static_assert(std::is_copy_constructible<ValueType>::value ==
true,
391 "SmallVector: Element type is not copy-constructible");
392 fill_empty_vector_with_copied_range(
other.cbegin(),
other.cend());
411 noexcept(std::is_nothrow_move_constructible<ValueType>::value)
413 move_or_copy_all_elements_from(std::move(
other));
425 fill_empty_vector_with_copied_range(
init.begin(),
init.end());
432 if (is_storage_allocated())
433 deallocate_space_for_elements(data_ptr_);
469 typename = std::enable_if_t<not std::is_integral<InputIterator>::value>>
473 fill_empty_vector_with_copied_range(first, last);
489 std::uninitialized_copy(
init.begin(),
init.end(),
data());
500 throw std::out_of_range(
cat(
"Index out of range: ",
idx,
" >= ", size_));
508 throw std::out_of_range(
cat(
"Index out of range: ",
idx,
" >= ", size_));
518 return *(data_end() - 1);
527 return *(data_end() - 1);
578 destroy_range(
data(), data_end());
590 return std::make_reverse_iterator(
end());
601 return std::make_reverse_iterator(
begin());
653 return data_end() - 1;
658 return insert_single_value(
pos, std::move(
v));
681 if (size_ == capacity_)
684 ::new(
static_cast<void*
>(data_end()))
791 return insert_single_value(
pos,
value);
805 return insert_single_value(
pos, std::move(
value));
857 typename = std::enable_if_t<not std::is_integral<InputIterator>::value>>
872 copy_range_into_uninitialized_cells(size_, first +
num_assignable, last);
905 return std::numeric_limits<SizeType>::max();
916 noexcept(std::is_nothrow_copy_assignable<ValueType>::value)
923 size_ =
other.size();
943 noexcept(std::is_nothrow_move_constructible<ValueType>::value)
948 if (is_storage_allocated())
949 deallocate_space_for_elements(data_ptr_);
950 move_or_copy_all_elements_from(std::move(
other));
975 return std::equal(
lhs.cbegin(),
lhs.cend(),
rhs.cbegin(),
rhs.cend());
1009 auto p = data_end();
1024 if (size_ == capacity_)
1042 if (size_ == capacity_)
1058 return std::make_reverse_iterator(
end());
1068 return std::make_reverse_iterator(
begin());
1088 auto*
d_end = data_end();
1092 if (is_storage_allocated())
1093 deallocate_space_for_elements(data_ptr_);
1117 static_assert(std::is_default_constructible<ValueType>::value,
1118 "SmallVector: For using resize(), element type must be default-constructible");
1127 fill_uninitialized_cells_with_default_constructed_elements(size_,
1178 new_data = get_internal_array_pointer();
1186 auto*
d_end = data_end();
1190 if (is_storage_allocated())
1191 deallocate_space_for_elements(data_ptr_);
1210 if (is_storage_allocated())
1212 if (
other.is_storage_allocated())
1213 swap_heap_with_heap(*
this,
other);
1215 swap_heap_with_internal(*
this,
other);
1219 if (
other.is_storage_allocated())
1220 swap_heap_with_internal(
other, *
this);
1222 swap_internal_with_internal(*
this,
other);
1235 ValueType* data_ptr_{ get_internal_array_pointer() };
1251 std::align_val_t{
alignof(
ValueType) }));
1266 template<
class InputIterator>
1274 for (
auto it = first;
it != last; ++
it)
1307 return data_ptr_ + size_;
1313 return data_ptr_ + size_;
1317 static void deallocate_space_for_elements(
ValueType*
ptr)
noexcept
1350 template<
typename InputIterator>
1356 copy_range_into_uninitialized_cells(0
u, first, last);
1383 void fill_uninitialized_cells_with_default_constructed_elements(
SizeType pos,
1405 return reinterpret_cast<const ValueType*
>(internal_array_.data());
1411 return reinterpret_cast<ValueType*
>(internal_array_.data());
1425 const auto remaining_space = std::numeric_limits<SizeType>::max() - capacity_;
1428 throw std::length_error(
"Max. capacity reached");
1440 template <
typename T>
1446 return begin() + size_ - 1;
1451 if (size_ == capacity_)
1472 return data_ptr_ != get_internal_array_pointer();
1534 noexcept(std::is_nothrow_move_constructible<ValueType>::value)
1537 if (
other.is_storage_allocated())
1539 data_ptr_ = std::exchange(
other.data_ptr_,
other.get_internal_array_pointer());
1540 capacity_ = std::exchange(
other.capacity_,
other.inner_capacity());
1541 size_ = std::exchange(
other.size_, 0
u);
1545 data_ptr_ = get_internal_array_pointer();
1548 size_ =
other.size();
1560 std::swap(
a.data_ptr_,
b.data_ptr_);
1561 std::swap(
a.capacity_,
b.capacity_);
1562 std::swap(
a.size_,
b.size_);
1573 uninitialized_move_or_copy(
b.begin(),
b.end(),
a.get_internal_array_pointer());
1574 destroy_range(
b.begin(),
b.end());
1576 b.data_ptr_ = std::exchange(
a.data_ptr_,
a.get_internal_array_pointer());
1578 std::swap(
a.capacity_,
b.capacity_);
1579 std::swap(
a.size_,
b.size_);
1589 if (
a.size_ <=
b.size_)
1592 std::swap(
a.data()[
i],
b.data()[
i]);
1594 uninitialized_move_or_copy(
b.begin() +
a.size_,
b.end(),
a.begin() +
a.size_);
1595 destroy_range(
b.begin() +
a.size_,
b.end());
1600 std::swap(
a.data()[
i],
b.data()[
i]);
1602 uninitialized_move_or_copy(
a.begin() +
b.size_,
a.end(),
b.begin() +
b.size_);
1603 destroy_range(
a.begin() +
b.size_,
a.end());
1606 std::swap(
a.size_,
b.size_);
1613 template <
typename T>
1614 static typename std::enable_if_t<not std::is_nothrow_move_constructible<T>::value>
1642 template <
typename T>
1643 static typename std::enable_if_t<std::is_nothrow_move_constructible<T>::value>
1659 template <
typename T>
1660 static typename std::enable_if_t<std::is_nothrow_move_constructible<T>::value>
1668 template <
typename T>
1669 static typename std::enable_if_t<not std::is_nothrow_move_constructible<T>::value
and
1670 std::is_copy_constructible<T>::value>
1678 template <
typename T>
1679 static typename std::enable_if_t<not std::is_nothrow_move_constructible<T>::value
1680 and not std::is_copy_constructible<T>::value>
1695template<
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:278
constexpr Iterator begin() noexcept
Return an iterator to the first element of the vector.
Definition SmallVector.h:534
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:443
Iterator emplace(ConstIterator pos, ArgumentTypes &&... arguments)
Construct an additional element at an arbitrary position in the vector.
Definition SmallVector.h:648
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:915
std::reverse_iterator< ConstIterator > ConstReverseIterator
Iterator to a const element in reversed container.
Definition SmallVector.h:313
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:423
constexpr Reference operator[](SizeType idx)
Return a reference to the element at the specified index.
Definition SmallVector.h:991
constexpr ConstIterator cbegin() const noexcept
Return a const iterator to the first element of the vector.
Definition SmallVector.h:558
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:985
void clear() noexcept
Erase all elements from the container without changing its capacity.
Definition SmallVector.h:576
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:351
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:1056
constexpr Reference at(SizeType idx)
Return a reference to the element at the specified index with bounds-checking.
Definition SmallVector.h:497
Reference reference
Reference to an element.
Definition SmallVector.h:295
constexpr ConstReference operator[](SizeType idx) const
Return a const reference to the element at the specified index.
Definition SmallVector.h:997
constexpr Reference back() noexcept
Return a reference to the last element in the vector.
Definition SmallVector.h:516
ValueType value_type
Type of the elements in the underlying container.
Definition SmallVector.h:283
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:817
ConstReverseIterator crbegin() noexcept
Return a const reverse iterator to the first element of the reversed vector (which is the last elemen...
Definition SmallVector.h:588
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:973
void assign(InputIterator first, InputIterator last)
Fill the vector with copies of elements from the given range.
Definition SmallVector.h:470
ValueType * Iterator
Iterator to an element.
Definition SmallVector.h:301
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:483
Iterator insert(ConstIterator pos, std::initializer_list< ValueType > init)
Insert elements from an initializer list before the indicated position.
Definition SmallVector.h:892
void shrink_to_fit()
Reduce the capacity as far as possible while retaining all stored elements.
Definition SmallVector.h:1164
ElementT ValueType
Type of the elements in the underlying container.
Definition SmallVector.h:281
constexpr ConstIterator cend() const noexcept
Return a const iterator pointing past the last element of the vector.
Definition SmallVector.h:567
const ValueType & ConstReference
Reference to a const element.
Definition SmallVector.h:297
Iterator iterator
Iterator to an element.
Definition SmallVector.h:303
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:410
~SmallVector()
Destructor: Destroys all stored elements and frees all allocated memory.
Definition SmallVector.h:429
void swap(SmallVector &other)
Exchange the contents of this SmallVector with those of another one.
Definition SmallVector.h:1208
void reserve(SizeType new_capacity)
Increase the capacity of the vector to the specified size.
Definition SmallVector.h:1079
std::ptrdiff_t DifferenceType
Signed integer type for the difference of two iterators.
Definition SmallVector.h:289
constexpr ConstReference front() const noexcept
Return a const reference to the first element in the vector.
Definition SmallVector.h:769
constexpr ConstIterator begin() const noexcept
Return a const iterator to the first element of the vector.
Definition SmallVector.h:543
SmallVector() noexcept=default
Construct an empty SmallVector.
ConstReference const_reference
Reference to a const element.
Definition SmallVector.h:299
Iterator erase(ConstIterator pos)
Erase a single element from the vector, moving elements behind it forward.
Definition SmallVector.h:724
constexpr ValueType * data() noexcept
Return a pointer to the contiguous data storage of the vector.
Definition SmallVector.h:608
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:960
SmallVector(InputIterator first, InputIterator last)
Construct a SmallVector that is filled with copies of elements from the given range.
Definition SmallVector.h:374
ReverseIterator rend() noexcept
Return a reverse iterator pointing past the last element of the reversed vector.
Definition SmallVector.h:1066
ValueType & Reference
Reference to an element.
Definition SmallVector.h:293
std::uint32_t SizeType
Unsigned integer type for indexing, number of elements, capacity.
Definition SmallVector.h:285
SizeType size_type
Unsigned integer type for indexing, number of elements, capacity.
Definition SmallVector.h:287
constexpr ConstReference back() const noexcept
Return a const reference to the last element in the vector.
Definition SmallVector.h:525
constexpr Reference front() noexcept
Return a reference to the first element in the vector.
Definition SmallVector.h:760
constexpr const ValueType * data() const noexcept
Return a pointer to the contiguous data storage of the vector.
Definition SmallVector.h:617
Iterator insert(ConstIterator pos, ValueType &&value)
Insert a single element before the indicated position.
Definition SmallVector.h:803
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:387
constexpr bool empty() const noexcept
Determine if the vector is empty.
Definition SmallVector.h:693
Iterator insert(ConstIterator pos, const ValueType &value)
Insert a single element before the indicated position.
Definition SmallVector.h:789
void resize(SizeType num_elements)
Change the number of elements in the container.
Definition SmallVector.h:1115
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:552
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:942
constexpr ConstIterator end() const noexcept
Return a const iterator pointing past the last element of the vector.
Definition SmallVector.h:708
constexpr SizeType inner_capacity() const noexcept
Return the number of elements this SmallVector can hold internally without having to allocate storage...
Definition SmallVector.h:778
Reference emplace_back(ArgumentTypes &&... arguments)
Construct an additional element at the end of the buffer.
Definition SmallVector.h:679
void push_back(ValueType &&value)
Move one element to the end of the buffer.
Definition SmallVector.h:1040
Iterator erase(ConstIterator first, ConstIterator last)
Erase a range of elements from the vector, moving elements behind the range forward.
Definition SmallVector.h:742
Iterator insert(ConstIterator pos, InputIterator first, InputIterator last)
Insert a range of values before the indicated position.
Definition SmallVector.h:858
constexpr SizeType max_size() const noexcept
Return the maximum number of elements that this vector can theoretically hold.
Definition SmallVector.h:903
ConstReverseIterator const_reverse_iterator
Iterator to a const element in reversed container.
Definition SmallVector.h:315
ConstIterator const_iterator
Iterator to a const element.
Definition SmallVector.h:307
constexpr ConstReference at(SizeType idx) const
Return a const reference to the element at the specified index.
Definition SmallVector.h:505
void push_back(const ValueType &value)
Copy one element to the end of the buffer.
Definition SmallVector.h:1022
void resize(SizeType num_elements, const ValueType &element)
Change the number of elements in the container.
Definition SmallVector.h:1145
constexpr SizeType size() const noexcept
Return the number of elements that are currently stored.
Definition SmallVector.h:1199
void pop_back()
Remove the last element from the vector.
Definition SmallVector.h:1006
DifferenceType difference_type
Signed integer type for the difference of two iterators.
Definition SmallVector.h:291
ReverseIterator reverse_iterator
Iterator to an element in reversed container.
Definition SmallVector.h:311
std::reverse_iterator< Iterator > ReverseIterator
Iterator to an element in reversed container.
Definition SmallVector.h:309
ConstReverseIterator crend() noexcept
Return a const reverse iterator pointing past the last element of the reversed vector.
Definition SmallVector.h:599
const ValueType * ConstIterator
Iterator to a const element.
Definition SmallVector.h:305
constexpr Iterator end() noexcept
Return an iterator pointing past the last element of the vector.
Definition SmallVector.h:699
Implementation of FinalAction and finally().
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:1696
auto constexpr bit_set(unsigned bit) noexcept -> ReturnT
Set a bit in an integral type.
Definition bit_manip.h:124
std::string cat()
Efficiently concatenate an arbitrary number of strings and numbers.
Definition cat.h:100
Definition of macros used internally by GUL.
Namespace gul17 contains all functions and classes of the General Utility Library.
Definition doxygen.h:29