53template<
class T,
bool unique>
54class sorted_vector :
public std::vector<T> {
56 sorted_vector() : std::vector<T>() { }
57 sorted_vector(std::size_t n) : std::vector<T>(n) { }
58 sorted_vector(std::size_t n,
const T& x) : std::vector<T>(n, x) { }
60 sorted_vector(
const sorted_vector& v) : std::vector<T>(v) { }
61 sorted_vector(sorted_vector&& v) : std::vector<T>(std::move(v)) { }
63 sorted_vector& operator= (
const sorted_vector& v) { *
this = v; }
64 sorted_vector& operator= (sorted_vector&& v) { *
this = std::move(v); }
69 inline typename std::vector<T>::iterator
70 insert_sorted(
const T& x) {
71 if constexpr (not unique) {
72 const auto it = std::upper_bound(this->begin(), this->end(), x);
73 return this->insert(it, x);
76 if (this->size() == 0) {
81 const auto it = upper_bound(this->begin(), this->end(), x);
82 const auto pre_it = it == this->begin() ? it : it - 1;
84 return this->insert(it, x);
90 inline typename std::vector<T>::iterator
91 insert_sorted(T&& x) {
92 if constexpr (not unique) {
93 const auto it = std::upper_bound(this->begin(), this->end(), x);
94 return this->insert(it, std::move(x));
97 if (this->size() == 0) {
98 this->push_back(std::move(x));
102 const auto it = std::upper_bound(this->begin(), this->end(), x);
103 const auto pre_it = it == this->begin() ? it : it - 1;
105 return this->insert(it, std::move(x));
112 inline typename std::vector<T>::iterator
113 remove_sorted(
const T& x) {
117 const auto i1 = std::lower_bound(this->begin(), this->end(), x);
118 return this->erase(i1);
122 inline bool contains(
const T& x)
const {
123 return std::binary_search(this->begin(), this->end(), x);
127 inline typename std::vector<T>::iterator
128 find_sorted(
const T& x) {
129 const auto i = std::lower_bound(this->begin(), this->end(), x);
130 if (i == this->end()) {
return this->end(); }
132 if (*i == x) {
return i; }
Main namespace of the library.
Definition definitions.hpp:48