Added LLVM notice.
[oota-llvm.git] / include / llvm / ADT / Tree.h
1 //===- Support/Tree.h - Generic n-way tree structure ------------*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
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.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef SUPPORT_TREE_H
16 #define SUPPORT_TREE_H
17
18 #include <vector>
19
20 template<class ConcreteTreeNode, class Payload>
21 class Tree {
22   std::vector<ConcreteTreeNode*> Children;        // This nodes children, if any
23   ConcreteTreeNode              *Parent;          // Parent of this node...
24   Payload                        Data;            // Data held in this node...
25
26 protected:
27   void setChildren(const std::vector<ConcreteTreeNode*> &children) {
28     Children = children;
29   }
30 public:
31   inline Tree(ConcreteTreeNode *parent) : Parent(parent) {}
32   inline Tree(const std::vector<ConcreteTreeNode*> &children,
33               ConcreteTreeNode *par) : Children(children), Parent(par) {}
34
35   inline Tree(const std::vector<ConcreteTreeNode*> &children,
36               ConcreteTreeNode *par, const Payload &data) 
37     : Children(children), Parent(parent), Data(data) {}
38
39   // Tree dtor - Free all children
40   inline ~Tree() {
41     for (unsigned i = Children.size(); i > 0; --i)
42       delete Children[i-1];
43   }
44
45   // Tree manipulation/walking routines...
46   inline ConcreteTreeNode *getParent() const { return Parent; }
47   inline unsigned getNumChildren() const { return Children.size(); }
48   inline ConcreteTreeNode *getChild(unsigned i) const {
49     assert(i < Children.size() && "Tree::getChild with index out of range!");
50     return Children[i];
51   }
52
53   // Payload access...
54   inline Payload &getTreeData() { return Data; }
55   inline const Payload &getTreeData() const { return Data; }
56 };
57
58
59 #endif