1 #ifndef LXGUI_SORTED_VECTOR_HPP
2 #define LXGUI_SORTED_VECTOR_HPP
4 #include "lxgui/lxgui.hpp"
68 template<
typename T,
typename Cmp = std::less<T>>
70 using base = std::vector<T>;
152 std::pair<iterator, bool>
insert(U&& t) {
154 base::push_back(std::forward<U>(t));
155 return {base::begin(),
true};
157 auto iter = std::lower_bound(base::begin(), base::end(), t, compare);
158 if (iter != base::end() && !compare(t, *iter)) {
159 return {iter,
false};
161 return {base::insert(iter, std::forward<U>(t)),
true};
172 auto [iter, inserted] =
insert(std::forward<U>(t));
174 *iter = std::forward<U>(t);
180 return base::erase(iter);
185 return base::erase(first, last);
194 template<
typename Key>
198 return base::erase(iter);
209 template<
typename Key>
212 auto iter = std::lower_bound(begin(), end(), k, compare);
213 if (iter != end() && !compare(k, *iter)) {
225 template<
typename Key>
228 auto iter = std::lower_bound(begin(), end(), k, compare);
229 if (iter != end() && !compare(k, *iter)) {
237 using base::capacity;
241 using base::max_size;
242 using base::pop_back;
Sorted std::vector wrapper. This class is a light alternative to std::set. Inspired from: [1] www....
sorted_vector(const Cmp &c)
Default constructor, with comparator. Creates an empty vector and sets the comparator function.
sorted_vector & operator=(sorted_vector &&s)=default
Move another vector into this one. The other vector is left empty in the process, and all its content...
const_iterator find(const Key &k) const
Find an object in this vector by its key. The key can be a copy of the element itself,...
typename base::const_reverse_iterator const_reverse_iterator
sorted_vector()=default
Default constructor. Creates an empty vector.
iterator find(const Key &k)
Find an object in this vector by its key. The key can be a copy of the element itself,...
typename base::reverse_iterator reverse_iterator
std::pair< iterator, bool > insert(U &&t)
Insert the provided object in the vector, only if no object exists with the same key.
iterator erase(const Key &k)
Erase an element from this vector by its key. The key can be a copy of the element itself,...
iterator erase(iterator first, iterator last)
Erase a range from this vector.
sorted_vector(sorted_vector &&s)=default
Move another sorted_vector into this one. The other vector is left empty in the process,...
typename base::const_iterator const_iterator
iterator erase(iterator iter)
Erase an element from this vector.
typename base::iterator iterator
Cmp & comparator()
Return the comparator object.
sorted_vector & operator=(const sorted_vector &s)=default
Copy another sorted_vector into this one.
sorted_vector(const base &s)
Copy a pre-built vector into this one. The provided vector must be sorted, and shall not contain any ...
iterator insert_or_assign(U &&t)
Insert the provided object in the vector.
const Cmp & comparator() const
Return the comparator object.
sorted_vector(base &&s)
Move a pre-built vector into this one. The provided vector must be sorted, and shall not contain any ...
sorted_vector(std::initializer_list< T > l)
Write an initializer list into this vector. The list need not be sorted.
sorted_vector(const sorted_vector &s)=default
Copy another vector into this one.
Empty type, used in the implementation of utils::variant.