From 68aedbf2faf9b0cf22130c30c2ab281927c547be Mon Sep 17 00:00:00 2001 From: lisyarus Date: Sun, 7 Jun 2026 18:48:10 +0300 Subject: [PATCH] Add hash table formatters --- libs/util/include/psemek/util/hash_table.hpp | 57 ++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/libs/util/include/psemek/util/hash_table.hpp b/libs/util/include/psemek/util/hash_table.hpp index f5989f0f..ae0d1acc 100644 --- a/libs/util/include/psemek/util/hash_table.hpp +++ b/libs/util/include/psemek/util/hash_table.hpp @@ -6,6 +6,7 @@ #include #include +#include namespace psemek::util { @@ -716,3 +717,59 @@ namespace psemek::util }; } + + +namespace std +{ + + template + struct formatter<::psemek::util::hash_set, Char> + : std::formatter + { + using std::formatter::parse; + + template + auto format(::psemek::util::hash_set const & s, FormatContext & ctx) const + { + bool first = true; + ctx.advance_to(std::format_to(ctx.out(), "{{")); + for (auto const & v : s) + { + if (!first) + ctx.advance_to(std::format_to(ctx.out(), ", ")); + first = false; + ctx.advance_to(formatter::format(v, ctx)); + } + return std::format_to(ctx.out(), "}}"); + } + }; + + template + struct formatter<::psemek::util::hash_map, Char> + : std::pair, formatter> + { + template + constexpr auto parse(ParseContext & ctx) + { + return std::pair, formatter>::first.parse(ctx); + } + + template + auto format(::psemek::util::hash_map const & m, FormatContext & ctx) const + { + bool first = true; + ctx.advance_to(std::format_to(ctx.out(), "{{")); + for (auto const & p : m) + { + if (!first) + ctx.advance_to(std::format_to(ctx.out(), ", ")); + first = false; + ctx.advance_to(std::pair, formatter>::first.format(p.first, ctx)); + ctx.advance_to(std::format_to(ctx.out(), ": ")); + ctx.advance_to(std::pair, formatter>::second.format(p.second, ctx)); + } + return std::format_to(ctx.out(), "}}"); + } + }; + +}