unuse set in hb_map2_t impl

also some code cleanup
This commit is contained in:
Michiharu Ariza 2019-02-22 10:22:08 -08:00
parent a762cf5033
commit 6ecfaaa6d5
1 changed files with 23 additions and 23 deletions

View File

@ -1586,6 +1586,9 @@ static inline void ClassDef_serialize (hb_serialize_context_t *c,
hb_array_t<const HBUINT16> klasses) hb_array_t<const HBUINT16> klasses)
{ c->start_embed<ClassDef> ()->serialize (c, glyphs, klasses); } { c->start_embed<ClassDef> ()->serialize (c, glyphs, klasses); }
/* Bi-directional map.
* nww ids are assigned incrementally & contiguous; old ids may be random & sparse
* all mappings unique & no duplicate */
struct hb_map2_t struct hb_map2_t
{ {
hb_map2_t () { init (); } hb_map2_t () { init (); }
@ -1596,71 +1599,68 @@ struct hb_map2_t
count = 0; count = 0;
old_to_new_map.init (); old_to_new_map.init ();
new_to_old_map.init (); new_to_old_map.init ();
set.init ();
} }
void fini (void) void fini (void)
{ {
old_to_new_map.fini (); old_to_new_map.fini ();
new_to_old_map.fini (); new_to_old_map.fini ();
set.fini ();
} }
bool has (hb_codepoint_t id) const { return set.has (id); } bool has (hb_codepoint_t _old) const { return old_to_new_map.has (_old); }
hb_codepoint_t add (hb_codepoint_t i) hb_codepoint_t add (hb_codepoint_t _old)
{ {
hb_codepoint_t v = old_to_new_map[i]; hb_codepoint_t _new = old_to_new_map[_old];
if (v == HB_MAP_VALUE_INVALID) if (_new == HB_MAP_VALUE_INVALID)
{ {
set.add (i); _new = count++;
v = count++; old_to_new_map.set (_old, _new);
old_to_new_map.set (i, v); new_to_old_map.set (_new, _old);
new_to_old_map.set (v, i);
} }
return v; return _new;
} }
/* returns HB_MAP_VALUE_INVALID if unmapped */ /* returns HB_MAP_VALUE_INVALID if unmapped */
hb_codepoint_t operator [] (hb_codepoint_t i) const { return old_to_new (i); } hb_codepoint_t operator [] (hb_codepoint_t _old) const { return old_to_new (_old); }
hb_codepoint_t old_to_new (hb_codepoint_t i) const { return old_to_new_map[i]; } hb_codepoint_t old_to_new (hb_codepoint_t _old) const { return old_to_new_map[_old]; }
hb_codepoint_t new_to_old (hb_codepoint_t i) const { return new_to_old_map[i]; } hb_codepoint_t new_to_old (hb_codepoint_t _new) const { return new_to_old_map[_new]; }
bool identity (unsigned int size) bool identity (unsigned int size)
{ {
hb_codepoint_t i; hb_codepoint_t i;
old_to_new_map.clear (); old_to_new_map.clear ();
new_to_old_map.clear (); new_to_old_map.clear ();
set.clear ();
for (i = 0; i < size; i++) for (i = 0; i < size; i++)
{ {
old_to_new_map.set (i, i); old_to_new_map.set (i, i);
new_to_old_map.set (i, i); new_to_old_map.set (i, i);
set.add (i);
} }
count = i; count = i;
return old_to_new_map.successful && new_to_old_map.successful && set.successful; return old_to_new_map.successful && new_to_old_map.successful;
} }
/* Optional: after finished adding all mappings in a random order, /* Optional: after finished adding all mappings in a random order,
* reorder outputs in the same order as the inputs. */ * reassign new ids to old ids so that they are in the same order. */
void reorder (void) void reorder (void)
{ {
for (hb_codepoint_t i = HB_SET_VALUE_INVALID, count = 0; set.next (&i); count++) hb_codepoint_t _new = 0;
for (hb_codepoint_t _old = 0; _new < count; _old++)
{ {
new_to_old_map.set (count, i); if (!has (_old)) continue;
old_to_new_map.set (i, count); new_to_old_map.set (_new, _old);
old_to_new_map.set (_old, _new);
_old++;
_new++;
} }
} }
unsigned int get_count () const { return count; } unsigned int get_count () const { return count; }
unsigned int get_bits () const { return count? hb_bit_storage (count - 1): 0; }
protected: protected:
unsigned int count; unsigned int count;
hb_map_t old_to_new_map; hb_map_t old_to_new_map;
hb_map_t new_to_old_map; hb_map_t new_to_old_map;
hb_set_t set;
}; };
/* /*