VTK
|
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 "vtkObject.h" 00027 00028 #include <map> // used for cache storage 00029 #include <list> // use for LRU ordering 00030 00031 //BTX 00032 class VTK_HYBRID_EXPORT vtkExodusIICacheKey 00033 { 00034 public: 00035 int Time; 00036 int ObjectType; 00037 int ObjectId; 00038 int ArrayId; 00039 vtkExodusIICacheKey() 00040 { 00041 Time = -1; 00042 ObjectType = -1; 00043 ObjectId = -1; 00044 ArrayId = -1; 00045 } 00046 vtkExodusIICacheKey( int time, int objType, int objId, int arrId ) 00047 { 00048 Time = time; 00049 ObjectType = objType; 00050 ObjectId = objId; 00051 ArrayId = arrId; 00052 } 00053 vtkExodusIICacheKey( const vtkExodusIICacheKey& src ) 00054 { 00055 Time = src.Time; 00056 ObjectType = src.ObjectType; 00057 ObjectId = src.ObjectId; 00058 ArrayId = src.ArrayId; 00059 } 00060 bool match( const vtkExodusIICacheKey&other, const vtkExodusIICacheKey& pattern ) const 00061 { 00062 if ( pattern.Time && this->Time != other.Time ) 00063 return false; 00064 if ( pattern.ObjectType && this->ObjectType != other.ObjectType ) 00065 return false; 00066 if ( pattern.ObjectId && this->ObjectId != other.ObjectId ) 00067 return false; 00068 if ( pattern.ArrayId && this->ArrayId != other.ArrayId ) 00069 return false; 00070 return true; 00071 } 00072 bool operator < ( const vtkExodusIICacheKey& other ) const 00073 { 00074 if ( this->Time < other.Time ) 00075 return true; 00076 else if ( this->Time > other.Time ) 00077 return false; 00078 if ( this->ObjectType < other.ObjectType ) 00079 return true; 00080 else if ( this->ObjectType > other.ObjectType ) 00081 return false; 00082 if ( this->ObjectId < other.ObjectId ) 00083 return true; 00084 else if ( this->ObjectId > other.ObjectId ) 00085 return false; 00086 if ( this->ArrayId < other.ArrayId ) 00087 return true; 00088 return false; 00089 } 00090 }; 00091 00092 class vtkExodusIICacheEntry; 00093 class vtkExodusIICache; 00094 class vtkDataArray; 00095 00096 typedef std::map<vtkExodusIICacheKey,vtkExodusIICacheEntry*> vtkExodusIICacheSet; 00097 typedef std::map<vtkExodusIICacheKey,vtkExodusIICacheEntry*>::iterator vtkExodusIICacheRef; 00098 typedef std::list<vtkExodusIICacheRef> vtkExodusIICacheLRU; 00099 typedef std::list<vtkExodusIICacheRef>::iterator vtkExodusIICacheLRURef; 00100 00101 class VTK_HYBRID_EXPORT vtkExodusIICacheEntry 00102 { 00103 public: 00104 vtkExodusIICacheEntry(); 00105 vtkExodusIICacheEntry( vtkDataArray* arr ); 00106 vtkExodusIICacheEntry( const vtkExodusIICacheEntry& other ); 00107 00108 ~vtkExodusIICacheEntry(); 00109 00110 vtkDataArray* GetValue() { return this->Value; } 00111 00112 protected: 00113 vtkDataArray* Value; 00114 vtkExodusIICacheLRURef LRUEntry; 00115 00116 friend class vtkExodusIICache; 00117 }; 00118 //ETX 00119 00120 class VTK_HYBRID_EXPORT vtkExodusIICache : public vtkObject 00121 { 00122 public: 00123 static vtkExodusIICache* New(); 00124 vtkTypeMacro(vtkExodusIICache,vtkObject); 00125 void PrintSelf( ostream& os, vtkIndent indent ); 00126 00128 void Clear(); 00129 00131 void SetCacheCapacity( double sizeInMiB ); 00132 00137 double GetSpaceLeft() 00138 { return this->Capacity - this->Size; } 00139 00143 int ReduceToSize( double newSize ); 00144 00145 //BTX 00147 void Insert( vtkExodusIICacheKey& key, vtkDataArray* value ); 00148 00152 vtkDataArray*& Find( vtkExodusIICacheKey ); 00153 00158 int Invalidate( vtkExodusIICacheKey key ); 00159 00169 int Invalidate( vtkExodusIICacheKey key, vtkExodusIICacheKey pattern ); 00170 //ETX 00171 00172 protected: 00174 vtkExodusIICache(); 00175 00177 ~vtkExodusIICache(); 00178 00179 00181 void RecomputeSize(); 00182 00184 double Capacity; 00185 00187 double Size; 00188 00189 //BTX 00196 vtkExodusIICacheSet Cache; 00197 00199 vtkExodusIICacheLRU LRU; 00200 //ETX 00201 00202 private: 00203 vtkExodusIICache( const vtkExodusIICache& ); // Not implemented 00204 void operator = ( const vtkExodusIICache& ); // Not implemented 00205 }; 00206 #endif // __vtkExodusIICache_h