| 1 |
|
| 2 |
|
| 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 |
|
| 71 |
|
| 72 |
|
| 73 |
auto bound = Map.upper_bound(from); |
| 74 |
if (bound == Map.end()) |
| 75 |
{ |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
if (Map.empty()) |
| 80 |
{ |
| 81 |
insert_range(from, to); |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
--bound; |
| 87 |
if (from > get_to(bound)) |
| 88 |
{ |
| 89 |
insert_range(from, to); |
| 90 |
return; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
maybe_expand_to(bound, to); |
| 95 |
return; |
| 96 |
} |
| 97 |
|
| 98 |
if (bound == Map.begin()) |
| 99 |
{ |
| 100 |
|
| 101 |
|
| 102 |
|
| 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 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 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 |
|
| 124 |
|
| 125 |
|
| 126 |
if (get_from(abound) <= to) |
| 127 |
{ |
| 128 |
|
| 129 |
auto inserted = insert_range(from, to >= get_to(abound) ? to : get_to(abound)); |
| 130 |
|
| 131 |
abound = erase_range(abound); |
| 132 |
merge_from_iterator_to_value(inserted, abound, to); |
| 133 |
return; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
insert_range(from, to); |
| 139 |
} |
| 140 |
|
| 141 |
void erase(T from, T to) |
| 142 |
{ |
| 143 |
if (from >= to) |
| 144 |
return; |
| 145 |
|
| 146 |
|
| 147 |
auto bound = Map.upper_bound(from); |
| 148 |
if (bound == Map.end()) |
| 149 |
{ |
| 150 |
|
| 151 |
if (Map.empty()) |
| 152 |
{ |
| 153 |
|
| 154 |
return; |
| 155 |
} |
| 156 |
--bound; |
| 157 |
|
| 158 |
if (from >= get_to(bound)) |
| 159 |
{ |
| 160 |
|
| 161 |
return; |
| 162 |
} |
| 163 |
|
| 164 |
if (to >= get_to(bound)) |
| 165 |
{ |
| 166 |
if (from == get_from(bound)) |
| 167 |
{ |
| 168 |
|
| 169 |
erase_range(bound); |
| 170 |
return; |
| 171 |
} |
| 172 |
else |
| 173 |
{ |
| 174 |
|
| 175 |
reduce_to(bound, from); |
| 176 |
return; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
if (from == get_from(bound)) |
| 181 |
{ |
| 182 |
|
| 183 |
reduce_from(bound, to); |
| 184 |
return; |
| 185 |
} |
| 186 |
else |
| 187 |
{ |
| 188 |
|
| 189 |
bisect_range(bound, from, to); |
| 190 |
return; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
if (bound == Map.begin()) |
| 195 |
{ |
| 196 |
|
| 197 |
|
| 198 |
erase_from_iterator_to_value(bound, to); |
| 199 |
return; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
auto abound = bound--; |
| 204 |
|
| 205 |
if (from == get_from(bound)) |
| 206 |
{ |
| 207 |
|
| 208 |
erase_from_iterator_to_value(bound, to); |
| 209 |
return; |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
if (from < get_to(bound)) |
| 216 |
{ |
| 217 |
if (to < get_to(bound)) |
| 218 |
{ |
| 219 |
|
| 220 |
bisect_range(bound, from, to); |
| 221 |
return; |
| 222 |
} |
| 223 |
else |
| 224 |
{ |
| 225 |
|
| 226 |
reduce_to(bound, from); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 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 |
|
| 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 |
|
| 389 |
while (true) |
| 390 |
{ |
| 391 |
|
| 392 |
if (to <= get_from(bound)) |
| 393 |
{ |
| 394 |
|
| 395 |
return; |
| 396 |
} |
| 397 |
|
| 398 |
if (to < get_to(bound)) |
| 399 |
{ |
| 400 |
|
| 401 |
reduce_from(bound, to); |
| 402 |
return; |
| 403 |
} |
| 404 |
|
| 405 |
if (to == get_to(bound)) |
| 406 |
{ |
| 407 |
|
| 408 |
erase_range(bound); |
| 409 |
return; |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
bound = erase_range(bound); |
| 415 |
if (bound == Map.end()) |
| 416 |
{ |
| 417 |
|
| 418 |
return; |
| 419 |
} |
| 420 |
} |
| 421 |
} |
| 422 |
}; |
| 423 |
} |