Files

82 lines
2.1 KiB
C
Raw Permalink Normal View History

2026-03-01 12:16:08 +08:00
#pragma once
2026-03-02 14:51:05 +07:00
#include <cstdint>
2026-03-01 12:16:08 +08:00
// Java doesn't have a default hash value for ints, however, the hashmap itself does some "supplemental" hashing, so
// our ints actually get hashed by code as implemented below. std templates *do* have a standard hash for ints, but it
// would appear to be a bit expensive so matching the java one for now anyway. This code implements the supplemental
// hashing that happens in java so we can match what their maps are doing with ints.
2026-03-02 14:51:05 +07:00
struct IntKeyHash
2026-03-01 12:16:08 +08:00
{
2026-03-02 14:51:05 +07:00
inline int operator()(const int &k) const
2026-03-01 12:16:08 +08:00
{
int h = k;
h += ~(h << 9);
h ^= (((unsigned int)h) >> 14);
h += (h << 4);
h ^= (((unsigned int)h) >> 10);
return h;
}
2026-03-02 14:51:05 +07:00
};
2026-03-01 12:16:08 +08:00
2026-03-02 14:51:05 +07:00
struct IntKeyEq
2026-03-01 12:16:08 +08:00
{
2026-03-02 14:51:05 +07:00
inline bool operator()(const int &x, const int &y) const
{ return x==y; }
};
2026-03-01 12:16:08 +08:00
// This hash functor is taken from the IntHashMap java class used by the game, so that we can use a standard std hashmap with this hash rather
// than implement the class itself
2026-03-02 14:51:05 +07:00
struct IntKeyHash2
2026-03-01 12:16:08 +08:00
{
2026-03-02 14:51:05 +07:00
inline int operator()(const int &k) const
2026-03-01 12:16:08 +08:00
{
2026-03-02 14:51:05 +07:00
unsigned int h = static_cast<unsigned int>(k);
2026-03-01 12:16:08 +08:00
h ^= (h >> 20) ^ (h >> 12);
2026-03-02 14:51:05 +07:00
return static_cast<int>(h ^ (h >> 7) ^ (h >> 4));
2026-03-01 12:16:08 +08:00
}
2026-03-02 14:51:05 +07:00
};
2026-03-01 12:16:08 +08:00
// This hash functor is taken from the LongHashMap java class used by the game, so that we can use a standard std hashmap with this hash rather
// than implement the class itself
2026-03-02 14:51:05 +07:00
struct LongKeyHash
2026-03-01 12:16:08 +08:00
{
2026-03-02 14:51:05 +07:00
inline int hash(const int &k) const
2026-03-01 12:16:08 +08:00
{
2026-03-02 14:51:05 +07:00
unsigned int h = static_cast<unsigned int>(k);
2026-03-01 12:16:08 +08:00
h ^= (h >> 20) ^ (h >> 12);
2026-03-02 14:51:05 +07:00
return static_cast<int>(h ^ (h >> 7) ^ (h >> 4));
2026-03-01 12:16:08 +08:00
}
2026-03-02 14:51:05 +07:00
inline int operator()(const int64_t &k) const
2026-03-01 12:16:08 +08:00
{
2026-03-02 14:51:05 +07:00
return hash(static_cast<int>(k ^ ((static_cast<uint64_t>(k)) >> 32)));
2026-03-01 12:16:08 +08:00
}
2026-03-02 14:51:05 +07:00
};
2026-03-01 12:16:08 +08:00
2026-03-02 14:51:05 +07:00
struct LongKeyEq
2026-03-01 12:16:08 +08:00
{
2026-03-02 14:51:05 +07:00
inline bool operator() (const int64_t &x, const int64_t &y) const
{ return x == y; }
};
2026-03-01 12:16:08 +08:00
2026-03-02 14:51:05 +07:00
enum eINSTANCEOF;
struct eINSTANCEOFKeyHash
2026-03-01 12:16:08 +08:00
{
2026-03-02 14:51:05 +07:00
int operator()(const eINSTANCEOF &k) const
2026-03-01 12:16:08 +08:00
{
2026-03-02 14:51:05 +07:00
unsigned int h = static_cast<unsigned int>(k);
2026-03-01 12:16:08 +08:00
h ^= (h >> 20) ^ (h >> 12);
2026-03-02 14:51:05 +07:00
return static_cast<int>(h ^ (h >> 7) ^ (h >> 4));
2026-03-01 12:16:08 +08:00
}
2026-03-02 14:51:05 +07:00
};
2026-03-01 12:16:08 +08:00
2026-03-02 14:51:05 +07:00
struct eINSTANCEOFKeyEq
2026-03-01 12:16:08 +08:00
{
2026-03-02 14:51:05 +07:00
inline bool operator()(const eINSTANCEOF &x, const eINSTANCEOF &y) const
{ return x == y; }
};
2026-03-01 12:16:08 +08:00