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   bool match( const vtkExodusIICacheKey&other, const vtkExodusIICacheKey& pattern ) const
00062     {
00063     if ( pattern.Time && this->Time != other.Time )
00064       return false;
00065     if ( pattern.ObjectType && this->ObjectType != other.ObjectType )
00066       return false;
00067     if ( pattern.ObjectId && this->ObjectId != other.ObjectId )
00068       return false;
00069     if ( pattern.ArrayId && this->ArrayId != other.ArrayId )
00070       return false;
00071     return true;
00072     }
00073   bool operator < ( const vtkExodusIICacheKey& other ) const
00074     {
00075     if ( this->Time < other.Time )
00076       return true;
00077     else if ( this->Time > other.Time )
00078       return false;
00079     if ( this->ObjectType < other.ObjectType )
00080       return true;
00081     else if ( this->ObjectType > other.ObjectType )
00082       return false;
00083     if ( this->ObjectId < other.ObjectId )
00084       return true;
00085     else if ( this->ObjectId > other.ObjectId )
00086       return false;
00087     if ( this->ArrayId < other.ArrayId )
00088       return true;
00089     return false;
00090     }
00091 };
00092 
00093 class vtkExodusIICacheEntry;
00094 class vtkExodusIICache;
00095 class vtkDataArray;
00096 
00097 typedef std::map<vtkExodusIICacheKey,vtkExodusIICacheEntry*> vtkExodusIICacheSet;
00098 typedef std::map<vtkExodusIICacheKey,vtkExodusIICacheEntry*>::iterator vtkExodusIICacheRef;
00099 typedef std::list<vtkExodusIICacheRef> vtkExodusIICacheLRU;
00100 typedef std::list<vtkExodusIICacheRef>::iterator vtkExodusIICacheLRURef;
00101 
00102 class VTKIOEXODUS_EXPORT vtkExodusIICacheEntry
00103 {
00104 public:
00105   vtkExodusIICacheEntry();
00106   vtkExodusIICacheEntry( vtkDataArray* arr );
00107   vtkExodusIICacheEntry( const vtkExodusIICacheEntry& other );
00108 
00109   ~vtkExodusIICacheEntry();
00110 
00111   vtkDataArray* GetValue() { return this->Value; }
00112 
00113 protected:
00114   vtkDataArray* Value;
00115   vtkExodusIICacheLRURef LRUEntry;
00116 
00117   friend class vtkExodusIICache;
00118 };
00119 //ETX
00120 
00121 class VTKIOEXODUS_EXPORT vtkExodusIICache : public vtkObject
00122 {
00123 public:
00124   static vtkExodusIICache* New();
00125   vtkTypeMacro(vtkExodusIICache,vtkObject);
00126   void PrintSelf( ostream& os, vtkIndent indent );
00127 
00129   void Clear();
00130 
00132   void SetCacheCapacity( double sizeInMiB );
00133 
00138   double GetSpaceLeft()
00139     { return this->Capacity - this->Size; }
00140 
00144   int ReduceToSize( double newSize );
00145 
00146   //BTX
00148   void Insert( vtkExodusIICacheKey& key, vtkDataArray* value );
00149 
00153   vtkDataArray*& Find( vtkExodusIICacheKey );
00154 
00159   int Invalidate( vtkExodusIICacheKey key );
00160 
00170   int Invalidate( vtkExodusIICacheKey key, vtkExodusIICacheKey pattern );
00171   //ETX
00172 
00173 protected:
00175   vtkExodusIICache();
00176 
00178   ~vtkExodusIICache();
00179 
00180 
00182   void RecomputeSize();
00183 
00185   double Capacity;
00186 
00188   double Size;
00189 
00190   //BTX
00197   vtkExodusIICacheSet Cache;
00198 
00200   vtkExodusIICacheLRU LRU;
00201   //ETX
00202 
00203 private:
00204   vtkExodusIICache( const vtkExodusIICache& ); // Not implemented
00205   void operator = ( const vtkExodusIICache& ); // Not implemented
00206 };
00207 #endif // __vtkExodusIICache_h