home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / xpcom / src / nsHashtable.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  3.6 KB  |  132 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /*
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #include "prmem.h"
  20. #include "nsHashtable.h"
  21.  
  22. //
  23. // Key operations
  24. //
  25.  
  26. static PR_CALLBACK PLHashNumber _hashValue(const void *key) 
  27. {
  28.   return ((nsHashKey *) key)->HashValue();
  29. }
  30.  
  31. static PR_CALLBACK PRIntn _hashKeyCompare(const void *key1, const void *key2) {
  32.   return ((nsHashKey *) key1)->Equals((nsHashKey *) key2);
  33. }
  34.  
  35. static PR_CALLBACK PRIntn _hashValueCompare(const void *value1,
  36.                                             const void *value2) {
  37.   // We're not going to make any assumptions about value equality
  38.   return 0;
  39. }
  40.  
  41. //
  42. // Memory callbacks
  43. //
  44.  
  45. static PR_CALLBACK void *_hashAllocTable(void *pool, PRSize size) {
  46.   return PR_MALLOC(size);
  47. }
  48.  
  49. static PR_CALLBACK void _hashFreeTable(void *pool, void *item) {
  50.   PR_DELETE(item);
  51. }
  52.  
  53. static PR_CALLBACK PLHashEntry *_hashAllocEntry(void *pool, const void *key) {
  54.   return PR_NEW(PLHashEntry);
  55. }
  56.  
  57. static PR_CALLBACK void _hashFreeEntry(void *pool, PLHashEntry *entry, 
  58.                                        PRUintn flag) {
  59.   if (flag == HT_FREE_ENTRY) {
  60.     delete (nsHashKey *) (entry->key);
  61.     PR_DELETE(entry);
  62.   }
  63. }
  64.  
  65. static PLHashAllocOps _hashAllocOps = {
  66.     _hashAllocTable, _hashFreeTable,
  67.     _hashAllocEntry, _hashFreeEntry
  68. };
  69.  
  70. //
  71. // Enumerator callback
  72. //
  73.  
  74. static PR_CALLBACK PRIntn _hashEnumerate(PLHashEntry *he, PRIntn i, void *arg)
  75. {
  76.   return ((nsHashtableEnumFunc) arg)((nsHashKey *) he->key, he->value) ? 
  77.     HT_ENUMERATE_NEXT : 
  78.     HT_ENUMERATE_STOP;
  79. }
  80.  
  81. nsHashtable::nsHashtable(PRUint32 aInitSize) {
  82.   hashtable = PL_NewHashTable(aInitSize,
  83.                               _hashValue,
  84.                               _hashKeyCompare,
  85.                               _hashValueCompare,
  86.                               &_hashAllocOps,
  87.                               NULL);
  88. }
  89.  
  90. nsHashtable::~nsHashtable() {
  91.   PL_HashTableDestroy(hashtable);
  92. }
  93.  
  94. void *nsHashtable::Put(nsHashKey *aKey, void *aData) {
  95.   void *res =  NULL;
  96.   PLHashNumber hash = aKey->HashValue();
  97.   PLHashEntry *he;
  98.   PLHashEntry **hep = PL_HashTableRawLookup(hashtable, hash, (void *) aKey);
  99.  
  100.   if ((he = *hep) != NULL) {
  101.     res = he->value;
  102.     he->value = aData;
  103.   } else {
  104.     PL_HashTableRawAdd(hashtable, hep, hash,
  105.                        (void *) aKey->Clone(), aData);
  106.   }
  107.  
  108.   return res;
  109. }
  110.  
  111. void *nsHashtable::Get(nsHashKey *aKey) {
  112.   return PL_HashTableLookup(hashtable, (void *) aKey);
  113. }
  114.  
  115. void *nsHashtable::Remove(nsHashKey *aKey) {
  116.   PLHashNumber hash = aKey->HashValue();
  117.   PLHashEntry *he;
  118.   PLHashEntry **hep = PL_HashTableRawLookup(hashtable, hash, (void *) aKey);
  119.   void *res = NULL;
  120.   
  121.   if ((he = *hep) != NULL) {
  122.     res = he->value;
  123.     PL_HashTableRawRemove(hashtable, hep, he);
  124.   }
  125.  
  126.   return res;
  127. }
  128.  
  129. void nsHashtable::Enumerate(nsHashtableEnumFunc aEnumFunc) {
  130.   PL_HashTableEnumerateEntries(hashtable, _hashEnumerate, aEnumFunc);
  131. }
  132.