1 //===- llvm/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
13 template<class ConcreteTreeNode, class Payload>
15 vector<ConcreteTreeNode*> Children; // This nodes children, if any
16 ConcreteTreeNode *Parent; // Parent of this node...
17 Payload Data; // Data held in this node...
20 void setChildren(const vector<ConcreteTreeNode*> &children) {
24 inline Tree(ConcreteTreeNode *parent) : Parent(parent) {}
25 inline Tree(const vector<ConcreteTreeNode*> &children, ConcreteTreeNode *par)
26 : Children(children), Parent(par) {}
28 inline Tree(const vector<ConcreteTreeNode*> &children, ConcreteTreeNode *par,
30 : Children(children), Parent(parent), Data(data) {}
32 // Tree dtor - Free all children
34 for (unsigned i = Children.size(); i > 0; --i)
38 // Tree manipulation/walking routines...
39 inline ConcreteTreeNode *getParent() const { return Parent; }
40 inline unsigned getNumChildren() const { return Children.size(); }
41 inline ConcreteTreeNode *getChild(unsigned i) const {
42 assert(i < Children.size() && "Tree::getChild with index out of range!");
47 inline Payload &getTreeData() { return Data; }
48 inline const Payload &getTreeData() const { return Data; }