[serialize] Fix hb_hashmap_t<> for pointers and use in packed_map

This commit is contained in:
Behdad Esfahbod 2019-04-02 19:27:02 -07:00
parent 42ab32cbba
commit 5b66b033fd
4 changed files with 29 additions and 14 deletions

View File

@ -79,6 +79,7 @@ struct hb_array_t : hb_iter_with_fallback_t<hb_array_t<Type>, Type&>
operator hb_array_t<const Type> () { return hb_array_t<const Type> (arrayZ, length); } operator hb_array_t<const Type> () { return hb_array_t<const Type> (arrayZ, length); }
template <typename T> operator T * () const { return arrayZ; } template <typename T> operator T * () const { return arrayZ; }
bool operator == (const hb_array_t &o) const;
uint32_t hash () const; uint32_t hash () const;
/* /*
@ -275,6 +276,15 @@ template <typename T, unsigned int length_> inline hb_sorted_array_t<T>
hb_sorted_array (T (&array_)[length_]) hb_sorted_array (T (&array_)[length_])
{ return hb_sorted_array_t<T> (array_); } { return hb_sorted_array_t<T> (array_); }
template <typename T>
bool hb_array_t<T>::operator == (const hb_array_t<T> &o) const
{
return length == o.length &&
+ hb_zip (*this, o)
| hb_map ([] (hb_pair_t<T&, T&> &&_) -> bool { return _.first == _.second; })
| hb_all
;
}
template <typename T> template <typename T>
uint32_t hb_array_t<T>::hash () const uint32_t hb_array_t<T>::hash () const
{ {
@ -288,7 +298,7 @@ uint32_t hb_array_t<T>::hash () const
typedef hb_array_t<const char> hb_bytes_t; typedef hb_array_t<const char> hb_bytes_t;
typedef hb_array_t<const unsigned char> hb_ubytes_t; typedef hb_array_t<const unsigned char> hb_ubytes_t;
/* TODO Specialize hashing for hb_bytes_t and hb_ubytes_t. */ /* TODO Specialize opeator==/hash() for hb_bytes_t and hb_ubytes_t. */
//template <> //template <>
//uint32_t hb_array_t<const char>::hash () const { return 0; } //uint32_t hb_array_t<const char>::hash () const { return 0; }

View File

@ -57,6 +57,8 @@ struct hb_hashmap_t
K key; K key;
V value; V value;
void clear () { key = kINVALID; value = vINVALID; }
bool operator == (K o) { return hb_deref_pointer (key) == hb_deref_pointer (o); } bool operator == (K o) { return hb_deref_pointer (key) == hb_deref_pointer (o); }
bool operator == (const item_t &o) { return *this == o.key; } bool operator == (const item_t &o) { return *this == o.key; }
bool is_unused () const { return key == kINVALID; } bool is_unused () const { return key == kINVALID; }
@ -98,9 +100,7 @@ struct hb_hashmap_t
void reset () void reset ()
{ {
/* TODO Keep array? */ clear ();
fini_shallow ();
init_shallow ();
} }
bool in_error () const { return !successful; } bool in_error () const { return !successful; }
@ -117,7 +117,9 @@ struct hb_hashmap_t
successful = false; successful = false;
return false; return false;
} }
memset (new_items, 0xFF, (size_t) new_size * sizeof (item_t)); + hb_iter (new_items, new_size)
| hb_apply ([] (item_t &_) { _.clear (); }) /* TODO make pointer-to-methods invokable. */
;
unsigned int old_size = mask + 1; unsigned int old_size = mask + 1;
item_t *old_items = items; item_t *old_items = items;
@ -168,7 +170,7 @@ struct hb_hashmap_t
{ {
if (unlikely (!items)) return vINVALID; if (unlikely (!items)) return vINVALID;
unsigned int i = bucket_for (key); unsigned int i = bucket_for (key);
return items[i] == key ? items[i].value : vINVALID; return !items[i].is_unused () && items[i] == key ? items[i].value : vINVALID;
} }
void del (K key) { set (key, vINVALID); } void del (K key) { set (key, vINVALID); }
@ -183,7 +185,11 @@ struct hb_hashmap_t
void clear () void clear ()
{ {
if (items) memset (items, 0xFF, ((size_t) mask + 1) * sizeof (item_t)); if (items)
+ hb_iter (items, mask + 1)
| hb_apply ([] (item_t &_) { _.clear (); }) /* TODO make pointer-to-methods invokable. */
;
population = occupancy = 0; population = occupancy = 0;
} }

View File

@ -54,9 +54,9 @@ struct hb_serialize_context_t
bool operator == (const object_t &o) const bool operator == (const object_t &o) const
{ {
return (tail - head == o.tail - o.head) return (tail - head == o.tail - o.head)
&& (links.length != o.links.length) && (links.length == o.links.length)
&& 0 == memcmp (head, o.head, tail - head) && 0 == hb_memcmp (head, o.head, tail - head)
&& 0 == memcmp (&links, &o.links, links.get_size ()); && links.as_bytes () == o.links.as_bytes ();
} }
uint32_t hash () const uint32_t hash () const
{ {
@ -197,7 +197,6 @@ struct hb_serialize_context_t
objidx = packed.length - 1; objidx = packed.length - 1;
if (0) // XXX
packed_map.set (key, objidx); packed_map.set (key, objidx);
return objidx; return objidx;
@ -217,7 +216,6 @@ struct hb_serialize_context_t
while (packed.length > 1 && while (packed.length > 1 &&
packed.tail ().head < tail) packed.tail ().head < tail)
{ {
if (0) // XXX
packed_map.del (&packed.tail ()); packed_map.del (&packed.tail ());
packed.pop (); packed.pop ();
} }

View File

@ -103,6 +103,7 @@ struct hb_vector_t
hb_bytes_t as_bytes () const { return hb_bytes_t ((const char *) arrayZ_, hb_bytes_t as_bytes () const { return hb_bytes_t ((const char *) arrayZ_,
length * item_size); } length * item_size); }
bool operator == (const hb_vector_t &o) const { return as_bytes () == o.as_bytes (); }
uint32_t hash () const { return as_bytes ().hash (); } uint32_t hash () const { return as_bytes ().hash (); }
const Type * arrayZ () const { return arrayZ_; } const Type * arrayZ () const { return arrayZ_; }