Mercurial > hg > orthanc
annotate OrthancFramework/Sources/Cache/LeastRecentlyUsedIndex.h @ 4141:99927401587c
define ORTHANC_FRAMEWORK_VERSION_IS_ABOVE even if Orthanc framework is statically linked
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 11 Aug 2020 16:57:34 +0200 |
parents | bf7b9edf6b81 |
children | fbc49a65340a |
rev | line source |
---|---|
282 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
1900 | 3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
1288
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
4 * Department, University Hospital of Liege, Belgium |
3640
94f4a18a79cc
upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3453
diff
changeset
|
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium |
282 | 6 * |
7 * This program is free software: you can redistribute it and/or | |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
8 * modify it under the terms of the GNU Lesser General Public License |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
9 * as published by the Free Software Foundation, either version 3 of |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
10 * the License, or (at your option) any later version. |
282 | 11 * |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
15 * Lesser General Public License for more details. |
282 | 16 * |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
17 * You should have received a copy of the GNU Lesser General Public |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
18 * License along with this program. If not, see |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
19 * <http://www.gnu.org/licenses/>. |
282 | 20 **/ |
21 | |
22 | |
23 #pragma once | |
24 | |
25 #include <list> | |
26 #include <map> | |
3453
8c663bbe5363
LeastRecentlyUsedIndex::GetAllKeys
Alain Mazy <alain@mazy.be>
parents:
3060
diff
changeset
|
27 #include <vector> |
282 | 28 #include <boost/noncopyable.hpp> |
283 | 29 #include <cassert> |
30 | |
31 #include "../OrthancException.h" | |
284
06aa7b7b6723
implementation of a single-threaded cache mechanism
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
283
diff
changeset
|
32 #include "../Toolbox.h" |
282 | 33 |
34 namespace Orthanc | |
35 { | |
36 /** | |
37 * This class implements the index of a cache with least recently | |
38 * used (LRU) recycling policy. All the items of the cache index | |
39 * can be associated with a payload. | |
40 * Reference: http://stackoverflow.com/a/2504317 | |
41 **/ | |
42 template <typename T, typename Payload = NullType> | |
505
f59e4518fd57
rename CacheIndex as LeastRecentlyUsedIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
43 class LeastRecentlyUsedIndex : public boost::noncopyable |
282 | 44 { |
45 private: | |
46 typedef std::list< std::pair<T, Payload> > Queue; | |
47 typedef std::map<T, typename Queue::iterator> Index; | |
48 | |
49 Index index_; | |
50 Queue queue_; | |
51 | |
52 /** | |
53 * Internal method for debug builds to check whether the internal | |
54 * data structures are not corrupted. | |
55 **/ | |
56 void CheckInvariants() const; | |
57 | |
58 public: | |
59 /** | |
60 * Add a new element to the cache index, and make it the most | |
61 * recent element. | |
62 * \param id The ID of the element. | |
63 * \param payload The payload of the element. | |
64 **/ | |
65 void Add(T id, Payload payload = Payload()); | |
66 | |
509
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
67 void AddOrMakeMostRecent(T id, Payload payload = Payload()); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
68 |
282 | 69 /** |
70 * When accessing an element of the cache, this method tags the | |
71 * element as the most recently used. | |
72 * \param id The most recently accessed item. | |
73 **/ | |
509
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
74 void MakeMostRecent(T id); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
75 |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
76 void MakeMostRecent(T id, Payload updatedPayload); |
282 | 77 |
78 /** | |
79 * Remove an element from the cache index. | |
80 * \param id The item to remove. | |
81 **/ | |
82 Payload Invalidate(T id); | |
83 | |
84 /** | |
85 * Get the oldest element in the cache and remove it. | |
86 * \return The oldest item. | |
87 **/ | |
509
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
88 T RemoveOldest(); |
282 | 89 |
90 /** | |
91 * Get the oldest element in the cache, remove it and return the | |
92 * associated payload. | |
93 * \param payload Where to store the associated payload. | |
94 * \return The oldest item. | |
95 **/ | |
96 T RemoveOldest(Payload& payload); | |
97 | |
98 /** | |
99 * Check whether an element is contained in the cache. | |
100 * \param id The item. | |
101 * \return \c true iff the item is indexed by the cache. | |
102 **/ | |
103 bool Contains(T id) const | |
104 { | |
105 return index_.find(id) != index_.end(); | |
106 } | |
107 | |
283 | 108 bool Contains(T id, Payload& payload) const |
109 { | |
110 typename Index::const_iterator it = index_.find(id); | |
111 if (it == index_.end()) | |
112 { | |
113 return false; | |
114 } | |
115 else | |
116 { | |
117 payload = it->second->second; | |
118 return true; | |
119 } | |
120 } | |
121 | |
122 /** | |
123 * Return the number of elements in the cache. | |
124 * \return The number of elements. | |
125 **/ | |
126 size_t GetSize() const | |
127 { | |
128 assert(index_.size() == queue_.size()); | |
129 return queue_.size(); | |
130 } | |
131 | |
282 | 132 /** |
133 * Check whether the cache index is empty. | |
134 * \return \c true iff the cache is empty. | |
135 **/ | |
136 bool IsEmpty() const | |
137 { | |
138 return index_.empty(); | |
139 } | |
507
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
140 |
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
141 const T& GetOldest() const; |
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
142 |
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
143 const Payload& GetOldestPayload() const; |
3453
8c663bbe5363
LeastRecentlyUsedIndex::GetAllKeys
Alain Mazy <alain@mazy.be>
parents:
3060
diff
changeset
|
144 |
8c663bbe5363
LeastRecentlyUsedIndex::GetAllKeys
Alain Mazy <alain@mazy.be>
parents:
3060
diff
changeset
|
145 void GetAllKeys(std::vector<T>& keys) const |
8c663bbe5363
LeastRecentlyUsedIndex::GetAllKeys
Alain Mazy <alain@mazy.be>
parents:
3060
diff
changeset
|
146 { |
8c663bbe5363
LeastRecentlyUsedIndex::GetAllKeys
Alain Mazy <alain@mazy.be>
parents:
3060
diff
changeset
|
147 keys.clear(); |
8c663bbe5363
LeastRecentlyUsedIndex::GetAllKeys
Alain Mazy <alain@mazy.be>
parents:
3060
diff
changeset
|
148 keys.reserve(GetSize()); |
8c663bbe5363
LeastRecentlyUsedIndex::GetAllKeys
Alain Mazy <alain@mazy.be>
parents:
3060
diff
changeset
|
149 for (typename Index::const_iterator it = index_.begin(); it != index_.end(); it++) |
8c663bbe5363
LeastRecentlyUsedIndex::GetAllKeys
Alain Mazy <alain@mazy.be>
parents:
3060
diff
changeset
|
150 { |
8c663bbe5363
LeastRecentlyUsedIndex::GetAllKeys
Alain Mazy <alain@mazy.be>
parents:
3060
diff
changeset
|
151 keys.push_back(it->first); |
8c663bbe5363
LeastRecentlyUsedIndex::GetAllKeys
Alain Mazy <alain@mazy.be>
parents:
3060
diff
changeset
|
152 } |
8c663bbe5363
LeastRecentlyUsedIndex::GetAllKeys
Alain Mazy <alain@mazy.be>
parents:
3060
diff
changeset
|
153 } |
8c663bbe5363
LeastRecentlyUsedIndex::GetAllKeys
Alain Mazy <alain@mazy.be>
parents:
3060
diff
changeset
|
154 |
282 | 155 }; |
283 | 156 |
157 | |
158 | |
159 | |
160 /****************************************************************** | |
161 ** Implementation of the template | |
162 ******************************************************************/ | |
163 | |
164 template <typename T, typename Payload> | |
505
f59e4518fd57
rename CacheIndex as LeastRecentlyUsedIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
165 void LeastRecentlyUsedIndex<T, Payload>::CheckInvariants() const |
283 | 166 { |
167 #ifndef NDEBUG | |
168 assert(index_.size() == queue_.size()); | |
169 | |
170 for (typename Index::const_iterator | |
171 it = index_.begin(); it != index_.end(); it++) | |
172 { | |
173 assert(it->second != queue_.end()); | |
174 assert(it->second->first == it->first); | |
175 } | |
176 #endif | |
177 } | |
178 | |
179 | |
180 template <typename T, typename Payload> | |
505
f59e4518fd57
rename CacheIndex as LeastRecentlyUsedIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
181 void LeastRecentlyUsedIndex<T, Payload>::Add(T id, Payload payload) |
283 | 182 { |
183 if (Contains(id)) | |
184 { | |
185 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
186 } | |
187 | |
188 queue_.push_front(std::make_pair(id, payload)); | |
189 index_[id] = queue_.begin(); | |
190 | |
191 CheckInvariants(); | |
192 } | |
193 | |
194 | |
195 template <typename T, typename Payload> | |
509
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
196 void LeastRecentlyUsedIndex<T, Payload>::MakeMostRecent(T id) |
283 | 197 { |
198 if (!Contains(id)) | |
199 { | |
200 throw OrthancException(ErrorCode_InexistentItem); | |
201 } | |
202 | |
203 typename Index::iterator it = index_.find(id); | |
204 assert(it != index_.end()); | |
205 | |
206 std::pair<T, Payload> item = *(it->second); | |
207 | |
208 queue_.erase(it->second); | |
209 queue_.push_front(item); | |
210 index_[id] = queue_.begin(); | |
211 | |
212 CheckInvariants(); | |
213 } | |
214 | |
215 | |
216 template <typename T, typename Payload> | |
509
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
217 void LeastRecentlyUsedIndex<T, Payload>::AddOrMakeMostRecent(T id, Payload payload) |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
218 { |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
219 typename Index::iterator it = index_.find(id); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
220 |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
221 if (it != index_.end()) |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
222 { |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
223 // Already existing. Make it most recent. |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
224 std::pair<T, Payload> item = *(it->second); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
225 item.second = payload; |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
226 queue_.erase(it->second); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
227 queue_.push_front(item); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
228 } |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
229 else |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
230 { |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
231 // New item |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
232 queue_.push_front(std::make_pair(id, payload)); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
233 } |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
234 |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
235 index_[id] = queue_.begin(); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
236 |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
237 CheckInvariants(); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
238 } |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
239 |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
240 |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
241 template <typename T, typename Payload> |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
242 void LeastRecentlyUsedIndex<T, Payload>::MakeMostRecent(T id, Payload updatedPayload) |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
243 { |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
244 if (!Contains(id)) |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
245 { |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
246 throw OrthancException(ErrorCode_InexistentItem); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
247 } |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
248 |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
249 typename Index::iterator it = index_.find(id); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
250 assert(it != index_.end()); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
251 |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
252 std::pair<T, Payload> item = *(it->second); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
253 item.second = updatedPayload; |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
254 |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
255 queue_.erase(it->second); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
256 queue_.push_front(item); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
257 index_[id] = queue_.begin(); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
258 |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
259 CheckInvariants(); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
260 } |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
261 |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
262 |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
263 template <typename T, typename Payload> |
505
f59e4518fd57
rename CacheIndex as LeastRecentlyUsedIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
264 Payload LeastRecentlyUsedIndex<T, Payload>::Invalidate(T id) |
283 | 265 { |
266 if (!Contains(id)) | |
267 { | |
268 throw OrthancException(ErrorCode_InexistentItem); | |
269 } | |
270 | |
271 typename Index::iterator it = index_.find(id); | |
272 assert(it != index_.end()); | |
273 | |
274 Payload payload = it->second->second; | |
275 queue_.erase(it->second); | |
276 index_.erase(it); | |
277 | |
278 CheckInvariants(); | |
279 return payload; | |
280 } | |
281 | |
282 | |
283 template <typename T, typename Payload> | |
505
f59e4518fd57
rename CacheIndex as LeastRecentlyUsedIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
284 T LeastRecentlyUsedIndex<T, Payload>::RemoveOldest(Payload& payload) |
283 | 285 { |
286 if (IsEmpty()) | |
287 { | |
288 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
289 } | |
290 | |
291 std::pair<T, Payload> item = queue_.back(); | |
292 T oldest = item.first; | |
293 payload = item.second; | |
294 | |
295 queue_.pop_back(); | |
296 assert(index_.find(oldest) != index_.end()); | |
297 index_.erase(oldest); | |
298 | |
299 CheckInvariants(); | |
300 | |
301 return oldest; | |
302 } | |
507
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
303 |
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
304 |
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
305 template <typename T, typename Payload> |
509
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
306 T LeastRecentlyUsedIndex<T, Payload>::RemoveOldest() |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
307 { |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
308 if (IsEmpty()) |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
309 { |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
310 throw OrthancException(ErrorCode_BadSequenceOfCalls); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
311 } |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
312 |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
313 std::pair<T, Payload> item = queue_.back(); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
314 T oldest = item.first; |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
315 |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
316 queue_.pop_back(); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
317 assert(index_.find(oldest) != index_.end()); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
318 index_.erase(oldest); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
319 |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
320 CheckInvariants(); |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
321 |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
322 return oldest; |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
323 } |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
324 |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
325 |
e7841864c97c
StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
507
diff
changeset
|
326 template <typename T, typename Payload> |
507
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
327 const T& LeastRecentlyUsedIndex<T, Payload>::GetOldest() const |
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
328 { |
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
329 if (IsEmpty()) |
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
330 { |
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
331 throw OrthancException(ErrorCode_BadSequenceOfCalls); |
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
332 } |
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
333 |
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
334 return queue_.back().first; |
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
335 } |
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
336 |
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
337 |
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
338 template <typename T, typename Payload> |
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
339 const Payload& LeastRecentlyUsedIndex<T, Payload>::GetOldestPayload() const |
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
340 { |
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
341 if (IsEmpty()) |
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
342 { |
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
343 throw OrthancException(ErrorCode_BadSequenceOfCalls); |
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
344 } |
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
345 |
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
346 return queue_.back().second; |
c4122c3a47c1
access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
505
diff
changeset
|
347 } |
282 | 348 } |