1 //===- Support/Tree.h - Generic n-way tree structure ------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This class defines a generic N way tree node structure. The tree structure
11 // is immutable after creation, but the payload contained within it is not.
13 //===----------------------------------------------------------------------===//
15 #ifndef SUPPORT_TREE_H
16 #define SUPPORT_TREE_H
22 template<class ConcreteTreeNode, class Payload>
24 std::vector<ConcreteTreeNode*> Children; // This nodes children, if any
25 ConcreteTreeNode *Parent; // Parent of this node...
26 Payload Data; // Data held in this node...
29 void setChildren(const std::vector<ConcreteTreeNode*> &children) {
33 inline Tree(ConcreteTreeNode *parent) : Parent(parent) {}
34 inline Tree(const std::vector<ConcreteTreeNode*> &children,
35 ConcreteTreeNode *par) : Children(children), Parent(par) {}
37 inline Tree(const std::vector<ConcreteTreeNode*> &children,
38 ConcreteTreeNode *par, const Payload &data)
39 : Children(children), Parent(par), Data(data) {}
41 // Tree dtor - Free all children
43 for (unsigned i = Children.size(); i > 0; --i)
47 // Tree manipulation/walking routines...
48 inline ConcreteTreeNode *getParent() const { return Parent; }
49 inline unsigned getNumChildren() const { return Children.size(); }
50 inline ConcreteTreeNode *getChild(unsigned i) const {
51 assert(i < Children.size() && "Tree::getChild with index out of range!");
56 inline Payload &getTreeData() { return Data; }
57 inline const Payload &getTreeData() const { return Data; }
60 } // End llvm namespace