00001 /* 00002 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. 00003 * 00004 * @APPLE_LICENSE_HEADER_START@ 00005 * 00006 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights 00007 * Reserved. This file contains Original Code and/or Modifications of 00008 * Original Code as defined in and that are subject to the Apple Public 00009 * Source License Version 1.1 (the "License"). You may not use this file 00010 * except in compliance with the License. Please obtain a copy of the 00011 * License at http://www.apple.com/publicsource and read it before using 00012 * this file. 00013 * 00014 * The Original Code and all software distributed under the License are 00015 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER 00016 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 00017 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 00018 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the 00019 * License for the specific language governing rights and limitations 00020 * under the License. 00021 * 00022 * @APPLE_LICENSE_HEADER_END@ 00023 */ 00024 /* 00025 hashtable2.h 00026 Scalable hash table. 00027 Copyright 1989-1996 NeXT Software, Inc. 00028 */ 00029 00030 #warning The API in this header is obsoleted by NSHashtable.h 00031 00032 #ifndef _OBJC_LITTLE_HASHTABLE_H_ 00033 #define _OBJC_LITTLE_HASHTABLE_H_ 00034 00035 #import <objc/objc.h> 00036 00037 /************************************************************************* 00038 * Hash tables of arbitrary data 00039 *************************************************************************/ 00040 00041 /* This module allows hashing of arbitrary data. Such data must be pointers or integers, and client is responsible for allocating/deallocating this data. A deallocation call-back is provided. 00042 The objective C class HashTable is preferred when dealing with (key, values) associations because it is easier to use in that situation. 00043 As well-behaved scalable data structures, hash tables double in size when they start becoming full, thus guaranteeing both average constant time access and linear size. */ 00044 00045 typedef struct { 00046 uarith_t (*hash)(const void *info, const void *data); 00047 int (*isEqual)(const void *info, const void *data1, const void *data2); 00048 void (*free)(const void *info, void *data); 00049 int style; /* reserved for future expansion; currently 0 */ 00050 } NXHashTablePrototype; 00051 00052 /* the info argument allows a certain generality, such as freeing according to some owner information */ 00053 /* invariants assumed by the implementation: 00054 1 - data1 = data2 => hash(data1) = hash(data2) 00055 when data varies over time, hash(data) must remain invariant 00056 e.g. if data hashes over a string key, the string must not be changed 00057 2- isEqual (data1, data2) => data1= data2 00058 */ 00059 00060 typedef struct { 00061 const NXHashTablePrototype *prototype; 00062 unsigned count; 00063 unsigned nbBuckets; 00064 void *buckets; 00065 const void *info; 00066 } NXHashTable; 00067 /* private data structure; may change */ 00068 00069 OBJC_EXPORT NXHashTable *NXCreateHashTableFromZone (NXHashTablePrototype prototype, unsigned capacity, const void *info, void *z); 00070 OBJC_EXPORT NXHashTable *NXCreateHashTable (NXHashTablePrototype prototype, unsigned capacity, const void *info); 00071 /* if hash is 0, pointer hash is assumed */ 00072 /* if isEqual is 0, pointer equality is assumed */ 00073 /* if free is 0, elements are not freed */ 00074 /* capacity is only a hint; 0 creates a small table */ 00075 /* info allows call backs to be very general */ 00076 00077 OBJC_EXPORT void NXFreeHashTable (NXHashTable *table); 00078 /* calls free for each data, and recovers table */ 00079 00080 OBJC_EXPORT void NXEmptyHashTable (NXHashTable *table); 00081 /* does not deallocate table nor data; keeps current capacity */ 00082 00083 OBJC_EXPORT void NXResetHashTable (NXHashTable *table); 00084 /* frees each entry; keeps current capacity */ 00085 00086 OBJC_EXPORT BOOL NXCompareHashTables (NXHashTable *table1, NXHashTable *table2); 00087 /* Returns YES if the two sets are equal (each member of table1 in table2, and table have same size) */ 00088 00089 OBJC_EXPORT NXHashTable *NXCopyHashTable (NXHashTable *table); 00090 /* makes a fresh table, copying data pointers, not data itself. */ 00091 00092 OBJC_EXPORT unsigned NXCountHashTable (NXHashTable *table); 00093 /* current number of data in table */ 00094 00095 OBJC_EXPORT int NXHashMember (NXHashTable *table, const void *data); 00096 /* returns non-0 iff data is present in table. 00097 Example of use when the hashed data is a struct containing the key, 00098 and when the callee only has a key: 00099 MyStruct pseudo; 00100 pseudo.key = myKey; 00101 return NXHashMember (myTable, &pseudo) 00102 */ 00103 00104 OBJC_EXPORT void *NXHashGet (NXHashTable *table, const void *data); 00105 /* return original table data or NULL. 00106 Example of use when the hashed data is a struct containing the key, 00107 and when the callee only has a key: 00108 MyStruct pseudo; 00109 MyStruct *original; 00110 pseudo.key = myKey; 00111 original = NXHashGet (myTable, &pseudo) 00112 */ 00113 00114 OBJC_EXPORT void *NXHashInsert (NXHashTable *table, const void *data); 00115 /* previous data or NULL is returned. */ 00116 00117 OBJC_EXPORT void *NXHashInsertIfAbsent (NXHashTable *table, const void *data); 00118 /* If data already in table, returns the one in table 00119 else adds argument to table and returns argument. */ 00120 00121 OBJC_EXPORT void *NXHashRemove (NXHashTable *table, const void *data); 00122 /* previous data or NULL is returned */ 00123 00124 /* Iteration over all elements of a table consists in setting up an iteration state and then to progress until all entries have been visited. An example of use for counting elements in a table is: 00125 unsigned count = 0; 00126 MyData *data; 00127 NXHashState state = NXInitHashState(table); 00128 while (NXNextHashState(table, &state, &data)) { 00129 count++; 00130 } 00131 */ 00132 00133 typedef struct {int i; int j;} NXHashState; 00134 /* callers should not rely on actual contents of the struct */ 00135 00136 OBJC_EXPORT NXHashState NXInitHashState(NXHashTable *table); 00137 00138 OBJC_EXPORT int NXNextHashState(NXHashTable *table, NXHashState *state, void **data); 00139 /* returns 0 when all elements have been visited */ 00140 00141 /************************************************************************* 00142 * Conveniences for writing hash, isEqual and free functions 00143 * and common prototypes 00144 *************************************************************************/ 00145 00146 OBJC_EXPORT uarith_t NXPtrHash(const void *info, const void *data); 00147 /* scrambles the address bits; info unused */ 00148 OBJC_EXPORT uarith_t NXStrHash(const void *info, const void *data); 00149 /* string hashing; info unused */ 00150 OBJC_EXPORT int NXPtrIsEqual(const void *info, const void *data1, const void *data2); 00151 /* pointer comparison; info unused */ 00152 OBJC_EXPORT int NXStrIsEqual(const void *info, const void *data1, const void *data2); 00153 /* string comparison; NULL ok; info unused */ 00154 OBJC_EXPORT void NXNoEffectFree(const void *info, void *data); 00155 /* no effect; info unused */ 00156 OBJC_EXPORT void NXReallyFree(const void *info, void *data); 00157 /* frees it; info unused */ 00158 00159 /* The two following prototypes are useful for manipulating set of pointers or set of strings; For them free is defined as NXNoEffectFree */ 00160 OBJC_EXPORT const NXHashTablePrototype NXPtrPrototype; 00161 /* prototype when data is a pointer (void *) */ 00162 OBJC_EXPORT const NXHashTablePrototype NXStrPrototype; 00163 /* prototype when data is a string (char *) */ 00164 00165 /* following prototypes help describe mappings where the key is the first element of a struct and is either a pointer or a string. 00166 For example NXStrStructKeyPrototype can be used to hash pointers to Example, where Example is: 00167 typedef struct { 00168 char *key; 00169 int data1; 00170 ... 00171 } Example 00172 00173 For the following prototypes, free is defined as NXReallyFree. 00174 */ 00175 OBJC_EXPORT const NXHashTablePrototype NXPtrStructKeyPrototype; 00176 OBJC_EXPORT const NXHashTablePrototype NXStrStructKeyPrototype; 00177 00178 /************************************************************************* 00179 * Unique strings and buffers 00180 *************************************************************************/ 00181 00182 /* Unique strings allows C users to enjoy the benefits of Lisp's atoms: 00183 A unique string is a string that is allocated once for all (never de-allocated) and that has only one representant (thus allowing comparison with == instead of strcmp). A unique string should never be modified (and in fact some memory protection is done to ensure that). In order to more explicitly insist on the fact that the string has been uniqued, a synonym of (const char *) has been added, NXAtom. */ 00184 00185 typedef const char *NXAtom; 00186 00187 OBJC_EXPORT NXAtom NXUniqueString(const char *buffer); 00188 /* assumes that buffer is \0 terminated, and returns 00189 a previously created string or a new string that is a copy of buffer. 00190 If NULL is passed returns NULL. 00191 Returned string should never be modified. To ensure this invariant, 00192 allocations are made in a special read only zone. */ 00193 00194 OBJC_EXPORT NXAtom NXUniqueStringWithLength(const char *buffer, int length); 00195 /* assumes that buffer is a non NULL buffer of at least 00196 length characters. Returns a previously created string or 00197 a new string that is a copy of buffer. 00198 If buffer contains \0, string will be truncated. 00199 As for NXUniqueString, returned string should never be modified. */ 00200 00201 OBJC_EXPORT NXAtom NXUniqueStringNoCopy(const char *string); 00202 /* If there is already a unique string equal to string, returns the original. 00203 Otherwise, string is entered in the table, without making a copy. Argument should then never be modified. */ 00204 00205 OBJC_EXPORT char *NXCopyStringBuffer(const char *buffer); 00206 /* given a buffer, allocates a new string copy of buffer. 00207 Buffer should be \0 terminated; returned string is \0 terminated. */ 00208 00209 OBJC_EXPORT char *NXCopyStringBufferFromZone(const char *buffer, void *z); 00210 /* given a buffer, allocates a new string copy of buffer. 00211 Buffer should be \0 terminated; returned string is \0 terminated. */ 00212 00213 #endif /* _OBJC_LITTLE_HASHTABLE_H_ */