mirror of
https://github.com/Lurkki14/tuxclocker.git
synced 2024-11-25 01:30:18 -06:00
lib: add Tree -> FlatTree
This commit is contained in:
parent
471ddfc980
commit
a2ab1b99b6
@ -12,9 +12,34 @@ struct FlatTreeNode {
|
||||
std::vector<int> childIndices;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class TreeNode;
|
||||
|
||||
template <typename T>
|
||||
struct FlatTree {
|
||||
std::vector<FlatTreeNode<T>> nodes;
|
||||
|
||||
static TreeNode<T> toTree(FlatTree<T> flatTree) {
|
||||
std::function<void(TreeNode<T>*, uint)> recur;
|
||||
recur = [&recur, flatTree](TreeNode<T> *node, uint i) {
|
||||
auto c_indices = flatTree.nodes[i].childIndices;
|
||||
for (auto index : c_indices)
|
||||
node->appendChild(TreeNode<T>(flatTree.nodes[index].value));
|
||||
|
||||
uint j = 0;
|
||||
for (auto &c_node : *(node->childrenPtr())) {
|
||||
recur(&c_node, c_indices[j]);
|
||||
j++;
|
||||
}
|
||||
};
|
||||
TreeNode<T> root;
|
||||
if (flatTree.nodes.size() > 0) {
|
||||
auto realRoot = TreeNode<T>(flatTree.nodes.front().value);
|
||||
recur(&realRoot, 0);
|
||||
root = realRoot;
|
||||
}
|
||||
return root;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
@ -51,7 +76,6 @@ public:
|
||||
j++;
|
||||
});
|
||||
node.childIndices.push_back(index);
|
||||
//j = 0, index = 0;
|
||||
}
|
||||
nodes.push_back(node);
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user