Add hb_hash()

I don't like the hb_remove_reference() hack, but necessary.
This commit is contained in:
Behdad Esfahbod 2019-03-28 21:58:07 -07:00
parent 343e6063dc
commit bdd5a9c48d
3 changed files with 19 additions and 10 deletions

View File

@ -28,9 +28,26 @@
#define HB_ALGS_HH #define HB_ALGS_HH
#include "hb.hh" #include "hb.hh"
#include "hb-meta.hh"
#include "hb-null.hh" #include "hb-null.hh"
static const struct
{
template <typename T,
hb_enable_if (hb_is_integer (T))>
uint32_t operator () (T v) const
{
/* Knuth's multiplicative method: */
return (uint32_t) v * 2654435761u;
}
template <typename T>
uint32_t operator () (T *v) const { return hb_hash (*v); }
template <typename T,
hb_enable_if (!hb_is_integer (hb_remove_reference (T)) && !hb_is_pointer (T))>
uint32_t operator () (T&& v) const { return v.hash (); }
} hb_hash HB_UNUSED;
static const struct static const struct
{ {
template <typename T> T template <typename T> T

View File

@ -30,14 +30,6 @@
#include "hb.hh" #include "hb.hh"
template <typename T>
inline uint32_t Hash (const T &v)
{
/* Knuth's multiplicative method: */
return (uint32_t) v * 2654435761u;
}
/* /*
* hb_map_t * hb_map_t
*/ */
@ -184,7 +176,7 @@ struct hb_map_t
unsigned int bucket_for (hb_codepoint_t key) const unsigned int bucket_for (hb_codepoint_t key) const
{ {
unsigned int i = Hash (key) % prime; unsigned int i = hb_hash (key) % prime;
unsigned int step = 0; unsigned int step = 0;
unsigned int tombstone = INVALID; unsigned int tombstone = INVALID;
while (!items[i].is_unused ()) while (!items[i].is_unused ())

View File

@ -639,7 +639,7 @@ _hb_memalign(void **memptr, size_t alignment, size_t size)
#include "hb-mutex.hh" #include "hb-mutex.hh"
#include "hb-atomic.hh" // Requires: hb-meta #include "hb-atomic.hh" // Requires: hb-meta
#include "hb-null.hh" // Requires: hb-meta #include "hb-null.hh" // Requires: hb-meta
#include "hb-algs.hh" // Requires: hb-null #include "hb-algs.hh" // Requires: hb-meta hb-null
#include "hb-iter.hh" // Requires: hb-meta #include "hb-iter.hh" // Requires: hb-meta
#include "hb-debug.hh" // Requires: hb-algs hb-atomic #include "hb-debug.hh" // Requires: hb-algs hb-atomic
#include "hb-array.hh" // Requires: hb-algs hb-iter hb-null #include "hb-array.hh" // Requires: hb-algs hb-iter hb-null