2001-09-20 00:02:59 +00:00
|
|
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
1998-10-14 10:22:38 +00:00
|
|
|
*
|
2001-09-20 00:02:59 +00:00
|
|
|
* The contents of this file are subject to the Netscape Public
|
1999-09-28 23:12:09 +00:00
|
|
|
* License Version 1.1 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
2001-09-20 00:02:59 +00:00
|
|
|
* the License at http://www.mozilla.org/NPL/
|
1998-10-14 10:22:38 +00:00
|
|
|
*
|
1999-09-28 23:12:09 +00:00
|
|
|
* Software distributed under the License is distributed on an "AS
|
2001-09-20 00:02:59 +00:00
|
|
|
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express oqr
|
1999-09-28 23:12:09 +00:00
|
|
|
* implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
1998-10-14 10:22:38 +00:00
|
|
|
*
|
2001-09-20 00:02:59 +00:00
|
|
|
* The Original Code is Mozilla Communicator client code, released
|
|
|
|
* March 31, 1998.
|
1999-09-28 23:12:09 +00:00
|
|
|
*
|
2001-09-20 00:02:59 +00:00
|
|
|
* The Initial Developer of the Original Code is Netscape
|
|
|
|
* Communications Corporation. Portions created by Netscape are
|
1999-09-28 23:12:09 +00:00
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
2001-09-20 00:02:59 +00:00
|
|
|
* Contributor(s):
|
1999-09-28 23:12:09 +00:00
|
|
|
*
|
2001-09-20 00:02:59 +00:00
|
|
|
* Alternatively, the contents of this file may be used under the
|
|
|
|
* terms of the GNU Public License (the "GPL"), in which case the
|
|
|
|
* provisions of the GPL are applicable instead of those above.
|
|
|
|
* If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of the GPL and not to allow others to use your
|
|
|
|
* version of this file under the NPL, indicate your decision by
|
|
|
|
* deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this
|
|
|
|
* file under either the NPL or the GPL.
|
|
|
|
*/
|
1998-09-02 22:37:14 +00:00
|
|
|
|
|
|
|
/*
|
1998-10-14 10:22:38 +00:00
|
|
|
* PR hash table package.
|
1998-09-02 22:37:14 +00:00
|
|
|
*/
|
2000-05-17 06:24:45 +00:00
|
|
|
#include "jsstddef.h"
|
1998-09-02 22:37:14 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1998-10-14 10:22:38 +00:00
|
|
|
#include "jstypes.h"
|
|
|
|
#include "jsbit.h"
|
|
|
|
#include "jsutil.h" /* Added by JSIFY */
|
|
|
|
#include "jshash.h" /* Added by JSIFY */
|
1998-09-02 22:37:14 +00:00
|
|
|
|
|
|
|
/* Compute the number of buckets in ht */
|
1998-10-14 10:22:38 +00:00
|
|
|
#define NBUCKETS(ht) JS_BIT(JS_HASH_BITS - (ht)->shift)
|
1998-09-02 22:37:14 +00:00
|
|
|
|
|
|
|
/* The smallest table has 16 buckets */
|
|
|
|
#define MINBUCKETSLOG2 4
|
1998-10-14 10:22:38 +00:00
|
|
|
#define MINBUCKETS JS_BIT(MINBUCKETSLOG2)
|
1998-09-02 22:37:14 +00:00
|
|
|
|
|
|
|
/* Compute the maximum entries given n buckets that we will tolerate, ~90% */
|
|
|
|
#define OVERLOADED(n) ((n) - ((n) >> 3))
|
|
|
|
|
|
|
|
/* Compute the number of entries below which we shrink the table by half */
|
|
|
|
#define UNDERLOADED(n) (((n) > MINBUCKETS) ? ((n) >> 2) : 0)
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Stubs for default hash allocator ops.
|
|
|
|
*/
|
1998-10-14 10:22:38 +00:00
|
|
|
static void *
|
|
|
|
DefaultAllocTable(void *pool, size_t size)
|
1998-09-02 22:37:14 +00:00
|
|
|
{
|
1998-10-14 10:22:38 +00:00
|
|
|
return malloc(size);
|
1998-09-02 22:37:14 +00:00
|
|
|
}
|
|
|
|
|
1998-10-14 10:22:38 +00:00
|
|
|
static void
|
1998-09-02 22:37:14 +00:00
|
|
|
DefaultFreeTable(void *pool, void *item)
|
|
|
|
{
|
1998-10-14 10:22:38 +00:00
|
|
|
free(item);
|
1998-09-02 22:37:14 +00:00
|
|
|
}
|
|
|
|
|
1998-10-14 10:22:38 +00:00
|
|
|
static JSHashEntry *
|
1998-09-02 22:37:14 +00:00
|
|
|
DefaultAllocEntry(void *pool, const void *key)
|
|
|
|
{
|
2000-02-02 01:10:31 +00:00
|
|
|
return (JSHashEntry*) malloc(sizeof(JSHashEntry));
|
1998-09-02 22:37:14 +00:00
|
|
|
}
|
|
|
|
|
1998-10-14 10:22:38 +00:00
|
|
|
static void
|
|
|
|
DefaultFreeEntry(void *pool, JSHashEntry *he, uintN flag)
|
1998-09-02 22:37:14 +00:00
|
|
|
{
|
|
|
|
if (flag == HT_FREE_ENTRY)
|
1998-10-14 10:22:38 +00:00
|
|
|
free(he);
|
1998-09-02 22:37:14 +00:00
|
|
|
}
|
|
|
|
|
1998-10-14 10:22:38 +00:00
|
|
|
static JSHashAllocOps defaultHashAllocOps = {
|
1998-09-02 22:37:14 +00:00
|
|
|
DefaultAllocTable, DefaultFreeTable,
|
|
|
|
DefaultAllocEntry, DefaultFreeEntry
|
|
|
|
};
|
|
|
|
|
1999-11-23 01:02:28 +00:00
|
|
|
JS_PUBLIC_API(JSHashTable *)
|
1998-10-14 10:22:38 +00:00
|
|
|
JS_NewHashTable(uint32 n, JSHashFunction keyHash,
|
|
|
|
JSHashComparator keyCompare, JSHashComparator valueCompare,
|
|
|
|
JSHashAllocOps *allocOps, void *allocPriv)
|
1998-09-02 22:37:14 +00:00
|
|
|
{
|
1998-10-14 10:22:38 +00:00
|
|
|
JSHashTable *ht;
|
|
|
|
size_t nb;
|
1998-09-02 22:37:14 +00:00
|
|
|
|
|
|
|
if (n <= MINBUCKETS) {
|
|
|
|
n = MINBUCKETSLOG2;
|
|
|
|
} else {
|
1998-10-14 10:22:38 +00:00
|
|
|
n = JS_CeilingLog2(n);
|
|
|
|
if ((int32)n < 0)
|
|
|
|
return NULL;
|
1998-09-02 22:37:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!allocOps) allocOps = &defaultHashAllocOps;
|
|
|
|
|
2000-02-02 01:10:31 +00:00
|
|
|
ht = (JSHashTable*) (*allocOps->allocTable)(allocPriv, sizeof *ht);
|
1998-09-02 22:37:14 +00:00
|
|
|
if (!ht)
|
1998-10-14 10:22:38 +00:00
|
|
|
return NULL;
|
1998-09-02 22:37:14 +00:00
|
|
|
memset(ht, 0, sizeof *ht);
|
1998-10-14 10:22:38 +00:00
|
|
|
ht->shift = JS_HASH_BITS - n;
|
|
|
|
n = JS_BIT(n);
|
|
|
|
#if defined(XP_PC) && defined _MSC_VER && _MSC_VER <= 800
|
1998-09-02 22:37:14 +00:00
|
|
|
if (n > 16000) {
|
|
|
|
(*allocOps->freeTable)(allocPriv, ht);
|
1998-10-14 10:22:38 +00:00
|
|
|
return NULL;
|
1998-09-02 22:37:14 +00:00
|
|
|
}
|
|
|
|
#endif /* WIN16 */
|
1998-10-14 10:22:38 +00:00
|
|
|
nb = n * sizeof(JSHashEntry *);
|
2000-02-02 01:10:31 +00:00
|
|
|
ht->buckets = (JSHashEntry**) (*allocOps->allocTable)(allocPriv, nb);
|
1998-09-02 22:37:14 +00:00
|
|
|
if (!ht->buckets) {
|
|
|
|
(*allocOps->freeTable)(allocPriv, ht);
|
1998-10-14 10:22:38 +00:00
|
|
|
return NULL;
|
1998-09-02 22:37:14 +00:00
|
|
|
}
|
|
|
|
memset(ht->buckets, 0, nb);
|
|
|
|
|
|
|
|
ht->keyHash = keyHash;
|
|
|
|
ht->keyCompare = keyCompare;
|
|
|
|
ht->valueCompare = valueCompare;
|
|
|
|
ht->allocOps = allocOps;
|
|
|
|
ht->allocPriv = allocPriv;
|
|
|
|
return ht;
|
|
|
|
}
|
|
|
|
|
1999-11-23 01:02:28 +00:00
|
|
|
JS_PUBLIC_API(void)
|
1998-10-14 10:22:38 +00:00
|
|
|
JS_HashTableDestroy(JSHashTable *ht)
|
1998-09-02 22:37:14 +00:00
|
|
|
{
|
1998-10-14 10:22:38 +00:00
|
|
|
uint32 i, n;
|
2001-05-17 01:43:58 +00:00
|
|
|
JSHashEntry *he, **hep;
|
1998-10-14 10:22:38 +00:00
|
|
|
JSHashAllocOps *allocOps = ht->allocOps;
|
1998-09-02 22:37:14 +00:00
|
|
|
void *allocPriv = ht->allocPriv;
|
|
|
|
|
|
|
|
n = NBUCKETS(ht);
|
|
|
|
for (i = 0; i < n; i++) {
|
2001-05-17 01:43:58 +00:00
|
|
|
hep = &ht->buckets[i];
|
|
|
|
while ((he = *hep) != NULL) {
|
|
|
|
*hep = he->next;
|
1998-09-02 22:37:14 +00:00
|
|
|
(*allocOps->freeEntry)(allocPriv, he, HT_FREE_ENTRY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
memset(ht->buckets, 0xDB, n * sizeof ht->buckets[0]);
|
|
|
|
#endif
|
|
|
|
(*allocOps->freeTable)(allocPriv, ht->buckets);
|
|
|
|
#ifdef DEBUG
|
|
|
|
memset(ht, 0xDB, sizeof *ht);
|
|
|
|
#endif
|
|
|
|
(*allocOps->freeTable)(allocPriv, ht);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Multiplicative hash, from Knuth 6.4.
|
|
|
|
*/
|
1999-11-23 01:02:28 +00:00
|
|
|
JS_PUBLIC_API(JSHashEntry **)
|
1998-10-14 10:22:38 +00:00
|
|
|
JS_HashTableRawLookup(JSHashTable *ht, JSHashNumber keyHash, const void *key)
|
1998-09-02 22:37:14 +00:00
|
|
|
{
|
1998-10-14 10:22:38 +00:00
|
|
|
JSHashEntry *he, **hep, **hep0;
|
|
|
|
JSHashNumber h;
|
1998-09-02 22:37:14 +00:00
|
|
|
|
|
|
|
#ifdef HASHMETER
|
|
|
|
ht->nlookups++;
|
|
|
|
#endif
|
1998-10-14 10:22:38 +00:00
|
|
|
h = keyHash * JS_GOLDEN_RATIO;
|
1998-09-02 22:37:14 +00:00
|
|
|
h >>= ht->shift;
|
|
|
|
hep = hep0 = &ht->buckets[h];
|
1998-10-14 10:22:38 +00:00
|
|
|
while ((he = *hep) != NULL) {
|
1998-09-02 22:37:14 +00:00
|
|
|
if (he->keyHash == keyHash && (*ht->keyCompare)(key, he->key)) {
|
|
|
|
/* Move to front of chain if not already there */
|
|
|
|
if (hep != hep0) {
|
|
|
|
*hep = he->next;
|
|
|
|
he->next = *hep0;
|
|
|
|
*hep0 = he;
|
|
|
|
}
|
|
|
|
return hep0;
|
|
|
|
}
|
|
|
|
hep = &he->next;
|
|
|
|
#ifdef HASHMETER
|
|
|
|
ht->nsteps++;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return hep;
|
|
|
|
}
|
|
|
|
|
1999-11-23 01:02:28 +00:00
|
|
|
JS_PUBLIC_API(JSHashEntry *)
|
1998-10-14 10:22:38 +00:00
|
|
|
JS_HashTableRawAdd(JSHashTable *ht, JSHashEntry **hep,
|
|
|
|
JSHashNumber keyHash, const void *key, void *value)
|
1998-09-02 22:37:14 +00:00
|
|
|
{
|
1998-10-14 10:22:38 +00:00
|
|
|
uint32 i, n;
|
|
|
|
JSHashEntry *he, *next, **oldbuckets;
|
|
|
|
size_t nb;
|
1998-09-02 22:37:14 +00:00
|
|
|
|
|
|
|
/* Grow the table if it is overloaded */
|
|
|
|
n = NBUCKETS(ht);
|
|
|
|
if (ht->nentries >= OVERLOADED(n)) {
|
|
|
|
oldbuckets = ht->buckets;
|
1998-10-14 10:22:38 +00:00
|
|
|
#if defined(XP_PC) && defined _MSC_VER && _MSC_VER <= 800
|
1998-09-02 22:37:14 +00:00
|
|
|
if (2 * n > 16000)
|
1998-10-14 10:22:38 +00:00
|
|
|
return NULL;
|
1998-09-02 22:37:14 +00:00
|
|
|
#endif /* WIN16 */
|
1998-10-14 10:22:38 +00:00
|
|
|
nb = 2 * n * sizeof(JSHashEntry *);
|
2000-02-02 01:10:31 +00:00
|
|
|
ht->buckets = (JSHashEntry**) (*ht->allocOps->allocTable)(ht->allocPriv, nb);
|
1998-09-02 22:37:14 +00:00
|
|
|
if (!ht->buckets) {
|
|
|
|
ht->buckets = oldbuckets;
|
1998-10-14 10:22:38 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
1998-09-02 22:37:14 +00:00
|
|
|
memset(ht->buckets, 0, nb);
|
2001-08-07 02:48:10 +00:00
|
|
|
#ifdef HASHMETER
|
|
|
|
ht->ngrows++;
|
|
|
|
#endif
|
|
|
|
ht->shift--;
|
1998-09-02 22:37:14 +00:00
|
|
|
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
for (he = oldbuckets[i]; he; he = next) {
|
|
|
|
next = he->next;
|
1998-10-14 10:22:38 +00:00
|
|
|
hep = JS_HashTableRawLookup(ht, he->keyHash, he->key);
|
|
|
|
JS_ASSERT(*hep == NULL);
|
|
|
|
he->next = NULL;
|
1998-09-02 22:37:14 +00:00
|
|
|
*hep = he;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
memset(oldbuckets, 0xDB, n * sizeof oldbuckets[0]);
|
|
|
|
#endif
|
|
|
|
(*ht->allocOps->freeTable)(ht->allocPriv, oldbuckets);
|
1998-10-14 10:22:38 +00:00
|
|
|
hep = JS_HashTableRawLookup(ht, keyHash, key);
|
1998-09-02 22:37:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Make a new key value entry */
|
|
|
|
he = (*ht->allocOps->allocEntry)(ht->allocPriv, key);
|
|
|
|
if (!he)
|
1998-10-14 10:22:38 +00:00
|
|
|
return NULL;
|
1998-09-02 22:37:14 +00:00
|
|
|
he->keyHash = keyHash;
|
|
|
|
he->key = key;
|
|
|
|
he->value = value;
|
|
|
|
he->next = *hep;
|
|
|
|
*hep = he;
|
|
|
|
ht->nentries++;
|
|
|
|
return he;
|
|
|
|
}
|
|
|
|
|
1999-11-23 01:02:28 +00:00
|
|
|
JS_PUBLIC_API(JSHashEntry *)
|
1998-10-14 10:22:38 +00:00
|
|
|
JS_HashTableAdd(JSHashTable *ht, const void *key, void *value)
|
1998-09-02 22:37:14 +00:00
|
|
|
{
|
1998-10-14 10:22:38 +00:00
|
|
|
JSHashNumber keyHash;
|
|
|
|
JSHashEntry *he, **hep;
|
1998-09-02 22:37:14 +00:00
|
|
|
|
|
|
|
keyHash = (*ht->keyHash)(key);
|
1998-10-14 10:22:38 +00:00
|
|
|
hep = JS_HashTableRawLookup(ht, keyHash, key);
|
|
|
|
if ((he = *hep) != NULL) {
|
1998-09-02 22:37:14 +00:00
|
|
|
/* Hit; see if values match */
|
|
|
|
if ((*ht->valueCompare)(he->value, value)) {
|
|
|
|
/* key,value pair is already present in table */
|
|
|
|
return he;
|
|
|
|
}
|
|
|
|
if (he->value)
|
|
|
|
(*ht->allocOps->freeEntry)(ht->allocPriv, he, HT_FREE_VALUE);
|
|
|
|
he->value = value;
|
|
|
|
return he;
|
|
|
|
}
|
1998-10-14 10:22:38 +00:00
|
|
|
return JS_HashTableRawAdd(ht, hep, keyHash, key, value);
|
1998-09-02 22:37:14 +00:00
|
|
|
}
|
|
|
|
|
1999-11-23 01:02:28 +00:00
|
|
|
JS_PUBLIC_API(void)
|
1998-10-14 10:22:38 +00:00
|
|
|
JS_HashTableRawRemove(JSHashTable *ht, JSHashEntry **hep, JSHashEntry *he)
|
1998-09-02 22:37:14 +00:00
|
|
|
{
|
1998-10-14 10:22:38 +00:00
|
|
|
uint32 i, n;
|
|
|
|
JSHashEntry *next, **oldbuckets;
|
|
|
|
size_t nb;
|
1998-09-02 22:37:14 +00:00
|
|
|
|
|
|
|
*hep = he->next;
|
|
|
|
(*ht->allocOps->freeEntry)(ht->allocPriv, he, HT_FREE_ENTRY);
|
|
|
|
|
|
|
|
/* Shrink table if it's underloaded */
|
|
|
|
n = NBUCKETS(ht);
|
|
|
|
if (--ht->nentries < UNDERLOADED(n)) {
|
|
|
|
oldbuckets = ht->buckets;
|
1998-10-14 10:22:38 +00:00
|
|
|
nb = n * sizeof(JSHashEntry*) / 2;
|
2000-02-02 01:10:31 +00:00
|
|
|
ht->buckets = (JSHashEntry**) (*ht->allocOps->allocTable)(ht->allocPriv, nb);
|
1998-09-02 22:37:14 +00:00
|
|
|
if (!ht->buckets) {
|
|
|
|
ht->buckets = oldbuckets;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
memset(ht->buckets, 0, nb);
|
2001-08-07 02:48:10 +00:00
|
|
|
#ifdef HASHMETER
|
|
|
|
ht->nshrinks++;
|
|
|
|
#endif
|
|
|
|
ht->shift++;
|
1998-09-02 22:37:14 +00:00
|
|
|
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
for (he = oldbuckets[i]; he; he = next) {
|
|
|
|
next = he->next;
|
1998-10-14 10:22:38 +00:00
|
|
|
hep = JS_HashTableRawLookup(ht, he->keyHash, he->key);
|
|
|
|
JS_ASSERT(*hep == NULL);
|
|
|
|
he->next = NULL;
|
1998-09-02 22:37:14 +00:00
|
|
|
*hep = he;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
memset(oldbuckets, 0xDB, n * sizeof oldbuckets[0]);
|
|
|
|
#endif
|
|
|
|
(*ht->allocOps->freeTable)(ht->allocPriv, oldbuckets);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-11-23 01:02:28 +00:00
|
|
|
JS_PUBLIC_API(JSBool)
|
1998-10-14 10:22:38 +00:00
|
|
|
JS_HashTableRemove(JSHashTable *ht, const void *key)
|
1998-09-02 22:37:14 +00:00
|
|
|
{
|
1998-10-14 10:22:38 +00:00
|
|
|
JSHashNumber keyHash;
|
|
|
|
JSHashEntry *he, **hep;
|
1998-09-02 22:37:14 +00:00
|
|
|
|
|
|
|
keyHash = (*ht->keyHash)(key);
|
1998-10-14 10:22:38 +00:00
|
|
|
hep = JS_HashTableRawLookup(ht, keyHash, key);
|
|
|
|
if ((he = *hep) == NULL)
|
|
|
|
return JS_FALSE;
|
1998-09-02 22:37:14 +00:00
|
|
|
|
|
|
|
/* Hit; remove element */
|
1998-10-14 10:22:38 +00:00
|
|
|
JS_HashTableRawRemove(ht, hep, he);
|
|
|
|
return JS_TRUE;
|
1998-09-02 22:37:14 +00:00
|
|
|
}
|
|
|
|
|
1999-11-23 01:02:28 +00:00
|
|
|
JS_PUBLIC_API(void *)
|
1998-10-14 10:22:38 +00:00
|
|
|
JS_HashTableLookup(JSHashTable *ht, const void *key)
|
1998-09-02 22:37:14 +00:00
|
|
|
{
|
1998-10-14 10:22:38 +00:00
|
|
|
JSHashNumber keyHash;
|
|
|
|
JSHashEntry *he, **hep;
|
1998-09-02 22:37:14 +00:00
|
|
|
|
|
|
|
keyHash = (*ht->keyHash)(key);
|
1998-10-14 10:22:38 +00:00
|
|
|
hep = JS_HashTableRawLookup(ht, keyHash, key);
|
|
|
|
if ((he = *hep) != NULL) {
|
1998-09-02 22:37:14 +00:00
|
|
|
return he->value;
|
|
|
|
}
|
1998-10-14 10:22:38 +00:00
|
|
|
return NULL;
|
1998-09-02 22:37:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Iterate over the entries in the hash table calling func for each
|
1998-10-14 10:22:38 +00:00
|
|
|
** entry found. Stop if "f" says to (return value & JS_ENUMERATE_STOP).
|
1998-09-02 22:37:14 +00:00
|
|
|
** Return a count of the number of elements scanned.
|
|
|
|
*/
|
1999-11-23 01:02:28 +00:00
|
|
|
JS_PUBLIC_API(int)
|
1998-10-14 10:22:38 +00:00
|
|
|
JS_HashTableEnumerateEntries(JSHashTable *ht, JSHashEnumerator f, void *arg)
|
1998-09-02 22:37:14 +00:00
|
|
|
{
|
1998-10-14 10:22:38 +00:00
|
|
|
JSHashEntry *he, **hep;
|
|
|
|
uint32 i, nbuckets;
|
1998-09-02 22:37:14 +00:00
|
|
|
int rv, n = 0;
|
1998-10-14 10:22:38 +00:00
|
|
|
JSHashEntry *todo = NULL;
|
1998-09-02 22:37:14 +00:00
|
|
|
|
|
|
|
nbuckets = NBUCKETS(ht);
|
|
|
|
for (i = 0; i < nbuckets; i++) {
|
|
|
|
hep = &ht->buckets[i];
|
1998-10-14 10:22:38 +00:00
|
|
|
while ((he = *hep) != NULL) {
|
1998-09-02 22:37:14 +00:00
|
|
|
rv = (*f)(he, n, arg);
|
|
|
|
n++;
|
|
|
|
if (rv & (HT_ENUMERATE_REMOVE | HT_ENUMERATE_UNHASH)) {
|
|
|
|
*hep = he->next;
|
|
|
|
if (rv & HT_ENUMERATE_REMOVE) {
|
|
|
|
he->next = todo;
|
|
|
|
todo = he;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
hep = &he->next;
|
|
|
|
}
|
|
|
|
if (rv & HT_ENUMERATE_STOP) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
hep = &todo;
|
1998-10-14 10:22:38 +00:00
|
|
|
while ((he = *hep) != NULL) {
|
|
|
|
JS_HashTableRawRemove(ht, hep, he);
|
1998-09-02 22:37:14 +00:00
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HASHMETER
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
1999-11-23 01:02:28 +00:00
|
|
|
JS_PUBLIC_API(void)
|
1998-10-14 10:22:38 +00:00
|
|
|
JS_HashTableDumpMeter(JSHashTable *ht, JSHashEnumerator dump, FILE *fp)
|
1998-09-02 22:37:14 +00:00
|
|
|
{
|
2000-05-20 18:41:13 +00:00
|
|
|
double sqsum, mean, variance, sigma;
|
|
|
|
uint32 nchains, nbuckets, nentries;
|
1998-10-14 10:22:38 +00:00
|
|
|
uint32 i, n, maxChain, maxChainLen;
|
|
|
|
JSHashEntry *he;
|
1998-09-02 22:37:14 +00:00
|
|
|
|
2000-05-20 18:41:13 +00:00
|
|
|
sqsum = 0;
|
1998-09-02 22:37:14 +00:00
|
|
|
nchains = 0;
|
|
|
|
maxChainLen = 0;
|
|
|
|
nbuckets = NBUCKETS(ht);
|
|
|
|
for (i = 0; i < nbuckets; i++) {
|
|
|
|
he = ht->buckets[i];
|
|
|
|
if (!he)
|
|
|
|
continue;
|
|
|
|
nchains++;
|
|
|
|
for (n = 0; he; he = he->next)
|
|
|
|
n++;
|
2000-05-20 18:41:13 +00:00
|
|
|
sqsum += n * n;
|
1998-09-02 22:37:14 +00:00
|
|
|
if (n > maxChainLen) {
|
|
|
|
maxChainLen = n;
|
|
|
|
maxChain = i;
|
|
|
|
}
|
|
|
|
}
|
2000-05-20 18:41:13 +00:00
|
|
|
nentries = ht->nentries;
|
|
|
|
mean = (double)nentries / nchains;
|
|
|
|
variance = nchains * sqsum - nentries * nentries;
|
|
|
|
if (variance < 0 || nchains == 1)
|
|
|
|
variance = 0;
|
|
|
|
else
|
|
|
|
variance /= nchains * (nchains - 1);
|
|
|
|
sigma = sqrt(variance);
|
1998-09-02 22:37:14 +00:00
|
|
|
|
|
|
|
fprintf(fp, "\nHash table statistics:\n");
|
|
|
|
fprintf(fp, " number of lookups: %u\n", ht->nlookups);
|
|
|
|
fprintf(fp, " number of entries: %u\n", ht->nentries);
|
|
|
|
fprintf(fp, " number of grows: %u\n", ht->ngrows);
|
|
|
|
fprintf(fp, " number of shrinks: %u\n", ht->nshrinks);
|
|
|
|
fprintf(fp, " mean steps per hash: %g\n", (double)ht->nsteps
|
|
|
|
/ ht->nlookups);
|
|
|
|
fprintf(fp, "mean hash chain length: %g\n", mean);
|
2000-05-20 18:41:13 +00:00
|
|
|
fprintf(fp, " standard deviation: %g\n", sigma);
|
1998-09-02 22:37:14 +00:00
|
|
|
fprintf(fp, " max hash chain length: %u\n", maxChainLen);
|
|
|
|
fprintf(fp, " max hash chain: [%u]\n", maxChain);
|
|
|
|
|
|
|
|
for (he = ht->buckets[maxChain], i = 0; he; he = he->next, i++)
|
|
|
|
if ((*dump)(he, i, fp) != HT_ENUMERATE_NEXT)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif /* HASHMETER */
|
|
|
|
|
1999-11-23 01:02:28 +00:00
|
|
|
JS_PUBLIC_API(int)
|
1998-10-14 10:22:38 +00:00
|
|
|
JS_HashTableDump(JSHashTable *ht, JSHashEnumerator dump, FILE *fp)
|
1998-09-02 22:37:14 +00:00
|
|
|
{
|
|
|
|
int count;
|
|
|
|
|
1998-10-14 10:22:38 +00:00
|
|
|
count = JS_HashTableEnumerateEntries(ht, dump, fp);
|
1998-09-02 22:37:14 +00:00
|
|
|
#ifdef HASHMETER
|
1998-10-14 10:22:38 +00:00
|
|
|
JS_HashTableDumpMeter(ht, dump, fp);
|
1998-09-02 22:37:14 +00:00
|
|
|
#endif
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
1999-11-23 01:02:28 +00:00
|
|
|
JS_PUBLIC_API(JSHashNumber)
|
1998-10-14 10:22:38 +00:00
|
|
|
JS_HashString(const void *key)
|
1998-09-02 22:37:14 +00:00
|
|
|
{
|
1998-10-14 10:22:38 +00:00
|
|
|
JSHashNumber h;
|
|
|
|
const unsigned char *s;
|
1998-09-02 22:37:14 +00:00
|
|
|
|
|
|
|
h = 0;
|
2000-02-02 01:10:31 +00:00
|
|
|
for (s = (const unsigned char *)key; *s; s++)
|
- [jsemit.c] Fix horrid stupid bugs generating JSOP_ARGCNT and JSOP_ARGSUB,
where any occurrence of arguments.length or arguments[0], e.g., would be
"optimized" to use those bytecodes. This is just wrong if the occurrence
is an operand of delete, ++, --, or the left-hand-side of an assignment
operator!
- [jsfun.c, jsinterp.c] args_getProperty etc. must use JS_GetInstancePrivate,
not JS_GetPrivate, as the arguments object is exposed, and can be made a
prototype of other objects that do not have private data, or private data
that's a JSStackFrame*. Same goes for fun_getProperty, js_GetArgument, etc.
- [jsfun.c, jsobj.c, jsstr.c] No need to specialize fun_delProperty and
str_delProperty to help convince users and ECMA conformance tests that
fun.length and str.length are direct properties of instances, instead of
being delegated to Function.prototype.length and String.prototype.length.
This special case is done universally in js_DeleteProperty for all SHARED
and PERMANENT proto-properties.
- [jshash.c] Sneaking this followup-fix for bug 69271 in: use JS_HASH_BITS
rather than hardcoded 32.
- [jsobj.c, jsscope.[ch]] Fix misnamed js_HashValue (it takes a jsid, so it
is now js_HashId).
- [jsscript.c] script_compile needs to call JS_InstanceOf, to ensure that obj
is a Script object.
2001-03-22 02:52:42 +00:00
|
|
|
h = (h >> (JS_HASH_BITS - 4)) ^ (h << 4) ^ *s;
|
1998-09-02 22:37:14 +00:00
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
1999-11-23 01:02:28 +00:00
|
|
|
JS_PUBLIC_API(int)
|
1998-10-14 10:22:38 +00:00
|
|
|
JS_CompareValues(const void *v1, const void *v2)
|
1998-09-02 22:37:14 +00:00
|
|
|
{
|
|
|
|
return v1 == v2;
|
|
|
|
}
|