1 //========================================================================
5 // Copyright 2001-2002 Glyph & Cog, LLC
7 //========================================================================
10 #pragma implementation
16 #include "NameToCharCode.h"
18 //------------------------------------------------------------------------
20 struct NameToCharCodeEntry {
25 //------------------------------------------------------------------------
27 NameToCharCode::NameToCharCode() {
32 tab = (NameToCharCodeEntry *)gmalloc(size * sizeof(NameToCharCodeEntry));
33 for (i = 0; i < size; ++i) {
38 NameToCharCode::~NameToCharCode() {
41 for (i = 0; i < size; ++i) {
49 void NameToCharCode::add(char *name, CharCode c) {
50 NameToCharCodeEntry *oldTab;
53 // expand the table if necessary
54 if (len >= size / 2) {
58 tab = (NameToCharCodeEntry *)gmalloc(size * sizeof(NameToCharCodeEntry));
59 for (h = 0; h < size; ++h) {
62 for (i = 0; i < oldSize; ++i) {
64 h = hash(oldTab[i].name);
78 while (tab[h].name && strcmp(tab[h].name, name)) {
84 tab[h].name = copyString(name);
91 CharCode NameToCharCode::lookup(char *name) {
96 if (!strcmp(tab[h].name, name)) {
106 int NameToCharCode::hash(char *name) {
111 for (p = name; *p; ++p) {
112 h = 17 * h + (int)(*p & 0xff);
114 return (int)(h % size);