MCPcopy Create free account
hub / github.com/comaps/comaps / KeyAge

Class KeyAge

libs/base/lru_cache.hpp:77–169  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

75 /// all keys of |m_ageToKey| should be values of |m_ageToKey|.
76 /// \note Ages should be unique for all keys.
77 class KeyAge
78 {
79 template <typename K, typename V>
80 friend class LruCacheKeyAgeTest;
81
82 public:
83 void Clear()
84 {
85 m_age = 0;
86 m_ageToKey.clear();
87 m_keyToAge.clear();
88 }
89
90 /// \brief Increments |m_age| and insert key to |m_ageToKey| and |m_keyToAge|.
91 /// \note This method should be used only if there's no |key| in |m_ageToKey| and |m_keyToAge|.
92 void InsertKey(Key const & key)
93 {
94 ++m_age;
95 m_ageToKey[m_age] = key;
96 m_keyToAge[key] = m_age;
97 }
98
99 /// \brief Increments |m_age| and updates key age in |m_ageToKey| and |m_keyToAge|.
100 /// \note This method should be used only if there's |key| in |m_ageToKey| and |m_keyToAge|.
101 void UpdateAge(Key const & key)
102 {
103 ++m_age;
104 auto keyToAgeIt = m_keyToAge.find(key);
105 CHECK(keyToAgeIt != m_keyToAge.end(), ());
106 // Removing former age.
107 size_t const removed = m_ageToKey.erase(keyToAgeIt->second);
108 CHECK_EQUAL(removed, 1, ());
109 // Putting new age.
110 m_ageToKey[m_age] = key;
111 keyToAgeIt->second = m_age;
112 }
113
114 /// \returns Least recently used key without updating the age.
115 /// \note |m_ageToKey| and |m_keyToAge| shouldn't be empty.
116 Key const & GetLruKey() const
117 {
118 CHECK(!m_ageToKey.empty(), ());
119 // The smaller age the older item.
120 return m_ageToKey.cbegin()->second;
121 }
122
123 void RemoveLru()
124 {
125 Key const & lru = GetLruKey();
126 size_t const removed = m_keyToAge.erase(lru);
127 CHECK_EQUAL(removed, 1, ());
128 m_ageToKey.erase(m_ageToKey.begin());
129 }
130
131 /// \brief Checks for coherence class params.
132 /// \note It's a time consumption method and should be called for tests only.
133 bool IsValidForTesting() const
134 {

Callers

nothing calls this directly

Calls 2

emptyMethod · 0.45
cbeginMethod · 0.45

Tested by

no test coverage detected