summaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2001-09-14 01:42:01 +0000
committerChris Lattner <sabre@nondot.org>2001-09-14 01:42:01 +0000
commit07ea7d7f7e8c7c89e40a27e2d7711f58792c1f2b (patch)
treef4feb5cd26bcdfeae98d8fc21eec05e230b79c2b /include
parent5f6baf78ea26168d328444f15bc1da355afd88a8 (diff)
downloadexternal_llvm-07ea7d7f7e8c7c89e40a27e2d7711f58792c1f2b.zip
external_llvm-07ea7d7f7e8c7c89e40a27e2d7711f58792c1f2b.tar.gz
external_llvm-07ea7d7f7e8c7c89e40a27e2d7711f58792c1f2b.tar.bz2
Generic k-way tree support
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@554 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'include')
-rw-r--r--include/llvm/Support/Tree.h52
1 files changed, 52 insertions, 0 deletions
diff --git a/include/llvm/Support/Tree.h b/include/llvm/Support/Tree.h
new file mode 100644
index 0000000..679b6df
--- /dev/null
+++ b/include/llvm/Support/Tree.h
@@ -0,0 +1,52 @@
+//===- llvm/Support/Tree.h - Generic n-way tree structure --------*- C++ -*--=//
+//
+// This class defines a generic N way tree node structure. The tree structure
+// is immutable after creation, but the payload contained within it is not.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_SUPPORT_TREE_H
+#define LLVM_SUPPORT_TREE_H
+
+#include <vector>
+
+template<class ConcreteTreeNode, class Payload>
+class Tree {
+ vector<ConcreteTreeNode*> Children; // This nodes children, if any
+ ConcreteTreeNode *Parent; // Parent of this node...
+ Payload Data; // Data held in this node...
+
+protected:
+ void setChildren(const vector<ConcreteTreeNode*> &children) {
+ Children = children;
+ }
+public:
+ inline Tree(ConcreteTreeNode *parent) : Parent(parent) {}
+ inline Tree(const vector<ConcreteTreeNode*> &children, ConcreteTreeNode *par)
+ : Children(children), Parent(par) {}
+
+ inline Tree(const vector<ConcreteTreeNode*> &children, ConcreteTreeNode *par,
+ const Payload &data)
+ : Children(children), Parent(parent), Data(data) {}
+
+ // 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; }
+};
+
+
+#endif