core/vil/vil_block_cache.h
Go to the documentation of this file.
00001 // This is core/vil/vil_block_cache.h
00002 #ifndef vil_block_cache_h_
00003 #define vil_block_cache_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \brief A block cache with block population prioritized by age
00010 // \author J. L. Mundy
00011 //
00012 #include <vcl_iostream.h>
00013 #include <vcl_queue.h>
00014 #include <vcl_vector.h>
00015 #include <vil/vil_image_view_base.h>
00016 
00017 // \verbatim
00018 //  Modifications
00019 //   J.L. Mundy replaced priority queue with sort on block vector
00020 //   container for simplicity, January 01, 2012
00021 // \endverbatim
00022 
00023 //container for blocks to maintain a timestamp 
00024 //note that the larger value of time corresponds to the newest block
00025 struct bcell
00026 {
00027   bcell(const unsigned bindex_i, const unsigned bindex_j,
00028         vil_image_view_base_sptr const& blk) :
00029     bindex_i_(bindex_i), bindex_j_(bindex_j), birthdate_(time_++), blk_(blk)
00030   {}
00031 
00032   //:block indices
00033   unsigned bindex_i_;  unsigned bindex_j_;
00034   //:the time of insertion into the queue
00035   unsigned long birthdate_;
00036   //:the block itself
00037   vil_image_view_base_sptr blk_;
00038   //:update the age of a block
00039   void touch(){birthdate_=time_++;}
00040   //: for debug
00041   void print() const { vcl_cout << '[' << bindex_i_ << ' ' << bindex_j_
00042                                 << "](" << birthdate_ << ")\n"; }
00043  private:
00044   static unsigned long time_; //static timekeeper
00045 };
00046 // the ordering predicate for block birthdate. Oldest block is at 
00047 // blocks_.begin()
00048 class bcell_less
00049 {
00050  public:
00051   bcell_less(){}
00052   //the predicate function
00053   bool operator()(bcell* const& ba, bcell* const& bb) const
00054   {
00055     return ba->birthdate_ < bb->birthdate_;
00056   }
00057 };
00058 class vil_block_cache
00059 {
00060  public:
00061   vil_block_cache(const unsigned block_capacity):nblocks_(block_capacity){}
00062   ~vil_block_cache();
00063 
00064   //:add a block to the buffer
00065   bool add_block(const unsigned& block_index_i, const unsigned& block_index_j,
00066                  vil_image_view_base_sptr const& blk);
00067 
00068   //:retrieve a block from the buffer
00069   bool get_block(const unsigned& block_index_i, const unsigned& block_index_j,
00070                  vil_image_view_base_sptr& blk) const;
00071 
00072   //:block capacity
00073   unsigned block_size() const{return nblocks_;}
00074  private:
00075   //:block index member
00076   vcl_vector<bcell*> blocks_;
00077   //:capacity in blocks
00078   unsigned nblocks_;
00079   //:remove the lowest priority block
00080   bool remove_block();
00081 };
00082 
00083 #endif // vil_block_cache_h_