1 //========================================================================
5 // Copyright 2001-2003 Glyph & Cog, LLC
7 //========================================================================
11 #ifdef USE_GCC_PRAGMAS
12 #pragma implementation
17 #include "NameToCharCode.h"
19 //------------------------------------------------------------------------
21 struct NameToCharCodeEntry
{
26 //------------------------------------------------------------------------
28 NameToCharCode::NameToCharCode() {
33 tab
= (NameToCharCodeEntry
*)gmalloc(size
* sizeof(NameToCharCodeEntry
));
34 for (i
= 0; i
< size
; ++i
) {
39 NameToCharCode::~NameToCharCode() {
42 for (i
= 0; i
< size
; ++i
) {
50 void NameToCharCode::add(char *name
, CharCode c
) {
51 NameToCharCodeEntry
*oldTab
;
54 // expand the table if necessary
55 if (len
>= size
/ 2) {
59 tab
= (NameToCharCodeEntry
*)gmalloc(size
* sizeof(NameToCharCodeEntry
));
60 for (h
= 0; h
< size
; ++h
) {
63 for (i
= 0; i
< oldSize
; ++i
) {
65 h
= hash(oldTab
[i
].name
);
79 while (tab
[h
].name
&& strcmp(tab
[h
].name
, name
)) {
85 tab
[h
].name
= copyString(name
);
92 CharCode
NameToCharCode::lookup(char *name
) {
97 if (!strcmp(tab
[h
].name
, name
)) {
107 int NameToCharCode::hash(char *name
) {
112 for (p
= name
; *p
; ++p
) {
113 h
= 17 * h
+ (int)(*p
& 0xff);
115 return (int)(h
% size
);