Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Hashing sf::Event  (Read 1450 times)

0 Members and 1 Guest are viewing this topic.

burikko

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Hashing sf::Event
« on: August 02, 2018, 01:54:37 pm »
I wish to be able to hash sf::Event in a reliable manner, because I want to use events as keys in std::unordered_map. This is the best I've come up with so far:

int sfhash(const sf::Event & e) {
    int seed = 1000000 * e.type;
    int mod = 1;
    switch (e.type) {
        case sf::Event::KeyPressed:
            seed = seed + e.key.code;
            break;

        case sf::Event::KeyReleased:
            seed = -seed - e.key.code;
            mod = -1;
            break;

        case sf::Event::MouseButtonPressed:
            seed = seed + e.mouseButton.button;
            break;

        case sf::Event::MouseButtonReleased:
            seed = -seed - e.mouseButton.button;
            mod = -1;
            break;

        default:
            break;
    }
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::LControl)
        || sf::Keyboard::isKeyPressed(sf::Keyboard::RControl))
    {
        seed += mod * 1000;
    }
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::LShift)
        || sf::Keyboard::isKeyPressed(sf::Keyboard::RShift))
    {
        seed += mod * 2000;
    }
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::LAlt)) {
        seed += mod * 3000;
    }
    return seed;
}

Can you tell me a reason why this will fail, or if there is a much simpler way to do it that I didn't think of?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Hashing sf::Event
« Reply #1 on: August 02, 2018, 02:06:56 pm »
To implement a hash function for an aggregate type, a typical solution is simply to std::hash all the members, and combine the resulting hashes with a XOR based formula that you'll find easily on Google.

Reading the keyboard state in a function that operates on a sf::Event instance that may have been produced long ago, is a very bad idea. All the information about modifier keys is inside sf::Event, use that instead.
Laurent Gomila - SFML developer

burikko

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Hashing sf::Event
« Reply #2 on: August 02, 2018, 03:04:07 pm »
I actually read about that on Google before trying it myself, but since sf::Event seems to use union for its members I don't know how to hash all the members like you say. Good point about the modifiers!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Hashing sf::Event
« Reply #3 on: August 05, 2018, 09:15:52 pm »
You must still have that switch/case to know what struct of the sf::Event union is the one you must read. But then it's just a few primitive types that you can combine using well known formulas.
Laurent Gomila - SFML developer

 

anything