32#ifndef DRAIN_TREE_NAME
33#warning "Use TreeOrdered.h or TreeUnordered.h to include this file."
43#include <drain/TypeName.h>
44#include <drain/TypeUtils.h>
45#include <drain/Variable.h>
146template <
class T,
bool EXCLUSIVE=
false,
class P=
drain::Path<std::string,
'/'> >
151 typedef T node_data_t;
153 typedef typename path_t::elem_t key_t;
154 typedef typename path_t::elem_t path_elem_t;
156 typedef std::pair<key_t,tree_t> pair_t;
158 typedef DRAIN_TREE_CONTAINER(key_t,
tree_t) container_t;
160 typedef typename container_t::iterator iterator;
161 typedef typename container_t::const_iterator const_iterator;
192 bool dataEmpty()
const {
193 return dataEmpty(
data);
198 bool hasMultipleData()
const {
205 const node_data_t & getData()
const {
return data;};
208 node_data_t & getData(){
return data;};
211 const node_data_t & getData(
const key_t & key)
const {
212 return retrieveChild(key).
data;
216 node_data_t & getData(
const key_t & key){
217 return retrieveChild(key).
data;
232 typename container_t::const_iterator
begin()
const {
return children.begin(); };
236 typename container_t::const_iterator
end()
const {
return children.end(); };
240 typename container_t::iterator
begin(){
return children.begin(); };
244 typename container_t::iterator
end(){
return children.end(); };
261 #ifdef DRAIN_TREE_MULTI
271 #ifdef DRAIN_TREE_ORDERED
331 return this->
operator=(std::string(str));
340 template <
typename K,
typename V>
343 for (
const auto & entry: l){
363 for (
const auto & entry: l){
364 this[entry.first] = entry.second;
375 template <
typename V>
396 tree_t & child = retrieveChild(entry.first);
397 child.
data = entry.second;
403 operator const node_data_t &()
const {
412 operator node_data_t &(){
425 return retrieveChild(
getKey(key));
433 return retrieveChild(
getKey(key));
452 return get(path.begin(), path.end());
490 return get(path.begin(), path.end());
554 typename path_t::const_iterator pit = path.end();
555 if (pit == path.begin())
562 tree_t & parent = this->
get(path.begin(), pit);
564 #ifdef DRAIN_TREE_ORDERED
567 parent.children.erase(*pit);
572 for (iterator it = children.begin(); it != children.end(); ++it){
573 if (it->first == *pit){
575 parent.children.erase(it);
605 bool hasChildren()
const {
606 return !children.empty();
626 #ifdef DRAIN_TREE_ORDERED
629 return (children.find(key) == children.end()) ? 0 : 1;
640 for (
const auto & entry: children){
641 if (entry.first == key){
661 #ifdef DRAIN_TREE_ORDERED
664 return (children.find(key) != children.end());
669 for (
const auto & entry: children){
670 if (entry.first == key){
680 template <
typename K>
689 bool hasPath(
const path_t & path)
const {
690 return hasPath(path.begin(), path.end());
708 void generateKey(
const tree_t & tree,
typename P::elem_t & key);
716 typename P::elem_t key;
717 generateKey(*
this, key);
749 std::cerr <<
typeid(
tree_t).name() << std::endl;
750 throw std::runtime_error(
drain::StringBuilder<
':'>(__FILE__,__FUNCTION__,
" empty key (ADD static child counter based naming)"));
759 #ifdef DRAIN_TREE_ORDERED
761 iterator it = children.find(key);
762 if (it != children.end()){
766 tree_t & child = children.insert(children.begin(), pair_t(key,
tree_t()))->second;
775 for (
auto & entry: children){
777 if (entry.first == key){
784 children.push_back(pair_t(key,
tree_t()));
785 tree_t & child = children.back().second;
796 template <
typename E>
818 template <
typename E>
828 #ifdef DRAIN_TREE_ORDERED
835 throw std::runtime_error(
drain::StringBuilder<
':'>(__FILE__,__FUNCTION__,
" empty key (ADD static child counter based naming"));
845 for (
auto & entry: children){
846 if (entry.first == key){
853 children.push_front(pair_t(key, tree_t()));
854 tree_t & child = children.front().second;
907 children.swap(t.children);
915 ": node data exists but is not swapped");
942 template <
typename K>
949 container_t children;
955 tree_t & retrieveChild(
const key_t & key){
962 #ifdef DRAIN_TREE_ORDERED
966 iterator it = children.find(key);
967 if (it != children.end()){
971 tree_t & child = children.insert(children.begin(), pair_t(key, tree_t()))->second;
980 for (
auto & entry: children){
981 if (entry.first == key){
986 children.push_back(pair_t(key, tree_t()));
987 tree_t & child = children.back().second;
997 const tree_t & retrieveChild(
const key_t & key)
const {
1002 #ifdef DRAIN_TREE_ORDERED
1003 const const_iterator it = children.find(key);
1004 if (it != children.end()){
1008 for (
const auto & entry: children){
1009 if (entry.first == key){
1010 return entry.second;
1024 bool hasPath(
typename path_t::const_iterator it,
typename path_t::const_iterator eit)
const {
1029 const typename path_t::elem_t elem = *it;
1032 return hasPath(++it, eit);
1036 return this->
operator [](elem).hasPath(++it, eit);
1044 tree_t &
get(
typename path_t::const_iterator it,
typename path_t::const_iterator eit) {
1052 return get(++it, eit);
1055 return child.
get(++it, eit);
1066 const tree_t &
get(
typename path_t::const_iterator it,
typename path_t::const_iterator eit)
const {
1074 return get(++it, eit);
1080 return retrieveChild(*it).
get(++it, eit);
1109template <
class T,
bool EXCLUSIVE,
class P>
1110const DRAIN_TREE_NAME<T,EXCLUSIVE, P> DRAIN_TREE_NAME<T,EXCLUSIVE,P>::emptyNode;
1112template <
class T,
bool EXCLUSIVE,
class P>
1113void DRAIN_TREE_NAME<T,EXCLUSIVE, P>::generateKey(
const DRAIN_TREE_NAME<T,EXCLUSIVE, P> & tree,
typename P::elem_t & key){
1114 std::stringstream k;
1118 k << tree.getChildren().size();
1124#define DRAIN_TREE_NODE_SWAP(tree_t) template <> inline void tree_t::swapData(tree_t::node_data_t & node){ data.swap(node)); }
1129template <
class T,
bool EXCLUSIVE,
class P>
1135 const std::string &
str(){
1138 tree_t::isOrdered()?
"Ordered":
"Unordered",
1139 tree_t::isMultiple()?
"Multi":
"",
"Tree",
1140 tree_t::isExclusive()?
"(Exclusive)":
"",
A templated class for directed rooted trees.
Definition Tree.h:147
virtual int hasChildren(const key_t &key) const
The number of children with name key.
Definition Tree.h:624
void clearChildren()
Clears the children of this node. Does not clear data.
Definition Tree.h:537
tree_t & operator[](const K &key)
NEW 2025 templated child addressing operator.
Definition Tree.h:424
void swap(tree_t &t)
Replace children (but no data?)
Definition Tree.h:906
const tree_t & operator()(const char *arg) const
Redirects the call to operator()(const std::string & arg) .
Definition Tree.h:502
const tree_t & get(typename path_t::const_iterator it, typename path_t::const_iterator eit) const
Given the descendant pointed to by a given path segment.
Definition Tree.h:1066
container_t::const_iterator end() const
Child iterator pointing beyond the last child.
Definition Tree.h:236
tree_t & operator=(const tree_t &t)
Copies the data of another node. Does not copy the children.
Definition Tree.h:286
tree_t & prependChild()
Push in the front. This is only available for unordered trees.
Definition Tree.h:808
node_data_t data
Contents (data) of the node.
Definition Tree.h:180
tree_t & operator=(const std::initializer_list< V > &l)
Assign data.
Definition Tree.h:377
static bool isMultiple()
True, if this tree type can contain children with a same key (path element).
Definition Tree.h:260
container_t::iterator begin()
Child iterator pointing to the first child.
Definition Tree.h:240
const tree_t & operator[](const K &key) const
NEW 2025 templated child addressing operator.
Definition Tree.h:432
tree_t & operator()(const char *arg)
Redirects the call to operator()(const std::string & arg) .
Definition Tree.h:476
virtual const tree_t & getEmpty() const
Definition Tree.h:592
const tree_t & operator()(const path_t &path) const
Returns a descendant if that exists, else returns an empty node. Otherwise like non-const counterpart...
Definition Tree.h:489
static const key_t & getKey(const K &key)
Mapping of keys of external type - for example an enumerated type - to native key_t type.
tree_t & operator++()
Shorthand for prependChild().
Definition Tree.h:864
tree_t & addChild(const E &key)
Converts argument to std:string and calls addChild()
Definition Tree.h:798
virtual bool empty() const
Check if the tree node has empty data and no children.
Definition Tree.h:599
const container_t & getChildren() const
Returns the map containing the children.
Definition Tree.h:886
static key_t getKey(const char *key)
Conversion of char array to key type (which is never a char array).
Definition Tree.h:933
tree_t & operator=(std::initializer_list< std::pair< K, V > > l)
Assign tree structure (of depth one).
Definition Tree.h:342
void initChild(tree_t &) const
Means for automatically setting something, for example the type of a node.
Definition Tree.h:703
DRAIN_TREE_NAME(const DRAIN_TREE_NAME &t)
Copy constructor; copy only node data at the root.
Definition Tree.h:173
const tree_t & operator()(const S &arg) const
Returns a descendant if that exists, else returns an empty node. Otherwise like non-const counterpart...
Definition Tree.h:496
static bool isExclusive()
True, if this tree type can either children or node data, but not both.
Definition Tree.h:254
const node_data_t * operator->() const
Fast access to data, applied widely in TreeXML (HTML/SVG)
Definition Tree.h:893
void swapData(node_data_t &node)
User should redefine (implement) this function for applied node types.
Definition Tree.h:912
tree_t & addChild(const key_t &key)
Ensure that a child node with given key exists. For MULTIPLE, always add one.
Definition Tree.h:746
bool hasPath(typename path_t::const_iterator it, typename path_t::const_iterator eit) const
Checks if there is a node with a given path name.
Definition Tree.h:1024
tree_t & operator=(const T2 &v)
Assigns value to contents.
Definition Tree.h:318
node_data_t * operator->()
Fast access to data, applied widely in TreeXML (HTML/SVG)
Definition Tree.h:899
void erase(const path_t &path)
Deletes a descendant node and hence its subtrees.
Definition Tree.h:549
container_t & getChildren()
Returns the map containing the children.
Definition Tree.h:882
tree_t & operator<<(const node_pair_t &entry)
Experimental. Given pair(elem, data) assigns child[elem] = data;.
Definition Tree.h:392
DRAIN_TREE_NAME()
Default constructor.
Definition Tree.h:165
tree_t & prependChild(const E &key)
Converts argument to std:string and calls addChild()
Definition Tree.h:820
tree_t & operator()(const S &arg)
Returns a descendant. Creates one if not existing already.
Definition Tree.h:467
static const key_t & getKey(const key_t &key)
"Default implementation" of key conversion – the identity mapping.
Definition Tree.h:925
void clearData()
Definition Tree.h:525
virtual bool hasChild(const key_t &key) const
Check if the tree node has a direct descendant with name key.
Definition Tree.h:659
void clear()
Clear children and node data.
Definition Tree.h:514
std::pair< key_t, node_data_t > node_pair_t
Experimental. Given pair<elem, data> assigns child[elem] = data;.
Definition Tree.h:387
container_t::const_iterator begin() const
Iterator interface enables for-each type iteration.
Definition Tree.h:232
container_t::iterator end()
Child iterator end.
Definition Tree.h:244
DRAIN_TREE_NAME(const node_data_t &data)
Copy constructor; copy only node data at the root.
Definition Tree.h:169
tree_t & addChild()
Add a child with an automatically generated key (path element).
Definition Tree.h:715
tree_t & operator()(const path_t &path)
Returns a descendant. Creates one if not existing already.
Definition Tree.h:446
static bool isOrdered()
True, if the keys of the children of a node are stored in an ordered structure (that is,...
Definition Tree.h:270
static bool dataEmpty(const node_data_t &data)
Default implementation. To be redefined (template-implemented).
Definition Tree.h:187
tree_t & operator=(std::initializer_list< std::pair< const char *, const Variable > > l)
Assign tree structure (of depth one).
Definition Tree.h:359
tree_t & get(typename path_t::const_iterator it, typename path_t::const_iterator eit)
Returns a descendant. Creates one, if not present.
Definition Tree.h:1044
tree_t & operator++(int)
Shorthand for appendChild().
Definition Tree.h:870
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
Definition StringBuilder.h:58
Default implementation.
Definition TypeName.h:54
static const std::string & str()
Default implementation: name returned by std::type_info::name()
Definition TypeName.h:68