dlinear  0.0.1
Delta-complete SMT solver for linear programming
Loading...
Searching...
No Matches
SortedVector.hpp
1
7#pragma once
8
9#include <algorithm>
10#include <functional>
11#include <iostream>
12#include <iterator>
13#include <utility>
14#include <vector>
15
16namespace dlinear {
17struct Bound; // Forward declaration
18
48template <class T, class Compare = std::less<T>>
50 public:
51 using value_type = T;
52 using size_type = size_t;
53 using difference_type = std::ptrdiff_t;
54 using reference = T&;
55 using const_reference = const T&;
56 using pointer = T*;
57 using const_pointer = const T*;
58 using iterator = typename std::vector<T>::iterator;
59 using const_iterator = typename std::vector<T>::const_iterator;
61 typename std::vector<T>::reverse_iterator;
63 typename std::vector<T>::const_reverse_iterator;
64
66 SortedVector() = default;
71 explicit SortedVector(size_t size) : vector_(size) {}
78 SortedVector(std::initializer_list<T> list) {
79 vector_.reserve(list.size());
80 for (const auto& value : list) insert(value);
81 }
82
92 template <typename V>
93 iterator insert(V&& value) {
94 auto it = std::lower_bound(vector_.cbegin(), vector_.cend(), value, compare_);
95 return vector_.insert(it, std::forward<V>(value));
96 }
97
109 template <typename V>
110 iterator insert(V&& value, bool insert_lower) {
111 auto it = insert_lower ? std::lower_bound(vector_.cbegin(), vector_.cend(), value, compare_)
112 : std::upper_bound(vector_.cbegin(), vector_.cend(), value, compare_);
113 return vector_.insert(it, std::forward<V>(value));
114 }
115
123 template <typename... Args>
124 iterator emplace(Args&&... args) {
125 return insert(T(std::forward<Args>(args)...));
126 }
127
137 template <typename... Args>
138 iterator emplace(bool insert_lower, Args&&... args) {
139 return insert(T(std::forward<Args>(args)...), insert_lower);
140 }
141
143 [[nodiscard]] size_t size() const { return vector_.size(); }
144
146 [[nodiscard]] bool empty() const { return vector_.empty(); }
147
154 const T& at(size_t i) const {
155 if (i >= vector_.size()) throw std::out_of_range("Index out of range");
156 return vector_[i];
157 }
158
167 const T& at(int i) const {
168 if (i < 0) i = static_cast<int>(vector_.size()) + i;
169 if (i < 0 || i >= static_cast<int>(vector_.size())) throw std::out_of_range("Index out of range");
170 return vector_[i];
171 }
172
178 const T& operator[](size_t i) const { return vector_[i]; }
179
184 const T& front() const { return vector_.front(); }
185
190 const T& back() const { return vector_.back(); }
191
200 bool erase(const const_iterator& it) {
201 if (it == vector_.end()) return false;
202 vector_.erase(it);
203 return true;
204 }
212 bool erase(size_t i) {
213 if (i >= vector_.size()) return false;
214 vector_.erase(vector_.begin() + i);
215 return true;
216 }
226 bool erase(int i) {
227 if (i < 0) i = static_cast<int>(vector_.size()) - i;
228 if (i < 0 || i >= static_cast<int>(vector_.size())) return false;
229 vector_.erase(vector_.begin() + i);
230 return true;
231 }
241 bool erase_value(const T& value) {
242 auto it = std::lower_bound(vector_.cbegin(), vector_.cend(), value, compare_);
243 if (it == vector_.cend() || !is_equal(*it, value)) return false;
244 vector_.erase(it);
245 return true;
246 }
247
256 const_iterator find(const T& value) const {
257 auto it = std::lower_bound(vector_.begin(), vector_.end(), value, compare_);
258 if (it == vector_.end() || !is_equal(*it, value)) return end();
259 return it;
260 }
261
270 const_iterator lower_bound(const T& value) const {
271 return std::lower_bound(vector_.begin(), vector_.end(), value, compare_);
272 }
273
282 const_iterator upper_bound(const T& value) const {
283 return std::upper_bound(vector_.begin(), vector_.end(), value, compare_);
284 }
285
293 [[nodiscard]] size_t count(const T& value) const {
294 auto it = find(value);
295 if (it == vector_.end()) return 0;
296 size_t count = 1;
297 for (it++; it != vector_.end() && is_equal(*it, value); ++it) ++count;
298 return count;
299 }
300
307 [[nodiscard]] bool contains(const T& value) const { return find(value) != end(); }
308
315 [[nodiscard]] const_iterator lesser_end(const T& value) const {
316 return std::lower_bound(vector_.begin(), vector_.end(), value, compare_);
317 }
318
325 [[nodiscard]] const_iterator greater_begin(const T& value) const {
326 auto it = std::upper_bound(vector_.begin(), vector_.end(), value, compare_);
327 while (it != vector_.cend() && is_equal(*it, value)) ++it;
328 return it;
329 }
330
332 void clear() { vector_.clear(); }
333
334 iterator begin() { return vector_.begin(); }
335 iterator end() { return vector_.end(); }
336 const_iterator begin() const { return vector_.cbegin(); }
337 const_iterator end() const { return vector_.cend(); }
338 const_iterator cbegin() const { return vector_.begin(); }
339 const_iterator cend() const { return vector_.end(); }
340 reverse_iterator rbegin() { return vector_.rbegin(); }
341 reverse_iterator rend() { return vector_.rend(); }
342 const_reverse_iterator rbegin() const { return vector_.crbegin(); }
343 const_reverse_iterator rend() const { return vector_.crend(); }
344 const_reverse_iterator crbegin() const { return vector_.crbegin(); }
345 const_reverse_iterator crend() const { return vector_.crend(); }
346
347 private:
357 [[nodiscard]] inline bool is_equal(const T& lhs, const T& rhs) const {
358 return !compare_(lhs, rhs) && !compare_(rhs, lhs);
359 }
360
361 std::vector<T> vector_;
362 Compare compare_;
363};
364
365template <typename T>
366std::ostream& operator<<(std::ostream& os, const SortedVector<T>& it) {
367 std::copy(it.cbegin(), std::prev(it.cend()), std::ostream_iterator<T>(os, ", "));
368 return os << *(it.crbegin());
369}
370
371extern template class SortedVector<Bound>;
372extern template class SortedVector<int>;
373extern template class SortedVector<double>;
374
375} // namespace dlinear
Vector that maintains its elements sorted.
std::vector< T > vector_
Underlying vector to store the sorted list.
SortedVector()=default
Construct a new SortedVector object.
const T & at(size_t i) const
Access element at index i with bounds checking.
const T & operator[](size_t i) const
Direct access to the underlying vector.
typename std::vector< T >::reverse_iterator reverse_iterator
Type of the reverse iterator to the sorted list.
bool erase_value(const T &value)
Remove an element with the provided value from the sorted list.
const T & const_reference
Type of the const reference to an element in the sorted list.
const T & at(int i) const
Access element at index i with bounds checking.
const_iterator find(const T &value) const
Find the index of an element with the provided value in the sorted list.
Compare compare_
Comparison function to maintain the sorted order.
iterator insert(V &&value, bool insert_lower)
Insert an element with the provided value into the sorted list.
bool contains(const T &value) const
Check if the element with the provided value is contained in the vector.
SortedVector(size_t size)
Construct a new SortedVector object with the giver size.
bool erase(size_t i)
Remove the element at index i from the sorted list.
bool erase(const const_iterator &it)
Remove the element at position it from the sorted list.
const_iterator lesser_end(const T &value) const
Find the last element in the vector with a value lesser than value and return an iterator to the posi...
void clear()
Clear the sorted list, removing all elements.
size_t size() const
Get read-only access to the size of the vector.
const T * const_pointer
Type of the const pointer to an element in the sorted list.
const T & front() const
Reference to the first element in the sorted list.
T * pointer
Type of the pointer to an element in the sorted list.
const_iterator upper_bound(const T &value) const
Find the last position in which an element with the provided value could be inserted without changing...
bool is_equal(const T &lhs, const T &rhs) const
Use the compare_ function to check if two elements are equal.
std::ptrdiff_t difference_type
Type of the difference between two iterators.
const_iterator greater_begin(const T &value) const
Find the first element in the vector with a value greater than value and return an iterator to its po...
typename std::vector< T >::const_reverse_iterator const_reverse_iterator
Type of the const reverse iterator to the sorted list.
SortedVector(std::initializer_list< T > list)
Construct a new SortedVector object using an initializer list.
iterator insert(V &&value)
Insert an element with the provided value into the sorted list.
bool empty() const
Check whether the vector is emtpy.
bool erase(int i)
Remove the element at index i from the sorted list.
size_t count(const T &value) const
Count the number of occurrences of an element with the provided value in the sorted list.
iterator emplace(bool insert_lower, Args &&... args)
Emplace a new element into the sorted list.
iterator emplace(Args &&... args)
Emplace a new element into the sorted list.
typename std::vector< T >::const_iterator const_iterator
Type of the const iterator to the sorted list.
typename std::vector< T >::iterator iterator
Type of the iterator to the sorted list.
T & reference
Type of the reference to an element in the sorted list.
size_t size_type
Type of the size of the sorted list.
const T & back() const
Reference to the last element in the sorted list.
const_iterator lower_bound(const T &value) const
Find the first position in which an element with the provided value could be inserted without changin...
T value_type
Type of the elements in the sorted list.
Global namespace for the dlinear library.