-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringtab.cpp
More file actions
93 lines (84 loc) · 2 KB
/
stringtab.cpp
File metadata and controls
93 lines (84 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// stringtab.cpp - implementation of string table
#include "stringtab.h"
#include <string.h>
#include <malloc.h>
#include <stdio.h>
#include <stddef.h>
namespace js2cpp
{
StringTable::StringTable()
{
memset(m_bucket, 0, sizeof m_bucket);
}
StringTable::~StringTable()
{
int total = 0, maxdepth = 0;
fprintf(stdout, "StringTable destruction report\n");
for (int i = 0; i < BUCKETS; i++) {
STAtom *atom = m_bucket[i];
int depth = 0;
while (atom) {
STAtom *link = atom->m_link;
free(atom->m_name);
delete atom;
atom = link;
depth++;
}
total += depth;
if (depth > maxdepth) {
maxdepth = depth;
}
} // for i
fprintf(stdout, " total entries = %d\n", total);
fprintf(stdout, " average hash bucket size: %0.2f entries\n", (double)total / BUCKETS);
fprintf(stdout, " deepest hash bucket: %d entries\n", maxdepth);
} // ~StringTable
STAtom* AddToList(STAtom *&list, const char *pz, int n)
{
STAtom *atom = list;
while (atom && (memcmp(atom->m_name, pz, n) || atom->m_name[n])) {
atom = atom->m_link;
}
if (!atom) {
// str is not in the list, add it
atom = new STAtom;
if (!atom) {
return NULL;
}
char *str = (char*)malloc(n+1);
if (!str) {
delete atom;
return NULL;
}
atom->m_id = 0;
memcpy(str, pz, n);
str[n]='\0';
atom->m_name = str;
// insert new atom at front of list
atom->m_link = list;
list = atom;
} else {
// found string in table
}
return atom;
}
STAtom *StringTable::Intern(const char *str)
{
int n = strlen(str);
unsigned h = Hash(str, n) % BUCKETS;
return AddToList(m_bucket[h], str, n);
}
STAtom *StringTable::Intern(const char *pz, int n)
{
unsigned h = Hash(pz, n) % BUCKETS;
return AddToList(m_bucket[h], pz, n);
} // Intern
unsigned StringTable::Hash(const char *pz, int n)
{
unsigned h = 0;
while (n--) {
h = (h * 33) ^ *pz++;
}
return h;
} // Hash
} // namespace