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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
/*
* This file is part of the KDE libraries
* Copyright (C) 2003 Apple Computer, Inc
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#include "identifier.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DUMP_STATISTICS 0
namespace KJS {
#if DUMP_STATISTICS
static int numProbes;
static int numCollisions;
struct IdentifierStatisticsExitLogger { ~IdentifierStatisticsExitLogger(); };
static IdentifierStatisticsExitLogger logger;
IdentifierStatisticsExitLogger::~IdentifierStatisticsExitLogger()
{
printf("\nKJS::Identifier statistics\n\n");
printf("%d probes\n", numProbes);
printf("%d collisions (%.1f%%)\n", numCollisions, 100.0 * numCollisions / numProbes);
}
#endif
extern const Identifier argumentsPropertyName("arguments");
extern const Identifier calleePropertyName("callee");
extern const Identifier callerPropertyName("caller");
extern const Identifier constructorPropertyName("constructor");
extern const Identifier lengthPropertyName("length");
extern const Identifier messagePropertyName("message");
extern const Identifier namePropertyName("name");
extern const Identifier prototypePropertyName("prototype");
extern const Identifier specialPrototypePropertyName("__proto__");
extern const Identifier toLocaleStringPropertyName("toLocaleString");
extern const Identifier toStringPropertyName("toString");
extern const Identifier valueOfPropertyName("valueOf");
static const int _minTableSize = 64;
UString::Rep **Identifier::_table;
int Identifier::_tableSize;
int Identifier::_tableSizeMask;
int Identifier::_keyCount;
bool Identifier::equal(UString::Rep *r, const char *s)
{
int length = r->len;
const UChar *d = r->data();
for (int i = 0; i != length; ++i)
if (d[i].uc != (unsigned char)s[i])
return false;
return s[length] == 0;
}
bool Identifier::equal(UString::Rep *r, const UChar *s, int length)
{
if (r->len != length)
return false;
const UChar *d = r->data();
for (int i = 0; i != length; ++i)
if (d[i].uc != s[i].uc)
return false;
return true;
}
bool Identifier::equal(UString::Rep *r, UString::Rep *b)
{
int length = r->len;
if (length != b->len)
return false;
const UChar *d = r->data();
const UChar *s = b->data();
for (int i = 0; i != length; ++i)
if (d[i].uc != s[i].uc)
return false;
return true;
}
UString::Rep *Identifier::add(const char *c)
{
if (!c)
return &UString::Rep::null;
int length = strlen(c);
if (length == 0)
return &UString::Rep::empty;
if (!_table)
expand();
unsigned hash = UString::Rep::computeHash(c);
int i = hash & _tableSizeMask;
#if DUMP_STATISTICS
++numProbes;
numCollisions += _table[i] && !equal(_table[i], c);
#endif
while (UString::Rep *key = _table[i]) {
if (equal(key, c))
return key;
i = (i + 1) & _tableSizeMask;
}
UChar *d = new UChar[length];
for (int j = 0; j != length; j++)
d[j] = c[j];
UString::Rep *r = new UString::Rep;
r->dat = d;
r->len = length;
r->capacity = UString::Rep::capacityForIdentifier;
r->rc = 0;
r->_hash = hash;
_table[i] = r;
++_keyCount;
if (_keyCount * 2 >= _tableSize)
expand();
return r;
}
UString::Rep *Identifier::add(const UChar *s, int length)
{
if (length == 0)
return &UString::Rep::empty;
if (!_table)
expand();
unsigned hash = UString::Rep::computeHash(s, length);
int i = hash & _tableSizeMask;
#if DUMP_STATISTICS
++numProbes;
numCollisions += _table[i] && !equal(_table[i], s, length);
#endif
while (UString::Rep *key = _table[i]) {
if (equal(key, s, length))
return key;
i = (i + 1) & _tableSizeMask;
}
UChar *d = new UChar[length];
for (int j = 0; j != length; j++)
d[j] = s[j];
UString::Rep *r = new UString::Rep;
r->dat = d;
r->len = length;
r->capacity = UString::Rep::capacityForIdentifier;
r->rc = 0;
r->_hash = hash;
_table[i] = r;
++_keyCount;
if (_keyCount * 2 >= _tableSize)
expand();
return r;
}
UString::Rep *Identifier::add(UString::Rep *r)
{
if (r->capacity == UString::Rep::capacityForIdentifier)
return r;
if (r->len == 0)
return &UString::Rep::empty;
if (!_table)
expand();
unsigned hash = r->hash();
int i = hash & _tableSizeMask;
#if DUMP_STATISTICS
++numProbes;
numCollisions += _table[i] && !equal(_table[i], r);
#endif
while (UString::Rep *key = _table[i]) {
if (equal(key, r))
return key;
i = (i + 1) & _tableSizeMask;
}
r->capacity = UString::Rep::capacityForIdentifier;
_table[i] = r;
++_keyCount;
if (_keyCount * 2 >= _tableSize)
expand();
return r;
}
inline void Identifier::insert(UString::Rep *key)
{
unsigned hash = key->hash();
int i = hash & _tableSizeMask;
#if DUMP_STATISTICS
++numProbes;
numCollisions += _table[i] != 0;
#endif
while (_table[i])
i = (i + 1) & _tableSizeMask;
_table[i] = key;
}
void Identifier::remove(UString::Rep *r)
{
unsigned hash = r->hash();
UString::Rep *key;
int i = hash & _tableSizeMask;
#if DUMP_STATISTICS
++numProbes;
numCollisions += _table[i] && equal(_table[i], r);
#endif
while ((key = _table[i])) {
if (equal(key, r))
break;
i = (i + 1) & _tableSizeMask;
}
if (!key)
return;
_table[i] = 0;
--_keyCount;
if (_keyCount * 6 < _tableSize && _tableSize > _minTableSize) {
shrink();
return;
}
// Reinsert all the items to the right in the same cluster.
while (1) {
i = (i + 1) & _tableSizeMask;
key = _table[i];
if (!key)
break;
_table[i] = 0;
insert(key);
}
}
void Identifier::expand()
{
rehash(_tableSize == 0 ? _minTableSize : _tableSize * 2);
}
void Identifier::shrink()
{
rehash(_tableSize / 2);
}
void Identifier::rehash(int newTableSize)
{
int oldTableSize = _tableSize;
UString::Rep **oldTable = _table;
_tableSize = newTableSize;
_tableSizeMask = newTableSize - 1;
_table = (UString::Rep **)calloc(newTableSize, sizeof(UString::Rep *));
for (int i = 0; i != oldTableSize; ++i)
if (UString::Rep *key = oldTable[i])
insert(key);
free(oldTable);
}
const Identifier &Identifier::null()
{
static Identifier null;
return null;
}
} // namespace KJS
|