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
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
119
  
120
  
121
  
122
  
123
  
124
  
125
  
126
  
127
  
128
  
129
  
130
  
131
  
132
  
133
  
134
  
135
  
136
  
137
  
138
  
139
  
140
  
141
  
142
  
143
  
144
  
145
  
146
  
147
  
148
  
149
  
150
  
151
  
152
  
153
  
154
  
155
  
156
  
157
  
158
  
159
  
160
  
161
  
162
  
163
  
164
  
165
  
166
  
167
  
168
  
169
  
170
  
171
  
172
  
173
  
174
  
175
  
176
  
177
  
178
  
179
  
180
  
181
  
182
  
183
  
/* 
|| This file is part of Pike. For copyright information see COPYRIGHT. 
|| Pike is distributed under GPL, LGPL and MPL. See the file COPYING 
|| for more information. 
|| $Id$ 
*/ 
 
#include "global.h" 
#include "hashtable.h" 
#include "stralloc.h" 
#include "stuff.h" 
#include "pike_error.h" 
 
static size_t gobble(struct pike_string *s) 
{ 
  size_t i; 
  i=my_hash_string(s); 
  i+=i >> 3; 
  i+=i >> 7; 
  i+=i >> 12; 
  return i; 
} 
 
/* 
 * Search hash for a specific string. 
 */ 
struct hash_entry *hash_lookup(struct hash_table *h, struct pike_string *s) 
{ 
  struct hash_entry *e, **prev, **base; 
 
  if(!h) return 0; 
  base = prev = h->htable + (gobble(s) & h->mask); 
  for( ;(e = *prev); prev= &e->next) 
  { 
    if(s == e->s) 
    { 
      /* Teleport entry to beginning of line */ 
      *prev = e->next; 
      e->next = *base; 
      *base = e; 
      /* Entry arrives in a puff of smoke. */ 
 
      return e; 
    } 
  } 
  return 0; 
} 
 
/* 
 * We want to keep the order of the hash chain, as it has been carefully 
 * built up to be fast. 
 */ 
static void rehash_list_backwards(struct hash_table *h, 
                                  struct hash_entry *n) 
{ 
  struct hash_entry **base; 
  if(!n) return; 
  rehash_list_backwards(h,n->next); 
  base=h->htable + (gobble(n->s) & h->mask); 
  n->next = *base; 
  *base=n; 
} 
 
/* 
 * create a new, empty hashable 
*/ 
struct hash_table *create_hash_table(void) 
{ 
  struct hash_table *new; 
  new=(struct hash_table *)calloc(1,sizeof(struct hash_table)+ 
                                  (NEW_HASHTABLE_SIZE-1)*sizeof(struct hash_entry *)); 
  new->entries=0; 
  new->mask=NEW_HASHTABLE_SIZE-1; 
  return new; 
} 
 
/* 
 * rehash - ugh 
 */ 
struct hash_table *hash_rehash(struct hash_table *h,int size) 
{ 
  struct hash_table *new; 
  int e; 
 
#ifdef PIKE_DEBUG 
  if( 1 << my_log2(size) != size) 
    Pike_fatal("Size is not a power of two!\n"); 
#endif 
 
  new=(struct hash_table *)calloc(1,sizeof(struct hash_table)+ 
                                  (size-1)*sizeof(struct hash_entry *)); 
  new->mask = size - 1; 
  new->entries = h->entries; 
 
  for(e=0; e<=h->mask; e++) 
    rehash_list_backwards(new,h->htable[e]); 
 
  free((char *)h); 
  return new; 
} 
 
/* 
 * insert a newly created hash entry on it's rightful place in the 
 * hashtable 
 */ 
struct hash_table *hash_insert(struct hash_table *h, struct hash_entry *s) 
{ 
  struct hash_entry **base; 
 
  if(!h) h=create_hash_table(); 
 
  if(h->entries > AVERAGE_HASH_LENGTH * 2 * h->mask) 
    h=hash_rehash(h,(h->mask+1) * 2); 
 
  base = h->htable + (gobble(s->s) & h->mask); 
  s->next = *base; 
  *base = s; 
  h->entries++; 
 
  return h; 
} 
 
/* 
 * unlink an entry from an hash table 
 */ 
struct hash_table *hash_unlink(struct hash_table *h, struct hash_entry *s) 
{ 
  struct hash_entry **prev,*e; 
 
  prev = h->htable + (gobble(s->s) & h->mask); 
  for(;( e=*prev ); prev=&e->next) 
  { 
    if(e==s) 
    { 
      *prev = e->next; 
      h->entries--; 
      if(h->mask > NEW_HASHTABLE_SIZE  && 
         h->entries < AVERAGE_HASH_LENGTH / 2 * h->mask) 
        h=hash_rehash(h,(h->mask+1) / 2); 
      return h; 
    } 
  } 
#ifdef PIKE_DEBUG 
  Pike_fatal("hash_entry not in hashtable\n"); 
#endif 
  return h; 
} 
 
void map_hashtable(struct hash_table *h, void (*fun)(struct hash_entry *)) 
{ 
  INT32 e; 
  struct hash_entry *i, *n; 
  for(e=0;e<=h->mask;e++) 
  { 
    for(i=h->htable[e];i;i=n) 
    { 
      n=i->next; 
      fun(i); 
    } 
  } 
} 
 
void free_hashtable(struct hash_table *h, 
                    void (*free_entry)(struct hash_entry *)) 
{ 
  INT32 e; 
  struct hash_entry *i, *n; 
  for(e=0;e<=h->mask;e++) 
  { 
    for(i=h->htable[e];i;i=n) 
    { 
      n=i->next; 
      free_string(i->s); 
      if(free_entry) 
        free_entry(i); 
      else 
        free((char *)i); 
    } 
  } 
  free((char *)h); 
}