1 /* Atoms: Names to numbers HTAtom.c
2 ** =======================
4 ** Atoms are names which are given representative pointer values
5 ** so that they can be stored more efficiently, and comparisons
6 ** for equality done more efficiently.
8 ** Atoms are kept in a hash table consisting of an array of linked lists.
11 ** TBL Tim Berners-Lee, WorldWideWeb project, CERN
12 ** (c) Copyright CERN 1991 - See Copyright.html
15 #define HASH_SIZE 101 /* Tunable */
23 PRIVATE HTAtom * hash_table[HASH_SIZE];
24 PRIVATE BOOL initialised = NO;
27 PUBLIC HTAtom * HTAtom_for(const char * string)
29 PUBLIC HTAtom * HTAtom_for(string)
37 /* First time around, clear hash table
41 for (i=0; i<HASH_SIZE; i++)
42 hash_table[i] = (HTAtom *) 0;
46 /* Generate hash function
48 for(p=string, hash=0; *p; p++) {
49 hash = (hash * 3 + *p) % HASH_SIZE;
52 /* Search for the string in the list
54 for (a=hash_table[hash]; a; a=a->next) {
55 if (0==strcmp(a->name, string)) {
56 if (TRACE) printf("HTAtom: Old atom %d for `&s'\n", a, string);
57 return a; /* Found: return it */
61 /* Generate a new entry
63 a = (HTAtom *)malloc(sizeof(*a));
64 if (a == NULL) outofmem(__FILE__, "HTAtom_for");
65 a->name = (char *)malloc(strlen(string)+1);
66 if (a->name == NULL) outofmem(__FILE__, "HTAtom_for");
67 strcpy(a->name, string);
68 a->next = hash_table[hash]; /* Put onto the head of list */
70 if (TRACE) printf("HTAtom: New atom %d for `&s'\n", a, string);