16 template<
class Data_,
class Key_ = Id,
template<
class>
class Alloc = SmallAllocator>
19 template<
class,
class,
template<
class>
class>
friend class TreeNode;
25 typedef list<deref_wrap<TreeNode>, Alloc<deref_wrap<TreeNode>>>
ChildList;
51 TreeNode(
const Data& data) : _data(data) { init(); }
52 TreeNode(
const Data& data,
const Key& key) : _data(data), _key(key) { init(); }
58 if (hasListenerList()) _listenerList().template dispatch<sigDestroy>(*this);
65 if (hasListenerList()) _listenerList().template dispatch<sigSetData>(*
this, _data);
68 const Data&
getData()
const {
return _data; }
71 operator const Data&()
const {
return getData(); }
85 ChildMapIter itMap = _parent->childMapIter(*
this);
86 assert(itMap != _parent->_childMap.end(),
sout() <<
"Child not found in parent's children map. Parent Id: " << _parent->_key <<
" ; Child Id: " << _key);
87 _parent->_childMap.erase(itMap);
93 if (
hasParent()) _parent->_childMap.insert(make_pair(_key,
this));
94 if (hasListenerList()) _listenerList().template dispatch<sigSetKey>(*
this, _key);
99 const Key&
getKey()
const {
return _key; }
105 if (parent)
return parent->
addChild(*
this);
106 return _childList.end();
132 if (child.hasListenerList()) child._listenerList().template dispatch<sigSetParent>(
child,
this);
133 child._parent =
this;
135 child._itSib = _childList.insert(pos, &child);
137 if (child._key != _keyNull) _childMap.insert(make_pair(child._key, &child));
138 if (hasListenerList()) _listenerList().template dispatch<sigInsertChild>(*
this, pos != _childList.end() ? pos->ptr() :
nullptr,
child);
145 ChildIter itChild =
childPos(child);
146 if (itChild != _childList.end())
return removeChild(itChild);
147 return _childList.end();
154 assert(child._parent ==
this);
155 if (hasListenerList()) _listenerList().template dispatch<sigRemoveChild>(*
this,
child);
156 if (child.hasListenerList()) child._listenerList().template dispatch<sigSetParent>(
child,
nullptr);
157 child._parent =
nullptr;
158 child._itSib = child._childList.end();
160 auto itRet = _childList.erase(pos);
162 if (child._key != _keyNull)
164 ChildMapIter itMap = childMapIter(child);
165 assert(itMap != _childMap.end(),
sout() <<
"Child not found in parent's children map. Parent Id: " << _key <<
" ; Child Id: " << child._key);
166 _childMap.erase(itMap);
191 auto it = _childMap.find(key);
192 return it != _childMap.end() ?
ChildConstIter(it->second->_itSib) : _childList.end();
194 ChildIter
childPos(
const Key& key) {
auto ret =
const_cast<const TreeNode*
>(
this)->
childPos(key);
return reinterpret_cast<ChildIter&
>(ret); }
203 auto it = _childMap.find(key);
204 return it != _childMap.end() ? it->second.ptr() :
nullptr;
214 {
return range(_childMap.equal_range(key)); }
236 while (parent->
hasParent()) parent = parent->_parent;
250 if (node == &ancestor)
return true;
260 if (_key == key)
return this;
276 template<
class TreeNode>
286 PreOrdIter_() : _root(nullptr), _node(nullptr), _skipChildren(false), _count(0) {}
289 _skipChildren(false),
294 _node = (!end) ? _root : _root->_parent;
299 if (_node == _root->_parent)
return *
this;
301 if (!_node->
isLeaf() && !_skipChildren)
304 _node = _node->_childList.begin()->ptr();
309 _skipChildren =
false;
311 for (parent = _node; parent != _root->_parent; parent = parent->_parent)
316 parent = begin(parent->
sibNext())->ptr();
330 if (_node == _root)
return *
this;
333 if (_node == _root->_parent || _node->
sibHasPrev())
336 _node = (_node == _root->_parent) ? _root : begin(_node->
sibPrev())->ptr();
337 while (_node->
childCount() > 0) _node = (--_node->_childList.end())->ptr();
342 _node = _node->_parent;
392 _itSib = _childList.end();
400 ListenerList& _listenerList() {
if (_listenerList_p)
return *_listenerList_p;
return *(_listenerList_p = make_unique<ListenerList>()); }
401 bool hasListenerList()
const {
return _listenerList_p; }
407 ChildList _childList;
410 UniquePtr<ListenerList> _listenerList_p;
412 static const Key _keyNull;
415 template<
class Data,
class Key,
template<
class>
class Alloc>
416 const Key TreeNode<Data,Key,Alloc>::_keyNull;
Range_< ChildConstIter, ChildConstIter > children() const
Get all children.
Definition: Tree.h:175
PreOrdIter_ operator++(int)
Definition: Tree.h:349
PreOrdIter_ & operator++()
Definition: Tree.h:297
Range_< ChildConstIter, ChildConstIter > sibNext() const
Returns forward iterator range starting at next sibling.
Definition: Tree.h:217
stdutil::unordered_multimap< Key, deref_wrap< TreeNode >, Alloc > ChildMap
Map holding children at keys.
Definition: Tree.h:32
std::bidirectional_iterator_tag iterator_category
Definition: Tree.h:280
Range_< PreOrdConstIterR, PreOrdConstIterR > preOrdR() const
Get a reversed depth-first pre-order range.
Definition: Tree.h:382
ChildIter setChild(ChildIter pos, TreeNode &child)
Set child into list at position. Returns position of child in list.
Definition: Tree.h:124
Range_< PreOrdIterR, PreOrdIterR > preOrdR()
Definition: Tree.h:383
const Data & operator*() const
Get the data at this node.
Definition: Tree.h:74
Range_< ChildConstIterR, ChildConstIterR > sibPrev() const
Returns reverse iterator range starting at previous sibling.
Definition: Tree.h:221
ListenerList & listeners()
Get listener list.
Definition: Tree.h:386
auto findVal(MultiMap &map, const Key &key, const Val &val) -> mt_iterOf(map)
Get iterator to key with value. Returns end if not found.
Definition: StdUtil.h:70
Unrooted acyclic tree.
Definition: Tree.h:17
ptrdiff_t sdt
Size difference type, shorthand for ptrdiff_t.
Definition: Core.h:92
ChildMap::iterator ChildMapIter
Definition: Tree.h:33
TreeNode * pointer
Definition: Tree.h:283
Depth-first pre-order traversal.
Definition: Tree.h:277
Data & operator->()
Definition: Tree.h:77
bool hasParent() const
Check if node has a parent.
Definition: Tree.h:113
ChildList::reverse_iterator ChildIterR
Definition: Tree.h:28
Range_< ChildConstIterR, ChildConstIterR > childrenR() const
Get all children in reverse order.
Definition: Tree.h:179
void skipChildren()
Skip the current node's children on next step of this iterator.
Definition: Tree.h:359
ChildConstIter childPos(const TreeNode &child) const
Get child position in list. Returns children().end() if not in parent.
Definition: Tree.h:197
Range_< ChildIter, ChildIter > children()
Definition: Tree.h:176
std::enable_if< mt::isIterator< Iter1 >::value, Range_< Iter1, Iter2 > >::type range(Iter1 &&first, Iter2 &&last)
Range from iterators [first, last)
Definition: Range.h:116
ChildIter insertChild(ChildIter pos, TreeNode &child)
Insert child into list at position. Returns position of child in list.
Definition: Tree.h:127
szt sibCount() const
Get number of siblings (count doesn't include this node)
Definition: Tree.h:225
bool isRoot() const
Check if this is a root node at the top of a tree.
Definition: Tree.h:242
const Data & getData() const
Definition: Tree.h:68
ChildIter removeChild(TreeNode &child)
Remove child from children list. Returns position of next child, or children().end() if child doesn't...
Definition: Tree.h:143
sdt difference_type
Definition: Tree.h:282
bool operator!=(const PreOrdIter_ &rhs) const
Definition: Tree.h:353
PreOrdIter_< const TreeNode > PreOrdConstIter
Definition: Tree.h:372
szt count() const
Get the number of nodes between the current position and the beginning of the iteration.
Definition: Tree.h:362
ChildIter removeChild(ChildIter pos)
Remove child at position in list. Returns position of next child. Will fail if pos is invalid...
Definition: Tree.h:151
const TreeNode & root() const
Get the root (top-most) node of the tree that contains this node.
Definition: Tree.h:233
PreOrdIter_()
Definition: Tree.h:286
std::reverse_iterator< PreOrdIter > PreOrdIterR
Definition: Tree.h:375
ChildList::const_reverse_iterator ChildConstIterR
Definition: Tree.h:29
virtual ~TreeNode()
Definition: Tree.h:54
Key & getKey()
Get key used to identify this node.
Definition: Tree.h:98
ChildIter childPos(const Key &key)
Definition: Tree.h:194
bool hasChild(const TreeNode &child) const
Check if this node has a child in its list.
Definition: Tree.h:209
TreeNode(const Data &data)
Definition: Tree.h:51
ostringstream sout()
Shorthand to create ostringstream.
Definition: Stream.h:15
ChildIter addChild(TreeNode &child)
Add child into list, returns position of child in list.
Definition: Tree.h:116
ChildIter setParent(TreeNode *parent)
Set parent node. Returns position in new parent's child list, or children().end() if parent is null...
Definition: Tree.h:102
ChildIter childPos(const TreeNode &child)
Definition: Tree.h:198
Data & operator*()
Definition: Tree.h:75
TreeNode * findNode(const Key &key)
Definition: Tree.h:268
Range_< PreOrdConstIter, PreOrdConstIter > preOrd() const
Get a depth-first pre-order range.
Definition: Tree.h:378
Range_< ChildIter, ChildIter > sibNext()
Definition: Tree.h:218
#define assert(...)
Forwards to assert_#args. See assert_1(), assert_2().
Definition: Debug.h:24
PreOrdIter_< TreeNode > PreOrdIter
Definition: Tree.h:371
list< deref_wrap< TreeNode >, Alloc< deref_wrap< TreeNode > > > ChildList
Child linked list.
Definition: Tree.h:25
TreeNode * child(const Key &key)
Definition: Tree.h:206
SIGNAL(sigDestroy,(TreeNode &src))
Called before class is destroyed.
TreeNode & root()
Definition: Tree.h:239
ChildList::iterator ChildIter
Definition: Tree.h:26
bool isAncestor(const TreeNode &ancestor) const
Test if ancestor is this node's ancestor. Returns false if ancestor is same as this node...
Definition: Tree.h:247
size_t szt
Size type, shorthand for size_t.
Definition: Core.h:90
#define SIGNAL_DECL(BaseClass)
Call once inside a class that has signals.
Definition: Signal.h:18
TreeNode & reference
Definition: Tree.h:284
std::unordered_multimap< Key, Value, std::hash< Key >, std::equal_to< Key >, Alloc< pair< const Key, Value >>> unordered_multimap
std::unordered_multimap with custom allocator
Definition: StdUtil.h:86
pointer operator->() const
Definition: Tree.h:356
ChildList::const_iterator ChildConstIter
Definition: Tree.h:27
bool hasChildren() const
Check if node has children.
Definition: Tree.h:186
Iterator range. See range(Iter1&&, Iter2&&) to create.
Definition: Range.h:88
szt childCount() const
Get number of children.
Definition: Tree.h:183
TreeNode(const Data &data, const Key &key)
Definition: Tree.h:52
ChildMap::const_iterator ChildMapConstIter
Definition: Tree.h:34
bool sibHasNext() const
Check if node has a next sibling.
Definition: Tree.h:228
Range_< PreOrdIter, PreOrdIter > preOrd()
Definition: Tree.h:379
PreOrdIter_(TreeNode *root, bool end)
Definition: Tree.h:288
const Key & getKey() const
Definition: Tree.h:99
PreOrdIter_ & operator--()
Definition: Tree.h:328
ChildConstIter childPos(const Key &key) const
Get child position in list. Returns first child found at key, or children().end() if not found...
Definition: Tree.h:189
PreOrdIter_ operator--(int)
Definition: Tree.h:350
std::reverse_iterator< PreOrdConstIter > PreOrdConstIterR
Definition: Tree.h:374
void setKey(const Key &key)
Set the key used to identify this node.
Definition: Tree.h:80
bool isAncestorOf(const TreeNode &node) const
Test if this node is an ancestor of node. Returns false if node is same as this node.
Definition: Tree.h:255
bool isLeaf() const
Check if this is a leaf node at the end of a tree branch.
Definition: Tree.h:244
bool operator==(const PreOrdIter_ &rhs) const
Definition: Tree.h:352
TreeNode * getParent()
Definition: Tree.h:111
const TreeNode * findNode(const Key &key) const
Returns first node found with key in tree of descendants (depth-first pre-order traversal) ...
Definition: Tree.h:258
Range_< ChildIterR, ChildIterR > childrenR()
Definition: Tree.h:180
reference operator*() const
Definition: Tree.h:355
void setData(const Data &data)
Set the data that this node contains.
Definition: Tree.h:62
TreeNode value_type
Definition: Tree.h:281
const Data & operator->() const
Definition: Tree.h:76
Range_< ChildIterR, ChildIterR > sibPrev()
Definition: Tree.h:222
Collection of listeners.
Definition: ListenerList.h:16
Global Honeycomb namespace.
Range_< ChildMapIter, ChildMapIter > children(const Key &key)
Get children that match the key. The child node TreeNode& is stored in the map pair ChildMapIter->sec...
Definition: Tree.h:212
TreeNode()
Definition: Tree.h:50
const TreeNode * getParent() const
Get parent node.
Definition: Tree.h:110
const TreeNode * child(const Key &key) const
Get child in list. Returns first child found at key, or null if not found.
Definition: Tree.h:201
Key_ Key
Definition: Tree.h:22
void clearChildren()
Clear all children.
Definition: Tree.h:172
bool sibHasPrev() const
Check if node has a previous sibling.
Definition: Tree.h:230
Data & getData()
Definition: Tree.h:69
Range_< ChildMapConstIter, ChildMapConstIter > children(const Key &key) const
Definition: Tree.h:213
Data_ Data
Definition: Tree.h:21