Back to index

lightning-sunbird  0.9+nobinonly
plhash.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #ifndef plhash_h___
00039 #define plhash_h___
00040 /*
00041  * API to portable hash table code.
00042  */
00043 #include <stdio.h>
00044 #include "prtypes.h"
00045 
00046 PR_BEGIN_EXTERN_C
00047 
00048 typedef struct PLHashEntry  PLHashEntry;
00049 typedef struct PLHashTable  PLHashTable;
00050 typedef PRUint32 PLHashNumber;
00051 #define PL_HASH_BITS 32  /* Number of bits in PLHashNumber */
00052 typedef PLHashNumber (PR_CALLBACK *PLHashFunction)(const void *key);
00053 typedef PRIntn (PR_CALLBACK *PLHashComparator)(const void *v1, const void *v2);
00054 
00055 #if defined(XP_OS2_VACPP) && defined(VACPP_FLIP) /* for nsSpaceManager.cpp */
00056 PR_END_EXTERN_C                                  /* and nsHTMLDocument.cpp */
00057 #endif
00058 typedef PRIntn (PR_CALLBACK *PLHashEnumerator)(PLHashEntry *he, PRIntn i, void *arg);
00059 
00060 #if defined(XP_OS2_VACPP) && defined(VACPP_FLIP)
00061 PR_BEGIN_EXTERN_C
00062 #endif
00063 
00064 /* Flag bits in PLHashEnumerator's return value */
00065 #define HT_ENUMERATE_NEXT       0       /* continue enumerating entries */
00066 #define HT_ENUMERATE_STOP       1       /* stop enumerating entries */
00067 #define HT_ENUMERATE_REMOVE     2       /* remove and free the current entry */
00068 #define HT_ENUMERATE_UNHASH     4       /* just unhash the current entry */
00069 
00070 typedef struct PLHashAllocOps {
00071     void *              (PR_CALLBACK *allocTable)(void *pool, PRSize size);
00072     void                (PR_CALLBACK *freeTable)(void *pool, void *item);
00073     PLHashEntry *       (PR_CALLBACK *allocEntry)(void *pool, const void *key);
00074     void                (PR_CALLBACK *freeEntry)(void *pool, PLHashEntry *he, PRUintn flag);
00075 } PLHashAllocOps;
00076 
00077 #define HT_FREE_VALUE   0               /* just free the entry's value */
00078 #define HT_FREE_ENTRY   1               /* free value and entire entry */
00079 
00080 struct PLHashEntry {
00081     PLHashEntry         *next;          /* hash chain linkage */
00082     PLHashNumber        keyHash;        /* key hash function result */
00083     const void          *key;           /* ptr to opaque key */
00084     void                *value;         /* ptr to opaque value */
00085 };
00086 
00087 struct PLHashTable {
00088     PLHashEntry         **buckets;      /* vector of hash buckets */
00089     PRUint32              nentries;       /* number of entries in table */
00090     PRUint32              shift;          /* multiplicative hash shift */
00091     PLHashFunction      keyHash;        /* key hash function */
00092     PLHashComparator    keyCompare;     /* key comparison function */
00093     PLHashComparator    valueCompare;   /* value comparison function */
00094     const PLHashAllocOps *allocOps;     /* allocation operations */
00095     void                *allocPriv;     /* allocation private data */
00096 #ifdef HASHMETER
00097     PRUint32              nlookups;       /* total number of lookups */
00098     PRUint32              nsteps;         /* number of hash chains traversed */
00099     PRUint32              ngrows;         /* number of table expansions */
00100     PRUint32              nshrinks;       /* number of table contractions */
00101 #endif
00102 };
00103 
00104 /*
00105  * Create a new hash table.
00106  * If allocOps is null, use default allocator ops built on top of malloc().
00107  */
00108 PR_EXTERN(PLHashTable *)
00109 PL_NewHashTable(PRUint32 numBuckets, PLHashFunction keyHash,
00110                 PLHashComparator keyCompare, PLHashComparator valueCompare,
00111                 const PLHashAllocOps *allocOps, void *allocPriv);
00112 
00113 PR_EXTERN(void)
00114 PL_HashTableDestroy(PLHashTable *ht);
00115 
00116 /* Higher level access methods */
00117 PR_EXTERN(PLHashEntry *)
00118 PL_HashTableAdd(PLHashTable *ht, const void *key, void *value);
00119 
00120 PR_EXTERN(PRBool)
00121 PL_HashTableRemove(PLHashTable *ht, const void *key);
00122 
00123 PR_EXTERN(void *)
00124 PL_HashTableLookup(PLHashTable *ht, const void *key);
00125 
00126 PR_EXTERN(void *)
00127 PL_HashTableLookupConst(PLHashTable *ht, const void *key);
00128 
00129 PR_EXTERN(PRIntn)
00130 PL_HashTableEnumerateEntries(PLHashTable *ht, PLHashEnumerator f, void *arg);
00131 
00132 /* General-purpose C string hash function. */
00133 PR_EXTERN(PLHashNumber)
00134 PL_HashString(const void *key);
00135 
00136 /* Compare strings using strcmp(), return true if equal. */
00137 PR_EXTERN(PRIntn)
00138 PL_CompareStrings(const void *v1, const void *v2);
00139 
00140 /* Stub function just returns v1 == v2 */
00141 PR_EXTERN(PRIntn)
00142 PL_CompareValues(const void *v1, const void *v2);
00143 
00144 /* Low level access methods */
00145 PR_EXTERN(PLHashEntry **)
00146 PL_HashTableRawLookup(PLHashTable *ht, PLHashNumber keyHash, const void *key);
00147 
00148 PR_EXTERN(PLHashEntry **)
00149 PL_HashTableRawLookupConst(PLHashTable *ht, PLHashNumber keyHash,
00150                            const void *key);
00151 
00152 PR_EXTERN(PLHashEntry *)
00153 PL_HashTableRawAdd(PLHashTable *ht, PLHashEntry **hep, PLHashNumber keyHash,
00154                    const void *key, void *value);
00155 
00156 PR_EXTERN(void)
00157 PL_HashTableRawRemove(PLHashTable *ht, PLHashEntry **hep, PLHashEntry *he);
00158 
00159 /* This can be trivially implemented using PL_HashTableEnumerateEntries. */
00160 PR_EXTERN(PRIntn)
00161 PL_HashTableDump(PLHashTable *ht, PLHashEnumerator dump, FILE *fp);
00162 
00163 PR_END_EXTERN_C
00164 
00165 #endif /* plhash_h___ */