⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 quadtree4.cpp

📁 在n维空间(每维范围为0-1)内对插入的数值根据坐标进行分区。从一个没有分区的空间开始插入
💻 CPP
字号:
#include <string>
#include <iostream>
#include <cmath>

#include "qtree.h"
#include "BFile.h"
#include "Cache.h"
#include "generaldef.h"
#include "utility.h"

//using namespace std;



//---------------- global variables-----------------

int qtree_level=0; // total level of qtree
Cache cache_memory;

char filename[12]={'d','t','f','i','l','e','.','t','x','t'}; 
BlockFile myblockfile(filename,F_BLOCKLENGTH);


//--------------------------------------






int main(){
//initialize root of qtree
qtree_node root(0);

          for (int i=0; i<DIMENSION; i++)
             {
              root.coor_lower[i]=0;
              root.coor_upper[i]=1;
             }
          root.level=0;    
          root.is_leaf=true;                   
          root.num_pts=0;
          root.node_id="0";
          root.ptr_to_cache=&cache_memory.cacheblock[0];

cache_memory.cache_cont[0]="0";

// data to be inserted   



Entry entry[TOTALNUMBER];

for(int m=0; m<TOTALNUMBER; m++){
     for(int j=0; j<DIMENSION; j++)   
       { entry[m].data[j]=rand()%10000/10000.0; cout<<entry[m].data[j]<<"   "; }
     cout<<m<<endl;

     root.insert_one_data(&entry[m]);
}
getchar();

//define & initialize entries for output
int t, file_index,k;
int counter=0,counter2=0;
for (k=0;k<CACHESIZE;k++){
    cout<<"-------------cacheblock "<<k;
    Entry** dataArray;      
    t=cache_memory.cacheblock[k].num_pts;
    cout<<"  with num "<<t<<"  id="<<cache_memory.cacheblock[k].block_id<<endl;
    if(t==0) continue;
    
    dataArray= new Entry*[t+1];
    for (int n=0; n<t; n++)
        dataArray[n]= new Entry;

// read from cache, then output (for validation)
   cache_memory.cacheblock[k].read_whole_block(dataArray);   

   for(int i=0; i<t; i++){
        cout<<++counter<<"  ";
        for (int j=0; j< DIMENSION; j++)
            cout<<dataArray[i]->data[j]<<"  ";
        cout<<endl;        
        }

   delete[] dataArray;
 
   file_index=myblockfile.retrieve_index(cache_memory.cacheblock[k].block_id);	   //search in the file, see whether the current
   Entry** dataArray2;																   //block has data in file
   if(file_index>=0){  //it has data in file

		t=myblockfile.file_cont_no[file_index];
		dataArray2= new Entry*[t+1];
		for (int n=0; n<t; n++)
			dataArray2[n]= new Entry;

		myblockfile.fread_block(dataArray2, file_index);

		for(int i=0;i<t;i++){
			counter2++;
			cout<<++counter<<"  ";
			for (int j=0; j< DIMENSION; j++)
				cout<<dataArray2[i]->data[j]<<"  ";
			cout<<"  (in file)"<<endl;		
		}								//read and output these data
		
	//	delete[] dataArray2;
   
   }
}
cout<<"No. of data stored in file which belong to the blocks in cache: ";
cout<<counter2<<endl;
cout<<"Following are the data blocks completely in file: "<<endl;
getchar();

for (int k2=0;k2<FBLOCKNO;k2++){	 // output those blocks which are only in file
	if(myblockfile.file_cont[k2]!=""){
		if(cache_memory.in_cache(myblockfile.file_cont[k2])<0) {
		    cout<<"-------------fileblock "<<k;
			k++;
		    cout<<"  with num "<<myblockfile.file_cont_no[k2]<<"  id="<<myblockfile.file_cont[k2]<<endl;

			Entry** dataArray3;
			t=myblockfile.file_cont_no[k2];
			dataArray3= new Entry*[t+1];
			for (int n=0; n<t; n++)
				dataArray3[n]= new Entry;

			myblockfile.fread_block(dataArray3, k2);

			for(int i=0;i<t;i++){
				cout<<++counter<<"  ";
				for (int j=0; j< DIMENSION; j++)
					cout<<dataArray3[i]->data[j]<<"  ";
				cout<<endl; 		
			}							
		//	delete[] dataArray3;

		
		}

	}

}


cout<<"Press Enter to delete the file and exit"<<endl;
getchar();


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -