Skip to content

Commit b63e0b5

Browse files
committed
hypergraph i/o refined
1 parent 382b44f commit b63e0b5

4 files changed

Lines changed: 52 additions & 14 deletions

File tree

include/gl/io/format.hpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,17 @@ range_formatter(R&& r) -> range_formatter<std::views::all_t<R>>;
3939

4040
template <std::ranges::range R>
4141
auto set_formatter(R&& range, std::string_view sep = ", ") {
42-
return range_formatter{std::forward<R>(range), sep, "{", "}"};
42+
using view_type = std::views::all_t<R>;
43+
return range_formatter<view_type>{std::views::all(std::forward<R>(range)), sep, "{", "}"};
44+
}
45+
46+
/// @todo Add an indent_width parameter
47+
template <std::ranges::range R>
48+
auto multiline_set_formatter(R&& range) {
49+
using view_type = std::views::all_t<R>;
50+
return range_formatter<view_type>{
51+
std::views::all(std::forward<R>(range)), ",\n ", "{\n ", "\n}"
52+
};
4353
}
4454

4555
} // namespace gl::io

include/gl/io/options.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ inline const stream_options_manipulator without_properties = unset_options(
3939
{detail::option_bit::with_vertex_properties, detail::option_bit::with_connection_properties}
4040
);
4141

42+
// TODO: rename to _spec_fmt
4243
inline const stream_options_manipulator enable_gsf =
4344
set_option(detail::option_bit::specification_fmt);
4445
inline const stream_options_manipulator disable_gsf =

include/hgl/hypergraph.hpp

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ class hypergraph final {
761761
struct hyperedge_formatter {
762762
public:
763763
const hypergraph& hg;
764-
const hyperedge_type& hyperedge;
764+
const hyperedge_type hyperedge;
765765

766766
friend std::ostream& operator<<(std::ostream& os, const hyperedge_formatter& proxy)
767767
requires std::same_as<directional_tag, undirected_t>
@@ -946,10 +946,18 @@ class hypergraph final {
946946
for (const auto& vertex : this->vertices())
947947
os << " - " << vertex << '\n';
948948
}
949+
else {
950+
this->_write_implicit_vset(os) << '\n';
951+
}
949952

950-
os << "hyperedges:\n";
951-
for (const auto& edge : this->hyperedges())
952-
os << " - " << this->display(edge) << '\n';
953+
if (this->size() == 0uz) {
954+
os << "hyperedges: {}";
955+
}
956+
else {
957+
os << "hyperedges:\n";
958+
for (const auto& edge : this->hyperedges())
959+
os << " - " << this->display(edge) << '\n';
960+
}
953961

954962
return os;
955963
}
@@ -960,22 +968,38 @@ class hypergraph final {
960968
const bool with_v_props =
961969
io::is_option_set(os, with_vertex_properties)
962970
and traits::c_writable<vertex_properties_type>;
963-
if (with_v_props) {
964-
os << "V :";
965-
for (const auto& vertex : this->vertices())
966-
os << " " << vertex;
971+
if (with_v_props)
972+
os << "V = " << io::multiline_set_formatter(this->vertices()) << '\n';
973+
else
974+
this->_write_implicit_vset(os) << '\n';
975+
976+
if (this->size() == 0uz) {
977+
os << "E = {}\n";
967978
}
968979
else {
969-
os << "|V| = " << this->order();
970-
}
980+
auto hyperedges = std::views::transform(this->hyperedges(), [this](auto hyperedge) {
981+
return this->display(hyperedge);
982+
});
971983

972-
os << "\nE:\n";
973-
for (const auto& edge : this->hyperedges())
974-
os << this->display(edge) << '\n';
984+
// TODO: set/multiline_set
985+
os << "E = " << io::multiline_set_formatter(hyperedges) << '\n';
986+
}
975987

976988
return os;
977989
}
978990

991+
std::ostream& _write_implicit_vset(std::ostream& os) const {
992+
const auto n = this->order();
993+
if (n == 0uz)
994+
return os << "V = {}";
995+
if (n == 1uz)
996+
return os << "V = {0}";
997+
if (n == 2uz)
998+
return os << "V = {0, 1}";
999+
1000+
return os << "V = {0, ..., " << n - 1 << '}';
1001+
}
1002+
9791003
// --- data members ---
9801004

9811005
size_type _n_vertices = 0uz;

include/hgl/io.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace hgl::io {
1515

16+
using gl::io::multiline_set_formatter;
1617
using gl::io::range_formatter;
1718
using gl::io::set_formatter;
1819

@@ -42,6 +43,8 @@ inline const stream_options_manipulator with_hyperedge_properties =
4243
inline const stream_options_manipulator without_hyperedge_properties =
4344
unset_option(detail::option_bit::with_connection_properties);
4445

46+
// TODO: spec fmt
47+
4548
using gl::io::with_properties;
4649
using gl::io::without_properties;
4750

0 commit comments

Comments
 (0)