10#include <tracking/trackFindingCDC/utilities/Algorithms.h>
11#include <tracking/trackFindingCDC/utilities/Functional.h>
12#include <framework/logging/Logger.h>
27 namespace TrackFindingCDC {
34 template<
class AProperties,
class ASubPropertiesFactory>
48 class Node :
public AProperties {
68 using AProperties::AProperties;
71 using AProperties::operator=;
92 B2ASSERT(
"DynTree datastructure only supports levels < 255",
getLevel() < 255);
95 Node* parentNode =
this;
100 child.m_parent =
this;
119 template<
class AWalker>
122 static_assert(std::is_assignable<std::function<bool(
Node*)>, AWalker>(),
"");
124 bool walkChildren = walker(
this);
126 if (children and walkChildren) {
127 for (
Node& childNode : *children) {
128 childNode.walk(walker);
138 template<
class AWalker,
class APriorityMeasure>
139 void walk(AWalker& walker, APriorityMeasure& priority)
141 static_assert(std::is_assignable<std::function<bool(
Node*)>, AWalker>(),
"");
142 static_assert(std::is_assignable<std::function<float(
Node*)>, APriorityMeasure>(),
"");
144 bool walkChildren = walker(
this);
146 if (children and walkChildren) {
147 std::vector<std::pair<float, Node*> > prioritisedChildNodes;
148 size_t nChildren = children->size();
150 prioritisedChildNodes.reserve(nChildren);
152 for (
Node& childNode : *children) {
153 float childPriority = priority(&childNode);
154 if (std::isnan(childPriority))
continue;
155 prioritisedChildNodes.push_back(std::make_pair(childPriority, &childNode));
158 std::make_heap(prioritisedChildNodes.begin(), prioritisedChildNodes.end());
160 while (not prioritisedChildNodes.empty()) {
161 std::pop_heap(prioritisedChildNodes.begin(), prioritisedChildNodes.end());
162 Node* priorityChildNode = prioritisedChildNodes.back().second;
163 prioritisedChildNodes.pop_back();
165 priorityChildNode->
walk(walker, priority);
169 erase_remove_if(prioritisedChildNodes,
170 [&reheap, &priority](std::pair<float, Node*>& prioritisedChildNode) {
171 float childPriority = priority(prioritisedChildNode.second);
172 if (std::isnan(childPriority))
return true;
174 reheap |= prioritisedChildNode.first != childPriority;
175 prioritisedChildNode.first = childPriority;
180 std::make_heap(prioritisedChildNodes.begin(), prioritisedChildNodes.end());
183 assert(prioritisedChildNodes.empty());
251 auto countNodes = [&nNodes](
const Node*) ->
bool {
266 std::map<int, int> nNodesByLevel;
267 auto countNodes = [&nNodesByLevel](
const Node * node) ->
bool {
268 if (nNodesByLevel.count(node->getLevel()) == 0)
270 nNodesByLevel[node->getLevel()] = 1;
273 nNodesByLevel[node->getLevel()]++;
279 return nNodesByLevel;
288 if (subProperties.empty()) {
292 result->resize(subProperties.size(),
Node(subProperties.back()));
294 for (
auto& properties : subProperties) {
295 clearIfApplicable(result->at(iSubNode));
296 result->at(iSubNode) = properties;
315 template<
class AWalker>
318 static_assert(std::is_assignable<std::function<bool(
Node*)>, AWalker>(),
"");
324 template<
class AWalker,
class APriorityMeasure>
325 void walk(AWalker& walker, APriorityMeasure& priority)
327 static_assert(std::is_assignable<std::function<bool(
Node*)>, AWalker>(),
"");
328 static_assert(std::is_assignable<std::function<float(
Node*)>, APriorityMeasure>(),
"");
340 for (
Node& node : children) {
341 clearIfApplicable(node);
Class for a node in the tree.
Children * m_children
Children that are created only if needed.
const Node * getParent() const
Const getter of the node one up the hierarchy.
friend Tree
Allow the tree access to the node constructor to create the top node.
Tree * m_tree
Reference to the tree that contains this node.
int m_level
Level of the node within the tree.
void walk(AWalker &walker, APriorityMeasure &priority)
Calls the walker with each node starting with the top node and continues depth first The walker can s...
void createChildren()
Creates the children of the node.
int getLevel() const
Get the level of the node.
void walk(AWalker &walker)
Calls the walker with each node starting with the top node and continues depth first The walker can s...
Children * getChildren()
Getter for the children.
AProperties Properties
Type of the Properties.
Node * m_parent
Parent in the tree hierarchy of this node.
void unlink()
Remove to node from the tree hierarchy.
Tree * getTree() const
Getter for the tree containing this node.
const Children * getChildren() const
Const getter for the children.
std::vector< Node > Children
Type of the container of the children of the node.
Node * getParent()
Getter of the node one up the hierarchy.
This is the base class for all hough trees.
void fell()
Fell to tree meaning deleting all child nodes from the tree. Keeps the top node.
ASubPropertiesFactory SubPropertiesFactory
Type of the factory for the sub node properties.
std::vector< Node > * getUnusedChildren()
Acquire the next unused child node structure, recycling all memory.
void raze()
Like fell but also releases all memory the tree has acquired during long execution.
DynTree(const Properties &properties, const SubPropertiesFactory &subPropertiesFactory=SubPropertiesFactory())
Constructor taking properties with which the top node of the tree is initialised.
Node m_topNode
Memory for the top node of the tree.
DynTree & operator=(const DynTree &)=delete
Forbid copy assignment.
DynTree(const DynTree &node)=delete
Forbid copy construction.
Node & getTopNode()
Getter for the top node of the tree.
std::deque< typename Node::Children > m_children
Central point to provide memory for the child structures.
void walk(AWalker &walker, APriorityMeasure &priority)
Forward walk to the top node.
void walk(AWalker &walker)
Forward walk to the top node.
std::vector< Node > * createChildren(Node *parentNode)
Create child nodes for the given parents.
const Node & getTopNode() const
Constant getter for the top node of the tree.
std::map< int, int > getNNodesByLevel() const
Gets the number of nodes by level in the tree Also demonstrates how to walk over the tree.
SubPropertiesFactory m_subPropertiesFactory
Instance of the properties factory for the sub nodes.
AProperties Properties
Type of the Properties.
int getNNodes() const
Gets the number of nodes currently contained in the tree Also demonstrates how to walk over the tree.
DynTree< AProperties, ASubPropertiesFactory > This
Type of this class.
size_t m_nUsedChildren
Last index of used children.
Abstract base class for different kinds of events.