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>
45 #include <drain/Variable.h>
50 #include "TreeUtils.h"
184 template <
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(); };
272 #ifdef DRAIN_TREE_MULTI
281 #ifdef DRAIN_TREE_ORDERED
364 template <
typename K,
typename V>
367 for (
const auto & entry: l){
387 for (
const auto & entry: l){
388 this[entry.first] = entry.second;
409 template <
typename V>
440 tree_t & child = retrieveChild(entry.first);
441 child.
data = entry.second;
459 operator const node_data_t &()
const {
464 operator node_data_t &(){
472 return retrieveChild(key);
478 return retrieveChild(key);
490 return retrieveChild(
static_cast<key_t
>(key));
498 return retrieveChild(
static_cast<key_t
>(key));
516 return get(path.begin(), path.end());
553 return get(path.begin(), path.end());
622 typename path_t::const_iterator pit = path.end();
623 if (pit == path.begin())
630 tree_t & parent = this->
get(path.begin(), pit);
632 #ifdef DRAIN_TREE_ORDERED
635 parent.children.erase(*pit);
640 for (iterator it = children.begin(); it != children.end(); ++it){
641 if (it->first == *pit){
643 parent.children.erase(it);
673 return (
data.empty() && !hasChildren());
678 bool hasChildren()
const {
679 return !children.empty();
699 #ifdef DRAIN_TREE_ORDERED
702 return (children.find(key) == children.end()) ? 0 : 1;
713 for (
const auto & entry: children){
714 if (entry.first == key){
733 #ifdef DRAIN_TREE_ORDERED
735 return (children.find(key) != children.end());
739 for (
const auto & entry: children){
740 if (entry.first == key){
750 template <
typename K>
754 return hasChild(
static_cast<key_t
>(key));
759 bool hasPath(
const path_t & path)
const {
760 return hasPath(path.begin(), path.end());
785 throw std::runtime_error(
drain::StringBuilder<':'>(__FILE__,__FUNCTION__,
" empty key (ADD static child counter based naming"));
792 #ifdef DRAIN_TREE_ORDERED
793 iterator it = children.find(key);
794 if (it != children.end()){
798 return children.insert(children.begin(), pair_t(key,
tree_t()))->second;
806 for (
auto & entry: children){
807 if (entry.first == key){
814 children.push_back(pair_t(key,
tree_t()));
815 return children.back().second;
824 tree_t & retrieveChild(
const key_t & key){
831 #ifdef DRAIN_TREE_ORDERED
835 iterator it = children.find(key);
836 if (it != children.end()){
840 return children.insert(children.begin(), pair_t(key, tree_t()))->second;
845 for (
auto & entry: children){
846 if (entry.first == key){
851 children.push_back(pair_t(key, tree_t()));
852 return children.back().second;
859 const tree_t & retrieveChild(
const key_t & key)
const {
864 #ifdef DRAIN_TREE_ORDERED
865 const const_iterator it = children.find(key);
866 if (it != children.end()){
870 for (
const auto & entry: children){
871 if (entry.first == key){
912 children.swap(t.children);
917 container_t children;
920 const tree_t emptyNode;
957 bool hasPath(
typename path_t::const_iterator it,
typename path_t::const_iterator eit)
const {
962 const typename path_t::elem_t elem = *it;
965 return hasPath(++it, eit);
977 tree_t &
get(
typename path_t::const_iterator it,
typename path_t::const_iterator eit) {
985 return get(++it, eit);
988 return child.
get(++it, eit);
999 const tree_t &
get(
typename path_t::const_iterator it,
typename path_t::const_iterator eit)
const {
1007 return get(++it, eit);
1013 return retrieveChild(*it).
get(++it, eit);
1042 template <
class T,
bool EXCLUSIVE,
class P>
1043 const DRAIN_TREE_NAME<T,EXCLUSIVE, P> DRAIN_TREE_NAME<T,EXCLUSIVE,P>::emptyNode;
1055 template <
class T,
bool EXCLUSIVE,
class P>
1061 const std::string & str(){
1064 tree_t::isOrdered()?
"Ordered":
"Unordered",
1065 tree_t::isMulti()?
"Multi":
"",
"Tree",
1066 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:697
void clearChildren()
Clears the children of this node. Does not clear data.
Definition: Tree.h:600
void swap(tree_t &t)
Replace children (but no data?)
Definition: Tree.h:911
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:559
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:552
container_t::const_iterator end() const
Child iterator pointing beyond the last child.
Definition: Tree.h:252
node_data_t * operator->()
Fast access to data, applied widely in TreeXML (HTML/SVG)
Definition: Tree.h:905
const node_data_t * operator->() const
Fast access to data, applied widely in TreeXML (HTML/SVG)
Definition: Tree.h:899
container_t & getChildren()
Returns the map containing the children.
Definition: Tree.h:888
node_data_t data
Contents (data) of the node.
Definition: Tree.h:214
container_t::iterator begin()
Child iterator pointing to the first child.
Definition: Tree.h:256
tree_t & operator=(const std::initializer_list< V > &l)
Assign data.
Definition: Tree.h:411
tree_t & operator()(const path_t &path)
Returns a descendant. Creates one if not existing already.
Definition: Tree.h:510
virtual bool empty() const
Check if the tree structure is empty.
Definition: Tree.h:668
const container_t & getChildren() const
Returns the map containing the children.
Definition: Tree.h:892
const tree_t & operator()(const char *arg) const
Redirects the call to operator()(const std::string & arg) .
Definition: Tree.h:565
const tree_t & operator[](const key_t &key) const
Child addressing operator.
Definition: Tree.h:477
tree_t & operator[](const key_t &key)
Child addressing operator.
Definition: Tree.h:471
const tree_t & operator[](const K &key) const
NEW 2025 templated child addressing operator.
Definition: Tree.h:496
DRAIN_TREE_NAME(const DRAIN_TREE_NAME &t)
Copy constructor; copy only node data at the root.
Definition: Tree.h:211
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:977
virtual tree_t & addChild(const key_t &key=key_t())
Add a child node. If unordered and UNIQUE, reuse existing nodes.
Definition: Tree.h:782
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:957
void erase(const path_t &path)
Deletes a descendant node and hence its subtrees.
Definition: Tree.h:617
tree_t & operator=(const tree_t &t)
Copies the data of another node. Does not copy the children.
Definition: Tree.h:296
DRAIN_TREE_NAME()
Default constructor.
Definition: Tree.h:203
void clearData()
Definition: Tree.h:588
virtual bool hasChild(const key_t &key) const
Check if the tree node has a direct descendant with name key.
Definition: Tree.h:732
void clear()
Clear children and node data.
Definition: Tree.h:577
std::pair< key_t, node_data_t > node_pair_t
Experimental. Given pair<elem, data> assigns child[elem] = data;.
Definition: Tree.h:421
tree_t & operator()(const S &arg)
Returns a descendant. Creates one if not existing already.
Definition: Tree.h:530
tree_t & operator=(const T2 &v)
Assigns value to contents.
Definition: Tree.h:349
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
virtual const tree_t & getEmpty() const
Definition: Tree.h:660
tree_t & operator=(std::initializer_list< std::pair< K, V > > l)
Assign tree structure (of depth one).
Definition: Tree.h:366
tree_t & operator[](const K &key)
NEW 2025 templated child addressing operator.
Definition: Tree.h:488
tree_t & operator()(const char *arg)
Redirects the call to operator()(const std::string & arg) .
Definition: Tree.h:539
tree_t & operator<<(const node_pair_t &entry)
Experimental. Given pair(elem, data) assigns child[elem] = data;.
Definition: Tree.h:436
tree_t & operator=(std::initializer_list< std::pair< const char *, const Variable > > l)
Assign tree structure (of depth one).
Definition: Tree.h:383
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:999
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition: Log.h:310
Logger & attention(const TT &... args)
Possible error, but execution can continue. Special type of Logger::warn().
Definition: Log.h:474
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