From 937f52598a1f90277d019a5b312b80627f154a1b Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Thu, 8 Sep 2016 15:00:08 +0200 Subject: [PATCH] add nice print capability to std tree --- alib2std/src/extensions/forward_tree.hpp | 28 ++++++++++++++++++++++ alib2std/src/extensions/tree.hpp | 30 ++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/alib2std/src/extensions/forward_tree.hpp b/alib2std/src/extensions/forward_tree.hpp index 6a5baabbf7..1a72d89bfd 100644 --- a/alib2std/src/extensions/forward_tree.hpp +++ b/alib2std/src/extensions/forward_tree.hpp @@ -70,6 +70,27 @@ class forward_tree { return children; } + void nicePrint ( std::ostream & os, const std::string & prefix, const bool last ) const { + os << prefix; + + std::string nextPrefix ( prefix ); + + if ( last ) { + os << "\\-"; + nextPrefix += " "; + } else { + os << "|-"; + nextPrefix += "| "; + } + + os << getData ( ) << std::endl; + + for ( unsigned int i = 0; i < children.size ( ); ++i ) { + os << nextPrefix << "|" << std::endl; + children[i].nicePrint ( os, nextPrefix, i == children.size ( ) - 1 ); + } + } + }; tree_node root; @@ -700,6 +721,13 @@ public: return compare ( other ) >= 0; } +// ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + + std::ostream & nicePrint ( std::ostream & os ) const { + root.nicePrint ( os, "", true ); + return os; + } + }; template < class T, class ... Ts > diff --git a/alib2std/src/extensions/tree.hpp b/alib2std/src/extensions/tree.hpp index 2a0a9170fc..b5721b1186 100644 --- a/alib2std/src/extensions/tree.hpp +++ b/alib2std/src/extensions/tree.hpp @@ -90,6 +90,29 @@ class tree { return children; } + void nicePrint ( std::ostream & os, const std::string & prefix, const bool last ) const { + os << prefix; + + std::string nextPrefix ( prefix ); + + if ( last ) { + os << "\\-"; + nextPrefix += " "; + } else { + os << "|-"; + nextPrefix += "| "; + } + + os << getData ( ) << std::endl; + + for ( unsigned int i = 0; i < children.size ( ); ++i ) { + os << nextPrefix << "|" << std::endl; + children[i].nicePrint ( os, nextPrefix, i == children.size ( ) - 1 ); + } + } + + friend struct tree_node; + }; tree_node root; @@ -740,6 +763,13 @@ public: return compare ( other ) >= 0; } +// ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + + std::ostream & nicePrint ( std::ostream & os ) const { + root.nicePrint ( os, "", true ); + return os; + } + }; template < class T, class ... Ts > -- GitLab