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