Included assert.h so that the code compiles under newer versions of GCC.
[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 LLVM_SUPPORT_TREE_H
9 #define LLVM_SUPPORT_TREE_H
10
11 #include <vector>
12
13 #include <assert.h>
14
15 template<class ConcreteTreeNode, class Payload>
16 class Tree {
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...
20
21 protected:
22   void setChildren(const std::vector<ConcreteTreeNode*> &children) {
23     Children = children;
24   }
25 public:
26   inline Tree(ConcreteTreeNode *parent) : Parent(parent) {}
27   inline Tree(const std::vector<ConcreteTreeNode*> &children,
28               ConcreteTreeNode *par) : Children(children), Parent(par) {}
29
30   inline Tree(const std::vector<ConcreteTreeNode*> &children,
31               ConcreteTreeNode *par, const Payload &data) 
32     : Children(children), Parent(parent), Data(data) {}
33
34   // Tree dtor - Free all children
35   inline ~Tree() {
36     for (unsigned i = Children.size(); i > 0; --i)
37       delete Children[i-1];
38   }
39
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!");
45     return Children[i];
46   }
47
48   // Payload access...
49   inline Payload &getTreeData() { return Data; }
50   inline const Payload &getTreeData() const { return Data; }
51 };
52
53
54 #endif