general_spacetree.h
Go to the documentation of this file.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_GENERAL_SPACETREE_H
00041 #define TREE_GENERAL_SPACETREE_H
00042
00043 #include "fastlib/fastlib.h"
00044
00045
00057 template<class TBound,
00058 class TDataset,
00059 class TStatistic = EmptyStatistic<TDataset> >
00060 class GeneralBinarySpaceTree {
00061 public:
00062 typedef TBound Bound;
00063 typedef TDataset Dataset;
00064 typedef TStatistic Statistic;
00065
00066 Bound bound_;
00067 GeneralBinarySpaceTree *left_;
00068 GeneralBinarySpaceTree *right_;
00069 index_t begin_;
00070 index_t count_;
00071 Statistic stat_;
00072
00073 OT_DEF(GeneralBinarySpaceTree) {
00074 OT_MY_OBJECT(bound_);
00075 OT_PTR_NULLABLE(left_);
00076 OT_PTR_NULLABLE(right_);
00077 OT_MY_OBJECT(begin_);
00078 OT_MY_OBJECT(count_);
00079 OT_MY_OBJECT(stat_);
00080 }
00081
00082 public:
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103 void Init(index_t begin_in, index_t count_in) {
00104 DEBUG_ASSERT(begin_ == BIG_BAD_NUMBER);
00105 DEBUG_POISON_PTR(left_);
00106 DEBUG_POISON_PTR(right_);
00107 begin_ = begin_in;
00108 count_ = count_in;
00109 }
00110
00122 const GeneralBinarySpaceTree* FindByBeginCount
00123 (index_t begin_q, index_t count_q) const {
00124 DEBUG_ASSERT(begin_q >= begin_);
00125 DEBUG_ASSERT(count_q <= count_);
00126 if (begin_ == begin_q && count_ == count_q) {
00127 return this;
00128 } else if (unlikely(is_leaf())) {
00129 return NULL;
00130 } else if (begin_q < right_->begin_) {
00131 return left_->FindByBeginCount(begin_q, count_q);
00132 } else {
00133 return right_->FindByBeginCount(begin_q, count_q);
00134 }
00135 }
00136
00148 GeneralBinarySpaceTree* FindByBeginCount
00149 (index_t begin_q, index_t count_q) {
00150 DEBUG_ASSERT(begin_q >= begin_);
00151 DEBUG_ASSERT(count_q <= count_);
00152 if (begin_ == begin_q && count_ == count_q) {
00153 return this;
00154 } else if (unlikely(is_leaf())) {
00155 return NULL;
00156 } else if (begin_q < right_->begin_) {
00157 return left_->FindByBeginCount(begin_q, count_q);
00158 } else {
00159 return right_->FindByBeginCount(begin_q, count_q);
00160 }
00161 }
00162
00163
00164
00168 void set_children
00169 (const Dataset& data, GeneralBinarySpaceTree *left_in,
00170 GeneralBinarySpaceTree *right_in) {
00171 left_ = left_in;
00172 right_ = right_in;
00173 if (is_leaf()) {
00174 stat_.Init(data, begin_, count_);
00175 }
00176 else {
00177 stat_.Init(data, begin_, count_, left_->stat_, right_->stat_);
00178 }
00179 }
00180
00181 const Bound& bound() const {
00182 return bound_;
00183 }
00184
00185 Bound& bound() {
00186 return bound_;
00187 }
00188
00189 const Statistic& stat() const {
00190 return stat_;
00191 }
00192
00193 Statistic& stat() {
00194 return stat_;
00195 }
00196
00197 bool is_leaf() const {
00198 return !left_;
00199 }
00200
00204 GeneralBinarySpaceTree *left() const {
00205
00206 return left_;
00207 }
00208
00212 GeneralBinarySpaceTree *right() const {
00213
00214 return right_;
00215 }
00216
00220 index_t begin() const {
00221 return begin_;
00222 }
00223
00227 index_t end() const {
00228 return begin_ + count_;
00229 }
00230
00234 index_t count() const {
00235 return count_;
00236 }
00237
00238 void Print() const {
00239 if (!is_leaf()) {
00240 printf("internal node: %d to %d: %d points total\n",
00241 begin_, begin_ + count_ - 1, count_);
00242 }
00243 else {
00244 printf("leaf node: %d to %d: %d points total\n",
00245 begin_, begin_ + count_ - 1, count_);
00246 }
00247
00248 if (!is_leaf()) {
00249 left_->Print();
00250 right_->Print();
00251 }
00252 }
00253
00254 };
00255
00256 #endif