10#include "../utility/utility.hpp"
11#include "../macros.hpp"
20hi_export_module(hikogui.container.lean_vector);
26hi_warning_ignore_msvc(26459);
28hi_export
namespace hi {
inline namespace v1 {
38 using pointer = value_type *;
39 using const_pointer = value_type
const *;
40 using reference = value_type&;
41 using const_reference = value_type
const&;
42 using iterator = value_type *;
43 using const_iterator = value_type
const *;
50 constexpr static size_t value_alignment =
alignof(value_type);
96 if (
other._is_short()) {
99 other._set_short_size(0);
103 _ptr = std::exchange(
other._ptr,
nullptr);
104 _end = std::exchange(
other._end,
nullptr);
105 _cap = std::exchange(
other._cap,
nullptr);
118 hi_return_on_self_assignment(
other);
140 hi_return_on_self_assignment(
other);
151 if (
other._is_short()) {
157 _ptr = std::exchange(
other._ptr,
nullptr);
158 _end = std::exchange(
other._end,
nullptr);
159 _cap = std::exchange(
other._cap,
nullptr);
192 std::uninitialized_value_construct_n(_short_data(), count);
193 _set_short_size(count);
197 std::uninitialized_value_construct_n(update.ptr, count);
198 _reserve_update(update, count);
202 lean_vector(size_type count, value_type
const& value)
206 _set_short_size(count);
211 _reserve_update(update, count);
220 lean_vector(std::input_iterator
auto first, std::input_iterator
auto last)
222 insert(_short_data(), first, last);
250 void assign(std::input_iterator
auto first, std::input_iterator
auto last)
273 if (_short_size() == 0) {
276 return _short_data();
304 return _short_size() == 0;
317 return _short_size();
319 hi_axiom(_ptr <= _end);
334 if constexpr (std::endian::native == std::endian::little) {
336 return (
sizeof(pointer) * 3 -
alignof(value_type)) /
sizeof(value_type);
339 return (
sizeof(pointer) * 3 - 1) /
sizeof(value_type);
352 return _long_capacity();
364 auto const is_short = _is_short();
365 auto const size_ = is_short ? _short_size() : _long_size();
367 return _begin_data(is_short)[index];
392 hi_axiom(index <
size());
393 return _begin_data(_is_short())[index];
404 return const_cast<lean_vector *
>(
this)->
operator[](index);
415 return *_begin_data(_is_short());
436 return *(_end_data(_is_short()) - 1);
455 return _begin_data(_is_short());
464 return _begin_data(_is_short());
482 return _end_data(_is_short());
509 auto const is_short = _is_short();
510 std::destroy(_begin_data(is_short), _end_data(is_short));
511 _set_size(0, is_short);
525 _reserve_update(update);
570 template<
typename...
Args>
577 std::construct_at(update.end, std::forward<Args>(
args)...);
594 iterator
insert(const_iterator
pos, value_type
const& value)
665 template<std::input_iterator First, std::input_iterator Last>
675 std::uninitialized_move_n(first, n, update.end);
683 for (
auto it = first;
it != last; ++
it) {
721 auto const is_short = _is_short();
722 _set_size(
size() - 1, is_short);
723 std::destroy_at(_end_data(is_short));
739 iterator
erase(const_iterator first, const_iterator last)
745 std::destroy(
end() - n,
end());
746 _set_size(
size() - n, _is_short());
757 template<
typename...
Args>
763 auto const obj = std::construct_at(update.end, std::forward<Args>(
args)...);
805 auto const is_short = _is_short();
806 _set_size(
size() - 1, is_short);
807 std::destroy_at(_end_data(is_short));
826 std::uninitialized_value_construct_n(update.end, n);
830 auto const is_short = _is_short();
832 std::destroy(_end_data(is_short) - n, _end_data(is_short));
858 auto const is_short = _is_short();
860 std::destroy(
end() - n,
end());
873 return std::equal(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
884 return std::lexicographical_compare_three_way(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
897 c.erase(
it, c.end());
907 template<
typename Pred>
912 c.erase(
it, c.end());
922 pointer _ptr =
nullptr;
923 pointer _end =
nullptr;
924 pointer _cap =
nullptr;
928 if constexpr (std::endian::native == std::endian::little) {
929 if (_ptr ==
nullptr) {
932 return to_bool(to_int(_ptr) & 1);
935 if (_cap ==
nullptr) {
938 return to_bool(to_int(_cap) & 1);
945 static_assert(
alignof(value_type) <=
alignof(pointer));
948 if constexpr (std::endian::native == std::endian::little) {
951 return std::launder(
static_cast<pointer
>(p));
961 if constexpr (std::endian::native == std::endian::little) {
962 if (_ptr ==
nullptr) {
965 return (to_int(_ptr) >> 1) & 0x1f;
968 if (_cap ==
nullptr) {
971 return (to_int(_cap) >> 1) & 0x1f;
978 hi_axiom(_ptr <= _end);
984 hi_axiom(_ptr <= _cap);
988 void _set_short_size(
size_t new_size)
noexcept
994 if constexpr (std::endian::native == std::endian::little) {
1001 void _set_size(
size_t new_size,
bool is_short)
noexcept
1011 [[
nodiscard]] pointer _begin_data(
bool is_short)
const noexcept
1014 return _short_data();
1016 return _long_data();
1020 [[
nodiscard]] pointer _end_data(
bool is_short)
const noexcept
1023 return _short_data() + _short_size();
1029 struct _reserve_type {
1041 template<
bool ForInsert>
1044 auto const is_short = _is_short();
1047 [[
likely]]
return {_begin_data(is_short), _end_data(is_short),
nullptr,
false, is_short};
1061 auto const size_ = is_short ? _short_size() : _long_size();
1065 void _reserve_update(_reserve_type update)
1067 if (
not update.resized) {
1071 auto const is_short = _is_short();
1072 auto const old_size = is_short ? _short_size() : _long_size();
1073 auto const old_ptr = is_short ? _short_data() : _long_data();
1088 void _reserve_update(_reserve_type update, size_type
new_size)
1090 _reserve_update(update);
1091 _set_size(
new_size, update.is_short);
1095template<std::input_iterator It, std::input_iterator ItEnd>
1099template<
typename T, std::convertible_to<T>... Args>
@ 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:35
lean_vector(lean_vector const &other)
Copy-construct a vector.
Definition lean_vector.hpp:79
const_reference front() const noexcept
Get a const-reference to the first item in the vector.
Definition lean_vector.hpp:423
size_t capacity() const noexcept
Get the current capacity of the vector.
Definition lean_vector.hpp:347
reference back() noexcept
Get a reference to the last item in the vector.
Definition lean_vector.hpp:433
void push_back(value_type &&value)
Move an item to the end of the vector.
Definition lean_vector.hpp:786
const_iterator end() const noexcept
Get an const-iterator beyond the last item in the vector.
Definition lean_vector.hpp:489
lean_vector & operator=(lean_vector &&other) noexcept
Move-assign a vector.
Definition lean_vector.hpp:138
void clear() noexcept
Remove all items from the vector.
Definition lean_vector.hpp:507
void push_back(value_type const &value)
Copy an item to the end of the vector.
Definition lean_vector.hpp:773
constexpr size_t short_capacity() const noexcept
The maximum number of items that can fit without allocation.
Definition lean_vector.hpp:332
lean_vector(std::initializer_list< value_type > list)
Construct a vector with the given initializer list.
Definition lean_vector.hpp:229
iterator end() noexcept
Get an iterator beyond the last item in the vector.
Definition lean_vector.hpp:480
const_iterator cend() const noexcept
Get an const-iterator beyond the last item in the vector.
Definition lean_vector.hpp:498
iterator begin() noexcept
Get an iterator to the first item in the vector.
Definition lean_vector.hpp:453
iterator insert(const_iterator pos, std::initializer_list< value_type > list)
Insert new items.
Definition lean_vector.hpp:698
iterator erase(const_iterator pos)
Erase an item at position.
Definition lean_vector.hpp:715
void resize(size_type new_size, value_type const &value)
Resize a vector.
Definition lean_vector.hpp:848
friend size_type erase(lean_vector &c, value_type const &value)
Erase items of a value from a vector.
Definition lean_vector.hpp:893
reference emplace_back(Args &&...args)
In-place construct an item at the end of the vector.
Definition lean_vector.hpp:758
const_pointer data() const noexcept
Get a const-pointer to the first item.
Definition lean_vector.hpp:291
void resize(size_type new_size)
Resize a vector.
Definition lean_vector.hpp:820
pointer data() noexcept
Get a pointer to the first item.
Definition lean_vector.hpp:270
lean_vector(lean_vector &&other) noexcept(std::is_nothrow_move_constructible_v< value_type >)
Move-construct a vector.
Definition lean_vector.hpp:94
constexpr bool empty() const noexcept
Check if the vector is empty.
Definition lean_vector.hpp:301
friend bool operator==(lean_vector const &lhs, lean_vector const &rhs) noexcept
Compare two vectors.
Definition lean_vector.hpp:871
iterator insert(const_iterator pos, value_type &&value)
Insert a new item.
Definition lean_vector.hpp:617
friend auto operator<=>(lean_vector const &lhs, lean_vector const &rhs) noexcept
Compare two vectors lexicographically.
Definition lean_vector.hpp:882
void assign(std::initializer_list< value_type > list)
Replace the data in the vector.
Definition lean_vector.hpp:260
const_iterator begin() const noexcept
Get an const-iterator to the first item in the vector.
Definition lean_vector.hpp:462
friend size_type erase(lean_vector &c, Pred pred)
Erase items of a value from a vector.
Definition lean_vector.hpp:908
const_iterator cbegin() const noexcept
Get an const-iterator to the first item in the vector.
Definition lean_vector.hpp:471
void assign(std::input_iterator auto first, std::input_iterator auto last)
Replace the data in the vector.
Definition lean_vector.hpp:250
iterator insert(const_iterator pos, size_type count, value_type const &value)
Insert a new item.
Definition lean_vector.hpp:641
const_reference operator[](size_type index) const noexcept
Get a const-reference to an item in the vector.
Definition lean_vector.hpp:402
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:362
constexpr size_type size() const noexcept
Get the number of items in the vector.
Definition lean_vector.hpp:314
void shrink_to_fit()
Shrink the allocation to fit the current number of items.
Definition lean_vector.hpp:536
void pop_back()
Remove the last item from the vector.
Definition lean_vector.hpp:801
lean_vector & operator=(lean_vector const &other) noexcept
Copy-assign a vector.
Definition lean_vector.hpp:116
const_reference at(size_type index) const
Get a const-reference to an item in the vector.
Definition lean_vector.hpp:379
reference front() noexcept
Get a reference to the first item in the vector.
Definition lean_vector.hpp:412
iterator emplace(const_iterator pos, Args &&...args)
Construct in-place a new item.
Definition lean_vector.hpp:571
const_reference back() const noexcept
Get a const-reference to the last item in the vector.
Definition lean_vector.hpp:444
iterator insert(const_iterator pos, First first, Last last)
Insert new items.
Definition lean_vector.hpp:666
constexpr allocator_type get_allocator() const noexcept
The allocator_type used to allocate items.
Definition lean_vector.hpp:54
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:220
void assign(size_type count, value_type const &value)
Replace the data in the vector.
Definition lean_vector.hpp:239
reference operator[](size_type index) noexcept
Get a reference to an item in the vector.
Definition lean_vector.hpp:390
iterator erase(const_iterator first, const_iterator last)
Erase an items.
Definition lean_vector.hpp:739
void reserve(size_type new_capacity)
Reserve capacity for items.
Definition lean_vector.hpp:522
iterator insert(const_iterator pos, value_type const &value)
Insert a new item.
Definition lean_vector.hpp:594
T uninitialized_copy_n(T... args)
T uninitialized_fill_n(T... args)