16
16
* You should have received a copy of the GNU General Public License
17
17
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18
18
*/
19
- #include < optional>
20
- #include < boost/unordered/unordered_flat_map.hpp>
21
19
22
20
template <template <typename ...> class MementoMap , typename ... Args>
23
21
class Memento final
@@ -36,23 +34,24 @@ class Memento final
36
34
};
37
35
38
36
MementoMap<uint32_t , Entry> m_table;
37
+ uint32_t m_table_size;
39
38
40
39
public:
41
- Memento ();
40
+ Memento () {}
42
41
43
42
/* *
44
43
* Returns the size of the replacement set.
45
44
*
46
45
* @return the size of the replacement set
47
46
*/
48
- int32_t size () const noexcept ;
47
+ int32_t size () const noexcept { return m_table_size; }
49
48
50
49
/* *
51
50
* Returns {@code true} if the replacement set is empty.
52
51
*
53
52
* @return {@code true} if empty, {@code false} otherwise
54
53
*/
55
- bool isEmpty () const noexcept ;
54
+ bool isEmpty () const noexcept { return m_table_size == 0 ; }
56
55
57
56
/* *
58
57
* Remembers that the given bucket has been removed
@@ -67,7 +66,11 @@ class Memento final
67
66
* @param prevRemoved the previous removed bucket
68
67
* @return the value of the new last removed bucket
69
68
*/
70
- int32_t remember (uint32_t bucket, uint32_t replacer, uint32_t prevRemoved ) noexcept ;
69
+ int32_t remember (uint32_t bucket, uint32_t replacer, uint32_t prevRemoved ) noexcept {
70
+ m_table.emplace (bucket, Entry{replacer, prevRemoved});
71
+ ++m_table_size;
72
+ return bucket;
73
+ }
71
74
72
75
/* *
73
76
* Restores the given bucket by removing it
@@ -79,7 +82,15 @@ class Memento final
79
82
* @param bucket the bucket to restore
80
83
* @return the new last removed bucket
81
84
*/
82
- int32_t restore (uint32_t bucket) noexcept ;
85
+ int32_t restore (uint32_t bucket) noexcept {
86
+ if (m_table_size == 0 ) {
87
+ return bucket + 1 ;
88
+ }
89
+ auto e = m_table.find (bucket);
90
+ m_table.erase (e);
91
+ --m_table_size;
92
+ return e->second .prevRemoved ;
93
+ }
83
94
84
95
/* *
85
96
* Returns the replacer of the bucket if it
@@ -93,87 +104,16 @@ class Memento final
93
104
* @param bucket the bucket to search for
94
105
* @return the replacing bucket if any, {@code std::nullopt} otherwise
95
106
*/
96
- std::optional<int32_t > replacer (int32_t bucket ) const noexcept ;
107
+ int32_t replacer (int32_t bucket ) const noexcept {
108
+ if (m_table_size == 0 ) {
109
+ return -1 ;
110
+ }
111
+ auto e = m_table.find (bucket);
112
+ if (e != m_table.end ()) {
113
+ return e->second .replacer ;
114
+ } else {
115
+ return -1 ;
116
+ }
117
+ }
97
118
};
98
-
99
- template <template <typename ...> class MementoMap , typename ... Args>
100
- Memento<MementoMap,Args...>::Memento() {}
101
-
102
- /* *
103
- * Returns the size of the replacement set.
104
- *
105
- * @return the size of the replacement set
106
- */
107
- template <template <typename ...> class MementoMap , typename ... Args>
108
- inline int32_t Memento<MementoMap,Args...>::size() const noexcept { return m_table.size (); }
109
-
110
- /* *
111
- * Returns {@code true} if the replacement set is empty.
112
- *
113
- * @return {@code true} if empty, {@code false} otherwise
114
- */
115
- template <template <typename ...> class MementoMap , typename ... Args>
116
- inline bool Memento<MementoMap,Args...>::isEmpty() const noexcept { return m_table.empty (); }
117
-
118
- /* *
119
- * Remembers that the given bucket has been removed
120
- * and that was replaced by the given replacer.
121
- * <p>
122
- * This method also stores the last removed bucket
123
- * (before the current one) to create the sequence
124
- * of removals.
125
- *
126
- * @param bucket the removed bucket
127
- * @param replacer the replacing bucket
128
- * @param prevRemoved the previous removed bucket
129
- * @return the value of the new last removed bucket
130
- */
131
- template <template <typename ...> class MementoMap , typename ... Args>
132
- inline int32_t Memento<MementoMap,Args...>::remember(uint32_t bucket, uint32_t replacer,
133
- uint32_t prevRemoved) noexcept {
134
- m_table.emplace (bucket, Entry{replacer, prevRemoved});
135
- return bucket;
136
- }
137
-
138
- /* *
139
- * Restores the given bucket by removing it
140
- * from the memory.
141
- * <p>
142
- * If the memory is empty the last removed bucket
143
- * becomes the given bucket + 1.
144
- *
145
- * @param bucket the bucket to restore
146
- * @return the new last removed bucket
147
- */
148
- template <template <typename ...> class MementoMap , typename ... Args>
149
- inline int32_t Memento<MementoMap,Args...>::restore(uint32_t bucket) noexcept {
150
- if (m_table.empty ()) {
151
- return bucket + 1 ;
152
- }
153
- auto e = m_table.find (bucket);
154
- m_table.erase (e);
155
- return e->second .prevRemoved ;
156
- }
157
-
158
- /* *
159
- * Returns the replacer of the bucket if it
160
- * was removed, otherwise returns {@code -1}.
161
- * <p>
162
- * The value returned by this method represents
163
- * both the bucket that replaced the given one
164
- * and the size of the working set after removing
165
- * the given bucket.
166
- *
167
- * @param bucket the bucket to search for
168
- * @return the replacing bucket if any, {@code std::nullopt} otherwise
169
- */
170
- template <template <typename ...> class MementoMap , typename ... Args>
171
- inline std::optional<int32_t > Memento<MementoMap,Args...>::replacer(int32_t bucket) const noexcept {
172
- auto e = m_table.find (bucket);
173
- if (e != m_table.end ()) {
174
- return e->second .replacer ;
175
- } else {
176
- return std::nullopt ;
177
- }
178
- }
179
119
#endif // MEMENTO_H
0 commit comments