comparison Core/Cache/LeastRecentlyUsedIndex.h @ 509:e7841864c97c

StableResourcesMonitor
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 16 Aug 2013 14:23:54 +0200
parents c4122c3a47c1
children 2d0a347e8cfc
comparison
equal deleted inserted replaced
507:c4122c3a47c1 509:e7841864c97c
71 * \param id The ID of the element. 71 * \param id The ID of the element.
72 * \param payload The payload of the element. 72 * \param payload The payload of the element.
73 **/ 73 **/
74 void Add(T id, Payload payload = Payload()); 74 void Add(T id, Payload payload = Payload());
75 75
76 void AddOrMakeMostRecent(T id, Payload payload = Payload());
77
76 /** 78 /**
77 * When accessing an element of the cache, this method tags the 79 * When accessing an element of the cache, this method tags the
78 * element as the most recently used. 80 * element as the most recently used.
79 * \param id The most recently accessed item. 81 * \param id The most recently accessed item.
80 **/ 82 **/
81 void TagAsMostRecent(T id); 83 void MakeMostRecent(T id);
84
85 void MakeMostRecent(T id, Payload updatedPayload);
82 86
83 /** 87 /**
84 * Remove an element from the cache index. 88 * Remove an element from the cache index.
85 * \param id The item to remove. 89 * \param id The item to remove.
86 **/ 90 **/
88 92
89 /** 93 /**
90 * Get the oldest element in the cache and remove it. 94 * Get the oldest element in the cache and remove it.
91 * \return The oldest item. 95 * \return The oldest item.
92 **/ 96 **/
93 T RemoveOldest() 97 T RemoveOldest();
94 {
95 Payload p;
96 return RemoveOldest(p);
97 }
98 98
99 /** 99 /**
100 * Get the oldest element in the cache, remove it and return the 100 * Get the oldest element in the cache, remove it and return the
101 * associated payload. 101 * associated payload.
102 * \param payload Where to store the associated payload. 102 * \param payload Where to store the associated payload.
189 CheckInvariants(); 189 CheckInvariants();
190 } 190 }
191 191
192 192
193 template <typename T, typename Payload> 193 template <typename T, typename Payload>
194 void LeastRecentlyUsedIndex<T, Payload>::TagAsMostRecent(T id) 194 void LeastRecentlyUsedIndex<T, Payload>::MakeMostRecent(T id)
195 { 195 {
196 if (!Contains(id)) 196 if (!Contains(id))
197 { 197 {
198 throw OrthancException(ErrorCode_InexistentItem); 198 throw OrthancException(ErrorCode_InexistentItem);
199 } 199 }
210 CheckInvariants(); 210 CheckInvariants();
211 } 211 }
212 212
213 213
214 template <typename T, typename Payload> 214 template <typename T, typename Payload>
215 void LeastRecentlyUsedIndex<T, Payload>::AddOrMakeMostRecent(T id, Payload payload)
216 {
217 typename Index::iterator it = index_.find(id);
218
219 if (it != index_.end())
220 {
221 // Already existing. Make it most recent.
222 std::pair<T, Payload> item = *(it->second);
223 item.second = payload;
224 queue_.erase(it->second);
225 queue_.push_front(item);
226 }
227 else
228 {
229 // New item
230 queue_.push_front(std::make_pair(id, payload));
231 }
232
233 index_[id] = queue_.begin();
234
235 CheckInvariants();
236 }
237
238
239 template <typename T, typename Payload>
240 void LeastRecentlyUsedIndex<T, Payload>::MakeMostRecent(T id, Payload updatedPayload)
241 {
242 if (!Contains(id))
243 {
244 throw OrthancException(ErrorCode_InexistentItem);
245 }
246
247 typename Index::iterator it = index_.find(id);
248 assert(it != index_.end());
249
250 std::pair<T, Payload> item = *(it->second);
251 item.second = updatedPayload;
252
253 queue_.erase(it->second);
254 queue_.push_front(item);
255 index_[id] = queue_.begin();
256
257 CheckInvariants();
258 }
259
260
261 template <typename T, typename Payload>
215 Payload LeastRecentlyUsedIndex<T, Payload>::Invalidate(T id) 262 Payload LeastRecentlyUsedIndex<T, Payload>::Invalidate(T id)
216 { 263 {
217 if (!Contains(id)) 264 if (!Contains(id))
218 { 265 {
219 throw OrthancException(ErrorCode_InexistentItem); 266 throw OrthancException(ErrorCode_InexistentItem);
252 return oldest; 299 return oldest;
253 } 300 }
254 301
255 302
256 template <typename T, typename Payload> 303 template <typename T, typename Payload>
304 T LeastRecentlyUsedIndex<T, Payload>::RemoveOldest()
305 {
306 if (IsEmpty())
307 {
308 throw OrthancException(ErrorCode_BadSequenceOfCalls);
309 }
310
311 std::pair<T, Payload> item = queue_.back();
312 T oldest = item.first;
313
314 queue_.pop_back();
315 assert(index_.find(oldest) != index_.end());
316 index_.erase(oldest);
317
318 CheckInvariants();
319
320 return oldest;
321 }
322
323
324 template <typename T, typename Payload>
257 const T& LeastRecentlyUsedIndex<T, Payload>::GetOldest() const 325 const T& LeastRecentlyUsedIndex<T, Payload>::GetOldest() const
258 { 326 {
259 if (IsEmpty()) 327 if (IsEmpty())
260 { 328 {
261 throw OrthancException(ErrorCode_BadSequenceOfCalls); 329 throw OrthancException(ErrorCode_BadSequenceOfCalls);