mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-03-24 00:00:44 +01:00
TreeNode::parent algorithm
This commit is contained in:
@@ -2,6 +2,35 @@
|
||||
#include "tree_pool.h"
|
||||
#include "expression_node.h"
|
||||
|
||||
TreeNode * TreeNode::parent() const {
|
||||
int cursor = -1;
|
||||
TreeNode * parentsHistory[TreePool::MaxNumberOfNodes];
|
||||
int numberOfChildrenHistory[TreePool::MaxNumberOfNodes];
|
||||
int childrenVisitedCountHistory[TreePool::MaxNumberOfNodes];
|
||||
for (TreeNode * node : TreePool::sharedPool()->allNodes()) {
|
||||
if (node->identifier() == m_identifier) {
|
||||
printf("Parent of %d is %d\n", m_identifier, cursor >= 0 ? parentsHistory[cursor]->identifier() : -1);
|
||||
return cursor >= 0 ? parentsHistory[cursor] : nullptr;
|
||||
}
|
||||
if (cursor >= 0) {
|
||||
childrenVisitedCountHistory[cursor] = childrenVisitedCountHistory[cursor]+1;
|
||||
}
|
||||
int nodeChildrenCount = node->numberOfChildren();
|
||||
if (nodeChildrenCount > 0) {
|
||||
cursor++;
|
||||
parentsHistory[cursor] = node;
|
||||
numberOfChildrenHistory[cursor] = nodeChildrenCount;
|
||||
childrenVisitedCountHistory[cursor] = 0;
|
||||
} else {
|
||||
if (cursor >= 0 && (numberOfChildrenHistory[cursor] == childrenVisitedCountHistory[cursor])) {
|
||||
cursor--;
|
||||
}
|
||||
}
|
||||
}
|
||||
assert(false);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void TreeNode::release() {
|
||||
printf("Release %d(%p)\n", m_identifier, this);
|
||||
m_referenceCounter--;
|
||||
|
||||
@@ -19,15 +19,13 @@ class TreeNode {
|
||||
// friend class TreeReference<T>;
|
||||
friend class TreePool;
|
||||
public:
|
||||
virtual ~TreeNode() {
|
||||
}
|
||||
|
||||
virtual ~TreeNode() {}
|
||||
int indentifier() const { return m_identifier; }
|
||||
|
||||
// Iterators
|
||||
TreeNode * parent() const;
|
||||
|
||||
class Iterator {
|
||||
public:
|
||||
public:
|
||||
Iterator(TreeNode * node) : m_node(node) {}
|
||||
TreeNode * operator*() { return m_node; }
|
||||
bool operator!=(const Iterator& it) const { return (m_node != it.m_node); }
|
||||
@@ -73,7 +71,6 @@ public:
|
||||
|
||||
DepthFirst depthFirstChildren() { return DepthFirst(this); }
|
||||
|
||||
|
||||
int identifier() const { return m_identifier; }
|
||||
|
||||
#if TREE_LOGGING
|
||||
|
||||
33
tree_pool.h
33
tree_pool.h
@@ -6,6 +6,7 @@
|
||||
#include <new>
|
||||
|
||||
class TreePool {
|
||||
friend class TreeNode;
|
||||
public:
|
||||
static TreePool * sharedPool();
|
||||
|
||||
@@ -88,17 +89,37 @@ public:
|
||||
void log();
|
||||
#endif
|
||||
|
||||
private:
|
||||
TreePool() : m_cursor(m_buffer) { }
|
||||
static inline void insert(char * destination, char * source, size_t length);
|
||||
protected:
|
||||
constexpr static int BufferSize = 256;
|
||||
constexpr static int MaxNumberOfNodes = BufferSize/sizeof(TreeNode);
|
||||
|
||||
class AllPool {
|
||||
public:
|
||||
AllPool(TreeNode * node) : m_node(node) {}
|
||||
class Iterator : public TreeNode::Iterator {
|
||||
public:
|
||||
using TreeNode::Iterator::Iterator;
|
||||
Iterator & operator++() {
|
||||
m_node = m_node->next();
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
Iterator begin() const { return Iterator(m_node); }
|
||||
Iterator end() const { return Iterator(TreePool::sharedPool()->last()); }
|
||||
private:
|
||||
TreeNode * m_node;
|
||||
};
|
||||
AllPool allNodes() { return AllPool(*(begin())); }
|
||||
|
||||
TreeNode::DepthFirst::Iterator begin() const { return TreeNode::DepthFirst::Iterator(first()); }
|
||||
TreeNode::DepthFirst::Iterator end() const { return TreeNode::DepthFirst::Iterator(last()); }
|
||||
|
||||
char * m_cursor;
|
||||
private:
|
||||
TreePool() : m_cursor(m_buffer) { }
|
||||
static inline void insert(char * destination, char * source, size_t length);
|
||||
|
||||
constexpr static int BufferSize = 256;
|
||||
char * m_cursor;
|
||||
char m_buffer[BufferSize];
|
||||
constexpr static int MaxNumberOfNodes = BufferSize/sizeof(TreeNode);
|
||||
TreeNode * m_nodeForIdentifier[MaxNumberOfNodes];
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user