Skip to content

Commit 0542d44

Browse files
committed
graph file io aligned
1 parent c97d7ea commit 0542d44

8 files changed

Lines changed: 237 additions & 195 deletions

File tree

include/gl/edge_descriptor.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class edge_descriptor final {
1919
using directional_tag = DirectionalTag;
2020
using properties_type = Properties;
2121
using properties_ref_type = std::conditional_t<
22-
type_traits::is_default_properties_type_v<properties_type>,
22+
type_traits::c_empty_properties<properties_type>,
2323
types::empty_properties,
2424
properties_type&>;
2525

@@ -32,7 +32,7 @@ class edge_descriptor final {
3232
explicit edge_descriptor(
3333
const types::id_type id, const types::id_type first, const types::id_type second
3434
)
35-
requires(type_traits::is_default_properties_type_v<properties_type>)
35+
requires(type_traits::c_empty_properties<properties_type>)
3636
: _id(id), _vertices(first, second) {}
3737

3838
explicit edge_descriptor(
@@ -41,17 +41,17 @@ class edge_descriptor final {
4141
const types::id_type second,
4242
properties_type& properties
4343
)
44-
requires(not type_traits::is_default_properties_type_v<properties_type>)
44+
requires(type_traits::c_non_empty_properties<properties_type>)
4545
: _id(id), _vertices(first, second), _properties(properties) {}
4646

4747
[[nodiscard]] static gl_attr_force_inline edge_descriptor invalid() noexcept
48-
requires(type_traits::is_default_properties_type_v<properties_type>)
48+
requires(type_traits::c_empty_properties<properties_type>)
4949
{
5050
return edge_descriptor(constants::invalid_id, constants::invalid_id, constants::invalid_id);
5151
}
5252

5353
[[nodiscard]] static gl_attr_force_inline edge_descriptor invalid() noexcept
54-
requires(not type_traits::is_default_properties_type_v<properties_type>)
54+
requires(type_traits::c_non_empty_properties<properties_type>)
5555
{
5656
static properties_type invalid_properties{};
5757
return edge_descriptor(

include/gl/graph.hpp

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class graph final {
3535
using vertex_type = typename traits_type::vertex_type;
3636
using vertex_properties_type = typename traits_type::vertex_properties_type;
3737
using vertex_properties_map_type = std::conditional_t<
38-
type_traits::is_default_properties_type_v<vertex_properties_type>,
38+
type_traits::c_empty_properties<vertex_properties_type>,
3939
types::empty_properties_map,
4040
std::vector<std::unique_ptr<vertex_properties_type>>>;
4141

@@ -44,7 +44,7 @@ class graph final {
4444
using edge_properties_type = typename traits_type::edge_properties_type;
4545

4646
using edge_properties_map_type = std::conditional_t<
47-
type_traits::is_default_properties_type_v<edge_properties_type>,
47+
type_traits::c_empty_properties<edge_properties_type>,
4848
types::empty_properties_map,
4949
std::vector<std::unique_ptr<edge_properties_type>>>;
5050

@@ -54,7 +54,7 @@ class graph final {
5454
graph() = default;
5555

5656
explicit graph(const types::size_type n_vertices) : _n_vertices(n_vertices), _impl(n_vertices) {
57-
if constexpr (not type_traits::is_default_properties_type_v<vertex_properties_type>) {
57+
if constexpr (type_traits::c_non_empty_properties<vertex_properties_type>) {
5858
this->_vertex_properties.reserve(n_vertices);
5959
for (const auto _ : this->vertex_ids())
6060
this->_vertex_properties.push_back(std::make_unique<vertex_properties_type>());
@@ -79,14 +79,14 @@ class graph final {
7979
// --- vertex methods ---
8080

8181
[[nodiscard]] gl_attr_force_inline auto vertices() const
82-
requires(type_traits::is_default_properties_type_v<vertex_properties_type>)
82+
requires(type_traits::c_empty_properties<vertex_properties_type>)
8383
{
8484
return this->vertex_ids()
8585
| std::views::transform([](const types::id_type id) { return vertex_descriptor{id}; });
8686
}
8787

8888
[[nodiscard]] gl_attr_force_inline auto vertices() const
89-
requires(not type_traits::is_default_properties_type_v<vertex_properties_type>)
89+
requires(type_traits::c_non_empty_properties<vertex_properties_type>)
9090
{
9191
return this->_vertex_properties | std::views::enumerate
9292
| std::views::transform([](const auto& x) {
@@ -105,7 +105,7 @@ class graph final {
105105

106106
[[nodiscard]] gl_attr_force_inline vertex_type get_vertex(const types::id_type vertex_id) const {
107107
this->_verify_vertex_id(vertex_id);
108-
if constexpr (type_traits::is_default_properties_type_v<vertex_properties_type>)
108+
if constexpr (type_traits::c_empty_properties<vertex_properties_type>)
109109
return vertex_descriptor{vertex_id};
110110
else
111111
return vertex_descriptor{vertex_id, *this->_vertex_properties[vertex_id]};
@@ -125,7 +125,7 @@ class graph final {
125125
this->_impl.add_vertex();
126126
const auto new_vertex_id = this->_n_vertices++;
127127

128-
if constexpr (type_traits::is_default_properties_type_v<vertex_properties_type>)
128+
if constexpr (type_traits::c_empty_properties<vertex_properties_type>)
129129
return vertex_descriptor{new_vertex_id};
130130
else {
131131
this->_vertex_properties.push_back(std::make_unique<vertex_properties_type>());
@@ -134,7 +134,7 @@ class graph final {
134134
}
135135

136136
vertex_type add_vertex(vertex_properties_type properties)
137-
requires(not type_traits::is_default_properties_type_v<vertex_properties_type>)
137+
requires(type_traits::c_non_empty_properties<vertex_properties_type>)
138138
{
139139
this->_impl.add_vertex();
140140
this->_vertex_properties.push_back(
@@ -147,7 +147,7 @@ class graph final {
147147
this->_impl.add_vertices(n);
148148
this->_n_vertices += n;
149149

150-
if constexpr (not type_traits::is_default_properties_type_v<vertex_properties_type>) {
150+
if constexpr (type_traits::c_non_empty_properties<vertex_properties_type>) {
151151
const auto old_size = this->_vertex_properties.size();
152152
this->_vertex_properties.reserve(this->_n_vertices);
153153
for (types::size_type i = old_size; i < this->_n_vertices; ++i)
@@ -158,14 +158,14 @@ class graph final {
158158
void add_vertices_with(
159159
const type_traits::c_sized_range_of<vertex_properties_type> auto& properties_range
160160
)
161-
requires(not type_traits::is_default_properties_type_v<vertex_properties_type>)
161+
requires(type_traits::c_non_empty_properties<vertex_properties_type>)
162162
{
163163
const auto n = std::ranges::size(properties_range);
164164

165165
this->_impl.add_vertices(n);
166166
this->_n_vertices += n;
167167

168-
if constexpr (not type_traits::is_default_properties_type_v<vertex_properties_type>) {
168+
if constexpr (type_traits::c_non_empty_properties<vertex_properties_type>) {
169169
for (auto& properties : properties_range) {
170170
this->_vertex_properties.emplace_back(
171171
std::make_unique<vertex_properties_type>(properties)
@@ -250,7 +250,7 @@ class graph final {
250250
}
251251

252252
[[nodiscard]] gl_attr_force_inline auto vertex_properties_map() const noexcept
253-
requires(not type_traits::is_default_properties_type_v<vertex_properties_type>)
253+
requires(type_traits::c_non_empty_properties<vertex_properties_type>)
254254
{
255255
return util::deref_view(this->_vertex_properties);
256256
}
@@ -269,7 +269,7 @@ class graph final {
269269
const auto new_edge_id = this->_n_unique_edges++;
270270
this->_impl.add_edge(new_edge_id, first_id, second_id);
271271

272-
if constexpr (type_traits::is_default_properties_type_v<edge_properties_type>) {
272+
if constexpr (type_traits::c_empty_properties<edge_properties_type>) {
273273
return edge_type{new_edge_id, first_id, second_id};
274274
}
275275
else {
@@ -283,7 +283,7 @@ class graph final {
283283
const types::id_type second_id,
284284
const edge_properties_type& properties
285285
)
286-
requires(not type_traits::is_default_properties_type_v<edge_properties_type>)
286+
requires(type_traits::c_non_empty_properties<edge_properties_type>)
287287
{
288288
this->_verify_vertex_id(first_id);
289289
this->_verify_vertex_id(second_id);
@@ -305,7 +305,7 @@ class graph final {
305305
const gl_attr_force_inline edge_type add_edge(
306306
const vertex_type& first, const vertex_type& second, const edge_properties_type& properties
307307
)
308-
requires(not type_traits::is_default_properties_type_v<edge_properties_type>)
308+
requires(type_traits::c_non_empty_properties<edge_properties_type>)
309309
{
310310
return this->add_edge(first.id(), second.id(), properties);
311311
}
@@ -319,7 +319,7 @@ class graph final {
319319

320320
for (const auto target_id : target_id_range) {
321321
this->_verify_vertex_id(target_id);
322-
if constexpr (not type_traits::is_default_properties_type_v<edge_properties_type>)
322+
if constexpr (type_traits::c_non_empty_properties<edge_properties_type>)
323323
this->_edge_properties.emplace_back(std::make_unique<edge_properties_type>());
324324
}
325325

@@ -339,7 +339,7 @@ class graph final {
339339

340340
for (const auto& target : target_range) {
341341
this->_verify_vertex_id(target.id());
342-
if constexpr (not type_traits::is_default_properties_type_v<edge_properties_type>)
342+
if constexpr (type_traits::c_non_empty_properties<edge_properties_type>)
343343
this->_edge_properties.emplace_back(std::make_unique<edge_properties_type>());
344344
}
345345

@@ -376,7 +376,7 @@ class graph final {
376376
this->_verify_vertex_id(first_id);
377377
this->_verify_vertex_id(second_id);
378378

379-
if constexpr (type_traits::is_default_properties_type_v<edge_properties_type>)
379+
if constexpr (type_traits::c_empty_properties<edge_properties_type>)
380380
return this->_impl.get_edge(first_id, second_id);
381381
else
382382
return this->_impl.get_edge(first_id, second_id, this->_edge_properties);
@@ -394,7 +394,7 @@ class graph final {
394394
this->_verify_vertex_id(first_id);
395395
this->_verify_vertex_id(second_id);
396396

397-
if constexpr (type_traits::is_default_properties_type_v<edge_properties_type>)
397+
if constexpr (type_traits::c_empty_properties<edge_properties_type>)
398398
return this->_impl.get_edges(first_id, second_id);
399399
else
400400
return this->_impl.get_edges(first_id, second_id, this->_edge_properties);
@@ -408,7 +408,7 @@ class graph final {
408408

409409
gl_attr_force_inline void remove_edge(const edge_type& edge) {
410410
this->_verify_edge(edge);
411-
if constexpr (not type_traits::is_default_properties_type_v<edge_properties_type>)
411+
if constexpr (type_traits::c_non_empty_properties<edge_properties_type>)
412412
this->_edge_properties.erase(this->_edge_properties.begin() + edge.id());
413413
this->_impl.remove_edge(edge);
414414
this->_n_unique_edges--;
@@ -418,7 +418,7 @@ class graph final {
418418
const auto removed_edge_ids = this->_impl.remove_edges(edges);
419419
this->_n_unique_edges -= removed_edge_ids.size();
420420

421-
if constexpr (not type_traits::is_default_properties_type_v<edge_properties_type>) {
421+
if constexpr (type_traits::c_non_empty_properties<edge_properties_type>) {
422422
// IDs are sorted and do not contain duplicates
423423
for (const auto edge_id : std::views::reverse(removed_edge_ids))
424424
this->_edge_properties.erase(this->_edge_properties.begin() + edge_id);
@@ -427,7 +427,7 @@ class graph final {
427427

428428
[[nodiscard]] inline auto adjacent_edges(const types::id_type vertex_id) const {
429429
this->_verify_vertex_id(vertex_id);
430-
if constexpr (type_traits::is_default_properties_type_v<edge_properties_type>)
430+
if constexpr (type_traits::c_empty_properties<edge_properties_type>)
431431
return this->_impl.adjacent_edges(vertex_id);
432432
else
433433
return this->_impl.adjacent_edges(vertex_id, this->_edge_properties);
@@ -525,10 +525,10 @@ class graph final {
525525
this->_n_vertices--;
526526
this->_n_unique_edges -= removed_edge_ids.size();
527527

528-
if constexpr (not type_traits::is_default_properties_type_v<vertex_properties_type>)
528+
if constexpr (type_traits::c_non_empty_properties<vertex_properties_type>)
529529
this->_vertex_properties.erase(this->_vertex_properties.begin() + vertex_id);
530530

531-
if constexpr (not type_traits::is_default_properties_type_v<edge_properties_type>) {
531+
if constexpr (type_traits::c_non_empty_properties<edge_properties_type>) {
532532
// IDs are sorted and do not contain duplicates
533533
for (const auto& edge_id : std::views::reverse(removed_edge_ids))
534534
this->_edge_properties.erase(this->_edge_properties.begin() + edge_id);
@@ -547,7 +547,7 @@ class graph final {
547547

548548
for (const auto& vertex : this->vertices()) {
549549
os << "- " << vertex << "\n adjacent edges:\n";
550-
for (const auto& edge : this->_impl.adjacent_edges(vertex.id()))
550+
for (const auto& edge : this->adjacent_edges(vertex.id()))
551551
os << "\t- " << edge << '\n';
552552
}
553553
}
@@ -559,7 +559,7 @@ class graph final {
559559

560560
for (const auto& vertex : this->vertices()) {
561561
os << "- " << vertex << " :";
562-
for (const auto& edge : this->_impl.adjacent_edges(vertex.id()))
562+
for (const auto& edge : this->adjacent_edges(vertex.id()))
563563
os << ' ' << edge;
564564
os << '\n';
565565
}
@@ -584,15 +584,15 @@ class graph final {
584584
if constexpr (type_traits::c_writable<typename vertex_type::properties_type>)
585585
if (with_vertex_properties)
586586
for (const auto& vertex : this->vertices())
587-
os << vertex._properties << '\n';
587+
os << vertex.properties() << '\n';
588588

589589
if constexpr (type_traits::c_writable<typename edge_type::properties_type>) {
590590
if (with_edge_properties) {
591591
const auto print_incident_edges = [this, &os](const types::id_type vertex_id) {
592-
for (const auto& edge : this->_impl.adjacent_edges(vertex_id)) {
592+
for (const auto& edge : this->adjacent_edges(vertex_id)) {
593593
if (edge.first() != vertex_id)
594594
continue; // vertex is not the source
595-
os << edge.first() << ' ' << edge.second() << ' ' << edge._properties
595+
os << edge.first() << ' ' << edge.second() << ' ' << edge.properties()
596596
<< '\n';
597597
}
598598
};
@@ -605,7 +605,7 @@ class graph final {
605605
}
606606

607607
const auto print_incident_edges = [this, &os](const types::id_type vertex_id) {
608-
for (const auto& edge : this->_impl.adjacent_edges(vertex_id)) {
608+
for (const auto& edge : this->adjacent_edges(vertex_id)) {
609609
if (edge.first() != vertex_id)
610610
continue; // vertex is not the source
611611
os << edge.first() << ' ' << edge.second() << '\n';

include/gl/impl/adjacency_list.hpp

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ class adjacency_list final {
113113

114114
[[nodiscard]] std::optional<edge_type> get_edge(
115115
const types::id_type source_id, const types::id_type target_id
116-
) const {
116+
) const
117+
requires(type_traits::c_has_empty_properties<edge_type>)
118+
{
117119
const auto& adjacent_edges = this->_list[source_id];
118120
const auto item_it = std::ranges::find(adjacent_edges, target_id, [](const auto& item) {
119121
return item.target_id;
@@ -127,21 +129,25 @@ class adjacency_list final {
127129
const types::id_type source_id,
128130
const types::id_type target_id,
129131
const auto& edge_properties_map
130-
) const {
132+
) const
133+
requires(type_traits::c_has_non_empty_properties<edge_type>)
134+
{
131135
const auto& adjacent_edges = this->_list[source_id];
132136
const auto item_it = std::ranges::find(adjacent_edges, target_id, [](const auto& item) {
133137
return item.target_id;
134138
});
135139
if (item_it == adjacent_edges.cend())
136140
return std::nullopt;
137141
return std::make_optional<edge_type>(
138-
item_it->id, source_id, target_id, edge_properties_map[item_it->id]
142+
item_it->id, source_id, target_id, *edge_properties_map[item_it->id]
139143
);
140144
}
141145

142146
[[nodiscard]] std::vector<edge_type> get_edges(
143147
const types::id_type source_id, const types::id_type target_id
144-
) const {
148+
) const
149+
requires(type_traits::c_has_empty_properties<edge_type>)
150+
{
145151
return this->_list[source_id] | std::views::filter([&target_id](const auto& item) {
146152
return item.target_id == target_id;
147153
})
@@ -155,7 +161,9 @@ class adjacency_list final {
155161
const types::id_type source_id,
156162
const types::id_type target_id,
157163
const auto& edge_properties_map
158-
) const {
164+
) const
165+
requires(type_traits::c_has_non_empty_properties<edge_type>)
166+
{
159167
return this->_list[source_id] | std::views::filter([&target_id](const auto& item) {
160168
return item.target_id == target_id;
161169
})
@@ -184,15 +192,19 @@ class adjacency_list final {
184192
return removed_edge_ids;
185193
}
186194

187-
[[nodiscard]] gl_attr_force_inline auto adjacent_edges(const types::id_type vertex_id) const {
195+
[[nodiscard]] gl_attr_force_inline auto adjacent_edges(const types::id_type vertex_id) const
196+
requires(type_traits::c_has_empty_properties<edge_type>)
197+
{
188198
return this->_list[vertex_id] | std::views::transform([vertex_id](const auto& item) {
189199
return edge_type{item.id, vertex_id, item.target_id};
190200
});
191201
}
192202

193203
[[nodiscard]] gl_attr_force_inline auto adjacent_edges(
194204
const types::id_type vertex_id, const auto& edge_properties_map
195-
) const {
205+
) const
206+
requires(type_traits::c_has_non_empty_properties<edge_type>)
207+
{
196208
return this->_list[vertex_id]
197209
| std::views::transform([vertex_id, &edge_properties_map](const auto& item) {
198210
return edge_type{
@@ -203,13 +215,17 @@ class adjacency_list final {
203215

204216
// --- access operators ---
205217

206-
[[nodiscard]] gl_attr_force_inline auto at(const types::id_type vertex_id) const {
218+
[[nodiscard]] gl_attr_force_inline auto at(const types::id_type vertex_id) const
219+
requires(type_traits::c_has_empty_properties<edge_type>)
220+
{
207221
return this->adjacent_edges(vertex_id);
208222
}
209223

210224
[[nodiscard]] gl_attr_force_inline auto at(
211225
const types::id_type vertex_id, const auto& edge_properties_map
212-
) const {
226+
) const
227+
requires(type_traits::c_has_non_empty_properties<edge_type>)
228+
{
213229
return this->adjacent_edges(vertex_id, edge_properties_map);
214230
}
215231

0 commit comments

Comments
 (0)