mirror of
https://github.com/Lurkki14/tuxclocker.git
synced 2025-02-25 18:55:24 -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;
|
std::vector<int> childIndices;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
class TreeNode;
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
struct FlatTree {
|
struct FlatTree {
|
||||||
std::vector<FlatTreeNode<T>> nodes;
|
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>
|
template <typename T>
|
||||||
@ -51,7 +76,6 @@ public:
|
|||||||
j++;
|
j++;
|
||||||
});
|
});
|
||||||
node.childIndices.push_back(index);
|
node.childIndices.push_back(index);
|
||||||
//j = 0, index = 0;
|
|
||||||
}
|
}
|
||||||
nodes.push_back(node);
|
nodes.push_back(node);
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user