Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enum_fuse seems to generate duplicate values #386

Closed
bshm opened this issue Nov 2, 2024 · 1 comment · Fixed by #387
Closed

enum_fuse seems to generate duplicate values #386

bshm opened this issue Nov 2, 2024 · 1 comment · Fixed by #387
Milestone

Comments

@bshm
Copy link
Contributor

bshm commented Nov 2, 2024

Hi,

The following code fails with both static assertion indicating that enum_fuse does not work as expected:
If you uncomment the assertions you see that both value1 and value2 evaluate to 4;
Expected behavior: enum_fuse generates distinct values for every combination of input parameters
Actual behavior: enum_fuse sometimes returns the identical values

Seen on latest master and on v0.9.6
https://godbolt.org/z/PP5v76Pdq

#include <iostream>
#include "magic_enum.hpp"
#include "magic_enum_fuse.hpp"

enum class Continent
{
    EUROPE,
    ASIA,
    AFRICA,
    AMERICA,
    AUSTRALIA,
};

enum class Color
{
    RED,
    GREEN,
};

static_assert(magic_enum::enum_fuse(Continent::AUSTRALIA, Continent::EUROPE).value() !=
              magic_enum::enum_fuse(Continent::EUROPE, Continent::ASIA).value());

static_assert(magic_enum::enum_fuse(Continent::AUSTRALIA, Color::RED).value() !=
              magic_enum::enum_fuse(Continent::EUROPE, Color::GREEN).value());

int main() {

    auto value1 = magic_enum::enum_integer(magic_enum::enum_fuse(Continent::AUSTRALIA, Color::RED).value());
    auto value2 = magic_enum::enum_integer(magic_enum::enum_fuse(Continent::EUROPE, Color::GREEN).value());
    std::cout << "value1: " << value1 << std::endl;
    std::cout << "value2: " << value2 << std::endl;

    return 0;
}

CMakeLists.txt for minimal example

cmake_minimum_required(VERSION 3.14)
project(MagicEnumExample)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)
include(FetchContent)
FetchContent_Declare(
    magic_enum
    GIT_REPOSITORY https://github.com/Neargye/magic_enum.git
    GIT_TAG        master
)
FetchContent_MakeAvailable(magic_enum)
include_directories(${magic_enum_SOURCE_DIR}/include/magic_enum)
add_executable(MagicEnumExample main.cpp)
target_link_libraries(MagicEnumExample PRIVATE magic_enum::magic_enum)
@bshm
Copy link
Contributor Author

bshm commented Nov 2, 2024

Ok, likely the shift value is slightly miscalculated.

log2(enum_count<E>() + 1)) returns 2 (instead of 3) e.g. for enum of size 5 and 6.
So I suggest to use $log_2(2*count - 1)$.
log2((enum_count<E>() << 1) - 1))
instead

template <typename E>
constexpr optional<std::uintmax_t> fuse_one_enum(optional<std::uintmax_t> hash, E value) noexcept {
  if (hash) {
    if (const auto index = enum_index(value)) {
      return (*hash << log2(enum_count<E>() + 1)) | *index;
    }
  }
  return {};
}

needs to be

template <typename E>
constexpr optional<std::uintmax_t> fuse_one_enum(optional<std::uintmax_t> hash, E value) noexcept {
  if (hash) {
    if (const auto index = enum_index(value)) {
      return (*hash << log2((enum_count<E>() << 1) - 1)) | *index;
    }
  }
  return {};
}

bshm pushed a commit to bshm/magic_enum that referenced this issue Nov 2, 2024
@Neargye Neargye added this to the v0.9.7 milestone Nov 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants