1 //===- Support/Tree.h - Generic n-way tree structure -------------*- C++ -*--=//
3 // This class defines a generic N way tree node structure. The tree structure
4 // is immutable after creation, but the payload contained within it is not.
6 //===----------------------------------------------------------------------===//
8 #ifndef LLVM_SUPPORT_TREE_H
9 #define LLVM_SUPPORT_TREE_H
15 template<class ConcreteTreeNode, class Payload>
17 std::vector<ConcreteTreeNode*> Children; // This nodes children, if any
18 ConcreteTreeNode *Parent; // Parent of this node...
19 Payload Data; // Data held in this node...
22 void setChildren(const std::vector<ConcreteTreeNode*> &children) {
26 inline Tree(ConcreteTreeNode *parent) : Parent(parent) {}
27 inline Tree(const std::vector<ConcreteTreeNode*> &children,
28 ConcreteTreeNode *par) : Children(children), Parent(par) {}
30 inline Tree(const std::vector<ConcreteTreeNode*> &children,
31 ConcreteTreeNode *par, const Payload &data)
32 : Children(children), Parent(parent), Data(data) {}
34 // Tree dtor - Free all children
36 for (unsigned i = Children.size(); i > 0; --i)
40 // Tree manipulation/walking routines...
41 inline ConcreteTreeNode *getParent() const { return Parent; }
42 inline unsigned getNumChildren() const { return Children.size(); }
43 inline ConcreteTreeNode *getChild(unsigned i) const {
44 assert(i < Children.size() && "Tree::getChild with index out of range!");
49 inline Payload &getTreeData() { return Data; }
50 inline const Payload &getTreeData() const { return Data; }