vil_block_cache.h
Go to the documentation of this file.
1 // This is core/vil/vil_block_cache.h
2 #ifndef vil_block_cache_h_
3 #define vil_block_cache_h_
4 //:
5 // \file
6 // \brief A block cache with block population prioritized by age
7 // \author J. L. Mundy
8 //
9 #include <iostream>
10 #include <queue>
11 #include <vector>
12 #ifdef _MSC_VER
13 # include <vcl_msvc_warnings.h>
14 #endif
16 
17 // \verbatim
18 // Modifications
19 // J.L. Mundy replaced priority queue with sort on block vector
20 // container for simplicity, January 01, 2012
21 // \endverbatim
22 
23 //container for blocks to maintain a timestamp
24 //note that the larger value of time corresponds to the newest block
25 struct bcell
26 {
27  bcell(const unsigned bindex_i, const unsigned bindex_j,
28  vil_image_view_base_sptr const& blk) :
29  bindex_i_(bindex_i), bindex_j_(bindex_j), birthdate_(time_++), blk_(blk)
30  {}
31 
32  //:block indices
33  unsigned bindex_i_; unsigned bindex_j_;
34  //:the time of insertion into the queue
35  unsigned long birthdate_;
36  //:the block itself
38  //:update the age of a block
39  void touch(){birthdate_=time_++;}
40  //: for debug
41  void print() const { std::cout << '[' << bindex_i_ << ' ' << bindex_j_
42  << "](" << birthdate_ << ")\n"; }
43  private:
44  static unsigned long time_; //static timekeeper
45 };
46 // the ordering predicate for block birthdate. Oldest block is at
47 // blocks_.begin()
49 {
50  public:
51  bcell_less()= default;
52  //the predicate function
53  bool operator()(bcell* const& ba, bcell* const& bb) const
54  {
55  return ba->birthdate_ < bb->birthdate_;
56  }
57 };
59 {
60  public:
61  vil_block_cache(const unsigned block_capacity):nblocks_(block_capacity){}
63 
64  //:add a block to the buffer
65  bool add_block(const unsigned& block_index_i, const unsigned& block_index_j,
66  vil_image_view_base_sptr const& blk);
67 
68  //:retrieve a block from the buffer
69  bool get_block(const unsigned& block_index_i, const unsigned& block_index_j,
70  vil_image_view_base_sptr& blk) const;
71 
72  //:block capacity
73  unsigned block_size() const{return nblocks_;}
74  private:
75  //:block index member
76  std::vector<bcell*> blocks_;
77  //:capacity in blocks
78  unsigned nblocks_;
79  //:remove the lowest priority block
80  bool remove_block();
81 };
82 
83 #endif // vil_block_cache_h_
unsigned nblocks_
capacity in blocks.
bcell_less()=default
A base class reference-counting view of some image data.
void touch()
update the age of a block.
void print() const
for debug.
vil_image_view_base_sptr blk_
the block itself.
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.
unsigned bindex_i_
block indices.
unsigned block_size() const
block capacity.
static unsigned long time_
bool operator()(bcell *const &ba, bcell *const &bb) const
unsigned long birthdate_
the time of insertion into the queue.
std::vector< bcell * > blocks_
block index member.
unsigned bindex_j_
vil_block_cache(const unsigned block_capacity)
bcell(const unsigned bindex_i, const unsigned bindex_j, vil_image_view_base_sptr const &blk)
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.