diff options
Diffstat (limited to 'include/input/PrintTools.h')
-rw-r--r-- | include/input/PrintTools.h | 29 |
1 files changed, 18 insertions, 11 deletions
diff --git a/include/input/PrintTools.h b/include/input/PrintTools.h index 0e3fbb1982..83fffa37c6 100644 --- a/include/input/PrintTools.h +++ b/include/input/PrintTools.h @@ -20,6 +20,7 @@ #include <map> #include <optional> #include <set> +#include <sstream> #include <string> #include <vector> @@ -33,6 +34,13 @@ std::string bitsetToString(const std::bitset<N>& bitset) { return bitset.to_string(); } +template <class T> +std::string streamableToString(const T& streamable) { + std::stringstream out; + out << streamable; + return out.str(); +} + template <typename T> inline std::string constToString(const T& v) { return std::to_string(v); @@ -75,11 +83,12 @@ std::string dumpSet(const std::set<T>& v, std::string (*toString)(const T&) = co } /** - * Convert a map to string. Both keys and values of the map should be integral type. + * Convert a map or multimap to string. Both keys and values of the map should be integral type. */ -template <typename K, typename V> -std::string dumpMap(const std::map<K, V>& map, std::string (*keyToString)(const K&) = constToString, - std::string (*valueToString)(const V&) = constToString) { +template <typename T> +std::string dumpMap(const T& map, + std::string (*keyToString)(const typename T::key_type&) = constToString, + std::string (*valueToString)(const typename T::mapped_type&) = constToString) { std::string out; for (const auto& [k, v] : map) { if (!out.empty()) { @@ -104,15 +113,13 @@ std::string dumpMapKeys(const std::map<K, V>& map, return out.empty() ? "{}" : (out + "}"); } -/** - * Convert a vector to a string. The values of the vector should be of a type supported by - * constToString. - */ +/** Convert a vector to a string. */ template <typename T> -std::string dumpVector(std::vector<T> values) { - std::string dump = constToString(values[0]); +std::string dumpVector(const std::vector<T>& values, + std::string (*valueToString)(const T&) = constToString) { + std::string dump = valueToString(values[0]); for (size_t i = 1; i < values.size(); i++) { - dump += ", " + constToString(values[i]); + dump += ", " + valueToString(values[i]); } return dump; } |