| 1 |
|
| 2 |
|
| 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 |
|
| 15 |
|
| 16 |
template <typename T> |
| 17 |
class RangeSizeSet |
| 18 |
{ |
| 19 |
private: |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 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 |
|
| 43 |
struct Value |
| 44 |
{ |
| 45 |
|
| 46 |
T To; |
| 47 |
|
| 48 |
|
| 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 |
|
| 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 |
|
| 170 |
|
| 171 |
|
| 172 |
auto bound = Map.upper_bound(from); |
| 173 |
if (bound == Map.end()) |
| 174 |
{ |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
if (Map.empty()) |
| 179 |
{ |
| 180 |
insert_range(from, to); |
| 181 |
return; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
--bound; |
| 186 |
if (from > get_to(bound)) |
| 187 |
{ |
| 188 |
insert_range(from, to); |
| 189 |
return; |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
maybe_expand_to(bound, to); |
| 194 |
return; |
| 195 |
} |
| 196 |
|
| 197 |
if (bound == Map.begin()) |
| 198 |
{ |
| 199 |
|
| 200 |
|
| 201 |
|
| 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 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 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 |
|
| 223 |
|
| 224 |
|
| 225 |
if (get_from(abound) <= to) |
| 226 |
{ |
| 227 |
|
| 228 |
auto inserted = insert_range(from, to >= get_to(abound) ? to : get_to(abound)); |
| 229 |
|
| 230 |
abound = erase_range(abound); |
| 231 |
merge_from_iterator_to_value(inserted, abound, to); |
| 232 |
return; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
insert_range(from, to); |
| 238 |
} |
| 239 |
|
| 240 |
void erase(T from, T to) |
| 241 |
{ |
| 242 |
if (from >= to) |
| 243 |
return; |
| 244 |
|
| 245 |
|
| 246 |
auto bound = Map.upper_bound(from); |
| 247 |
if (bound == Map.end()) |
| 248 |
{ |
| 249 |
|
| 250 |
if (Map.empty()) |
| 251 |
{ |
| 252 |
|
| 253 |
return; |
| 254 |
} |
| 255 |
--bound; |
| 256 |
|
| 257 |
if (from >= get_to(bound)) |
| 258 |
{ |
| 259 |
|
| 260 |
return; |
| 261 |
} |
| 262 |
|
| 263 |
if (to >= get_to(bound)) |
| 264 |
{ |
| 265 |
if (from == get_from(bound)) |
| 266 |
{ |
| 267 |
|
| 268 |
erase_range(bound); |
| 269 |
return; |
| 270 |
} |
| 271 |
else |
| 272 |
{ |
| 273 |
|
| 274 |
reduce_to(bound, from); |
| 275 |
return; |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
if (from == get_from(bound)) |
| 280 |
{ |
| 281 |
|
| 282 |
reduce_from(bound, to); |
| 283 |
return; |
| 284 |
} |
| 285 |
else |
| 286 |
{ |
| 287 |
|
| 288 |
bisect_range(bound, from, to); |
| 289 |
return; |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
if (bound == Map.begin()) |
| 294 |
{ |
| 295 |
|
| 296 |
|
| 297 |
erase_from_iterator_to_value(bound, to); |
| 298 |
return; |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
auto abound = bound--; |
| 303 |
|
| 304 |
if (from == get_from(bound)) |
| 305 |
{ |
| 306 |
|
| 307 |
erase_from_iterator_to_value(bound, to); |
| 308 |
return; |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
if (from < get_to(bound)) |
| 315 |
{ |
| 316 |
if (to < get_to(bound)) |
| 317 |
{ |
| 318 |
|
| 319 |
bisect_range(bound, from, to); |
| 320 |
return; |
| 321 |
} |
| 322 |
else |
| 323 |
{ |
| 324 |
|
| 325 |
reduce_to(bound, from); |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
MapT Map; |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 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 |
|
| 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 |
|
| 552 |
while (true) |
| 553 |
{ |
| 554 |
|
| 555 |
if (to <= get_from(bound)) |
| 556 |
{ |
| 557 |
|
| 558 |
return; |
| 559 |
} |
| 560 |
|
| 561 |
if (to < get_to(bound)) |
| 562 |
{ |
| 563 |
|
| 564 |
reduce_from(bound, to); |
| 565 |
return; |
| 566 |
} |
| 567 |
|
| 568 |
if (to == get_to(bound)) |
| 569 |
{ |
| 570 |
|
| 571 |
erase_range(bound); |
| 572 |
return; |
| 573 |
} |
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
bound = erase_range(bound); |
| 578 |
if (bound == Map.end()) |
| 579 |
{ |
| 580 |
|
| 581 |
return; |
| 582 |
} |
| 583 |
} |
| 584 |
} |
| 585 |
}; |
| 586 |
} |