33#ifndef DRAIN_TREE_UTILS
34#define DRAIN_TREE_UTILS "2.1"
60 template <
class TR,
class S>
63 for (
const auto & entry: tree){
64 typename TR::path_t p;
66 getPaths(entry.second, container, p);
75 template <
class TR,
class S>
77 void getPaths(
const TR & tree, S & container,
const typename TR::path_t & path){
78 container.push_back(path);
79 for (
const auto & entry: tree){
81 typename TR::path_t p = path;
83 getPaths(entry.second, container, p);
87 template <
class T1,
class T2>
89 void deepCopy(
const T1 & srcTree, T2 & dstTree){
91 for (
const auto & entry: srcTree){
92 deepCopy(entry.second, dstTree.addChild(entry.first));
95 if (dstTree.empty() || !dstTree.isExclusive())
96 dstTree.data = srcTree.data;
111 template <
class T,
class H>
112 static void traverse(H & visitor, T & tree,
const typename T::path_t & path =
typename T::path_t()){
115 if (visitor.visitPrefix(tree, path)){
121 for (
auto & entry: tree(path).getChildren()){
123 traverse(visitor, tree,
typename T::path_t(path, entry.first));
126 if (visitor.visitPostfix(tree, path)){
151 template <
class TR,
bool SKIP_EMPTY=false>
153 bool dump(
const TR & tree, std::ostream &ostr = std::cout,
154 bool (* callBack)(
const typename TR::node_data_t &, std::ostream &) =
TreeUtils::dataDumper,
const std::string & indent=
"") {
168 if (callBack !=
nullptr){
169 if (! (tree.isExclusive() && tree.hasChildren())){
171 empty = (*callBack)(tree.data, ostr);
176 for (
typename TR::container_t::const_iterator it = tree.begin(); it != tree.end(); it++){
178 if (it->second.empty()){
182 std::ostream & ostrChild = ostr;
185 if (it == --tree.end()){
186 ostrChild << indent <<
"└──";
187 indent2 = indent +
" ";
190 ostrChild << indent <<
"├──";
191 indent2 = indent +
"│ ";
193 ostrChild << it->first;
195 if (callBack !=
nullptr){
200 bool empty2 =
dump(it->second, ostrChild, callBack, indent2);
214 void dumpContents(
const TR & tree, std::ostream &ostr = std::cout,
const typename TR::path_t & path =
"") {
216 ostr << tree.data <<
'\n';
217 for (
const auto & entry: tree){
218 ostr << entry.first <<
'\t';
227 void writeINI(
const TR & t, std::ostream & ostr = std::cout,
const typename TR::path_t & prefix =
typename TR::path_t()){
236 void readINI(TR & tree, std::istream & istr){
238 mout.unimplemented(
"future extension");
281DRAIN_TYPENAME(TreeUtils);
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:326
Logger & unimplemented(const TT &... args)
Feature to be done. Special type of Logger::note().
Definition Log.h:521
Collection of functions for investigating and processing trees.
Definition TreeUtils.h:52
static void getPaths(const TR &tree, S &container)
Retrieve all the paths.
Definition TreeUtils.h:62
static void writeINI(const TR &t, std::ostream &ostr=std::cout, const typename TR::path_t &prefix=typename TR::path_t())
Write a Windows INI file.
Definition TreeUtils.h:227
static void getPaths(const TR &tree, S &container, const typename TR::path_t &path)
Retrieve all the paths.
Definition TreeUtils.h:77
static void traverse(H &visitor, T &tree, const typename T::path_t &path=typename T::path_t())
Traverse tree, visiting each node as a prefix operation.
Definition TreeUtils.h:112
static bool dataDumper(const T &data, std::ostream &ostr)
Default implementation for recursive dump()
Definition TreeUtils.h:137
static void dumpContents(const TR &tree, std::ostream &ostr=std::cout, const typename TR::path_t &path="")
Debugging utility - dumps the tree, also the contents.
Definition TreeUtils.h:214
static bool dump(const TR &tree, std::ostream &ostr=std::cout, bool(*callBack)(const typename TR::node_data_t &, std::ostream &)=TreeUtils::dataDumper, const std::string &indent="")
Render a tree using character graphics.
Definition TreeUtils.h:153
Default implementation of a tree visitor (concept) compatible TreeUtils::traverser()
Definition TreeUtils.h:253
virtual int visitPrefix(T &tree, const typename T::path_t &path)
Tasks to be executed before traversing child nodes.
Definition TreeUtils.h:265
virtual int visitPostfix(T &tree, const typename T::path_t &path)
Tasks to be executed after traversing child nodes.
Definition TreeUtils.h:271