node_test passed

This commit is contained in:
vasiloglou
2007-05-02 15:18:24 +00:00
parent 3c57098c19
commit 26bc4b77ba
6 changed files with 71 additions and 48 deletions
+15 -6
View File
@@ -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;
}
@@ -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__
+18 -16
View File
@@ -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<pair<Precision_t, Point_t> >::iterator it;
it=nearest.begin()+range;
//for(index_t k=0; k<range; k++) {
// it++;
//}
if (Loki::TypeTraits<NEIGHBORTYPE>::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<pair<Precision_t, Point_t> >::iterator it;
it=nearest.begin()+range;
if (Loki::TypeTraits<NEIGHBORTYPE>::isStdFloat==false) {
std::partial_sort(nearest.begin(),
it,
nearest.end(),
PairComparator());
if (nearest.size()>(uint32)range) {
nearest.erase(it, nearest.end());
}
}
}
+8 -2
View File
@@ -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<pair<Precision_t, Point_t> >::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());
}
+15 -11
View File
@@ -60,14 +60,15 @@ class NodeTest {
data_file_="data";
dataset_.Init(data_file_, num_of_points_, dimension_);
for(index_t i=0; i<num_of_points_; i++) {
dataset_.At(i)[0]=rand()/14.333;
dataset_.At(i)[1]=rand()/1.6778;
dataset_.At(i)[0]=Precision_t(rand())/RAND_MAX;
dataset_.At(i)[1]=Precision_t(-rand())/RAND_MAX;
dataset_.set_id(i,i);
}
hyper_rectangle_->Init(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<pair<Precision_t, Point<Precision_t, Allocator_t> > > nearest;
ComputationsCounter<diagnostic> comp;
for(index_t i=0; i<num_of_points_; i++) {
Point_t query_point;
query_point.Alias(dataset_.At(i), dataset_.get_id(i));
nearest.clear();
node_->FindNearest(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<Precision_t>::max();
SimpleDiscriminator discriminator;
ComputationsCounter<diagnostic> comp;
@@ -161,8 +165,8 @@ class NodeTest {
private:
typename Allocator_t:: template Ptr<Node_t> node_;
string data_file_;
typename Allocator_t:: template Ptr<HyperRectangle_t> hyper_rectangle_;
string data_file_;
HyperRectangle_t hyper_rectangle_;
BinaryDataset<Precision_t> dataset_;
index_t num_of_points_;
int32 dimension_;
+14 -11
View File
@@ -60,14 +60,15 @@ class NodeTest {
data_file_="data";
dataset_.Init(data_file_, num_of_points_, dimension_);
for(index_t i=0; i<num_of_points_; i++) {
dataset_.At(i)[0]=rand()/14.333;
dataset_.At(i)[1]=rand()/1.6778;
dataset_.At(i)[0]=Precision_t(rand())/RAND_MAX;
dataset_.At(i)[1]=Precision_t(-rand())/RAND_MAX;
dataset_.set_id(i,i);
}
hyper_rectangle_->Init(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<pair<Precision_t, Point<Precision_t, Allocator_t> > > nearest;
ComputationsCounter<diagnostic> comp;
for(index_t i=0; i<num_of_points_; i++) {
Point_t query_point;
query_point.Alias(dataset_.At(i), dataset_.get_id(i));
nearest.clear();
node_->FindNearest(query_point,
nearest,
1,
@@ -97,7 +100,7 @@ class NodeTest {
discriminator,
comp);
Precision_t min_dist=numeric_limits<Precision_t>::max();
index_t min_id;
index_t min_id=0;
for(index_t j=0; j<num_of_points_; j++) {
if (unlikely(dataset_.get_id(j)==dataset_.get_id(i))) {
continue;
@@ -118,7 +121,7 @@ class NodeTest {
}
void FindAllNearest() {
typename Node_t::NNResult result[num_of_points_];
node_->set_kneighbors(result, num_of_points_);
node_->set_kneighbors(result, 1);
Precision_t max_neighbor_distance=numeric_limits<Precision_t>::max();
SimpleDiscriminator discriminator;
ComputationsCounter<diagnostic> comp;
@@ -161,8 +164,8 @@ class NodeTest {
private:
typename Allocator_t:: template Ptr<Node_t> node_;
string data_file_;
typename Allocator_t:: template Ptr<HyperRectangle_t> hyper_rectangle_;
string data_file_;
HyperRectangle_t hyper_rectangle_;
BinaryDataset<Precision_t> dataset_;
index_t num_of_points_;
int32 dimension_;