My program hangs

#include <cstdio>
#include <kdtree++/kdtree.hpp>
#include <vector>
#include <map>
#include <set>
#include <iostream> // cout, endl
#include <fstream> // fstream
#include <vector>
#include <string>
#include <algorithm> // copy
#include <iterator> // ostream_operator
#include <boost/tokenizer.hpp>
#include <boost/array.hpp>
#include <boost/lexical_cast.hpp>
#include <time.h>
using namespace std;

const int dim=12;
typedef boost::array<double, dim> arraydim;

struct kdtreeNode
{
typedef double value_type;

size_t index;
arraydim vals;
kdtreeNode(arraydim data)
{
vals=data;
}
kdtreeNode(const kdtreeNode & data)
{
vals=data.vals;
}

value_type operator[](size_t n) const
{
return vals[n];
}
};
std::ostream& operator<<(std::ostream& out, kdtreeNode const& T)
{
std::string s;
for(int i=0;i<dim;i++)
{
s.append(boost::lexical_cast<string>(T.vals[i]));
s.append(",");
}
return out << '(' << s << ')';
}

inline bool operator==(kdtreeNode const& A, kdtreeNode const& B) {
arraydim::const_iterator it1 = A.vals.begin();
arraydim::const_iterator it2 = B.vals.begin();
//if(A.vals[0]==B.vals[0]) return true;

while(true)
{
for(int i=0;i<dim;i++)
{
if(A.vals[i]==B.vals[i]) continue;
else return false;;
}
}
return true;
}

int main(int argc,char *argv[])
{

using namespace std;
using namespace boost;
time_t start,end;
string data("./data/star2000_small.csv");
cout << "loading the file";
ifstream in(data.c_str());
if (!in.is_open()) return 1;
typedef tokenizer< escaped_list_separator<char> > Tokenizer;
vector< string > vec;
string line;
vector<kdtreeNode> pts;
typedef KDTree::KDTree<dim,kdtreeNode> treeType;
treeType tree;
while (getline(in,line))
{
boost::array<double, dim> x;
Tokenizer tok(line);
vec.assign(tok.begin(),tok.end());
Tokenizer::iterator it(tok.begin()),end1(tok.end());
for (int i=0; it != end1; ++it,i++)
{
x[i]=atof((*it).c_str());
}
kdtreeNode node(x);
// tree.insert( node);
pts.push_back( node);
//delete x;

}
time(&start);
tree.efficient_replace_and_optimise(pts);
time(&end);
printf("time taken to build the tree =%f",difftime (end,start));
//tree.insert(pts);
time(&start);
tree.optimise();
time(&end);
printf("\n time taken to optimize %f seconds ",difftime (end,start));
vector<kdtreeNode> vectors;
arraydim insertnode={{266, 909, 159625, 159627, 2635, 20000827.0117590018, 1239029, 1341, -26.399744, 1228, 0.56000537, 49}};
kdtreeNode sv(insertnode);
time(&start);
tree.insert(sv);
time(&end);
printf("Test insert took %f seconds",difftime (end,start));
time(&start);
tree.find_within_range(sv, 100.0f, std::back_inserter(vectors) );
time(&end);
std::cout << std::endl << "Test find_with_range found " << vectors.size() << " candidates." << std::endl;
printf("\n time take for within range %f seconds ", difftime (end,start));
tree.optimize();
time(&start);
tree.erase(sv);
time(&end);
printf("\n time take for erase %f seconds\n ", difftime (end,start));
time(&start);
vectors.clear();
insertnode={{266}};
tree.insert(sv);
time(&end);
printf(" inserted node in %f seconds\n ",difftime (end,start));
time(&start);
kdtreeNode sv1(insertnode);
tree.insert(sv1);
time(&end);
printf(" inserted node in %f seconds \n",difftime (end,start));

time(&start);
tree.optimize();
time(&end);
printf("optimized again in %f seconds \n",difftime (end,start));
tree.find_within_range(sv1, 5555500.0f, std::back_inserter(vectors) );
time(&end);
std::cout << std::endl << "Test find_with_range found " << vectors.size() << " candidates.\n" << std::endl;
time(&start);
std::vector<kdtreeNode> v;
unsigned int RANGE = 3;

size_t count = tree.count_within_range(sv, RANGE);
time(&end);
printf("search done in %f seconds\n",difftime (end,start));
std::cout << "counted " << count
<< " nodes within range " << RANGE << ".\n";
tree.find_within_range(sv, RANGE, std::back_inserter(v));

std::vector<kdtreeNode>::const_iterator ci = v.begin();
for (; ci != v.end(); ++ci)
std::cout << *ci << " ";
std::cout << "\n" << std::endl;

// std::cout << std::endl << tree << std::endl;


RANGE = 10;
v.clear();
time(&start);
count = tree.count_within_range(sv, RANGE);
time(&end);
printf("search done in %f seconds\n",difftime (end,start));

std::cout << "counted " << count
<< " nodes within range " << RANGE << ".\n";
printf("finding the nodes in this search\n");
time(&start);
tree.find_within_range(sv, RANGE, std::back_inserter(v));
time(&end);
printf("search done in %f seconds\n",difftime (end,start));
printf("2");
ci = v.begin();
for (; ci != v.end(); ++ci)
{
std::cout << *ci << " ";
}
cout<<"3";
std::cout<<"testing find exact";
treeType::iterator cj= tree.find_exact(sv);
std::cout << *cj << " ";
std::cout << "\n I am done" << std::endl;
v.clear();
return 0;
}


It prints till the line printf("search done in %f seconds\n",difftime (end,start));

and then just hangs. It does not print "2". I need help.

This is the output I get

loading the filetime taken to build the tree =0.000000
time taken to optimize 0.000000 seconds Test insert took 0.000000 seconds
Test find_with_range found 1 candidates.

time take for within range 0.000000 seconds
time take for erase 0.000000 seconds
inserted node in 0.000000 seconds
inserted node in 0.000000 seconds
optimized again in 0.000000 seconds

Test find_with_range found 1 candidates.

search done in 0.000000 seconds
counted 1 nodes within range 3.
(266,909,159625,159627,2635,20000827.011759002,1239029,1341,-26.399743999999998,1228,0.56000536999999995,49,)

search done in 0.000000 seconds
counted 1 nodes within range 10.
finding the nodes in this search
search done in 0.000000 seconds
^C
and thats it . I have to kill it all the time.
Last edited on
_ [code]"Please use code tags"[/code]

_ Learn to debug.
If you are going to put allusive messages everywhere, then use an unbuffered output (stderr) or flush it.
Or better, execute a step by step run through a debugger.

_ Minimize your example.
Remove the code that is not related to the issue
By instance, you ought to remove all that profile code (you should let that to a profiler, by the way)

_ Put some comments.


Also, ┬┐where could I obtain the kdtree++/kdtree.hpp file?
Topic archived. No new replies allowed.