19namespace hi {
inline namespace v1 {
29 using pointer = value_type *;
30 using const_pointer = value_type
const *;
31 using reference = value_type&;
32 using const_reference = value_type
const&;
33 using iterator = value_type *;
34 using const_iterator = value_type
const *;
41 constexpr static size_t value_alignment =
alignof(value_type);
73 hilet update = _reserve<false>(other_size);
75 _reserve_update(update, other_size);
87 if (
other._is_short()) {
89 std::uninitialized_move_n(
other.begin(), other_size,
begin());
90 other._set_short_size(0);
91 _set_short_size(other_size);
94 _ptr = std::exchange(
other._ptr,
nullptr);
95 _end = std::exchange(
other._end,
nullptr);
96 _cap = std::exchange(
other._cap,
nullptr);
109 hi_return_on_self_assignment(
other);
114 hilet update = _reserve<false>(other_size);
116 _reserve_update(update, other_size);
131 hi_return_on_self_assignment(
other);
133 if (not _is_short() and not
other._is_short()) {
142 if (
other._is_short()) {
144 std::uninitialized_move_n(
other.begin(), other_size, _short_data());
145 _set_short_size(other_size);
148 _ptr = std::exchange(
other._ptr,
nullptr);
149 _end = std::exchange(
other._end,
nullptr);
150 _cap = std::exchange(
other._cap,
nullptr);
159 hilet this_is_short = this->_is_short();
167 }
else if (other_is_short) {
183 std::uninitialized_value_construct_n(_short_data(), count);
184 _set_short_size(count);
187 hilet update = _reserve<false>(count);
188 std::uninitialized_value_construct_n(update.ptr, count);
189 _reserve_update(update, count);
193 lean_vector(size_type count, value_type
const& value)
197 _set_short_size(count);
200 hilet update = _reserve<false>(count);
202 _reserve_update(update, count);
211 lean_vector(std::input_iterator
auto first, std::input_iterator
auto last)
213 insert(_short_data(), first, last);
241 void assign(std::input_iterator
auto first, std::input_iterator
auto last)
261 [[nodiscard]] pointer
data() noexcept
264 if (_short_size() == 0) {
267 return _short_data();
282 [[nodiscard]] const_pointer
data() const noexcept
292 [[nodiscard]]
constexpr bool empty() const noexcept
295 return _short_size() == 0;
308 return _short_size();
311 return truncate<size_type>(_long_size());
315 [[nodiscard]]
constexpr size_type max_size() const noexcept
325 if constexpr (std::endian::native == std::endian::little) {
327 return (
sizeof(pointer) * 3 -
alignof(value_type)) /
sizeof(value_type);
330 return (
sizeof(pointer) * 3 - 1) /
sizeof(value_type);
343 return _long_capacity();
355 hilet is_short = _is_short();
356 hilet size_ = is_short ? _short_size() : _long_size();
358 return _begin_data(is_short)[index];
384 return _begin_data(_is_short())[index];
395 return const_cast<lean_vector *
>(
this)->
operator[](index);
403 [[nodiscard]] reference
front() noexcept
406 return *_begin_data(_is_short());
414 [[nodiscard]] const_reference
front() const noexcept
424 [[nodiscard]] reference
back() noexcept
427 return *(_end_data(_is_short()) - 1);
435 [[nodiscard]] const_reference
back() const noexcept
444 [[nodiscard]] iterator
begin() noexcept
446 return _begin_data(_is_short());
453 [[nodiscard]] const_iterator
begin() const noexcept
455 return _begin_data(_is_short());
462 [[nodiscard]] const_iterator
cbegin() const noexcept
471 [[nodiscard]] iterator
end() noexcept
473 return _end_data(_is_short());
480 [[nodiscard]] const_iterator
end() const noexcept
489 [[nodiscard]] const_iterator
cend() const noexcept
500 hilet is_short = _is_short();
501 std::destroy(_begin_data(is_short), _end_data(is_short));
502 _set_size(0, is_short);
515 hilet update = _reserve<false>(new_capacity);
516 _reserve_update(update);
533 hilet old_ptr = _ptr;
538 _set_short_size(old_size);
542 _end = _ptr + old_size;
543 _cap = _ptr + old_size;
546 std::uninitialized_move_n(old_ptr, old_size,
begin());
547 std::destroy_n(old_ptr, old_size);
561 template<
typename... Args>
562 iterator
emplace(const_iterator pos, Args&&...args)
567 hilet update = _reserve<true>(new_size);
568 std::construct_at(update.end, std::forward<Args>(args)...);
569 _reserve_update(update, new_size);
585 iterator
insert(const_iterator pos, value_type
const& value)
590 hilet update = _reserve<true>(new_size);
592 _reserve_update(update, new_size);
608 iterator
insert(const_iterator pos, value_type&& value)
613 hilet update = _reserve<true>(new_size);
615 _reserve_update(update, new_size);
637 hilet update = _reserve<true>(new_size);
639 _reserve_update(update, new_size);
656 iterator
insert(const_iterator pos, std::input_iterator
auto first, std::input_iterator
auto last)
664 hilet update = _reserve<true>(new_size);
665 std::uninitialized_move_n(first, n, update.end);
666 _reserve_update(update, new_size);
673 for (
auto it = first; it != last; ++it) {
674 pos =
insert(pos, *it) + 1;
711 hilet is_short = _is_short();
712 _set_size(
size() - 1, is_short);
713 std::destroy_at(_end_data(is_short));
728 iterator
erase(const_iterator first, const_iterator last)
734 std::destroy(
end() - n,
end());
735 _set_size(
size() - n, _is_short());
746 template<
typename... Args>
751 hilet update = _reserve<true>(new_size);
752 hilet obj = std::construct_at(update.end, std::forward<Args>(args)...);
753 _reserve_update(update, new_size);
766 hilet update = _reserve<true>(new_size);
768 _reserve_update(update, new_size);
779 hilet update = _reserve<true>(new_size);
781 _reserve_update(update, new_size);
794 hilet is_short = _is_short();
795 _set_size(
size() - 1, is_short);
796 std::destroy_at(_end_data(is_short));
811 if (
hilet old_size =
size(); new_size > old_size) {
812 hilet n = new_size - old_size;
814 hilet update = _reserve<true>(new_size);
815 std::uninitialized_value_construct_n(update.end, n);
816 _reserve_update(update, new_size);
819 hilet is_short = _is_short();
820 hilet n = old_size - new_size;
821 std::destroy(_end_data(is_short) - n, _end_data(is_short));
822 _set_size(new_size, is_short);
839 if (
hilet old_size =
size(); new_size > old_size) {
840 hilet n = new_size - old_size;
842 hilet update = _reserve<true>(new_size);
844 _reserve_update(update, new_size);
847 hilet is_short = _is_short();
848 hilet n = old_size - new_size;
849 std::destroy(
end() - n,
end());
850 _set_size(new_size, is_short);
862 return std::equal(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
873 return std::lexicographical_compare_three_way(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
886 c.erase(it, c.end());
896 template<
typename Pred>
901 c.erase(it, c.end());
911 pointer _ptr =
nullptr;
912 pointer _end =
nullptr;
913 pointer _cap =
nullptr;
915 [[nodiscard]]
constexpr bool _is_short() const noexcept
917 if constexpr (std::endian::native == std::endian::little) {
918 if (_ptr ==
nullptr) {
921 return to_bool(to_int(_ptr) & 1);
924 if (_cap ==
nullptr) {
927 return to_bool(to_int(_cap) & 1);
932 [[nodiscard]] pointer _short_data() const noexcept
934 static_assert(
alignof(value_type) <=
alignof(pointer));
937 if constexpr (std::endian::native == std::endian::little) {
940 return std::launder(
reinterpret_cast<pointer
>(p));
943 [[nodiscard]] pointer _long_data() const noexcept
948 [[nodiscard]]
constexpr size_type _short_size() const noexcept
950 if constexpr (std::endian::native == std::endian::little) {
951 if (_ptr ==
nullptr) {
954 return (to_int(_ptr) >> 1) & 0x1f;
957 if (_cap ==
nullptr) {
960 return (to_int(_cap) >> 1) & 0x1f;
965 [[nodiscard]]
constexpr size_type _long_size() const noexcept
971 [[nodiscard]]
constexpr size_type _long_capacity() const noexcept
977 void _set_short_size(
size_t new_size)
noexcept
979 constexpr auto mask = ~intptr_t{0xff};
983 if constexpr (std::endian::native == std::endian::little) {
984 _ptr = to_ptr<pointer>((to_int(_ptr) & mask) | new_size);
986 _cap = to_ptr<pointer>((to_int(_cap) & mask) | new_size);
990 void _set_size(
size_t new_size,
bool is_short)
noexcept
994 _set_short_size(new_size);
996 _end = _ptr + new_size;
1000 [[nodiscard]] pointer _begin_data(
bool is_short)
const noexcept
1003 return _short_data();
1005 return _long_data();
1009 [[nodiscard]] pointer _end_data(
bool is_short)
const noexcept
1012 return _short_data() + _short_size();
1018 struct _reserve_type {
1030 template<
bool ForInsert>
1031 [[nodiscard]] _reserve_type _reserve(size_type new_capacity)
const
1033 hilet is_short = _is_short();
1036 [[likely]]
return {_begin_data(is_short), _end_data(is_short),
nullptr,
false, is_short};
1039 if constexpr (ForInsert) {
1041 if (new_capacity > next_capacity) {
1042 next_capacity = new_capacity + new_capacity / 2;
1044 new_capacity = next_capacity;
1050 hilet size_ = is_short ? _short_size() : _long_size();
1051 return {new_ptr, new_ptr + size_, new_ptr + new_capacity,
true,
false};
1054 void _reserve_update(_reserve_type update)
1056 if (not update.resized) {
1060 hilet is_short = _is_short();
1061 hilet old_size = is_short ? _short_size() : _long_size();
1062 hilet old_ptr = is_short ? _short_data() : _long_data();
1064 std::uninitialized_move_n(old_ptr, old_size, update.ptr);
1065 std::destroy_n(old_ptr, old_size);
1077 void _reserve_update(_reserve_type update, size_type new_size)
1079 _reserve_update(update);
1080 _set_size(new_size, update.is_short);
1084template<std::input_iterator It, std::input_iterator ItEnd>
1085lean_vector(It first, ItEnd last) -> lean_vector<typename std::iterator_traits<It>::value_type>;
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:133
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
@ other
The gui_event does not have associated data.
DOXYGEN BUG.
Definition algorithm.hpp:15
void * advance_bytes(void *ptr, std::ptrdiff_t distance) noexcept
Advance a pointer by a number of bytes.
Definition memory.hpp:192
geometry/margins.hpp
Definition assert.hpp:18
Lean-vector with (SVO) short-vector-optimization.
Definition lean_vector.hpp:26
lean_vector(lean_vector const &other)
Copy-construct a vector.
Definition lean_vector.hpp:70
iterator insert(const_iterator pos, std::input_iterator auto first, std::input_iterator auto last)
Insert new items.
Definition lean_vector.hpp:656
const_reference front() const noexcept
Get a const-reference to the first item in the vector.
Definition lean_vector.hpp:414
size_t capacity() const noexcept
Get the current capacity of the vector.
Definition lean_vector.hpp:338
reference back() noexcept
Get a reference to the last item in the vector.
Definition lean_vector.hpp:424
void push_back(value_type &&value)
Move an item to the end of the vector.
Definition lean_vector.hpp:775
const_iterator end() const noexcept
Get an const-iterator beyond the last item in the vector.
Definition lean_vector.hpp:480
lean_vector & operator=(lean_vector &&other) noexcept
Move-assign a vector.
Definition lean_vector.hpp:129
void clear() noexcept
Remove all items from the vector.
Definition lean_vector.hpp:498
void push_back(value_type const &value)
Copy an item to the end of the vector.
Definition lean_vector.hpp:762
constexpr size_t short_capacity() const noexcept
The maximum number of items that can fit without allocation.
Definition lean_vector.hpp:323
lean_vector(std::initializer_list< value_type > list)
Construct a vector with the given initializer list.
Definition lean_vector.hpp:220
iterator end() noexcept
Get an iterator beyond the last item in the vector.
Definition lean_vector.hpp:471
const_iterator cend() const noexcept
Get an const-iterator beyond the last item in the vector.
Definition lean_vector.hpp:489
iterator begin() noexcept
Get an iterator to the first item in the vector.
Definition lean_vector.hpp:444
iterator insert(const_iterator pos, std::initializer_list< value_type > list)
Insert new items.
Definition lean_vector.hpp:688
iterator erase(const_iterator pos)
Erase an item at position.
Definition lean_vector.hpp:705
void resize(size_type new_size, value_type const &value)
Resize a vector.
Definition lean_vector.hpp:837
friend size_type erase(lean_vector &c, value_type const &value)
Erase items of a value from a vector.
Definition lean_vector.hpp:882
reference emplace_back(Args &&...args)
In-place construct an item at the end of the vector.
Definition lean_vector.hpp:747
const_pointer data() const noexcept
Get a const-pointer to the first item.
Definition lean_vector.hpp:282
void resize(size_type new_size)
Resize a vector.
Definition lean_vector.hpp:809
pointer data() noexcept
Get a pointer to the first item.
Definition lean_vector.hpp:261
constexpr bool empty() const noexcept
Check if the vector is empty.
Definition lean_vector.hpp:292
friend bool operator==(lean_vector const &lhs, lean_vector const &rhs) noexcept
Compare two vectors.
Definition lean_vector.hpp:860
iterator insert(const_iterator pos, value_type &&value)
Insert a new item.
Definition lean_vector.hpp:608
friend auto operator<=>(lean_vector const &lhs, lean_vector const &rhs) noexcept
Compare two vectors lexicographically.
Definition lean_vector.hpp:871
void assign(std::initializer_list< value_type > list)
Replace the data in the vector.
Definition lean_vector.hpp:251
lean_vector(lean_vector &&other) noexcept
Move-construct a vector.
Definition lean_vector.hpp:85
const_iterator begin() const noexcept
Get an const-iterator to the first item in the vector.
Definition lean_vector.hpp:453
friend size_type erase(lean_vector &c, Pred pred)
Erase items of a value from a vector.
Definition lean_vector.hpp:897
const_iterator cbegin() const noexcept
Get an const-iterator to the first item in the vector.
Definition lean_vector.hpp:462
void assign(std::input_iterator auto first, std::input_iterator auto last)
Replace the data in the vector.
Definition lean_vector.hpp:241
iterator insert(const_iterator pos, size_type count, value_type const &value)
Insert a new item.
Definition lean_vector.hpp:632
const_reference operator[](size_type index) const noexcept
Get a const-reference to an item in the vector.
Definition lean_vector.hpp:393
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:353
constexpr size_type size() const noexcept
Get the number of items in the vector.
Definition lean_vector.hpp:305
void shrink_to_fit()
Shrink the allocation to fit the current number of items.
Definition lean_vector.hpp:527
void pop_back()
Remove the last item from the vector.
Definition lean_vector.hpp:790
lean_vector & operator=(lean_vector const &other) noexcept
Copy-assign a vector.
Definition lean_vector.hpp:107
const_reference at(size_type index) const
Get a const-reference to an item in the vector.
Definition lean_vector.hpp:370
reference front() noexcept
Get a reference to the first item in the vector.
Definition lean_vector.hpp:403
iterator emplace(const_iterator pos, Args &&...args)
Construct in-place a new item.
Definition lean_vector.hpp:562
const_reference back() const noexcept
Get a const-reference to the last item in the vector.
Definition lean_vector.hpp:435
constexpr allocator_type get_allocator() const noexcept
The allocator_type used to allocate items.
Definition lean_vector.hpp:45
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:211
void assign(size_type count, value_type const &value)
Replace the data in the vector.
Definition lean_vector.hpp:230
reference operator[](size_type index) noexcept
Get a reference to an item in the vector.
Definition lean_vector.hpp:381
iterator erase(const_iterator first, const_iterator last)
Erase an items.
Definition lean_vector.hpp:728
void reserve(size_type new_capacity)
Reserve capacity for items.
Definition lean_vector.hpp:513
iterator insert(const_iterator pos, value_type const &value)
Insert a new item.
Definition lean_vector.hpp:585
T uninitialized_copy_n(T... args)
T uninitialized_fill_n(T... args)