-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.hpp
More file actions
651 lines (528 loc) · 23.3 KB
/
graph.hpp
File metadata and controls
651 lines (528 loc) · 23.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
// Copyright (c) 2024 Jakub Musiał
// This file is part of the CPP-GL project (https://github.com/SpectraL519/cpp-gl).
// Licensed under the MIT License. See the LICENSE file in the project root for full license information.
#pragma once
#include "constants.hpp"
#include "graph_traits.hpp"
#include "impl/impl_tags.hpp"
#include "io/stream_options_manipulator.hpp"
#include "types/iterator_range.hpp"
#include <set>
namespace gl {
template <type_traits::c_instantiation_of<graph_traits> GraphTraits = graph_traits<>>
class graph final {
public:
using traits_type = GraphTraits;
using implementation_tag = typename traits_type::implementation_tag;
using implementation_type = typename implementation_tag::template type<traits_type>;
using vertex_type = typename traits_type::vertex_type;
using vertex_ptr_type = typename traits_type::vertex_ptr_type;
using vertex_properties_type = typename traits_type::vertex_properties_type;
using vetex_list_type = std::vector<vertex_ptr_type>;
using vertex_iterator_type =
types::dereferencing_iterator<typename vetex_list_type::const_iterator>;
// TODO: reverese iterators should be available for bidirectional ranges
using edge_type = typename traits_type::edge_type;
using edge_ptr_type = typename traits_type::edge_ptr_type;
using edge_directional_tag = typename traits_type::edge_directional_tag;
using edge_properties_type = typename traits_type::edge_properties_type;
using edge_list_type = typename implementation_type::edge_list_type;
using edge_iterator_type = typename implementation_type::edge_iterator_type;
graph(const graph&) = delete;
graph& operator=(const graph&) = delete;
graph() = default;
graph(const types::size_type n_vertices) : _impl(n_vertices) {
this->_vertices.reserve(n_vertices);
for (auto vertex_id = constants::initial_id; vertex_id < n_vertices; ++vertex_id)
this->_vertices.push_back(detail::make_vertex<vertex_type>(vertex_id));
}
graph(graph&&) = default;
graph& operator=(graph&&) = default;
~graph() = default;
// --- general methods ---
[[nodiscard]] gl_attr_force_inline types::size_type n_vertices() const {
return this->_vertices.size();
}
[[nodiscard]] gl_attr_force_inline types::size_type n_unique_edges() const {
return this->_impl.n_unique_edges();
}
// --- vertex methods ---
[[nodiscard]] gl_attr_force_inline types::iterator_range<vertex_iterator_type> vertices(
) const {
return make_iterator_range(deref_cbegin(this->_vertices), deref_cend(this->_vertices));
}
[[nodiscard]] gl_attr_force_inline std::ranges::iota_view<types::id_type, types::id_type>
vertex_ids() const {
return std::views::iota(constants::initial_id, this->n_vertices());
}
// clang-format off
// gl_attr_force_inline misplacement
[[nodiscard]] gl_attr_force_inline const vertex_type& get_vertex(
const types::id_type vertex_id
) const {
this->_verify_vertex_id(vertex_id);
return *this->_vertices[vertex_id];
}
// clang-format on
[[nodiscard]] gl_attr_force_inline bool has_vertex(const types::id_type vertex_id) const {
return vertex_id < this->n_vertices();
}
[[nodiscard]] gl_attr_force_inline bool has_vertex(const vertex_type& vertex) const {
return this->has_vertex(vertex.id()) and &vertex == this->_vertices[vertex.id()].get();
}
const vertex_type& add_vertex() {
this->_impl.add_vertex();
this->_vertices.push_back(detail::make_vertex<vertex_type>(this->n_vertices()));
return *this->_vertices.back();
}
const vertex_type& add_vertex(const vertex_properties_type& properties)
requires(not type_traits::is_default_properties_type_v<vertex_properties_type>)
{
this->_impl.add_vertex();
this->_vertices.push_back(detail::make_vertex<vertex_type>(this->n_vertices(), properties));
return *this->_vertices.back();
}
void add_vertices(const types::size_type n) {
this->_impl.add_vertices(n);
this->_vertices.reserve(this->n_vertices() + n);
for (types::size_type _ = constants::begin_idx; _ < n; ++_)
this->_vertices.push_back(detail::make_vertex<vertex_type>(this->n_vertices()));
}
template <type_traits::c_sized_range_of<vertex_properties_type> VertexPropertiesRange>
void add_vertices_with(const VertexPropertiesRange& properties_range) {
const auto n = std::ranges::size(properties_range);
this->_impl.add_vertices(n);
this->_vertices.reserve(this->n_vertices() + n);
for (const auto& properties : properties_range)
this->_vertices.push_back(
detail::make_vertex<vertex_type>(this->n_vertices(), properties)
);
}
gl_attr_force_inline void remove_vertex(const types::size_type vertex_id) {
this->_remove_vertex_impl(this->get_vertex(vertex_id));
}
inline void remove_vertex(const vertex_type& vertex) {
this->_verify_vertex(vertex);
this->_remove_vertex_impl(vertex);
}
template <type_traits::c_sized_range_of<types::id_type> IdRange>
void remove_vertices_from(const IdRange& vertex_id_range) {
// sorts the ids in a descending order and removes duplicate ids
std::set<types::id_type, std::greater<types::id_type>> vertex_id_set(
std::ranges::begin(vertex_id_range), std::ranges::end(vertex_id_range)
);
// TODO: optimize
for (const auto vertex_id : vertex_id_set)
this->_remove_vertex_impl(this->get_vertex(vertex_id));
}
template <type_traits::c_sized_range_of<types::const_ref_wrap<vertex_type>> VertexRefRange>
void remove_vertices_from(const VertexRefRange& vertex_ref_range) {
// TODO [C++26]: replace with std::greater
struct vertex_ref_greater_comparator {
[[nodiscard]] bool operator()(
const types::const_ref_wrap<vertex_type>& lhs,
const types::const_ref_wrap<vertex_type>& rhs
) const {
return lhs.get() > rhs.get();
}
};
// sorts the ids in a descending order and removes duplicate ids
std::set<types::const_ref_wrap<vertex_type>, vertex_ref_greater_comparator> vertex_ref_set(
std::ranges::begin(vertex_ref_range), std::ranges::end(vertex_ref_range)
);
// TODO: optimize
for (const auto& vertex_ref : vertex_ref_set)
this->_remove_vertex_impl(vertex_ref.get());
}
[[nodiscard]] gl_attr_force_inline types::size_type in_degree(const vertex_type& vertex) const {
this->_verify_vertex(vertex);
return this->_impl.in_degree(vertex.id());
}
[[nodiscard]] gl_attr_force_inline types::size_type in_degree(const types::id_type vertex_id
) const {
this->_verify_vertex_id(vertex_id);
return this->_impl.in_degree(vertex_id);
}
[[nodiscard]] gl_attr_force_inline std::vector<types::size_type> in_degree_map() const {
return this->_impl.in_degree_map();
}
[[nodiscard]] gl_attr_force_inline types::size_type out_degree(const vertex_type& vertex
) const {
this->_verify_vertex(vertex);
return this->_impl.out_degree(vertex.id());
}
[[nodiscard]] gl_attr_force_inline types::size_type out_degree(const types::id_type vertex_id
) const {
this->_verify_vertex_id(vertex_id);
return this->_impl.out_degree(vertex_id);
}
[[nodiscard]] gl_attr_force_inline std::vector<types::size_type> out_degree_map() const {
return this->_impl.out_degree_map();
}
[[nodiscard]] gl_attr_force_inline types::size_type degree(const vertex_type& vertex) const {
this->_verify_vertex(vertex);
return this->_impl.degree(vertex.id());
}
[[nodiscard]] gl_attr_force_inline types::size_type degree(const types::id_type vertex_id
) const {
this->_verify_vertex_id(vertex_id);
return this->_impl.degree(vertex_id);
}
[[nodiscard]] gl_attr_force_inline std::vector<types::size_type> degree_map() const {
return this->_impl.degree_map();
}
// --- edge methods ---
// clang-format off
// gl_attr_force_inline misplacement
const edge_type& add_edge(
const types::id_type first_id, const types::id_type second_id
) {
this->_verify_vertex_id(first_id);
this->_verify_vertex_id(second_id);
return this->_impl.add_edge(
detail::make_edge<edge_type>(this->get_vertex(first_id), this->get_vertex(second_id))
);
}
const edge_type& add_edge(
const types::id_type first_id,
const types::id_type second_id,
const edge_properties_type& properties
)
requires(not type_traits::is_default_properties_type_v<edge_properties_type>)
{
this->_verify_vertex_id(first_id);
this->_verify_vertex_id(second_id);
return this->_impl.add_edge(detail::make_edge<edge_type>(
this->get_vertex(first_id), this->get_vertex(second_id), properties
));
}
// clang-format on
const edge_type& add_edge(const vertex_type& first, const vertex_type& second) {
this->_verify_vertex(first);
this->_verify_vertex(second);
return this->_impl.add_edge(detail::make_edge<edge_type>(first, second));
}
const edge_type& add_edge(
const vertex_type& first, const vertex_type& second, const edge_properties_type& properties
)
requires(not type_traits::is_default_properties_type_v<edge_properties_type>)
{
this->_verify_vertex(first);
this->_verify_vertex(second);
return this->_impl.add_edge(detail::make_edge<edge_type>(first, second, properties));
}
template <type_traits::c_sized_range_of<types::id_type> IdRange>
void add_edges_from(const types::id_type source_id, const IdRange& target_id_range) {
const auto& source = this->get_vertex(source_id);
std::vector<edge_ptr_type> new_edges;
new_edges.reserve(std::ranges::size(target_id_range));
for (const auto target_id : target_id_range) {
new_edges.push_back(detail::make_edge<edge_type>(source, this->get_vertex(target_id)));
}
this->_impl.add_edges_from(source_id, std::move(new_edges));
}
template <type_traits::c_sized_range_of<types::const_ref_wrap<vertex_type>> VertexRefRange>
void add_edges_from(const vertex_type& source, const VertexRefRange& target_range) {
this->_verify_vertex(source);
std::vector<edge_ptr_type> new_edges;
new_edges.reserve(std::ranges::size(target_range));
for (const auto& target_ref : target_range) {
const auto& target = target_ref.get();
this->_verify_vertex(target);
new_edges.push_back(detail::make_edge<edge_type>(source, target));
}
this->_impl.add_edges_from(source.id(), std::move(new_edges));
}
[[nodiscard]] gl_attr_force_inline bool has_edge(
const types::id_type first_id, const types::id_type second_id
) const {
return this->_impl.has_edge(first_id, second_id);
}
[[nodiscard]] bool has_edge(const vertex_type& first, const vertex_type& second) const {
this->_verify_vertex(first);
this->_verify_vertex(second);
return this->has_edge(first.id(), second.id());
}
[[nodiscard]] gl_attr_force_inline bool has_edge(const edge_type& edge) const {
return this->_impl.has_edge(edge);
}
[[nodiscard]] gl_attr_force_inline types::optional_cref<edge_type> get_edge(
const types::id_type first_id, const types::id_type second_id
) const {
return this->_impl.get_edge(first_id, second_id);
}
[[nodiscard]] types::optional_cref<edge_type> get_edge(
const vertex_type& first, const vertex_type& second
) const {
if (not (this->has_vertex(first) and this->has_vertex(second)))
return std::nullopt;
// TODO: optimize this so that the vertex ids are not checked twice
return this->_impl.get_edge(first.id(), second.id());
}
[[nodiscard]] inline std::vector<types::const_ref_wrap<edge_type>> get_edges(
const types::id_type first_id, const types::id_type second_id
) const {
using edge_ref_set = std::vector<types::const_ref_wrap<edge_type>>;
if constexpr (std::same_as<implementation_tag, impl::list_t>) {
return this->_impl.get_edges(first_id, second_id);
}
else {
const auto edge_opt = this->_impl.get_edge(first_id, second_id);
return edge_opt.has_value() ? edge_ref_set{edge_opt.value()} : edge_ref_set{};
}
}
[[nodiscard]] std::vector<types::const_ref_wrap<edge_type>> get_edges(
const vertex_type& first, const vertex_type& second
) const {
this->_verify_vertex(first);
this->_verify_vertex(second);
return this->get_edges(first.id(), second.id());
}
gl_attr_force_inline void remove_edge(const edge_type& edge) {
this->_impl.remove_edge(edge);
}
template <type_traits::c_range_of<types::const_ref_wrap<edge_type>> EdgeRefRange>
inline void remove_edges_from(const EdgeRefRange edges) {
for (const auto& edge_ref : edges)
this->_impl.remove_edge(edge_ref.get());
}
[[nodiscard]] inline types::iterator_range<edge_iterator_type> adjacent_edges(
const types::id_type vertex_id
) const {
this->_verify_vertex_id(vertex_id);
return this->_impl.adjacent_edges(vertex_id);
}
[[nodiscard]] inline types::iterator_range<edge_iterator_type> adjacent_edges(
const vertex_type& vertex
) const {
this->_verify_vertex(vertex);
return this->_impl.adjacent_edges(vertex.id());
}
// --- incidence methods ---
[[nodiscard]] bool are_incident(const types::id_type first_id, const types::id_type second_id)
const {
this->_verify_vertex_id(first_id);
if (first_id == second_id)
return true;
this->_verify_vertex_id(second_id);
if constexpr (type_traits::is_directed_v<edge_type>)
return this->has_edge(first_id, second_id) or this->has_edge(second_id, first_id);
else
return this->has_edge(first_id, second_id);
}
[[nodiscard]] bool are_incident(const vertex_type& first, const vertex_type& second) const {
this->_verify_vertex(first);
this->_verify_vertex(second);
if (first == second)
return true;
if constexpr (type_traits::is_directed_v<edge_type>)
return this->has_edge(first.id(), second.id())
or this->has_edge(second.id(), first.id());
else
return this->has_edge(first.id(), second.id());
}
[[nodiscard]] bool are_incident(const vertex_type& vertex, const edge_type& edge) const {
this->_verify_vertex(vertex);
this->_verify_edge(edge);
return edge.is_incident_with(vertex);
}
[[nodiscard]] bool are_incident(const edge_type& edge, const vertex_type& vertex) const {
this->_verify_vertex(vertex);
this->_verify_edge(edge);
return edge.is_incident_with(vertex);
}
[[nodiscard]] bool are_incident(const edge_type& edge_1, const edge_type& edge_2) const {
this->_verify_edge(edge_1);
this->_verify_edge(edge_2);
return edge_1.is_incident_with(edge_2.first()) or edge_1.is_incident_with(edge_2.second());
}
friend std::ostream& operator<<(std::ostream& os, const graph& g) {
if (io::is_option_set(os, io::graph_option::gsf)) {
g._gsf_write(os);
return os;
}
if (io::is_option_set(os, io::graph_option::verbose))
g._verbose_write(os);
else
g._concise_write(os);
return os;
}
friend inline std::istream& operator>>(std::istream& is, graph& g) {
g._gsf_read(is);
return is;
}
private:
[[nodiscard]] static constexpr std::string _directed_type_str() {
return type_traits::is_directed_v<edge_type> ? "directed" : "undirected";
}
// --- graph element verification methods ---
gl_attr_force_inline void _verify_vertex_id(const types::id_type vertex_id) const {
if (not this->has_vertex(vertex_id))
throw std::out_of_range(std::format("Got invalid vertex id [{}]", vertex_id));
}
void _verify_vertex(const vertex_type& vertex) const {
const auto vertex_id = vertex.id();
const auto& self_vertex = this->get_vertex(vertex_id);
if (&vertex != &self_vertex)
throw std::invalid_argument(std::format(
"Got invalid vertex [id = {} | expected addr = {} | actual addr = {}]",
vertex_id,
io::format(&self_vertex),
io::format(&vertex)
));
}
void _verify_edge(const edge_type& edge) const {
if (not this->has_edge(edge))
throw std::invalid_argument(std::format(
"Got invalid edge [vertices = ({}, {}) | addr = {}]",
edge.first_id(),
edge.second_id(),
io::format(&edge)
));
}
// --- vertex methods ---
void _remove_vertex_impl(const vertex_type& vertex) {
const auto vertex_id = vertex.id();
this->_impl.remove_vertex(vertex);
this->_vertices.erase(std::next(std::begin(this->_vertices), vertex_id));
// align ids of remainig vertices
std::for_each(
std::next(std::begin(this->_vertices), vertex_id),
this->_vertices.end(),
[](auto& v) { --v->_id; }
);
}
// --- io methods ---
void _verbose_write(std::ostream& os) const {
os << std::format(
"type: {}\nnumber of vertices: {}\nnumber of edges: {}\nvertices:\n",
_directed_type_str(),
this->n_vertices(),
this->n_unique_edges()
);
constexpr bool within_context = true;
for (const auto& vertex : this->vertices()) {
os << "- " << vertex << "\n adjacent edges:\n";
for (const auto& edge : this->_impl.adjacent_edges(vertex.id())) {
os << "\t- ";
edge._write(os, within_context);
os << '\n';
}
}
}
void _concise_write(std::ostream& os) const {
os << std::format(
"{} {} {}\n", _directed_type_str(), this->n_vertices(), this->n_unique_edges()
);
for (const auto& vertex : this->vertices()) {
os << "- " << vertex << " :";
for (const auto& edge : this->_impl.adjacent_edges(vertex.id()))
os << ' ' << edge;
os << '\n';
}
}
void _gsf_write(std::ostream& os) const {
const bool with_vertex_properties =
io::is_option_set(os, io::graph_option::with_vertex_properties);
const bool with_edge_properties =
io::is_option_set(os, io::graph_option::with_edge_properties);
// print graph size
os << std::format(
"{} {} {} {} {}\n",
static_cast<int>(type_traits::is_directed_v<edge_type>),
this->n_vertices(),
this->n_unique_edges(),
static_cast<int>(with_vertex_properties),
static_cast<int>(with_edge_properties)
);
if constexpr (type_traits::c_writable<typename vertex_type::properties_type>)
if (with_vertex_properties)
for (const auto& vertex : this->vertices())
os << vertex.properties << '\n';
if constexpr (type_traits::c_writable<typename edge_type::properties_type>) {
if (with_edge_properties) {
const auto print_incident_edges = [this, &os](const types::id_type vertex_id) {
for (const auto& edge : this->_impl.adjacent_edges(vertex_id)) {
if (edge.first_id() != vertex_id)
continue; // vertex is not the source
os << edge.first_id() << ' ' << edge.second_id() << ' ' << edge.properties
<< '\n';
}
};
for (const auto vertex_id : this->vertex_ids())
print_incident_edges(vertex_id);
return;
}
}
const auto print_incident_edges = [this, &os](const types::id_type vertex_id) {
for (const auto& edge : this->_impl.adjacent_edges(vertex_id)) {
if (edge.first_id() != vertex_id)
continue; // vertex is not the source
os << edge.first_id() << ' ' << edge.second_id() << '\n';
}
};
for (const auto vertex_id : this->vertex_ids())
print_incident_edges(vertex_id);
}
void _gsf_read(std::istream& is) {
bool directed;
is >> directed;
if (directed != type_traits::is_directed_v<edge_type>)
throw std::ios_base::failure(std::format(
"Invalid graph specification: directional tag does not match - should be {}",
_directed_type_str()
));
// read initial graph parameters
types::id_type n_vertices, n_edges;
is >> n_vertices >> n_edges;
bool with_vertex_properties, with_edge_properties;
is >> with_vertex_properties >> with_edge_properties;
if (with_vertex_properties) {
if constexpr (not type_traits::c_readable<vertex_properties_type>) {
throw std::ios_base::failure(
"Invalid graph specification: vertex_properties=true "
"when vertex_properties_type is not readable"
);
}
else {
// read vertex properties and use them to initialze the vertices
std::vector<vertex_properties_type> vertex_properties(n_vertices);
for (types::size_type i = constants::begin_idx; i < n_vertices; ++i)
is >> vertex_properties[i];
this->add_vertices_with(vertex_properties);
}
}
else {
// initialize the vertices with default (or none) properties
this->add_vertices(n_vertices);
}
if (with_edge_properties) {
if constexpr (not type_traits::c_readable<edge_properties_type>) {
throw std::ios_base::failure(
"Invalid graph specification: edge_properties=true "
"when edge_properties_type is not readable"
);
}
else {
// read edges with their properties
types::id_type first_id, second_id;
edge_properties_type properties;
for (types::size_type i = constants::begin_idx; i < n_edges; ++i) {
is >> first_id >> second_id >> properties;
this->add_edge(first_id, second_id, properties);
}
}
}
else {
// read the edges
types::id_type first_id, second_id;
for (types::size_type i = constants::begin_idx; i < n_edges; ++i) {
is >> first_id >> second_id;
this->add_edge(first_id, second_id);
}
}
}
vetex_list_type _vertices{};
implementation_type _impl{};
};
} // namespace gl