From 33bde8be579ad303b74d836516d0ea3f648419d7 Mon Sep 17 00:00:00 2001 From: Guillaume Meunier Date: Sat, 26 Oct 2024 10:08:32 +0200 Subject: [PATCH] Add debug options: - Log driver version when starting - Add debug menu to offset the controller mesh positions/orientations in the lobby --- CMakeLists.txt | 1 + assets/xyz-arrows.glb | Bin 0 -> 21408 bytes client/application.cpp | 28 +- client/hardware.cpp | 36 + client/hardware.h | 7 +- client/render/imgui_impl.cpp | 4 +- client/render/imgui_impl.h | 2 + client/scenes/input_profile.cpp | 33 +- client/scenes/input_profile.h | 6 +- client/scenes/lobby.cpp | 101 ++- client/scenes/lobby.h | 16 + client/scenes/lobby_gui.cpp | 91 +++ common/wivrn_config.h.in | 1 + external/magic_enum_containers.hpp | 1170 ++++++++++++++++++++++++++++ 14 files changed, 1443 insertions(+), 53 deletions(-) create mode 100644 assets/xyz-arrows.glb create mode 100644 external/magic_enum_containers.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index aa582d40..f54f63e8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -45,6 +45,7 @@ option(WIVRN_FEATURE_STEAMVR_LIGHTHOUSE "Enable SteamVR Lighthouse driver" OFF) option(WIVRN_CHECK_CAPSYSNICE "Check if CAP_SYS_NICE is available at startup" ON) option(WIVRN_SHOW_IMGUI_DEMO_WINDOW "Show the Dear ImGui demo window in the client" OFF) +option(WIVRN_CLIENT_DEBUG_MENU "Enable the debug menu in the client" OFF) option(WIVRN_OPENXR_INSTALL_ABSOLUTE_RUNTIME_PATH OFF) diff --git a/assets/xyz-arrows.glb b/assets/xyz-arrows.glb new file mode 100644 index 0000000000000000000000000000000000000000..675e27ef98ee6dced1c7c2c39848a3650a0450dc GIT binary patch literal 21408 zcmeHNd6-qzk*~`xC?Fsp2s94`m!^69(#yMz*ea0#0Vf(2rNL%V=*I4L3og$rYK)0y zQR8Svqd0Df%NWN@BRmqsBqm13C?;bTjYfk@f-#%M8Ru7Z&v~!jeebl*Kl6R_eR=PD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CK8 z8U5LJjHy*x3HXv8qo2=Y_*032JwEIyR-*PDQ(jwZD(Nx$?Q;dj+A0`_fA$&U@Q>9K zqBZRijKe<_^9O=)_{Zug1{Q?4UFz2GX z&B2em)KrXA_ literal 0 HcmV?d00001 diff --git a/client/application.cpp b/client/application.cpp index 8c5a01a8..acdfcf20 100644 --- a/client/application.cpp +++ b/client/application.cpp @@ -359,6 +359,28 @@ VkBool32 application::vulkan_debug_report_callback( return VK_FALSE; } +std::string parse_driver_sersion(const vk::PhysicalDeviceProperties & p) +{ + // See https://github.com/SaschaWillems/vulkan.gpuinfo.org/blob/1e6ca6e3c0763daabd6a101b860ab4354a07f5d3/functions.php#L294 + switch (p.vendorID) + { + case 0x10de: // nvidia + return fmt::format( + "{}.{}.{}.{}", + (p.driverVersion >> 22) & 0x3ff, + (p.driverVersion >> 14) & 0xff, + (p.driverVersion >> 6) & 0xff, + p.driverVersion & 0x3f); + + default: + return fmt::format( + "{}.{}.{}", + VK_VERSION_MAJOR(p.driverVersion), + VK_VERSION_MINOR(p.driverVersion), + VK_VERSION_PATCH(p.driverVersion)); + } +} + void application::initialize_vulkan() { auto graphics_requirements = xr_system_id.graphics_requirements(); @@ -493,8 +515,10 @@ void application::initialize_vulkan() vk_device_extensions.push_back(it->data()); } - vk::PhysicalDeviceProperties prop = vk_physical_device.getProperties(); - spdlog::info("Initializing Vulkan with device {}", prop.deviceName.data()); + spdlog::info("Initializing Vulkan with device {}", physical_device_properties.deviceName.data()); + spdlog::info(" Vendor ID: 0x{:04x}", physical_device_properties.vendorID); + spdlog::info(" Device ID: 0x{:04x}", physical_device_properties.deviceID); + spdlog::info(" Driver version: {}", parse_driver_sersion(physical_device_properties)); std::vector queue_properties = vk_physical_device.getQueueFamilyProperties(); diff --git a/client/hardware.cpp b/client/hardware.cpp index 219c112f..f4f53238 100644 --- a/client/hardware.cpp +++ b/client/hardware.cpp @@ -233,6 +233,42 @@ std::string controller_name() __builtin_unreachable(); } +std::pair controller_offset(std::string_view profile, xr::spaces space) +{ + if (profile == "oculus-touch-v2") + switch (space) + { + case xr::spaces::grip_left: + case xr::spaces::grip_right: + return {{0, -0.006, -0.025}, glm::angleAxis(glm::radians(-15.f), glm::vec3{1, 0, 0})}; + + case xr::spaces::aim_left: + return {{-0.010, 0, 0.025}, {1, 0, 0, 0}}; + + case xr::spaces::aim_right: + return {{0.010, 0, 0.025}, {1, 0, 0, 0}}; + + default: + break; + } + else if (profile == "htc-vive-focus-3") + switch (space) + { + case xr::spaces::grip_left: + case xr::spaces::grip_right: + return {{0, 0.007, -0.030}, {1, 0, 0, 0}}; + + case xr::spaces::aim_left: + case xr::spaces::aim_right: + return {{0, -0.025, 0.005}, {1, 0, 0, 0}}; + + default: + break; + } + + return {{0, 0, 0}, {1, 0, 0, 0}}; +} + std::string controller_ray_model_name() { switch (guess_model()) diff --git a/client/hardware.h b/client/hardware.h index 200f395c..29bc375a 100644 --- a/client/hardware.h +++ b/client/hardware.h @@ -18,8 +18,12 @@ */ #pragma once -#include +#include "xr/space.h" +#include +#include #include +#include +#include enum class model { @@ -54,3 +58,4 @@ const char * permission_name(feature f); std::string controller_name(); std::string controller_ray_model_name(); +std::pair controller_offset(std::string_view profile, xr::spaces space); diff --git a/client/render/imgui_impl.cpp b/client/render/imgui_impl.cpp index f290a377..4324a337 100644 --- a/client/render/imgui_impl.cpp +++ b/client/render/imgui_impl.cpp @@ -653,8 +653,8 @@ std::vector imgui_context::read_controllers_sta if (auto location = application::locate_controller(ctrl.aim, world, display_time)) { new_state.active = true; - new_state.aim_position = location->first; - new_state.aim_orientation = location->second; + new_state.aim_position = location->first + glm::mat3_cast(location->second * ctrl.offset.second) * ctrl.offset.first; + new_state.aim_orientation = location->second * ctrl.offset.second; if (ctrl.trigger) { diff --git a/client/render/imgui_impl.h b/client/render/imgui_impl.h index c6c0d410..2138aada 100644 --- a/client/render/imgui_impl.h +++ b/client/render/imgui_impl.h @@ -60,6 +60,8 @@ public: struct controller { XrSpace aim; + std::pair offset; + XrAction trigger; // XR_ACTION_TYPE_FLOAT_INPUT XrAction squeeze; // XR_ACTION_TYPE_FLOAT_INPUT XrAction scroll; // XR_ACTION_TYPE_VECTOR2F_INPUT diff --git a/client/scenes/input_profile.cpp b/client/scenes/input_profile.cpp index 271c063f..9bb5ceee 100644 --- a/client/scenes/input_profile.cpp +++ b/client/scenes/input_profile.cpp @@ -212,15 +212,15 @@ input_profile::input_profile(const std::filesystem::path & json_profile, scene_l node_handle root_node = scene_controllers.new_node(); root_node->name = layout; - XrSpace space; + xr::spaces space; if (layout == "left") - space = application::space(xr::spaces::grip_left); + space = xr::spaces::grip_left; else if (layout == "right") - space = application::space(xr::spaces::grip_right); + space = xr::spaces::grip_right; else if (layout == "left_aim") - space = application::space(xr::spaces::aim_left); + space = xr::spaces::aim_left; else if (layout == "right_aim") - space = application::space(xr::spaces::aim_right); + space = xr::spaces::aim_right; else continue; @@ -231,11 +231,11 @@ input_profile::input_profile(const std::filesystem::path & json_profile, scene_l for (simdjson::dom::key_value_pair layout: simdjson::dom::object(root["layouts"])) { - XrSpace space; + xr::spaces space; if (layout.key == "left") - space = application::space(xr::spaces::aim_left); + space = xr::spaces::aim_left; else if (layout.key == "right") - space = application::space(xr::spaces::aim_right); + space = xr::spaces::aim_right; else continue; @@ -304,6 +304,8 @@ input_profile::input_profile(const std::filesystem::path & json_profile, scene_l response.target.node = scene_controllers.find_node(controller_root_node, json_response.target_node); response.target.state = json_response.state; } + + offset.fill({{0, 0, 0}, {1, 0, 0, 0}}); } static void apply_visual_response(node_handle node, std::pair transforms, float value) @@ -343,23 +345,28 @@ void input_profile::apply(XrSpace world_space, XrTime predicted_display_time, bo { for (auto && [space, node]: model_handles) { - if ((space == application::space(xr::spaces::grip_left) or space == application::space(xr::spaces::aim_left)) and hide_left) + if ((space == xr::spaces::grip_left or space == xr::spaces::aim_left) and hide_left) { node->visible = false; continue; } - if ((space == application::space(xr::spaces::grip_right) or space == application::space(xr::spaces::aim_right)) and hide_right) + if ((space == xr::spaces::grip_right or space == xr::spaces::aim_right) and hide_right) { node->visible = false; continue; } - if (auto location = application::locate_controller(space, world_space, predicted_display_time); location) + if (auto location = application::locate_controller(application::space(space), world_space, predicted_display_time); location) { node->visible = true; - node->position = location->first; - node->orientation = location->second; + + assert((int)space >= 0); + assert((int)space < offset.size()); + auto & [p, q] = offset[space]; + + node->position = location->first + glm::mat3_cast(location->second * q) * p; + node->orientation = location->second * q; } else { diff --git a/client/scenes/input_profile.h b/client/scenes/input_profile.h index 6dd14276..50062a69 100644 --- a/client/scenes/input_profile.h +++ b/client/scenes/input_profile.h @@ -20,9 +20,11 @@ #include #include +#include #include #include #include +#include #include struct input_profile @@ -59,10 +61,12 @@ struct input_profile std::string id; std::vector responses; - std::vector> model_handles; + std::vector> model_handles; node_handle left_ray; node_handle right_ray; + magic_enum::containers::array> offset; + input_profile(const std::filesystem::path & json_profile, scene_loader & loader, scene_data & scene_controllers, scene_data & scene_rays); // application::poll_actions() must have been called before diff --git a/client/scenes/lobby.cpp b/client/scenes/lobby.cpp index 4f34c8a1..a922efd3 100644 --- a/client/scenes/lobby.cpp +++ b/client/scenes/lobby.cpp @@ -61,39 +61,6 @@ scenes::lobby::~lobby() renderer->wait_idle(); } -static std::string choose_webxr_profile() -{ -#ifndef __ANDROID__ - const char * controller = std::getenv("WIVRN_CONTROLLER"); - if (controller && strcmp(controller, "")) - return controller; -#endif - - switch (guess_model()) - { - case model::oculus_quest: - return "oculus-touch-v2"; - case model::oculus_quest_2: - return "oculus-touch-v3"; - case model::meta_quest_pro: - return "meta-quest-touch-pro"; - case model::meta_quest_3: - return "meta-quest-touch-plus"; - case model::pico_neo_3: - return "pico-neo3"; - case model::pico_4: - return "pico-4"; - case model::htc_vive_focus_3: - case model::htc_vive_xr_elite: - return "htc-vive-focus-3"; - case model::lynx_r1: - case model::unknown: - return "generic-trigger-squeeze"; - } - - __builtin_unreachable(); -} - static const std::array supported_color_formats = { vk::Format::eR8G8B8A8Srgb, vk::Format::eB8G8R8A8Srgb, @@ -758,6 +725,41 @@ void scenes::lobby::render(const XrFrameState & frame_state) } } +#if WIVRN_CLIENT_DEBUG_MENU + if (display_debug_axes) + { + const xr::spaces left = display_grip_instead_of_aim ? xr::spaces::grip_left : xr::spaces::aim_left; + const xr::spaces right = display_grip_instead_of_aim ? xr::spaces::grip_right : xr::spaces::aim_right; + + if (hide_left_controller) + xyz_axes_left_controller->visible = false; + else if (auto location = application::locate_controller(application::space(left), world_space, frame_state.predictedDisplayTime)) + { + xyz_axes_left_controller->visible = true; + xyz_axes_left_controller->position = location->first; + xyz_axes_left_controller->orientation = location->second; + } + else + xyz_axes_left_controller->visible = false; + + if (hide_right_controller) + xyz_axes_right_controller->visible = false; + else if (auto location = application::locate_controller(application::space(right), world_space, frame_state.predictedDisplayTime)) + { + xyz_axes_right_controller->visible = true; + xyz_axes_right_controller->position = location->first; + xyz_axes_right_controller->orientation = location->second; + } + else + xyz_axes_right_controller->visible = false; + } + else + { + xyz_axes_left_controller->visible = false; + xyz_axes_right_controller->visible = false; + } +#endif + if (head_position && new_gui_position) { move_gui(head_position->first, *new_gui_position); @@ -996,14 +998,43 @@ void scenes::lobby::on_focused() controllers_scene.emplace(); scene_data & controllers_scene_data = composition_layer_depth_test_supported ? *lobby_scene : *controllers_scene; + + auto profile = controller_name(); input = input_profile( - "controllers/" + choose_webxr_profile() + "/profile.json", + "controllers/" + profile + "/profile.json", loader, controllers_scene_data, *controllers_scene); spdlog::info("Loaded input profile {}", input->id); + for (auto i: {xr::spaces::aim_left, xr::spaces::aim_right, xr::spaces::grip_left, xr::spaces::grip_right}) + { + auto [p, q] = input->offset[i] = controller_offset(controller_name(), i); + + auto rot = glm::degrees(glm::eulerAngles(q)); + spdlog::info("Initializing offset of space {} to ({}, {}, {}) mm, ({}, {}, {})°", + magic_enum::enum_name(i), + 1000 * p.x, + 1000 * p.y, + 1000 * p.z, + rot.x, + rot.y, + rot.z); + } + +#if WIVRN_CLIENT_DEBUG_MENU + offset_position = input->offset[xr::spaces::grip_left].first; + offset_orientation = glm::degrees(glm::eulerAngles(input->offset[xr::spaces::grip_left].second)); + ray_offset = input->offset[xr::spaces::aim_left].first.z; + + xyz_axes_left_controller = controllers_scene_data.new_node(); + controllers_scene_data.import(loader("xyz-arrows.glb"), xyz_axes_left_controller); + + xyz_axes_right_controller = controllers_scene_data.new_node(); + controllers_scene_data.import(loader("xyz-arrows.glb"), xyz_axes_right_controller); +#endif + if (application::get_hand_tracking_supported()) { left_hand.emplace("left-hand.glb", loader, controllers_scene_data); @@ -1016,12 +1047,14 @@ void scenes::lobby::on_focused() std::vector imgui_inputs{ imgui_context::controller{ .aim = get_action_space("left_aim"), + .offset = input->offset[xr::spaces::aim_left], .trigger = get_action("left_trigger").first, .squeeze = get_action("left_squeeze").first, .scroll = get_action("left_scroll").first, }, imgui_context::controller{ .aim = get_action_space("right_aim"), + .offset = input->offset[xr::spaces::aim_right], .trigger = get_action("right_trigger").first, .squeeze = get_action("right_squeeze").first, .scroll = get_action("right_scroll").first, diff --git a/client/scenes/lobby.h b/client/scenes/lobby.h index 2d90ff37..2d89685a 100644 --- a/client/scenes/lobby.h +++ b/client/scenes/lobby.h @@ -26,6 +26,7 @@ #include "scenes/hand_model.h" #include "scenes/lobby_keyboard.h" #include "wifi_lock.h" +#include "wivrn_config.h" #include "wivrn_discover.h" #include @@ -93,6 +94,17 @@ class lobby : public scene_impl xr::passthrough passthrough; XrViewConfigurationView stream_view; +#if WIVRN_CLIENT_DEBUG_MENU + // GUI debug + node_handle xyz_axes_left_controller; + node_handle xyz_axes_right_controller; + bool display_debug_axes = false; + bool display_grip_instead_of_aim = false; + glm::vec3 offset_position{}; + glm::vec3 offset_orientation{}; + float ray_offset{}; +#endif + void update_server_list(); std::vector> draw_gui(XrTime predicted_display_time); @@ -113,6 +125,9 @@ class lobby : public scene_impl { server_list, settings, +#if WIVRN_CLIENT_DEBUG_MENU + debug, +#endif about, licenses, exit @@ -129,6 +144,7 @@ class lobby : public scene_impl void gui_server_list(); void gui_new_server(); void gui_settings(); + void gui_debug(); void gui_about(); void gui_licenses(); void gui_keyboard(); diff --git a/client/scenes/lobby_gui.cpp b/client/scenes/lobby_gui.cpp index cf2baf64..797f8ff7 100644 --- a/client/scenes/lobby_gui.cpp +++ b/client/scenes/lobby_gui.cpp @@ -602,6 +602,68 @@ void scenes::lobby::gui_settings() } } +#if WIVRN_CLIENT_DEBUG_MENU +void scenes::lobby::gui_debug() +{ + ImGui::GetIO().ConfigDragClickToInputText = true; + + ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(20, 20)); + + ImGui::Checkbox("Display debug axes", &display_debug_axes); + vibrate_on_hover(); + + if (display_debug_axes) + { + ImGui::Checkbox("Display grip instead of aim", &display_grip_instead_of_aim); + vibrate_on_hover(); + } + + ImGui::SetNextItemWidth(140); + ImGui::DragFloat("##offset x", &offset_position.x, 0.0001); + vibrate_on_hover(); + + ImGui::SameLine(); + ImGui::SetNextItemWidth(140); + ImGui::DragFloat("##offset y", &offset_position.y, 0.0001); + vibrate_on_hover(); + + ImGui::SameLine(); + ImGui::SetNextItemWidth(140); + ImGui::DragFloat("Position", &offset_position.z, 0.0001); + vibrate_on_hover(); + + ImGui::SameLine(); + if (ImGui::Button("Reset##position")) + offset_position = {0, 0, 0}; + vibrate_on_hover(); + + ImGui::SetNextItemWidth(140); + ImGui::DragFloat("##offset roll", &offset_orientation.x, 0.01); + vibrate_on_hover(); + + ImGui::SameLine(); + ImGui::SetNextItemWidth(140); + ImGui::DragFloat("##offset pitch", &offset_orientation.y, 0.01); + vibrate_on_hover(); + + ImGui::SameLine(); + ImGui::SetNextItemWidth(140); + ImGui::DragFloat("Rotation", &offset_orientation.z, 0.01); + vibrate_on_hover(); + + ImGui::SameLine(); + if (ImGui::Button("Reset##orientation")) + offset_orientation = {0, 0, 0}; + vibrate_on_hover(); + + ImGui::SetNextItemWidth(140); + ImGui::DragFloat("Ray offset", &ray_offset, 0.0001); + vibrate_on_hover(); + + ImGui::PopStyleVar(); +} +#endif + void scenes::lobby::gui_about() { ImGui::PushFont(imgui_ctx->large_font); @@ -1194,6 +1256,12 @@ std::vector> scenes::lobby::draw_gui(XrTi gui_settings(); break; +#if WIVRN_CLIENT_DEBUG_MENU + case tab::debug: + gui_debug(); + break; +#endif + case tab::about: gui_about(); break; @@ -1232,6 +1300,11 @@ std::vector> scenes::lobby::draw_gui(XrTi RadioButtonWithoutCheckBox(ICON_FA_GEARS " " + _("Settings"), ¤t_tab, tab::settings, {TabWidth, 0}); vibrate_on_hover(); +#if WIVRN_CLIENT_DEBUG_MENU + RadioButtonWithoutCheckBox(ICON_FA_BUG_SLASH " " + _("Debug"), ¤t_tab, tab::debug, {TabWidth, 0}); + vibrate_on_hover(); +#endif + ImGui::SetCursorPosY(ImGui::GetContentRegionMax().y - 3 * ImGui::GetCurrentContext()->FontSize - 6 * style.FramePadding.y - 2 * style.ItemSpacing.y - style.WindowPadding.y); RadioButtonWithoutCheckBox(ICON_FA_CIRCLE_INFO " " + _("About"), ¤t_tab, tab::about, {TabWidth, 0}); vibrate_on_hover(); @@ -1265,5 +1338,23 @@ std::vector> scenes::lobby::draw_gui(XrTi application::haptic_start(haptic_output[controller], XR_NULL_PATH, 10'000'000, 1000, 1); } +#if WIVRN_CLIENT_DEBUG_MENU + { + input->offset[xr::spaces::grip_left].first = offset_position; + input->offset[xr::spaces::grip_right].first = offset_position; + + glm::quat qx = glm::quat(std::cos(offset_orientation.x * M_PI / 360), sin(offset_orientation.x * M_PI / 360), 0, 0); + glm::quat qy = glm::quat(std::cos(offset_orientation.y * M_PI / 360), 0, sin(offset_orientation.y * M_PI / 360), 0); + glm::quat qz = glm::quat(std::cos(offset_orientation.z * M_PI / 360), 0, 0, sin(offset_orientation.z * M_PI / 360)); + glm::quat q = qz * qy * qx; + + input->offset[xr::spaces::grip_left].second = q; + input->offset[xr::spaces::grip_right].second = q; + + input->offset[xr::spaces::aim_left].first.z = ray_offset; + input->offset[xr::spaces::aim_right].first.z = ray_offset; + } +#endif + return imgui_ctx->end_frame(); } diff --git a/common/wivrn_config.h.in b/common/wivrn_config.h.in index 9355dc04..0879b051 100644 --- a/common/wivrn_config.h.in +++ b/common/wivrn_config.h.in @@ -33,5 +33,6 @@ #cmakedefine01 WIVRN_FEATURE_SOLARXR #cmakedefine01 WIVRN_SHOW_IMGUI_DEMO_WINDOW +#cmakedefine01 WIVRN_CLIENT_DEBUG_MENU #define WIVRN_INSTALL_PREFIX "@CMAKE_INSTALL_PREFIX@" diff --git a/external/magic_enum_containers.hpp b/external/magic_enum_containers.hpp new file mode 100644 index 00000000..870f1586 --- /dev/null +++ b/external/magic_enum_containers.hpp @@ -0,0 +1,1170 @@ +// __ __ _ ______ _____ +// | \/ | (_) | ____| / ____|_ _ +// | \ / | __ _ __ _ _ ___ | |__ _ __ _ _ _ __ ___ | | _| |_ _| |_ +// | |\/| |/ _` |/ _` | |/ __| | __| | '_ \| | | | '_ ` _ \ | | |_ _|_ _| +// | | | | (_| | (_| | | (__ | |____| | | | |_| | | | | | | | |____|_| |_| +// |_| |_|\__,_|\__, |_|\___| |______|_| |_|\__,_|_| |_| |_| \_____| +// __/ | https://github.com/Neargye/magic_enum +// |___/ version 0.9.5 +// +// 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 +// 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)) +# include +# define MAGIC_ENUM_THROW(...) throw (__VA_ARGS__) +#else +# include +# define MAGIC_ENUM_THROW(...) std::abort() +#endif + +namespace magic_enum::containers { + +namespace detail { + +template +static constexpr bool is_transparent_v{}; + +template +static constexpr bool is_transparent_v>{true}; + +template , 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 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 +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 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 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 = void> +class indexing { + [[nodiscard]] static constexpr auto get_indices() noexcept { + // reverse result index mapping + std::array()> rev_res{}; + + // std::iota + for (std::size_t i = 0; i < enum_count(); ++i) { + rev_res[i] = i; + } + + constexpr auto orig_values = enum_values(); + constexpr Cmp cmp{}; + + // ~std::sort + for (std::size_t i = 0; i < enum_count(); ++i) { + for (std::size_t j = i + 1; j < enum_count(); ++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()> sorted_values{}; + // reverse the sorted indices + std::array()> res{}; + for (std::size_t i = 0; i < enum_count(); ++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 at(E val) noexcept { + if (auto i = enum_index(val)) { + return indices.second[*i]; + } + return {}; + } +}; + +template +class indexing> && (std::is_same_v> || std::is_same_v>)>> { + static constexpr auto& values = enum_values(); + + 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 at(E val) noexcept { return enum_index(val); } +}; + +template +struct indexing { + using is_transparent = std::true_type; + + template + [[nodiscard]] static constexpr optional at(E val) noexcept { + return indexing::at(val); + } +}; + +template , 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 +struct name_sort_impl { + using is_transparent = std::true_type; + + template + struct FullCmp : C {}; + + template + struct FullCmp && std::is_invocable_v>> { + [[nodiscard]] constexpr bool operator()(string_view s1, string_view s2) const noexcept { return lexicographical_compare(s1, s2); } + }; + + template + [[nodiscard]] constexpr std::enable_if_t< + // at least one of need to be an enum type + (std::is_enum_v> || std::is_enum_v>) && + // if both is enum, only accept if the same enum + (!std::is_enum_v> || !std::is_enum_v> || std::is_same_v) && + // is invocable with comparator + (std::is_invocable_r_v, std::conditional_t>, string_view, E1>, std::conditional_t>, string_view, E2>>), + bool> + operator()(E1 e1, E2 e2) const noexcept { + using D1 = std::decay_t; + using D2 = std::decay_t; + constexpr FullCmp<> cmp{}; + + if constexpr (std::is_enum_v && std::is_enum_v) { + return cmp(enum_name(e1), enum_name(e2)); + } else if constexpr (std::is_enum_v) { + return cmp(enum_name(e1), e2); + } else /* if constexpr (std::is_enum_v) */ { + return cmp(e1, enum_name(e2)); + } + } +}; + +struct raw_access_t {}; + +template +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>; + 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 && 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) {} + + constexpr FilteredIterator(Parent p, Iterator begin, Iterator end, Iterator curr, Getter getter = {}, Predicate pred = {}) + : parent(p), first(std::move(begin)), last(std::move(end)), current(std::move(curr)), getter{std::move(getter)}, 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 +using name_less = detail::name_sort_impl; + +template +using name_greater = detail::name_sort_impl>; + +using name_less_case_insensitive = detail::name_sort_impl>>; + +using name_greater_case_insensitive = detail::name_sort_impl>>; + +template +using default_indexing = detail::indexing; + +template > +using comparator_indexing = detail::indexing; + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// ARRAY // +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +template > +struct array { + static_assert(std::is_enum_v); + static_assert(std::is_trivially_constructible_v); + static_assert(enum_count() == 0 || Index::at(enum_values().front())); // check Index is constexpr + + using index_type = Index; + using container_type = std::array()>; + + 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) { + 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 +constexpr array> to_array_impl(T (&a)[N], std::index_sequence) { + return {{a[I]...}}; +} + +template +constexpr array> to_array_impl(T(&&a)[N], std::index_sequence) { + return {{std::move(a[I])...}}; +} + +} // namespace detail + +template +constexpr std::enable_if_t<(enum_count() == N), array>> to_array(T (&a)[N]) { + return detail::to_array_impl(a, std::make_index_sequence{}); +} + +template +constexpr std::enable_if_t<(enum_count() == N), array>> to_array(T(&&a)[N]) { + return detail::to_array_impl(std::move(a), std::make_index_sequence{}); +} + +template +constexpr std::enable_if_t<(enum_count() == sizeof...(Ts)), array>>> make_array(Ts&&... ts) { + return {{std::forward(ts)...}}; +} + +inline constexpr detail::raw_access_t raw_access{}; + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// BITSET // +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +template > +class bitset { + static_assert(std::is_enum_v); + static_assert(std::is_trivially_constructible_v); + static_assert(enum_count() == 0 || Index::at(enum_values().front())); // check Index is constexpr + + using base_type = std::conditional_t() <= 8, std::uint_least8_t, + std::conditional_t() <= 16, std::uint_least16_t, + std::conditional_t() <= 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() > 0 ? (enum_count() - 1) / bits_per_base + 1 : 0); + static constexpr std::size_t not_interested = base_type_count * bits_per_base - enum_count(); + static constexpr base_type last_value_max = (base_type{1} << (bits_per_base - not_interested)) - 1; + + template + class reference_impl { + friend class bitset; + + parent_t parent; + 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::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 { + 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(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(*this); } + + constexpr reference_impl& flip() noexcept { + *this = ~*this; + return *this; + } + }; + + template + [[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; + using reference = reference_impl<>; + using const_reference = reference_impl; + + 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()) { + 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('0'), char_type one = static_cast('1')) + : a{{}} { + std::size_t i = 0; + for (auto c : sv.substr(pos, n)) { + if (c == one) { + if (i >= enum_count()) { + 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('0'), char_type one = static_cast('1')) + : bitset(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) { + *this |= bitset(f); + } + } else { + for (auto& f : starters) { + set(f); + } + } + } + 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()) { + 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 > + constexpr explicit bitset(string_view sv, Cmp&& cmp = {}, char_type sep = static_cast('|')) { + 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(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(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(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(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(); } + + [[nodiscard]] constexpr std::size_t max_size() const noexcept { return enum_count(); } + + 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 + [[nodiscard]] constexpr explicit operator std::enable_if_t == magic_enum::detail::enum_subtype::flags, E>() const { + E res{}; + for (const auto& e : enum_values()) { + if (test(e)) { + res |= e; + } + } + return res; + } + + [[nodiscard]] string to_string(char_type sep = static_cast('|')) const { + string name; + + for (const auto& e : enum_values()) { + 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('0'), char_type one = static_cast('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_(raw); } + + [[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::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 +explicit bitset(V starter) -> bitset; + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// SET // +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +template > +class set { + using index_type = detail::indexing; + 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; + 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; + using const_iterator = detail::FilteredIterator; + using reverse_iterator = std::reverse_iterator; + using const_reverse_iterator = std::reverse_iterator; + + constexpr set() noexcept = default; + + template + constexpr set(InputIt first, InputIt last) { + while (first != last) { + insert(*first++); + } + } + + constexpr set(std::initializer_list ilist) { + for (auto e : ilist) { + insert(e); + } + } + 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()) { + 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 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 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 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 + constexpr void insert(InputIt first, InputIt last) noexcept { + while (first != last) { + insert(*first++); + } + } + + constexpr void insert(std::initializer_list ilist) noexcept { + for (auto v : ilist) { + insert(v); + } + } + + template + 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 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 + constexpr std::enable_if_t, 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 + [[nodiscard]] constexpr std::enable_if_t, 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 + [[nodiscard]] constexpr std::enable_if_t, 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 + [[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)}; } + + template + [[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 { + 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 + [[nodiscard]] constexpr std::enable_if_t, 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 + [[nodiscard]] constexpr std::enable_if_t, 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 + 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 +explicit set(V starter) -> set; + +template +constexpr std::enable_if_t<(std::is_integral_v && I < enum_count()), V&> get(array& a) noexcept { + return a.a[I]; +} + +template +constexpr std::enable_if_t<(std::is_integral_v && I < enum_count()), V&&> get(array&& a) noexcept { + return std::move(a.a[I]); +} + +template +constexpr std::enable_if_t<(std::is_integral_v && I < enum_count()), const V&> get(const array& a) noexcept { + return a.a[I]; +} + +template +constexpr std::enable_if_t<(std::is_integral_v && I < enum_count()), const V&&> get(const array&& a) noexcept { + return std::move(a.a[I]); +} + +template +constexpr std::enable_if_t && enum_contains(Enum), V&> get(array& a) { + return a[Enum]; +} + +template +constexpr std::enable_if_t && enum_contains(Enum), V&&> get(array&& a) { + return std::move(a[Enum]); +} + +template +constexpr std::enable_if_t && enum_contains(Enum), const V&> get(const array& a) { + return a[Enum]; +} + +template +constexpr std::enable_if_t && enum_contains(Enum), const V&&> get(const array&& a) { + return std::move(a[Enum]); +} + +} // namespace magic_enum::containers + +#endif // NEARGYE_MAGIC_ENUM_CONTAINERS_HPP