mCRL2
Loading...
Searching...
No Matches
cache_policy.h
Go to the documentation of this file.
1// Author(s): Maurice Laveaux
2// Copyright: see the accompanying file COPYING or copy at
3// https://github.com/mCRL2org/mCRL2/blob/master/COPYING
4//
5// Distributed under the Boost Software License, Version 1.0.
6// (See accompanying file LICENSE_1_0.txt or copy at
7// http://www.boost.org/LICENSE_1_0.txt)
8//
9
10#ifndef MCRL2_UTILITIES_CACHE_POLICY_H
11#define MCRL2_UTILITIES_CACHE_POLICY_H
12
13#include <forward_list>
14
15#include <cassert>
16
17namespace mcrl2
18{
19namespace utilities
20{
21
23template<typename Map>
25{
26public:
27 using key_type = typename Map::key_type;
28 using map_type = Map;
29
30protected:
32 virtual void clear() = 0;
33
35 virtual void inserted(const key_type& key) = 0;
36
38 virtual typename Map::iterator replacement_candidate(Map& map) = 0;
39
41 virtual void touch(const key_type& key) = 0;
42};
43
45template<typename Map>
46class no_policy final : public replacement_policy<Map>
47{
48public:
49 using key_type = typename Map::key_type;
50
51 typename Map::iterator replacement_candidate(Map& map) override { return map.begin(); }
52
53 // Not implemented.
54 void clear() override {}
55 void inserted(const key_type&) override {}
56 void touch(const key_type&) override {}
57};
58
59template<typename Map>
60class fifo_policy final : public replacement_policy<Map>
61{
62public:
63 using key_type = typename Map::key_type;
64
66 {
67 m_last_element_it = m_queue.before_begin();
68 }
69
71 : m_queue(other.m_queue)
72 {
74 }
75
77 {
78 m_queue = other.m_queue;
80 return *this;
81 }
82
83 // These moves work when moving m_queue guarantees that m_last_element_it remains valid.
84 fifo_policy(fifo_policy&& other) noexcept = default;
85 fifo_policy& operator=(fifo_policy&& other) noexcept = default;
86
87 void clear() override
88 {
89 m_queue.clear();
91 }
92
93 typename Map::iterator replacement_candidate(Map& map) override
94 {
95 assert(!m_queue.empty());
96 // Remove the first key (the first one to be inserted into the queue).
97 auto it = map.find(m_queue.front());
98 m_queue.erase_after(m_queue.before_begin());
99 assert(it != map.end());
100 return it;
101 }
102
103 void inserted(const key_type& key) override
104 {
105 // A new key was inserted, so it must be the last one to be removed.
106 m_last_element_it = m_queue.insert_after(m_last_element_it, key);
107 }
108
109 void touch(const key_type&) override
110 {}
111
112private:
114 {
115 // Update the iterator to the last element of the queue.
116 for (auto it = m_queue.before_begin(); it != m_queue.end(); ++it)
117 {
119 }
120 }
121
122 std::forward_list<key_type> m_queue;
123 typename std::forward_list<key_type>::iterator m_last_element_it;
124};
125
126} // namespace utilities
127} // namespace mcrl2
128
129#endif // MCRL2_UTILITIES_CACHE_POLICY_H
fifo_policy & operator=(const fifo_policy &other)
Map::iterator replacement_candidate(Map &map) override
void touch(const key_type &) override
Called whenever an element was found in the cache.
fifo_policy & operator=(fifo_policy &&other) noexcept=default
std::forward_list< key_type > m_queue
typename Map::key_type key_type
void inserted(const key_type &key) override
Called whenever a new element has been inserted into the cache.
std::forward_list< key_type >::iterator m_last_element_it
fifo_policy(fifo_policy &&other) noexcept=default
void clear() override
Called whenever the underlying cache is cleared.
fifo_policy(const fifo_policy &other)
A policy that replaces an arbitrary (but not random) element.
void inserted(const key_type &) override
Called whenever a new element has been inserted into the cache.
void clear() override
Called whenever the underlying cache is cleared.
typename Map::key_type key_type
void touch(const key_type &) override
Called whenever an element was found in the cache.
Map::iterator replacement_candidate(Map &map) override
An interface to implement a replacement policy for the fixed_size_cache.
virtual void clear()=0
Called whenever the underlying cache is cleared.
typename Map::key_type key_type
virtual void inserted(const key_type &key)=0
Called whenever a new element has been inserted into the cache.
virtual Map::iterator replacement_candidate(Map &map)=0
virtual void touch(const key_type &key)=0
Called whenever an element was found in the cache.
A class that takes a linear process specification and checks all tau-summands of that LPS for conflue...
Definition indexed_set.h:72