LAL: Linear Arrangement Library 24.10.00
A library focused on algorithms on linear arrangements of graphs.
Loading...
Searching...
No Matches
sorted_vector.hpp
1/*********************************************************************
2 *
3 * Linear Arrangement Library - A library that implements a collection
4 * algorithms for linear arrangments of graphs.
5 *
6 * Copyright (C) 2019 - 2024
7 *
8 * This file is part of Linear Arrangement Library. The full code is available
9 * at:
10 * https://github.com/LAL-project/linear-arrangement-library.git
11 *
12 * Linear Arrangement Library is free software: you can redistribute it
13 * and/or modify it under the terms of the GNU Affero General Public License
14 * as published by the Free Software Foundation, either version 3 of the
15 * License, or (at your option) any later version.
16 *
17 * Linear Arrangement Library is distributed in the hope that it will be
18 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Affero General Public License for more details.
21 *
22 * You should have received a copy of the GNU Affero General Public License
23 * along with Linear Arrangement Library. If not, see <http://www.gnu.org/licenses/>.
24 *
25 * Contact:
26 *
27 * LluĂ­s Alemany Puig (lluis.alemany.puig@upc.edu)
28 * LQMC (Quantitative, Mathematical, and Computational Linguisitcs)
29 * CQL (Complexity and Quantitative Linguistics Lab)
30 * Jordi Girona St 1-3, Campus Nord UPC, 08034 Barcelona. CATALONIA, SPAIN
31 * Webpage: https://cqllab.upc.edu/people/lalemany/
32 *
33 * Ramon Ferrer i Cancho (rferrericancho@cs.upc.edu)
34 * LQMC (Quantitative, Mathematical, and Computational Linguisitcs)
35 * CQL (Complexity and Quantitative Linguistics Lab)
36 * Office 220, Omega building
37 * Jordi Girona St 1-3, Campus Nord UPC, 08034 Barcelona. CATALONIA, SPAIN
38 * Webpage: https://cqllab.upc.edu/people/rferrericancho/
39 *
40 ********************************************************************/
41
42#pragma once
43
44// C++ includes
45#if defined DEBUG
46#include <cassert>
47#endif
48#include <algorithm>
49#include <vector>
50
51namespace lal {
52namespace detail {
53namespace sorting {
54
64template <class T, bool unique, typename allocator_t = std::allocator<T> >
65class sorted_vector : public std::vector<T, allocator_t> {
66public:
67 using iterator_t = typename std::vector<T, allocator_t>::iterator;
68
70 sorted_vector() noexcept : std::vector<T, allocator_t>() { }
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) { }
75
77 sorted_vector(const sorted_vector& v) noexcept = default;
79 sorted_vector(sorted_vector&& v) noexcept = default;
80
82 [[nodiscard]] sorted_vector& operator= (const sorted_vector& v) noexcept = default;
84 [[nodiscard]] sorted_vector& operator= (sorted_vector&& v) noexcept = default;
85
87 ~sorted_vector() noexcept = default;
88
94 iterator_t insert_sorted(const T& x) noexcept {
95 if constexpr (not unique) {
96 const auto it = std::upper_bound(this->begin(), this->end(), x);
97 return this->insert(it, x);
98 }
99 else {
100 if (this->size() == 0) {
101 this->push_back(x);
102 return this->begin();
103 }
104
105 const auto it = upper_bound(this->begin(), this->end(), x);
106 const auto pre_it = it == this->begin() ? it : it - 1;
107 if (*pre_it != x) {
108 return this->insert(it, x);
109 }
110 return it;
111 }
112 }
118 iterator_t insert_sorted(T&& x) noexcept {
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));
122 }
123 else {
124 if (this->size() == 0) {
125 this->push_back(std::forward<T>(x));
126 return this->begin();
127 }
128
129 const auto it = std::upper_bound(this->begin(), this->end(), x);
130 const auto pre_it = it == this->begin() ? it : it - 1;
131 if (*pre_it != x) {
132 return this->insert(it, std::forward<T>(x));
133 }
134 return it;
135 }
136 }
137
143 iterator_t remove_sorted(const T& x) noexcept {
144#if defined DEBUG
145 assert(contains(x));
146#endif
147 const auto i1 = std::lower_bound(this->begin(), this->end(), x);
148 return this->erase(i1);
149 }
150
158 [[nodiscard]] bool contains(const T& x) const noexcept {
159 return std::binary_search(this->begin(), this->end(), x);
160 }
161
168 [[nodiscard]] iterator_t find_sorted(const T& x) noexcept {
169 const auto i = std::lower_bound(this->begin(), this->end(), x);
170 if (i == this->end()) { return this->end(); }
171
172 if (*i == x) { return i; }
173 return this->end();
174 }
175};
176
177} // -- namespace sorting
178} // -- namespace detail
179} // -- namespace lal
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