Back to index

lightning-sunbird  0.9+nobinonly
jshash.h
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
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 #ifndef jshash_h___
00041 #define jshash_h___
00042 /*
00043  * API to portable hash table code.
00044  */
00045 #include <stddef.h>
00046 #include <stdio.h>
00047 #include "jstypes.h"
00048 #include "jscompat.h"
00049 
00050 JS_BEGIN_EXTERN_C
00051 
00052 typedef uint32 JSHashNumber;
00053 typedef struct JSHashEntry JSHashEntry;
00054 typedef struct JSHashTable JSHashTable;
00055 
00056 #define JS_HASH_BITS 32
00057 #define JS_GOLDEN_RATIO 0x9E3779B9U
00058 
00059 typedef JSHashNumber (* JS_DLL_CALLBACK JSHashFunction)(const void *key);
00060 typedef intN (* JS_DLL_CALLBACK JSHashComparator)(const void *v1, const void *v2);
00061 typedef intN (* JS_DLL_CALLBACK JSHashEnumerator)(JSHashEntry *he, intN i, void *arg);
00062 
00063 /* Flag bits in JSHashEnumerator's return value */
00064 #define HT_ENUMERATE_NEXT       0       /* continue enumerating entries */
00065 #define HT_ENUMERATE_STOP       1       /* stop enumerating entries */
00066 #define HT_ENUMERATE_REMOVE     2       /* remove and free the current entry */
00067 
00068 typedef struct JSHashAllocOps {
00069     void *              (*allocTable)(void *pool, size_t size);
00070     void                (*freeTable)(void *pool, void *item);
00071     JSHashEntry *       (*allocEntry)(void *pool, const void *key);
00072     void                (*freeEntry)(void *pool, JSHashEntry *he, uintN flag);
00073 } JSHashAllocOps;
00074 
00075 #define HT_FREE_VALUE   0               /* just free the entry's value */
00076 #define HT_FREE_ENTRY   1               /* free value and entire entry */
00077 
00078 struct JSHashEntry {
00079     JSHashEntry         *next;          /* hash chain linkage */
00080     JSHashNumber        keyHash;        /* key hash function result */
00081     const void          *key;           /* ptr to opaque key */
00082     void                *value;         /* ptr to opaque value */
00083 };
00084 
00085 struct JSHashTable {
00086     JSHashEntry         **buckets;      /* vector of hash buckets */
00087     uint32              nentries;       /* number of entries in table */
00088     uint32              shift;          /* multiplicative hash shift */
00089     JSHashFunction      keyHash;        /* key hash function */
00090     JSHashComparator    keyCompare;     /* key comparison function */
00091     JSHashComparator    valueCompare;   /* value comparison function */
00092     JSHashAllocOps      *allocOps;      /* allocation operations */
00093     void                *allocPriv;     /* allocation private data */
00094 #ifdef HASHMETER
00095     uint32              nlookups;       /* total number of lookups */
00096     uint32              nsteps;         /* number of hash chains traversed */
00097     uint32              ngrows;         /* number of table expansions */
00098     uint32              nshrinks;       /* number of table contractions */
00099 #endif
00100 };
00101 
00102 /*
00103  * Create a new hash table.
00104  * If allocOps is null, use default allocator ops built on top of malloc().
00105  */
00106 extern JS_PUBLIC_API(JSHashTable *)
00107 JS_NewHashTable(uint32 n, JSHashFunction keyHash,
00108                 JSHashComparator keyCompare, JSHashComparator valueCompare,
00109                 JSHashAllocOps *allocOps, void *allocPriv);
00110 
00111 extern JS_PUBLIC_API(void)
00112 JS_HashTableDestroy(JSHashTable *ht);
00113 
00114 /* Low level access methods */
00115 extern JS_PUBLIC_API(JSHashEntry **)
00116 JS_HashTableRawLookup(JSHashTable *ht, JSHashNumber keyHash, const void *key);
00117 
00118 extern JS_PUBLIC_API(JSHashEntry *)
00119 JS_HashTableRawAdd(JSHashTable *ht, JSHashEntry **hep, JSHashNumber keyHash,
00120                    const void *key, void *value);
00121 
00122 extern JS_PUBLIC_API(void)
00123 JS_HashTableRawRemove(JSHashTable *ht, JSHashEntry **hep, JSHashEntry *he);
00124 
00125 /* Higher level access methods */
00126 extern JS_PUBLIC_API(JSHashEntry *)
00127 JS_HashTableAdd(JSHashTable *ht, const void *key, void *value);
00128 
00129 extern JS_PUBLIC_API(JSBool)
00130 JS_HashTableRemove(JSHashTable *ht, const void *key);
00131 
00132 extern JS_PUBLIC_API(intN)
00133 JS_HashTableEnumerateEntries(JSHashTable *ht, JSHashEnumerator f, void *arg);
00134 
00135 extern JS_PUBLIC_API(void *)
00136 JS_HashTableLookup(JSHashTable *ht, const void *key);
00137 
00138 extern JS_PUBLIC_API(intN)
00139 JS_HashTableDump(JSHashTable *ht, JSHashEnumerator dump, FILE *fp);
00140 
00141 /* General-purpose C string hash function. */
00142 extern JS_PUBLIC_API(JSHashNumber)
00143 JS_HashString(const void *key);
00144 
00145 /* Stub function just returns v1 == v2 */
00146 extern JS_PUBLIC_API(intN)
00147 JS_CompareValues(const void *v1, const void *v2);
00148 
00149 JS_END_EXTERN_C
00150 
00151 #endif /* jshash_h___ */