1999-07-28 07:56:28 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
1998-03-28 02:44:41 +00:00
|
|
|
/*
|
1999-11-06 03:43:54 +00:00
|
|
|
* The contents of this file are subject to the Netscape Public
|
|
|
|
* License Version 1.1 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.mozilla.org/NPL/
|
1998-03-28 02:44:41 +00:00
|
|
|
*
|
1999-11-06 03:43:54 +00:00
|
|
|
* Software distributed under the License is distributed on an "AS
|
|
|
|
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
|
|
|
* implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
1998-03-28 02:44:41 +00:00
|
|
|
*
|
1999-11-06 03:43:54 +00:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
1998-03-28 02:44:41 +00:00
|
|
|
* Communications Corporation. Portions created by Netscape are
|
1999-11-06 03:43:54 +00:00
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
1998-03-28 02:44:41 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "prmem.h"
|
1999-03-30 08:15:39 +00:00
|
|
|
#include "prlog.h"
|
1998-03-28 02:44:41 +00:00
|
|
|
#include "nsHashtable.h"
|
|
|
|
|
|
|
|
//
|
|
|
|
// Key operations
|
|
|
|
//
|
|
|
|
|
|
|
|
static PR_CALLBACK PLHashNumber _hashValue(const void *key)
|
|
|
|
{
|
1998-05-22 21:56:30 +00:00
|
|
|
return ((const nsHashKey *) key)->HashValue();
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static PR_CALLBACK PRIntn _hashKeyCompare(const void *key1, const void *key2) {
|
1998-05-22 21:56:30 +00:00
|
|
|
return ((const nsHashKey *) key1)->Equals((const nsHashKey *) key2);
|
1998-03-28 02:44:41 +00: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 00:16:47 +00:00
|
|
|
struct _HashEnumerateArgs {
|
|
|
|
nsHashtableEnumFunc fn;
|
|
|
|
void* arg;
|
|
|
|
};
|
|
|
|
|
1998-03-28 02:44:41 +00:00
|
|
|
static PR_CALLBACK PRIntn _hashEnumerate(PLHashEntry *he, PRIntn i, void *arg)
|
|
|
|
{
|
1998-09-01 00:16:47 +00:00
|
|
|
_HashEnumerateArgs* thunk = (_HashEnumerateArgs*)arg;
|
|
|
|
return thunk->fn((nsHashKey *) he->key, he->value, thunk->arg)
|
|
|
|
? HT_ENUMERATE_NEXT
|
|
|
|
: HT_ENUMERATE_STOP;
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
|
1998-05-22 21:56:30 +00:00
|
|
|
//
|
|
|
|
// HashKey
|
|
|
|
//
|
|
|
|
nsHashKey::nsHashKey(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsHashKey::~nsHashKey(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
1999-03-30 08:15:39 +00:00
|
|
|
nsHashtable::nsHashtable(PRUint32 aInitSize, PRBool threadSafe)
|
|
|
|
: mLock(NULL)
|
|
|
|
{
|
1998-03-28 02:44:41 +00:00
|
|
|
hashtable = PL_NewHashTable(aInitSize,
|
|
|
|
_hashValue,
|
|
|
|
_hashKeyCompare,
|
|
|
|
_hashValueCompare,
|
|
|
|
&_hashAllocOps,
|
|
|
|
NULL);
|
1999-03-30 08:15:39 +00: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 02:44:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsHashtable::~nsHashtable() {
|
|
|
|
PL_HashTableDestroy(hashtable);
|
1999-03-30 08:15:39 +00:00
|
|
|
if (mLock) PR_DestroyLock(mLock);
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
|
1999-03-19 05:51:49 +00:00
|
|
|
PRBool nsHashtable::Exists(nsHashKey *aKey)
|
|
|
|
{
|
|
|
|
PLHashNumber hash = aKey->HashValue();
|
1999-03-30 08:15:39 +00:00
|
|
|
|
|
|
|
if (mLock) PR_Lock(mLock);
|
|
|
|
|
1999-03-19 05:51:49 +00:00
|
|
|
PLHashEntry **hep = PL_HashTableRawLookup(hashtable, hash, (void *) aKey);
|
|
|
|
|
1999-03-30 08:15:39 +00:00
|
|
|
if (mLock) PR_Unlock(mLock);
|
|
|
|
|
1999-03-19 05:51:49 +00:00
|
|
|
return *hep != NULL;
|
|
|
|
}
|
|
|
|
|
1998-03-28 02:44:41 +00:00
|
|
|
void *nsHashtable::Put(nsHashKey *aKey, void *aData) {
|
|
|
|
void *res = NULL;
|
|
|
|
PLHashNumber hash = aKey->HashValue();
|
|
|
|
PLHashEntry *he;
|
1999-03-30 08:15:39 +00:00
|
|
|
|
|
|
|
if (mLock) PR_Lock(mLock);
|
|
|
|
|
1998-03-28 02:44:41 +00: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 08:15:39 +00:00
|
|
|
if (mLock) PR_Unlock(mLock);
|
|
|
|
|
1998-03-28 02:44:41 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *nsHashtable::Get(nsHashKey *aKey) {
|
1999-03-30 08:15:39 +00:00
|
|
|
|
|
|
|
if (mLock) PR_Lock(mLock);
|
|
|
|
|
|
|
|
void *ret = PL_HashTableLookup(hashtable, (void *) aKey);
|
|
|
|
|
|
|
|
if (mLock) PR_Unlock(mLock);
|
|
|
|
|
|
|
|
return ret;
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void *nsHashtable::Remove(nsHashKey *aKey) {
|
|
|
|
PLHashNumber hash = aKey->HashValue();
|
|
|
|
PLHashEntry *he;
|
1999-03-30 08:15:39 +00:00
|
|
|
|
|
|
|
if (mLock) PR_Lock(mLock);
|
|
|
|
|
1998-03-28 02:44:41 +00: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 08:15:39 +00:00
|
|
|
if (mLock) PR_Unlock(mLock);
|
|
|
|
|
1998-03-28 02:44:41 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
1999-07-28 07:56:28 +00:00
|
|
|
// XXX This method was called _hashEnumerateCopy, but it didn't copy the element!
|
|
|
|
// I don't know how this was supposed to work since the elements are neither copied
|
|
|
|
// nor refcounted.
|
|
|
|
static PR_CALLBACK PRIntn _hashEnumerateShare(PLHashEntry *he, PRIntn i, void *arg)
|
1998-06-01 22:11:06 +00:00
|
|
|
{
|
|
|
|
nsHashtable *newHashtable = (nsHashtable *)arg;
|
|
|
|
newHashtable->Put((nsHashKey *) he->key, he->value);
|
|
|
|
return HT_ENUMERATE_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsHashtable * nsHashtable::Clone() {
|
1999-03-30 08:15:39 +00:00
|
|
|
PRBool threadSafe = PR_FALSE;
|
|
|
|
if (mLock)
|
|
|
|
threadSafe = PR_TRUE;
|
|
|
|
nsHashtable *newHashTable = new nsHashtable(hashtable->nentries, threadSafe);
|
|
|
|
|
1999-07-28 07:56:28 +00:00
|
|
|
PL_HashTableEnumerateEntries(hashtable, _hashEnumerateShare, newHashTable);
|
1998-06-01 22:11:06 +00:00
|
|
|
return newHashTable;
|
|
|
|
}
|
|
|
|
|
1998-09-01 00:16:47 +00:00
|
|
|
void nsHashtable::Enumerate(nsHashtableEnumFunc aEnumFunc, void* closure) {
|
|
|
|
_HashEnumerateArgs thunk;
|
|
|
|
thunk.fn = aEnumFunc;
|
|
|
|
thunk.arg = closure;
|
|
|
|
PL_HashTableEnumerateEntries(hashtable, _hashEnumerate, &thunk);
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
1999-02-03 19:35:11 +00:00
|
|
|
|
|
|
|
static PR_CALLBACK PRIntn _hashEnumerateRemove(PLHashEntry *he, PRIntn i, void *arg)
|
|
|
|
{
|
1999-07-31 05:41:54 +00:00
|
|
|
_HashEnumerateArgs* thunk = (_HashEnumerateArgs*)arg;
|
|
|
|
if (thunk)
|
|
|
|
return thunk->fn((nsHashKey *) he->key, he->value, thunk->arg)
|
|
|
|
? HT_ENUMERATE_REMOVE
|
|
|
|
: HT_ENUMERATE_STOP;
|
|
|
|
else
|
|
|
|
return HT_ENUMERATE_REMOVE;
|
1999-02-03 19:35:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void nsHashtable::Reset() {
|
1999-07-31 05:41:54 +00:00
|
|
|
Reset(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsHashtable::Reset(nsHashtableEnumFunc destroyFunc, void* closure)
|
|
|
|
{
|
|
|
|
if (destroyFunc != NULL)
|
|
|
|
{
|
|
|
|
_HashEnumerateArgs thunk;
|
|
|
|
thunk.fn = destroyFunc;
|
|
|
|
thunk.arg = closure;
|
|
|
|
PL_HashTableEnumerateEntries(hashtable, _hashEnumerateRemove, &thunk);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
PL_HashTableEnumerateEntries(hashtable, _hashEnumerateRemove, NULL);
|
1999-02-03 19:35:11 +00:00
|
|
|
}
|
|
|
|
|
1999-03-09 14:09:11 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
1999-03-19 06:18:08 +00:00
|
|
|
|
1999-07-22 02:11:04 +00:00
|
|
|
nsStringKey::nsStringKey(const char* str)
|
1999-03-19 06:18:08 +00:00
|
|
|
{
|
1999-07-22 02:11:04 +00:00
|
|
|
mStr.Assign(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsStringKey::nsStringKey(const PRUnichar* str)
|
|
|
|
{
|
|
|
|
mStr.Assign(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsStringKey::nsStringKey(const nsStr& str)
|
|
|
|
{
|
|
|
|
mStr.Assign(str);
|
1999-03-19 06:18:08 +00:00
|
|
|
}
|
|
|
|
|
1999-07-22 02:11:04 +00:00
|
|
|
nsStringKey::~nsStringKey(void)
|
1999-03-19 06:18:08 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
1999-07-22 02:11:04 +00:00
|
|
|
PRUint32 nsStringKey::HashValue(void) const
|
1999-03-19 06:18:08 +00:00
|
|
|
{
|
1999-07-22 02:11:04 +00:00
|
|
|
if(mStr.IsUnicode())
|
|
|
|
{
|
|
|
|
PRUint32 h;
|
1999-07-23 05:19:33 +00:00
|
|
|
PRUint32 n;
|
|
|
|
PRUint32 m;
|
1999-07-22 02:11:04 +00:00
|
|
|
const PRUnichar* c;
|
|
|
|
|
|
|
|
h = 0;
|
1999-07-23 05:19:33 +00:00
|
|
|
n = mStr.Length();
|
|
|
|
c = mStr.GetUnicode();
|
|
|
|
if(n < 16)
|
|
|
|
{ /* Hash every char in a short string. */
|
|
|
|
for(; n; c++, n--)
|
|
|
|
h = (h >> 28) ^ (h << 4) ^ *c;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ /* Sample a la jave.lang.String.hash(). */
|
|
|
|
for(m = n / 8; n >= m; c += m, n -= m)
|
|
|
|
h = (h >> 28) ^ (h << 4) ^ *c;
|
|
|
|
}
|
1999-07-22 02:11:04 +00:00
|
|
|
return h;
|
|
|
|
}
|
1999-07-23 05:19:33 +00:00
|
|
|
return (PRUint32)PL_HashString((const void*) mStr.GetBuffer());
|
1999-03-19 06:18:08 +00:00
|
|
|
}
|
|
|
|
|
1999-07-22 02:11:04 +00:00
|
|
|
PRBool nsStringKey::Equals(const nsHashKey* aKey) const
|
1999-03-19 06:18:08 +00:00
|
|
|
{
|
1999-07-22 02:11:04 +00:00
|
|
|
return ((nsStringKey*)aKey)->mStr == mStr;
|
1999-03-19 06:18:08 +00:00
|
|
|
}
|
|
|
|
|
1999-07-22 02:11:04 +00:00
|
|
|
nsHashKey* nsStringKey::Clone() const
|
1999-03-19 06:18:08 +00:00
|
|
|
{
|
1999-07-23 05:19:33 +00:00
|
|
|
return new nsStringKey(mStr);
|
1999-03-19 06:18:08 +00:00
|
|
|
}
|
|
|
|
|
1999-08-09 05:02:25 +00:00
|
|
|
const nsString& nsStringKey::GetString() const
|
|
|
|
{
|
|
|
|
return mStr;
|
|
|
|
}
|
|
|
|
|
1999-03-19 06:18:08 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
1999-07-28 07:56:28 +00:00
|
|
|
// nsObjectHashtable: an nsHashtable where the elements are C++ objects to be
|
|
|
|
// deleted
|
|
|
|
|
|
|
|
nsObjectHashtable::nsObjectHashtable(nsHashtableCloneElementFunc cloneElementFun,
|
|
|
|
void* cloneElementClosure,
|
|
|
|
nsHashtableEnumFunc destroyElementFun,
|
|
|
|
void* destroyElementClosure,
|
|
|
|
PRUint32 aSize, PRBool threadSafe)
|
|
|
|
: nsHashtable(aSize, threadSafe),
|
|
|
|
mCloneElementFun(cloneElementFun),
|
|
|
|
mCloneElementClosure(cloneElementClosure),
|
|
|
|
mDestroyElementFun(destroyElementFun),
|
|
|
|
mDestroyElementClosure(destroyElementClosure)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsObjectHashtable::~nsObjectHashtable()
|
|
|
|
{
|
1999-07-31 05:50:13 +00:00
|
|
|
Reset();
|
1999-07-28 07:56:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PR_CALLBACK PRIntn
|
|
|
|
nsObjectHashtable::CopyElement(PLHashEntry *he, PRIntn i, void *arg)
|
|
|
|
{
|
|
|
|
nsObjectHashtable *newHashtable = (nsObjectHashtable *)arg;
|
|
|
|
void* newElement =
|
|
|
|
newHashtable->mCloneElementFun((nsHashKey*)he->key, he->value,
|
|
|
|
newHashtable->mCloneElementClosure);
|
|
|
|
if (newElement == nsnull)
|
|
|
|
return HT_ENUMERATE_STOP;
|
|
|
|
newHashtable->Put((nsHashKey*)he->key, newElement);
|
|
|
|
return HT_ENUMERATE_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsHashtable*
|
|
|
|
nsObjectHashtable::Clone()
|
|
|
|
{
|
|
|
|
PRBool threadSafe = PR_FALSE;
|
|
|
|
if (mLock)
|
|
|
|
threadSafe = PR_TRUE;
|
|
|
|
nsObjectHashtable* newHashTable =
|
|
|
|
new nsObjectHashtable(mCloneElementFun, mCloneElementClosure,
|
|
|
|
mDestroyElementFun, mDestroyElementClosure,
|
|
|
|
hashtable->nentries, threadSafe);
|
|
|
|
|
|
|
|
PL_HashTableEnumerateEntries(hashtable, CopyElement, newHashTable);
|
|
|
|
return newHashTable;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsObjectHashtable::Reset()
|
|
|
|
{
|
1999-07-31 05:50:13 +00:00
|
|
|
nsHashtable::Reset(mDestroyElementFun, mDestroyElementClosure);
|
1999-07-28 07:56:28 +00:00
|
|
|
}
|
|
|
|
|
1999-08-02 23:55:03 +00:00
|
|
|
PRBool
|
|
|
|
nsObjectHashtable::RemoveAndDelete(nsHashKey *aKey)
|
|
|
|
{
|
|
|
|
void *value = Remove(aKey);
|
|
|
|
if (value && mDestroyElementFun)
|
|
|
|
{
|
|
|
|
return (*mDestroyElementFun)(aKey, value, mDestroyElementClosure);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
1999-07-28 07:56:28 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsSupportsHashtable: an nsHashtable where the elements are nsISupports*
|
|
|
|
|
|
|
|
static PR_CALLBACK PRBool
|
|
|
|
_ReleaseElement(nsHashKey *aKey, void *aData, void* closure)
|
|
|
|
{
|
|
|
|
nsISupports* element = NS_STATIC_CAST(nsISupports*, aData);
|
1999-11-11 20:41:54 +00:00
|
|
|
NS_IF_RELEASE(element);
|
1999-07-28 07:56:28 +00:00
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsSupportsHashtable::~nsSupportsHashtable()
|
|
|
|
{
|
|
|
|
Enumerate(_ReleaseElement, nsnull);
|
|
|
|
}
|
|
|
|
|
|
|
|
void*
|
|
|
|
nsSupportsHashtable::Put(nsHashKey *aKey, void *aData)
|
|
|
|
{
|
|
|
|
nsISupports* element = NS_STATIC_CAST(nsISupports*, aData);
|
1999-11-11 20:41:54 +00:00
|
|
|
NS_IF_ADDREF(element);
|
1999-07-28 07:56:28 +00:00
|
|
|
return nsHashtable::Put(aKey, aData);
|
|
|
|
}
|
|
|
|
|
|
|
|
void*
|
|
|
|
nsSupportsHashtable::Get(nsHashKey *aKey)
|
|
|
|
{
|
|
|
|
void* data = nsHashtable::Get(aKey);
|
1999-09-02 07:01:53 +00:00
|
|
|
if (!data)
|
|
|
|
return nsnull;
|
1999-07-28 07:56:28 +00:00
|
|
|
nsISupports* element = NS_STATIC_CAST(nsISupports*, data);
|
1999-11-11 20:41:54 +00:00
|
|
|
NS_IF_ADDREF(element);
|
1999-07-28 07:56:28 +00:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void*
|
|
|
|
nsSupportsHashtable::Remove(nsHashKey *aKey)
|
|
|
|
{
|
|
|
|
void* data = nsHashtable::Remove(aKey);
|
1999-09-02 07:01:53 +00:00
|
|
|
if (!data)
|
|
|
|
return nsnull;
|
1999-07-28 07:56:28 +00:00
|
|
|
nsISupports* element = NS_STATIC_CAST(nsISupports*, data);
|
1999-11-11 20:41:54 +00:00
|
|
|
NS_IF_RELEASE(element);
|
1999-07-28 07:56:28 +00:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PR_CALLBACK PRIntn
|
|
|
|
_hashEnumerateCopy(PLHashEntry *he, PRIntn i, void *arg)
|
|
|
|
{
|
|
|
|
nsHashtable *newHashtable = (nsHashtable *)arg;
|
|
|
|
nsISupports* element = NS_STATIC_CAST(nsISupports*, he->value);
|
1999-11-11 20:41:54 +00:00
|
|
|
NS_IF_ADDREF(element);
|
1999-07-28 07:56:28 +00:00
|
|
|
newHashtable->Put((nsHashKey*)he->key, he->value);
|
|
|
|
return HT_ENUMERATE_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsHashtable*
|
|
|
|
nsSupportsHashtable::Clone()
|
|
|
|
{
|
|
|
|
PRBool threadSafe = PR_FALSE;
|
|
|
|
if (mLock)
|
|
|
|
threadSafe = PR_TRUE;
|
|
|
|
nsSupportsHashtable* newHashTable =
|
|
|
|
new nsSupportsHashtable(hashtable->nentries, threadSafe);
|
|
|
|
|
|
|
|
PL_HashTableEnumerateEntries(hashtable, _hashEnumerateCopy, newHashTable);
|
|
|
|
return newHashTable;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsSupportsHashtable::Reset()
|
|
|
|
{
|
|
|
|
Enumerate(_ReleaseElement, nsnull);
|
|
|
|
nsHashtable::Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
1999-11-16 03:10:54 +00:00
|
|
|
// nsOpaqueKey: Where keys are opaque byte array blobs
|
|
|
|
//
|
|
|
|
|
|
|
|
// Note opaque keys are not copied by this constructor. If you want a private
|
|
|
|
// copy in each hash key, you must create one and pass it in to this function.
|
|
|
|
nsOpaqueKey::nsOpaqueKey(const char *aOpaqueKey, PRUint32 aKeyLength)
|
|
|
|
{
|
|
|
|
mOpaqueKey = aOpaqueKey;
|
|
|
|
mKeyLength = aKeyLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsOpaqueKey::~nsOpaqueKey(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUint32
|
|
|
|
nsOpaqueKey::HashValue(void) const
|
|
|
|
{
|
|
|
|
PRUint32 h, i, k;
|
|
|
|
|
|
|
|
h = 0;
|
|
|
|
|
|
|
|
// Same hashing technique as for java.lang.String.hashCode()
|
|
|
|
if (mKeyLength <= 15) {
|
|
|
|
// A short key; Use a dense sampling to compute the hash code
|
|
|
|
for (i = 0; i < mKeyLength; i++)
|
|
|
|
h += 37 * mOpaqueKey[i];
|
|
|
|
} else {
|
|
|
|
// A long key; Use a sparse sampling to compute the hash code
|
|
|
|
k = mKeyLength >> 3;
|
|
|
|
for (i = 0; i < mKeyLength; i += k)
|
|
|
|
h += 39 * mOpaqueKey[i];
|
|
|
|
}
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsOpaqueKey::Equals(const nsHashKey* aKey) const
|
|
|
|
{
|
|
|
|
nsOpaqueKey *otherKey = (nsOpaqueKey*)aKey;
|
|
|
|
if (mKeyLength != otherKey->mKeyLength)
|
|
|
|
return PR_FALSE;
|
|
|
|
return !(PRBool)memcmp(otherKey->mOpaqueKey, mOpaqueKey, mKeyLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsHashKey*
|
|
|
|
nsOpaqueKey::Clone() const
|
|
|
|
{
|
|
|
|
return new nsOpaqueKey(mOpaqueKey, mKeyLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUint32
|
|
|
|
nsOpaqueKey::GetKeyLength() const
|
|
|
|
{
|
|
|
|
return mKeyLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char*
|
|
|
|
nsOpaqueKey::GetKey() const
|
|
|
|
{
|
|
|
|
return mOpaqueKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|