64template <
class T,
bool unique,
typename allocator_t = std::allocator<T> >
67 using iterator_t =
typename std::vector<T, allocator_t>::iterator;
72 sorted_vector(
const std::size_t n) noexcept : std::vector<T, allocator_t>(n) { }
74 sorted_vector(
const std::size_t n,
const T& x) noexcept : std::vector<T, allocator_t>(n, x) { }
95 if constexpr (not unique) {
96 const auto it = std::upper_bound(this->begin(), this->end(), x);
97 return this->insert(it, x);
100 if (this->size() == 0) {
102 return this->begin();
105 const auto it = upper_bound(this->begin(), this->end(), x);
106 const auto pre_it = it == this->begin() ? it : it - 1;
108 return this->insert(it, x);
119 if constexpr (not unique) {
120 const auto it = std::upper_bound(this->begin(), this->end(), x);
121 return this->insert(it, std::forward<T>(x));
124 if (this->size() == 0) {
125 this->push_back(std::forward<T>(x));
126 return this->begin();
129 const auto it = std::upper_bound(this->begin(), this->end(), x);
130 const auto pre_it = it == this->begin() ? it : it - 1;
132 return this->insert(it, std::forward<T>(x));
147 const auto i1 = std::lower_bound(this->begin(), this->end(), x);
148 return this->erase(i1);
158 [[nodiscard]]
bool contains(
const T& x)
const noexcept {
159 return std::binary_search(this->begin(), this->end(), x);
169 const auto i = std::lower_bound(this->begin(), this->end(), x);
170 if (i == this->end()) {
return this->end(); }
172 if (*i == x) {
return i; }
Sorted vector class.
Definition sorted_vector.hpp:65
iterator_t insert_sorted(T &&x) noexcept
Insert an element to the vector.
Definition sorted_vector.hpp:118
bool contains(const T &x) const noexcept
Query whether an element is in the vector or not.
Definition sorted_vector.hpp:158
iterator_t insert_sorted(const T &x) noexcept
Insert an element to the vector.
Definition sorted_vector.hpp:94
iterator_t remove_sorted(const T &x) noexcept
Remove an element from the vector.
Definition sorted_vector.hpp:143
sorted_vector() noexcept
Empty constructor.
Definition sorted_vector.hpp:70
sorted_vector(sorted_vector &&v) noexcept=default
Move constructor.
iterator_t find_sorted(const T &x) noexcept
Find the position of an element in the vector.
Definition sorted_vector.hpp:168
~sorted_vector() noexcept=default
Empty destructor.
sorted_vector(const std::size_t n, const T &x) noexcept
Constructor with number of elements and initial element.
Definition sorted_vector.hpp:74
sorted_vector(const sorted_vector &v) noexcept=default
Copy constructor.
sorted_vector & operator=(const sorted_vector &v) noexcept=default
Copy-assignment operator.
sorted_vector(const std::size_t n) noexcept
Constructor with number of elements.
Definition sorted_vector.hpp:72
Main namespace of the library.
Definition basic_types.hpp:48