Maps a (position, key) pair to an opaque 32-bit value. The position acts as a salt, so the same key at different positions maps to different values (which keeps the configured step values from revealing repeats). constexpr so the comparison constants fold at compile time.
| 14 | // keeps the configured step values from revealing repeats). constexpr so the |
| 15 | // comparison constants fold at compile time. |
| 16 | constexpr std::uint32_t stepHash(std::size_t pos, int key) { |
| 17 | constexpr std::uint32_t kOffset = 0x811c9dc5u; |
| 18 | constexpr std::uint32_t kPrime = 0x01000193u; |
| 19 | constexpr std::uint32_t kSalt = 0x9e3779b9u; |
| 20 | std::uint32_t h = kOffset; |
| 21 | h = (h ^ (static_cast<std::uint32_t>(pos) + kSalt)) * kPrime; |
| 22 | h = (h ^ static_cast<std::uint32_t>(key)) * kPrime; |
| 23 | return h; |
| 24 | } |
| 25 | |
| 26 | // A tiny fixed-sequence detector. It is configured with one step value per |
| 27 | // position (see stepHash). Feed key codes one at a time; feed() returns true |