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 //container for blocks to maintain a timestamp and mutation state
00018 struct bcell
00019 {
00020   bcell(const unsigned bindex_i, const unsigned bindex_j,
00021         vil_image_view_base_sptr const& blk) :
00022     bindex_i_(bindex_i), bindex_j_(bindex_j), birthdate_(time_++), blk_(blk)
00023   {}
00024 
00025   //:block indices
00026   unsigned bindex_i_;  unsigned bindex_j_;
00027   //:the time of insertion into the queue
00028   unsigned long birthdate_;
00029   //:the block itself
00030   vil_image_view_base_sptr blk_;
00031   //:update the age of a block
00032   void touch(){birthdate_=time_++;}
00033   //: for debug
00034   void print() const { vcl_cout << '[' << bindex_i_ << ' ' << bindex_j_
00035                                 << "](" << birthdate_ << ")\n"; }
00036  private:
00037   static unsigned long time_; //static timekeeper
00038 };
00039 
00040 class vil_block_cache
00041 {
00042  public:
00043   vil_block_cache(const unsigned block_capacity):nblocks_(block_capacity){}
00044   ~vil_block_cache();
00045 
00046   //:add a block to the buffer
00047   bool add_block(const unsigned& block_index_i, const unsigned& block_index_j,
00048                  vil_image_view_base_sptr const& blk);
00049 
00050   //:retrieve a block from the buffer
00051   bool get_block(const unsigned& block_index_i, const unsigned& block_index_j,
00052                  vil_image_view_base_sptr& blk) const;
00053 
00054   //:block capacity
00055   unsigned block_size() const{return nblocks_;}
00056  private:
00057   struct compare
00058   {
00059     bool operator()(bcell* const& c1, bcell* const& c2) const
00060     { if (c1&&c2)return c1->birthdate_ > c2->birthdate_; else return false; }
00061   };
00062   //:block queue member
00063   vcl_priority_queue<compare, vcl_vector<bcell*> > queue_;
00064   //:block index member
00065   vcl_vector<bcell*> blocks_;
00066   //:capacity in blocks
00067   unsigned nblocks_;
00068   //:remove the lowest priority block
00069   bool remove_block();
00070 };
00071 
00072 #endif // vil_block_cache_h_

Generated on Thu Aug 21 05:07:43 2008 for core/vil by  doxygen 1.5.1