VTK
dox/IO/Exodus/vtkExodusIICache.h
Go to the documentation of this file.
00001 #ifndef __vtkExodusIICache_h
00002 #define __vtkExodusIICache_h
00003 
00004 // ============================================================================
00005 // The following classes define an LRU cache for data arrays
00006 // loaded by the Exodus reader. Here's how they work:
00007 //
00008 // The actual cache consists of two STL containers: a set of
00009 // cache entries (vtkExodusIICacheEntry) and a list of
00010 // cache references (vtkExodusIICacheRef). The entries in
00011 // these containers are sorted for fast retrieval:
00012 // 1. The cache entries are indexed by the timestep, the
00013 //    object type (edge block, face set, ...), and the
00014 //    object ID (if one exists). When you call Find() to
00015 //    retrieve a cache entry, you provide a key containing
00016 //    this information and the array is returned if it exists.
00017 // 2. The list of cache references are stored in "least-recently-used"
00018 //    order. The least recently referenced array is the first in
00019 //    the list. Whenever you request an entry with Find(), it is
00020 //    moved to the back of the list if it exists.
00021 // This makes retrieving arrays O(n log n) and popping LRU
00022 // entries O(1). Each cache entry stores an iterator into
00023 // the list of references so that it can be located quickly for
00024 // removal.
00025 
00026 #include "vtkIOExodusModule.h" // For export macro
00027 #include "vtkObject.h"
00028 
00029 #include <map> // used for cache storage
00030 #include <list> // use for LRU ordering
00031 
00032 //BTX
00033 class VTKIOEXODUS_EXPORT vtkExodusIICacheKey
00034 {
00035 public:
00036   int Time;
00037   int ObjectType;
00038   int ObjectId;
00039   int ArrayId;
00040   vtkExodusIICacheKey()
00041     {
00042     Time = -1;
00043     ObjectType = -1;
00044     ObjectId = -1;
00045     ArrayId = -1;
00046     }
00047   vtkExodusIICacheKey( int time, int objType, int objId, int arrId )
00048     {
00049     Time = time;
00050     ObjectType = objType;
00051     ObjectId = objId;
00052     ArrayId = arrId;
00053     }
00054   vtkExodusIICacheKey( const vtkExodusIICacheKey& src )
00055     {
00056     Time = src.Time;
00057     ObjectType = src.ObjectType;
00058     ObjectId = src.ObjectId;
00059     ArrayId = src.ArrayId;
00060     }
00061   vtkExodusIICacheKey& operator = ( const vtkExodusIICacheKey& src )
00062     {
00063     Time = src.Time;
00064     ObjectType = src.ObjectType;
00065     ObjectId = src.ObjectId;
00066     ArrayId = src.ArrayId;
00067     return *this;
00068     }
00069   bool match( const vtkExodusIICacheKey&other, const vtkExodusIICacheKey& pattern ) const
00070     {
00071     if ( pattern.Time && this->Time != other.Time )
00072       return false;
00073     if ( pattern.ObjectType && this->ObjectType != other.ObjectType )
00074       return false;
00075     if ( pattern.ObjectId && this->ObjectId != other.ObjectId )
00076       return false;
00077     if ( pattern.ArrayId && this->ArrayId != other.ArrayId )
00078       return false;
00079     return true;
00080     }
00081   bool operator < ( const vtkExodusIICacheKey& other ) const
00082     {
00083     if ( this->Time < other.Time )
00084       return true;
00085     else if ( this->Time > other.Time )
00086       return false;
00087     if ( this->ObjectType < other.ObjectType )
00088       return true;
00089     else if ( this->ObjectType > other.ObjectType )
00090       return false;
00091     if ( this->ObjectId < other.ObjectId )
00092       return true;
00093     else if ( this->ObjectId > other.ObjectId )
00094       return false;
00095     if ( this->ArrayId < other.ArrayId )
00096       return true;
00097     return false;
00098     }
00099 };
00100 
00101 class vtkExodusIICacheEntry;
00102 class vtkExodusIICache;
00103 class vtkDataArray;
00104 
00105 typedef std::map<vtkExodusIICacheKey,vtkExodusIICacheEntry*> vtkExodusIICacheSet;
00106 typedef std::map<vtkExodusIICacheKey,vtkExodusIICacheEntry*>::iterator vtkExodusIICacheRef;
00107 typedef std::list<vtkExodusIICacheRef> vtkExodusIICacheLRU;
00108 typedef std::list<vtkExodusIICacheRef>::iterator vtkExodusIICacheLRURef;
00109 
00110 class VTKIOEXODUS_EXPORT vtkExodusIICacheEntry
00111 {
00112 public:
00113   vtkExodusIICacheEntry();
00114   vtkExodusIICacheEntry( vtkDataArray* arr );
00115   vtkExodusIICacheEntry( const vtkExodusIICacheEntry& other );
00116 
00117   ~vtkExodusIICacheEntry();
00118 
00119   vtkDataArray* GetValue() { return this->Value; }
00120 
00121 protected:
00122   vtkDataArray* Value;
00123   vtkExodusIICacheLRURef LRUEntry;
00124 
00125   friend class vtkExodusIICache;
00126 };
00127 //ETX
00128 
00129 class VTKIOEXODUS_EXPORT vtkExodusIICache : public vtkObject
00130 {
00131 public:
00132   static vtkExodusIICache* New();
00133   vtkTypeMacro(vtkExodusIICache,vtkObject);
00134   void PrintSelf( ostream& os, vtkIndent indent );
00135 
00137   void Clear();
00138 
00140   void SetCacheCapacity( double sizeInMiB );
00141 
00146   double GetSpaceLeft()
00147     { return this->Capacity - this->Size; }
00148 
00152   int ReduceToSize( double newSize );
00153 
00154   //BTX
00156   void Insert( vtkExodusIICacheKey& key, vtkDataArray* value );
00157 
00161   vtkDataArray*& Find( vtkExodusIICacheKey );
00162 
00167   int Invalidate( vtkExodusIICacheKey key );
00168 
00178   int Invalidate( vtkExodusIICacheKey key, vtkExodusIICacheKey pattern );
00179   //ETX
00180 
00181 protected:
00183   vtkExodusIICache();
00184 
00186   ~vtkExodusIICache();
00187 
00188 
00190   void RecomputeSize();
00191 
00193   double Capacity;
00194 
00196   double Size;
00197 
00198   //BTX
00205   vtkExodusIICacheSet Cache;
00206 
00208   vtkExodusIICacheLRU LRU;
00209   //ETX
00210 
00211 private:
00212   vtkExodusIICache( const vtkExodusIICache& ); // Not implemented
00213   void operator = ( const vtkExodusIICache& ); // Not implemented
00214 };
00215 #endif // __vtkExodusIICache_h