VTK  9.3.20240417
vtkCGNSCache.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
3 
14 #ifndef vtkCGNSCache_h
15 #define vtkCGNSCache_h
16 
17 #include "vtkSmartPointer.h"
18 
19 #include <iterator>
20 #include <sstream>
21 #include <string>
22 #include <unordered_map>
23 
24 namespace CGNSRead
25 {
26 VTK_ABI_NAMESPACE_BEGIN
27 // No priority and no size limit right now
28 
29 template <typename CacheDataType>
31 {
32 public:
33  vtkCGNSCache();
34 
36 
38 
39  void ClearCache();
40 
41  void SetCacheSizeLimit(int size);
42  int GetCacheSizeLimit();
43 
44 private:
45  vtkCGNSCache(const vtkCGNSCache&) = delete;
46  void operator=(const vtkCGNSCache&) = delete;
47 
48  typedef std::unordered_map<std::string, vtkSmartPointer<CacheDataType>> CacheMapper;
49  CacheMapper CacheData;
50 
51  typename CacheMapper::iterator LastCacheAccess;
52 
53  int cacheSizeLimit;
54 };
55 
56 template <typename CacheDataType>
58  : CacheData()
59 {
60  this->cacheSizeLimit = -1;
61  this->LastCacheAccess = CacheData.end();
62 }
63 
64 template <typename CacheDataType>
66 {
67  this->cacheSizeLimit = size;
68 }
69 
70 template <typename CacheDataType>
72 {
73  return this->cacheSizeLimit;
74 }
75 
76 template <typename CacheDataType>
78 {
79  typename CacheMapper::iterator iter;
80  iter = this->CacheData.find(query);
81  if (iter == this->CacheData.end())
82  return vtkSmartPointer<CacheDataType>(nullptr);
83  this->LastCacheAccess = iter;
84  return iter->second;
85 }
86 
87 template <typename CacheDataType>
90 {
91 
92  if (this->cacheSizeLimit > 0 &&
93  this->CacheData.size() >= static_cast<size_t>(this->cacheSizeLimit))
94  {
95  // Make some room by removing last accessed/inserted item
96  this->CacheData.erase(this->LastCacheAccess);
97  }
98  this->CacheData[key] = data;
99  this->LastCacheAccess = this->CacheData.find(key);
100 }
101 
102 template <typename CacheDataType>
104 {
105  this->CacheData.clear();
106 }
107 VTK_ABI_NAMESPACE_END
108 }
109 #endif // vtkCGNSCache_h
110 // VTK-HeaderTest-Exclude: vtkCGNSCache.h
vtkSmartPointer< CacheDataType > Find(const std::string &query)
Definition: vtkCGNSCache.h:77
void Insert(const std::string &key, const vtkSmartPointer< CacheDataType > &data)
Definition: vtkCGNSCache.h:88
void SetCacheSizeLimit(int size)
Definition: vtkCGNSCache.h:65
Hold a reference to a vtkObjectBase instance.
This file defines functions used by vtkCGNSReader and vtkCGNSReaderInternal.
Definition: cgio_helpers.h:19
@ key
Definition: vtkX3D.h:257
@ size
Definition: vtkX3D.h:253
@ data
Definition: vtkX3D.h:315
@ string
Definition: vtkX3D.h:490