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(); };
272 #ifdef DRAIN_TREE_MULTI
281 #ifdef DRAIN_TREE_ORDERED
365 return this->
operator=(std::string(str));
374 template <
typename K,
typename V>
377 for (
const auto & entry: l){
397 for (
const auto & entry: l){
398 this[entry.first] = entry.second;
419 template <
typename V>
450 tree_t & child = retrieveChild(entry.first);
451 child.
data = entry.second;
469 operator const node_data_t &()
const {
474 operator node_data_t &(){
482 return retrieveChild(key);
488 return retrieveChild(key);
500 return retrieveChild(
static_cast<key_t
>(key));
508 return retrieveChild(
static_cast<key_t
>(key));
526 return get(path.begin(), path.end());
564 return get(path.begin(), path.end());
633 typename path_t::const_iterator pit = path.end();
634 if (pit == path.begin())
641 tree_t & parent = this->
get(path.begin(), pit);
643 #ifdef DRAIN_TREE_ORDERED
646 parent.children.erase(*pit);
651 for (iterator it = children.begin(); it != children.end(); ++it){
652 if (it->first == *pit){
654 parent.children.erase(it);
684 return (
data.empty() && !hasChildren());
689 bool hasChildren()
const {
690 return !children.empty();
710 #ifdef DRAIN_TREE_ORDERED
713 return (children.find(key) == children.end()) ? 0 : 1;
724 for (
const auto & entry: children){
725 if (entry.first == key){
744 #ifdef DRAIN_TREE_ORDERED
746 return (children.find(key) != children.end());
750 for (
const auto & entry: children){
751 if (entry.first == key){
761 template <
typename K>
765 return hasChild(
static_cast<key_t
>(key));
770 bool hasPath(
const path_t & path)
const {
771 return hasPath(path.begin(), path.end());
796 throw std::runtime_error(
drain::StringBuilder<
':'>(__FILE__,__FUNCTION__,
" empty key (ADD static child counter based naming"));
803 #ifdef DRAIN_TREE_ORDERED
804 iterator it = children.find(key);
805 if (it != children.end()){
809 return children.insert(children.begin(), pair_t(key,
tree_t()))->second;
817 for (
auto & entry: children){
818 if (entry.first == key){
825 children.push_back(pair_t(key,
tree_t()));
826 return children.back().second;
835 tree_t & retrieveChild(
const key_t & key){
842 #ifdef DRAIN_TREE_ORDERED
846 iterator it = children.find(key);
847 if (it != children.end()){
851 return children.insert(children.begin(), pair_t(key, tree_t()))->second;
856 for (
auto & entry: children){
857 if (entry.first == key){
862 children.push_back(pair_t(key, tree_t()));
863 return children.back().second;
870 const tree_t & retrieveChild(
const key_t & key)
const {
875 #ifdef DRAIN_TREE_ORDERED
876 const const_iterator it = children.find(key);
877 if (it != children.end()){
881 for (
const auto & entry: children){
882 if (entry.first == key){
923 children.swap(t.children);
928 container_t children;
931 const tree_t emptyNode;
968 bool hasPath(
typename path_t::const_iterator it,
typename path_t::const_iterator eit)
const {
973 const typename path_t::elem_t elem = *it;
976 return hasPath(++it, eit);
988 tree_t &
get(
typename path_t::const_iterator it,
typename path_t::const_iterator eit) {
996 return get(++it, eit);
999 return child.
get(++it, eit);
1010 const tree_t &
get(
typename path_t::const_iterator it,
typename path_t::const_iterator eit)
const {
1018 return get(++it, eit);
1024 return retrieveChild(*it).
get(++it, eit);
1053template <
class T,
bool EXCLUSIVE,
class P>
1054const DRAIN_TREE_NAME<T,EXCLUSIVE, P> DRAIN_TREE_NAME<T,EXCLUSIVE,P>::emptyNode;
1066template <
class T,
bool EXCLUSIVE,
class P>
1072 const std::string & str(){
1075 tree_t::isOrdered()?
"Ordered":
"Unordered",
1076 tree_t::isMulti()?
"Multi":
"",
"Tree",
1077 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:708
void clearChildren()
Clears the children of this node. Does not clear data.
Definition Tree.h:611
virtual tree_t & addChild(const key_t &key=key_t())
Add a child node. If unordered and UNIQUE, reuse existing nodes.
Definition Tree.h:793
tree_t & operator[](const K &key)
NEW 2025 templated child addressing operator.
Definition Tree.h:498
void swap(tree_t &t)
Replace children (but no data?)
Definition Tree.h:922
const tree_t & operator()(const char *arg) const
Redirects the call to operator()(const std::string & arg) .
Definition Tree.h:576
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:1010
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:296
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:421
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:506
tree_t & operator()(const char *arg)
Redirects the call to operator()(const std::string & arg) .
Definition Tree.h:550
virtual const tree_t & getEmpty() const
Definition Tree.h:671
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:563
virtual bool empty() const
Check if the tree structure is empty.
Definition Tree.h:679
const container_t & getChildren() const
Returns the map containing the children.
Definition Tree.h:903
tree_t & operator=(std::initializer_list< std::pair< K, V > > l)
Assign tree structure (of depth one).
Definition Tree.h:376
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:570
const node_data_t * operator->() const
Fast access to data, applied widely in TreeXML (HTML/SVG)
Definition Tree.h:910
const tree_t & operator[](const key_t &key) const
Child addressing operator.
Definition Tree.h:487
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:968
tree_t & operator=(const T2 &v)
Assigns value to contents.
Definition Tree.h:353
node_data_t * operator->()
Fast access to data, applied widely in TreeXML (HTML/SVG)
Definition Tree.h:916
void erase(const path_t &path)
Deletes a descendant node and hence its subtrees.
Definition Tree.h:628
container_t & getChildren()
Returns the map containing the children.
Definition Tree.h:899
tree_t & operator<<(const node_pair_t &entry)
Experimental. Given pair(elem, data) assigns child[elem] = data;.
Definition Tree.h:446
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:541
void clearData()
Definition Tree.h:599
virtual bool hasChild(const key_t &key) const
Check if the tree node has a direct descendant with name key.
Definition Tree.h:743
void clear()
Clear children and node data.
Definition Tree.h:588
std::pair< key_t, node_data_t > node_pair_t
Experimental. Given pair<elem, data> assigns child[elem] = data;.
Definition Tree.h:431
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 & operator()(const path_t &path)
Returns a descendant. Creates one if not existing already.
Definition Tree.h:520
tree_t & operator[](const key_t &key)
Child addressing operator.
Definition Tree.h:481
tree_t & operator=(std::initializer_list< std::pair< const char *, const Variable > > l)
Assign tree structure (of depth one).
Definition Tree.h:393
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:988
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:312
Logger & attention(const TT &... args)
Possible error, but execution can continue. Special type of Logger::warn().
Definition Log.h:476
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