VTK  9.2.20230328
vtkCGNSCache.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkCGNSCache.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
15 
26 #ifndef vtkCGNSCache_h
27 #define vtkCGNSCache_h
28 
29 #include "vtkSmartPointer.h"
30 
31 #include <iterator>
32 #include <sstream>
33 #include <string>
34 #include <unordered_map>
35 
36 namespace CGNSRead
37 {
38 VTK_ABI_NAMESPACE_BEGIN
39 // No priority and no size limit right now
40 
41 template <typename CacheDataType>
43 {
44 public:
45  vtkCGNSCache();
46 
48 
50 
51  void ClearCache();
52 
53  void SetCacheSizeLimit(int size);
54  int GetCacheSizeLimit();
55 
56 private:
57  vtkCGNSCache(const vtkCGNSCache&) = delete;
58  void operator=(const vtkCGNSCache&) = delete;
59 
60  typedef std::unordered_map<std::string, vtkSmartPointer<CacheDataType>> CacheMapper;
61  CacheMapper CacheData;
62 
63  typename CacheMapper::iterator LastCacheAccess;
64 
65  int cacheSizeLimit;
66 };
67 
68 template <typename CacheDataType>
70  : CacheData()
71 {
72  this->cacheSizeLimit = -1;
73  this->LastCacheAccess = CacheData.end();
74 }
75 
76 template <typename CacheDataType>
78 {
79  this->cacheSizeLimit = size;
80 }
81 
82 template <typename CacheDataType>
84 {
85  return this->cacheSizeLimit;
86 }
87 
88 template <typename CacheDataType>
90 {
91  typename CacheMapper::iterator iter;
92  iter = this->CacheData.find(query);
93  if (iter == this->CacheData.end())
94  return vtkSmartPointer<CacheDataType>(nullptr);
95  this->LastCacheAccess = iter;
96  return iter->second;
97 }
98 
99 template <typename CacheDataType>
102 {
103 
104  if (this->cacheSizeLimit > 0 &&
105  this->CacheData.size() >= static_cast<size_t>(this->cacheSizeLimit))
106  {
107  // Make some room by removing last accessed/inserted item
108  this->CacheData.erase(this->LastCacheAccess);
109  }
110  this->CacheData[key] = data;
111  this->LastCacheAccess = this->CacheData.find(key);
112 }
113 
114 template <typename CacheDataType>
116 {
117  this->CacheData.clear();
118 }
119 VTK_ABI_NAMESPACE_END
120 }
121 #endif // vtkCGNSCache_h
122 // VTK-HeaderTest-Exclude: vtkCGNSCache.h
vtkSmartPointer< CacheDataType > Find(const std::string &query)
Definition: vtkCGNSCache.h:89
void Insert(const std::string &key, const vtkSmartPointer< CacheDataType > &data)
Definition: vtkCGNSCache.h:100
void SetCacheSizeLimit(int size)
Definition: vtkCGNSCache.h:77
Hold a reference to a vtkObjectBase instance.
This file defines functions used by vtkCGNSReader and vtkCGNSReaderInternal.
Definition: cgio_helpers.h:33
@ key
Definition: vtkX3D.h:269
@ size
Definition: vtkX3D.h:265
@ data
Definition: vtkX3D.h:327
@ string
Definition: vtkX3D.h:502