LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

Tree.h

Go to the documentation of this file.
00001 //===- llvm/ADT/Tree.h - Generic n-way tree structure -----------*- C++ -*-===//
00002 // 
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 // 
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This class defines a generic N way tree node structure.  The tree structure
00011 // is immutable after creation, but the payload contained within it is not.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_ADT_TREE_H
00016 #define LLVM_ADT_TREE_H
00017 
00018 #include <vector>
00019 
00020 namespace llvm {
00021 
00022 template<class ConcreteTreeNode, class Payload>
00023 class Tree {
00024   std::vector<ConcreteTreeNode*> Children;        // This nodes children, if any
00025   ConcreteTreeNode              *Parent;          // Parent of this node...
00026   Payload                        Data;            // Data held in this node...
00027 
00028 protected:
00029   void setChildren(const std::vector<ConcreteTreeNode*> &children) {
00030     Children = children;
00031   }
00032 public:
00033   inline Tree(ConcreteTreeNode *parent) : Parent(parent) {}
00034   inline Tree(const std::vector<ConcreteTreeNode*> &children,
00035               ConcreteTreeNode *par) : Children(children), Parent(par) {}
00036 
00037   inline Tree(const std::vector<ConcreteTreeNode*> &children,
00038               ConcreteTreeNode *par, const Payload &data) 
00039     : Children(children), Parent(par), Data(data) {}
00040 
00041   // Tree dtor - Free all children
00042   inline ~Tree() {
00043     for (unsigned i = Children.size(); i > 0; --i)
00044       delete Children[i-1];
00045   }
00046 
00047   // Tree manipulation/walking routines...
00048   inline ConcreteTreeNode *getParent() const { return Parent; }
00049   inline unsigned getNumChildren() const { return Children.size(); }
00050   inline ConcreteTreeNode *getChild(unsigned i) const {
00051     assert(i < Children.size() && "Tree::getChild with index out of range!");
00052     return Children[i];
00053   }
00054 
00055   // Payload access...
00056   inline Payload &getTreeData() { return Data; }
00057   inline const Payload &getTreeData() const { return Data; }
00058 };
00059 
00060 } // End llvm namespace
00061 
00062 #endif