1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
// Copyright (c) 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef SOURCE_UTIL_MOVE_TO_FRONT_H_
#define SOURCE_UTIL_MOVE_TO_FRONT_H_
#include <cassert>
#include <cstdint>
#include <map>
#include <set>
#include <unordered_map>
#include <vector>
namespace spvtools {
namespace utils {
// Log(n) move-to-front implementation. Implements the following functions:
// Insert - pushes value to the front of the mtf sequence
// (only unique values allowed).
// Remove - remove value from the sequence.
// ValueFromRank - access value by its 1-indexed rank in the sequence.
// RankFromValue - get the rank of the given value in the sequence.
// Accessing a value with ValueFromRank or RankFromValue moves the value to the
// front of the sequence (rank of 1).
//
// The implementation is based on an AVL-based order statistic tree. The tree
// is ordered by timestamps issued when values are inserted or accessed (recent
// values go to the left side of the tree, old values are gradually rotated to
// the right side).
//
// Terminology
// rank: 1-indexed rank showing how recently the value was inserted or accessed.
// node: handle used internally to access node data.
// size: size of the subtree of a node (including the node).
// height: distance from a node to the farthest leaf.
class MoveToFront {
public:
explicit MoveToFront(size_t reserve_capacity = 4) {
nodes_.reserve(reserve_capacity);
// Create NIL node.
nodes_.emplace_back(Node());
}
virtual ~MoveToFront() = default;
// Inserts value in the move-to-front sequence. Does nothing if the value is
// already in the sequence. Returns true if insertion was successful.
// The inserted value is placed at the front of the sequence (rank 1).
bool Insert(uint32_t value);
// Removes value from move-to-front sequence. Returns false iff the value
// was not found.
bool Remove(uint32_t value);
// Computes 1-indexed rank of value in the move-to-front sequence and moves
// the value to the front. Example:
// Before the call: 4 8 2 1 7
// RankFromValue(8) returns 2
// After the call: 8 4 2 1 7
// Returns true iff the value was found in the sequence.
bool RankFromValue(uint32_t value, uint32_t* rank);
// Returns value corresponding to a 1-indexed rank in the move-to-front
// sequence and moves the value to the front. Example:
// Before the call: 4 8 2 1 7
// ValueFromRank(2) returns 8
// After the call: 8 4 2 1 7
// Returns true iff the rank is within bounds [1, GetSize()].
bool ValueFromRank(uint32_t rank, uint32_t* value);
// Moves the value to the front of the sequence.
// Returns false iff value is not in the sequence.
bool Promote(uint32_t value);
// Returns true iff the move-to-front sequence contains the value.
bool HasValue(uint32_t value) const;
// Returns the number of elements in the move-to-front sequence.
uint32_t GetSize() const { return SizeOf(root_); }
protected:
// Internal tree data structure uses handles instead of pointers. Leaves and
// root parent reference a singleton under handle 0. Although dereferencing
// a null pointer is not possible, inappropriate access to handle 0 would
// cause an assertion. Handles are not garbage collected if value was
// deprecated
// with DeprecateValue(). But handles are recycled when a node is
// repositioned.
// Internal tree data structure node.
struct Node {
// Timestamp from a logical clock which updates every time the element is
// accessed through ValueFromRank or RankFromValue.
uint32_t timestamp = 0;
// The size of the node's subtree, including the node.
// SizeOf(LeftOf(node)) + SizeOf(RightOf(node)) + 1.
uint32_t size = 0;
// Handles to connected nodes.
uint32_t left = 0;
uint32_t right = 0;
uint32_t parent = 0;
// Distance to the farthest leaf.
// Leaves have height 0, real nodes at least 1.
uint32_t height = 0;
// Stored value.
uint32_t value = 0;
};
// Creates node and sets correct values. Non-NIL nodes should be created only
// through this function. If the node with this value has been created
// previously
// and since orphaned, reuses the old node instead of creating a new one.
uint32_t CreateNode(uint32_t timestamp, uint32_t value);
// Node accessor methods. Naming is designed to be similar to natural
// language as these functions tend to be used in sequences, for example:
// ParentOf(LeftestDescendentOf(RightOf(node)))
// Returns value of the node referenced by |handle|.
uint32_t ValueOf(uint32_t node) const { return nodes_.at(node).value; }
// Returns left child of |node|.
uint32_t LeftOf(uint32_t node) const { return nodes_.at(node).left; }
// Returns right child of |node|.
uint32_t RightOf(uint32_t node) const { return nodes_.at(node).right; }
// Returns parent of |node|.
uint32_t ParentOf(uint32_t node) const { return nodes_.at(node).parent; }
// Returns timestamp of |node|.
uint32_t TimestampOf(uint32_t node) const {
assert(node);
return nodes_.at(node).timestamp;
}
// Returns size of |node|.
uint32_t SizeOf(uint32_t node) const { return nodes_.at(node).size; }
// Returns height of |node|.
uint32_t HeightOf(uint32_t node) const { return nodes_.at(node).height; }
// Returns mutable reference to value of |node|.
uint32_t& MutableValueOf(uint32_t node) {
assert(node);
return nodes_.at(node).value;
}
// Returns mutable reference to handle of left child of |node|.
uint32_t& MutableLeftOf(uint32_t node) {
assert(node);
return nodes_.at(node).left;
}
// Returns mutable reference to handle of right child of |node|.
uint32_t& MutableRightOf(uint32_t node) {
assert(node);
return nodes_.at(node).right;
}
// Returns mutable reference to handle of parent of |node|.
uint32_t& MutableParentOf(uint32_t node) {
assert(node);
return nodes_.at(node).parent;
}
// Returns mutable reference to timestamp of |node|.
uint32_t& MutableTimestampOf(uint32_t node) {
assert(node);
return nodes_.at(node).timestamp;
}
// Returns mutable reference to size of |node|.
uint32_t& MutableSizeOf(uint32_t node) {
assert(node);
return nodes_.at(node).size;
}
// Returns mutable reference to height of |node|.
uint32_t& MutableHeightOf(uint32_t node) {
assert(node);
return nodes_.at(node).height;
}
// Returns true iff |node| is left child of its parent.
bool IsLeftChild(uint32_t node) const {
assert(node);
return LeftOf(ParentOf(node)) == node;
}
// Returns true iff |node| is right child of its parent.
bool IsRightChild(uint32_t node) const {
assert(node);
return RightOf(ParentOf(node)) == node;
}
// Returns true iff |node| has no relatives.
bool IsOrphan(uint32_t node) const {
assert(node);
return !ParentOf(node) && !LeftOf(node) && !RightOf(node);
}
// Returns true iff |node| is in the tree.
bool IsInTree(uint32_t node) const {
assert(node);
return node == root_ || !IsOrphan(node);
}
// Returns the height difference between right and left subtrees.
int BalanceOf(uint32_t node) const {
return int(HeightOf(RightOf(node))) - int(HeightOf(LeftOf(node)));
}
// Updates size and height of the node, assuming that the children have
// correct values.
void UpdateNode(uint32_t node);
// Returns the most LeftOf(LeftOf(... descendent which is not leaf.
uint32_t LeftestDescendantOf(uint32_t node) const {
uint32_t parent = 0;
while (node) {
parent = node;
node = LeftOf(node);
}
return parent;
}
// Returns the most RightOf(RightOf(... descendent which is not leaf.
uint32_t RightestDescendantOf(uint32_t node) const {
uint32_t parent = 0;
while (node) {
parent = node;
node = RightOf(node);
}
return parent;
}
// Inserts node in the tree. The node must be an orphan.
void InsertNode(uint32_t node);
// Removes node from the tree. May change value_to_node_ if removal uses a
// scapegoat. Returns the removed (orphaned) handle for recycling. The
// returned handle may not be equal to |node| if scapegoat was used.
uint32_t RemoveNode(uint32_t node);
// Rotates |node| left, reassigns all connections and returns the node
// which takes place of the |node|.
uint32_t RotateLeft(const uint32_t node);
// Rotates |node| right, reassigns all connections and returns the node
// which takes place of the |node|.
uint32_t RotateRight(const uint32_t node);
// Root node handle. The tree is empty if root_ is 0.
uint32_t root_ = 0;
// Incremented counters for next timestamp and value.
uint32_t next_timestamp_ = 1;
// Holds all tree nodes. Indices of this vector are node handles.
std::vector<Node> nodes_;
// Maps ids to node handles.
std::unordered_map<uint32_t, uint32_t> value_to_node_;
// Cache for the last accessed value in the sequence.
uint32_t last_accessed_value_ = 0;
bool last_accessed_value_valid_ = false;
};
class MultiMoveToFront {
public:
// Inserts |value| to sequence with handle |mtf|.
// Returns false if |mtf| already has |value|.
bool Insert(uint64_t mtf, uint32_t value) {
if (GetMtf(mtf).Insert(value)) {
val_to_mtfs_[value].insert(mtf);
return true;
}
return false;
}
// Removes |value| from sequence with handle |mtf|.
// Returns false if |mtf| doesn't have |value|.
bool Remove(uint64_t mtf, uint32_t value) {
if (GetMtf(mtf).Remove(value)) {
val_to_mtfs_[value].erase(mtf);
return true;
}
assert(val_to_mtfs_[value].count(mtf) == 0);
return false;
}
// Removes |value| from all sequences which have it.
void RemoveFromAll(uint32_t value) {
auto it = val_to_mtfs_.find(value);
if (it == val_to_mtfs_.end()) return;
auto& mtfs_containing_value = it->second;
for (uint64_t mtf : mtfs_containing_value) {
GetMtf(mtf).Remove(value);
}
val_to_mtfs_.erase(value);
}
// Computes rank of |value| in sequence |mtf|.
// Returns false if |mtf| doesn't have |value|.
bool RankFromValue(uint64_t mtf, uint32_t value, uint32_t* rank) {
return GetMtf(mtf).RankFromValue(value, rank);
}
// Finds |value| with |rank| in sequence |mtf|.
// Returns false if |rank| is out of bounds.
bool ValueFromRank(uint64_t mtf, uint32_t rank, uint32_t* value) {
return GetMtf(mtf).ValueFromRank(rank, value);
}
// Returns size of |mtf| sequence.
uint32_t GetSize(uint64_t mtf) { return GetMtf(mtf).GetSize(); }
// Promotes |value| in all sequences which have it.
void Promote(uint32_t value) {
const auto it = val_to_mtfs_.find(value);
if (it == val_to_mtfs_.end()) return;
const auto& mtfs_containing_value = it->second;
for (uint64_t mtf : mtfs_containing_value) {
GetMtf(mtf).Promote(value);
}
}
// Inserts |value| in sequence |mtf| or promotes if it's already there.
void InsertOrPromote(uint64_t mtf, uint32_t value) {
if (!Insert(mtf, value)) {
GetMtf(mtf).Promote(value);
}
}
// Returns if |mtf| sequence has |value|.
bool HasValue(uint64_t mtf, uint32_t value) {
return GetMtf(mtf).HasValue(value);
}
private:
// Returns actual MoveToFront object corresponding to |handle|.
// As multiple operations are often performed consecutively for the same
// sequence, the last returned value is cached.
MoveToFront& GetMtf(uint64_t handle) {
if (!cached_mtf_ || cached_handle_ != handle) {
cached_handle_ = handle;
cached_mtf_ = &mtfs_[handle];
}
return *cached_mtf_;
}
// Container holding MoveToFront objects. Map key is sequence handle.
std::map<uint64_t, MoveToFront> mtfs_;
// Container mapping value to sequences which contain that value.
std::unordered_map<uint32_t, std::set<uint64_t>> val_to_mtfs_;
// Cache for the last accessed sequence.
uint64_t cached_handle_ = 0;
MoveToFront* cached_mtf_ = nullptr;
};
} // namespace utils
} // namespace spvtools
#endif // SOURCE_UTIL_MOVE_TO_FRONT_H_
|