1. promote empty tree nodes

This commit is contained in:
Artem Pavlenko 2005-02-22 19:13:54 +00:00
parent fb1084c69d
commit 1735454283
2 changed files with 236 additions and 203 deletions

View file

@ -34,30 +34,51 @@ struct quadtree_node
Envelope<double> ext_;
quadtree_node<T>* children_[4];
quadtree_node(const Envelope<double>& ext)
:ext_(ext)
: ext_(ext),data_()
{
memset(children_,0,sizeof(quadtree_node<T>*)*4);
}
~quadtree_node()
{
for (int i=0;i<4;++i)
{
if (children_[i])
{
delete children_[i],children_[i]=0;
}
}
}
int num_subnodes() const
{
int count=0;
for (int i=0;i<4;++i)
{
if (children_[i])
{
++count;
}
}
return count;
}
};
template <typename T>
class quadtree
{
private:
private:
quadtree_node<T>* root_;
const int maxdepth_;
const double ratio_;
public:
public:
quadtree(const Envelope<double>& extent,int maxdepth,double ratio)
: root_(new quadtree_node<T>(extent)),
maxdepth_(maxdepth),
ratio_(ratio){}
~quadtree()
{
destroy_node(root_);
if (root_) delete root_;
}
void insert(const T& data,const Envelope<double>& item_ext)
{
@ -67,11 +88,20 @@ class quadtree
{
return count_nodes(root_);
}
int count_items() const
{
int count=0;
count_items(root_,count);
return count;
}
void print() const
{
print(root_);
}
void trim()
{
trim_tree(root_);
}
void write(std::ostream& out)
{
@ -84,55 +114,32 @@ class quadtree
header[5]='i';
header[6]='k';
out.write(header,16);
//trim_tree(root_);//TODO trim empty nodes
write_node(out,root_);
}
private:
private:
void destroy_node(quadtree_node<T>* node)
void trim_tree(quadtree_node<T>*& node)
{
if (node)
{
for (int i=0;i<4;++i)
{
destroy_node(node->children_[i]);
}
delete node,node=0;
}
trim_tree(node->children_[i]);
}
bool trim_tree(quadtree_node<T>* node)
if (node->num_subnodes()==1 && node->data_.size()==0)
{
for (int i=0;i<4;++i)
{
if (node->children_[i] && trim_tree(node->children_[i]))
{
std::cout << "destroy"<<std::endl;
destroy_node(node->children_[i]);
}
}
int num_shapes=node->data_.size();
int num_subnodes=0;
if (num_shapes==0)
{
quadtree_node<T>* subnode=0;
for (int i=0;i<4;++i)
{
if (node->children_[i])
{
subnode=node->children_[i];
++num_subnodes;
node=node->children_[i];
break;
}
}
if (num_subnodes==1)
{
node=subnode; // memory leak!
}
}
return (num_shapes == 0 && num_subnodes == 0);
}
int count_nodes(const quadtree_node<T>* node) const
@ -152,6 +159,18 @@ class quadtree
}
}
void count_items(const quadtree_node<T>* node,int& count) const
{
if (node)
{
count += node->data_.size();
for (int i=0;i<4;++i)
{
count_items(node->children_[i],count);
}
}
}
int subnode_offset(const quadtree_node<T>* node) const
{
int offset=0;
@ -186,8 +205,10 @@ class quadtree
for (int i=0;i<4;++i)
{
if (node->children_[i])
{
++num_subnodes;
}
}
memcpy(node_record + 40 + shape_count * sizeof(T),&num_subnodes,4);
out.write(node_record,recsize);
delete [] node_record;
@ -199,12 +220,18 @@ class quadtree
}
}
void print(const quadtree_node<T>* node) const
void print(const quadtree_node<T>* node,int level=0) const
{
if (node)
{
typename std::vector<T>::const_iterator itr=node->data_.begin();
std::cout<<"node extent:"<<node->ext_<<std::endl;
std::string pad;
for (int i=0;i<level;++i)
{
pad+=" ";
}
std::cout<<pad<<"node "<<node<<" extent:"<<node->ext_<<std::endl;
std::cout<<pad;
while(itr!=node->data_.end())
{
std::cout<<*itr<<" ";
@ -213,7 +240,7 @@ class quadtree
std::cout<<std::endl;
for (int i=0;i<4;++i)
{
print(node->children_[i]);
print(node->children_[i],level+4);
}
}
}

View file

@ -109,7 +109,8 @@ int main (int argc,char** argv) {
arguments.ratio = DEFAULT_RATIO;
argp_parse(&argp, argc, argv, 0, 0, &arguments);
std::cout<<"start processing "<<arguments.args[0]<<std::endl;
std::cout<<"processing "<<arguments.args[0]<<std::endl;
std::cout<<"max tree depth:"<<arguments.depth<<std::endl;
std::cout<<"split ratio:"<<arguments.ratio<<std::endl;
@ -131,8 +132,8 @@ int main (int argc,char** argv) {
shp.read_envelope(extent);
if (!arguments.silent) {
std::cout<<"length="<<file_length<<std::endl;
std::cout<<"version="<<version<<std::endl;
//std::cout<<"length="<<file_length<<std::endl;
//std::cout<<"version="<<version<<std::endl;
std::cout<<"type="<<shape_type<<std::endl;
std::cout<<"extent:"<<extent<<std::endl;
}
@ -176,8 +177,8 @@ int main (int argc,char** argv) {
}
}
shp.close();
std::cout<<" number shapes "<<count<<std::endl;
std::cout<<" number nodes "<<tree.count()<<std::endl;
std::cout<<" number shapes="<<count<<std::endl;
std::fstream file((shapename+".index").c_str(),
std::ios::in | std::ios::out | std::ios::trunc | std::ios::binary);
@ -185,6 +186,11 @@ int main (int argc,char** argv) {
std::cerr << "cannot open index file for writing file \""
<<(shapename+".index")<<"\""<<std::endl;
} else {
tree.trim();
std::cout<<" number nodes="<<tree.count()<<std::endl;
tree.write(file);
file.close();
}