From b189bbc48fb4b7c251d30b26a57ad84d1cb6dbe4 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 30 Mar 2019 19:41:48 -0700 Subject: [PATCH] Implement hashing of objects Should be improved for hb_bytes_t. --- src/hb-algs.hh | 2 +- src/hb-array.hh | 13 +++++++++++++ src/hb-serialize.hh | 5 +++++ src/hb-vector.hh | 5 +++++ 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/hb-algs.hh b/src/hb-algs.hh index c1b38e8..adf5c48 100644 --- a/src/hb-algs.hh +++ b/src/hb-algs.hh @@ -40,7 +40,7 @@ static const struct //{ return hb_deref_pointer (v).hash (); } /* Instead, the following ugly soution: */ template + hb_enable_if (!hb_is_integer (hb_remove_const (hb_remove_reference (T))) && !hb_is_pointer (T))> uint32_t operator () (T&& v) const { return v.hash (); } template diff --git a/src/hb-array.hh b/src/hb-array.hh index ab453f5..16f53cc 100644 --- a/src/hb-array.hh +++ b/src/hb-array.hh @@ -79,6 +79,8 @@ struct hb_array_t : hb_iter_with_fallback_t, Type&> operator hb_array_t () { return hb_array_t (arrayZ, length); } template operator T * () const { return arrayZ; } + uint32_t hash () const; + /* * Compare, Sort, and Search. */ @@ -273,9 +275,20 @@ template inline hb_sorted_array_t hb_sorted_array (T (&array_)[length_]) { return hb_sorted_array_t (array_); } +template +uint32_t hb_array_t::hash () const +{ + uint32_t h = 0; + for (unsigned i = 0; i < length; i++) + h ^= hb_hash (arrayZ[i]); + return h; +} typedef hb_array_t hb_bytes_t; typedef hb_array_t hb_ubytes_t; +/* TODO Specialize hashing for hb_bytes_t and hb_ubytes_t. */ +//template <> +//uint32_t hb_array_t::hash () const { return 0; } #endif /* HB_ARRAY_HH */ diff --git a/src/hb-serialize.hh b/src/hb-serialize.hh index 3bbff75..d8e1e2f 100644 --- a/src/hb-serialize.hh +++ b/src/hb-serialize.hh @@ -58,6 +58,11 @@ struct hb_serialize_context_t && 0 == memcmp (head, o.head, tail - head) && 0 == memcmp (&links, &o.links, links.get_size ()); } + uint32_t hash () const + { + return hb_bytes_t (head, tail - head).hash () ^ + links.as_bytes ().hash (); + } struct link_t { diff --git a/src/hb-vector.hh b/src/hb-vector.hh index 2a92a89..4621b51 100644 --- a/src/hb-vector.hh +++ b/src/hb-vector.hh @@ -100,6 +100,11 @@ struct hb_vector_t return *this; } + hb_bytes_t as_bytes () const { return hb_bytes_t ((const char *) arrayZ_, + length * item_size); } + + uint32_t hash () const { return as_bytes ().hash (); } + const Type * arrayZ () const { return arrayZ_; } Type * arrayZ () { return arrayZ_; } -- 2.7.4