ref: bd6b4b8bf37c00319dbc17b898c1426594169809
parent: e4eb154d9ae6e0d3d7d2669e363820b9c3c8fc1a
author: Ali Gholami Rudi <ali@rudi.ir>
date: Tue Jul 30 10:28:12 EDT 2013
font: optimize font_find() with starting character lists
--- a/font.c
+++ b/font.c
@@ -13,10 +13,12 @@
struct glyph *font_find(struct font *fn, char *name)
{
- int i;
- for (i = 0; i < fn->n; i++)
- if (name[0] == fn->c[i][0] && !strcmp(name, fn->c[i]))
+ int i = fn->head[(unsigned char) name[0]];
+ while (i >= 0) {
+ if (!strcmp(name, fn->c[i]))
return fn->g[i];
+ i = fn->next[i];
+ }
return NULL;
}
@@ -64,6 +66,8 @@
prev = glyph;
strcpy(fn->c[fn->n], name);
fn->g[fn->n] = glyph;
+ fn->next[fn->n] = fn->head[(unsigned char) name[0]];
+ fn->head[(unsigned char) name[0]] = fn->n;
fn->n++;
}
}
@@ -143,8 +147,11 @@
struct font *fn = malloc(sizeof(*fn));
char tok[ILNLEN];
FILE *fin;
+ int i;
fin = fopen(path, "r");
memset(fn, 0, sizeof(*fn));
+ for (i = 0; i < LEN(fn->head); i++)
+ fn->head[i] = -1;
while (fscanf(fin, "%s", tok) == 1) {
if (tok[0] == '#') {
skipline(fin);
--- a/roff.h
+++ b/roff.h
@@ -113,6 +113,9 @@
char kern_c1[NKERNS][GNLEN];
char kern_c2[NKERNS][GNLEN];
int nkern;
+ /* glyph list based on the first character of glyph names */
+ int head[256]; /* glyph list head */
+ int next[NGLYPHS]; /* next item in glyph list */
};
/* output device functions */