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);
496 return retrieveChild(key);
505 return retrieveChild(key);
523 return get(path.begin(), path.end());
561 return get(path.begin(), path.end());
630 typename path_t::const_iterator pit = path.end();
631 if (pit == path.begin())
638 tree_t & parent = this->
get(path.begin(), pit);
640 #ifdef DRAIN_TREE_ORDERED
643 parent.children.erase(*pit);
648 for (iterator it = children.begin(); it != children.end(); ++it){
649 if (it->first == *pit){
651 parent.children.erase(it);
676 return (
data.empty() && !hasChildren());
680 bool hasChildren()
const {
681 return !children.empty();
701 #ifdef DRAIN_TREE_ORDERED
704 return (children.find(key) == children.end()) ? 0 : 1;
715 for (
const auto & entry: children){
716 if (entry.first == key){
735 #ifdef DRAIN_TREE_ORDERED
737 return (children.find(key) != children.end());
741 for (
const auto & entry: children){
742 if (entry.first == key){
752 template <
typename K>
756 return hasChild(
static_cast<key_t
>(key));
761 bool hasPath(
const path_t & path)
const {
762 return hasPath(path.begin(), path.end());
791 typename P::elem_t key;
792 generateKey(*
this, key);
798 void generateKey(
const tree_t & tree,
typename P::elem_t & key);
815 std::cerr <<
typeid(
tree_t).name() << std::endl;
816 throw std::runtime_error(
drain::StringBuilder<
':'>(__FILE__,__FUNCTION__,
" empty key (ADD static child counter based naming)"));
825 #ifdef DRAIN_TREE_ORDERED
827 iterator it = children.find(key);
828 if (it != children.end()){
832 tree_t & child = children.insert(children.begin(), pair_t(key,
tree_t()))->second;
843 for (
auto & entry: children){
844 if (entry.first == key){
851 children.push_back(pair_t(key,
tree_t()));
852 tree_t & child = children.back().second;
871 #ifdef DRAIN_TREE_ORDERED
878 throw std::runtime_error(
drain::StringBuilder<
':'>(__FILE__,__FUNCTION__,
" empty key (ADD static child counter based naming"));
888 for (
auto & entry: children){
889 if (entry.first == key){
896 children.push_front(pair_t(key, tree_t()));
897 tree_t & child = children.front().second;
909 tree_t & retrieveChild(
const key_t & key){
916 #ifdef DRAIN_TREE_ORDERED
920 iterator it = children.find(key);
921 if (it != children.end()){
925 tree_t & child = children.insert(children.begin(), pair_t(key, tree_t()))->second;
934 for (
auto & entry: children){
935 if (entry.first == key){
940 children.push_back(pair_t(key, tree_t()));
941 tree_t & child = children.back().second;
951 const tree_t & retrieveChild(
const key_t & key)
const {
956 #ifdef DRAIN_TREE_ORDERED
957 const const_iterator it = children.find(key);
958 if (it != children.end()){
962 for (
const auto & entry: children){
963 if (entry.first == key){
1004 children.swap(t.children);
1009 container_t children;
1012 const tree_t emptyNode;
1049 bool hasPath(
typename path_t::const_iterator it,
typename path_t::const_iterator eit)
const {
1054 const typename path_t::elem_t elem = *it;
1057 return hasPath(++it, eit);
1061 return this->
operator [](elem).hasPath(++it, eit);
1069 tree_t &
get(
typename path_t::const_iterator it,
typename path_t::const_iterator eit) {
1077 return get(++it, eit);
1080 return child.
get(++it, eit);
1091 const tree_t &
get(
typename path_t::const_iterator it,
typename path_t::const_iterator eit)
const {
1099 return get(++it, eit);
1105 return retrieveChild(*it).
get(++it, eit);
1134template <
class T,
bool EXCLUSIVE,
class P>
1135const DRAIN_TREE_NAME<T,EXCLUSIVE, P> DRAIN_TREE_NAME<T,EXCLUSIVE,P>::emptyNode;
1137template <
class T,
bool EXCLUSIVE,
class P>
1138void DRAIN_TREE_NAME<T,EXCLUSIVE, P>::generateKey(
const DRAIN_TREE_NAME<T,EXCLUSIVE, P> & tree,
typename P::elem_t & key){
1139 std::stringstream k;
1143 k << tree.getChildren().size();
1160template <
class T,
bool EXCLUSIVE,
class P>
1166 const std::string & str(){
1169 tree_t::isOrdered()?
"Ordered":
"Unordered",
1170 tree_t::isMultiple()?
"Multi":
"",
"Tree",
1171 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:699
void clearChildren()
Clears the children of this node. Does not clear data.
Definition Tree.h:608
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:1003
void initChild(tree_t &child) const
Means for automatically setting something, for example the type of a node.
Definition Tree.h:782
const tree_t & operator()(const char *arg) const
Redirects the call to operator()(const std::string & arg) .
Definition Tree.h:573
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:1091
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:862
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:547
virtual const tree_t & getEmpty() const
Definition Tree.h:668
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:560
virtual bool empty() const
Check if the tree node has empty data and no children.
Definition Tree.h:675
const container_t & getChildren() const
Returns the map containing the children.
Definition Tree.h:984
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:567
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:991
tree_t & addChild(const key_t &key)
Add a child node. If UNORDERED and not MULTIPLE, reuse existing nodes.
Definition Tree.h:812
bool hasPath(typename path_t::const_iterator it, typename path_t::const_iterator eit) const
"Default implementation" of key conversion – the identity mapping.
Definition Tree.h:1049
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:997
void erase(const path_t &path)
Deletes a descendant node and hence its subtrees.
Definition Tree.h:625
container_t & getChildren()
Returns the map containing the children.
Definition Tree.h:980
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:538
void clearData()
Definition Tree.h:596
virtual bool hasChild(const key_t &key) const
Check if the tree node has a direct descendant with name key.
Definition Tree.h:734
void clear()
Clear children and node data.
Definition Tree.h:585
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:790
tree_t & operator()(const path_t &path)
Returns a descendant. Creates one if not existing already.
Definition Tree.h:517
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:1069
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