diff --git a/fastlib/trunk/contrib/dongryel/thesis_research/core/tree/gen_kdtree.h b/fastlib/trunk/contrib/dongryel/thesis_research/core/tree/gen_kdtree.h index 213dcd4d93..2cb202c6cc 100644 --- a/fastlib/trunk/contrib/dongryel/thesis_research/core/tree/gen_kdtree.h +++ b/fastlib/trunk/contrib/dongryel/thesis_research/core/tree/gen_kdtree.h @@ -93,12 +93,12 @@ class GenKdTree { } } - template + template static bool AttemptSplitting( const core::metric_kernels::AbstractMetric &metric_in, core::table::DenseMatrix& matrix, TreeType *node, TreeType **left, TreeType **right, int leaf_size, - int *old_from_new, + IndexType *old_from_new, core::table::MemoryMappedFile *m_file_in) { *left = NULL; diff --git a/fastlib/trunk/contrib/dongryel/thesis_research/core/tree/gen_metric_tree.h b/fastlib/trunk/contrib/dongryel/thesis_research/core/tree/gen_metric_tree.h index 10b7bbc972..e5e41817e4 100644 --- a/fastlib/trunk/contrib/dongryel/thesis_research/core/tree/gen_metric_tree.h +++ b/fastlib/trunk/contrib/dongryel/thesis_research/core/tree/gen_metric_tree.h @@ -132,12 +132,11 @@ class GenMetricTree { } } - template + template static bool AttemptSplitting( const core::metric_kernels::AbstractMetric &metric_in, core::table::DenseMatrix& matrix, TreeType *node, TreeType **left, - TreeType **right, int leaf_size, - int *old_from_new, + TreeType **right, int leaf_size, IndexType *old_from_new, core::table::MemoryMappedFile *m_file_in) { // Pick a random row. diff --git a/fastlib/trunk/contrib/dongryel/thesis_research/core/tree/general_spacetree.h b/fastlib/trunk/contrib/dongryel/thesis_research/core/tree/general_spacetree.h index 7ac3f5ac9c..c762fbf07e 100644 --- a/fastlib/trunk/contrib/dongryel/thesis_research/core/tree/general_spacetree.h +++ b/fastlib/trunk/contrib/dongryel/thesis_research/core/tree/general_spacetree.h @@ -36,6 +36,62 @@ extern core::table::MemoryMappedFile *global_m_file_; namespace core { namespace tree { + +template +class IndexInitializer { + public: + static void OldFromNew( + const core::table::DenseMatrix &matrix_in, + IndexType *old_from_new_out); + + static void NewFromOld( + const core::table::DenseMatrix &matrix_in, + IndexType *old_from_new_in, + IndexType *new_from_old_out); +}; + +template<> +class IndexInitializer< std::pair > { + public: + static void OldFromNew( + const core::table::DenseMatrix &matrix_in, + std::pair *old_from_new_out) { + for(int i = 0; i < matrix_in.n_cols(); i++) { + old_from_new_out[i] = std::pair(0, i); + } + } + + static void NewFromOld( + const core::table::DenseMatrix &matrix_in, + std::pair *old_from_new_in, + std::pair *new_from_old_out) { + for(int i = 0; i < matrix_in.n_cols(); i++) { + new_from_old_out[old_from_new_in[i].second] = std::pair(0, i); + } + } +}; + +template<> +class IndexInitializer< int > { + public: + static void OldFromNew( + const core::table::DenseMatrix &matrix_in, + int *old_from_new_out) { + for(int i = 0; i < matrix_in.n_cols(); i++) { + old_from_new_out[i] = i; + } + } + + static void NewFromOld( + const core::table::DenseMatrix &matrix_in, + int *old_from_new_in, + int *new_from_old_out) { + for(int i = 0; i < matrix_in.n_cols(); i++) { + new_from_old_out[old_from_new_in[i]] = i; + } + } +}; + template < class TreeSpecType > class GeneralBinarySpaceTree { private: @@ -287,6 +343,7 @@ class GeneralBinarySpaceTree { } } + template static void SplitTree( const core::metric_kernels::AbstractMetric &metric_in, core::table::DenseMatrix& matrix, @@ -294,7 +351,7 @@ class GeneralBinarySpaceTree { int leaf_size, int max_num_leaf_nodes, int *current_num_leaf_nodes, - int *old_from_new, + IndexType *old_from_new, int *num_nodes) { TreeType *left = NULL; @@ -353,23 +410,21 @@ class GeneralBinarySpaceTree { * original indexes to new indices * @param num_nodes the number of nodes constructed in total. */ + template static TreeType *MakeTree( const core::metric_kernels::AbstractMetric &metric_in, core::table::DenseMatrix& matrix, int leaf_size, int max_num_leaf_nodes = std::numeric_limits::max(), - int *old_from_new = NULL, - int *new_from_old = NULL, + IndexType *old_from_new = NULL, + IndexType *new_from_old = NULL, int *num_nodes = NULL) { TreeType *node = (core::table::global_m_file_) ? core::table::global_m_file_->Construct() : new TreeType(); - if(old_from_new) { - for(int i = 0; i < matrix.n_cols(); i++) { - old_from_new[i] = i; - } - } + // Initialize the old_from_new mapping. + IndexInitializer::OldFromNew(matrix, old_from_new); int num_nodes_in = 1; node->Init(0, matrix.n_cols()); @@ -385,11 +440,10 @@ class GeneralBinarySpaceTree { if(num_nodes) { *num_nodes = num_nodes_in; } - if(new_from_old) { - for(int i = 0; i < matrix.n_cols(); i++) { - new_from_old[old_from_new[i]] = i; - } - } + + // Finalize the new_from_old mapping from old_from_new mapping. + IndexInitializer::NewFromOld( + matrix, old_from_new, new_from_old); return node; }