10#include "../utility/utility.hpp"
11#include "../macros.hpp"
24hi_warning_ignore_msvc(26459);
26namespace hi {
inline namespace v1 {
36 using pointer = value_type *;
37 using const_pointer = value_type
const *;
38 using reference = value_type&;
39 using const_reference = value_type
const&;
40 using iterator = value_type *;
41 using const_iterator = value_type
const *;
48 constexpr static size_t value_alignment =
alignof(value_type);
94 if (
other._is_short()) {
97 other._set_short_size(0);
101 _ptr = std::exchange(
other._ptr,
nullptr);
102 _end = std::exchange(
other._end,
nullptr);
103 _cap = std::exchange(
other._cap,
nullptr);
116 hi_return_on_self_assignment(
other);
138 hi_return_on_self_assignment(
other);
149 if (
other._is_short()) {
155 _ptr = std::exchange(
other._ptr,
nullptr);
156 _end = std::exchange(
other._end,
nullptr);
157 _cap = std::exchange(
other._cap,
nullptr);
190 std::uninitialized_value_construct_n(_short_data(), count);
191 _set_short_size(count);
195 std::uninitialized_value_construct_n(update.ptr, count);
196 _reserve_update(update, count);
200 lean_vector(size_type count, value_type
const& value)
204 _set_short_size(count);
209 _reserve_update(update, count);
218 lean_vector(std::input_iterator
auto first, std::input_iterator
auto last)
220 insert(_short_data(), first, last);
248 void assign(std::input_iterator
auto first, std::input_iterator
auto last)
271 if (_short_size() == 0) {
274 return _short_data();
302 return _short_size() == 0;
315 return _short_size();
317 hi_axiom(_ptr <= _end);
332 if constexpr (std::endian::native == std::endian::little) {
334 return (
sizeof(
pointer) * 3 -
alignof(value_type)) /
sizeof(value_type);
337 return (
sizeof(
pointer) * 3 - 1) /
sizeof(value_type);
350 return _long_capacity();
362 hilet is_short = _is_short();
363 hilet
size_ = is_short ? _short_size() : _long_size();
365 return _begin_data(is_short)[index];
390 hi_axiom(index <
size());
391 return _begin_data(_is_short())[index];
402 return const_cast<lean_vector *
>(
this)->
operator[](index);
413 return *_begin_data(_is_short());
434 return *(_end_data(_is_short()) - 1);
453 return _begin_data(_is_short());
462 return _begin_data(_is_short());
480 return _end_data(_is_short());
507 hilet is_short = _is_short();
508 std::destroy(_begin_data(is_short), _end_data(is_short));
509 _set_size(0, is_short);
523 _reserve_update(update);
568 template<
typename...
Args>
575 std::construct_at(update.end, std::forward<Args>(args)...);
592 iterator
insert(const_iterator
pos, value_type
const& value)
663 iterator
insert(const_iterator
pos, std::input_iterator
auto first, std::input_iterator
auto last)
672 std::uninitialized_move_n(first, n, update.end);
680 for (
auto it = first;
it != last; ++
it) {
718 hilet is_short = _is_short();
719 _set_size(
size() - 1, is_short);
720 std::destroy_at(_end_data(is_short));
736 iterator
erase(const_iterator first, const_iterator last)
742 std::destroy(
end() - n,
end());
743 _set_size(
size() - n, _is_short());
754 template<
typename...
Args>
760 hilet
obj = std::construct_at(update.end, std::forward<Args>(args)...);
802 hilet is_short = _is_short();
803 _set_size(
size() - 1, is_short);
804 std::destroy_at(_end_data(is_short));
823 std::uninitialized_value_construct_n(update.end, n);
827 hilet is_short = _is_short();
829 std::destroy(_end_data(is_short) - n, _end_data(is_short));
855 hilet is_short = _is_short();
857 std::destroy(
end() - n,
end());
870 return std::equal(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
881 return std::lexicographical_compare_three_way(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
894 c.erase(
it, c.end());
904 template<
typename Pred>
909 c.erase(
it, c.end());
919 pointer _ptr =
nullptr;
920 pointer _end =
nullptr;
921 pointer _cap =
nullptr;
925 if constexpr (std::endian::native == std::endian::little) {
926 if (_ptr ==
nullptr) {
929 return to_bool(to_int(_ptr) & 1);
932 if (_cap ==
nullptr) {
935 return to_bool(to_int(_cap) & 1);
942 static_assert(
alignof(value_type) <=
alignof(pointer));
945 if constexpr (std::endian::native == std::endian::little) {
948 return std::launder(
static_cast<pointer
>(p));
958 if constexpr (std::endian::native == std::endian::little) {
959 if (_ptr ==
nullptr) {
962 return (to_int(_ptr) >> 1) & 0x1f;
965 if (_cap ==
nullptr) {
968 return (to_int(_cap) >> 1) & 0x1f;
975 hi_axiom(_ptr <= _end);
981 hi_axiom(_ptr <= _cap);
985 void _set_short_size(
size_t new_size)
noexcept
991 if constexpr (std::endian::native == std::endian::little) {
998 void _set_size(
size_t new_size,
bool is_short)
noexcept
1008 [[
nodiscard]] pointer _begin_data(
bool is_short)
const noexcept
1011 return _short_data();
1013 return _long_data();
1017 [[
nodiscard]] pointer _end_data(
bool is_short)
const noexcept
1020 return _short_data() + _short_size();
1026 struct _reserve_type {
1038 template<
bool ForInsert>
1041 hilet is_short = _is_short();
1044 [[
likely]]
return {_begin_data(is_short), _end_data(is_short),
nullptr,
false, is_short};
1058 hilet
size_ = is_short ? _short_size() : _long_size();
1062 void _reserve_update(_reserve_type update)
1064 if (
not update.resized) {
1068 hilet is_short = _is_short();
1069 hilet
old_size = is_short ? _short_size() : _long_size();
1070 hilet
old_ptr = is_short ? _short_data() : _long_data();
1085 void _reserve_update(_reserve_type update, size_type
new_size)
1087 _reserve_update(update);
1088 _set_size(
new_size, update.is_short);
1092template<std::input_iterator It, std::input_iterator ItEnd>
@ other
The gui_event does not have associated data.
DOXYGEN BUG.
Definition algorithm.hpp:16
void * advance_bytes(void *ptr, std::ptrdiff_t distance) noexcept
Advance a pointer by a number of bytes.
Definition memory.hpp:195
geometry/margins.hpp
Definition lookahead_iterator.hpp:5
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
Lean-vector with (SVO) short-vector-optimization.
Definition lean_vector.hpp:33
lean_vector(lean_vector const &other)
Copy-construct a vector.
Definition lean_vector.hpp:77
iterator insert(const_iterator pos, std::input_iterator auto first, std::input_iterator auto last)
Insert new items.
Definition lean_vector.hpp:663
const_reference front() const noexcept
Get a const-reference to the first item in the vector.
Definition lean_vector.hpp:421
size_t capacity() const noexcept
Get the current capacity of the vector.
Definition lean_vector.hpp:345
reference back() noexcept
Get a reference to the last item in the vector.
Definition lean_vector.hpp:431
void push_back(value_type &&value)
Move an item to the end of the vector.
Definition lean_vector.hpp:783
const_iterator end() const noexcept
Get an const-iterator beyond the last item in the vector.
Definition lean_vector.hpp:487
lean_vector & operator=(lean_vector &&other) noexcept
Move-assign a vector.
Definition lean_vector.hpp:136
void clear() noexcept
Remove all items from the vector.
Definition lean_vector.hpp:505
void push_back(value_type const &value)
Copy an item to the end of the vector.
Definition lean_vector.hpp:770
constexpr size_t short_capacity() const noexcept
The maximum number of items that can fit without allocation.
Definition lean_vector.hpp:330
lean_vector(std::initializer_list< value_type > list)
Construct a vector with the given initializer list.
Definition lean_vector.hpp:227
iterator end() noexcept
Get an iterator beyond the last item in the vector.
Definition lean_vector.hpp:478
const_iterator cend() const noexcept
Get an const-iterator beyond the last item in the vector.
Definition lean_vector.hpp:496
iterator begin() noexcept
Get an iterator to the first item in the vector.
Definition lean_vector.hpp:451
iterator insert(const_iterator pos, std::initializer_list< value_type > list)
Insert new items.
Definition lean_vector.hpp:695
iterator erase(const_iterator pos)
Erase an item at position.
Definition lean_vector.hpp:712
void resize(size_type new_size, value_type const &value)
Resize a vector.
Definition lean_vector.hpp:845
friend size_type erase(lean_vector &c, value_type const &value)
Erase items of a value from a vector.
Definition lean_vector.hpp:890
reference emplace_back(Args &&...args)
In-place construct an item at the end of the vector.
Definition lean_vector.hpp:755
const_pointer data() const noexcept
Get a const-pointer to the first item.
Definition lean_vector.hpp:289
void resize(size_type new_size)
Resize a vector.
Definition lean_vector.hpp:817
pointer data() noexcept
Get a pointer to the first item.
Definition lean_vector.hpp:268
lean_vector(lean_vector &&other) noexcept(std::is_nothrow_move_constructible_v< value_type >)
Move-construct a vector.
Definition lean_vector.hpp:92
constexpr bool empty() const noexcept
Check if the vector is empty.
Definition lean_vector.hpp:299
friend bool operator==(lean_vector const &lhs, lean_vector const &rhs) noexcept
Compare two vectors.
Definition lean_vector.hpp:868
iterator insert(const_iterator pos, value_type &&value)
Insert a new item.
Definition lean_vector.hpp:615
friend auto operator<=>(lean_vector const &lhs, lean_vector const &rhs) noexcept
Compare two vectors lexicographically.
Definition lean_vector.hpp:879
void assign(std::initializer_list< value_type > list)
Replace the data in the vector.
Definition lean_vector.hpp:258
const_iterator begin() const noexcept
Get an const-iterator to the first item in the vector.
Definition lean_vector.hpp:460
friend size_type erase(lean_vector &c, Pred pred)
Erase items of a value from a vector.
Definition lean_vector.hpp:905
const_iterator cbegin() const noexcept
Get an const-iterator to the first item in the vector.
Definition lean_vector.hpp:469
void assign(std::input_iterator auto first, std::input_iterator auto last)
Replace the data in the vector.
Definition lean_vector.hpp:248
iterator insert(const_iterator pos, size_type count, value_type const &value)
Insert a new item.
Definition lean_vector.hpp:639
const_reference operator[](size_type index) const noexcept
Get a const-reference to an item in the vector.
Definition lean_vector.hpp:400
constexpr lean_vector() noexcept=default
Construct an empty vector.
reference at(size_type index)
Get a reference to an item in the vector.
Definition lean_vector.hpp:360
constexpr size_type size() const noexcept
Get the number of items in the vector.
Definition lean_vector.hpp:312
void shrink_to_fit()
Shrink the allocation to fit the current number of items.
Definition lean_vector.hpp:534
void pop_back()
Remove the last item from the vector.
Definition lean_vector.hpp:798
lean_vector & operator=(lean_vector const &other) noexcept
Copy-assign a vector.
Definition lean_vector.hpp:114
const_reference at(size_type index) const
Get a const-reference to an item in the vector.
Definition lean_vector.hpp:377
reference front() noexcept
Get a reference to the first item in the vector.
Definition lean_vector.hpp:410
iterator emplace(const_iterator pos, Args &&...args)
Construct in-place a new item.
Definition lean_vector.hpp:569
const_reference back() const noexcept
Get a const-reference to the last item in the vector.
Definition lean_vector.hpp:442
constexpr allocator_type get_allocator() const noexcept
The allocator_type used to allocate items.
Definition lean_vector.hpp:52
lean_vector(std::input_iterator auto first, std::input_iterator auto last)
Construct a vector with the data pointed by iterators.
Definition lean_vector.hpp:218
void assign(size_type count, value_type const &value)
Replace the data in the vector.
Definition lean_vector.hpp:237
reference operator[](size_type index) noexcept
Get a reference to an item in the vector.
Definition lean_vector.hpp:388
iterator erase(const_iterator first, const_iterator last)
Erase an items.
Definition lean_vector.hpp:736
void reserve(size_type new_capacity)
Reserve capacity for items.
Definition lean_vector.hpp:520
iterator insert(const_iterator pos, value_type const &value)
Insert a new item.
Definition lean_vector.hpp:592
Definition concepts.hpp:39
Definition concepts.hpp:42
T uninitialized_copy_n(T... args)
T uninitialized_fill_n(T... args)