3
/* string to hash map, and vice versa; for quick string lookups */
8
int map::create(int passed_size)
12
tab = (struct hash_struct *)malloc(size * sizeof(struct hash_struct));
14
/* initialise map to be empty */
17
/* a hash for quick string lookups */
18
tab_hash = new OAHash;
19
tab_hash->create(size*9);
24
void map::empty(int from, int to)
27
struct hash_struct *ptr;
29
for(ptr = &tab[from], x = from; x < to; ++ptr, ++x)
40
int map::destroy(void)
50
int map::resize(int newsize)
53
struct hash_struct *newtab;
55
/* we have to resize the tab, then clear the hash, remake the hash
56
* at the newsize*5, then re-populate the hash. Remaking the hash is
57
* quite expensive, so we'd like to do this as infrequently as
59
newtab = (struct hash_struct *)
60
realloc(tab, newsize * sizeof(struct hash_struct));
65
perror("realloc map");
71
/* empty the new area */
72
this->empty(size, newsize);
74
/* remake the hash since memory addresses may have moved */
76
tab_hash = new OAHash;
77
tab_hash->create(newsize*9);
78
for (x = 0 ; x < size ; ++x)
80
tab_hash->insert(tab[x].string, &tab[x]);
83
// dprintf("%p resize from %d to %d\n", this, size, newsize);
90
int map::insert(char *string)
93
struct hash_struct *ptr;
95
/* if this string is in the map, return existing position only */
96
if ((x = this->lookup(string)) != -1)
98
/* we wanted to insert, but didn't, so the refcount for this
99
* particular entry is incremented */
102
// dprintf("%d Found %p %d for %s\n", time(NULL), this, x, string);
106
/* find free table slot FIXME make this more efficient */
107
for (ptr = &tab[0], x = 0; x <= size; ++ptr, ++x)
111
/* none free, make room */
112
this->resize(size * 2);
114
/* realloc may well move the table in
115
* memory so update the pointer */
121
/* if this map entry has a refcount of zero (this is new in
122
* v0.12) then nothing is pointing at it, hopefully, so it
124
if (ptr->refcount == 0)
128
/* if we are re-using, free up the old data */
131
tab_hash->remove(ptr->string);
135
// dprintf("%d Using %p %d for %s\n", time(NULL), this, x, string);
137
/* make entry in our table */
140
ptr->string = strdup(string);
143
tab_hash->insert(ptr->string, ptr);
148
int map::remove(char *string)
150
struct hash_struct *ptr;
152
/* find string in hash */
153
if ((ptr = (struct hash_struct *)tab_hash->lookup(string)))
155
// dprintf("%d Remove %p %d for %s\n", time(NULL), this, ptr->pos, string);
157
/* remove from table */
163
/* remove from hash */
164
tab_hash->remove(string);
170
int map::lookup(char *string)
172
struct hash_struct *ptr;
174
if ((ptr = (struct hash_struct *)tab_hash->lookup(string)))
176
ptr->time = now; /* touch it, so insert won't remove */
183
char *map::reverse(int pos)
185
/* return pointer to char for this string pos */
186
return tab[pos].string;
189
void map::sub_ref(int pos)
191
// dprintf("%d subref %p %d for %s\n",
192
// time(NULL), this, pos, tab[pos].string);
194
if (tab[pos].refcount > 0)