📄 cache.cpp
字号:
#include "Cache.h"
extern int qtree_level;
extern Cache cache_memory;
extern filename[];
extern BlockFile myblockfile;
CacheBlock::CacheBlock(){
num_pts = 0;
block_id = "";
cache = new char[BLOCKLENGTH];
write_ptr = cache + sizeof(int); //write pointer always points to data, not to header
}
CacheBlock::~CacheBlock(){
delete[] cache;
}
bool CacheBlock::write_one_data (Entry* data){
int i= DIMENSION * sizeof(float);
if ((write_ptr + i) > (cache + BLOCKLENGTH)) return false; //cacheblock full
// cacheblock not full
num_pts++;
memcpy(cache, &num_pts, sizeof(int)); //update number of points
for (int j=0; j<DIMENSION; j++)
{
memcpy(write_ptr, &(data->data[j]), sizeof(float)); // write one data in cache
write_ptr += sizeof(float); //move write pointer
}
return true;
}
bool CacheBlock::read_whole_block (Entry** dataArray){
int num=0;
memcpy(&num, cache, sizeof(int));
char* temp_ptr=cache + sizeof(int); // pointer for reading
for (int i=0;i<num ;i++)
{
for (int j=0; j<DIMENSION; j++)
{
memcpy(&(dataArray[i]->data[j]) ,temp_ptr, sizeof(float));
temp_ptr += sizeof(float);
}
}
return true;
}
//------------------------------------------------------------------------------------
Cache::Cache() {
int i;
cache_cont = new string[CACHESIZE];
for (i = 0; i < CACHESIZE; i++) {
cache_cont[i] = "";
}
cacheblock = new CacheBlock[CACHESIZE];
// for (i=0; i<cachesize;i++)
// cacheblock[i](blocklength);
}
Cache::~Cache() {
delete[] cache_cont;
delete[] cacheblock;
}
/*
bool Cache::write_block(Block block, string block_id) {
int c_ind;
c_ind = in_cache(block_id);
if(c_ind >= 0) // in cache, overwrite it
memcpy(cache[c_ind], block, blocklength);
else // not in cache, so it's a new block , allocate new space for it in the cache
{ c_ind=find_available_space();
if (c_ind<0) {cout<<"Cache full!"<<endl; //need to write some block to file, to be improved
return false;}
else
{cache_cont[c_ind]= block_id;
memcpy(cache[c_ind], block, blocklength);
}
}
return true;
}
*/
int Cache::in_cache(string b_id) {
int ret_val = -1;
for (int i = 0; i < CACHESIZE; i++){
if (cache_cont[i] == b_id) {
{ret_val = i; break;}
}
// cout<<cache_cont[i]<<"!="<<b_id<<"!"<<endl;
}
return ret_val;
}
int Cache::find_available_space(){
int ret_val = -1;
for (int i = 0; i < CACHESIZE; i++)
if (cache_cont[i]=="")
{ret_val = i; break;}
return ret_val;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -