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 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
|
// __ __ _ ______ _____
// | \/ | (_) | ____| / ____|_ _
// | \ / | __ _ __ _ _ ___ | |__ _ __ _ _ _ __ ___ | | _| |_ _| |_
// | |\/| |/ _` |/ _` | |/ __| | __| | '_ \| | | | '_ ` _ \ | | |_ _|_ _|
// | | | | (_| | (_| | | (__ | |____| | | | |_| | | | | | | | |____|_| |_|
// |_| |_|\__,_|\__, |_|\___| |______|_| |_|\__,_|_| |_| |_| \_____|
// __/ | https://github.com/Neargye/magic_enum
// |___/ version 0.9.7
//
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
// SPDX-License-Identifier: MIT
// Copyright (c) 2019 - 2024 Daniil Goncharov <neargye@gmail.com>.
// Copyright (c) 2022 - 2023 Bela Schaum <schaumb@gmail.com>.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#ifndef NEARGYE_MAGIC_ENUM_CONTAINERS_HPP
#define NEARGYE_MAGIC_ENUM_CONTAINERS_HPP
#include "magic_enum.hpp"
#if !defined(MAGIC_ENUM_NO_EXCEPTION) && (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND))
#ifndef MAGIC_ENUM_USE_STD_MODULE
# include <stdexcept>
#endif
# define MAGIC_ENUM_THROW(...) throw (__VA_ARGS__)
#else
#ifndef MAGIC_ENUM_USE_STD_MODULE
# include <cstdlib>
#endif
# define MAGIC_ENUM_THROW(...) std::abort()
#endif
namespace magic_enum::containers {
namespace detail {
template <typename T, typename = void>
static constexpr bool is_transparent_v{};
template <typename T>
static constexpr bool is_transparent_v<T, std::void_t<typename T::is_transparent>>{true};
template <typename Eq = std::equal_to<>, typename T1, typename T2>
constexpr bool equal(T1&& t1, T2&& t2, Eq&& eq = {}) {
auto first1 = t1.begin();
auto last1 = t1.end();
auto first2 = t2.begin();
auto last2 = t2.end();
for (; first1 != last1; ++first1, ++first2) {
if (first2 == last2 || !eq(*first1, *first2)) {
return false;
}
}
return first2 == last2;
}
template <typename Cmp = std::less<>, typename T1, typename T2>
constexpr bool lexicographical_compare(T1&& t1, T2&& t2, Cmp&& cmp = {}) noexcept {
auto first1 = t1.begin();
auto last1 = t1.end();
auto first2 = t2.begin();
auto last2 = t2.end();
// copied from std::lexicographical_compare
for (; (first1 != last1) && (first2 != last2); ++first1, (void)++first2) {
if (cmp(*first1, *first2)) {
return true;
}
if (cmp(*first2, *first1)) {
return false;
}
}
return (first1 == last1) && (first2 != last2);
}
template <typename T>
constexpr std::size_t popcount(T x) noexcept {
std::size_t c = 0;
while (x > 0) {
c += x & 1;
x >>= 1;
}
return c;
}
template <typename Cmp = std::less<>, typename ForwardIt, typename E>
constexpr ForwardIt lower_bound(ForwardIt first, ForwardIt last, E&& e, Cmp&& comp = {}) {
auto count = std::distance(first, last);
for (auto it = first; count > 0;) {
auto step = count / 2;
std::advance(it, step);
if (comp(*it, e)) {
first = ++it;
count -= step + 1;
} else {
count = step;
}
}
return first;
}
template <typename Cmp = std::less<>, typename BidirIt, typename E>
constexpr auto equal_range(BidirIt begin, BidirIt end, E&& e, Cmp&& comp = {}) {
const auto first = lower_bound(begin, end, e, comp);
return std::pair{first, lower_bound(std::make_reverse_iterator(end), std::make_reverse_iterator(first), e, [&comp](auto&& lhs, auto&& rhs) { return comp(rhs, lhs); }).base()};
}
template <typename E = void, typename Cmp = std::less<E>, typename = void>
class indexing {
[[nodiscard]] static constexpr auto get_indices() noexcept {
// reverse result index mapping
std::array<std::size_t, enum_count<E>()> rev_res{};
// std::iota
for (std::size_t i = 0; i < enum_count<E>(); ++i) {
rev_res[i] = i;
}
constexpr auto orig_values = enum_values<E>();
constexpr Cmp cmp{};
// ~std::sort
for (std::size_t i = 0; i < enum_count<E>(); ++i) {
for (std::size_t j = i + 1; j < enum_count<E>(); ++j) {
if (cmp(orig_values[rev_res[j]], orig_values[rev_res[i]])) {
auto tmp = rev_res[i];
rev_res[i] = rev_res[j];
rev_res[j] = tmp;
}
}
}
std::array<E, enum_count<E>()> sorted_values{};
// reverse the sorted indices
std::array<std::size_t, enum_count<E>()> res{};
for (std::size_t i = 0; i < enum_count<E>(); ++i) {
res[rev_res[i]] = i;
sorted_values[i] = orig_values[rev_res[i]];
}
return std::pair{sorted_values, res};
}
static constexpr auto indices = get_indices();
public:
[[nodiscard]] static constexpr const E* begin() noexcept { return indices.first.data(); }
[[nodiscard]] static constexpr const E* end() noexcept { return indices.first.data() + indices.first.size(); }
[[nodiscard]] static constexpr const E* it(std::size_t i) noexcept { return indices.first.data() + i; }
[[nodiscard]] static constexpr optional<std::size_t> at(E val) noexcept {
if (auto i = enum_index(val)) {
return indices.second[*i];
}
return {};
}
};
template <typename E, typename Cmp>
class indexing<E, Cmp, std::enable_if_t<std::is_enum_v<std::decay_t<E>> && (std::is_same_v<Cmp, std::less<E>> || std::is_same_v<Cmp, std::less<>>)>> {
static constexpr auto& values = enum_values<E>();
public:
[[nodiscard]] static constexpr const E* begin() noexcept { return values.data(); }
[[nodiscard]] static constexpr const E* end() noexcept { return values.data() + values.size(); }
[[nodiscard]] static constexpr const E* it(std::size_t i) noexcept { return values.data() + i; }
[[nodiscard]] static constexpr optional<std::size_t> at(E val) noexcept { return enum_index(val); }
};
template <typename Cmp>
struct indexing<void, Cmp, void> {
using is_transparent = std::true_type;
template <typename E>
[[nodiscard]] static constexpr optional<std::size_t> at(E val) noexcept {
return indexing<E, Cmp>::at(val);
}
};
template <typename E = void, typename Cmp = std::less<>, typename = void>
struct name_sort_impl {
[[nodiscard]] constexpr bool operator()(E e1, E e2) const noexcept { return Cmp{}(enum_name(e1), enum_name(e2)); }
};
template <typename Cmp>
struct name_sort_impl<void, Cmp> {
using is_transparent = std::true_type;
template <typename C = Cmp, typename = void>
struct FullCmp : C {};
template <typename C>
struct FullCmp<C, std::enable_if_t<!std::is_invocable_v<C, string_view, string_view> && std::is_invocable_v<C, char_type, char_type>>> {
[[nodiscard]] constexpr bool operator()(string_view s1, string_view s2) const noexcept { return lexicographical_compare<C>(s1, s2); }
};
template <typename E1, typename E2>
[[nodiscard]] constexpr std::enable_if_t<
// at least one of need to be an enum type
(std::is_enum_v<std::decay_t<E1>> || std::is_enum_v<std::decay_t<E2>>) &&
// if both is enum, only accept if the same enum
(!std::is_enum_v<std::decay_t<E1>> || !std::is_enum_v<std::decay_t<E2>> || std::is_same_v<E1, E2>) &&
// is invocable with comparator
(std::is_invocable_r_v<bool, FullCmp<>, std::conditional_t<std::is_enum_v<std::decay_t<E1>>, string_view, E1>, std::conditional_t<std::is_enum_v<std::decay_t<E2>>, string_view, E2>>),
bool>
operator()(E1 e1, E2 e2) const noexcept {
using D1 = std::decay_t<E1>;
using D2 = std::decay_t<E2>;
constexpr FullCmp<> cmp{};
if constexpr (std::is_enum_v<D1> && std::is_enum_v<D2>) {
return cmp(enum_name(e1), enum_name(e2));
} else if constexpr (std::is_enum_v<D1>) {
return cmp(enum_name(e1), e2);
} else /* if constexpr (std::is_enum_v<D2>) */ {
return cmp(e1, enum_name(e2));
}
}
};
struct raw_access_t {};
template <typename Parent, typename Iterator, typename Getter, typename Predicate>
struct FilteredIterator {
Parent parent;
Iterator first;
Iterator last;
Iterator current;
Getter getter;
Predicate predicate;
using iterator_category = std::bidirectional_iterator_tag;
using value_type = std::remove_reference_t<std::invoke_result_t<Getter, Parent, Iterator>>;
using difference_type = std::ptrdiff_t;
using pointer = value_type*;
using reference = value_type&;
constexpr FilteredIterator() noexcept = default;
constexpr FilteredIterator(const FilteredIterator&) = default;
constexpr FilteredIterator& operator=(const FilteredIterator&) = default;
constexpr FilteredIterator(FilteredIterator&&) noexcept = default;
constexpr FilteredIterator& operator=(FilteredIterator&&) noexcept = default;
template <typename OtherParent, typename OtherIterator, typename = std::enable_if_t<std::is_convertible_v<OtherParent, Parent> && std::is_convertible_v<OtherIterator, Iterator>>*>
constexpr explicit FilteredIterator(const FilteredIterator<OtherParent, OtherIterator, Getter, Predicate>& other)
: parent(other.parent), first(other.first), last(other.last), current(other.current), getter(other.getter), predicate(other.predicate) {}
constexpr FilteredIterator(Parent p, Iterator begin, Iterator end, Iterator curr, Getter get = {}, Predicate pred = {})
: parent(p), first(std::move(begin)), last(std::move(end)), current(std::move(curr)), getter{std::move(get)}, predicate{std::move(pred)} {
if (current == first && !predicate(parent, current)) {
++*this;
}
}
[[nodiscard]] constexpr reference operator*() const { return getter(parent, current); }
[[nodiscard]] constexpr pointer operator->() const { return std::addressof(**this); }
constexpr FilteredIterator& operator++() {
do {
++current;
} while (current != last && !predicate(parent, current));
return *this;
}
[[nodiscard]] constexpr FilteredIterator operator++(int) {
FilteredIterator cp = *this;
++*this;
return cp;
}
constexpr FilteredIterator& operator--() {
do {
--current;
} while (current != first && !predicate(parent, current));
return *this;
}
[[nodiscard]] constexpr FilteredIterator operator--(int) {
FilteredIterator cp = *this;
--*this;
return cp;
}
[[nodiscard]] friend constexpr bool operator==(const FilteredIterator& lhs, const FilteredIterator& rhs) { return lhs.current == rhs.current; }
[[nodiscard]] friend constexpr bool operator!=(const FilteredIterator& lhs, const FilteredIterator& rhs) { return lhs.current != rhs.current; }
};
} // namespace detail
template <typename E = void>
using name_less = detail::name_sort_impl<E>;
template <typename E = void>
using name_greater = detail::name_sort_impl<E, std::greater<>>;
using name_less_case_insensitive = detail::name_sort_impl<void, magic_enum::detail::case_insensitive<std::less<>>>;
using name_greater_case_insensitive = detail::name_sort_impl<void, magic_enum::detail::case_insensitive<std::greater<>>>;
template <typename E = void>
using default_indexing = detail::indexing<E>;
template <typename Cmp = std::less<>>
using comparator_indexing = detail::indexing<void, Cmp>;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ARRAY //
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template <typename E, typename V, typename Index = default_indexing<E>>
struct array {
static_assert(std::is_enum_v<E>);
static_assert(std::is_trivially_constructible_v<Index>);
static_assert(enum_count<E>() > 0 && Index::at(enum_values<E>().front()));
using index_type = Index;
using container_type = std::array<V, enum_count<E>()>;
using value_type = typename container_type::value_type;
using size_type = typename container_type::size_type;
using difference_type = typename container_type::difference_type;
using reference = typename container_type::reference;
using const_reference = typename container_type::const_reference;
using pointer = typename container_type::pointer;
using const_pointer = typename container_type::const_pointer;
using iterator = typename container_type::iterator;
using const_iterator = typename container_type::const_iterator;
using reverse_iterator = typename container_type::reverse_iterator;
using const_reverse_iterator = typename container_type::const_reverse_iterator;
constexpr reference at(E pos) {
if (auto index = index_type::at(pos)) {
return a[*index];
}
MAGIC_ENUM_THROW(std::out_of_range("magic_enum::containers::array::at Unrecognized position"));
}
constexpr const_reference at(E pos) const {
if (auto index = index_type::at(pos)) {
return a[*index];
}
MAGIC_ENUM_THROW(std::out_of_range("magic_enum::containers::array::at: Unrecognized position"));
}
[[nodiscard]] constexpr reference operator[](E pos) {
auto i = index_type::at(pos);
return MAGIC_ENUM_ASSERT(i), a[*i];
}
[[nodiscard]] constexpr const_reference operator[](E pos) const {
auto i = index_type::at(pos);
return MAGIC_ENUM_ASSERT(i), a[*i];
}
[[nodiscard]] constexpr reference front() noexcept { return a.front(); }
[[nodiscard]] constexpr const_reference front() const noexcept { return a.front(); }
[[nodiscard]] constexpr reference back() noexcept { return a.back(); }
[[nodiscard]] constexpr const_reference back() const noexcept { return a.back(); }
[[nodiscard]] constexpr pointer data() noexcept { return a.data(); }
[[nodiscard]] constexpr const_pointer data() const noexcept { return a.data(); }
[[nodiscard]] constexpr iterator begin() noexcept { return a.begin(); }
[[nodiscard]] constexpr const_iterator begin() const noexcept { return a.begin(); }
[[nodiscard]] constexpr const_iterator cbegin() const noexcept { return a.cbegin(); }
[[nodiscard]] constexpr iterator end() noexcept { return a.end(); }
[[nodiscard]] constexpr const_iterator end() const noexcept { return a.end(); }
[[nodiscard]] constexpr const_iterator cend() const noexcept { return a.cend(); }
[[nodiscard]] constexpr iterator rbegin() noexcept { return a.rbegin(); }
[[nodiscard]] constexpr const_iterator rbegin() const noexcept { return a.rbegin(); }
[[nodiscard]] constexpr const_iterator crbegin() const noexcept { return a.crbegin(); }
[[nodiscard]] constexpr iterator rend() noexcept { return a.rend(); }
[[nodiscard]] constexpr const_iterator rend() const noexcept { return a.rend(); }
[[nodiscard]] constexpr const_iterator crend() const noexcept { return a.crend(); }
[[nodiscard]] constexpr bool empty() const noexcept { return a.empty(); }
[[nodiscard]] constexpr size_type size() const noexcept { return a.size(); }
[[nodiscard]] constexpr size_type max_size() const noexcept { return a.max_size(); }
constexpr void fill(const V& value) {
for (auto& v : a) {
v = value;
}
}
constexpr void swap(array& other) noexcept(std::is_nothrow_swappable_v<V>) {
for (std::size_t i = 0; i < a.size(); ++i) {
auto v = std::move(other.a[i]);
other.a[i] = std::move(a[i]);
a[i] = std::move(v);
}
}
[[nodiscard]] friend constexpr bool operator==(const array& a1, const array& a2) { return detail::equal(a1, a2); }
[[nodiscard]] friend constexpr bool operator!=(const array& a1, const array& a2) { return !detail::equal(a1, a2); }
[[nodiscard]] friend constexpr bool operator<(const array& a1, const array& a2) { return detail::lexicographical_compare(a1, a2); }
[[nodiscard]] friend constexpr bool operator<=(const array& a1, const array& a2) { return !detail::lexicographical_compare(a2, a1); }
[[nodiscard]] friend constexpr bool operator>(const array& a1, const array& a2) { return detail::lexicographical_compare(a2, a1); }
[[nodiscard]] friend constexpr bool operator>=(const array& a1, const array& a2) { return !detail::lexicographical_compare(a1, a2); }
container_type a;
};
namespace detail {
template <typename E, typename T, std::size_t N, std::size_t... I>
constexpr array<E, std::remove_cv_t<T>> to_array_impl(T (&a)[N], std::index_sequence<I...>) {
return {{a[I]...}};
}
template <typename E, typename T, std::size_t N, std::size_t... I>
constexpr array<E, std::remove_cv_t<T>> to_array_impl(T(&&a)[N], std::index_sequence<I...>) {
return {{std::move(a[I])...}};
}
} // namespace detail
template <typename E, typename T, std::size_t N>
constexpr std::enable_if_t<(enum_count<E>() == N), array<E, std::remove_cv_t<T>>> to_array(T (&a)[N]) {
return detail::to_array_impl<E>(a, std::make_index_sequence<N>{});
}
template <typename E, typename T, std::size_t N>
constexpr std::enable_if_t<(enum_count<E>() == N), array<E, std::remove_cv_t<T>>> to_array(T(&&a)[N]) {
return detail::to_array_impl<E>(std::move(a), std::make_index_sequence<N>{});
}
template <typename E, typename... Ts>
constexpr std::enable_if_t<(enum_count<E>() == sizeof...(Ts)), array<E, std::remove_cv_t<std::common_type_t<Ts...>>>> make_array(Ts&&... ts) {
return {{std::forward<Ts>(ts)...}};
}
inline constexpr detail::raw_access_t raw_access{};
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// BITSET //
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template <typename E, typename Index = default_indexing<E>>
class bitset {
static_assert(std::is_enum_v<E>);
static_assert(std::is_trivially_constructible_v<Index>);
static_assert(enum_count<E>() > 0 && Index::at(enum_values<E>().front()));
using base_type = std::conditional_t<enum_count<E>() <= 8, std::uint_least8_t,
std::conditional_t<enum_count<E>() <= 16, std::uint_least16_t,
std::conditional_t<enum_count<E>() <= 32, std::uint_least32_t,
std::uint_least64_t>>>;
static constexpr std::size_t bits_per_base = sizeof(base_type) * 8;
static constexpr std::size_t base_type_count = (enum_count<E>() > 0 ? (enum_count<E>() - 1) / bits_per_base + 1 : 0);
static constexpr std::size_t not_interested = base_type_count * bits_per_base - enum_count<E>();
static constexpr base_type last_value_max = (base_type{1} << (bits_per_base - not_interested)) - 1;
template <typename parent_t = bitset*>
class reference_impl {
friend class bitset;
parent_t parent;
std::size_t num_index;
base_type bit_index;
constexpr reference_impl(parent_t p, std::size_t i) noexcept : reference_impl(p, std::pair{i / bits_per_base, base_type{1} << (i % bits_per_base)}) {}
constexpr reference_impl(parent_t p, std::pair<std::size_t, base_type> i) noexcept : parent(p), num_index(std::get<0>(i)), bit_index(std::get<1>(i)) {}
public:
constexpr reference_impl& operator=(bool v) noexcept {
if (v) {
parent->a[num_index] |= bit_index;
} else {
parent->a[num_index] &= ~bit_index;
}
return *this;
}
constexpr reference_impl& operator=(const reference_impl& v) noexcept {
if (this == &v) {
return *this;
}
*this = static_cast<bool>(v);
return *this;
}
[[nodiscard]] constexpr operator bool() const noexcept { return (parent->a[num_index] & bit_index) > 0; }
[[nodiscard]] constexpr bool operator~() const noexcept { return !static_cast<bool>(*this); }
constexpr reference_impl& flip() noexcept {
*this = ~*this;
return *this;
}
};
template <typename T>
[[nodiscard]] constexpr T to_(detail::raw_access_t) const {
T res{};
T flag{1};
for (std::size_t i = 0; i < size(); ++i, flag <<= 1) {
if (const_reference{this, i}) {
if (i >= sizeof(T) * 8) {
MAGIC_ENUM_THROW(std::overflow_error("magic_enum::containers::bitset::to: Cannot represent enum in this type"));
}
res |= flag;
}
}
return res;
}
public:
using index_type = Index;
using container_type = std::array<base_type, base_type_count>;
using reference = reference_impl<>;
using const_reference = reference_impl<const bitset*>;
constexpr explicit bitset(detail::raw_access_t = raw_access) noexcept : a{{}} {}
constexpr explicit bitset(detail::raw_access_t, unsigned long long val) : a{{}} {
unsigned long long bit{1};
for (std::size_t i = 0; i < (sizeof(val) * 8); ++i, bit <<= 1) {
if ((val & bit) > 0) {
if (i >= enum_count<E>()) {
MAGIC_ENUM_THROW(std::out_of_range("magic_enum::containers::bitset::constructor: Upper bit set in raw number"));
}
reference{this, i} = true;
}
}
}
constexpr explicit bitset(detail::raw_access_t, string_view sv, string_view::size_type pos = 0, string_view::size_type n = string_view::npos, char_type zero = static_cast<char_type>('0'), char_type one = static_cast<char_type>('1'))
: a{{}} {
std::size_t i = 0;
for (auto c : sv.substr(pos, n)) {
if (c == one) {
if (i >= enum_count<E>()) {
MAGIC_ENUM_THROW(std::out_of_range("magic_enum::containers::bitset::constructor: Upper bit set in raw string"));
}
reference{this, i} = true;
} else if (c != zero) {
MAGIC_ENUM_THROW(std::invalid_argument("magic_enum::containers::bitset::constructor: Unrecognized character in raw string"));
}
++i;
}
}
constexpr explicit bitset(detail::raw_access_t, const char_type* str, std::size_t n = ~std::size_t{0}, char_type zero = static_cast<char_type>('0'), char_type one = static_cast<char_type>('1'))
: bitset(string_view{str, (std::min)(std::char_traits<char_type>::length(str), n)}, 0, n, zero, one) {}
constexpr bitset(std::initializer_list<E> starters) : a{{}} {
if constexpr (magic_enum::detail::subtype_v<E> == magic_enum::detail::enum_subtype::flags) {
for (auto& f : starters) {
*this |= bitset(f);
}
} else {
for (auto& f : starters) {
set(f);
}
}
}
template <typename V, std::enable_if_t<std::is_same_v<V, E> && magic_enum::detail::subtype_v<V> == magic_enum::detail::enum_subtype::flags, int> = 0>
constexpr explicit bitset(V starter) : a{{}} {
auto u = enum_underlying(starter);
for (E v : enum_values<E>()) {
if (auto ul = enum_underlying(v); (ul & u) != 0) {
u &= ~ul;
(*this)[v] = true;
}
}
if (u != 0) {
MAGIC_ENUM_THROW(std::invalid_argument("magic_enum::containers::bitset::constructor: Unrecognized enum value in flag"));
}
}
template <typename Cmp = std::equal_to<>>
constexpr explicit bitset(string_view sv, Cmp&& cmp = {}, char_type sep = static_cast<char_type>('|')) {
for (std::size_t to = 0; (to = magic_enum::detail::find(sv, sep)) != string_view::npos; sv.remove_prefix(to + 1)) {
if (auto v = enum_cast<E>(sv.substr(0, to), cmp)) {
set(*v);
} else {
MAGIC_ENUM_THROW(std::invalid_argument("magic_enum::containers::bitset::constructor: Unrecognized enum value in string"));
}
}
if (!sv.empty()) {
if (auto v = enum_cast<E>(sv, cmp)) {
set(*v);
} else {
MAGIC_ENUM_THROW(std::invalid_argument("magic_enum::containers::bitset::constructor: Unrecognized enum value in string"));
}
}
}
[[nodiscard]] friend constexpr bool operator==(const bitset& lhs, const bitset& rhs) noexcept { return detail::equal(lhs.a, rhs.a); }
[[nodiscard]] friend constexpr bool operator!=(const bitset& lhs, const bitset& rhs) noexcept { return !detail::equal(lhs.a, rhs.a); }
[[nodiscard]] constexpr bool operator[](E pos) const {
auto i = index_type::at(pos);
return MAGIC_ENUM_ASSERT(i), static_cast<bool>(const_reference(this, *i));
}
[[nodiscard]] constexpr reference operator[](E pos) {
auto i = index_type::at(pos);
return MAGIC_ENUM_ASSERT(i), reference{this, *i};
}
constexpr bool test(E pos) const {
if (auto i = index_type::at(pos)) {
return static_cast<bool>(const_reference(this, *i));
}
MAGIC_ENUM_THROW(std::out_of_range("magic_enum::containers::bitset::test: Unrecognized position"));
}
[[nodiscard]] constexpr bool all() const noexcept {
if constexpr (base_type_count == 0) {
return true;
}
for (std::size_t i = 0; i < base_type_count - (not_interested > 0); ++i) {
auto check = ~a[i];
if (check) {
return false;
}
}
if constexpr (not_interested > 0) {
return a[base_type_count - 1] == last_value_max;
}
}
[[nodiscard]] constexpr bool any() const noexcept {
for (auto& v : a) {
if (v > 0) {
return true;
}
}
return false;
}
[[nodiscard]] constexpr bool none() const noexcept { return !any(); }
[[nodiscard]] constexpr std::size_t count() const noexcept {
std::size_t c = 0;
for (auto& v : a) {
c += detail::popcount(v);
}
return c;
}
[[nodiscard]] constexpr std::size_t size() const noexcept { return enum_count<E>(); }
[[nodiscard]] constexpr std::size_t max_size() const noexcept { return enum_count<E>(); }
constexpr bitset& operator&=(const bitset& other) noexcept {
for (std::size_t i = 0; i < base_type_count; ++i) {
a[i] &= other.a[i];
}
return *this;
}
constexpr bitset& operator|=(const bitset& other) noexcept {
for (std::size_t i = 0; i < base_type_count; ++i) {
a[i] |= other.a[i];
}
return *this;
}
constexpr bitset& operator^=(const bitset& other) noexcept {
for (std::size_t i = 0; i < base_type_count; ++i) {
a[i] ^= other.a[i];
}
return *this;
}
[[nodiscard]] constexpr bitset operator~() const noexcept {
bitset res;
for (std::size_t i = 0; i < base_type_count - (not_interested > 0); ++i) {
res.a[i] = ~a[i];
}
if constexpr (not_interested > 0) {
res.a[base_type_count - 1] = ~a[base_type_count - 1] & last_value_max;
}
return res;
}
constexpr bitset& set() noexcept {
for (std::size_t i = 0; i < base_type_count - (not_interested > 0); ++i) {
a[i] = ~base_type{0};
}
if constexpr (not_interested > 0) {
a[base_type_count - 1] = last_value_max;
}
return *this;
}
constexpr bitset& set(E pos, bool value = true) {
if (auto i = index_type::at(pos)) {
reference{this, *i} = value;
return *this;
}
MAGIC_ENUM_THROW(std::out_of_range("magic_enum::containers::bitset::set: Unrecognized position"));
}
constexpr bitset& reset() noexcept { return *this = bitset{}; }
constexpr bitset& reset(E pos) {
if (auto i = index_type::at(pos)) {
reference{this, *i} = false;
return *this;
}
MAGIC_ENUM_THROW(std::out_of_range("magic_enum::containers::bitset::reset: Unrecognized position"));
}
constexpr bitset& flip() noexcept { return *this = ~*this; }
[[nodiscard]] friend constexpr bitset operator&(const bitset& lhs, const bitset& rhs) noexcept {
bitset cp = lhs;
cp &= rhs;
return cp;
}
[[nodiscard]] friend constexpr bitset operator|(const bitset& lhs, const bitset& rhs) noexcept {
bitset cp = lhs;
cp |= rhs;
return cp;
}
[[nodiscard]] friend constexpr bitset operator^(const bitset& lhs, const bitset& rhs) noexcept {
bitset cp = lhs;
cp ^= rhs;
return cp;
}
template <typename V = E>
[[nodiscard]] constexpr explicit operator std::enable_if_t<magic_enum::detail::subtype_v<V> == magic_enum::detail::enum_subtype::flags, E>() const {
E res{};
for (const auto& e : enum_values<E>()) {
if (test(e)) {
res |= e;
}
}
return res;
}
[[nodiscard]] string to_string(char_type sep = static_cast<char_type>('|')) const {
string name;
for (const auto& e : enum_values<E>()) {
if (test(e)) {
if (!name.empty()) {
name.append(1, sep);
}
auto n = enum_name(e);
name.append(n.data(), n.size());
}
}
return name;
}
[[nodiscard]] string to_string(detail::raw_access_t, char_type zero = static_cast<char_type>('0'), char_type one = static_cast<char_type>('1')) const {
string name;
name.reserve(size());
for (std::size_t i = 0; i < size(); ++i) {
name.append(1, const_reference{this, i} ? one : zero);
}
return name;
}
[[nodiscard]] constexpr unsigned long long to_ullong(detail::raw_access_t raw) const { return to_<unsigned long long>(raw); }
[[nodiscard]] constexpr unsigned long long to_ulong(detail::raw_access_t raw) const { return to_<unsigned long>(raw); }
friend std::ostream& operator<<(std::ostream& o, const bitset& bs) { return o << bs.to_string(); }
friend std::istream& operator>>(std::istream& i, bitset& bs) {
string s;
if (i >> s; !s.empty()) {
bs = bitset(string_view{s});
}
return i;
}
private:
container_type a;
};
template <typename V, int = 0>
explicit bitset(V starter) -> bitset<V>;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// SET //
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template <typename E, typename Cmp = std::less<E>>
class set {
using index_type = detail::indexing<E, Cmp>;
struct Getter {
constexpr const E& operator()(const set*, const E* p) const noexcept { return *p; }
};
struct Predicate {
constexpr bool operator()(const set* h, const E* e) const noexcept { return h->a[*e]; }
};
public:
using container_type = bitset<E, index_type>;
using key_type = E;
using value_type = E;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using key_compare = Cmp;
using value_compare = Cmp;
using reference = value_type&;
using const_reference = const value_type&;
using pointer = value_type*;
using const_pointer = const value_type*;
using iterator = detail::FilteredIterator<const set*, const E*, Getter, Predicate>;
using const_iterator = detail::FilteredIterator<const set*, const E*, Getter, Predicate>;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
constexpr set() noexcept = default;
template <typename InputIt>
constexpr set(InputIt first, InputIt last) {
while (first != last) {
insert(*first++);
}
}
constexpr set(std::initializer_list<E> ilist) {
for (auto e : ilist) {
insert(e);
}
}
template <typename V, std::enable_if_t<std::is_same_v<V, E> && magic_enum::detail::subtype_v<V> == magic_enum::detail::enum_subtype::flags, int> = 0>
constexpr explicit set(V starter) {
auto u = enum_underlying(starter);
for (E v : enum_values<E>()) {
if ((enum_underlying(v) & u) != 0) {
insert(v);
}
}
}
constexpr set(const set&) noexcept = default;
constexpr set(set&&) noexcept = default;
constexpr set& operator=(const set&) noexcept = default;
constexpr set& operator=(set&&) noexcept = default;
constexpr set& operator=(std::initializer_list<E> ilist) {
for (auto e : ilist) {
insert(e);
}
}
constexpr const_iterator begin() const noexcept {
return const_iterator{this, index_type::begin(), index_type::end(), index_type::begin()};
}
constexpr const_iterator end() const noexcept {
return const_iterator{this, index_type::begin(), index_type::end(), index_type::end()};
}
constexpr const_iterator cbegin() const noexcept { return begin(); }
constexpr const_iterator cend() const noexcept { return end(); }
constexpr const_reverse_iterator rbegin() const noexcept { return {end()}; }
constexpr const_reverse_iterator rend() const noexcept { return {begin()}; }
constexpr const_reverse_iterator crbegin() const noexcept { return rbegin(); }
constexpr const_reverse_iterator crend() const noexcept { return rend(); }
[[nodiscard]] constexpr bool empty() const noexcept { return s == 0; }
[[nodiscard]] constexpr size_type size() const noexcept { return s; }
[[nodiscard]] constexpr size_type max_size() const noexcept { return a.max_size(); }
constexpr void clear() noexcept {
a.reset();
s = 0;
}
constexpr std::pair<iterator, bool> insert(const value_type& value) noexcept {
if (auto i = index_type::at(value)) {
typename container_type::reference ref = a[value];
bool r = !ref;
if (r) {
ref = true;
++s;
}
return {iterator{this, index_type::begin(), index_type::end(), index_type::it(*i)}, r};
}
return {end(), false};
}
constexpr std::pair<iterator, bool> insert(value_type&& value) noexcept { return insert(value); }
constexpr iterator insert(const_iterator, const value_type& value) noexcept { return insert(value).first; }
constexpr iterator insert(const_iterator hint, value_type&& value) noexcept { return insert(hint, value); }
template <typename InputIt>
constexpr void insert(InputIt first, InputIt last) noexcept {
while (first != last) {
insert(*first++);
}
}
constexpr void insert(std::initializer_list<value_type> ilist) noexcept {
for (auto v : ilist) {
insert(v);
}
}
template <typename... Args>
constexpr std::pair<iterator, bool> emplace(Args&&... args) noexcept {
return insert({std::forward<Args>(args)...});
}
template <typename... Args>
constexpr iterator emplace_hint(const_iterator, Args&&... args) noexcept {
return emplace(std::forward<Args>(args)...).first;
}
constexpr iterator erase(const_iterator pos) noexcept {
erase(*pos++);
return pos;
}
constexpr iterator erase(const_iterator first, const_iterator last) noexcept {
while ((first = erase(first)) != last) {
}
return first;
}
constexpr size_type erase(const key_type& key) noexcept {
typename container_type::reference ref = a[key];
bool res = ref;
if (res) {
--s;
}
ref = false;
return res;
}
template <typename K, typename KC = key_compare>
constexpr std::enable_if_t<detail::is_transparent_v<KC>, size_type> erase(K&& x) noexcept {
size_type c = 0;
for (auto [first, last] = detail::equal_range(index_type::begin(), index_type::end(), x, key_compare{}); first != last;) {
c += erase(*first++);
}
return c;
}
void swap(set& other) noexcept {
set cp = *this;
*this = other;
other = cp;
}
[[nodiscard]] constexpr size_type count(const key_type& key) const noexcept { return index_type::at(key) && a[key]; }
template <typename K, typename KC = key_compare>
[[nodiscard]] constexpr std::enable_if_t<detail::is_transparent_v<KC>, size_type> count(const K& x) const {
size_type c = 0;
for (auto [first, last] = detail::equal_range(index_type::begin(), index_type::end(), x, key_compare{}); first != last; ++first) {
c += count(*first);
}
return c;
}
[[nodiscard]] constexpr const_iterator find(const key_type& key) const noexcept {
if (auto i = index_type::at(key); i && a.test(key)) {
return const_iterator{this, index_type::begin(), index_type::end(), index_type::it(*i)};
}
return end();
}
template <typename K, typename KC = key_compare>
[[nodiscard]] constexpr std::enable_if_t<detail::is_transparent_v<KC>, const_iterator> find(const K& x) const {
for (auto [first, last] = detail::equal_range(index_type::begin(), index_type::end(), x, key_compare{}); first != last; ++first) {
if (a.test(*first)) {
return find(*first);
}
}
return end();
}
[[nodiscard]] constexpr bool contains(const key_type& key) const noexcept { return count(key); }
template <typename K, typename KC = key_compare>
[[nodiscard]] constexpr std::enable_if_t<detail::is_transparent_v<KC>, bool> contains(const K& x) const noexcept {
return count(x) > 0;
}
[[nodiscard]] constexpr std::pair<const_iterator, const_iterator> equal_range(const key_type& key) const noexcept { return {lower_bound(key), upper_bound(key)}; }
template <typename K, typename KC = key_compare>
[[nodiscard]] constexpr std::enable_if_t<detail::is_transparent_v<KC>, std::pair<const_iterator, const_iterator>> equal_range(const K& x) const noexcept {
return {lower_bound(x), upper_bound(x)};
}
[[nodiscard]] constexpr const_iterator lower_bound(const key_type& key) const noexcept {
if (auto i = index_type::at(key)) {
auto it = const_iterator{this, index_type::begin(), index_type::end(), index_type::it(*i)};
return a.test(key) ? it : std::next(it);
}
return end();
}
template <typename K, typename KC = key_compare>
[[nodiscard]] constexpr std::enable_if_t<detail::is_transparent_v<KC>, const_iterator> lower_bound(const K& x) const noexcept {
auto [first, last] = detail::equal_range(index_type::begin(), index_type::end(), x, key_compare{});
return first != last ? lower_bound(*first) : end();
}
[[nodiscard]] constexpr const_iterator upper_bound(const key_type& key) const noexcept {
if (auto i = index_type::at(key)) {
return std::next(const_iterator{this, index_type::begin(), index_type::end(), index_type::it(*i)});
}
return end();
}
template <typename K, typename KC = key_compare>
[[nodiscard]] constexpr std::enable_if_t<detail::is_transparent_v<KC>, const_iterator> upper_bound(const K& x) const noexcept {
auto [first, last] = detail::equal_range(index_type::begin(), index_type::end(), x, key_compare{});
return first != last ? upper_bound(*std::prev(last)) : end();
}
[[nodiscard]] constexpr key_compare key_comp() const { return {}; }
[[nodiscard]] constexpr value_compare value_comp() const { return {}; }
[[nodiscard]] constexpr friend bool operator==(const set& lhs, const set& rhs) noexcept { return lhs.a == rhs.a; }
[[nodiscard]] constexpr friend bool operator!=(const set& lhs, const set& rhs) noexcept { return lhs.a != rhs.a; }
[[nodiscard]] constexpr friend bool operator<(const set& lhs, const set& rhs) noexcept {
if (lhs.s < rhs.s) {
return true;
}
if (rhs.s < lhs.s) {
return false;
}
for (auto it = index_type::begin(); it != index_type::end(); ++it) {
if (auto c = rhs.contains(*it); c != lhs.contains(*it)) {
return c;
}
}
return false;
}
[[nodiscard]] constexpr friend bool operator<=(const set& lhs, const set& rhs) noexcept { return !(rhs < lhs); }
[[nodiscard]] constexpr friend bool operator>(const set& lhs, const set& rhs) noexcept { return rhs < lhs; }
[[nodiscard]] constexpr friend bool operator>=(const set& lhs, const set& rhs) noexcept { return !(lhs < rhs); }
template <typename Pred>
size_type erase_if(Pred pred) {
auto old_size = size();
for (auto i = begin(), last = end(); i != last;) {
if (pred(*i)) {
i = erase(i);
} else {
++i;
}
}
return old_size - size();
}
private:
container_type a;
std::size_t s = 0;
};
template <typename V, int = 0>
explicit set(V starter) -> set<V>;
template <auto I, typename E, typename V, typename Index>
constexpr std::enable_if_t<(std::is_integral_v<decltype(I)> && I < enum_count<E>()), V&> get(array<E, V, Index>& a) noexcept {
return a.a[I];
}
template <auto I, typename E, typename V, typename Index>
constexpr std::enable_if_t<(std::is_integral_v<decltype(I)> && I < enum_count<E>()), V&&> get(array<E, V, Index>&& a) noexcept {
return std::move(a.a[I]);
}
template <auto I, typename E, typename V, typename Index>
constexpr std::enable_if_t<(std::is_integral_v<decltype(I)> && I < enum_count<E>()), const V&> get(const array<E, V, Index>& a) noexcept {
return a.a[I];
}
template <auto I, typename E, typename V, typename Index>
constexpr std::enable_if_t<(std::is_integral_v<decltype(I)> && I < enum_count<E>()), const V&&> get(const array<E, V, Index>&& a) noexcept {
return std::move(a.a[I]);
}
template <auto Enum, typename E, typename V, typename Index>
constexpr std::enable_if_t<std::is_same_v<decltype(Enum), E> && enum_contains(Enum), V&> get(array<E, V, Index>& a) {
return a[Enum];
}
template <auto Enum, typename E, typename V, typename Index>
constexpr std::enable_if_t<std::is_same_v<decltype(Enum), E> && enum_contains(Enum), V&&> get(array<E, V, Index>&& a) {
return std::move(a[Enum]);
}
template <auto Enum, typename E, typename V, typename Index>
constexpr std::enable_if_t<std::is_same_v<decltype(Enum), E> && enum_contains(Enum), const V&> get(const array<E, V, Index>& a) {
return a[Enum];
}
template <auto Enum, typename E, typename V, typename Index>
constexpr std::enable_if_t<std::is_same_v<decltype(Enum), E> && enum_contains(Enum), const V&&> get(const array<E, V, Index>&& a) {
return std::move(a[Enum]);
}
} // namespace magic_enum::containers
#endif // NEARGYE_MAGIC_ENUM_CONTAINERS_HPP
|