LAL: Linear Arrangement Library 24.10.00
A library focused on algorithms on linear arrangements of graphs.
Loading...
Searching...
No Matches
set_array.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// lal includes
45#include <lal/detail/array.hpp>
46
47namespace lal {
48namespace detail {
49
104template <typename value_t, class indexer_t = value_t>
106public:
107
113 template <
114 typename vt = value_t, typename it = indexer_t,
115 std::enable_if_t< std::is_same_v<vt, it>, bool > = true
116 >
117 void init(const std::size_t max_num_elems, const std::size_t max_index_value) noexcept
118 {
119 m_size = 0;
120 m_values.resize(max_num_elems);
121 m_exists.resize(max_index_value, NOT_EXISTS);
122 m_position.resize(max_index_value);
123 }
124
131 template <
132 typename vt = value_t, typename it = indexer_t,
133 std::enable_if_t< not std::is_same_v<vt, it>, bool > = true
134 >
135 void init(
136 const std::size_t max_num_elems,
137 const std::size_t max_index_value,
138 indexer_t&& i
139 )
140 noexcept
141 {
142 m_I = std::move(i);
143 m_size = 0;
144 m_values.resize(max_num_elems);
145 m_exists.resize(max_index_value, NOT_EXISTS);
146 m_position.resize(max_index_value);
147 }
148
154 [[nodiscard]] const value_t& operator[] (const std::size_t i) const noexcept {
155#if defined DEBUG
156 assert(i < size());
157#endif
158 return m_values[i];
159 }
160
162 [[nodiscard]] std::size_t capacity() const noexcept { return m_exists.size(); }
164 [[nodiscard]] std::size_t size() const noexcept { return m_size; }
165
167 [[nodiscard]] bool exists(const value_t& v) const noexcept
168 { return m_exists[index(v)] == EXISTS; }
169
171 [[nodiscard]] std::size_t position(const value_t& v) const noexcept
172 { return m_position[index(v)]; }
173
175 void add(const value_t& v) noexcept {
176 const std::size_t idx_v = index(v);
177
178 if (not m_exists[idx_v]) {
179 m_exists[idx_v] = EXISTS;
180
181#if defined DEBUG
182 assert(m_size < m_values.size());
183#endif
184
185 m_position[idx_v] = m_size;
186 m_values[m_size++] = std::move(v);
187 }
188 }
189
191 void add(value_t&& v) noexcept {
192 const std::size_t idx_v = index(v);
193
194 if (not m_exists[idx_v]) {
195 m_exists[idx_v] = EXISTS;
196
197#if defined DEBUG
198 assert(m_size < m_values.size());
199#endif
200
201 m_position[idx_v] = m_size;
202 m_values[m_size++] = std::move(v);
203 }
204 }
205
207 void remove(const value_t& v) noexcept {
208 const std::size_t idx_v = index(v);
209 if (m_exists[idx_v]) {
210
211 m_exists[idx_v] = NOT_EXISTS;
212
213#if defined DEBUG
214 assert(m_size > 0);
215#endif
216
217 const auto pos_v = m_position[idx_v];
218 const auto idx_last_value = index(m_values[m_size - 1]);
219
220#if defined DEBUG
221 assert(m_position[idx_last_value] == m_size - 1);
222#endif
223
224 std::swap(m_position[idx_v], m_position[idx_last_value]);
225 std::swap(m_values[pos_v], m_values[m_size - 1]);
226
227 --m_size;
228 }
229 }
230
232 [[nodiscard]] const value_t* begin_values() const noexcept { return m_values.begin(); }
234 [[nodiscard]] value_t* begin_values() noexcept { return m_values.begin(); }
236 [[nodiscard]] const value_t* end_values() const noexcept { return m_values.end(); }
238 [[nodiscard]] value_t* end_values() noexcept { return m_values.end(); }
239
241 [[nodiscard]] const value_t* begin_position() const noexcept { return m_position.begin(); }
243 [[nodiscard]] value_t* begin_position() noexcept { return m_position.begin(); }
245 [[nodiscard]] const value_t* end_position() const noexcept { return m_position.end(); }
247 [[nodiscard]] value_t* end_position() noexcept { return m_position.end(); }
248
249private:
251 static constexpr char EXISTS = 1;
253 static constexpr char NOT_EXISTS = 0;
254
255private:
257 indexer_t m_I;
258
262 std::size_t m_size;
263
272
273private:
275 [[nodiscard]] std::size_t index(const value_t& v) const noexcept {
276 if constexpr (not std::is_same_v<value_t, indexer_t>) {
277 return m_I(v);
278 }
279 else {
280 return v;
281 }
282 }
283};
284
285
286} // -- namespace detail
287} // -- namespace lal
A set-like data structure implemented with an array.
Definition set_array.hpp:105
void add(value_t &&v) noexcept
Add a new element to the set.
Definition set_array.hpp:191
array< char > m_exists
Does a value exist in the set?
Definition set_array.hpp:265
value_t * begin_values() noexcept
Begin iterator to m_values.
Definition set_array.hpp:234
array< value_t > m_values
The unique values in this set.
Definition set_array.hpp:260
std::size_t position(const value_t &v) const noexcept
Where is an element located?
Definition set_array.hpp:171
void init(const std::size_t max_num_elems, const std::size_t max_index_value, indexer_t &&i) noexcept
Initialize the set with an indexer object.
Definition set_array.hpp:135
value_t * begin_position() noexcept
Begin iterator to m_position.
Definition set_array.hpp:243
array< std::size_t > m_position
The position of every value in the set.
Definition set_array.hpp:271
const value_t * begin_values() const noexcept
Begin iterator to m_values.
Definition set_array.hpp:232
void init(const std::size_t max_num_elems, const std::size_t max_index_value) noexcept
Initialize the set with no indexer object.
Definition set_array.hpp:117
value_t * end_values() noexcept
End iterator to m_values.
Definition set_array.hpp:238
const value_t * end_position() const noexcept
End iterator to m_position.
Definition set_array.hpp:245
std::size_t capacity() const noexcept
Maximum size of this set.
Definition set_array.hpp:162
const value_t & operator[](const std::size_t i) const noexcept
Operator to access values in a given position.
Definition set_array.hpp:154
bool exists(const value_t &v) const noexcept
Does an element exist?
Definition set_array.hpp:167
std::size_t m_size
The number of values in m_values.
Definition set_array.hpp:262
value_t * end_position() noexcept
End iterator to m_position.
Definition set_array.hpp:247
void remove(const value_t &v) noexcept
Remove an element from the set.
Definition set_array.hpp:207
void add(const value_t &v) noexcept
Add a new element to the set.
Definition set_array.hpp:175
static constexpr char EXISTS
Does an element exist in the set?
Definition set_array.hpp:251
indexer_t m_I
The indexer object.
Definition set_array.hpp:257
const value_t * end_values() const noexcept
End iterator to m_values.
Definition set_array.hpp:236
std::size_t index(const value_t &v) const noexcept
Calculate the index of an element using the indexer object m_I.
Definition set_array.hpp:275
const value_t * begin_position() const noexcept
Begin iterator to m_position.
Definition set_array.hpp:241
static constexpr char NOT_EXISTS
Does an element not exist in the set?
Definition set_array.hpp:253
std::size_t size() const noexcept
Actual size of this set.
Definition set_array.hpp:164
Main namespace of the library.
Definition basic_types.hpp:48
Wrapper of a C array for automatic deallocation of memory.
Definition array.hpp:59
T * begin() noexcept
Non-constant raw pointer to first element.
Definition array.hpp:300
T * end() noexcept
Non-constant raw pointer to last+1 element.
Definition array.hpp:302
std::size_t size() const noexcept
Size of the array.
Definition array.hpp:215
void resize(const std::size_t new_size) noexcept
Resize the array.
Definition array.hpp:187