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
329 return this->
operator=(std::string(str));
338 template <
typename K,
typename V>
341 for (
const auto & entry: l){
361 for (
const auto & entry: l){
362 this[entry.first] = entry.second;
373 template <
typename V>
394 tree_t & child = retrieveChild(entry.first);
395 child.
data = entry.second;
401 operator const node_data_t &()
const {
410 operator node_data_t &(){
423 return retrieveChild(
getKey(key));
431 return retrieveChild(
getKey(key));
450 return get(path.begin(), path.end());
488 return get(path.begin(), path.end());
552 typename path_t::const_iterator pit = path.end();
553 if (pit == path.begin())
560 tree_t & parent = this->
get(path.begin(), pit);
562 #ifdef DRAIN_TREE_ORDERED
565 parent.children.erase(*pit);
570 for (iterator it = children.begin(); it != children.end(); ++it){
571 if (it->first == *pit){
573 parent.children.erase(it);
603 bool hasChildren()
const {
604 return !children.empty();
624 #ifdef DRAIN_TREE_ORDERED
627 return (children.find(key) == children.end()) ? 0 : 1;
638 for (
const auto & entry: children){
639 if (entry.first == key){
659 #ifdef DRAIN_TREE_ORDERED
662 return (children.find(key) != children.end());
667 for (
const auto & entry: children){
668 if (entry.first == key){
678 template <
typename K>
687 bool hasPath(
const path_t & path)
const {
688 return hasPath(path.begin(), path.end());
706 void generateKey(
const tree_t & tree,
typename P::elem_t & key);
714 typename P::elem_t key;
715 generateKey(*
this, key);
747 std::cerr <<
typeid(
tree_t).name() << std::endl;
748 throw std::runtime_error(
drain::StringBuilder<
':'>(__FILE__,__FUNCTION__,
" empty key (ADD static child counter based naming)"));
757 #ifdef DRAIN_TREE_ORDERED
759 iterator it = children.find(key);
760 if (it != children.end()){
764 tree_t & child = children.insert(children.begin(), pair_t(key,
tree_t()))->second;
773 for (
auto & entry: children){
775 if (entry.first == key){
782 children.push_back(pair_t(key,
tree_t()));
783 tree_t & child = children.back().second;
794 template <
typename E>
816 template <
typename E>
826 #ifdef DRAIN_TREE_ORDERED
833 throw std::runtime_error(
drain::StringBuilder<
':'>(__FILE__,__FUNCTION__,
" empty key (ADD static child counter based naming"));
843 for (
auto & entry: children){
844 if (entry.first == key){
851 children.push_front(pair_t(key, tree_t()));
852 tree_t & child = children.front().second;
905 children.swap(t.children);
913 ": node data exists but is not swapped");
940 template <
typename K>
947 container_t children;
953 tree_t & retrieveChild(
const key_t & key){
960 #ifdef DRAIN_TREE_ORDERED
964 iterator it = children.find(key);
965 if (it != children.end()){
969 tree_t & child = children.insert(children.begin(), pair_t(key, tree_t()))->second;
978 for (
auto & entry: children){
979 if (entry.first == key){
984 children.push_back(pair_t(key, tree_t()));
985 tree_t & child = children.back().second;
995 const tree_t & retrieveChild(
const key_t & key)
const {
1000 #ifdef DRAIN_TREE_ORDERED
1001 const const_iterator it = children.find(key);
1002 if (it != children.end()){
1006 for (
const auto & entry: children){
1007 if (entry.first == key){
1008 return entry.second;
1022 bool hasPath(
typename path_t::const_iterator it,
typename path_t::const_iterator eit)
const {
1027 const typename path_t::elem_t elem = *it;
1030 return hasPath(++it, eit);
1034 return this->
operator [](elem).hasPath(++it, eit);
1042 tree_t &
get(
typename path_t::const_iterator it,
typename path_t::const_iterator eit) {
1050 return get(++it, eit);
1053 return child.
get(++it, eit);
1064 const tree_t &
get(
typename path_t::const_iterator it,
typename path_t::const_iterator eit)
const {
1072 return get(++it, eit);
1078 return retrieveChild(*it).
get(++it, eit);
1107template <
class T,
bool EXCLUSIVE,
class P>
1108const DRAIN_TREE_NAME<T,EXCLUSIVE, P> DRAIN_TREE_NAME<T,EXCLUSIVE,P>::emptyNode;
1110template <
class T,
bool EXCLUSIVE,
class P>
1111void DRAIN_TREE_NAME<T,EXCLUSIVE, P>::generateKey(
const DRAIN_TREE_NAME<T,EXCLUSIVE, P> & tree,
typename P::elem_t & key){
1112 std::stringstream k;
1116 k << tree.getChildren().size();
1122#define DRAIN_TREE_NODE_SWAP(tree_t) template <> inline void tree_t::swapData(tree_t::node_data_t & node){ data.swap(node)); }
1127template <
class T,
bool EXCLUSIVE,
class P>
1133 const std::string &
str(){
1136 tree_t::isOrdered()?
"Ordered":
"Unordered",
1137 tree_t::isMultiple()?
"Multi":
"",
"Tree",
1138 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:622
void clearChildren()
Clears the children of this node. Does not clear data.
Definition Tree.h:535
tree_t & operator[](const K &key)
NEW 2025 templated child addressing operator.
Definition Tree.h:422
void swap(tree_t &t)
Replace children (but no data?)
Definition Tree.h:904
const tree_t & operator()(const char *arg) const
Redirects the call to operator()(const std::string & arg) .
Definition Tree.h:500
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:1064
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:806
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:375
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:430
tree_t & operator()(const char *arg)
Redirects the call to operator()(const std::string & arg) .
Definition Tree.h:474
virtual const tree_t & getEmpty() const
Definition Tree.h:590
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:487
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:862
tree_t & addChild(const E &key)
Converts argument to std:string and calls addChild()
Definition Tree.h:796
virtual bool empty() const
Check if the tree node has empty data and no children.
Definition Tree.h:597
const container_t & getChildren() const
Returns the map containing the children.
Definition Tree.h:884
static key_t getKey(const char *key)
Conversion of char array to key type (which is never a char array).
Definition Tree.h:931
tree_t & operator=(std::initializer_list< std::pair< K, V > > l)
Assign tree structure (of depth one).
Definition Tree.h:340
void initChild(tree_t &) const
Means for automatically setting something, for example the type of a node.
Definition Tree.h:701
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:494
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:891
void swapData(node_data_t &node)
User should redefine (implement) this function for applied node types.
Definition Tree.h:910
tree_t & addChild(const key_t &key)
Ensure that a child node with given key exists. For MULTIPLE, always add one.
Definition Tree.h:744
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:1022
tree_t & operator=(const T2 &v)
Assigns value to contents.
Definition Tree.h:317
node_data_t * operator->()
Fast access to data, applied widely in TreeXML (HTML/SVG)
Definition Tree.h:897
void erase(const path_t &path)
Deletes a descendant node and hence its subtrees.
Definition Tree.h:547
container_t & getChildren()
Returns the map containing the children.
Definition Tree.h:880
tree_t & operator<<(const node_pair_t &entry)
Experimental. Given pair(elem, data) assigns child[elem] = data;.
Definition Tree.h:390
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:818
tree_t & operator()(const S &arg)
Returns a descendant. Creates one if not existing already.
Definition Tree.h:465
static const key_t & getKey(const key_t &key)
"Default implementation" of key conversion – the identity mapping.
Definition Tree.h:923
void clearData()
Definition Tree.h:523
virtual bool hasChild(const key_t &key) const
Check if the tree node has a direct descendant with name key.
Definition Tree.h:657
void clear()
Clear children and node data.
Definition Tree.h:512
std::pair< key_t, node_data_t > node_pair_t
Experimental. Given pair<elem, data> assigns child[elem] = data;.
Definition Tree.h:385
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:713
tree_t & operator()(const path_t &path)
Returns a descendant. Creates one if not existing already.
Definition Tree.h:444
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:357
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:1042
tree_t & operator++(int)
Shorthand for appendChild().
Definition Tree.h:868
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