From 50adc0691e94ac2da2c439aca40bae57333861b4 Mon Sep 17 00:00:00 2001 From: neargye Date: Tue, 23 May 2023 21:12:42 +0400 Subject: [PATCH] fix clang-format --- example/example_containers_array.cpp | 1 + example/example_containers_bitset.cpp | 1 + example/example_containers_set.cpp | 1 + example/example_switch.cpp | 2 +- include/magic_enum_containers.hpp | 269 ++++++++++++-------------- test/test_containers.cpp | 1 + 6 files changed, 133 insertions(+), 142 deletions(-) diff --git a/example/example_containers_array.cpp b/example/example_containers_array.cpp index 81fe9b8..98a4e03 100644 --- a/example/example_containers_array.cpp +++ b/example/example_containers_array.cpp @@ -1,6 +1,7 @@ // Licensed under the MIT License . // SPDX-License-Identifier: MIT // Copyright (c) 2019 - 2023 Daniil Goncharov . +// Copyright (c) 2022 - 2023 Bela Schaum . // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal diff --git a/example/example_containers_bitset.cpp b/example/example_containers_bitset.cpp index 8bbab8e..e05fdd6 100644 --- a/example/example_containers_bitset.cpp +++ b/example/example_containers_bitset.cpp @@ -1,6 +1,7 @@ // Licensed under the MIT License . // SPDX-License-Identifier: MIT // Copyright (c) 2019 - 2023 Daniil Goncharov . +// Copyright (c) 2022 - 2023 Bela Schaum . // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal diff --git a/example/example_containers_set.cpp b/example/example_containers_set.cpp index c1ef168..a1925d8 100644 --- a/example/example_containers_set.cpp +++ b/example/example_containers_set.cpp @@ -1,6 +1,7 @@ // Licensed under the MIT License . // SPDX-License-Identifier: MIT // Copyright (c) 2019 - 2023 Daniil Goncharov . +// Copyright (c) 2022 - 2023 Bela Schaum . // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal diff --git a/example/example_switch.cpp b/example/example_switch.cpp index 509f918..78432b7 100644 --- a/example/example_switch.cpp +++ b/example/example_switch.cpp @@ -1,7 +1,7 @@ // Licensed under the MIT License . // SPDX-License-Identifier: MIT // Copyright (c) 2019 - 2023 Daniil Goncharov . -// Copyright (c) 2020 - 2023 Bela Schaum . +// Copyright (c) 2022 - 2023 Bela Schaum . // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal diff --git a/include/magic_enum_containers.hpp b/include/magic_enum_containers.hpp index 0abc037..a5037b1 100644 --- a/include/magic_enum_containers.hpp +++ b/include/magic_enum_containers.hpp @@ -10,7 +10,7 @@ // Licensed under the MIT License . // SPDX-License-Identifier: MIT // Copyright (c) 2019 - 2023 Daniil Goncharov . -// Copyright (c) 2023 - 2023 Bela Schaum . +// Copyright (c) 2022 - 2023 Bela Schaum . // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -33,10 +33,10 @@ #ifndef NEARGYE_MAGIC_ENUM_CONTAINERS_HPP #define NEARGYE_MAGIC_ENUM_CONTAINERS_HPP -#include "magic_enum.hpp" - #include +#include "magic_enum.hpp" + namespace magic_enum::containers { namespace detail { @@ -48,7 +48,7 @@ template constexpr static bool is_transparent_v>{true}; template , typename T1, typename T2> -constexpr bool equal(T1 &&t1, T2 &&t2, Eq &&eq = {}) { +constexpr bool equal(T1&& t1, T2&& t2, Eq&& eq = {}) { auto first1 = t1.begin(); auto last1 = t1.end(); auto first2 = t2.begin(); @@ -63,7 +63,7 @@ constexpr bool equal(T1 &&t1, T2 &&t2, Eq &&eq = {}) { } template , typename T1, typename T2> -constexpr bool lexicographical_compare(T1 &&t1, T2 &&t2, Cmp &&cmp = {}) noexcept { +constexpr bool lexicographical_compare(T1&& t1, T2&& t2, Cmp&& cmp = {}) noexcept { auto first1 = t1.begin(); auto last1 = t1.end(); auto first2 = t2.begin(); @@ -92,7 +92,7 @@ constexpr std::size_t popcount(T x) noexcept { } template , typename ForwardIt, typename E> -constexpr ForwardIt lower_bound(ForwardIt first, ForwardIt last, E &&e, Cmp &&comp = {}) { +constexpr ForwardIt lower_bound(ForwardIt first, ForwardIt last, E&& e, Cmp&& comp = {}) { auto count = std::distance(first, last); for (auto it = first; count > 0;) { @@ -109,11 +109,9 @@ constexpr ForwardIt lower_bound(ForwardIt first, ForwardIt last, E &&e, Cmp &&co } template , typename BidirIt, typename E> -constexpr auto equal_range(BidirIt begin, BidirIt end, E &&e, Cmp &&comp = {}) { +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()}; + 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 = void> @@ -153,7 +151,7 @@ struct indexing { } constexpr static inline std::array()> values = get_indices().first; - constexpr static inline const std::array()> *values_v = &values; + constexpr static inline const std::array()>* values_v = &values; constexpr static inline std::array()> reindex = get_indices().second; [[nodiscard]] constexpr inline optional operator()(E val) const noexcept { @@ -166,7 +164,7 @@ struct indexing { template struct indexing> && (std::is_same_v> || std::is_same_v>)>> { - constexpr static inline const std::array()> *values_v = &enum_values(); + constexpr static inline const std::array()>* values_v = &enum_values(); [[nodiscard]] constexpr inline optional operator()(E val) const noexcept { return enum_index(val); } }; @@ -235,18 +233,18 @@ struct FilteredIterator { using iterator_category = std::bidirectional_iterator_tag; using value_type = std::remove_reference_t>; using difference_type = std::ptrdiff_t; - using pointer = value_type *; - using reference = value_type &; + 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; + constexpr FilteredIterator(const FilteredIterator&) = default; + constexpr FilteredIterator& operator=(const FilteredIterator&) = default; + constexpr FilteredIterator(FilteredIterator&&) noexcept = default; + constexpr FilteredIterator& operator=(FilteredIterator&&) noexcept = default; template && std::is_convertible_v> *> - constexpr explicit FilteredIterator(const FilteredIterator &other) + typename = std::enable_if_t && std::is_convertible_v>*> + constexpr explicit FilteredIterator(const FilteredIterator& other) : parent(other.parent), first(other.first), last(other.last), current(other.current), getter(other.getter), predicate(other.predicate) {} ~FilteredIterator() = default; @@ -262,7 +260,7 @@ struct FilteredIterator { [[nodiscard]] constexpr pointer operator->() const { return std::addressof(**this); } - constexpr FilteredIterator &operator++() { + constexpr FilteredIterator& operator++() { do { ++current; } while (current != last && !predicate(parent, current)); @@ -275,7 +273,7 @@ struct FilteredIterator { return cp; } - constexpr FilteredIterator &operator--() { + constexpr FilteredIterator& operator--() { do { --current; } while (current != first && !predicate(parent, current)); @@ -288,9 +286,9 @@ struct FilteredIterator { 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; } - [[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 @@ -393,13 +391,13 @@ struct array { [[nodiscard]] constexpr size_type max_size() const noexcept { return a.max_size(); } - constexpr void fill(const V &value) { - for (auto &v : a) { + constexpr void fill(const V& value) { + for (auto& v : a) { v = value; } } - constexpr void swap(array &other) noexcept(std::is_nothrow_swappable_v) { + constexpr void swap(array& other) noexcept(std::is_nothrow_swappable_v) { for (std::size_t i{}; i < a.size(); ++i) { auto v = std::move(other.a[i]); other.a[i] = std::move(a[i]); @@ -407,24 +405,24 @@ struct array { } } - [[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::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(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(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); } + [[nodiscard]] friend constexpr bool operator>=(const array& a1, const array& a2) { return !detail::lexicographical_compare(a1, a2); } container_type a; }; namespace detail { -template +template constexpr array> to_array_impl(T (&a)[N], std::index_sequence) { return {{a[I]...}}; } @@ -447,7 +445,7 @@ constexpr std::enable_if_t<(enum_count() == N), array> } template -constexpr std::enable_if_t<(enum_count() == sizeof...(Ts)), array>>> make_array(Ts &&... ts) { +constexpr std::enable_if_t<(enum_count() == sizeof...(Ts)), array>>> make_array(Ts&&... ts) { return {{std::forward(ts)...}}; } @@ -472,7 +470,7 @@ class bitset { constexpr static std::size_t not_interested = base_type_count * bits_per_base - enum_count(); constexpr static base_type last_value_max = (base_type{1} << (bits_per_base - not_interested)) - 1; - template + template class reference_impl { friend class bitset; @@ -480,14 +478,12 @@ class bitset { std::size_t num_index; base_type bit_index; - constexpr reference_impl(parent_t parent, std::size_t ix) noexcept - : reference_impl(parent, std::pair{ix / bits_per_base, base_type{1} << (ix % bits_per_base)}) {} + constexpr reference_impl(parent_t parent, std::size_t ix) noexcept : reference_impl(parent, std::pair{ix / bits_per_base, base_type{1} << (ix % bits_per_base)}) {} - constexpr reference_impl(parent_t parent, std::pair ix) noexcept - : parent(parent), num_index(std::get<0>(ix)), bit_index(std::get<1>(ix)) {} + constexpr reference_impl(parent_t parent, std::pair ix) noexcept : parent(parent), num_index(std::get<0>(ix)), bit_index(std::get<1>(ix)) {} - public: - constexpr reference_impl &operator=(bool v) noexcept { + public: + constexpr reference_impl& operator=(bool v) noexcept { if (v) { parent->a[num_index] |= bit_index; } else { @@ -496,7 +492,7 @@ class bitset { return *this; } - constexpr reference_impl &operator=(const reference_impl &v) noexcept { + constexpr reference_impl& operator=(const reference_impl& v) noexcept { if (this == &v) { return *this; } @@ -508,7 +504,7 @@ class bitset { [[nodiscard]] constexpr bool operator~() const noexcept { return !static_cast(*this); } - constexpr reference_impl &flip() noexcept { + constexpr reference_impl& flip() noexcept { *this = ~*this; return *this; } @@ -529,11 +525,11 @@ class bitset { return res; } -public: + public: using index_type = Index; using container_type = std::array; using reference = reference_impl<>; - using const_reference = reference_impl; + using const_reference = reference_impl; constexpr explicit bitset(detail::raw_access_t = raw_access) noexcept : a{{}} {} @@ -566,22 +562,21 @@ public: } } - constexpr explicit bitset(detail::raw_access_t, const char *str, std::size_t n = ~std::size_t{}, char zero = '0', char one = '1') + constexpr explicit bitset(detail::raw_access_t, const char* str, std::size_t n = ~std::size_t{}, char zero = '0', char one = '1') : bitset(std::string_view{str, (std::min)(std::char_traits::length(str), n)}, 0, n, zero, one) {} constexpr bitset(std::initializer_list starters) : a{{}} { if constexpr (magic_enum::detail::subtype_v == magic_enum::detail::enum_subtype::flags) { - for (auto &f : starters) { + for (auto& f : starters) { *this |= bitset(f); } } else { - for (auto &f : starters) { + for (auto& f : starters) { set(f); } } } - template && magic_enum::detail::subtype_v == magic_enum::detail::enum_subtype::flags, int> = 0> + template && magic_enum::detail::subtype_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()) { @@ -596,16 +591,16 @@ public: } template > - constexpr explicit bitset(string_view sv, Cmp &&cmp = {}, char sep = '|') { + constexpr explicit bitset(string_view sv, Cmp&& cmp = {}, char sep = '|') { for (std::size_t to{}; (to = magic_enum::detail::find(sv, sep)) != string_view::npos; sv.remove_prefix(to + 1)) { - if (auto v = magic_enum::enum_cast(sv.substr(0, to), cmp)) { + if (auto v = enum_cast(sv.substr(0, to), cmp)) { set(v); } else { throw std::invalid_argument("enum bitset::constructor: unrecognized enum value in string"); } } if (!sv.empty()) { - if (auto v = magic_enum::enum_cast(sv, cmp)) { + if (auto v = enum_cast(sv, cmp)) { set(v); } else { throw std::invalid_argument("enum bitset::constructor: unrecognized enum value in string"); @@ -613,9 +608,9 @@ public: } } - [[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]] 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 noexcept { return static_cast(const_reference(this, *index_type{}(pos))); } @@ -646,7 +641,7 @@ public: } [[nodiscard]] constexpr bool any() const noexcept { - for (auto &v : a) { + for (auto& v : a) { if (v > 0) { return true; } @@ -658,7 +653,7 @@ public: [[nodiscard]] constexpr std::size_t count() const noexcept { std::size_t c{}; - for (auto &v : a) { + for (auto& v : a) { c += detail::popcount(v); } return c; @@ -668,21 +663,21 @@ public: [[nodiscard]] constexpr std::size_t max_size() const noexcept { return enum_count(); } - constexpr bitset &operator&=(const bitset &other) noexcept { + constexpr bitset& operator&=(const bitset& other) noexcept { for (std::size_t i{}; i < base_type_count; ++i) { a[i] &= other.a[i]; } return *this; } - constexpr bitset &operator|=(const bitset &other) noexcept { + constexpr bitset& operator|=(const bitset& other) noexcept { for (std::size_t i{}; i < base_type_count; ++i) { a[i] |= other.a[i]; } return *this; } - constexpr bitset &operator^=(const bitset &other) noexcept { + constexpr bitset& operator^=(const bitset& other) noexcept { for (std::size_t i{}; i < base_type_count; ++i) { a[i] ^= other.a[i]; } @@ -701,7 +696,7 @@ public: return res; } - constexpr bitset &set() noexcept { + constexpr bitset& set() noexcept { for (std::size_t i{}; i < base_type_count - (not_interested > 0); ++i) { a[i] = ~base_type{}; } @@ -712,7 +707,7 @@ public: return *this; } - constexpr bitset &set(E pos, bool value = true) { + constexpr bitset& set(E pos, bool value = true) { if (auto ix = index_type{}(pos)) { reference{this, *ix} = value; return *this; @@ -720,9 +715,9 @@ public: throw std::out_of_range("enum bitset::set: unrecognized position"); } - constexpr bitset &reset() noexcept { return *this = bitset{}; } + constexpr bitset& reset() noexcept { return *this = bitset{}; } - constexpr bitset &reset(E pos) { + constexpr bitset& reset(E pos) { if (auto ix = index_type{}(pos)) { reference{this, *ix} = false; return *this; @@ -730,30 +725,30 @@ public: throw std::out_of_range("enum bitset::reset: unrecognized position"); } - constexpr bitset &flip() noexcept { return *this = ~*this; } + constexpr bitset& flip() noexcept { return *this = ~*this; } - [[nodiscard]] friend constexpr bitset operator&(const bitset &lhs, const bitset &rhs) noexcept { + [[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 { + [[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 { + [[nodiscard]] friend constexpr bitset operator^(const bitset& lhs, const bitset& rhs) noexcept { bitset cp = lhs; cp ^= rhs; return cp; } template - [[nodiscard]]constexpr explicit operator std::enable_if_t == magic_enum::detail::enum_subtype::flags, E>() const { + [[nodiscard]] constexpr explicit operator std::enable_if_t == magic_enum::detail::enum_subtype::flags, E>() const { E res{}; - for (auto &e : enum_values()) { + for (auto& e : enum_values()) { if (test(e)) { res |= e; } @@ -764,7 +759,7 @@ public: [[nodiscard]] string to_string(char sep = '|') const { string name; - for (auto &e : enum_values()) { + for (auto& e : enum_values()) { if (test(e)) { if (!name.empty()) { name.append(1, sep); @@ -789,9 +784,9 @@ public: [[nodiscard]] constexpr unsigned long long to_ulong(detail::raw_access_t raw) const { return to_(raw); } - friend std::ostream &operator<<(std::ostream &o, const bitset &bs) { return o << bs.to_string(); } + friend std::ostream& operator<<(std::ostream& o, const bitset& bs) { return o << bs.to_string(); } - friend std::istream &operator>>(std::istream &i, bitset &bs) { + friend std::istream& operator>>(std::istream& i, bitset& bs) { string s; if (i >> s; !s.empty()) { bs = bitset(string_view{s}); @@ -799,11 +794,11 @@ public: return i; } -private: + private: container_type a; }; -template +template explicit bitset(V starter) -> bitset; /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -813,13 +808,13 @@ template > class set { using index_type = detail::indexing; struct Getter { - constexpr const E &operator()(const set *, const E *p) const noexcept { return *p; } + 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]; } + constexpr bool operator()(const set* h, const E* e) const noexcept { return h->a[*e]; } }; -public: + public: using container_type = bitset; using key_type = E; using value_type = E; @@ -827,12 +822,12 @@ public: using difference_type = std::ptrdiff_t; using key_compare = CExprLess; using value_compare = CExprLess; - using reference = value_type &; - using const_reference = const value_type &; - using pointer = value_type *; - using const_pointer = const value_type *; - using iterator = detail::FilteredIterator; - using const_iterator = detail::FilteredIterator; + using reference = value_type&; + using const_reference = const value_type&; + using pointer = value_type*; + using const_pointer = const value_type*; + using iterator = detail::FilteredIterator; + using const_iterator = detail::FilteredIterator; using reverse_iterator = std::reverse_iterator; using const_reverse_iterator = std::reverse_iterator; @@ -850,8 +845,7 @@ public: insert(e); } } - template &&magic_enum::detail::subtype_v == magic_enum::detail::enum_subtype::flags, int> = 0> + template && magic_enum::detail::subtype_v == magic_enum::detail::enum_subtype::flags, int> = 0> constexpr explicit set(V starter) { auto u = enum_underlying(starter); for (E v : enum_values()) { @@ -861,12 +855,12 @@ public: } } - constexpr set(const set &) noexcept = default; - constexpr set(set &&) noexcept = default; + 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 ilist) { + constexpr set& operator=(const set&) noexcept = default; + constexpr set& operator=(set&&) noexcept = default; + constexpr set& operator=(std::initializer_list ilist) { for (auto e : ilist) { insert(e); } @@ -903,7 +897,7 @@ public: s = 0; } - constexpr std::pair insert(const value_type &value) noexcept { + constexpr std::pair insert(const value_type& value) noexcept { if (auto i = index_type{}(value)) { typename container_type::reference ref = a[value]; bool r = !ref; @@ -917,11 +911,11 @@ public: return {end(), false}; } - constexpr std::pair insert(value_type &&value) noexcept { return insert(value); } + constexpr std::pair 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, const value_type& value) noexcept { return insert(value).first; } - constexpr iterator insert(const_iterator hint, value_type &&value) noexcept { return insert(hint, value); } + constexpr iterator insert(const_iterator hint, value_type&& value) noexcept { return insert(hint, value); } template constexpr void insert(InputIt first, InputIt last) noexcept { @@ -937,10 +931,14 @@ public: } template - constexpr std::pair emplace(Args &&... args) noexcept { return insert({std::forward(args)...}); } + constexpr std::pair emplace(Args&&... args) noexcept { + return insert({std::forward(args)...}); + } template - constexpr iterator emplace_hint(const_iterator, Args &&... args) noexcept { return emplace(std::forward(args)...).first; } + constexpr iterator emplace_hint(const_iterator, Args&&... args) noexcept { + return emplace(std::forward(args)...).first; + } constexpr iterator erase(const_iterator pos) noexcept { erase(*pos++); @@ -954,7 +952,7 @@ public: return first; } - constexpr size_type erase(const key_type &key) noexcept { + constexpr size_type erase(const key_type& key) noexcept { typename container_type::reference ref = a[key]; bool res = ref; if (res) { @@ -965,7 +963,7 @@ public: } template - constexpr std::enable_if_t, size_type> erase(K &&x) noexcept { + constexpr std::enable_if_t, size_type> erase(K&& x) noexcept { size_type c{}; for (auto [first, last] = detail::equal_range(index_type::values_v->begin(), index_type::values_v->end(), x, key_compare{}); first != last;) { c += erase(*first++); @@ -973,16 +971,16 @@ public: return c; } - void swap(set &other) noexcept { + 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{}(key) && a[key]; } + [[nodiscard]] constexpr size_type count(const key_type& key) const noexcept { return index_type{}(key) && a[key]; } template - [[nodiscard]] constexpr std::enable_if_t, size_type> count(const K &x) const { + [[nodiscard]] constexpr std::enable_if_t, size_type> count(const K& x) const { size_type c{}; for (auto [first, last] = detail::equal_range(index_type::values_v->begin(), index_type::values_v->end(), x, key_compare{}); first != last; ++first) { c += count(*first); @@ -990,14 +988,14 @@ public: return c; } - [[nodiscard]] constexpr const_iterator find(const key_type &key) const noexcept { + [[nodiscard]] constexpr const_iterator find(const key_type& key) const noexcept { if (auto i = index_type{}(key); i && a.test(key)) return const_iterator{this, index_type::values_v->begin(), index_type::values_v->end(), &(*index_type::values_v)[*i]}; return end(); } template - [[nodiscard]] constexpr std::enable_if_t, const_iterator> find(const K &x) const { + [[nodiscard]] constexpr std::enable_if_t, const_iterator> find(const K& x) const { for (auto [first, last] = detail::equal_range(index_type::values_v->begin(), index_type::values_v->end(), x, key_compare{}); first != last; ++first) { if (a.test(*first)) { return find(*first); @@ -1006,23 +1004,21 @@ public: return end(); } - [[nodiscard]] constexpr bool contains(const key_type &key) const noexcept { return count(key); } + [[nodiscard]] constexpr bool contains(const key_type& key) const noexcept { return count(key); } template - [[nodiscard]] constexpr std::enable_if_t, bool> contains(const K &x) const noexcept { + [[nodiscard]] constexpr std::enable_if_t, bool> contains(const K& x) const noexcept { return count(x) > 0; } - [[nodiscard]] constexpr std::pair equal_range(const key_type &key) const noexcept { - return {lower_bound(key), upper_bound(key)}; - } + [[nodiscard]] constexpr std::pair equal_range(const key_type& key) const noexcept { return {lower_bound(key), upper_bound(key)}; } template - [[nodiscard]] constexpr std::enable_if_t, std::pair> equal_range(const K &x) const noexcept { + [[nodiscard]] constexpr std::enable_if_t, std::pair> 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 { + [[nodiscard]] constexpr const_iterator lower_bound(const key_type& key) const noexcept { if (auto i = index_type{}(key)) { auto it = const_iterator{this, index_type::values_v->begin(), index_type::values_v->end(), &(*index_type::values_v)[*i]}; return a.test(key) ? it : std::next(it); @@ -1031,12 +1027,12 @@ public: } template - [[nodiscard]] constexpr std::enable_if_t, const_iterator> lower_bound(const K &x) const noexcept { + [[nodiscard]] constexpr std::enable_if_t, const_iterator> lower_bound(const K& x) const noexcept { auto [first, last] = detail::equal_range(index_type::values_v->begin(), index_type::values_v->end(), x, key_compare{}); return first != last ? lower_bound(*first) : end(); } - [[nodiscard]] constexpr const_iterator upper_bound(const key_type &key) const noexcept { + [[nodiscard]] constexpr const_iterator upper_bound(const key_type& key) const noexcept { if (auto i = index_type{}(key)) { return std::next(const_iterator{this, index_type::values_v->begin(), index_type::values_v->end(), &(*index_type::values_v)[*i]}); } @@ -1044,7 +1040,7 @@ public: } template - [[nodiscard]] constexpr std::enable_if_t, const_iterator> upper_bound(const K &x) const noexcept { + [[nodiscard]] constexpr std::enable_if_t, const_iterator> upper_bound(const K& x) const noexcept { auto [first, last] = detail::equal_range(index_type::values_v->begin(), index_type::values_v->end(), x, key_compare{}); return first != last ? upper_bound(*std::prev(last)) : end(); } @@ -1053,11 +1049,11 @@ public: [[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 { 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 { + [[nodiscard]] constexpr friend bool operator<(const set& lhs, const set& rhs) noexcept { if (lhs.s < rhs.s) { return true; } @@ -1065,7 +1061,7 @@ public: return false; } - for (auto &e : *index_type::values_v) { + for (auto& e : *index_type::values_v) { if (auto c = rhs.contains(e); c != lhs.contains(e)) { return c; } @@ -1073,11 +1069,11 @@ public: 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 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); } + [[nodiscard]] constexpr friend bool operator>=(const set& lhs, const set& rhs) noexcept { return !(lhs < rhs); } template size_type erase_if(Pred pred) { @@ -1093,13 +1089,12 @@ public: } //... -private: + private: container_type a; std::size_t s{}; }; - -template +template explicit set(V starter) -> set; } // namespace magic_enum::containers @@ -1107,50 +1102,42 @@ explicit set(V starter) -> set; namespace std { template -constexpr std::enable_if_t<(std::is_integral_v && I < magic_enum::enum_count()), V &> -get(magic_enum::containers::array &a) noexcept { +constexpr std::enable_if_t<(std::is_integral_v && I < magic_enum::enum_count()), V&> get(magic_enum::containers::array& a) noexcept { return a.a[I]; } template -constexpr std::enable_if_t<(std::is_integral_v && I < magic_enum::enum_count()), V &&> -get(magic_enum::containers::array &&a) noexcept { +constexpr std::enable_if_t<(std::is_integral_v && I < magic_enum::enum_count()), V&&> get(magic_enum::containers::array&& a) noexcept { return std::move(a.a[I]); } template -constexpr std::enable_if_t<(std::is_integral_v && I < magic_enum::enum_count()), const V &> -get(const magic_enum::containers::array &a) noexcept { +constexpr std::enable_if_t<(std::is_integral_v && I < magic_enum::enum_count()), const V&> get(const magic_enum::containers::array& a) noexcept { return a.a[I]; } template -constexpr std::enable_if_t<(std::is_integral_v && I < magic_enum::enum_count()), const V &&> -get(const magic_enum::containers::array &&a) noexcept { +constexpr std::enable_if_t<(std::is_integral_v && I < magic_enum::enum_count()), const V&&> get(const magic_enum::containers::array&& a) noexcept { return std::move(a.a[I]); } template -constexpr std::enable_if_t && magic_enum::enum_contains(Enum), V &> -get(magic_enum::containers::array &a) noexcept { +constexpr std::enable_if_t && magic_enum::enum_contains(Enum), V&> get(magic_enum::containers::array& a) noexcept { return a[Enum]; } template -constexpr std::enable_if_t && magic_enum::enum_contains(Enum), V &&> -get(magic_enum::containers::array &&a) noexcept { +constexpr std::enable_if_t && magic_enum::enum_contains(Enum), V&&> get(magic_enum::containers::array&& a) noexcept { return std::move(a[Enum]); } template -constexpr std::enable_if_t && magic_enum::enum_contains(Enum), const V &> -get(const magic_enum::containers::array &a) noexcept { +constexpr std::enable_if_t && magic_enum::enum_contains(Enum), const V&> get(const magic_enum::containers::array& a) noexcept { return a[Enum]; } template -constexpr std::enable_if_t && magic_enum::enum_contains(Enum), const V &&> -get(const magic_enum::containers::array &&a) noexcept { +constexpr std::enable_if_t && magic_enum::enum_contains(Enum), const V&&> get(const magic_enum::containers::array&& a) noexcept { return std::move(a[Enum]); } diff --git a/test/test_containers.cpp b/test/test_containers.cpp index 73145e5..7b7d44c 100644 --- a/test/test_containers.cpp +++ b/test/test_containers.cpp @@ -1,6 +1,7 @@ // Licensed under the MIT License . // SPDX-License-Identifier: MIT // Copyright (c) 2019 - 2023 Daniil Goncharov . +// Copyright (c) 2022 - 2023 Bela Schaum . // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal