|
using | value_type = T |
| Type of the elements in the sorted list.
|
|
using | size_type = size_t |
| Type of the size of the sorted list.
|
|
using | difference_type = std::ptrdiff_t |
| Type of the difference between two iterators.
|
|
using | reference = T& |
| Type of the reference to an element in the sorted list.
|
|
using | const_reference = const T& |
| Type of the const reference to an element in the sorted list.
|
|
using | pointer = T* |
| Type of the pointer to an element in the sorted list.
|
|
using | const_pointer = const T* |
| Type of the const pointer to an element in the sorted list.
|
|
using | iterator = typename std::vector<T>::iterator |
| Type of the iterator to the sorted list.
|
|
using | const_iterator = typename std::vector<T>::const_iterator |
| Type of the const iterator to the sorted list.
|
|
using | reverse_iterator |
| Type of the reverse iterator to the sorted list.
|
|
using | const_reverse_iterator |
| Type of the const reverse iterator to the sorted list.
|
|
|
| SortedVector ()=default |
| Construct a new SortedVector object.
|
|
| SortedVector (size_t size) |
| Construct a new SortedVector object with the giver size .
|
|
| SortedVector (std::initializer_list< T > list) |
| Construct a new SortedVector object using an initializer list .
|
|
template<typename V > |
iterator | insert (V &&value) |
| Insert an element with the provided value into the sorted list.
|
|
template<typename V > |
iterator | insert (V &&value, bool insert_lower) |
| Insert an element with the provided value into the sorted list.
|
|
template<typename... Args> |
iterator | emplace (Args &&... args) |
| Emplace a new element into the sorted list.
|
|
template<typename... Args> |
iterator | emplace (bool insert_lower, Args &&... args) |
| Emplace a new element into the sorted list.
|
|
size_t | size () const |
| Get read-only access to the size of the vector.
|
|
bool | empty () const |
| Check whether the vector is emtpy.
|
|
const T & | at (size_t i) const |
| Access element at index i with bounds checking.
|
|
const T & | at (int i) const |
| Access element at index i with bounds checking.
|
|
const T & | operator[] (size_t i) const |
| Direct access to the underlying vector.
|
|
const T & | front () const |
| Reference to the first element in the sorted list.
|
|
const T & | back () const |
| Reference to the last element in the sorted list.
|
|
bool | erase (const const_iterator &it) |
| Remove the element at position it from the sorted list.
|
|
bool | erase (size_t i) |
| Remove the element at index i from the sorted list.
|
|
bool | erase (int i) |
| Remove the element at index i from the sorted list.
|
|
bool | erase_value (const T &value) |
| Remove an element with the provided value from the sorted list.
|
|
const_iterator | find (const T &value) const |
| Find the index of an element with the provided value 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 changing the ordering.
|
|
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 the ordering.
|
|
size_t | count (const T &value) const |
| Count the number of occurrences of an element with the provided value in the sorted list.
|
|
bool | contains (const T &value) const |
| Check if the element with the provided value is contained in the vector.
|
|
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 position after that one.
|
|
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 position.
|
|
void | clear () |
| Clear the sorted list, removing all elements.
|
|
template<class T, class Compare = std::less<T>>
class dlinear::SortedVector< T, Compare >
Vector that maintains its elements sorted.
This class is implemented as a wrapper around std::vector to provide a sorted list of elements. Each time an element is inserted, it is placed in the correct position to maintain the sorted order.
for (const auto& value : sorted_vector) {
std::cout << value << " ";
}
Vector that maintains its elements sorted.
iterator insert(V &&value)
Insert an element with the provided value into the sorted list.
Using a custom comparison function is also supported:
for (const auto& value : sorted_vector) {
std::cout << value << " ";
}
- Template Parameters
-
T | type of the elements in the sorted list |
Compare | comparison function to maintain the sorted order |
Definition at line 49 of file SortedVector.hpp.
template<class T , class Compare = std::less<T>>
Access element at index i
with bounds checking.
It also supports negative indices, where -1 is the last element, -2 is the second to last, and so on.
- Parameters
-
i | position of the element to access (negative indices are supported) |
- Returns
- element at the given position
- Exceptions
-
std::out_of_range | if i is out of range |
Definition at line 167 of file SortedVector.hpp.
template<class T , class Compare = std::less<T>>
Count the number of occurrences of an element with the provided value
in the sorted list.
If the element is not found, 0 is returned.
- Parameters
-
value | value of the element to count |
- Returns
- number of occurrences of the element in the sorted list
Definition at line 293 of file SortedVector.hpp.
template<class T , class Compare = std::less<T>>
template<typename... Args>
Emplace a new element into the sorted list.
The arguments are forwarded to the constructor of the element type. The element is placed in the correct position to maintain the sorted order.
- Parameters
-
args | arguments to forward to the constructor of the element type |
Definition at line 124 of file SortedVector.hpp.
template<class T , class Compare = std::less<T>>
template<typename... Args>
Emplace a new element into the sorted list.
The arguments are forwarded to the constructor of the element type. The element is placed in the correct position to maintain the sorted order. This version allows to specify if the element should be inserted in the lower or upper bound.
- Parameters
-
insert_lower | if true, the element is inserted in the lower bound, otherwise in the upper bound |
args | arguments to forward to the constructor of the element type |
Definition at line 138 of file SortedVector.hpp.
template<class T , class Compare = std::less<T>>
Remove the element at index i
from the sorted list.
It also supports negative indices, where -1 is the last element, -2 is the second to last, and so on. If the index is out of range, false is returned.
- Parameters
-
i | index of the element to remove |
- Returns
- true if the element has been removed
-
false if the element was not found
Definition at line 226 of file SortedVector.hpp.
template<class T , class Compare = std::less<T>>
Remove an element with the provided value
from the sorted list.
If multiple elements have the same value
, only the first one is removed. If the element is not found, false is returned.
- Parameters
-
value | value of the element to remove |
- Returns
- true if the element has been removed
-
false if the element was not found
Definition at line 241 of file SortedVector.hpp.
template<class T , class Compare = std::less<T>>
template<typename V >
Insert an element with the provided value
into the sorted list.
The element is placed in the correct position to maintain the sorted order. It returns an iterator to the inserted element. This version allows to specify if the element should be inserted in the lower or upper bound.
- Template Parameters
-
V | type of the element to insert |
- Parameters
-
value | value of the element to insert |
insert_lower | if true, the element is inserted in the lower bound, otherwise in the upper bound |
- Returns
- iterator to the inserted element
Definition at line 110 of file SortedVector.hpp.
template<class T , class Compare = std::less<T>>
Use the compare_ function to check if two elements are equal.
Since the function only checks ordering, to make sure two elements are equal compare_ must be used twice.
- Parameters
-
lhs | left-hand side element |
rhs | right-hand side element |
- Returns
- true if the elements are equal
-
false if the elements are not equal
Definition at line 357 of file SortedVector.hpp.