spacetree.h

00001 /* MLPACK 0.2
00002  *
00003  * Copyright (c) 2008, 2009 Alexander Gray,
00004  *                          Garry Boyer,
00005  *                          Ryan Riegel,
00006  *                          Nikolaos Vasiloglou,
00007  *                          Dongryeol Lee,
00008  *                          Chip Mappus, 
00009  *                          Nishant Mehta,
00010  *                          Hua Ouyang,
00011  *                          Parikshit Ram,
00012  *                          Long Tran,
00013  *                          Wee Chin Wong
00014  *
00015  * Copyright (c) 2008, 2009 Georgia Institute of Technology
00016  *
00017  * This program is free software; you can redistribute it and/or
00018  * modify it under the terms of the GNU General Public License as
00019  * published by the Free Software Foundation; either version 2 of the
00020  * License, or (at your option) any later version.
00021  *
00022  * This program is distributed in the hope that it will be useful, but
00023  * WITHOUT ANY WARRANTY; without even the implied warranty of
00024  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00025  * General Public License for more details.
00026  *
00027  * You should have received a copy of the GNU General Public License
00028  * along with this program; if not, write to the Free Software
00029  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00030  * 02110-1301, USA.
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 //#include "statistic.h"
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   BinarySpaceTree() {
00087     DEBUG_ONLY(begin_ = BIG_BAD_NUMBER);
00088     DEBUG_ONLY(count_ = BIG_BAD_NUMBER);
00089     DEBUG_POISON_PTR(left_);
00090     DEBUG_POISON_PTR(right_);
00091   }
00092 
00093   ~BinarySpaceTree() {
00094     if (!is_leaf()) {
00095       delete left_;
00096       delete right_;
00097     }
00098     DEBUG_ONLY(begin_ = BIG_BAD_NUMBER);
00099     DEBUG_ONLY(count_ = BIG_BAD_NUMBER);
00100     DEBUG_POISON_PTR(left_);
00101     DEBUG_POISON_PTR(right_);
00102   }
00103   */
00104 
00105   void Init(index_t begin_in, index_t count_in) {
00106     //DEBUG_ASSERT(begin_ == BIG_BAD_NUMBER);
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   // TODO: Not const correct
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     // TODO: Const correctness
00209     return left_;
00210   }
00211 
00215   BinarySpaceTree *right() const {
00216     // TODO: Const correctness
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
Generated on Mon Jan 24 12:04:37 2011 for FASTlib by  doxygen 1.6.3