10#include "../utility/utility.hpp"
11#include "../macros.hpp"
21hi_export_module(hikogui.container.lean_vector);
27hi_warning_ignore_msvc(26459);
29hi_export
namespace hi {
inline namespace v1 {
39 using pointer = value_type *;
40 using const_pointer = value_type
const *;
41 using reference = value_type&;
42 using const_reference = value_type
const&;
43 using iterator = value_type *;
44 using const_iterator = value_type
const *;
51 constexpr static size_t value_alignment =
alignof(value_type);
97 if (
other._is_short()) {
100 other._set_short_size(0);
104 _ptr = std::exchange(
other._ptr,
nullptr);
105 _end = std::exchange(
other._end,
nullptr);
106 _cap = std::exchange(
other._cap,
nullptr);
119 hi_return_on_self_assignment(
other);
141 hi_return_on_self_assignment(
other);
152 if (
other._is_short()) {
158 _ptr = std::exchange(
other._ptr,
nullptr);
159 _end = std::exchange(
other._end,
nullptr);
160 _cap = std::exchange(
other._cap,
nullptr);
193 std::uninitialized_value_construct_n(_short_data(), count);
194 _set_short_size(count);
198 std::uninitialized_value_construct_n(update.ptr, count);
199 _reserve_update(update, count);
203 lean_vector(size_type count, value_type
const& value)
207 _set_short_size(count);
212 _reserve_update(update, count);
221 lean_vector(std::input_iterator
auto first, std::input_iterator
auto last)
223 insert(_short_data(), first, last);
251 void assign(std::input_iterator
auto first, std::input_iterator
auto last)
274 if (_short_size() == 0) {
277 return _short_data();
305 return _short_size() == 0;
318 return _short_size();
320 hi_axiom(_ptr <= _end);
335 if constexpr (std::endian::native == std::endian::little) {
337 return (
sizeof(pointer) * 3 -
alignof(value_type)) /
sizeof(value_type);
340 return (
sizeof(pointer) * 3 - 1) /
sizeof(value_type);
353 return _long_capacity();
365 auto const is_short = _is_short();
366 auto const size_ = is_short ? _short_size() : _long_size();
368 return _begin_data(is_short)[index];
393 hi_axiom(index <
size());
394 return _begin_data(_is_short())[index];
405 return const_cast<lean_vector *
>(
this)->
operator[](index);
416 return *_begin_data(_is_short());
437 return *(_end_data(_is_short()) - 1);
456 return _begin_data(_is_short());
465 return _begin_data(_is_short());
483 return _end_data(_is_short());
510 auto const is_short = _is_short();
511 std::destroy(_begin_data(is_short), _end_data(is_short));
512 _set_size(0, is_short);
526 _reserve_update(update);
571 template<
typename...
Args>
578 std::construct_at(update.end, std::forward<Args>(
args)...);
595 iterator
insert(const_iterator
pos, value_type
const& value)
666 template<std::input_iterator First, std::input_iterator Last>
676 std::uninitialized_move_n(first, n, update.end);
684 for (
auto it = first;
it != last; ++
it) {
722 auto const is_short = _is_short();
723 _set_size(
size() - 1, is_short);
724 std::destroy_at(_end_data(is_short));
740 iterator
erase(const_iterator first, const_iterator last)
746 std::destroy(
end() - n,
end());
747 _set_size(
size() - n, _is_short());
758 template<
typename...
Args>
764 auto const obj = std::construct_at(update.end, std::forward<Args>(
args)...);
806 auto const is_short = _is_short();
807 _set_size(
size() - 1, is_short);
808 std::destroy_at(_end_data(is_short));
827 std::uninitialized_value_construct_n(update.end, n);
831 auto const is_short = _is_short();
833 std::destroy(_end_data(is_short) - n, _end_data(is_short));
859 auto const is_short = _is_short();
861 std::destroy(
end() - n,
end());
874 return std::equal(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
885 return std::lexicographical_compare_three_way(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
898 c.erase(
it, c.end());
908 template<
typename Pred>
913 c.erase(
it, c.end());
923 pointer _ptr =
nullptr;
924 pointer _end =
nullptr;
925 pointer _cap =
nullptr;
929 if constexpr (std::endian::native == std::endian::little) {
930 if (_ptr ==
nullptr) {
933 return to_bool(to_int(_ptr) & 1);
936 if (_cap ==
nullptr) {
939 return to_bool(to_int(_cap) & 1);
946 static_assert(
alignof(value_type) <=
alignof(pointer));
949 if constexpr (std::endian::native == std::endian::little) {
952 return std::launder(
static_cast<pointer
>(p));
962 if constexpr (std::endian::native == std::endian::little) {
963 if (_ptr ==
nullptr) {
966 return (to_int(_ptr) >> 1) & 0x1f;
969 if (_cap ==
nullptr) {
972 return (to_int(_cap) >> 1) & 0x1f;
979 hi_axiom(_ptr <= _end);
985 hi_axiom(_ptr <= _cap);
989 void _set_short_size(
size_t new_size)
noexcept
995 if constexpr (std::endian::native == std::endian::little) {
1002 void _set_size(
size_t new_size,
bool is_short)
noexcept
1012 [[
nodiscard]] pointer _begin_data(
bool is_short)
const noexcept
1015 return _short_data();
1017 return _long_data();
1021 [[
nodiscard]] pointer _end_data(
bool is_short)
const noexcept
1024 return _short_data() + _short_size();
1030 struct _reserve_type {
1042 template<
bool ForInsert>
1045 auto const is_short = _is_short();
1048 [[
likely]]
return {_begin_data(is_short), _end_data(is_short),
nullptr,
false, is_short};
1062 auto const size_ = is_short ? _short_size() : _long_size();
1066 void _reserve_update(_reserve_type update)
1068 if (
not update.resized) {
1072 auto const is_short = _is_short();
1073 auto const old_size = is_short ? _short_size() : _long_size();
1074 auto const old_ptr = is_short ? _short_data() : _long_data();
1089 void _reserve_update(_reserve_type update, size_type
new_size)
1091 _reserve_update(update);
1092 _set_size(
new_size, update.is_short);
1096template<std::input_iterator It, std::input_iterator ItEnd>
1100template<
typename T, std::convertible_to<T>... Args>
1117struct std::formatter<
hi::lean_vector<T>, char> : std::formatter<std::string, char> {
1127 r += std::format(
"{}", x);
1131 return std::formatter<std::string, char>::format(r, fc);
@ other
The gui_event does not have associated data.
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
hi_inline void * advance_bytes(void *ptr, std::ptrdiff_t distance) noexcept
Advance a pointer by a number of bytes.
Definition memory.hpp:253
The HikoGUI namespace.
Definition recursive_iterator.hpp:15
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:378
Lean-vector with (SVO) short-vector-optimization.
Definition lean_vector.hpp:36
lean_vector(lean_vector const &other)
Copy-construct a vector.
Definition lean_vector.hpp:80
const_reference front() const noexcept
Get a const-reference to the first item in the vector.
Definition lean_vector.hpp:424
size_t capacity() const noexcept
Get the current capacity of the vector.
Definition lean_vector.hpp:348
reference back() noexcept
Get a reference to the last item in the vector.
Definition lean_vector.hpp:434
void push_back(value_type &&value)
Move an item to the end of the vector.
Definition lean_vector.hpp:787
const_iterator end() const noexcept
Get an const-iterator beyond the last item in the vector.
Definition lean_vector.hpp:490
lean_vector & operator=(lean_vector &&other) noexcept
Move-assign a vector.
Definition lean_vector.hpp:139
void clear() noexcept
Remove all items from the vector.
Definition lean_vector.hpp:508
void push_back(value_type const &value)
Copy an item to the end of the vector.
Definition lean_vector.hpp:774
constexpr size_t short_capacity() const noexcept
The maximum number of items that can fit without allocation.
Definition lean_vector.hpp:333
lean_vector(std::initializer_list< value_type > list)
Construct a vector with the given initializer list.
Definition lean_vector.hpp:230
iterator end() noexcept
Get an iterator beyond the last item in the vector.
Definition lean_vector.hpp:481
const_iterator cend() const noexcept
Get an const-iterator beyond the last item in the vector.
Definition lean_vector.hpp:499
iterator begin() noexcept
Get an iterator to the first item in the vector.
Definition lean_vector.hpp:454
iterator insert(const_iterator pos, std::initializer_list< value_type > list)
Insert new items.
Definition lean_vector.hpp:699
iterator erase(const_iterator pos)
Erase an item at position.
Definition lean_vector.hpp:716
void resize(size_type new_size, value_type const &value)
Resize a vector.
Definition lean_vector.hpp:849
friend size_type erase(lean_vector &c, value_type const &value)
Erase items of a value from a vector.
Definition lean_vector.hpp:894
reference emplace_back(Args &&...args)
In-place construct an item at the end of the vector.
Definition lean_vector.hpp:759
const_pointer data() const noexcept
Get a const-pointer to the first item.
Definition lean_vector.hpp:292
void resize(size_type new_size)
Resize a vector.
Definition lean_vector.hpp:821
pointer data() noexcept
Get a pointer to the first item.
Definition lean_vector.hpp:271
lean_vector(lean_vector &&other) noexcept(std::is_nothrow_move_constructible_v< value_type >)
Move-construct a vector.
Definition lean_vector.hpp:95
constexpr bool empty() const noexcept
Check if the vector is empty.
Definition lean_vector.hpp:302
friend bool operator==(lean_vector const &lhs, lean_vector const &rhs) noexcept
Compare two vectors.
Definition lean_vector.hpp:872
iterator insert(const_iterator pos, value_type &&value)
Insert a new item.
Definition lean_vector.hpp:618
friend auto operator<=>(lean_vector const &lhs, lean_vector const &rhs) noexcept
Compare two vectors lexicographically.
Definition lean_vector.hpp:883
void assign(std::initializer_list< value_type > list)
Replace the data in the vector.
Definition lean_vector.hpp:261
const_iterator begin() const noexcept
Get an const-iterator to the first item in the vector.
Definition lean_vector.hpp:463
friend size_type erase(lean_vector &c, Pred pred)
Erase items of a value from a vector.
Definition lean_vector.hpp:909
const_iterator cbegin() const noexcept
Get an const-iterator to the first item in the vector.
Definition lean_vector.hpp:472
void assign(std::input_iterator auto first, std::input_iterator auto last)
Replace the data in the vector.
Definition lean_vector.hpp:251
iterator insert(const_iterator pos, size_type count, value_type const &value)
Insert a new item.
Definition lean_vector.hpp:642
const_reference operator[](size_type index) const noexcept
Get a const-reference to an item in the vector.
Definition lean_vector.hpp:403
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:363
constexpr size_type size() const noexcept
Get the number of items in the vector.
Definition lean_vector.hpp:315
void shrink_to_fit()
Shrink the allocation to fit the current number of items.
Definition lean_vector.hpp:537
void pop_back()
Remove the last item from the vector.
Definition lean_vector.hpp:802
lean_vector & operator=(lean_vector const &other) noexcept
Copy-assign a vector.
Definition lean_vector.hpp:117
const_reference at(size_type index) const
Get a const-reference to an item in the vector.
Definition lean_vector.hpp:380
reference front() noexcept
Get a reference to the first item in the vector.
Definition lean_vector.hpp:413
iterator emplace(const_iterator pos, Args &&...args)
Construct in-place a new item.
Definition lean_vector.hpp:572
const_reference back() const noexcept
Get a const-reference to the last item in the vector.
Definition lean_vector.hpp:445
iterator insert(const_iterator pos, First first, Last last)
Insert new items.
Definition lean_vector.hpp:667
constexpr allocator_type get_allocator() const noexcept
The allocator_type used to allocate items.
Definition lean_vector.hpp:55
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:221
void assign(size_type count, value_type const &value)
Replace the data in the vector.
Definition lean_vector.hpp:240
reference operator[](size_type index) noexcept
Get a reference to an item in the vector.
Definition lean_vector.hpp:391
iterator erase(const_iterator first, const_iterator last)
Erase an items.
Definition lean_vector.hpp:740
void reserve(size_type new_capacity)
Reserve capacity for items.
Definition lean_vector.hpp:523
iterator insert(const_iterator pos, value_type const &value)
Insert a new item.
Definition lean_vector.hpp:595
T uninitialized_copy_n(T... args)
T uninitialized_fill_n(T... args)