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());
563 return get(path.begin(), path.end());
632 typename path_t::const_iterator pit = path.end();
633 if (pit == path.begin())
640 tree_t & parent = this->
get(path.begin(), pit);
642 #ifdef DRAIN_TREE_ORDERED
645 parent.children.erase(*pit);
650 for (iterator it = children.begin(); it != children.end(); ++it){
651 if (it->first == *pit){
653 parent.children.erase(it);
683 return (
data.empty() && !hasChildren());
688 bool hasChildren()
const {
689 return !children.empty();
709 #ifdef DRAIN_TREE_ORDERED
712 return (children.find(key) == children.end()) ? 0 : 1;
723 for (
const auto & entry: children){
724 if (entry.first == key){
743 #ifdef DRAIN_TREE_ORDERED
745 return (children.find(key) != children.end());
749 for (
const auto & entry: children){
750 if (entry.first == key){
760 template <
typename K>
764 return hasChild(
static_cast<key_t
>(key));
769 bool hasPath(
const path_t & path)
const {
770 return hasPath(path.begin(), path.end());
795 throw std::runtime_error(
drain::StringBuilder<
':'>(__FILE__,__FUNCTION__,
" empty key (ADD static child counter based naming"));
802 #ifdef DRAIN_TREE_ORDERED
803 iterator it = children.find(key);
804 if (it != children.end()){
808 return children.insert(children.begin(), pair_t(key,
tree_t()))->second;
816 for (
auto & entry: children){
817 if (entry.first == key){
824 children.push_back(pair_t(key,
tree_t()));
825 return children.back().second;
834 tree_t & retrieveChild(
const key_t & key){
841 #ifdef DRAIN_TREE_ORDERED
845 iterator it = children.find(key);
846 if (it != children.end()){
850 return children.insert(children.begin(), pair_t(key, tree_t()))->second;
855 for (
auto & entry: children){
856 if (entry.first == key){
861 children.push_back(pair_t(key, tree_t()));
862 return children.back().second;
869 const tree_t & retrieveChild(
const key_t & key)
const {
874 #ifdef DRAIN_TREE_ORDERED
875 const const_iterator it = children.find(key);
876 if (it != children.end()){
880 for (
const auto & entry: children){
881 if (entry.first == key){
922 children.swap(t.children);
927 container_t children;
930 const tree_t emptyNode;
967 bool hasPath(
typename path_t::const_iterator it,
typename path_t::const_iterator eit)
const {
972 const typename path_t::elem_t elem = *it;
975 return hasPath(++it, eit);
987 tree_t &
get(
typename path_t::const_iterator it,
typename path_t::const_iterator eit) {
995 return get(++it, eit);
998 return child.
get(++it, eit);
1009 const tree_t &
get(
typename path_t::const_iterator it,
typename path_t::const_iterator eit)
const {
1017 return get(++it, eit);
1023 return retrieveChild(*it).
get(++it, eit);
1052template <
class T,
bool EXCLUSIVE,
class P>
1053const DRAIN_TREE_NAME<T,EXCLUSIVE, P> DRAIN_TREE_NAME<T,EXCLUSIVE,P>::emptyNode;
1065template <
class T,
bool EXCLUSIVE,
class P>
1071 const std::string & str(){
1074 tree_t::isOrdered()?
"Ordered":
"Unordered",
1075 tree_t::isMulti()?
"Multi":
"",
"Tree",
1076 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:707
void clearChildren()
Clears the children of this node. Does not clear data.
Definition Tree.h:610
virtual tree_t & addChild(const key_t &key=key_t())
Add a child node. If unordered and UNIQUE, reuse existing nodes.
Definition Tree.h:792
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:921
const tree_t & operator()(const char *arg) const
Redirects the call to operator()(const std::string & arg) .
Definition Tree.h:575
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:1009
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:549
virtual const tree_t & getEmpty() const
Definition Tree.h:670
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:562
virtual bool empty() const
Check if the tree structure is empty.
Definition Tree.h:678
const container_t & getChildren() const
Returns the map containing the children.
Definition Tree.h:902
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:569
const node_data_t * operator->() const
Fast access to data, applied widely in TreeXML (HTML/SVG)
Definition Tree.h:909
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:967
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:915
void erase(const path_t &path)
Deletes a descendant node and hence its subtrees.
Definition Tree.h:627
container_t & getChildren()
Returns the map containing the children.
Definition Tree.h:898
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:540
void clearData()
Definition Tree.h:598
virtual bool hasChild(const key_t &key) const
Check if the tree node has a direct descendant with name key.
Definition Tree.h:742
void clear()
Clear children and node data.
Definition Tree.h:587
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:987
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