From 6843569d2c70c1771ce964e3d1a4cf91e14e7687 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 5 May 2011 14:12:37 -0400 Subject: [PATCH] Replace fixed-size feature_maps array with hb_array_t --- src/hb-ot-map-private.hh | 10 +++------- src/hb-ot-map.cc | 12 ++++++------ src/hb-ot-shape.cc | 2 +- src/hb-private.hh | 42 +++++++++++++++++++++++++++++------------- 4 files changed, 39 insertions(+), 27 deletions(-) diff --git a/src/hb-ot-map-private.hh b/src/hb-ot-map-private.hh index 3b0cc19..bd4e868 100644 --- a/src/hb-ot-map-private.hh +++ b/src/hb-ot-map-private.hh @@ -82,8 +82,6 @@ struct hb_ot_map_t { public: - hb_ot_map_t (void) : feature_count (0) {} - void add_feature (hb_tag_t tag, unsigned int value, bool global) { feature_info_t *info = feature_infos.push(); @@ -104,13 +102,13 @@ struct hb_ot_map_t { inline hb_mask_t get_global_mask (void) const { return global_mask; } inline hb_mask_t get_mask (hb_tag_t tag, unsigned int *shift = NULL) const { - const feature_map_t *map = (const feature_map_t *) bsearch (&tag, feature_maps, feature_count, sizeof (feature_maps[0]), (hb_compare_func_t) feature_map_t::cmp); + const feature_map_t *map = feature_maps.bsearch (&tag); if (shift) *shift = map ? map->shift : 0; return map ? map->mask : 0; } inline hb_mask_t get_1_mask (hb_tag_t tag) const { - const feature_map_t *map = (const feature_map_t *) bsearch (&tag, feature_maps, feature_count, sizeof (feature_maps[0]), (hb_compare_func_t) feature_map_t::cmp); + const feature_map_t *map = feature_maps.bsearch (&tag); return map ? map->_1_mask : 0; } @@ -128,10 +126,8 @@ struct hb_ot_map_t { hb_mask_t global_mask; - unsigned int feature_count; hb_prealloced_array_t feature_infos; /* used before compile() only */ -#define MAX_FEATURES 100 - feature_map_t feature_maps[MAX_FEATURES]; + hb_prealloced_array_t feature_maps; lookup_map_t lookup_maps[2][MAX_LOOKUPS]; /* GSUB/GPOS */ unsigned int lookup_count[2]; diff --git a/src/hb-ot-map.cc b/src/hb-ot-map.cc index d313da7..04f41ee 100644 --- a/src/hb-ot-map.cc +++ b/src/hb-ot-map.cc @@ -106,10 +106,8 @@ hb_ot_map_t::compile (hb_face_t *face, /* Allocate bits now */ - feature_count = feature_infos.len; unsigned int next_bit = 1; - j = 0; - for (unsigned int i = 0; i < feature_count; i++) { + for (unsigned int i = 0; i < feature_infos.len; i++) { const feature_info_t *info = &feature_infos[i]; unsigned int bits_needed; @@ -137,7 +135,9 @@ hb_ot_map_t::compile (hb_face_t *face, continue; - feature_map_t *map = &feature_maps[j++]; + feature_map_t *map = feature_maps.push (); + if (unlikely (!map)) + break; map->tag = info->tag; map->index[0] = feature_index[0]; @@ -156,7 +156,7 @@ hb_ot_map_t::compile (hb_face_t *face, map->_1_mask = (1 << map->shift) & map->mask; } - feature_count = j; + feature_infos.shrink (0); /* Done with these */ for (unsigned int table_index = 0; table_index < 2; table_index++) { @@ -172,7 +172,7 @@ hb_ot_map_t::compile (hb_face_t *face, &required_feature_index)) add_lookups (face, table_index, required_feature_index, 1); - for (unsigned i = 0; i < feature_count; i++) + for (unsigned i = 0; i < feature_maps.len; i++) add_lookups (face, table_index, feature_maps[i].index[table_index], feature_maps[i].mask); /* Sort lookups and merge duplicates */ diff --git a/src/hb-ot-shape.cc b/src/hb-ot-shape.cc index e01f372..306bb41 100644 --- a/src/hb-ot-shape.cc +++ b/src/hb-ot-shape.cc @@ -370,7 +370,7 @@ hb_ot_shape (hb_font_t *font, const hb_feature_t *features, unsigned int num_features) { - hb_ot_shape_plan_t plan; + hb_ot_shape_plan_t plan = hb_ot_shape_plan_t (); hb_ot_shape_plan_internal (&plan, font->face, &buffer->props, features, num_features); hb_ot_shape_execute (&plan, font, buffer, features, num_features); diff --git a/src/hb-private.hh b/src/hb-private.hh index d3edad3..e776daf 100644 --- a/src/hb-private.hh +++ b/src/hb-private.hh @@ -285,10 +285,36 @@ struct hb_prealloced_array_t { /* TODO: shrink array if needed */ } + template + inline Type *find (T v) { + for (unsigned int i = 0; i < len; i++) + if (array[i] == v) + return &array[i]; + return NULL; + } + template + inline const Type *find (T v) const { + for (unsigned int i = 0; i < len; i++) + if (array[i] == v) + return &array[i]; + return NULL; + } + inline void sort (void) { qsort (array, len, sizeof (Type), (hb_compare_func_t) Type::cmp); } + + template + inline Type *bsearch (T *key) + { + return (Type *) ::bsearch (key, array, len, sizeof (Type), (hb_compare_func_t) Type::cmp); + } + template + inline const Type *bsearch (T *key) const + { + return (const Type *) ::bsearch (key, array, len, sizeof (Type), (hb_compare_func_t) Type::cmp); + } }; template @@ -300,22 +326,12 @@ struct hb_set_t { hb_array_t items; - private: - - template - inline item_t *find (T v) { - for (unsigned int i = 0; i < items.len; i++) - if (items[i] == v) - return &items[i]; - return NULL; - } - public: template inline bool insert (T v) { - item_t *item = find (v); + item_t *item = items.find (v); if (item) item->finish (); else @@ -328,7 +344,7 @@ struct hb_set_t template inline void remove (T v) { - item_t *item = find (v); + item_t *item = items.find (v); if (!item) return; item->finish (); @@ -339,7 +355,7 @@ struct hb_set_t template inline item_t *get (T v) { - return find (v); + return items.find (v); } void finish (void) { -- 2.7.4