vil_block_cache.cxx
Go to the documentation of this file.
1 #include <algorithm>
2 #include "vil_block_cache.h"
3 //:
4 // \file
5 #ifdef _MSC_VER
6 # include <vcl_msvc_warnings.h>
7 #endif
8 #include <cassert>
9 
10 unsigned long bcell::time_ = 0;
11 
13 {
14  for(auto & block : blocks_){
15  delete block;
16  block = nullptr;
17  }
18  blocks_.clear();//empty the index
19 }
20 
21 //:add a block to the buffer.
22 bool vil_block_cache::add_block(const unsigned& block_index_i,
23  const unsigned& block_index_j,
24  vil_image_view_base_sptr const& blk)
25 {
26  //create a cell
27 
28  auto* cell = new bcell(block_index_i, block_index_j, blk);
29  if (blocks_.size()>=nblocks_)
30  if (!this->remove_block())
31  return false;
32  blocks_.push_back(cell);
33  std::sort(blocks_.begin(), blocks_.end(), bcell_less());
34  return true;
35 }
36 
37 bool vil_block_cache::get_block(const unsigned& block_index_i,
38  const unsigned& block_index_j,
39  vil_image_view_base_sptr& blk) const
40 {
41  bool found = false;
42  for (auto bit=blocks_.begin(); bit!= blocks_.end()&&!found; ++bit)
43  {
44  if ((*bit)->bindex_i_!=block_index_i||(*bit)->bindex_j_!=block_index_j)
45  continue;
46  else
47  {
48  found = true;
49  blk = (*bit)->blk_;
50  (*bit)->touch();//block is in demand so update the age to zero
51  }
52  }
53  return found;
54 }
55 
56 //:remove the oldest priority block
58 {
59  if(!blocks_.size()){
60  std::cerr << "warning: attempt to remove block from empty cache\n";
61  return false;
62  }
63  // queue should already be sorted
64  // remove oldest
65  auto bit = blocks_.begin();
66  blocks_.erase(bit);
67  return true;
68 }
unsigned nblocks_
capacity in blocks.
A block cache with block population prioritized by age.
bool get_block(const unsigned &block_index_i, const unsigned &block_index_j, vil_image_view_base_sptr &blk) const
retrieve a block from the buffer.
static unsigned long time_
std::vector< bcell * > blocks_
block index member.
bool add_block(const unsigned &block_index_i, const unsigned &block_index_j, vil_image_view_base_sptr const &blk)
add a block to the buffer.
bool remove_block()
remove the lowest priority block.