diff 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
line wrap: on
line diff
--- a/Core/Cache/LeastRecentlyUsedIndex.h	Fri Aug 16 10:24:49 2013 +0200
+++ b/Core/Cache/LeastRecentlyUsedIndex.h	Fri Aug 16 14:23:54 2013 +0200
@@ -73,12 +73,16 @@
      **/
     void Add(T id, Payload payload = Payload());
 
+    void AddOrMakeMostRecent(T id, Payload payload = Payload());
+
     /**
      * When accessing an element of the cache, this method tags the
      * element as the most recently used.
      * \param id The most recently accessed item.
      **/
-    void TagAsMostRecent(T id);
+    void MakeMostRecent(T id);
+
+    void MakeMostRecent(T id, Payload updatedPayload);
 
     /**
      * Remove an element from the cache index.
@@ -90,11 +94,7 @@
      * Get the oldest element in the cache and remove it.
      * \return The oldest item.
      **/
-    T RemoveOldest()
-    {
-      Payload p;
-      return RemoveOldest(p);
-    }
+    T RemoveOldest();
 
     /**
      * Get the oldest element in the cache, remove it and return the
@@ -191,7 +191,7 @@
 
 
   template <typename T, typename Payload>
-  void LeastRecentlyUsedIndex<T, Payload>::TagAsMostRecent(T id)
+  void LeastRecentlyUsedIndex<T, Payload>::MakeMostRecent(T id)
   {
     if (!Contains(id))
     {
@@ -212,6 +212,53 @@
 
 
   template <typename T, typename Payload>
+  void LeastRecentlyUsedIndex<T, Payload>::AddOrMakeMostRecent(T id, Payload payload)
+  {
+    typename Index::iterator it = index_.find(id);
+
+    if (it != index_.end())
+    {
+      // Already existing. Make it most recent.
+      std::pair<T, Payload> item = *(it->second);
+      item.second = payload;
+      queue_.erase(it->second);
+      queue_.push_front(item);
+    }
+    else
+    {
+      // New item
+      queue_.push_front(std::make_pair(id, payload));
+    }
+
+    index_[id] = queue_.begin();
+
+    CheckInvariants();
+  }
+
+
+  template <typename T, typename Payload>
+  void LeastRecentlyUsedIndex<T, Payload>::MakeMostRecent(T id, Payload updatedPayload)
+  {
+    if (!Contains(id))
+    {
+      throw OrthancException(ErrorCode_InexistentItem);
+    }
+
+    typename Index::iterator it = index_.find(id);
+    assert(it != index_.end());
+
+    std::pair<T, Payload> item = *(it->second);
+    item.second = updatedPayload;
+    
+    queue_.erase(it->second);
+    queue_.push_front(item);
+    index_[id] = queue_.begin();
+
+    CheckInvariants();
+  }
+
+
+  template <typename T, typename Payload>
   Payload LeastRecentlyUsedIndex<T, Payload>::Invalidate(T id)
   {
     if (!Contains(id))
@@ -254,6 +301,27 @@
 
 
   template <typename T, typename Payload>
+  T LeastRecentlyUsedIndex<T, Payload>::RemoveOldest()
+  {
+    if (IsEmpty())
+    {
+      throw OrthancException(ErrorCode_BadSequenceOfCalls);
+    }
+
+    std::pair<T, Payload> item = queue_.back();
+    T oldest = item.first;
+
+    queue_.pop_back();
+    assert(index_.find(oldest) != index_.end());
+    index_.erase(oldest);
+
+    CheckInvariants();
+
+    return oldest;
+  }
+
+
+  template <typename T, typename Payload>
   const T& LeastRecentlyUsedIndex<T, Payload>::GetOldest() const
   {
     if (IsEmpty())