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)){
154 template <
class TR,
bool SKIP_EMPTY=false>
156 bool dump(
const TR & tree, std::ostream &ostr = std::cout,
157 bool (* callBack)(
const typename TR::node_data_t &, std::ostream &) =
TreeUtils::dataDumper,
const std::string & indent=
"") {
171 if (callBack !=
nullptr){
172 if (! (tree.isExclusive() && tree.hasChildren())){
174 empty = (*callBack)(tree.data, ostr);
176 else if (tree.isExclusive() && !tree.data.empty()){
182 for (
typename TR::container_t::const_iterator it = tree.begin(); it != tree.end(); it++){
184 if (it->second.empty()){
189 std::ostream & ostrChild = ostr;
192 if (it == --tree.end()){
193 ostrChild << indent <<
"└──";
194 indent2 = indent +
" ";
197 ostrChild << indent <<
"├──";
198 indent2 = indent +
"│ ";
200 ostrChild << it->first;
202 if (callBack !=
nullptr){
207 bool empty2 =
dump(it->second, ostrChild, callBack, indent2);
221 void dumpContents(
const TR & tree, std::ostream &ostr = std::cout,
const typename TR::path_t & path =
"") {
223 ostr << tree.data <<
'\n';
224 for (
const auto & entry: tree){
225 ostr << entry.first <<
'\t';
234 void writeINI(
const TR & t, std::ostream & ostr = std::cout,
const typename TR::path_t & prefix =
typename TR::path_t()){
243 void readINI(TR & tree, std::istream & istr){
245 mout.unimplemented(
"future extension");
294DRAIN_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:234
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:221
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:156
Default implementation of a tree visitor (concept) compatible TreeUtils::traverser()
Definition TreeUtils.h:266
virtual int visitPrefix(T &tree, const typename T::path_t &path)
Tasks to be executed before traversing child nodes.
Definition TreeUtils.h:278
virtual int visitPostfix(T &tree, const typename T::path_t &path)
Tasks to be executed after traversing child nodes.
Definition TreeUtils.h:284