LAL: Linear Arrangement Library 21.07.01
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 - 2021
7 *
8 * This file is part of Linear Arrangement Library. To see the full code
9 * visit the webpage:
10 * https://github.com/lluisalemanypuig/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 (lalemany@cs.upc.edu)
28 * LARCA (Laboratory for Relational Algorithmics, Complexity and Learning)
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 * LARCA (Laboratory for Relational Algorithmics, Complexity and Learning)
35 * CQL (Complexity and Quantitative Linguistics Lab)
36 * Office S124, 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 {
52
53template<class T, bool unique>
54class sorted_vector : public std::vector<T> {
55public:
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) { }
59
60 sorted_vector(const sorted_vector& v) : std::vector<T>(v) { }
61 sorted_vector(sorted_vector&& v) : std::vector<T>(std::move(v)) { }
62
63 sorted_vector& operator= (const sorted_vector& v) { *this = v; }
64 sorted_vector& operator= (sorted_vector&& v) { *this = std::move(v); }
65
66 ~sorted_vector() { }
67
68 // insert sorted, with copies
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);
74 }
75 else {
76 if (this->size() == 0) {
77 this->push_back(x);
78 return this->begin();
79 }
80
81 const auto it = upper_bound(this->begin(), this->end(), x);
82 const auto pre_it = it == this->begin() ? it : it - 1;
83 if (*pre_it != x) {
84 return this->insert(it, x);
85 }
86 return it;
87 }
88 }
89
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));
95 }
96 else {
97 if (this->size() == 0) {
98 this->push_back(std::move(x));
99 return this->begin();
100 }
101
102 const auto it = std::upper_bound(this->begin(), this->end(), x);
103 const auto pre_it = it == this->begin() ? it : it - 1;
104 if (*pre_it != x) {
105 return this->insert(it, std::move(x));
106 }
107 return it;
108 }
109 }
110
111 // remove an element, assuming that the element is sorted
112 inline typename std::vector<T>::iterator
113 remove_sorted(const T& x) {
114#if defined DEBUG
115 assert(contains(x));
116#endif
117 const auto i1 = std::lower_bound(this->begin(), this->end(), x);
118 return this->erase(i1);
119 }
120
121 // returns whether the vector contains an element
122 inline bool contains(const T& x) const {
123 return std::binary_search(this->begin(), this->end(), x);
124 }
125
126 // find the position of an element in logarithmic time
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(); }
131
132 if (*i == x) { return i; }
133 return this->end();
134 }
135};
136
137} // -- namespace lal
Main namespace of the library.
Definition definitions.hpp:48