Debian Codegraph @

Source/Core/Common/RangeSet.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 <utility>
10
11 namespace Common
12 {
13 template <typename T>
14 class RangeSet
15 {
16 private:
17 using MapT = std::map<T, T>;
18
19 public:
20 struct const_iterator
21 {
22 public:
23 const T& from() const { return It->first; }
24
25 const T& to() const { return It->second; }
26
27 std::pair<T, T> operator*() { return {from(), to()}; }
28
29 const_iterator& operator++()
30 {
31 ++It;
32 return *this;
33 }
34
35 const_iterator operator++(int)
36 {
37 const_iterator old = *this;
38 ++It;
39 return old;
40 }
41
42 const_iterator& operator--()
43 {
44 --It;
45 return *this;
46 }
47
48 const_iterator operator--(int)
49 {
50 const_iterator old = *this;
51 --It;
52 return old;
53 }
54
55 bool operator==(const const_iterator& rhs) const { return this->It == rhs.It; }
56
57 bool operator!=(const const_iterator& rhs) const { return !operator==(rhs); }
58
59 private:
60 typename MapT::const_iterator It;
61 const_iterator(typename MapT::const_iterator it) : It(it) {}
62 friend class RangeSet;
63 };
64
65 void insert(T from, T to)
66 {
67 if (from >= to)
68 return;
69
70 // Start by finding the closest range.
71 // upper_bound() returns the closest range whose starting position
72 // is greater than 'from'.
73 auto bound = Map.upper_bound(from);
74 if (bound == Map.end())
75 {
76 // There is no range that starts greater than the given one.
77 // This means we have three options:
78 // - 1. No range exists yet, this is the first range.
79 if (Map.empty())
80 {
81 insert_range(from, to);
82 return;
83 }
84
85 // - 2. The given range does not overlap the last range.
86 --bound;
87 if (from > get_to(bound))
88 {
89 insert_range(from, to);
90 return;
91 }
92
93 // - 3. The given range does overlap the last range.
94 maybe_expand_to(bound, to);
95 return;
96 }
97
98 if (bound == Map.begin())
99 {
100 // The given range starts before any of the existing ones.
101 // We must insert this as a new range even if we potentially overlap
102 // an existing one as we can't modify a key in a std::map.
103 auto inserted = insert_range(from, to);
104 merge_from_iterator_to_value(inserted, bound, to);
105 return;
106 }
107
108 auto abound = bound--;
109
110 // 'bound' now points at the first range in the map that
111 // could possibly be affected.
112
113 // If 'bound' overlaps with given range, update bounds object.
114 if (get_to(bound) >= from)
115 {
116 maybe_expand_to(bound, to);
117 auto inserted = bound;
118 ++bound;
119 merge_from_iterator_to_value(inserted, bound, to);
120 return;
121 }
122
123 // 'bound' *doesn't* overlap with given range, check next range.
124
125 // If this range overlaps with given range,
126 if (get_from(abound) <= to)
127 {
128 // insert new range
129 auto inserted = insert_range(from, to >= get_to(abound) ? to : get_to(abound));
130 // and delete overlaps
131 abound = erase_range(abound);
132 merge_from_iterator_to_value(inserted, abound, to);
133 return;
134 }
135
136 // Otherwise, if we come here, then this new range overlaps nothing
137 // and must be inserted as a new range.
138 insert_range(from, to);
139 }
140
141 void erase(T from, T to)
142 {
143 if (from >= to)
144 return;
145
146 // Like insert(), we use upper_bound to find the closest range.
147 auto bound = Map.upper_bound(from);
148 if (bound == Map.end())
149 {
150 // There is no range that starts greater than the given one.
151 if (Map.empty())
152 {
153 // nothing to do
154 return;
155 }
156 --bound;
157 // 'bound' now points at the last range.
158 if (from >= get_to(bound))
159 {
160 // Given range is larger than any range that exists, nothing to do.
161 return;
162 }
163
164 if (to >= get_to(bound))
165 {
166 if (from == get_from(bound))
167 {
168 // Given range fully overlaps last range, erase it.
169 erase_range(bound);
170 return;
171 }
172 else
173 {
174 // Given range overlaps end of last range, reduce it.
175 reduce_to(bound, from);
176 return;
177 }
178 }
179
180 if (from == get_from(bound))
181 {
182 // Given range overlaps begin of last range, reduce it.
183 reduce_from(bound, to);
184 return;
185 }
186 else
187 {
188 // Given range overlaps middle of last range, bisect it.
189 bisect_range(bound, from, to);
190 return;
191 }
192 }
193
194 if (bound == Map.begin())
195 {
196 // If we found the first range that means 'from' is before any stored range.
197 // This means we can just erase from start until 'to' and be done with it.
198 erase_from_iterator_to_value(bound, to);
199 return;
200 }
201
202 // check previous range
203 auto abound = bound--;
204
205 if (from == get_from(bound))
206 {
207 // Similarly, if the previous range starts with the given one, just erase until 'to'.
208 erase_from_iterator_to_value(bound, to);
209 return;
210 }
211
212 // If we come here, the given range may or may not overlap part of the current 'bound'
213 // (but never the full range), which means we may need to update the end position of it,
214 // or possibly even split it into two.
215 if (from < get_to(bound))
216 {
217 if (to < get_to(bound))
218 {
219 // need to split in two
220 bisect_range(bound, from, to);
221 return;
222 }
223 else
224 {
225 // just update end
226 reduce_to(bound, from);
227 }
228 }
229
230 // and then just erase until 'to'
231 erase_from_iterator_to_value(abound, to);
232 return;
233 }
234
235 const_iterator erase(const_iterator it) { return const_iterator(erase_range(it.It)); }
236
237 void clear() { Map.clear(); }
238
239 bool contains(T value) const
240 {
241 auto it = Map.upper_bound(value);
242 if (it == Map.begin())
243 return false;
244 --it;
245 return get_from(it) <= value && value < get_to(it);
246 }
247
248 bool overlaps(T from, T to) const
249 {
250 if (from >= to)
251 return false;
252
253 auto it = Map.lower_bound(to);
254 if (it == Map.begin())
255 return false;
256 --it;
257 return get_from(it) < to && from < get_to(it);
258 }
259
260 std::size_t size() const { return Map.size(); }
261
262 bool empty() const { return Map.empty(); }
263
264 void swap(RangeSet<T>& other) { Map.swap(other.Map); }
265
266 const_iterator begin() const { return const_iterator(Map.begin()); }
267
268 const_iterator end() const { return const_iterator(Map.end()); }
269
270 const_iterator cbegin() const { return const_iterator(Map.cbegin()); }
271
272 const_iterator cend() const { return const_iterator(Map.cend()); }
273
274 bool operator==(const RangeSet<T>& other) const { return this->Map == other.Map; }
275
276 bool operator!=(const RangeSet<T>& other) const { return !(*this == other); }
277
278 // Get free size and fragmentation ratio
279 std::pair<std::size_t, double> get_stats() const
280 {
281 std::size_t free_total = 0;
282 if (begin() == end())
283 return {free_total, 1.0};
284 std::size_t largest_size = 0;
285 for (auto iter = begin(); iter != end(); ++iter)
286 {
287 const std::size_t size = calc_size(iter.from(), iter.to());
288 if (size > largest_size)
289 largest_size = size;
290 free_total += size;
291 }
292 return {free_total, static_cast<double>(free_total - largest_size) / free_total};
293 }
294
295 private:
296 static std::size_t calc_size(T from, T to)
297 {
298 if constexpr (std::is_pointer_v<T>)
299 {
300 // For pointers we don't want pointer arithmetic here, else void* breaks.
301 return reinterpret_cast<std::size_t>(to) - reinterpret_cast<std::size_t>(from);
302 }
303 else
304 {
305 return static_cast<std::size_t>(to - from);
306 }
307 }
308
309 // Assumptions that can be made about the data:
310 // - Range are stored in the form [from, to[
311 // That is, the starting value is inclusive, and the end value is exclusive.
312 // - 'from' is the map key, 'to' is the map value
313 // - 'from' is always smaller than 'to'
314 // - Stored ranges never touch.
315 // - Stored ranges never overlap.
316 MapT Map;
317
318 T get_from(typename MapT::iterator it) const { return it->first; }
319
320 T get_to(typename MapT::iterator it) const { return it->second; }
321
322 T get_from(typename MapT::const_iterator it) const { return it->first; }
323
324 T get_to(typename MapT::const_iterator it) const { return it->second; }
325
326 typename MapT::iterator insert_range(T from, T to) { return Map.emplace(from, to).first; }
327
328 typename MapT::iterator erase_range(typename MapT::iterator it) { return Map.erase(it); }
329
330 typename MapT::const_iterator erase_range(typename MapT::const_iterator it)
331 {
332 return Map.erase(it);
333 }
334
335 void bisect_range(typename MapT::iterator it, T from, T to)
336 {
337 assert(get_from(it) < from);
338 assert(get_from(it) < to);
339 assert(get_to(it) > from);
340 assert(get_to(it) > to);
341 assert(from < to);
342 T itto = get_to(it);
343 reduce_to(it, from);
344 insert_range(to, itto);
345 }
346
347 typename MapT::iterator reduce_from(typename MapT::iterator it, T from)
348 {
349 assert(get_from(it) < from);
350 T itto = get_to(it);
351 erase_range(it);
352 return insert_range(from, itto);
353 }
354
355 void maybe_expand_to(typename MapT::iterator it, T to)
356 {
357 if (to <= get_to(it))
358 return;
359
360 expand_to(it, to);
361 }
362
363 void expand_to(typename MapT::iterator it, T to)
364 {
365 assert(get_to(it) < to);
366 it->second = to;
367 }
368
369 void reduce_to(typename MapT::iterator it, T to)
370 {
371 assert(get_to(it) > to);
372 it->second = to;
373 }
374
375 void merge_from_iterator_to_value(typename MapT::iterator inserted, typename MapT::iterator bound,
376 T to)
377 {
378 // Erase all ranges that overlap the inserted while updating the upper end.
379 while (bound != Map.end() && get_from(bound) <= to)
380 {
381 maybe_expand_to(inserted, get_to(bound));
382 bound = erase_range(bound);
383 }
384 }
385
386 void erase_from_iterator_to_value(typename MapT::iterator bound, T to)
387 {
388 // Assumption: Given bound starts at or after the 'from' value of the range to erase.
389 while (true)
390 {
391 // Given range starts before stored range.
392 if (to <= get_from(bound))
393 {
394 // Range ends before this range too, nothing to do.
395 return;
396 }
397
398 if (to < get_to(bound))
399 {
400 // Range ends in the middle of current range, reduce current.
401 reduce_from(bound, to);
402 return;
403 }
404
405 if (to == get_to(bound))
406 {
407 // Range ends exactly with current range, erase current.
408 erase_range(bound);
409 return;
410 }
411
412 // Range ends later than current range.
413 // First erase current, then loop to check the range(s) after this one too.
414 bound = erase_range(bound);
415 if (bound == Map.end())
416 {
417 // Unless that was the last range, in which case there's nothing else to do.
418 return;
419 }
420 }
421 }
422 };
423 } // namespace Common