1998-03-28 05:44:41 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/*
|
|
|
|
* The contents of this file are subject to the Netscape Public License
|
|
|
|
* Version 1.0 (the "NPL"); you may not use this file except in
|
|
|
|
* compliance with the NPL. You may obtain a copy of the NPL at
|
|
|
|
* http://www.mozilla.org/NPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the NPL is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* NPL.
|
|
|
|
*
|
|
|
|
* The Initial Developer of this code under the NPL is Netscape
|
|
|
|
* Communications Corporation. Portions created by Netscape are
|
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
|
|
|
|
* Reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "prmem.h"
|
1999-03-30 12:15:39 +04:00
|
|
|
#include "prlog.h"
|
1998-03-28 05:44:41 +03:00
|
|
|
#include "nsHashtable.h"
|
|
|
|
|
|
|
|
//
|
|
|
|
// Key operations
|
|
|
|
//
|
|
|
|
|
|
|
|
static PR_CALLBACK PLHashNumber _hashValue(const void *key)
|
|
|
|
{
|
1998-05-23 01:56:30 +04:00
|
|
|
return ((const nsHashKey *) key)->HashValue();
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PR_CALLBACK PRIntn _hashKeyCompare(const void *key1, const void *key2) {
|
1998-05-23 01:56:30 +04:00
|
|
|
return ((const nsHashKey *) key1)->Equals((const nsHashKey *) key2);
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PR_CALLBACK PRIntn _hashValueCompare(const void *value1,
|
|
|
|
const void *value2) {
|
|
|
|
// We're not going to make any assumptions about value equality
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Memory callbacks
|
|
|
|
//
|
|
|
|
|
|
|
|
static PR_CALLBACK void *_hashAllocTable(void *pool, PRSize size) {
|
|
|
|
return PR_MALLOC(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PR_CALLBACK void _hashFreeTable(void *pool, void *item) {
|
|
|
|
PR_DELETE(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PR_CALLBACK PLHashEntry *_hashAllocEntry(void *pool, const void *key) {
|
|
|
|
return PR_NEW(PLHashEntry);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PR_CALLBACK void _hashFreeEntry(void *pool, PLHashEntry *entry,
|
|
|
|
PRUintn flag) {
|
|
|
|
if (flag == HT_FREE_ENTRY) {
|
|
|
|
delete (nsHashKey *) (entry->key);
|
|
|
|
PR_DELETE(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static PLHashAllocOps _hashAllocOps = {
|
|
|
|
_hashAllocTable, _hashFreeTable,
|
|
|
|
_hashAllocEntry, _hashFreeEntry
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// Enumerator callback
|
|
|
|
//
|
|
|
|
|
1998-09-01 04:16:47 +04:00
|
|
|
struct _HashEnumerateArgs {
|
|
|
|
nsHashtableEnumFunc fn;
|
|
|
|
void* arg;
|
|
|
|
};
|
|
|
|
|
1998-03-28 05:44:41 +03:00
|
|
|
static PR_CALLBACK PRIntn _hashEnumerate(PLHashEntry *he, PRIntn i, void *arg)
|
|
|
|
{
|
1998-09-01 04:16:47 +04:00
|
|
|
_HashEnumerateArgs* thunk = (_HashEnumerateArgs*)arg;
|
|
|
|
return thunk->fn((nsHashKey *) he->key, he->value, thunk->arg)
|
|
|
|
? HT_ENUMERATE_NEXT
|
|
|
|
: HT_ENUMERATE_STOP;
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
|
|
|
|
1998-05-23 01:56:30 +04:00
|
|
|
//
|
|
|
|
// HashKey
|
|
|
|
//
|
|
|
|
nsHashKey::nsHashKey(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsHashKey::~nsHashKey(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
1999-03-30 12:15:39 +04:00
|
|
|
nsHashtable::nsHashtable(PRUint32 aInitSize, PRBool threadSafe)
|
|
|
|
: mLock(NULL)
|
|
|
|
{
|
1998-03-28 05:44:41 +03:00
|
|
|
hashtable = PL_NewHashTable(aInitSize,
|
|
|
|
_hashValue,
|
|
|
|
_hashKeyCompare,
|
|
|
|
_hashValueCompare,
|
|
|
|
&_hashAllocOps,
|
|
|
|
NULL);
|
1999-03-30 12:15:39 +04:00
|
|
|
if (threadSafe == PR_TRUE)
|
|
|
|
{
|
|
|
|
mLock = PR_NewLock();
|
|
|
|
if (mLock == NULL)
|
|
|
|
{
|
|
|
|
// Cannot create a lock. If running on a multiprocessing system
|
|
|
|
// we are sure to die.
|
|
|
|
PR_ASSERT(mLock != NULL);
|
|
|
|
}
|
|
|
|
}
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsHashtable::~nsHashtable() {
|
|
|
|
PL_HashTableDestroy(hashtable);
|
1999-03-30 12:15:39 +04:00
|
|
|
if (mLock) PR_DestroyLock(mLock);
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
|
|
|
|
1999-03-19 08:51:49 +03:00
|
|
|
PRBool nsHashtable::Exists(nsHashKey *aKey)
|
|
|
|
{
|
|
|
|
PLHashNumber hash = aKey->HashValue();
|
1999-03-30 12:15:39 +04:00
|
|
|
|
|
|
|
if (mLock) PR_Lock(mLock);
|
|
|
|
|
1999-03-19 08:51:49 +03:00
|
|
|
PLHashEntry **hep = PL_HashTableRawLookup(hashtable, hash, (void *) aKey);
|
|
|
|
|
1999-03-30 12:15:39 +04:00
|
|
|
if (mLock) PR_Unlock(mLock);
|
|
|
|
|
1999-03-19 08:51:49 +03:00
|
|
|
return *hep != NULL;
|
|
|
|
}
|
|
|
|
|
1998-03-28 05:44:41 +03:00
|
|
|
void *nsHashtable::Put(nsHashKey *aKey, void *aData) {
|
|
|
|
void *res = NULL;
|
|
|
|
PLHashNumber hash = aKey->HashValue();
|
|
|
|
PLHashEntry *he;
|
1999-03-30 12:15:39 +04:00
|
|
|
|
|
|
|
if (mLock) PR_Lock(mLock);
|
|
|
|
|
1998-03-28 05:44:41 +03:00
|
|
|
PLHashEntry **hep = PL_HashTableRawLookup(hashtable, hash, (void *) aKey);
|
|
|
|
|
|
|
|
if ((he = *hep) != NULL) {
|
|
|
|
res = he->value;
|
|
|
|
he->value = aData;
|
|
|
|
} else {
|
|
|
|
PL_HashTableRawAdd(hashtable, hep, hash,
|
|
|
|
(void *) aKey->Clone(), aData);
|
|
|
|
}
|
|
|
|
|
1999-03-30 12:15:39 +04:00
|
|
|
if (mLock) PR_Unlock(mLock);
|
|
|
|
|
1998-03-28 05:44:41 +03:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *nsHashtable::Get(nsHashKey *aKey) {
|
1999-03-30 12:15:39 +04:00
|
|
|
|
|
|
|
if (mLock) PR_Lock(mLock);
|
|
|
|
|
|
|
|
void *ret = PL_HashTableLookup(hashtable, (void *) aKey);
|
|
|
|
|
|
|
|
if (mLock) PR_Unlock(mLock);
|
|
|
|
|
|
|
|
return ret;
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void *nsHashtable::Remove(nsHashKey *aKey) {
|
|
|
|
PLHashNumber hash = aKey->HashValue();
|
|
|
|
PLHashEntry *he;
|
1999-03-30 12:15:39 +04:00
|
|
|
|
|
|
|
if (mLock) PR_Lock(mLock);
|
|
|
|
|
1998-03-28 05:44:41 +03:00
|
|
|
PLHashEntry **hep = PL_HashTableRawLookup(hashtable, hash, (void *) aKey);
|
|
|
|
void *res = NULL;
|
|
|
|
|
|
|
|
if ((he = *hep) != NULL) {
|
|
|
|
res = he->value;
|
|
|
|
PL_HashTableRawRemove(hashtable, hep, he);
|
|
|
|
}
|
|
|
|
|
1999-03-30 12:15:39 +04:00
|
|
|
if (mLock) PR_Unlock(mLock);
|
|
|
|
|
1998-03-28 05:44:41 +03:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
1998-06-02 02:11:06 +04:00
|
|
|
static PR_CALLBACK PRIntn _hashEnumerateCopy(PLHashEntry *he, PRIntn i, void *arg)
|
|
|
|
{
|
|
|
|
nsHashtable *newHashtable = (nsHashtable *)arg;
|
|
|
|
newHashtable->Put((nsHashKey *) he->key, he->value);
|
|
|
|
return HT_ENUMERATE_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsHashtable * nsHashtable::Clone() {
|
1999-03-30 12:15:39 +04:00
|
|
|
PRBool threadSafe = PR_FALSE;
|
|
|
|
if (mLock)
|
|
|
|
threadSafe = PR_TRUE;
|
|
|
|
nsHashtable *newHashTable = new nsHashtable(hashtable->nentries, threadSafe);
|
|
|
|
|
1998-06-02 02:11:06 +04:00
|
|
|
PL_HashTableEnumerateEntries(hashtable, _hashEnumerateCopy, newHashTable);
|
|
|
|
return newHashTable;
|
|
|
|
}
|
|
|
|
|
1998-09-01 04:16:47 +04:00
|
|
|
void nsHashtable::Enumerate(nsHashtableEnumFunc aEnumFunc, void* closure) {
|
|
|
|
_HashEnumerateArgs thunk;
|
|
|
|
thunk.fn = aEnumFunc;
|
|
|
|
thunk.arg = closure;
|
|
|
|
PL_HashTableEnumerateEntries(hashtable, _hashEnumerate, &thunk);
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
1999-02-03 22:35:11 +03:00
|
|
|
|
|
|
|
static PR_CALLBACK PRIntn _hashEnumerateRemove(PLHashEntry *he, PRIntn i, void *arg)
|
|
|
|
{
|
|
|
|
return HT_ENUMERATE_REMOVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsHashtable::Reset() {
|
|
|
|
PL_HashTableEnumerateEntries(hashtable, _hashEnumerateRemove, NULL);
|
|
|
|
}
|
|
|
|
|
1999-03-09 17:09:11 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
1999-03-19 09:18:08 +03:00
|
|
|
|
|
|
|
nsCStringKey::nsCStringKey(const char* str)
|
|
|
|
: mStr(mBuf)
|
|
|
|
{
|
1999-03-21 22:26:32 +03:00
|
|
|
PRUint32 len = PL_strlen(str);
|
1999-03-19 09:18:08 +03:00
|
|
|
if (len >= sizeof(mBuf)) {
|
|
|
|
mStr = PL_strdup(str);
|
|
|
|
NS_ASSERTION(mStr, "out of memory");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PL_strcpy(mStr, str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCStringKey::~nsCStringKey(void)
|
|
|
|
{
|
|
|
|
if (mStr != mBuf)
|
|
|
|
PL_strfree(mStr);
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUint32 nsCStringKey::HashValue(void) const
|
|
|
|
{
|
|
|
|
return (PRUint32) PL_HashString((const void*) mStr);
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool nsCStringKey::Equals(const nsHashKey* aKey) const
|
|
|
|
{
|
|
|
|
return PL_strcmp( ((nsCStringKey*)aKey)->mStr, mStr ) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsHashKey* nsCStringKey::Clone() const
|
|
|
|
{
|
|
|
|
return new nsCStringKey(mStr);
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|