32#ifndef DRAIN_TREE_NAME
33#warning "Use TreeOrdered.h or TreeUnordered.h to include this file."
43#include <drain/Type.h>
44#include <drain/TypeUtils.h>
45#include <drain/Variable.h>
184template <
class T,
bool EXCLUSIVE=
false,
class P=
drain::Path<std::string,
'/'> >
189 typedef T node_data_t;
191 typedef typename path_t::elem_t key_t;
192 typedef typename path_t::elem_t path_elem_t;
194 typedef std::pair<key_t,tree_t> pair_t;
196 typedef DRAIN_TREE_CONTAINER(key_t,
tree_t) container_t;
198 typedef typename container_t::iterator iterator;
199 typedef typename container_t::const_iterator const_iterator;
223 bool hasMultipleData()
const {
229 const node_data_t & getData()
const {
return data;};
232 node_data_t & getData(){
return data;};
235 const node_data_t & getData(
const key_t & key)
const {
236 return retrieveChild(key).
data;
240 node_data_t & getData(
const key_t & key){
241 return retrieveChild(key).
data;
248 typename container_t::const_iterator
begin()
const {
return children.begin(); };
252 typename container_t::const_iterator
end()
const {
return children.end(); };
256 typename container_t::iterator
begin(){
return children.begin(); };
260 typename container_t::iterator
end(){
return children.end(); };
277 #ifdef DRAIN_TREE_MULTI
287 #ifdef DRAIN_TREE_ORDERED
371 return this->
operator=(std::string(str));
380 template <
typename K,
typename V>
383 for (
const auto & entry: l){
403 for (
const auto & entry: l){
404 this[entry.first] = entry.second;
425 template <
typename V>
456 tree_t & child = retrieveChild(entry.first);
457 child.
data = entry.second;
463 operator const node_data_t &()
const {
468 operator node_data_t &(){
476 tree_t & operator[](const key_t & key){
477 return retrieveChild(key);
482 const tree_t & operator[](const key_t & key) const {
483 return retrieveChild(key);
495 return retrieveChild(
getKey(key));
503 return retrieveChild(
getKey(key));
522 return get(path.begin(), path.end());
560 return get(path.begin(), path.end());
629 typename path_t::const_iterator pit = path.end();
630 if (pit == path.begin())
637 tree_t & parent = this->
get(path.begin(), pit);
639 #ifdef DRAIN_TREE_ORDERED
642 parent.children.erase(*pit);
647 for (iterator it = children.begin(); it != children.end(); ++it){
648 if (it->first == *pit){
650 parent.children.erase(it);
675 return (
data.empty() && !hasChildren());
679 bool hasChildren()
const {
680 return !children.empty();
700 #ifdef DRAIN_TREE_ORDERED
703 return (children.find(key) == children.end()) ? 0 : 1;
714 for (
const auto & entry: children){
715 if (entry.first == key){
735 #ifdef DRAIN_TREE_ORDERED
738 return (children.find(key) != children.end());
743 for (
const auto & entry: children){
744 if (entry.first == key){
754 template <
typename K>
763 bool hasPath(
const path_t & path)
const {
764 return hasPath(path.begin(), path.end());
788 void generateKey(
const tree_t & tree,
typename P::elem_t & key);
796 typename P::elem_t key;
797 generateKey(*
this, key);
828 std::cerr <<
typeid(
tree_t).name() << std::endl;
829 throw std::runtime_error(
drain::StringBuilder<
':'>(__FILE__,__FUNCTION__,
" empty key (ADD static child counter based naming)"));
838 #ifdef DRAIN_TREE_ORDERED
840 iterator it = children.find(key);
841 if (it != children.end()){
845 tree_t & child = children.insert(children.begin(), pair_t(key,
tree_t()))->second;
854 for (
auto & entry: children){
856 if (entry.first == key){
863 children.push_back(pair_t(key,
tree_t()));
864 tree_t & child = children.back().second;
875 template <
typename E>
895 #ifdef DRAIN_TREE_ORDERED
902 throw std::runtime_error(
drain::StringBuilder<
':'>(__FILE__,__FUNCTION__,
" empty key (ADD static child counter based naming"));
912 for (
auto & entry: children){
913 if (entry.first == key){
920 children.push_front(pair_t(key, tree_t()));
921 tree_t & child = children.front().second;
933 tree_t & retrieveChild(
const key_t & key){
940 #ifdef DRAIN_TREE_ORDERED
944 iterator it = children.find(key);
945 if (it != children.end()){
949 tree_t & child = children.insert(children.begin(), pair_t(key, tree_t()))->second;
958 for (
auto & entry: children){
959 if (entry.first == key){
964 children.push_back(pair_t(key, tree_t()));
965 tree_t & child = children.back().second;
975 const tree_t & retrieveChild(
const key_t & key)
const {
980 #ifdef DRAIN_TREE_ORDERED
981 const const_iterator it = children.find(key);
982 if (it != children.end()){
986 for (
const auto & entry: children){
987 if (entry.first == key){
1029 children.swap(t.children);
1057 template <
typename K>
1064 container_t children;
1076 bool hasPath(
typename path_t::const_iterator it,
typename path_t::const_iterator eit)
const {
1081 const typename path_t::elem_t elem = *it;
1084 return hasPath(++it, eit);
1088 return this->
operator [](elem).hasPath(++it, eit);
1096 tree_t &
get(
typename path_t::const_iterator it,
typename path_t::const_iterator eit) {
1104 return get(++it, eit);
1107 return child.
get(++it, eit);
1118 const tree_t &
get(
typename path_t::const_iterator it,
typename path_t::const_iterator eit)
const {
1126 return get(++it, eit);
1132 return retrieveChild(*it).
get(++it, eit);
1161template <
class T,
bool EXCLUSIVE,
class P>
1162const DRAIN_TREE_NAME<T,EXCLUSIVE, P> DRAIN_TREE_NAME<T,EXCLUSIVE,P>::emptyNode;
1164template <
class T,
bool EXCLUSIVE,
class P>
1165void DRAIN_TREE_NAME<T,EXCLUSIVE, P>::generateKey(
const DRAIN_TREE_NAME<T,EXCLUSIVE, P> & tree,
typename P::elem_t & key){
1166 std::stringstream k;
1170 k << tree.getChildren().size();
1180template <
class T,
bool EXCLUSIVE,
class P>
1186 const std::string & str(){
1189 tree_t::isOrdered()?
"Ordered":
"Unordered",
1190 tree_t::isMultiple()?
"Multi":
"",
"Tree",
1191 tree_t::isExclusive()?
"(Exclusive)":
"",
A templated class for directed rooted trees.
Definition Tree.h:185
virtual int hasChildren(const key_t &key) const
The number of children with name key.
Definition Tree.h:698
void clearChildren()
Clears the children of this node. Does not clear data.
Definition Tree.h:607
tree_t & operator[](const K &key)
NEW 2025 templated child addressing operator.
Definition Tree.h:494
void swap(tree_t &t)
Replace children (but no data?)
Definition Tree.h:1028
void initChild(tree_t &child) const
Means for automatically setting something, for example the type of a node.
Definition Tree.h:784
const tree_t & operator()(const char *arg) const
Redirects the call to operator()(const std::string & arg) .
Definition Tree.h:572
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:1118
container_t::const_iterator end() const
Child iterator pointing beyond the last child.
Definition Tree.h:252
tree_t & operator=(const tree_t &t)
Copies the data of another node. Does not copy the children.
Definition Tree.h:302
tree_t & prependChild()
Push in the front. This is only available for unordered trees.
Definition Tree.h:886
node_data_t data
Contents (data) of the node.
Definition Tree.h:218
tree_t & operator=(const std::initializer_list< V > &l)
Assign data.
Definition Tree.h:427
static bool isMultiple()
True, if this tree type can contain children with a same key (path element).
Definition Tree.h:276
container_t::iterator begin()
Child iterator pointing to the first child.
Definition Tree.h:256
const tree_t & operator[](const K &key) const
NEW 2025 templated child addressing operator.
Definition Tree.h:502
tree_t & operator()(const char *arg)
Redirects the call to operator()(const std::string & arg) .
Definition Tree.h:546
virtual const tree_t & getEmpty() const
Definition Tree.h:667
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:559
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 & addChild(const E &key)
Converts argument to std:string and calls addChild()
Definition Tree.h:876
virtual bool empty() const
Check if the tree node has empty data and no children.
Definition Tree.h:674
const container_t & getChildren() const
Returns the map containing the children.
Definition Tree.h:1008
static key_t getKey(const char *key)
Conversion of char array to key type (which is never a char array).
Definition Tree.h:1048
tree_t & operator=(std::initializer_list< std::pair< K, V > > l)
Assign tree structure (of depth one).
Definition Tree.h:382
DRAIN_TREE_NAME(const DRAIN_TREE_NAME &t)
Copy constructor; copy only node data at the root.
Definition Tree.h:211
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:566
static bool isExclusive()
True, if this tree type can either children or node data, but not both.
Definition Tree.h:270
const node_data_t * operator->() const
Fast access to data, applied widely in TreeXML (HTML/SVG)
Definition Tree.h:1015
tree_t & addChild(const key_t &key)
Ensure that a child node with given key exists. For MULTIPLE, always add one.
Definition Tree.h:825
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:1076
tree_t & operator=(const T2 &v)
Assigns value to contents.
Definition Tree.h:359
node_data_t * operator->()
Fast access to data, applied widely in TreeXML (HTML/SVG)
Definition Tree.h:1021
void erase(const path_t &path)
Deletes a descendant node and hence its subtrees.
Definition Tree.h:624
container_t & getChildren()
Returns the map containing the children.
Definition Tree.h:1004
tree_t & operator<<(const node_pair_t &entry)
Experimental. Given pair(elem, data) assigns child[elem] = data;.
Definition Tree.h:452
DRAIN_TREE_NAME()
Default constructor.
Definition Tree.h:203
tree_t & operator()(const S &arg)
Returns a descendant. Creates one if not existing already.
Definition Tree.h:537
static const key_t & getKey(const key_t &key)
"Default implementation" of key conversion – the identity mapping.
Definition Tree.h:1040
void clearData()
Definition Tree.h:595
virtual bool hasChild(const key_t &key) const
Check if the tree node has a direct descendant with name key.
Definition Tree.h:733
void clear()
Clear children and node data.
Definition Tree.h:584
std::pair< key_t, node_data_t > node_pair_t
Experimental. Given pair<elem, data> assigns child[elem] = data;.
Definition Tree.h:437
container_t::const_iterator begin() const
Child iterator pointing to the first child.
Definition Tree.h:248
container_t::iterator end()
Child iterator end.
Definition Tree.h:260
DRAIN_TREE_NAME(const node_data_t &data)
Copy constructor; copy only node data at the root.
Definition Tree.h:207
tree_t & addChild()
Add a child with an automatically generated key (path element).
Definition Tree.h:795
tree_t & operator()(const path_t &path)
Returns a descendant. Creates one if not existing already.
Definition Tree.h:516
static bool isOrdered()
True, if the keys of the children of a node are stored in an ordered structure (that is,...
Definition Tree.h:286
tree_t & operator=(std::initializer_list< std::pair< const char *, const Variable > > l)
Assign tree structure (of depth one).
Definition Tree.h:399
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:1096
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:313
Logger & unimplemented(const TT &... args)
Feature to be done. Special type of Logger::note().
Definition Log.h:512
Definition StringBuilder.h:58
Definition DataSelector.cpp:1277
Default implementation.
Definition Type.h:541
static const std::string name
Default implementation: name returned by std::type_info::name()
Definition Type.h:549