From 3f78a71ca059b461f79d0ee64766c7c3f4327b14 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 2 Jun 2022 11:11:35 -0600 Subject: [PATCH] [map] Finally! Just can usd hb_hashmap_t Yay! --- src/hb-map.hh | 29 ++++++++++----------- src/hb-ot-post-table-v2subset.hh | 2 +- src/hb-serialize.hh | 3 +-- src/test-map.cc | 43 ++++++++------------------------ 4 files changed, 27 insertions(+), 50 deletions(-) diff --git a/src/hb-map.hh b/src/hb-map.hh index c62b9d7bb..9b435c7d7 100644 --- a/src/hb-map.hh +++ b/src/hb-map.hh @@ -35,8 +35,7 @@ */ template ::value ? 0 : std::is_signed::value ? hb_int_min (V) : (V) -1> + bool minus_one = false> struct hb_hashmap_t { hb_hashmap_t () { init (); } @@ -71,19 +70,24 @@ struct hb_hashmap_t uint32_t is_tombstone_ : 1; V value; - - bool is_used () const { return is_used_; } void set_used (bool is_used) { is_used_ = is_used; } bool is_tombstone () const { return is_tombstone_; } void set_tombstone (bool is_tombstone) { is_tombstone_ = is_tombstone; } bool is_real () const { return is_used_ && !is_tombstone_; } + template + static V default_value () { return V(); }; + template + static V default_value () { return V(-1); }; + void clear () { new (std::addressof (key)) K (); new (std::addressof (value)) V (); - value = hb_coerce (vINVALID); + value = default_value (); hash = 0; is_used_ = false; is_tombstone_ = false; @@ -200,12 +204,12 @@ struct hb_hashmap_t V get (K key) const { - if (unlikely (!items)) return hb_coerce (vINVALID); + if (unlikely (!items)) return item_t::default_value (); unsigned int i = bucket_for (key); - return items[i].is_real () && items[i] == key ? items[i].value : hb_coerce (vINVALID); + return items[i].is_real () && items[i] == key ? items[i].value : item_t::default_value (); } - void del (K key) { set_with_hash (key, hb_hash (key), hb_coerce (vINVALID), true); } + void del (K key) { set_with_hash (key, hb_hash (key), item_t::default_value (), true); } /* Has interface. */ typedef V value_t; @@ -214,8 +218,7 @@ struct hb_hashmap_t { const V &v = (*this)[k]; if (vp) *vp = v; - const V vinv = hb_coerce (vINVALID); - return v != vinv; + return v != item_t::default_value (); // TODO } /* Projection. */ V operator () (K k) const { return get (k); } @@ -398,13 +401,11 @@ struct hb_hashmap_t struct hb_map_t : hb_hashmap_t + true> { using hashmap = hb_hashmap_t; + true>; ~hb_map_t () = default; hb_map_t () : hashmap () {} diff --git a/src/hb-ot-post-table-v2subset.hh b/src/hb-ot-post-table-v2subset.hh index 8c0312fa4..718b09d7e 100644 --- a/src/hb-ot-post-table-v2subset.hh +++ b/src/hb-ot-post-table-v2subset.hh @@ -78,7 +78,7 @@ HB_INTERNAL bool postV2Tail::subset (hb_subset_context_t *c) const post::accelerator_t _post (c->plan->source); - hb_hashmap_t glyph_name_to_new_index; + hb_hashmap_t glyph_name_to_new_index; for (hb_codepoint_t new_gid = 0; new_gid < num_glyphs; new_gid++) { hb_codepoint_t old_gid = reverse_glyph_map.get (new_gid); diff --git a/src/hb-serialize.hh b/src/hb-serialize.hh index 3f4535ce4..6f56faafe 100644 --- a/src/hb-serialize.hh +++ b/src/hb-serialize.hh @@ -719,8 +719,7 @@ struct hb_serialize_context_t hb_vector_t packed; /* Map view of packed objects. */ - hb_hashmap_t packed_map; + hb_hashmap_t packed_map; }; #endif /* HB_SERIALIZE_HH */ diff --git a/src/test-map.cc b/src/test-map.cc index e62f65091..0351d8161 100644 --- a/src/test-map.cc +++ b/src/test-map.cc @@ -27,11 +27,6 @@ #include "hb-set.hh" #include -static const std::string invalid{"invalid"}; -static const hb_map_t invalid_map{}; -static const hb_set_t invalid_set{}; -static const hb_vector_t invalid_vector{}; - int main (int argc, char **argv) { @@ -127,19 +122,19 @@ main (int argc, char **argv) /* Test class key / value types. */ { - hb_hashmap_t m1; - hb_hashmap_t m2; - hb_hashmap_t m3; + hb_hashmap_t m1; + hb_hashmap_t m2; + hb_hashmap_t m3; assert (m1.get_population () == 0); assert (m2.get_population () == 0); assert (m3.get_population () == 0); } { - hb_hashmap_t m0; - hb_hashmap_t m1; - hb_hashmap_t m2; - hb_hashmap_t m3; + hb_hashmap_t m0; + hb_hashmap_t m1; + hb_hashmap_t m2; + hb_hashmap_t m3; std::string s; for (unsigned i = 1; i < 1000; i++) @@ -156,67 +151,49 @@ main (int argc, char **argv) { using pair = hb_pair_t; - hb_hashmap_t m1; - hb_hashmap_t m2; + hb_hashmap_t m1; m1.set (hb_map_t (), hb_map_t {}); - m2.set (hb_map_t (), hb_map_t {}); m1.set (hb_map_t (), hb_map_t {pair (1u, 2u)}); - m2.set (hb_map_t (), hb_map_t {pair (1u, 2u)}); m1.set (hb_map_t {pair (1u, 2u)}, hb_map_t {pair (2u, 3u)}); - m2.set (hb_map_t {pair (1u, 2u)}, hb_map_t {pair (2u, 3u)}); assert (m1.get (hb_map_t ()) == hb_map_t {pair (1u, 2u)}); - assert (m2.get (hb_map_t ()) == hb_map_t {pair (1u, 2u)}); assert (m1.get (hb_map_t {pair (1u, 2u)}) == hb_map_t {pair (2u, 3u)}); - assert (m2.get (hb_map_t {pair (1u, 2u)}) == hb_map_t {pair (2u, 3u)}); } /* Test hashing sets. */ { - hb_hashmap_t m1; - hb_hashmap_t m2; + hb_hashmap_t m1; m1.set (hb_set_t (), hb_set_t ()); - m2.set (hb_set_t (), hb_set_t ()); m1.set (hb_set_t (), hb_set_t {1}); - m2.set (hb_set_t (), hb_set_t {1}); m1.set (hb_set_t {1, 1000}, hb_set_t {2}); - m2.set (hb_set_t {1, 1000}, hb_set_t {2}); assert (m1.get (hb_set_t ()) == hb_set_t {1}); - assert (m2.get (hb_set_t ()) == hb_set_t {1}); assert (m1.get (hb_set_t {1000, 1}) == hb_set_t {2}); - assert (m2.get (hb_set_t {1000, 1}) == hb_set_t {2}); } /* Test hashing vectors. */ { using vector_t = hb_vector_t; - hb_hashmap_t m1; - hb_hashmap_t m2; + hb_hashmap_t m1; m1.set (vector_t (), vector_t ()); - m2.set (vector_t (), vector_t ()); m1.set (vector_t (), vector_t {1}); - m2.set (vector_t (), vector_t {1}); m1.set (vector_t {1}, vector_t {2}); - m2.set (vector_t {1}, vector_t {2}); assert (m1.get (vector_t ()) == vector_t {1}); - assert (m2.get (vector_t ()) == vector_t {1}); assert (m1.get (vector_t {1}) == vector_t {2}); - assert (m2.get (vector_t {1}) == vector_t {2}); } /* Test hb::shared_ptr. */