32 #ifndef DRAIN_TREE_NAME
33 #warning "Use TreeOrdered.h or TreeUnordered.h to include this file."
42 #include <drain/Log.h>
43 #include <drain/Type.h>
44 #include <drain/TypeUtils.h>
48 #include "TreeUtils.h"
182 template <
class T,
bool EXCLUSIVE=
false,
class P=
drain::Path<std::string,
'/'> >
187 typedef T node_data_t;
189 typedef typename path_t::elem_t key_t;
191 typedef std::pair<key_t,tree_t> pair_t;
193 typedef DRAIN_TREE_CONTAINER(key_t,
tree_t) container_t;
195 typedef typename container_t::iterator iterator;
196 typedef typename container_t::const_iterator const_iterator;
220 bool hasMultipleData()
const {
226 const node_data_t & getData()
const {
return data;};
229 node_data_t & getData(){
return data;};
232 const node_data_t & getData(
const key_t & key)
const {
233 return retrieveChild(key).
data;
237 node_data_t & getData(
const key_t & key){
238 return retrieveChild(key).
data;
245 typename container_t::const_iterator
begin()
const {
return children.begin(); };
249 typename container_t::const_iterator
end()
const {
return children.end(); };
253 typename container_t::iterator
begin(){
return children.begin(); };
257 typename container_t::iterator
end(){
return children.end(); };
269 #ifdef DRAIN_TREE_MULTI
278 #ifdef DRAIN_TREE_ORDERED
355 tree_t & child = retrieveChild(entry.first);
356 child.
data = entry.second;
363 return retrieveChild(entry.first);
366 return (*
this << entry);
372 operator const node_data_t &()
const {
377 operator node_data_t &(){
385 return retrieveChild(key);
391 return retrieveChild(key);
409 return get(path.begin(), path.end());
446 return get(path.begin(), path.end());
515 typename path_t::const_iterator pit = path.end();
516 if (pit == path.begin())
523 tree_t & parent = this->
get(path.begin(), pit);
525 #ifdef DRAIN_TREE_ORDERED
528 parent.children.erase(*pit);
533 for (iterator it = children.begin(); it != children.end(); ++it){
534 if (it->first == *pit){
536 parent.children.erase(it);
566 return (
data.empty() && !hasChildren());
571 bool hasChildren()
const {
572 return !children.empty();
592 #ifdef DRAIN_TREE_ORDERED
595 return (children.find(key) == children.end()) ? 0 : 1;
606 for (
const auto & entry: children){
607 if (entry.first == key){
626 #ifdef DRAIN_TREE_ORDERED
628 return (children.find(key) != children.end());
632 for (
const auto & entry: children){
633 if (entry.first == key){
644 bool hasPath(
const path_t & path)
const {
645 return hasPath(path.begin(), path.end());
675 #ifdef DRAIN_TREE_ORDERED
676 iterator it = children.find(key);
677 if (it != children.end()){
681 return children.insert(children.begin(), pair_t(key,
tree_t()))->second;
689 for (
auto & entry: children){
690 if (entry.first == key){
697 children.push_back(pair_t(key,
tree_t()));
698 return children.back().second;
707 tree_t & retrieveChild(
const key_t & key){
714 #ifdef DRAIN_TREE_ORDERED
718 iterator it = children.find(key);
719 if (it != children.end()){
723 return children.insert(children.begin(), pair_t(key, tree_t()))->second;
728 for (
auto & entry: children){
729 if (entry.first == key){
734 children.push_back(pair_t(key, tree_t()));
735 return children.back().second;
742 const tree_t & retrieveChild(
const key_t & key)
const {
747 #ifdef DRAIN_TREE_ORDERED
748 const const_iterator it = children.find(key);
749 if (it != children.end()){
753 for (
const auto & entry: children){
754 if (entry.first == key){
795 children.swap(t.children);
800 container_t children;
803 const tree_t emptyNode;
810 bool hasPath(
typename path_t::const_iterator it,
typename path_t::const_iterator eit)
const {
815 const typename path_t::elem_t elem = *it;
818 return hasPath(++it, eit);
830 tree_t &
get(
typename path_t::const_iterator it,
typename path_t::const_iterator eit) {
838 return get(++it, eit);
841 return child.
get(++it, eit);
852 const tree_t &
get(
typename path_t::const_iterator it,
typename path_t::const_iterator eit)
const {
860 return get(++it, eit);
866 return retrieveChild(*it).
get(++it, eit);
895 template <
class T,
bool EXCLUSIVE,
class P>
896 const DRAIN_TREE_NAME<T,EXCLUSIVE, P> DRAIN_TREE_NAME<T,EXCLUSIVE,P>::emptyNode;
901 template <
class T,
bool EXCLUSIVE,
class P>
907 const std::string & str(){
910 tree_t::isOrdered()?
"Ordered":
"Unordered",
911 tree_t::isMulti()?
"Multi":
"",
"Tree",
912 tree_t::isExclusive()?
"(Exclusive)":
"",
A templated class for directed rooted trees.
Definition: Tree.h:183
virtual int hasChildren(const key_t &key) const
The number of children with name key.
Definition: Tree.h:590
void clearChildren()
Clears the children of this node. Does not clear data.
Definition: Tree.h:493
void swap(tree_t &t)
Replace children (but no data?)
Definition: Tree.h:794
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:452
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:445
container_t::const_iterator end() const
Child iterator pointing beyond the last child.
Definition: Tree.h:249
node_data_t * operator->()
Fast access to data, applied widely in TreeXML (HTML/SVG)
Definition: Tree.h:788
const node_data_t * operator->() const
Fast access to data, applied widely in TreeXML (HTML/SVG)
Definition: Tree.h:782
container_t & getChildren()
Returns the map containing the children.
Definition: Tree.h:771
node_data_t data
Contents (data) of the node.
Definition: Tree.h:211
container_t::iterator begin()
Child iterator pointing to the first child.
Definition: Tree.h:253
tree_t & operator()(const path_t &path)
Returns a descendant. Creates one if not existing already.
Definition: Tree.h:403
virtual bool empty() const
Check if the tree structure is empty.
Definition: Tree.h:561
const container_t & getChildren() const
Returns the map containing the children.
Definition: Tree.h:775
const tree_t & operator()(const char *arg) const
Redirects the call to operator()(const std::string & arg) .
Definition: Tree.h:458
const tree_t & operator[](const key_t &key) const
Child addressing operator.
Definition: Tree.h:390
tree_t & operator[](const key_t &key)
Child addressing operator.
Definition: Tree.h:384
DRAIN_TREE_NAME(const DRAIN_TREE_NAME &t)
Copy constructor; copy only node data at the root.
Definition: Tree.h:208
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:830
virtual tree_t & addChild(const key_t &key=key_t())
Add a child node. If unordered and UNIQUE, reuse existing nodes.
Definition: Tree.h:667
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:810
void erase(const path_t &path)
Deletes a descendant node and hence its subtrees.
Definition: Tree.h:510
tree_t & operator=(const tree_t &t)
Copies the data of another node. Does not copy the children.
Definition: Tree.h:304
DRAIN_TREE_NAME()
Default constructor.
Definition: Tree.h:200
void clearData()
Definition: Tree.h:481
virtual bool hasChild(const key_t &key) const
Check if the tree node has a direct descendant with name key.
Definition: Tree.h:625
void clear()
Clear children and node data.
Definition: Tree.h:470
std::pair< key_t, node_data_t > node_pair_t
Experimental. Given pair<elem, data> assigns child[elem] = data;.
Definition: Tree.h:349
tree_t & operator()(const S &arg)
Returns a descendant. Creates one if not existing already.
Definition: Tree.h:423
tree_t & operator=(const T2 &v)
Assigns a value to contents.
Definition: Tree.h:340
container_t::const_iterator begin() const
Child iterator pointing to the first child.
Definition: Tree.h:245
container_t::iterator end()
Child iterator end.
Definition: Tree.h:257
DRAIN_TREE_NAME(const node_data_t &data)
Copy constructor; copy only node data at the root.
Definition: Tree.h:204
virtual const tree_t & getEmpty() const
Definition: Tree.h:553
tree_t & operator=(const std::initializer_list< S > &l)
Assigns value to contents.
Definition: Tree.h:329
tree_t & operator()(const char *arg)
Redirects the call to operator()(const std::string & arg) .
Definition: Tree.h:432
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:852
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition: Log.h:308
Logger & attention(const TT &... args)
Possible error, but execution can continue. Special type of Logger::warn().
Definition: Log.h:472
Definition: StringBuilder.h:58
Definition: DataSelector.cpp:1277
static const std::string name
Default implementation: name returned by std::type_info::name()
Definition: Type.h:558