23 #ifndef GUL14_SMALLVECTOR_H_
24 #define GUL14_SMALLVECTOR_H_
28 #include <initializer_list>
33 #include <type_traits>
268 template <
typename ElementT,
size_t in_capacity>
327 static_assert(std::is_default_constructible<ValueType>::value,
328 "SmallVector: Element type is not default-constructible");
331 for (
SizeType i = 0u; i != num_elements; ++i)
333 size_ = num_elements;
345 fill_empty_vector_with_copied_value(num_elements, value);
364 template<
class InputIterator,
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())
439 fill_empty_vector_with_copied_value(num_elements, value);
461 template<
class InputIterator,
462 typename = std::enable_if_t<not std::is_integral<InputIterator>::value>>
463 void assign(InputIterator first, InputIterator last)
466 fill_empty_vector_with_copied_range(first, last);
476 void assign(std::initializer_list<ValueType> init)
478 const auto num_elements =
static_cast<SizeType>(init.size());
482 std::uninitialized_copy(init.begin(), init.end(),
data());
483 size_ = num_elements;
493 throw std::out_of_range(
cat(
"Index out of range: ", idx,
" >= ", size_));
494 return *(
data() + idx);
501 throw std::out_of_range(
cat(
"Index out of range: ", idx,
" >= ", size_));
502 return *(
data() + idx);
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_);
640 template <
typename... ArgumentTypes>
645 emplace_back(std::forward<ArgumentTypes>(arguments)...);
646 return data_end() - 1;
649 ValueType v(std::forward<ArgumentTypes>(arguments)...);
651 return insert_single_value(pos, std::move(v));
671 template <
typename... ArgumentTypes>
674 if (size_ == capacity_)
677 ::new(
static_cast<void*
>(data_end()))
678 ValueType(std::forward<ArgumentTypes>(arguments)...);
686 constexpr
bool empty() const noexcept {
return size_ == 0u; }
719 return erase(pos, pos + 1);
737 auto range_begin =
const_cast<Iterator>(first);
738 auto range_end =
const_cast<Iterator>(last);
739 auto num_elements = range_end - range_begin;
741 std::move(range_end,
end(), range_begin);
742 destroy_range(
end() - num_elements,
end());
744 size_ -=
static_cast<SizeType>(num_elements);
784 return insert_single_value(pos, value);
798 return insert_single_value(pos, std::move(value));
813 if (num_elements < 1)
814 return begin() + idx;
816 const SizeType num_assignable = make_space_at_idx_for_n_elements(idx, num_elements);
820 std::fill_n(insert_pos, num_assignable, value);
823 copy_value_into_uninitialized_cells(size_, num_elements - num_assignable, value);
825 size_ += num_elements;
849 template<
class InputIterator,
850 typename = std::enable_if_t<not std::is_integral<InputIterator>::value>>
854 const auto num_elements =
static_cast<SizeType>(std::distance(first, last));
855 if (num_elements < 1)
856 return begin() + idx;
858 const SizeType num_assignable = make_space_at_idx_for_n_elements(idx, num_elements);
862 std::copy_n(first, num_assignable, insert_pos);
865 copy_range_into_uninitialized_cells(size_, first + num_assignable, last);
867 size_ += num_elements;
887 return insert(pos, init.begin(), init.end());
898 return std::numeric_limits<SizeType>::max();
909 noexcept(std::is_nothrow_copy_assignable<ValueType>::value)
915 std::uninitialized_copy(other.cbegin(), other.cend(),
data());
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));
980 return not (lhs == rhs);
986 return *(
data() + idx);
992 return *(
data() + idx);
1002 auto p = data_end();
1017 if (size_ == capacity_)
1020 ::new(
static_cast<void*
>(data_end()))
ValueType(value);
1035 if (size_ == capacity_)
1038 ::new(
static_cast<void*
>(data_end()))
ValueType(std::move(value));
1051 return std::make_reverse_iterator(
end());
1061 return std::make_reverse_iterator(
begin());
1074 if (new_capacity <= capacity_)
1077 auto new_data = std::make_unique<AlignedStorage[]>(new_capacity);
1079 const auto d_end = data_end();
1081 uninitialized_move_or_copy(
data(), d_end,
reinterpret_cast<ValueType*
>(new_data.get()));
1082 destroy_range(
data(), d_end);
1084 if (is_storage_allocated())
1087 data_ptr_ = new_data.release();
1088 capacity_ = new_capacity;
1108 static_assert(std::is_default_constructible<ValueType>::value,
1109 "SmallVector: For using resize(), element type must be default-constructible");
1111 if (num_elements < size_)
1113 destroy_range(
data() + num_elements, data_end());
1115 else if (num_elements > size_)
1118 fill_uninitialized_cells_with_default_constructed_elements(size_,
1119 num_elements - size_);
1122 size_ = num_elements;
1138 if (num_elements < size_)
1140 destroy_range(
data() + num_elements, data_end());
1142 else if (num_elements > size_)
1145 copy_value_into_uninitialized_cells(size_, num_elements - size_, element);
1148 size_ = num_elements;
1160 if (new_capacity == capacity_)
1163 AlignedStorage* new_data;
1164 auto new_memory = std::unique_ptr<AlignedStorage[]>{};
1167 new_data = internal_array_.data();
1169 new_memory = std::make_unique<AlignedStorage[]>(new_capacity);
1170 new_data = new_memory.get();
1173 const auto d_end = data_end();
1175 uninitialized_move_or_copy(
data(), d_end,
reinterpret_cast<ValueType*
>(new_data));
1176 destroy_range(
data(), d_end);
1178 if (is_storage_allocated())
1181 data_ptr_ = new_memory ? new_memory.release() : new_data;
1182 capacity_ = new_capacity;
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>
1245 void copy_range_into_uninitialized_cells(
SizeType pos, InputIterator first,
1248 auto data_ptr =
data() + pos;
1252 for (
auto it = first; it != last; ++it)
1254 ::new(
static_cast<void*
>(data_ptr))
ValueType{ *it };
1260 for (
auto p =
data() + pos; p != data_ptr; ++p)
1273 void copy_value_into_uninitialized_cells(
SizeType pos,
SizeType num_elements,
1276 const auto start_ptr =
data() + pos;
1277 const auto end_ptr = start_ptr + num_elements;
1278 for (
auto p = start_ptr; p != end_ptr; ++p)
1279 ::
new(
static_cast<void*
>(p))
ValueType(value);
1283 constexpr
ValueType* data_end() noexcept
1285 return reinterpret_cast<ValueType*
>(data_ptr_) + size_;
1289 constexpr
const ValueType* data_end() const noexcept
1291 return reinterpret_cast<const ValueType*
>(data_ptr_) + size_;
1304 for (
auto it = it_start; it != it_end; ++it)
1322 template<
typename InputIterator>
1323 void fill_empty_vector_with_copied_range(InputIterator first, InputIterator last)
1325 const auto num_elements =
static_cast<SizeType>(std::distance(first, last));
1328 copy_range_into_uninitialized_cells(0u, first, last);
1329 size_ = num_elements;
1340 void fill_empty_vector_with_copied_value(
SizeType num_elements,
const ValueType& value)
1343 copy_value_into_uninitialized_cells(0u, num_elements, value);
1344 size_ = num_elements;
1355 void fill_uninitialized_cells_with_default_constructed_elements(
SizeType pos,
1358 const auto start_ptr =
data() + pos;
1359 const auto end_ptr = start_ptr + num_elements;
1360 auto ptr = start_ptr;
1364 for (; ptr != end_ptr; ++ptr)
1365 ::
new(
static_cast<void*
>(ptr))
ValueType{};
1369 destroy_range(start_ptr, ptr);
1385 const auto remaining_space = std::numeric_limits<SizeType>::max() - capacity_;
1387 if (remaining_space == 0u)
1388 throw std::length_error(
"Max. capacity reached");
1390 SizeType increase = capacity_ / 2u;
1391 if (increase > remaining_space)
1392 increase = remaining_space;
1393 else if (increase == 0u)
1396 reserve(capacity_ + increase);
1400 template <
typename T>
1406 return begin() + size_ - 1;
1411 if (size_ == capacity_)
1416 auto data_ptr =
data();
1419 ::new(
static_cast<void*
>(data_ptr + size_))
ValueType(std::move(*(data_ptr + size_ - 1)));
1421 std::move_backward(insert_pos, data_ptr + size_ - 1, data_ptr + size_);
1424 *insert_pos = std::forward<T>(value);
1430 bool is_storage_allocated() const noexcept
1432 return data_ptr_ != internal_array_.data();
1447 const auto new_size = size_ + num_elements;
1449 if (new_size > capacity_)
1452 auto data_ptr =
data();
1457 const SizeType num_shifted = size_ - idx;
1458 const SizeType num_move_initialized = std::min(num_shifted, num_elements);
1459 const SizeType num_moved = num_shifted - num_move_initialized;
1462 const auto from_ptr = data_ptr + size_ - num_move_initialized;
1463 const auto to_ptr = from_ptr + num_elements;
1464 for (
SizeType i = 0; i != num_move_initialized; ++i)
1465 ::
new(
static_cast<void*
>(to_ptr + i))
ValueType(std::move(*(from_ptr + i)));
1468 std::move_backward(data_ptr + idx, data_ptr + idx + num_moved, to_ptr);
1470 return num_move_initialized;
1491 void move_or_copy_all_elements_from(
SmallVector&& other)
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();
1499 size_ = other.size_; other.size_ = 0u;
1503 data_ptr_ = internal_array_.data();
1504 capacity_ = in_capacity;
1505 uninitialized_move_or_copy(other.begin(), other.end(),
data());
1506 size_ = other.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();
1549 if (a.size_ <= b.size_)
1554 uninitialized_move_or_copy(b.begin() + a.size_, b.end(), a.begin() + a.size_);
1555 destroy_range(b.begin() + a.size_, b.end());
1559 for (
SizeType i = 0; i != b.size_; ++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());
1573 template <
typename T>
1574 static typename std::enable_if_t<not std::is_nothrow_move_constructible<T>::value>
1575 uninitialized_move(T* src_begin, T* src_end, T* dest_begin)
1577 auto src = src_begin;
1578 auto dest = dest_begin;
1582 while (src != src_end)
1584 ::new (
static_cast<void*
>(dest))
ValueType(std::move(*src));
1591 for (
auto p = dest_begin; p != dest; ++p)
1602 template <
typename T>
1603 static typename std::enable_if_t<std::is_nothrow_move_constructible<T>::value>
1604 uninitialized_move(T* src_begin, T* src_end, T* dest_begin) noexcept
1606 auto src = src_begin;
1607 auto dest = dest_begin;
1609 while (src != src_end)
1611 ::new (
static_cast<void*
>(dest))
ValueType(std::move(*src));
1619 template <
typename T>
1620 static typename std::enable_if_t<std::is_nothrow_move_constructible<T>::value>
1621 uninitialized_move_or_copy(T* src_begin, T* src_end, T* dest_begin) noexcept
1623 uninitialized_move(src_begin, src_end, dest_begin);
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>
1631 uninitialized_move_or_copy(T* src_begin, T* src_end, T* dest_begin)
1633 std::uninitialized_copy(src_begin, src_end, dest_begin);
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>
1641 uninitialized_move_or_copy(T* src_begin, T* src_end, T* dest_begin)
1643 uninitialized_move(src_begin, src_end, dest_begin);
1655 template<
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 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
constexpr ConstReference back() const noexcept
Return a const reference to the last element in the vector.
Definition: SmallVector.h:518
Iterator iterator
Iterator to an element.
Definition: SmallVector.h:295
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
Iterator erase(ConstIterator pos)
Erase a single element from the vector, moving elements behind it forward.
Definition: SmallVector.h:717
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
constexpr ConstReference front() const noexcept
Return a const reference to the first element in the vector.
Definition: SmallVector.h:762
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
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
void resize(SizeType num_elements)
Change the number of elements in the container.
Definition: SmallVector.h:1106
constexpr Iterator begin() noexcept
Return an iterator to the first element of the vector.
Definition: SmallVector.h:527
Reference emplace_back(ArgumentTypes &&... arguments)
Construct an additional element at the end of the buffer.
Definition: SmallVector.h:672
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
ReverseIterator reverse_iterator
Iterator to an element in reversed container.
Definition: SmallVector.h:303
void pop_back()
Remove the last element from the vector.
Definition: SmallVector.h:999
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
uint32_t SizeType
Unsigned integer type for indexing, number of elements, capacity.
Definition: SmallVector.h:277
constexpr const ValueType * data() const noexcept
Return a pointer to the contiguous data storage of the vector.
Definition: SmallVector.h:610
constexpr Reference at(SizeType idx)
Return a reference to the element at the specified index with bounds-checking.
Definition: SmallVector.h:490
const ValueType & ConstReference
Reference to a const element.
Definition: SmallVector.h:289
Iterator insert(ConstIterator pos, InputIterator first, InputIterator last)
Insert a range of values before the indicated position.
Definition: SmallVector.h:851
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
constexpr Reference back() noexcept
Return a reference to the last element in the vector.
Definition: SmallVector.h:509
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
constexpr SizeType max_size() const noexcept
Return the maximum number of elements that this vector can theoretically hold.
Definition: SmallVector.h:896
Iterator insert(ConstIterator pos, std::initializer_list< ValueType > init)
Insert elements from an initializer list before the indicated position.
Definition: SmallVector.h:885
void reserve(SizeType new_capacity)
Increase the capacity of the vector to the specified size.
Definition: SmallVector.h:1072
~SmallVector()
Destructor: Destroys all stored elements and frees all allocated memory.
Definition: SmallVector.h:421
ValueType * Iterator
Iterator to an element.
Definition: SmallVector.h:293
void swap(SmallVector &other)
Exchange the contents of this SmallVector with those of another one.
Definition: SmallVector.h:1195
ReverseIterator rend() noexcept
Return a reverse iterator pointing past the last element of the reversed vector.
Definition: SmallVector.h:1059
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 emplace(ConstIterator pos, ArgumentTypes &&... arguments)
Construct an additional element at an arbitrary position in the vector.
Definition: SmallVector.h:641
DifferenceType difference_type
Signed integer type for the difference of two iterators.
Definition: SmallVector.h:283
ConstIterator const_iterator
Iterator to a const element.
Definition: SmallVector.h:299
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
constexpr ValueType * data() noexcept
Return a pointer to the contiguous data storage of the vector.
Definition: SmallVector.h:601
ElementT ValueType
Type of the elements in the underlying container.
Definition: SmallVector.h:273
constexpr ConstReference at(SizeType idx) const
Return a const reference to the element at the specified index.
Definition: SmallVector.h:498
constexpr Iterator end() noexcept
Return an iterator pointing past the last element of the vector.
Definition: SmallVector.h:692
SizeType size_type
Unsigned integer type for indexing, number of elements, capacity.
Definition: SmallVector.h:279
constexpr bool empty() const noexcept
Determine if the vector is empty.
Definition: SmallVector.h:686
constexpr ConstIterator cend() const noexcept
Return a const iterator pointing past the last element of the vector.
Definition: SmallVector.h:560
ValueType value_type
Type of the elements in the underlying container.
Definition: SmallVector.h:275
Iterator insert(ConstIterator pos, const ValueType &value)
Insert a single element before the indicated position.
Definition: SmallVector.h:782
ConstReference const_reference
Reference to a const element.
Definition: SmallVector.h:291
std::reverse_iterator< ConstIterator > ConstReverseIterator
Iterator to a const element in reversed container.
Definition: SmallVector.h:305
const ValueType * ConstIterator
Iterator to a const element.
Definition: SmallVector.h:297
SmallVector() noexcept=default
Construct an empty SmallVector.
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
SmallVector(InputIterator first, InputIterator last)
Construct a SmallVector that is filled with copies of elements from the given range.
Definition: SmallVector.h:366
void resize(SizeType num_elements, const ValueType &element)
Change the number of elements in the container.
Definition: SmallVector.h:1136
ValueType & Reference
Reference to an element.
Definition: SmallVector.h:285
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 reference
Reference to an element.
Definition: SmallVector.h:287
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
void shrink_to_fit()
Reduce the capacity as far as possible while retaining all stored elements.
Definition: SmallVector.h:1155
constexpr ConstIterator end() const noexcept
Return a const iterator pointing past the last element of the vector.
Definition: SmallVector.h:701
constexpr ConstIterator begin() const noexcept
Return a const iterator to the first element of the vector.
Definition: SmallVector.h:536
void push_back(ValueType &&value)
Move one element to the end of the buffer.
Definition: SmallVector.h:1033
constexpr ConstIterator cbegin() const noexcept
Return a const iterator to the first element of the vector.
Definition: SmallVector.h:551
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
Iterator insert(ConstIterator pos, ValueType &&value)
Insert a single element before the indicated position.
Definition: SmallVector.h:796
std::reverse_iterator< Iterator > ReverseIterator
Iterator to an element in reversed container.
Definition: SmallVector.h:301
void assign(InputIterator first, InputIterator last)
Fill the vector with copies of elements from the given range.
Definition: SmallVector.h:463
constexpr Reference operator[](SizeType idx)
Return a reference to the element at the specified index.
Definition: SmallVector.h:984
void clear() noexcept
Erase all elements from the container without changing its capacity.
Definition: SmallVector.h:569
constexpr Reference front() noexcept
Return a reference to the first element in the vector.
Definition: SmallVector.h:753
constexpr ConstReference operator[](SizeType idx) const
Return a const reference to the element at the specified index.
Definition: SmallVector.h:990
std::ptrdiff_t DifferenceType
Signed integer type for the difference of two iterators.
Definition: SmallVector.h:281
ConstReverseIterator const_reverse_iterator
Iterator to a const element in reversed container.
Definition: SmallVector.h:307
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
ConstReverseIterator crend() noexcept
Return a const reverse iterator pointing past the last element of the reversed vector.
Definition: SmallVector.h:592
void push_back(const ValueType &value)
Copy one element to the end of the buffer.
Definition: SmallVector.h:1015
constexpr SizeType size() const noexcept
Return the number of elements that are currently stored.
Definition: SmallVector.h:1186
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
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
std::string cat()
Efficiently concatenate an arbitrary number of strings and numbers.
Definition: cat.h:97
Definition of macros used internally by GUL.
Namespace gul14 contains all functions and classes of the General Utility Library.
Definition: doxygen.h:26