Use insertion-sort instead of bubble-sort

Needed for upcoming merge-clusters fix.
pull/148/head
Behdad Esfahbod 9 years ago
parent fad2674874
commit 85846b3de7
  1. 4
      src/hb-buffer.cc
  2. 6
      src/hb-ot-shape-complex-arabic-fallback.hh
  3. 2
      src/hb-ot-shape-complex-indic.cc
  4. 2
      src/hb-ot-shape-complex-myanmar.cc
  5. 6
      src/hb-ot-shape-normalize.cc
  6. 56
      src/hb-private.hh

@ -1636,7 +1636,7 @@ normalize_glyphs_cluster (hb_buffer_t *buffer,
pos[end - 1].x_advance = total_x_advance; pos[end - 1].x_advance = total_x_advance;
pos[end - 1].y_advance = total_y_advance; pos[end - 1].y_advance = total_y_advance;
hb_bubble_sort (buffer->info + start, end - start - 1, compare_info_codepoint, buffer->pos + start); hb_stable_sort (buffer->info + start, end - start - 1, compare_info_codepoint, buffer->pos + start);
} else { } else {
/* Transfer all cluster advance to the first glyph. */ /* Transfer all cluster advance to the first glyph. */
pos[start].x_advance += total_x_advance; pos[start].x_advance += total_x_advance;
@ -1645,7 +1645,7 @@ normalize_glyphs_cluster (hb_buffer_t *buffer,
pos[i].x_offset -= total_x_advance; pos[i].x_offset -= total_x_advance;
pos[i].y_offset -= total_y_advance; pos[i].y_offset -= total_y_advance;
} }
hb_bubble_sort (buffer->info + start + 1, end - start - 1, compare_info_codepoint, buffer->pos + start + 1); hb_stable_sort (buffer->info + start + 1, end - start - 1, compare_info_codepoint, buffer->pos + start + 1);
} }
} }

@ -75,9 +75,9 @@ arabic_fallback_synthesize_lookup_single (const hb_ot_shape_plan_t *plan HB_UNUS
if (!num_glyphs) if (!num_glyphs)
return NULL; return NULL;
/* Bubble-sort! /* Bubble-sort or something equally good!
* May not be good-enough for presidential candidate interviews, but good-enough for us... */ * May not be good-enough for presidential candidate interviews, but good-enough for us... */
hb_bubble_sort (&glyphs[0], num_glyphs, OT::GlyphID::cmp, &substitutes[0]); hb_stable_sort (&glyphs[0], num_glyphs, OT::GlyphID::cmp, &substitutes[0]);
OT::Supplier<OT::GlyphID> glyphs_supplier (glyphs, num_glyphs); OT::Supplier<OT::GlyphID> glyphs_supplier (glyphs, num_glyphs);
OT::Supplier<OT::GlyphID> substitutes_supplier (substitutes, num_glyphs); OT::Supplier<OT::GlyphID> substitutes_supplier (substitutes, num_glyphs);
@ -126,7 +126,7 @@ arabic_fallback_synthesize_lookup_ligature (const hb_ot_shape_plan_t *plan HB_UN
first_glyphs_indirection[num_first_glyphs] = first_glyph_idx; first_glyphs_indirection[num_first_glyphs] = first_glyph_idx;
num_first_glyphs++; num_first_glyphs++;
} }
hb_bubble_sort (&first_glyphs[0], num_first_glyphs, OT::GlyphID::cmp, &first_glyphs_indirection[0]); hb_stable_sort (&first_glyphs[0], num_first_glyphs, OT::GlyphID::cmp, &first_glyphs_indirection[0]);
/* Now that the first-glyphs are sorted, walk again, populate ligatures. */ /* Now that the first-glyphs are sorted, walk again, populate ligatures. */
for (unsigned int i = 0; i < num_first_glyphs; i++) for (unsigned int i = 0; i < num_first_glyphs; i++)

@ -1012,7 +1012,7 @@ initial_reordering_consonant_syllable (const hb_ot_shape_plan_t *plan,
info[i].syllable() = i - start; info[i].syllable() = i - start;
/* Sit tight, rock 'n roll! */ /* Sit tight, rock 'n roll! */
hb_bubble_sort (info + start, end - start, compare_indic_order); hb_stable_sort (info + start, end - start, compare_indic_order);
/* Find base again */ /* Find base again */
base = end; base = end;
for (unsigned int i = start; i < end; i++) for (unsigned int i = start; i < end; i++)

@ -393,7 +393,7 @@ initial_reordering_consonant_syllable (hb_buffer_t *buffer,
buffer->merge_clusters (start, end); buffer->merge_clusters (start, end);
/* Sit tight, rock 'n roll! */ /* Sit tight, rock 'n roll! */
hb_bubble_sort (info + start, end - start, compare_myanmar_order); hb_stable_sort (info + start, end - start, compare_myanmar_order);
} }
static void static void

@ -344,15 +344,13 @@ _hb_ot_shape_normalize (const hb_ot_shape_plan_t *plan,
if (_hb_glyph_info_get_modified_combining_class (&buffer->info[end]) == 0) if (_hb_glyph_info_get_modified_combining_class (&buffer->info[end]) == 0)
break; break;
/* We are going to do a bubble-sort. Only do this if the /* We are going to do a O(n^2). Only do this if the sequence is short. */
* sequence is short. Doing it on long sequences can result
* in an O(n^2) DoS. */
if (end - i > 10) { if (end - i > 10) {
i = end; i = end;
continue; continue;
} }
hb_bubble_sort (buffer->info + i, end - i, compare_combining_class); hb_stable_sort (buffer->info + i, end - i, compare_combining_class);
i = end; i = end;
} }

@ -855,42 +855,36 @@ hb_in_ranges (T u, T lo1, T hi1, T lo2, T hi2, T lo3, T hi3)
template <typename T, typename T2> static inline void template <typename T, typename T2> static inline void
hb_bubble_sort (T *array, unsigned int len, int(*compar)(const T *, const T *), T2 *array2) hb_stable_sort (T *array, unsigned int len, int(*compar)(const T *, const T *), T2 *array2)
{ {
if (unlikely (!len)) for (unsigned int i = 1; i < len; i++)
return; {
unsigned int j = i;
unsigned int k = len - 1; while (j && compar (&array[j - 1], &array[i]) > 0)
do { j--;
unsigned int new_k = 0; if (i == j)
continue;
for (unsigned int j = 0; j < k; j++) /* Move item i to occupy place for item j, shift what's in between. */
if (compar (&array[j], &array[j+1]) > 0) {
{ T t;
{ t = array[i];
T t; memmove (&array[j + 1], &array[j], (i - j) * sizeof (T));
t = array[j]; array[j] = t;
array[j] = array[j + 1]; }
array[j + 1] = t; if (array2)
} {
if (array2) T2 t;
{ t = array2[i];
T2 t; memmove (&array2[j + 1], &array2[j], (i - j) * sizeof (T2));
t = array2[j]; array2[j] = t;
array2[j] = array2[j + 1]; }
array2[j + 1] = t; }
}
new_k = j;
}
k = new_k;
} while (k);
} }
template <typename T> static inline void template <typename T> static inline void
hb_bubble_sort (T *array, unsigned int len, int(*compar)(const T *, const T *)) hb_stable_sort (T *array, unsigned int len, int(*compar)(const T *, const T *))
{ {
hb_bubble_sort (array, len, compar, (int *) NULL); hb_stable_sort (array, len, compar, (int *) NULL);
} }
static inline hb_bool_t static inline hb_bool_t

Loading…
Cancel
Save