Debian Codegraph @

Source/Core/Common/RangeSizeSet.h

raw symbols SCIP
1 // Copyright 2020 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #pragma once
5
6 #include <cassert>
7 #include <cstddef>
8 #include <map>
9 #include <type_traits>
10 #include <utility>
11
12 namespace Common
13 {
14 // Like RangeSet, but additionally stores a map of the ranges sorted by their size, for quickly
15 // finding the largest or smallest range.
16 template <typename T>
17 class RangeSizeSet
18 {
19 private:
20 // Key type used in the by-size multimap. Should be a type big enough to hold all possible
21 // distances between possible 'from' and 'to'.
22 // I'd actually love to just do
23 // using SizeT = typename std::conditional<std::is_pointer_v<T>,
24 // std::size_t, typename std::make_unsigned<T>::type>::type;
25 // but that's apparently not possible due to the std::make_unsigned<T>::type not existing for
26 // pointer types so we'll work around this...
27 template <typename U, bool IsPointer>
28 struct GetSizeType
29 {
30 using S = typename std::make_unsigned<U>::type;
31 };
32 template <typename U>
33 struct GetSizeType<U, true>
34 {
35 using S = std::size_t;
36 };
37
38 public:
39 using SizeT = typename GetSizeType<T, std::is_pointer_v<T>>::S;
40
41 private:
42 // Value type stored in the regular range map.
43 struct Value
44 {
45 // End point of the range.
46 T To;
47
48 // Pointer to the same range in the by-size multimap.
49 typename std::multimap<SizeT, typename std::map<T, Value>::iterator,
50 std::greater<SizeT>>::iterator SizeIt;
51
52 Value(T to) : To(to) {}
53
54 bool operator==(const Value& other) const { return this->To == other.To; }
55
56 bool operator!=(const Value& other) const { return !operator==(other); }
57 };
58
59 using MapT = std::map<T, Value>;
60 using SizeMapT = std::multimap<SizeT, typename MapT::iterator, std::greater<SizeT>>;
61
62 public:
63 struct by_size_const_iterator;
64
65 struct const_iterator
66 {
67 public:
68 const T& from() const { return It->first; }
69
70 const T& to() const { return It->second.To; }
71
72 std::pair<T, T> operator*() { return {from(), to()}; }
73
74 const_iterator& operator++()
75 {
76 ++It;
77 return *this;
78 }
79
80 const_iterator operator++(int)
81 {
82 const_iterator old = *this;
83 ++It;
84 return old;
85 }
86
87 const_iterator& operator--()
88 {
89 --It;
90 return *this;
91 }
92
93 const_iterator operator--(int)
94 {
95 const_iterator old = *this;
96 --It;
97 return old;
98 }
99
100 bool operator==(const const_iterator& rhs) const { return this->It == rhs.It; }
101
102 bool operator!=(const const_iterator& rhs) const { return !operator==(rhs); }
103
104 by_size_const_iterator to_size_iterator() { return by_size_const_iterator(It->second.SizeIt); }
105
106 private:
107 typename MapT::const_iterator It;
108 const_iterator(typename MapT::const_iterator it) : It(it) {}
109 friend class RangeSizeSet;
110 };
111
112 struct by_size_const_iterator
113 {
114 public:
115 const T& from() const { return It->second->first; }
116
117 const T& to() const { return It->second->second.To; }
118
119 by_size_const_iterator& operator++()
120 {
121 ++It;
122 return *this;
123 }
124
125 by_size_const_iterator operator++(int)
126 {
127 by_size_const_iterator old = *this;
128 ++It;
129 return old;
130 }
131
132 by_size_const_iterator& operator--()
133 {
134 --It;
135 return *this;
136 }
137
138 by_size_const_iterator operator--(int)
139 {
140 by_size_const_iterator old = *this;
141 --It;
142 return old;
143 }
144
145 bool operator==(const by_size_const_iterator& rhs) const { return this->It == rhs.It; }
146
147 bool operator!=(const by_size_const_iterator& rhs) const { return !operator==(rhs); }
148
149 const_iterator to_range_iterator() { return const_iterator(It->second); }
150
151 private:
152 typename SizeMapT::const_iterator It;
153 by_size_const_iterator(typename SizeMapT::const_iterator it) : It(it) {}
154 friend class RangeSizeSet;
155 };
156
157 // We store iterators internally, so disallow copying.
158 RangeSizeSet() = default;
159 RangeSizeSet(const RangeSizeSet<T>&) = delete;
160 RangeSizeSet(RangeSizeSet<T>&&) = default;
161 RangeSizeSet<T>& operator=(const RangeSizeSet<T>&) = delete;
162 RangeSizeSet<T>& operator=(RangeSizeSet<T>&&) = default;
163
164 void insert(T from, T to)
165 {
166 if (from >= to)
167 return;
168
169 // Start by finding the closest range.
170 // upper_bound() returns the closest range whose starting position
171 // is greater than 'from'.
172 auto bound = Map.upper_bound(from);
173 if (bound == Map.end())
174 {
175 // There is no range that starts greater than the given one.
176 // This means we have three options:
177 // - 1. No range exists yet, this is the first range.
178 if (Map.empty())
179 {
180 insert_range(from, to);
181 return;
182 }
183
184 // - 2. The given range does not overlap the last range.
185 --bound;
186 if (from > get_to(bound))
187 {
188 insert_range(from, to);
189 return;
190 }
191
192 // - 3. The given range does overlap the last range.
193 maybe_expand_to(bound, to);
194 return;
195 }
196
197 if (bound == Map.begin())
198 {
199 // The given range starts before any of the existing ones.
200 // We must insert this as a new range even if we potentially overlap
201 // an existing one as we can't modify a key in a std::map.
202 auto inserted = insert_range(from, to);
203 merge_from_iterator_to_value(inserted, bound, to);
204 return;
205 }
206
207 auto abound = bound--;
208
209 // 'bound' now points at the first range in the map that
210 // could possibly be affected.
211
212 // If 'bound' overlaps with given range, update bounds object.
213 if (get_to(bound) >= from)
214 {
215 maybe_expand_to(bound, to);
216 auto inserted = bound;
217 ++bound;
218 merge_from_iterator_to_value(inserted, bound, to);
219 return;
220 }
221
222 // 'bound' *doesn't* overlap with given range, check next range.
223
224 // If this range overlaps with given range,
225 if (get_from(abound) <= to)
226 {
227 // insert new range
228 auto inserted = insert_range(from, to >= get_to(abound) ? to : get_to(abound));
229 // and delete overlaps
230 abound = erase_range(abound);
231 merge_from_iterator_to_value(inserted, abound, to);
232 return;
233 }
234
235 // Otherwise, if we come here, then this new range overlaps nothing
236 // and must be inserted as a new range.
237 insert_range(from, to);
238 }
239
240 void erase(T from, T to)
241 {
242 if (from >= to)
243 return;
244
245 // Like insert(), we use upper_bound to find the closest range.
246 auto bound = Map.upper_bound(from);
247 if (bound == Map.end())
248 {
249 // There is no range that starts greater than the given one.
250 if (Map.empty())
251 {
252 // nothing to do
253 return;
254 }
255 --bound;
256 // 'bound' now points at the last range.
257 if (from >= get_to(bound))
258 {
259 // Given range is larger than any range that exists, nothing to do.
260 return;
261 }
262
263 if (to >= get_to(bound))
264 {
265 if (from == get_from(bound))
266 {
267 // Given range fully overlaps last range, erase it.
268 erase_range(bound);
269 return;
270 }
271 else
272 {
273 // Given range overlaps end of last range, reduce it.
274 reduce_to(bound, from);
275 return;
276 }
277 }
278
279 if (from == get_from(bound))
280 {
281 // Given range overlaps begin of last range, reduce it.
282 reduce_from(bound, to);
283 return;
284 }
285 else
286 {
287 // Given range overlaps middle of last range, bisect it.
288 bisect_range(bound, from, to);
289 return;
290 }
291 }
292
293 if (bound == Map.begin())
294 {
295 // If we found the first range that means 'from' is before any stored range.
296 // This means we can just erase from start until 'to' and be done with it.
297 erase_from_iterator_to_value(bound, to);
298 return;
299 }
300
301 // check previous range
302 auto abound = bound--;
303
304 if (from == get_from(bound))
305 {
306 // Similarly, if the previous range starts with the given one, just erase until 'to'.
307 erase_from_iterator_to_value(bound, to);
308 return;
309 }
310
311 // If we come here, the given range may or may not overlap part of the current 'bound'
312 // (but never the full range), which means we may need to update the end position of it,
313 // or possibly even split it into two.
314 if (from < get_to(bound))
315 {
316 if (to < get_to(bound))
317 {
318 // need to split in two
319 bisect_range(bound, from, to);
320 return;
321 }
322 else
323 {
324 // just update end
325 reduce_to(bound, from);
326 }
327 }
328
329 // and then just erase until 'to'
330 erase_from_iterator_to_value(abound, to);
331 return;
332 }
333
334 const_iterator erase(const_iterator it) { return const_iterator(erase_range(it.It)); }
335
336 by_size_const_iterator erase(by_size_const_iterator it)
337 {
338 return by_size_const_iterator(erase_range_by_size(it.It));
339 }
340
341 void clear()
342 {
343 Map.clear();
344 Sizes.clear();
345 }
346
347 bool contains(T value) const
348 {
349 auto it = Map.upper_bound(value);
350 if (it == Map.begin())
351 return false;
352 --it;
353 return get_from(it) <= value && value < get_to(it);
354 }
355
356 bool overlaps(T from, T to) const
357 {
358 if (from >= to)
359 return false;
360
361 auto it = Map.lower_bound(to);
362 if (it == Map.begin())
363 return false;
364 --it;
365 return get_from(it) < to && from < get_to(it);
366 }
367
368 std::size_t size() const { return Map.size(); }
369
370 bool empty() const { return Map.empty(); }
371
372 std::size_t by_size_count(const SizeT& key) const { return Sizes.count(key); }
373
374 by_size_const_iterator by_size_find(const SizeT& key) const { return Sizes.find(key); }
375
376 std::pair<by_size_const_iterator, by_size_const_iterator>
377 by_size_equal_range(const SizeT& key) const
378 {
379 auto p = Sizes.equal_range(key);
380 return std::pair<by_size_const_iterator, by_size_const_iterator>(
381 by_size_const_iterator(p.first), by_size_const_iterator(p.second));
382 }
383
384 by_size_const_iterator by_size_lower_bound(const SizeT& key) const
385 {
386 return Sizes.lower_bound(key);
387 }
388
389 by_size_const_iterator by_size_upper_bound(const SizeT& key) const
390 {
391 return Sizes.upper_bound(key);
392 }
393
394 void swap(RangeSizeSet<T>& other)
395 {
396 Map.swap(other.Map);
397 Sizes.swap(other.Sizes);
398 }
399
400 const_iterator begin() const { return const_iterator(Map.begin()); }
401
402 const_iterator end() const { return const_iterator(Map.end()); }
403
404 const_iterator cbegin() const { return const_iterator(Map.cbegin()); }
405
406 const_iterator cend() const { return const_iterator(Map.cend()); }
407
408 by_size_const_iterator by_size_begin() const { return by_size_const_iterator(Sizes.begin()); }
409
410 by_size_const_iterator by_size_end() const { return by_size_const_iterator(Sizes.end()); }
411
412 by_size_const_iterator by_size_cbegin() const { return by_size_const_iterator(Sizes.cbegin()); }
413
414 by_size_const_iterator by_size_cend() const { return by_size_const_iterator(Sizes.cend()); }
415
416 bool operator==(const RangeSizeSet<T>& other) const { return this->Map == other.Map; }
417
418 bool operator!=(const RangeSizeSet<T>& other) const { return !(*this == other); }
419
420 // Get free size and fragmentation ratio
421 std::pair<std::size_t, double> get_stats() const
422 {
423 std::size_t free_total = 0;
424 if (begin() == end())
425 return {free_total, 1.0};
426 for (auto iter = begin(); iter != end(); ++iter)
427 free_total += calc_size(iter.from(), iter.to());
428 return {free_total, static_cast<double>(free_total - Sizes.begin()->first) / free_total};
429 }
430
431 private:
432 static SizeT calc_size(T from, T to)
433 {
434 if constexpr (std::is_pointer_v<T>)
435 {
436 // For pointers we don't want pointer arithmetic here, else void* breaks.
437 static_assert(sizeof(T) <= sizeof(SizeT));
438 return reinterpret_cast<SizeT>(to) - reinterpret_cast<SizeT>(from);
439 }
440 else
441 {
442 return static_cast<SizeT>(to - from);
443 }
444 }
445
446 // Assumptions that can be made about the data:
447 // - Range are stored in the form [from, to[
448 // That is, the starting value is inclusive, and the end value is exclusive.
449 // - 'from' is the map key, 'to' is the map value
450 // - 'from' is always smaller than 'to'
451 // - Stored ranges never touch.
452 // - Stored ranges never overlap.
453 MapT Map;
454
455 // The by-size multimap.
456 // Key is the size of the range.
457 // Value is a pointer to the range in the regular range map.
458 // We use std::greater so that Sizes.begin() gives us the largest range.
459 SizeMapT Sizes;
460
461 T get_from(typename MapT::iterator it) const { return it->first; }
462
463 T get_to(typename MapT::iterator it) const { return it->second.To; }
464
465 T get_from(typename MapT::const_iterator it) const { return it->first; }
466
467 T get_to(typename MapT::const_iterator it) const { return it->second.To; }
468
469 typename MapT::iterator insert_range(T from, T to)
470 {
471 auto m = Map.emplace(from, to).first;
472 m->second.SizeIt = Sizes.emplace(calc_size(from, to), m);
473 return m;
474 }
475
476 typename MapT::iterator erase_range(typename MapT::iterator it)
477 {
478 Sizes.erase(it->second.SizeIt);
479 return Map.erase(it);
480 }
481
482 typename MapT::const_iterator erase_range(typename MapT::const_iterator it)
483 {
484 Sizes.erase(it->second.SizeIt);
485 return Map.erase(it);
486 }
487
488 typename SizeMapT::const_iterator erase_range_by_size(typename SizeMapT::const_iterator it)
489 {
490 Map.erase(it->second);
491 return Sizes.erase(it);
492 }
493
494 void bisect_range(typename MapT::iterator it, T from, T to)
495 {
496 assert(get_from(it) < from);
497 assert(get_from(it) < to);
498 assert(get_to(it) > from);
499 assert(get_to(it) > to);
500 assert(from < to);
501 T itto = get_to(it);
502 reduce_to(it, from);
503 insert_range(to, itto);
504 }
505
506 typename MapT::iterator reduce_from(typename MapT::iterator it, T from)
507 {
508 assert(get_from(it) < from);
509 T itto = get_to(it);
510 erase_range(it);
511 return insert_range(from, itto);
512 }
513
514 void maybe_expand_to(typename MapT::iterator it, T to)
515 {
516 if (to <= get_to(it))
517 return;
518
519 expand_to(it, to);
520 }
521
522 void expand_to(typename MapT::iterator it, T to)
523 {
524 assert(get_to(it) < to);
525 it->second.To = to;
526 Sizes.erase(it->second.SizeIt);
527 it->second.SizeIt = Sizes.emplace(calc_size(get_from(it), to), it);
528 }
529
530 void reduce_to(typename MapT::iterator it, T to)
531 {
532 assert(get_to(it) > to);
533 it->second.To = to;
534 Sizes.erase(it->second.SizeIt);
535 it->second.SizeIt = Sizes.emplace(calc_size(get_from(it), to), it);
536 }
537
538 void merge_from_iterator_to_value(typename MapT::iterator inserted, typename MapT::iterator bound,
539 T to)
540 {
541 // Erase all ranges that overlap the inserted while updating the upper end.
542 while (bound != Map.end() && get_from(bound) <= to)
543 {
544 maybe_expand_to(inserted, get_to(bound));
545 bound = erase_range(bound);
546 }
547 }
548
549 void erase_from_iterator_to_value(typename MapT::iterator bound, T to)
550 {
551 // Assumption: Given bound starts at or after the 'from' value of the range to erase.
552 while (true)
553 {
554 // Given range starts before stored range.
555 if (to <= get_from(bound))
556 {
557 // Range ends before this range too, nothing to do.
558 return;
559 }
560
561 if (to < get_to(bound))
562 {
563 // Range ends in the middle of current range, reduce current.
564 reduce_from(bound, to);
565 return;
566 }
567
568 if (to == get_to(bound))
569 {
570 // Range ends exactly with current range, erase current.
571 erase_range(bound);
572 return;
573 }
574
575 // Range ends later than current range.
576 // First erase current, then loop to check the range(s) after this one too.
577 bound = erase_range(bound);
578 if (bound == Map.end())
579 {
580 // Unless that was the last range, in which case there's nothing else to do.
581 return;
582 }
583 }
584 }
585 };
586 } // namespace Common