Skip to content

Commit fc1df5b

Browse files
committed
gl io refinment (again, but small this time)
1 parent 15bcfad commit fc1df5b

5 files changed

Lines changed: 122 additions & 120 deletions

File tree

include/gl/edge_descriptor.hpp

Lines changed: 45 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -163,82 +163,70 @@ class edge_descriptor final {
163163
return this->_properties.get();
164164
}
165165

166-
friend gl_attr_force_inline std::ostream& operator<<(
167-
std::ostream& os, const edge_descriptor& edge
168-
) {
169-
return edge._write(os);
166+
friend std::ostream& operator<<(std::ostream& os, const edge_descriptor& edge) {
167+
using enum io::detail::option_bit;
168+
169+
if (io::is_option_set(os, verbose))
170+
return edge._verbose_write(os);
171+
else
172+
return edge._concise_write(os);
170173
}
171174

172175
private:
173-
std::ostream& _write(std::ostream& os) const
176+
std::ostream& _verbose_write(std::ostream& os) const
174177
requires std::same_as<directional_tag, undirected_t>
175178
{
176-
using io::detail::option_bit;
177-
178-
if constexpr (not traits::c_writable<properties_type>) {
179-
return this->_write_no_properties(os);
180-
}
181-
else {
182-
if (not io::is_option_set(os, option_bit::with_connection_properties))
183-
return this->_write_no_properties(os);
184-
185-
if (io::is_option_set(os, option_bit::verbose))
186-
return os
187-
<< "[id: " << this->_id << " | endpoints: {" << this->_vertices.first << ", "
188-
<< this->_vertices.second << "} | " << this->_properties.get() << ']';
189-
else
190-
return os << '{' << this->_vertices.first << ", " << this->_vertices.second << "}["
191-
<< this->_properties.get() << ']';
192-
}
179+
using enum io::detail::option_bit;
180+
181+
os << "[id: " << this->_id << " | endpoints: {" << this->_vertices.first << ", "
182+
<< this->_vertices.second << '}';
183+
if constexpr (traits::c_writable<properties_type>)
184+
if (io::is_option_set(os, with_connection_properties))
185+
os << " | " << this->_properties.get();
186+
os << ']';
187+
188+
return os;
193189
}
194190

195-
// TODO: rm
196-
std::ostream& _write_no_properties(std::ostream& os) const
191+
std::ostream& _concise_write(std::ostream& os) const
197192
requires std::same_as<directional_tag, undirected_t>
198193
{
199-
using io::detail::option_bit;
194+
using enum io::detail::option_bit;
200195

201-
if (io::is_option_set(os, option_bit::verbose))
202-
return os << "[id: " << this->_id << " | endpoints: {" << this->_vertices.first << ", "
203-
<< this->_vertices.second << "}]";
204-
else
205-
return os << '{' << this->_vertices.first << ", " << this->_vertices.second << '}';
196+
os << '{' << this->_vertices.first << ", " << this->_vertices.second << '}';
197+
if constexpr (traits::c_writable<properties_type>)
198+
if (io::is_option_set(os, with_vertex_properties))
199+
os << '[' << this->_properties.get() << ']';
200+
201+
return os;
206202
}
207203

208-
std::ostream& _write(std::ostream& os) const
204+
std::ostream& _verbose_write(std::ostream& os) const
209205
requires std::same_as<directional_tag, directed_t>
210206
{
211-
using io::detail::option_bit;
212-
213-
if constexpr (not traits::c_writable<properties_type>) {
214-
return this->_write_no_properties(os);
215-
}
216-
else {
217-
if (not io::is_option_set(os, option_bit::with_connection_properties))
218-
return this->_write_no_properties(os);
219-
220-
if (io::is_option_set(os, option_bit::verbose))
221-
return os
222-
<< "[id: " << this->_id << " | source: " << this->_vertices.first
223-
<< ", target: " << this->_vertices.second << " | " << this->_properties.get()
224-
<< ']';
225-
else
226-
return os << '(' << this->_vertices.first << ", " << this->_vertices.second << ")["
227-
<< this->_properties.get() << ']';
228-
}
207+
using enum io::detail::option_bit;
208+
209+
os << "[id: " << this->_id << " | source: " << this->_vertices.first
210+
<< ", target: " << this->_vertices.second;
211+
if constexpr (traits::c_writable<properties_type>)
212+
if (io::is_option_set(os, with_connection_properties))
213+
os << " | " << this->_properties.get();
214+
os << ']';
215+
216+
return os;
229217
}
230218

231-
// TODO: rm
232-
std::ostream& _write_no_properties(std::ostream& os) const
219+
std::ostream& _concise_write(std::ostream& os) const
233220
requires std::same_as<directional_tag, directed_t>
234221
{
235-
using io::detail::option_bit;
222+
using enum io::detail::option_bit;
236223

237-
if (io::is_option_set(os, option_bit::verbose))
238-
return os << "[id: " << this->_id << " | source: " << this->_vertices.first
239-
<< ", target: " << this->_vertices.second << ']';
240-
else
241-
return os << '(' << this->_vertices.first << ", " << this->_vertices.second << ')';
224+
os << '(' << this->_vertices.first << ", " << this->_vertices.second << ')';
225+
if constexpr (traits::c_writable<properties_type>)
226+
if (io::is_option_set(os, with_vertex_properties))
227+
os << '[' << this->_properties.get() << ']';
228+
229+
return os;
242230
}
243231

244232
id_type _id;

include/gl/graph.hpp

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "gl/io/graph_fmt_traits.hpp"
1212
#include "gl/io/options.hpp"
1313
#include "gl/io/stream_options_manipulator.hpp"
14+
#include "gl/traits.hpp"
1415
#include "gl/util/ranges.hpp"
1516

1617
#include <set>
@@ -718,6 +719,21 @@ class graph final {
718719

719720
// --- I/O utility ---
720721

722+
struct concise_target_formatter {
723+
edge_type edge;
724+
id_type src_id;
725+
bool with_props;
726+
727+
friend std::ostream& operator<<(std::ostream& os, const concise_target_formatter& proxy) {
728+
os << proxy.edge.other(proxy.src_id);
729+
if constexpr (traits::c_writable<edge_properties_type>)
730+
if (proxy.with_props)
731+
os << '[' << proxy.edge.properties() << ']';
732+
733+
return os;
734+
}
735+
};
736+
721737
std::ostream& _verbose_write(std::ostream& os) const {
722738
os << "type: " << fmt_traits::type << ", |V| = " << this->order()
723739
<< ", |E| = " << this->size() << '\n';
@@ -730,17 +746,17 @@ class graph final {
730746
}
731747

732748
std::ostream& _concise_write(std::ostream& os) const {
733-
using io::detail::option_bit;
749+
using enum io::detail::option_bit;
734750

735751
for (const auto& src : this->vertices()) {
736-
os << src << " :";
737-
for (const auto& edge : this->out_edges(src.id())) {
738-
os << ' ' << edge.other(src.id());
739-
if constexpr (traits::c_writable<edge_properties_type>)
740-
if (io::is_option_set(os, option_bit::with_connection_properties))
741-
os << '[' << edge.properties() << ']';
742-
}
743-
os << '\n';
752+
auto tgts = std::views::transform(
753+
this->out_edges(src.id()),
754+
[src_id = src.id(),
755+
with_props = io::is_option_set(os, with_connection_properties)](const auto& edge) {
756+
return concise_target_formatter{edge, src_id, with_props};
757+
}
758+
);
759+
os << src << " : " << io::range_formatter(tgts) << '\n';
744760
}
745761

746762
return os;
@@ -749,7 +765,6 @@ class graph final {
749765
std::ostream& _gsf_write(std::ostream& os) const {
750766
using io::detail::option_bit;
751767

752-
// TODO: include c_writable + rename to _props
753768
const bool with_vertex_properties =
754769
io::is_option_set(os, option_bit::with_vertex_properties);
755770
const bool with_edge_properties =

include/gl/io/format.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ struct range_formatter {
2424
os << formatter.open;
2525
bool first = true;
2626
for (const auto& item : formatter.range) {
27-
if (! first)
27+
if (not first)
2828
os << formatter.sep;
2929
os << item;
3030
first = false;

include/gl/vertex_descriptor.hpp

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "gl/constants.hpp"
99
#include "gl/decl/graph_traits.hpp"
1010
#include "gl/io/options.hpp"
11+
#include "gl/io/stream_options_manipulator.hpp"
1112
#include "gl/traits.hpp"
1213
#include "gl/types/core.hpp"
1314
#include "gl/types/properties.hpp"
@@ -92,38 +93,37 @@ class vertex_descriptor final {
9293
return this->_properties.get();
9394
}
9495

95-
friend gl_attr_force_inline std::ostream& operator<<(
96-
std::ostream& os, const vertex_descriptor& vertex
97-
) {
98-
return vertex._write(os);
96+
friend std::ostream& operator<<(std::ostream& os, const vertex_descriptor& vertex) {
97+
using enum io::detail::option_bit;
98+
99+
if (io::is_option_set(os, verbose))
100+
return vertex._verbose_write(os);
101+
else
102+
return vertex._concise_write(os);
99103
}
100104

101105
private:
102-
std::ostream& _write(std::ostream& os) const {
103-
using io::detail::option_bit;
104-
105-
if constexpr (not traits::c_writable<properties_type>) {
106-
return this->_write_no_properties(os);
107-
}
108-
else {
109-
if (not io::is_option_set(os, option_bit::with_vertex_properties))
110-
return this->_write_no_properties(os);
111-
112-
if (io::is_option_set(os, option_bit::verbose))
113-
return os << "[id: " << this->_id << " | " << this->_properties.get() << ']';
114-
else
115-
return os << this->_id << '[' << this->_properties.get() << ']';
116-
}
106+
std::ostream& _verbose_write(std::ostream& os) const {
107+
using enum io::detail::option_bit;
108+
109+
os << "[id: " << this->_id;
110+
if constexpr (traits::c_writable<properties_type>)
111+
if (io::is_option_set(os, with_vertex_properties))
112+
os << " | " << this->_properties.get();
113+
os << ']';
114+
115+
return os;
117116
}
118117

119-
// TODO: rm
120-
std::ostream& _write_no_properties(std::ostream& os) const {
121-
using io::detail::option_bit;
118+
std::ostream& _concise_write(std::ostream& os) const {
119+
using enum io::detail::option_bit;
122120

123-
if (io::is_option_set(os, option_bit::verbose))
124-
return os << "[id: " << this->_id << ']';
125-
else
126-
return os << this->_id;
121+
os << this->_id;
122+
if constexpr (traits::c_writable<properties_type>)
123+
if (io::is_option_set(os, with_vertex_properties))
124+
os << '[' << this->_properties.get() << ']';
125+
126+
return os;
127127
}
128128

129129
id_type _id;

include/hgl/hypergraph_elements.hpp

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -94,38 +94,37 @@ class hyperedge_descriptor final {
9494
return this->_properties.get();
9595
}
9696

97-
friend gl_attr_force_inline std::ostream& operator<<(
98-
std::ostream& os, const hyperedge_descriptor& hyperedge
99-
) {
100-
return hyperedge._write(os);
97+
friend std::ostream& operator<<(std::ostream& os, const hyperedge_descriptor& hyperedge) {
98+
using enum io::detail::option_bit;
99+
100+
if (io::is_option_set(os, verbose))
101+
return hyperedge._verbose_write(os);
102+
else
103+
return hyperedge._concise_write(os);
101104
}
102105

103106
private:
104-
std::ostream& _write(std::ostream& os) const {
105-
using io::detail::option_bit;
106-
107-
if constexpr (not traits::c_writable<properties_type>) {
108-
return this->_write_no_properties(os);
109-
}
110-
else {
111-
if (not io::is_option_set(os, option_bit::with_connection_properties))
112-
return this->_write_no_properties(os);
113-
114-
if (io::is_option_set(os, option_bit::verbose))
115-
return os << "[id: " << this->_id << " | " << this->_properties.get() << "]";
116-
else
117-
return os << this->_id << "[" << this->_properties.get() << "]";
118-
}
107+
std::ostream& _verbose_write(std::ostream& os) const {
108+
using enum io::detail::option_bit;
109+
110+
os << "[id: " << this->_id;
111+
if constexpr (traits::c_writable<properties_type>)
112+
if (io::is_option_set(os, with_vertex_properties))
113+
os << " | " << this->_properties.get();
114+
os << ']';
115+
116+
return os;
119117
}
120118

121-
// TODO: rm
122-
std::ostream& _write_no_properties(std::ostream& os) const {
123-
using io::detail::option_bit;
119+
std::ostream& _concise_write(std::ostream& os) const {
120+
using enum io::detail::option_bit;
124121

125-
if (io::is_option_set(os, option_bit::verbose))
126-
return os << "[id: " << this->_id << "]";
127-
else
128-
return os << this->_id;
122+
os << this->_id;
123+
if constexpr (traits::c_writable<properties_type>)
124+
if (io::is_option_set(os, with_vertex_properties))
125+
os << '[' << this->_properties.get() << ']';
126+
127+
return os;
129128
}
130129

131130
id_type _id;

0 commit comments

Comments
 (0)