core/vil/vil_block_cache.cxx
Go to the documentation of this file.
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   for(vcl_vector<bcell*>::iterator bit = blocks_.begin();
00012       bit != blocks_.end(); ++bit){
00013     delete *bit;
00014     *bit = 0;
00015   }
00016   blocks_.clear();//empty the index
00017 }
00018 
00019 //:add a block to the buffer.
00020 bool vil_block_cache::add_block(const unsigned& block_index_i,
00021                                 const unsigned& block_index_j,
00022                                 vil_image_view_base_sptr const& blk)
00023 {
00024   //create a cell
00025 
00026   bcell* cell = new bcell(block_index_i, block_index_j, blk);
00027   if (blocks_.size()>=nblocks_)
00028     if (!this->remove_block())
00029       return false;
00030   blocks_.push_back(cell);
00031   vcl_sort(blocks_.begin(), blocks_.end(), bcell_less());
00032   return true;
00033 }
00034 
00035 bool vil_block_cache::get_block(const unsigned& block_index_i,
00036                                 const unsigned& block_index_j,
00037                                 vil_image_view_base_sptr& blk) const
00038 {
00039   bool found = false;
00040   for (vcl_vector<bcell*>::const_iterator bit=blocks_.begin(); bit!= blocks_.end()&&!found; ++bit)
00041   {
00042     if ((*bit)->bindex_i_!=block_index_i||(*bit)->bindex_j_!=block_index_j)
00043       continue;
00044     else
00045     {
00046       found = true;
00047       blk = (*bit)->blk_;
00048       (*bit)->touch();//block is in demand so update the age to zero
00049     }
00050   }
00051   return found;
00052 }
00053 
00054 //:remove the oldest priority block
00055 bool vil_block_cache::remove_block()
00056 {
00057   if(!blocks_.size()){
00058     vcl_cerr << "warning: attempt to remove block from empty cache\n";
00059     return false;
00060   }
00061   // queue should already be sorted
00062   // remove oldest
00063   vcl_vector<bcell*>::iterator bit = blocks_.begin();
00064   blocks_.erase(bit);
00065   return true;
00066 }