00001 #include "vil_block_cache.h" 00002 //: 00003 // \file 00004 #include <vcl_algorithm.h> 00005 #include <vcl_cassert.h> 00006 00007 unsigned long bcell::time_ = 0; 00008 00009 vil_block_cache::~vil_block_cache() 00010 { 00011 assert(queue_.size()==blocks_.size()); 00012 blocks_.clear();//empty the index 00013 //empty the queue 00014 unsigned nq = queue_.size(); 00015 for (unsigned i = 0; i<nq; ++i) 00016 { 00017 bcell* fb = queue_.top(); 00018 queue_.pop(); 00019 delete fb; 00020 } 00021 } 00022 00023 //:add a block to the buffer. 00024 bool vil_block_cache::add_block(const unsigned& block_index_i, 00025 const unsigned& block_index_j, 00026 vil_image_view_base_sptr const& blk) 00027 { 00028 //create a cell 00029 00030 bcell* cell = new bcell(block_index_i, block_index_j, blk); 00031 if (queue_.size()>=nblocks_) 00032 if (!this->remove_block()) 00033 return false; 00034 queue_.push(cell); 00035 blocks_.push_back(cell); 00036 return true; 00037 } 00038 00039 bool vil_block_cache::get_block(const unsigned& block_index_i, 00040 const unsigned& block_index_j, 00041 vil_image_view_base_sptr& blk) const 00042 { 00043 bool found = false; 00044 for (vcl_vector<bcell*>::const_iterator bit=blocks_.begin(); bit!= blocks_.end()&&!found; ++bit) 00045 { 00046 if ((*bit)->bindex_i_!=block_index_i||(*bit)->bindex_j_!=block_index_j) 00047 continue; 00048 else 00049 { 00050 found = true; 00051 blk = (*bit)->blk_; 00052 (*bit)->touch();//block is in demand so update the age to zero 00053 } 00054 } 00055 return found; 00056 } 00057 00058 //:remove the lowest priority block 00059 bool vil_block_cache::remove_block() 00060 { 00061 if (queue_.size()==0) 00062 return false; 00063 00064 //force the queue to reorder 00065 bcell* temp = queue_.top(); 00066 queue_.pop(); 00067 // 00068 queue_.push(temp); 00069 bcell* top_cell = queue_.top(); 00070 vcl_vector<bcell*>::iterator bit; 00071 bit = vcl_find(blocks_.begin(), blocks_.end(), top_cell); 00072 if (bit == blocks_.end()) 00073 return false; 00074 blocks_.erase(bit); 00075 queue_.pop();//finally remove the top cell from the queue 00076 delete top_cell; 00077 return true; 00078 }
1.5.1