00001 // Copyright 2007, Massachusetts Institute of Technology. 00002 // The use of this code is permitted for research only. There is 00003 // absolutely no warranty for this software. 00004 // 00005 // Author: John Lee (jjl@mit.edu) 00006 // 00007 00008 #ifndef TREE_SPARSE_TREE_NODE_H 00009 #define TREE_SPARSE_TREE_NODE_H 00010 00011 #include <iostream> 00012 #include "util/sparse-vector.h" 00013 00014 using namespace std; 00015 00016 namespace libpmk { 00017 00019 00046 class SparseTreeNode { 00047 public: 00049 SparseTreeNode(); 00050 00052 SparseTreeNode(const LargeIndex& index); 00053 00055 00058 SparseTreeNode(const SparseTreeNode& other); 00059 00060 virtual ~SparseTreeNode(); 00061 00062 const LargeIndex& index() const { return index_; } 00063 SparseTreeNode* parent() { return parent_; } 00064 SparseTreeNode* prev_sibling() { return previous_sibling_; } 00065 SparseTreeNode* next_sibling() { return next_sibling_; } 00066 SparseTreeNode* first_child() { return first_child_; } 00067 SparseTreeNode* last_child() { return last_child_; } 00068 00069 void set_parent(SparseTreeNode* parent) { parent_ = parent; } 00070 void set_prev_sibling(SparseTreeNode* sibling) { 00071 previous_sibling_ = sibling; 00072 } 00073 void set_next_sibling(SparseTreeNode* sibling) { next_sibling_ = sibling; } 00074 void set_first_child(SparseTreeNode* child) { first_child_ = child; } 00075 void set_last_child(SparseTreeNode* child) { last_child_ = child; } 00076 void set_index(const LargeIndex& index) { index_ = index; } 00077 00078 bool has_parent() const { return parent_ != NULL; } 00079 bool has_prev_sibling() const { return previous_sibling_ != NULL; } 00080 bool has_next_sibling() const { return next_sibling_ != NULL; } 00081 bool has_child() const { return first_child_ != NULL; } 00082 00084 00094 void ReadFromStream(istream& input_stream); 00095 00097 00102 void WriteToStream(ostream& output_stream) const; 00103 00105 virtual void Combine(const SparseTreeNode& other) = 0; 00106 00108 bool operator<(const SparseTreeNode& other) const; 00109 00111 static bool CompareNodes(const SparseTreeNode* one, 00112 const SparseTreeNode* two); 00113 00114 protected: 00116 virtual void ReadData(istream& input_stream) = 0; 00117 00119 virtual void WriteData(ostream& output_stream) const = 0; 00120 private: 00121 LargeIndex index_; 00122 SparseTreeNode* parent_; 00123 SparseTreeNode* next_sibling_; 00124 SparseTreeNode* previous_sibling_; 00125 SparseTreeNode* first_child_; 00126 SparseTreeNode* last_child_; 00127 }; 00128 } // namespace libpmk 00129 00130 #endif // TREE_SPARSE_TREE_NODE_H