general_spacetree.h

Go to the documentation of this file.
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_GENERAL_SPACETREE_H
00041 #define TREE_GENERAL_SPACETREE_H
00042 
00043 #include "fastlib/fastlib.h"
00044 //#include "fastlib/fastlib_int.h"
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   GeneralBinarySpaceTree() {
00085     DEBUG_ONLY(begin_ = BIG_BAD_NUMBER);
00086     DEBUG_ONLY(count_ = BIG_BAD_NUMBER);
00087     DEBUG_POISON_PTR(left_);
00088     DEBUG_POISON_PTR(right_);
00089   }
00090   
00091   ~GeneralBinarySpaceTree() {
00092     if (!is_leaf()) {
00093       delete left_;
00094       delete right_;
00095     }
00096     DEBUG_ONLY(begin_ = BIG_BAD_NUMBER);
00097     DEBUG_ONLY(count_ = BIG_BAD_NUMBER);
00098     DEBUG_POISON_PTR(left_);
00099     DEBUG_POISON_PTR(right_);
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   // TODO: Not const correct
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     // TODO: Const correctness
00206     return left_;
00207   }
00208 
00212   GeneralBinarySpaceTree *right() const {
00213     // TODO: Const correctness
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
Generated on Mon Jan 24 12:04:38 2011 for FASTlib by  doxygen 1.6.3