/*========================================================================= Program: Visualization Toolkit Module: vtkCGNSCache.h Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen All rights reserved. See Copyright.txt or http://www.kitware.com/Copyright.htm for details. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notice for more information. =========================================================================*/ /** * @class vtkCGNSCache * * store an object in a container with its CGNS path key * * * @par Thanks: * Thanks to Mickael Philit */ #ifndef vtkCGNSCache_h #define vtkCGNSCache_h #include "vtkSmartPointer.h" #include #include #include #include namespace CGNSRead { // No priority and no size limit right now template class vtkCGNSCache { public: vtkCGNSCache(); vtkSmartPointer Find(const std::string& query); void Insert(const std::string& key, const vtkSmartPointer& data); void ClearCache(); void SetCacheSizeLimit(int size); int GetCacheSizeLimit(); private: vtkCGNSCache(const vtkCGNSCache&) = delete; void operator=(const vtkCGNSCache&) = delete; typedef std::unordered_map> CacheMapper; CacheMapper CacheData; typename CacheMapper::iterator LastCacheAccess; int cacheSizeLimit; }; template vtkCGNSCache::vtkCGNSCache() : CacheData() { this->cacheSizeLimit = -1; this->LastCacheAccess = CacheData.end(); } template void vtkCGNSCache::SetCacheSizeLimit(int size) { this->cacheSizeLimit = size; } template int vtkCGNSCache::GetCacheSizeLimit() { return this->cacheSizeLimit; } template vtkSmartPointer vtkCGNSCache::Find(const std::string& query) { typename CacheMapper::iterator iter; iter = this->CacheData.find(query); if (iter == this->CacheData.end()) return vtkSmartPointer(nullptr); this->LastCacheAccess = iter; return iter->second; } template void vtkCGNSCache::Insert( const std::string& key, const vtkSmartPointer& data) { if (this->cacheSizeLimit > 0 && this->CacheData.size() >= static_cast(this->cacheSizeLimit)) { // Make some room by removing last accessed/inserted item this->CacheData.erase(this->LastCacheAccess); } this->CacheData[key] = data; this->LastCacheAccess = this->CacheData.find(key); } template void vtkCGNSCache::ClearCache() { this->CacheData.clear(); } } #endif // vtkCGNSCache_h // VTK-HeaderTest-Exclude: vtkCGNSCache.h