diff --git a/fastlib/u/nvasil/dataset/binary_dataset.h b/fastlib/u/nvasil/dataset/binary_dataset.h index 4f23dd7b4f..b78edd1e4c 100644 --- a/fastlib/u/nvasil/dataset/binary_dataset.h +++ b/fastlib/u/nvasil/dataset/binary_dataset.h @@ -61,7 +61,6 @@ class BinaryDataset { } ~BinaryDataset() { - Destruct(); } // Initializes a the BinaryDataset from two existent files // one for data and one for the index @@ -76,7 +75,7 @@ class BinaryDataset { // If the index file is not given it assumes it has the same name as // the data file appended with ind success_t Init(string data_file) { - string temp=data_file.append("ind"); + string temp=data_file.append(".ind"); Init(data_file, temp); return SUCCESS_PASS; } @@ -87,7 +86,8 @@ class BinaryDataset { int32 dimension) { data_file_=data_file; index_file_=data_file; - data_file_.append("ind"); + index_file_.append(".ind"); + num_of_points_=num_of_points; Init(data_file_, index_file_, num_of_points_, dimension); return SUCCESS_PASS; } @@ -100,6 +100,8 @@ class BinaryDataset { dimension_=dimension; CreateDataFile(data_file, dimension, num_of_points); CreateIndexFile(index_file, num_of_points); + data_=(Precision_t*)MemoryMap(data_file_, sizeof(int32)); + index_=(uint64*)MemoryMap(index_file_,0); return SUCCESS_PASS; } // Use this to swap the points of a dataset @@ -217,8 +219,15 @@ class BinaryDataset { } uint64 map_size = info.st_size-sizeof(int32); int fp=open(file_name.c_str(), O_RDWR); - void *ptr=mmap(NULL, map_size, PROT_READ | PROT_WRITE, MAP_SHARED, fp, - offset); + if (fp<0) { + FATAL("Cannot open %s, error %s\n", file_name.c_str(), strerror(errno)); + } + lseek(fp, offset, SEEK_SET); + void *ptr=mmap(0, + map_size, + PROT_READ | PROT_WRITE, MAP_SHARED, + fp, + 0); if (unlikely(ptr==MAP_FAILED)) { FATAL("Error %s while mapping %s\n", strerror(errno), file_name.c_str()); @@ -236,7 +245,7 @@ class BinaryDataset { return SUCCESS_FAIL; uint64 map_size = info.st_size-offset; if(munmap(ptr , map_size)<0) { - NONFATAL("Error %s while mapping %s\n", + NONFATAL("Error %s while unmapping %s\n", strerror(errno), file_name.c_str()); return SUCCESS_FAIL; } diff --git a/fastlib/u/nvasil/tree/hyper_rectangle_impl.h~ b/fastlib/u/nvasil/tree/hyper_rectangle_impl.h~ index 94df57164f..412e4fc761 100644 --- a/fastlib/u/nvasil/tree/hyper_rectangle_impl.h~ +++ b/fastlib/u/nvasil/tree/hyper_rectangle_impl.h~ @@ -20,7 +20,7 @@ void HYPERRECTANGLE__::Init(int32 dimension) { TEMPLATE__ void HYPERRECTANGLE__::Init(Array_t min, Array_t max, int32 pivot_dimension, Precision_t pivot_value) { - min_ = min; + min_.Reset(min.get()); max_ = max; pivot_dimension_ = pivot_dimension; pivot_value_= pivot_value; @@ -194,7 +194,6 @@ inline typename HYPERRECTANGLE__::Precision_t HYPERRECTANGLE__::Distance( } } return dist; - } TEMPLATE__ diff --git a/fastlib/u/nvasil/tree/node_impl.h b/fastlib/u/nvasil/tree/node_impl.h index 1aa7c1217c..aae11d5868 100644 --- a/fastlib/u/nvasil/tree/node_impl.h +++ b/fastlib/u/nvasil/tree/node_impl.h @@ -57,7 +57,7 @@ NODE__::~Node() { TEMPLATE__ void *NODE__::operator new(size_t size) { - return Allocator_t::allocator->AllignedAlloc(size); + return Allocator_t::allocator_->AllignedAlloc(size); } TEMPLATE__ @@ -129,22 +129,24 @@ inline void NODE__::FindNearest(POINTTYPE query_point, point.Alias(points_.get()+i*dimension, index_[i]); nearest.push_back(make_pair(dist, point)); } + } else { + // for k nearest neighbors + Point_t point; + point.Alias(points_.get()+i*dimension, index_[i]); + nearest.push_back(make_pair(dist, point)); } - // for k-nearest neighbors - typename std::vector >::iterator it; - it=nearest.begin()+range; - //for(index_t k=0; k::isStdFloat==false) { - std::partial_sort(nearest.begin(), - it, - nearest.end(), - PairComparator()); - if (nearest.size()>(uint32)range) { - nearest.erase(it, nearest.end()); - } - } + } + // for k-nearest neighbors + typename std::vector >::iterator it; + it=nearest.begin()+range; + if (Loki::TypeTraits::isStdFloat==false) { + std::partial_sort(nearest.begin(), + it, + nearest.end(), + PairComparator()); + if (nearest.size()>(uint32)range) { + nearest.erase(it, nearest.end()); + } } } diff --git a/fastlib/u/nvasil/tree/node_impl.h~ b/fastlib/u/nvasil/tree/node_impl.h~ index 280d77f2a0..d403bdff79 100644 --- a/fastlib/u/nvasil/tree/node_impl.h~ +++ b/fastlib/u/nvasil/tree/node_impl.h~ @@ -57,7 +57,7 @@ NODE__::~Node() { TEMPLATE__ void *NODE__::operator new(size_t size) { - return Allocator_t::allocator->AllignedAlloc(size); + return Allocator_t::allocator_->AllignedAlloc(size); } TEMPLATE__ @@ -129,7 +129,13 @@ inline void NODE__::FindNearest(POINTTYPE query_point, point.Alias(points_.get()+i*dimension, index_[i]); nearest.push_back(make_pair(dist, point)); } + } else { + // for k nearest neighbors + Point_t point; + point.Alias(points_.get()+i*dimension, index_[i]); + nearest.push_back(make_pair(dist, point)); } + // for k-nearest neighbors typename std::vector >::iterator it; it=nearest.begin()+range; @@ -140,7 +146,7 @@ inline void NODE__::FindNearest(POINTTYPE query_point, std::partial_sort(nearest.begin(), it, nearest.end(), - PairComparator); + PairComparator()); if (nearest.size()>(uint32)range) { nearest.erase(it, nearest.end()); } diff --git a/fastlib/u/nvasil/tree/node_unit.cc b/fastlib/u/nvasil/tree/node_unit.cc index d95dc61b72..f5f6dbfb76 100644 --- a/fastlib/u/nvasil/tree/node_unit.cc +++ b/fastlib/u/nvasil/tree/node_unit.cc @@ -60,14 +60,15 @@ class NodeTest { data_file_="data"; dataset_.Init(data_file_, num_of_points_, dimension_); for(index_t i=0; iInit(min, max, 0, 0); + } + hyper_rectangle_.Init(min, max, 0, 0); NullStatistics statistics; // typename Node_t::NodeCachedStatistics_t statistics; - node_->Init(*hyper_rectangle_, + node_.Reset(new Node_t); + node_->Init(hyper_rectangle_, statistics, 0, 0, @@ -76,7 +77,7 @@ class NodeTest { &dataset_); } void Destruct() { - hyper_rectangle_->Destruct(); + hyper_rectangle_.Destruct(); delete Allocator_t::allocator_; dataset_.Destruct(); unlink(data_file_.c_str()); @@ -84,12 +85,14 @@ class NodeTest { } void FindNearest() { - SimpleDiscriminator discriminator; + printf("Testing find nearest\n"); + SimpleDiscriminator discriminator; vector > > nearest; ComputationsCounter comp; for(index_t i=0; iFindNearest(query_point, nearest, 1, @@ -117,8 +120,9 @@ class NodeTest { } } void FindAllNearest() { - typename Node_t::NNResult result[num_of_points_]; - node_->set_kneighbors(result, num_of_points_); + printf("Testing find all nearest\n"); + typename Node_t::NNResult result[num_of_points_]; + node_->set_kneighbors(result, 1); Precision_t max_neighbor_distance=numeric_limits::max(); SimpleDiscriminator discriminator; ComputationsCounter comp; @@ -161,8 +165,8 @@ class NodeTest { private: typename Allocator_t:: template Ptr node_; - string data_file_; - typename Allocator_t:: template Ptr hyper_rectangle_; + string data_file_; + HyperRectangle_t hyper_rectangle_; BinaryDataset dataset_; index_t num_of_points_; int32 dimension_; diff --git a/fastlib/u/nvasil/tree/node_unit.cc~ b/fastlib/u/nvasil/tree/node_unit.cc~ index 29ee809015..63142b5686 100644 --- a/fastlib/u/nvasil/tree/node_unit.cc~ +++ b/fastlib/u/nvasil/tree/node_unit.cc~ @@ -60,14 +60,15 @@ class NodeTest { data_file_="data"; dataset_.Init(data_file_, num_of_points_, dimension_); for(index_t i=0; iInit(min, max, 0, 0); + } + hyper_rectangle_.Init(min, max, 0, 0); NullStatistics statistics; // typename Node_t::NodeCachedStatistics_t statistics; - node_->Init(*hyper_rectangle_, + node_.Reset(new Node_t); + node_->Init(hyper_rectangle_, statistics, 0, 0, @@ -76,7 +77,7 @@ class NodeTest { &dataset_); } void Destruct() { - hyper_rectangle_->Destruct(); + hyper_rectangle_.Destruct(); delete Allocator_t::allocator_; dataset_.Destruct(); unlink(data_file_.c_str()); @@ -84,12 +85,14 @@ class NodeTest { } void FindNearest() { - SimpleDiscriminator discriminator; + printf("Testing find nearest\n"); + SimpleDiscriminator discriminator; vector > > nearest; ComputationsCounter comp; for(index_t i=0; iFindNearest(query_point, nearest, 1, @@ -97,7 +100,7 @@ class NodeTest { discriminator, comp); Precision_t min_dist=numeric_limits::max(); - index_t min_id; + index_t min_id=0; for(index_t j=0; jset_kneighbors(result, num_of_points_); + node_->set_kneighbors(result, 1); Precision_t max_neighbor_distance=numeric_limits::max(); SimpleDiscriminator discriminator; ComputationsCounter comp; @@ -161,8 +164,8 @@ class NodeTest { private: typename Allocator_t:: template Ptr node_; - string data_file_; - typename Allocator_t:: template Ptr hyper_rectangle_; + string data_file_; + HyperRectangle_t hyper_rectangle_; BinaryDataset dataset_; index_t num_of_points_; int32 dimension_;