spacetree.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00040 #ifndef TREE_SPACETREE_H
00041 #define TREE_SPACETREE_H
00042
00043 #include "fastlib/base/base.h"
00044 #include "fastlib/tree/statistic.h"
00045
00046
00058 template<class TBound,
00059 class TDataset,
00060 class TStatistic = EmptyStatistic<TDataset> >
00061 class BinarySpaceTree {
00062 public:
00063 typedef TBound Bound;
00064 typedef TDataset Dataset;
00065 typedef TStatistic Statistic;
00066
00067 private:
00068 Bound bound_;
00069 BinarySpaceTree *left_;
00070 BinarySpaceTree *right_;
00071 index_t begin_;
00072 index_t count_;
00073 Statistic stat_;
00074
00075 OT_DEF(BinarySpaceTree) {
00076 OT_MY_OBJECT(bound_);
00077 OT_PTR_NULLABLE(left_);
00078 OT_PTR_NULLABLE(right_);
00079 OT_MY_OBJECT(begin_);
00080 OT_MY_OBJECT(count_);
00081 OT_MY_OBJECT(stat_);
00082 }
00083
00084 public:
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105 void Init(index_t begin_in, index_t count_in) {
00106
00107 DEBUG_POISON_PTR(left_);
00108 DEBUG_POISON_PTR(right_);
00109 begin_ = begin_in;
00110 count_ = count_in;
00111 }
00112
00124 const BinarySpaceTree* FindByBeginCount(
00125 index_t begin_q, index_t count_q) const {
00126 DEBUG_ASSERT(begin_q >= begin_);
00127 DEBUG_ASSERT(count_q <= count_);
00128 if (begin_ == begin_q && count_ == count_q) {
00129 return this;
00130 } else if (unlikely(is_leaf())) {
00131 return NULL;
00132 } else if (begin_q < right_->begin_) {
00133 return left_->FindByBeginCount(begin_q, count_q);
00134 } else {
00135 return right_->FindByBeginCount(begin_q, count_q);
00136 }
00137 }
00138
00150 BinarySpaceTree* FindByBeginCount(
00151 index_t begin_q, index_t count_q) {
00152 DEBUG_ASSERT(begin_q >= begin_);
00153 DEBUG_ASSERT(count_q <= count_);
00154 if (begin_ == begin_q && count_ == count_q) {
00155 return this;
00156 } else if (unlikely(is_leaf())) {
00157 return NULL;
00158 } else if (begin_q < right_->begin_) {
00159 return left_->FindByBeginCount(begin_q, count_q);
00160 } else {
00161 return right_->FindByBeginCount(begin_q, count_q);
00162 }
00163 }
00164
00165
00166
00170 void set_children(const Dataset& data,
00171 BinarySpaceTree *left_in, BinarySpaceTree *right_in) {
00172 left_ = left_in;
00173 right_ = right_in;
00174 if (!is_leaf()) {
00175 stat_.Init(data, begin_, count_, left_->stat_, right_->stat_);
00176 DEBUG_ASSERT(count_ == left_->count_ + right_->count_);
00177 DEBUG_ASSERT(left_->begin_ == begin_);
00178 DEBUG_ASSERT(right_->begin_ == begin_ + left_->count_);
00179 } else {
00180 stat_.Init(data, begin_, count_);
00181 }
00182 }
00183
00184 const Bound& bound() const {
00185 return bound_;
00186 }
00187
00188 Bound& bound() {
00189 return bound_;
00190 }
00191
00192 const Statistic& stat() const {
00193 return stat_;
00194 }
00195
00196 Statistic& stat() {
00197 return stat_;
00198 }
00199
00200 bool is_leaf() const {
00201 return !left_;
00202 }
00203
00207 BinarySpaceTree *left() const {
00208
00209 return left_;
00210 }
00211
00215 BinarySpaceTree *right() const {
00216
00217 return right_;
00218 }
00219
00223 index_t begin() const {
00224 return begin_;
00225 }
00226
00230 index_t end() const {
00231 return begin_ + count_;
00232 }
00233
00237 index_t count() const {
00238 return count_;
00239 }
00240
00241 void Print() const {
00242 printf("node: %d to %d: %d points total\n",
00243 begin_, begin_ + count_ - 1, count_);
00244 if (!is_leaf()) {
00245 left_->Print();
00246 right_->Print();
00247 }
00248 }
00249
00250 };
00251
00252 #endif