diff --git a/fastlib/u/nvasil/tree/binary_tree_impl.h b/fastlib/u/nvasil/tree/binary_tree_impl.h index 8bb02f0f7c..0c5f19263f 100644 --- a/fastlib/u/nvasil/tree/binary_tree_impl.h +++ b/fastlib/u/nvasil/tree/binary_tree_impl.h @@ -210,6 +210,10 @@ void TREE__::NearestNeighbor(NodePtr_t ptr, NEIGHBORTYPE range, bool &found) { computations_.UpdateComparisons(); + Precision_t max_distance; + if (Loki::TypeTraits::isStdFloat==true) { + max_distance=range; + } if (!ptr->IsLeaf()){ computations_.UpdateComparisons(); pair child_pair = @@ -217,9 +221,13 @@ void TREE__::NearestNeighbor(NodePtr_t ptr, NearestNeighbor(child_pair.first, test_point, nearest_point, range, found); + if (Loki::TypeTraits::isStdFloat==false) { + max_distance=nearest_point->back().first; + } + if (child_pair.second->get_box().CrossesBoundaries(test_point, dimension_, - nearest_point->end()->first, + max_distance, computations_)) { NearestNeighbor(child_pair.second, test_point, @@ -230,8 +238,12 @@ void TREE__::NearestNeighbor(NodePtr_t ptr, if (found == true) { return; } else { + if (Loki::TypeTraits::isStdFloat==false) { + max_distance=nearest_point->back().first; + } found = ptr->get_box().IsWithin(test_point, - dimension_, nearest_point->end()->first, + dimension_, + max_distance, computations_)==0; if (found == true) { return; @@ -242,8 +254,11 @@ void TREE__::NearestNeighbor(NodePtr_t ptr, range, dimension_, discriminator_, computations_); + if (Loki::TypeTraits::isStdFloat==false) { + max_distance=nearest_point->back().first; + } found = ptr->get_box().IsWithin(test_point, dimension_, - nearest_point->end()->first, + max_distance, computations_); } } @@ -373,7 +388,7 @@ void TREE__::InitAllKNearestNeighborOutput(string file, fwrite(buffer, sizeof(typename Node_t::NNResult), (num_of_points_%kChunk)*knns, fp ); fclose(fp); - delete buffer; + delete []buffer; int fd=open(file.c_str(), O_RDWR); typename Node_t::NNResult *ptr =(typename Node_t::NNResult *)mmap(NULL, sizeof(typename Node_t::NNResult)*knns*num_of_points_, diff --git a/fastlib/u/nvasil/tree/binary_tree_impl.h~ b/fastlib/u/nvasil/tree/binary_tree_impl.h~ index 8538bb8c1d..c8d9daff7f 100644 --- a/fastlib/u/nvasil/tree/binary_tree_impl.h~ +++ b/fastlib/u/nvasil/tree/binary_tree_impl.h~ @@ -21,6 +21,7 @@ void TREE__::Init(BinaryDataset *data) { min_depth_ = numeric_limits::max(); max_points_on_leaf_ = 30; log_progress_=true; + pivoter_.Init(data_); } TEMPLATE__ @@ -209,6 +210,10 @@ void TREE__::NearestNeighbor(NodePtr_t ptr, NEIGHBORTYPE range, bool &found) { computations_.UpdateComparisons(); + Precision_t max_distance; + if (Loki::TypeTraits::isStdFloat==true) { + max_distance=range; + } if (!ptr->IsLeaf()){ computations_.UpdateComparisons(); pair child_pair = @@ -216,9 +221,13 @@ void TREE__::NearestNeighbor(NodePtr_t ptr, NearestNeighbor(child_pair.first, test_point, nearest_point, range, found); + if (Loki::TypeTraits::isStdFloat==false) { + max_distance=nearest_point->back().first; + } + if (child_pair.second->get_box().CrossesBoundaries(test_point, dimension_, - nearest_point->end()->first, + max_distance, computations_)) { NearestNeighbor(child_pair.second, test_point, @@ -229,8 +238,12 @@ void TREE__::NearestNeighbor(NodePtr_t ptr, if (found == true) { return; } else { + if (Loki::TypeTraits::isStdFloat==false) { + max_distance=nearest_point->back().first; + } found = ptr->get_box().IsWithin(test_point, - dimension_, nearest_point->end()->first, + dimension_, + max_distance, computations_)==0; if (found == true) { return; @@ -241,8 +254,11 @@ void TREE__::NearestNeighbor(NodePtr_t ptr, range, dimension_, discriminator_, computations_); + if (Loki::TypeTraits::isStdFloat==false) { + max_distance=nearest_point->back().first; + } found = ptr->get_box().IsWithin(test_point, dimension_, - nearest_point->end()->first, + max_distance, computations_); } } @@ -372,7 +388,7 @@ void TREE__::InitAllKNearestNeighborOutput(string file, fwrite(buffer, sizeof(typename Node_t::NNResult), (num_of_points_%kChunk)*knns, fp ); fclose(fp); - delete buffer; + delete buffer[]; int fd=open(file.c_str(), O_RDWR); typename Node_t::NNResult *ptr =(typename Node_t::NNResult *)mmap(NULL, sizeof(typename Node_t::NNResult)*knns*num_of_points_, diff --git a/fastlib/u/nvasil/tree/binary_tree_unit.cc b/fastlib/u/nvasil/tree/binary_tree_unit.cc index 487e8b0493..22dc6ed337 100644 --- a/fastlib/u/nvasil/tree/binary_tree_unit.cc +++ b/fastlib/u/nvasil/tree/binary_tree_unit.cc @@ -76,22 +76,24 @@ class BinaryTreeTest { void BuildDepthFirst(){ printf("Testing BuildDepthFirst...\n"); tree_.BuildDepthFirst(); - tree_.Print(); + //tree_.Print(); printf("%s\n", tree_.Statistics().c_str()); } void BuildBreadthFirst() { printf("Testing BuildBreadthFirst...\n"); tree_.BuildBreadthFirst(); - tree_.Print(); + // tree_.Print(); printf("%s\n", tree_.Statistics().c_str()); } void kNearestNeighbor() { printf("Testing kNearestNeighbor...\n"); tree_.BuildDepthFirst(); + tree_.Print(); vector > nearest_tree; pair nearest_naive[num_of_points_]; for(index_t i=0; i > nearest_tree; pair nearest_naive[num_of_points_]; for(index_t i=0; i::epsilon()); + nearest_tree[j].first, + numeric_limits::epsilon()); TEST_ASSERT(nearest_tree[j].second.get_id()== nearest_naive[j+1].second) ; } @@ -131,11 +136,11 @@ class BinaryTreeTest { tree_.AllNearestNeighbors(tree_.parent_, knns_); tree_.CloseAllKNearestNeighborOutput(knns_); struct stat info; - if (stat(data_file_.c_str(), &info)!=0) { + if (stat(result_file_.c_str(), &info)!=0) { FATAL("Error %s file %s\n", strerror(errno), data_file_.c_str()); } - uint64 map_size = info.st_size-sizeof(int32); + uint64 map_size = info.st_size; int fp=open(result_file_.c_str(), O_RDWR); typename Node_t::NNResult *res; @@ -232,18 +237,14 @@ class BinaryTreeTest { void Naive(index_t query, pair *result) { - for(index_t i=0; i > nearest_tree; pair nearest_naive[num_of_points_]; for(index_t i=0; i > nearest_tree; pair nearest_naive[num_of_points_]; for(index_t i=0; i::epsilon()); + nearest_tree[j].first, + numeric_limits::epsilon()); TEST_ASSERT(nearest_tree[j].second.get_id()== nearest_naive[j+1].second) ; } @@ -130,7 +136,7 @@ class BinaryTreeTest { tree_.AllNearestNeighbors(tree_.parent_, knns_); tree_.CloseAllKNearestNeighborOutput(knns_); struct stat info; - if (stat(data_file_.c_str(), &info)!=0) { + if (stat(result_file_.c_str(), &info)!=0) { FATAL("Error %s file %s\n", strerror(errno), data_file_.c_str()); } @@ -231,18 +237,14 @@ class BinaryTreeTest { void Naive(index_t query, pair *result) { - for(index_t i=0; i &a, + const pair &b) { + return a.first &a, - const pair &b) { - return a.first &a, + bool operator()(const pair &a, const pair &b) { return a.first::isStdFloat==false) { typename std::vector >::iterator it; - it=nearest.begin()+(index_t)range; - std::partial_sort(nearest.begin(), - it, - nearest.end(), - PairComparator()); - if (nearest.size()>(uint32)range) { + //it=nearest.begin()+(index_t)range; + index_t j=0; + for(it=nearest.begin(), j=0; j<(index_t)range; j++) { + it++; + } + std::sort(nearest.begin(), + nearest.end(), + PairComparator()); + if (likely(nearest.size()>(uint32)range)) { nearest.erase(it, nearest.end()); + } else { + pair dummy; + dummy.first=numeric_limits::max(); + index_t extra_size=(index_t)(range-nearest.size()); + for(index_t i=0; i::isStdFloat==false) { typename std::vector >::iterator it; - it=nearest.begin()+(index_t)range; - std::partial_sort(nearest.begin(), - it, - nearest.end(), - PairComparator()); - if (nearest.size()>(uint32)range) { + //it=nearest.begin()+(index_t)range; + index_t j=0; + for(it=nearest.begin(), j=0; j(uint32)range)) { nearest.erase(it, nearest.end()); + } else { + pair dummy; + dummy.first=numeric_limits::max(); + index_t extra_size=(index_t)(range-nearest.size()); + for(index_t i=0; i