2004-09-01 22:55:40 +00:00
|
|
|
//===- llvm/ADT/Tree.h - Generic n-way tree structure -----------*- C++ -*-===//
|
2005-04-21 20:19:05 +00:00
|
|
|
//
|
2003-10-20 19:46:57 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 19:59:42 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 20:19:05 +00:00
|
|
|
//
|
2003-10-20 19:46:57 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-09-14 01:42:01 +00:00
|
|
|
//
|
2009-02-20 22:20:18 +00:00
|
|
|
// This class defines a generic N-way tree node structure. The tree structure
|
2001-09-14 01:42:01 +00:00
|
|
|
// is immutable after creation, but the payload contained within it is not.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-09-01 22:55:40 +00:00
|
|
|
#ifndef LLVM_ADT_TREE_H
|
|
|
|
#define LLVM_ADT_TREE_H
|
2001-09-14 01:42:01 +00:00
|
|
|
|
|
|
|
#include <vector>
|
2003-06-11 14:01:36 +00:00
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
namespace llvm {
|
|
|
|
|
2001-09-14 01:42:01 +00:00
|
|
|
template<class ConcreteTreeNode, class Payload>
|
|
|
|
class Tree {
|
2009-02-20 22:20:18 +00:00
|
|
|
std::vector<ConcreteTreeNode*> Children; // This node's children, if any.
|
|
|
|
ConcreteTreeNode *Parent; // Parent of this node.
|
|
|
|
Payload Data; // Data held in this node.
|
2001-09-14 01:42:01 +00:00
|
|
|
|
|
|
|
protected:
|
2002-01-20 22:54:45 +00:00
|
|
|
void setChildren(const std::vector<ConcreteTreeNode*> &children) {
|
2001-09-14 01:42:01 +00:00
|
|
|
Children = children;
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
inline Tree(ConcreteTreeNode *parent) : Parent(parent) {}
|
2002-01-20 22:54:45 +00:00
|
|
|
inline Tree(const std::vector<ConcreteTreeNode*> &children,
|
|
|
|
ConcreteTreeNode *par) : Children(children), Parent(par) {}
|
2001-09-14 01:42:01 +00:00
|
|
|
|
2002-01-20 22:54:45 +00:00
|
|
|
inline Tree(const std::vector<ConcreteTreeNode*> &children,
|
2005-04-21 20:19:05 +00:00
|
|
|
ConcreteTreeNode *par, const Payload &data)
|
2003-11-29 19:55:02 +00:00
|
|
|
: Children(children), Parent(par), Data(data) {}
|
2001-09-14 01:42:01 +00:00
|
|
|
|
|
|
|
// Tree dtor - Free all children
|
|
|
|
inline ~Tree() {
|
|
|
|
for (unsigned i = Children.size(); i > 0; --i)
|
|
|
|
delete Children[i-1];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tree manipulation/walking routines...
|
|
|
|
inline ConcreteTreeNode *getParent() const { return Parent; }
|
|
|
|
inline unsigned getNumChildren() const { return Children.size(); }
|
|
|
|
inline ConcreteTreeNode *getChild(unsigned i) const {
|
|
|
|
assert(i < Children.size() && "Tree::getChild with index out of range!");
|
|
|
|
return Children[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Payload access...
|
|
|
|
inline Payload &getTreeData() { return Data; }
|
|
|
|
inline const Payload &getTreeData() const { return Data; }
|
|
|
|
};
|
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
} // End llvm namespace
|
2001-09-14 01:42:01 +00:00
|
|
|
|
|
|
|
#endif
|