Back to index

lightning-sunbird  0.9+nobinonly
jsj_hash.h
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is Mozilla Communicator client code, released
00017  * March 31, 1998.
00018  *
00019  * The Initial Developer of the Original Code is
00020  * Netscape Communications Corporation.
00021  * Portions created by the Initial Developer are Copyright (C) 1998
00022  * the Initial Developer. All Rights Reserved.
00023  *
00024  * Contributor(s):
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 /*
00041  * This is a copy of the NSPR hash-table library, but it has been slightly
00042  * modified to allow an additional argument to be passed into the hash
00043  * key-comparison function.  This is used to maintain thread-safety by
00044  * passing in a JNIEnv pointer to the key-comparison function rather
00045  * than storing it in a global.  All types,function names, etc. have
00046  * been renamed from their original NSPR names to protect the innocent.
00047  */
00048 
00049 #ifndef jsj_hash_h___
00050 #define jsj_hash_h___
00051 /*
00052  * API to portable hash table code.
00053  */
00054 #include <stddef.h>
00055 #include <stdio.h>
00056 #include "jstypes.h"
00057 
00058 JS_BEGIN_EXTERN_C
00059 
00060 typedef struct JSJHashEntry  JSJHashEntry;
00061 typedef struct JSJHashTable  JSJHashTable;
00062 typedef JSUint32 JSJHashNumber;
00063 #define JSJ_HASH_BITS 32
00064 typedef JSJHashNumber (* JS_DLL_CALLBACK JSJHashFunction)(const void *key, void *arg);
00065 typedef JSIntn (* JS_DLL_CALLBACK JSJHashComparator)(const void *v1, const void *v2, void *arg);
00066 typedef JSIntn (* JS_DLL_CALLBACK JSJHashEnumerator)(JSJHashEntry *he, JSIntn i, void *arg);
00067 
00068 /* Flag bits in JSJHashEnumerator's return value */
00069 #define HT_ENUMERATE_NEXT       0       /* continue enumerating entries */
00070 #define HT_ENUMERATE_STOP       1       /* stop enumerating entries */
00071 #define HT_ENUMERATE_REMOVE     2       /* remove and free the current entry */
00072 #define HT_ENUMERATE_UNHASH     4       /* just unhash the current entry */
00073 
00074 typedef struct JSJHashAllocOps {
00075     void *              (*allocTable)(void *pool, size_t size);
00076     void                (*freeTable)(void *pool, void *item);
00077     JSJHashEntry *      (*allocEntry)(void *pool, const void *key);
00078     void                (*freeEntry)(void *pool, JSJHashEntry *he, JSUintn flag);
00079 } JSJHashAllocOps;
00080 
00081 #define HT_FREE_VALUE   0               /* just free the entry's value */
00082 #define HT_FREE_ENTRY   1               /* free value and entire entry */
00083 
00084 struct JSJHashEntry {
00085     JSJHashEntry        *next;          /* hash chain linkage */
00086     JSJHashNumber       keyHash;        /* key hash function result */
00087     const void          *key;           /* ptr to opaque key */
00088     void                *value;         /* ptr to opaque value */
00089 };
00090 
00091 struct JSJHashTable {
00092     JSJHashEntry         **buckets;      /* vector of hash buckets */
00093     JSUint32              nentries;       /* number of entries in table */
00094     JSUint32              shift;          /* multiplicative hash shift */
00095     JSJHashFunction     keyHash;        /* key hash function */
00096     JSJHashComparator   keyCompare;     /* key comparison function */
00097     JSJHashComparator   valueCompare;   /* value comparison function */
00098     JSJHashAllocOps     *allocOps;      /* allocation operations */
00099     void                *allocPriv;     /* allocation private data */
00100 #ifdef HASHMETER
00101     JSUint32              nlookups;       /* total number of lookups */
00102     JSUint32              nsteps;         /* number of hash chains traversed */
00103     JSUint32              ngrows;         /* number of table expansions */
00104     JSUint32              nshrinks;       /* number of table contractions */
00105 #endif
00106 };
00107 
00108 /*
00109  * Create a new hash table.
00110  * If allocOps is null, use default allocator ops built on top of malloc().
00111  */
00112 JS_EXTERN_API(JSJHashTable *)
00113 JSJ_NewHashTable(JSUint32 n, JSJHashFunction keyHash,
00114                 JSJHashComparator keyCompare, JSJHashComparator valueCompare,
00115                 JSJHashAllocOps *allocOps, void *allocPriv);
00116 
00117 JS_EXTERN_API(void)
00118 JSJ_HashTableDestroy(JSJHashTable *ht);
00119 
00120 /* Low level access methods */
00121 JS_EXTERN_API(JSJHashEntry **)
00122 JSJ_HashTableRawLookup(JSJHashTable *ht, JSJHashNumber keyHash, const void *key, void *arg);
00123 
00124 JS_EXTERN_API(JSJHashEntry *)
00125 JSJ_HashTableRawAdd(JSJHashTable *ht, JSJHashEntry **hep, JSJHashNumber keyHash,
00126                    const void *key, void *value, void *arg);
00127 
00128 JS_EXTERN_API(void)
00129 JSJ_HashTableRawRemove(JSJHashTable *ht, JSJHashEntry **hep, JSJHashEntry *he, void *arg);
00130 
00131 /* Higher level access methods */
00132 JS_EXTERN_API(JSJHashEntry *)
00133 JSJ_HashTableAdd(JSJHashTable *ht, const void *key, void *value, void *arg);
00134 
00135 JS_EXTERN_API(JSBool)
00136 JSJ_HashTableRemove(JSJHashTable *ht, const void *key, void *arg);
00137 
00138 JS_EXTERN_API(JSIntn)
00139 JSJ_HashTableEnumerateEntries(JSJHashTable *ht, JSJHashEnumerator f, void *arg);
00140 
00141 JS_EXTERN_API(void *)
00142 JSJ_HashTableLookup(JSJHashTable *ht, const void *key, void *arg);
00143 
00144 JS_EXTERN_API(JSIntn)
00145 JSJ_HashTableDump(JSJHashTable *ht, JSJHashEnumerator dump, FILE *fp);
00146 
00147 /* General-purpose C string hash function. */
00148 JS_EXTERN_API(JSJHashNumber)
00149 JSJ_HashString(const void *key);
00150 
00151 /* Compare strings using strcmp(), return true if equal. */
00152 JS_EXTERN_API(int)
00153 JSJ_CompareStrings(const void *v1, const void *v2);
00154 
00155 /* Stub function just returns v1 == v2 */
00156 JS_EXTERN_API(JSIntn)
00157 JSJ_CompareValues(const void *v1, const void *v2);
00158 
00159 JS_END_EXTERN_C
00160 
00161 #endif /* jsj_hash_h___ */