LAL: Linear Arrangement Library 24.10.00
A library focused on algorithms on linear arrangements of graphs.
Loading...
Searching...
No Matches
aggregations.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 <iterator>
47#include <cassert>
48#endif
49#include <cstddef>
50
51namespace lal {
52namespace utilities {
53
131template <
132 typename result_t, bool second_set_empty,
133 typename iterator_first_t, typename iterator_second_t,
134 class metric,
135 class accumulate_Q, class accumulate_R,
136 class make_average_Q, class make_average_R,
137 class make_average
138>
139[[nodiscard]] result_t one_level_aggregation
140(
141 iterator_first_t bfirst,
142 const iterator_first_t efirst,
143 iterator_second_t bsecond,
144 [[maybe_unused]] const iterator_second_t esecond,
145 metric values,
146 accumulate_Q accQ,
147 accumulate_R accR,
148 make_average_Q avgQ,
149 make_average_R avgR,
150 make_average avg
151)
152noexcept
153{
154 if constexpr (second_set_empty) {
155 // no elements from the second set
156 auto [total_Q, total_R] = values(*bfirst);
157 std::size_t amount = 1;
158
159 ++bfirst;
160 for (; bfirst != efirst; ++bfirst, ++amount) {
161 const auto [Qi, Ri] = values(*bfirst);
162 accQ(total_Q, Qi);
163 accR(total_R, Ri);
164 }
165
166 return avg(avgQ(total_Q, amount), avgR(total_R, amount));
167 }
168 else {
169#if defined DEBUG
170 // ensure same number of elements in both sets
171 assert(std::distance(bfirst, efirst) == std::distance(bsecond, esecond));
172#endif
173
174 auto [total_Q, total_R] = values(*bfirst, *bsecond);
175 std::size_t amount = 1;
176
177 ++bfirst; ++bsecond;
178 for (; bfirst != efirst; ++bfirst, ++bsecond, ++amount) {
179 const auto [Qi, Ri] = values(*bfirst, *bsecond);
180 accQ(total_Q, Qi);
181 accR(total_R, Ri);
182 }
183
184 return avg(avgQ(total_Q, amount), avgR(total_R, amount));
185 }
186}
187
256template <
257 typename result_t, bool second_set_empty,
258 typename iterator_first_t,
259 typename iterator_second_t,
260 class metric, class combine, class accumulate, class make_average
261>
262[[nodiscard]] result_t two_level_aggregation
263(
264 iterator_first_t bfirst,
265 const iterator_first_t efirst,
266 iterator_second_t bsecond,
267 [[maybe_unused]] const iterator_second_t esecond,
268 metric values,
269 combine comb_values,
270 accumulate acc_values,
271 make_average avg
272)
273noexcept
274{
275 if constexpr (second_set_empty) {
276 // no elements from the second set
277 auto total = comb_values(values(*bfirst));
278 std::size_t amount = 1;
279
280 ++bfirst;
281 for (; bfirst != efirst; ++bfirst, ++amount) {
282 acc_values(total, comb_values(values(*bfirst)));
283 }
284 return avg(total, amount);
285 }
286 else {
287#if defined DEBUG
288 // ensure same of elements in both sets
289 assert(std::distance(bfirst, efirst) == std::distance(bsecond, esecond));
290#endif
291 auto total = comb_values(values(*bfirst, *bsecond));
292 std::size_t amount = 1;
293
294 ++bfirst; ++bsecond;
295 for (; bfirst != efirst; ++bfirst, ++bsecond, ++amount) {
296 acc_values(total, comb_values(values(*bfirst, *bsecond)));
297 }
298 return avg(total, amount);
299 }
300}
301
302} // -- namespace utilities
303} // -- namespace lal
result_t one_level_aggregation(iterator_first_t bfirst, const iterator_first_t efirst, iterator_second_t bsecond, const iterator_second_t esecond, metric values, accumulate_Q accQ, accumulate_R accR, make_average_Q avgQ, make_average_R avgR, make_average avg) noexcept
Computation of 1-level aggregation of and .
Definition aggregations.hpp:140
result_t two_level_aggregation(iterator_first_t bfirst, const iterator_first_t efirst, iterator_second_t bsecond, const iterator_second_t esecond, metric values, combine comb_values, accumulate acc_values, make_average avg) noexcept
Computation of 2-level aggregation of and .
Definition aggregations.hpp:263
Main namespace of the library.
Definition basic_types.hpp:48