]> git.saurik.com Git - apple/objc4.git/blob - runtime/hashtable2.h
objc4-706.tar.gz
[apple/objc4.git] / runtime / hashtable2.h
1 /*
2 * Copyright (c) 1999-2006 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /*
24 hashtable2.h
25 Scalable hash table.
26 Copyright 1989-1996 NeXT Software, Inc.
27 */
28
29 #ifndef _OBJC_LITTLE_HASHTABLE_H_
30 #define _OBJC_LITTLE_HASHTABLE_H_
31
32 #ifndef _OBJC_PRIVATE_H_
33 # define OBJC_HASH_AVAILABILITY \
34 __OSX_DEPRECATED(10.0, 10.1, "NXHashTable is deprecated") \
35 __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE
36 #else
37 # define OBJC_HASH_AVAILABILITY
38 #endif
39
40 #include <objc/objc.h>
41 #include <stdint.h>
42 #include <TargetConditionals.h>
43
44 __BEGIN_DECLS
45
46 /*************************************************************************
47 * Hash tables of arbitrary data
48 *************************************************************************/
49
50 /* 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.
51 The objective C class HashTable is preferred when dealing with (key, values) associations because it is easier to use in that situation.
52 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. */
53
54 typedef struct {
55 uintptr_t (*hash)(const void *info, const void *data);
56 int (*isEqual)(const void *info, const void *data1, const void *data2);
57 void (*free)(const void *info, void *data);
58 int style; /* reserved for future expansion; currently 0 */
59 } NXHashTablePrototype;
60
61 /* the info argument allows a certain generality, such as freeing according to some owner information */
62 /* invariants assumed by the implementation:
63 1 - data1 = data2 => hash(data1) = hash(data2)
64 when data varies over time, hash(data) must remain invariant
65 e.g. if data hashes over a string key, the string must not be changed
66 2- isEqual (data1, data2) => data1= data2
67 */
68
69 typedef struct {
70 const NXHashTablePrototype *prototype OBJC_HASH_AVAILABILITY;
71 unsigned count OBJC_HASH_AVAILABILITY;
72 unsigned nbBuckets OBJC_HASH_AVAILABILITY;
73 void *buckets OBJC_HASH_AVAILABILITY;
74 const void *info OBJC_HASH_AVAILABILITY;
75 } NXHashTable OBJC_HASH_AVAILABILITY;
76 /* private data structure; may change */
77
78 OBJC_EXPORT NXHashTable *NXCreateHashTableFromZone (NXHashTablePrototype prototype, unsigned capacity, const void *info, void *z) OBJC_HASH_AVAILABILITY;
79 OBJC_EXPORT NXHashTable *NXCreateHashTable (NXHashTablePrototype prototype, unsigned capacity, const void *info) OBJC_HASH_AVAILABILITY;
80 /* if hash is 0, pointer hash is assumed */
81 /* if isEqual is 0, pointer equality is assumed */
82 /* if free is 0, elements are not freed */
83 /* capacity is only a hint; 0 creates a small table */
84 /* info allows call backs to be very general */
85
86 OBJC_EXPORT void NXFreeHashTable (NXHashTable *table) OBJC_HASH_AVAILABILITY;
87 /* calls free for each data, and recovers table */
88
89 OBJC_EXPORT void NXEmptyHashTable (NXHashTable *table) OBJC_HASH_AVAILABILITY;
90 /* does not deallocate table nor data; keeps current capacity */
91
92 OBJC_EXPORT void NXResetHashTable (NXHashTable *table) OBJC_HASH_AVAILABILITY;
93 /* frees each entry; keeps current capacity */
94
95 OBJC_EXPORT BOOL NXCompareHashTables (NXHashTable *table1, NXHashTable *table2) OBJC_HASH_AVAILABILITY;
96 /* Returns YES if the two sets are equal (each member of table1 in table2, and table have same size) */
97
98 OBJC_EXPORT NXHashTable *NXCopyHashTable (NXHashTable *table) OBJC_HASH_AVAILABILITY;
99 /* makes a fresh table, copying data pointers, not data itself. */
100
101 OBJC_EXPORT unsigned NXCountHashTable (NXHashTable *table) OBJC_HASH_AVAILABILITY;
102 /* current number of data in table */
103
104 OBJC_EXPORT int NXHashMember (NXHashTable *table, const void *data) OBJC_HASH_AVAILABILITY;
105 /* returns non-0 iff data is present in table.
106 Example of use when the hashed data is a struct containing the key,
107 and when the callee only has a key:
108 MyStruct pseudo;
109 pseudo.key = myKey;
110 return NXHashMember (myTable, &pseudo)
111 */
112
113 OBJC_EXPORT void *NXHashGet (NXHashTable *table, const void *data) OBJC_HASH_AVAILABILITY;
114 /* return original table data or NULL.
115 Example of use when the hashed data is a struct containing the key,
116 and when the callee only has a key:
117 MyStruct pseudo;
118 MyStruct *original;
119 pseudo.key = myKey;
120 original = NXHashGet (myTable, &pseudo)
121 */
122
123 OBJC_EXPORT void *NXHashInsert (NXHashTable *table, const void *data) OBJC_HASH_AVAILABILITY;
124 /* previous data or NULL is returned. */
125
126 OBJC_EXPORT void *NXHashInsertIfAbsent (NXHashTable *table, const void *data) OBJC_HASH_AVAILABILITY;
127 /* If data already in table, returns the one in table
128 else adds argument to table and returns argument. */
129
130 OBJC_EXPORT void *NXHashRemove (NXHashTable *table, const void *data) OBJC_HASH_AVAILABILITY;
131 /* previous data or NULL is returned */
132
133 /* 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:
134 unsigned count = 0;
135 MyData *data;
136 NXHashState state = NXInitHashState(table);
137 while (NXNextHashState(table, &state, &data)) {
138 count++;
139 }
140 */
141
142 typedef struct {int i; int j;} NXHashState OBJC_HASH_AVAILABILITY;
143 /* callers should not rely on actual contents of the struct */
144
145 OBJC_EXPORT NXHashState NXInitHashState(NXHashTable *table) OBJC_HASH_AVAILABILITY;
146
147 OBJC_EXPORT int NXNextHashState(NXHashTable *table, NXHashState *state, void **data) OBJC_HASH_AVAILABILITY;
148 /* returns 0 when all elements have been visited */
149
150 /*************************************************************************
151 * Conveniences for writing hash, isEqual and free functions
152 * and common prototypes
153 *************************************************************************/
154
155 OBJC_EXPORT uintptr_t NXPtrHash(const void *info, const void *data) OBJC_HASH_AVAILABILITY;
156 /* scrambles the address bits; info unused */
157 OBJC_EXPORT uintptr_t NXStrHash(const void *info, const void *data) OBJC_HASH_AVAILABILITY;
158 /* string hashing; info unused */
159 OBJC_EXPORT int NXPtrIsEqual(const void *info, const void *data1, const void *data2) OBJC_HASH_AVAILABILITY;
160 /* pointer comparison; info unused */
161 OBJC_EXPORT int NXStrIsEqual(const void *info, const void *data1, const void *data2) OBJC_HASH_AVAILABILITY;
162 /* string comparison; NULL ok; info unused */
163 OBJC_EXPORT void NXNoEffectFree(const void *info, void *data) OBJC_HASH_AVAILABILITY;
164 /* no effect; info unused */
165 OBJC_EXPORT void NXReallyFree(const void *info, void *data) OBJC_HASH_AVAILABILITY;
166 /* frees it; info unused */
167
168 /* The two following prototypes are useful for manipulating set of pointers or set of strings; For them free is defined as NXNoEffectFree */
169 OBJC_EXPORT const NXHashTablePrototype NXPtrPrototype OBJC_HASH_AVAILABILITY;
170 /* prototype when data is a pointer (void *) */
171 OBJC_EXPORT const NXHashTablePrototype NXStrPrototype OBJC_HASH_AVAILABILITY;
172 /* prototype when data is a string (char *) */
173
174 /* following prototypes help describe mappings where the key is the first element of a struct and is either a pointer or a string.
175 For example NXStrStructKeyPrototype can be used to hash pointers to Example, where Example is:
176 typedef struct {
177 char *key;
178 int data1;
179 ...
180 } Example
181
182 For the following prototypes, free is defined as NXReallyFree.
183 */
184 OBJC_EXPORT const NXHashTablePrototype NXPtrStructKeyPrototype OBJC_HASH_AVAILABILITY;
185 OBJC_EXPORT const NXHashTablePrototype NXStrStructKeyPrototype OBJC_HASH_AVAILABILITY;
186
187
188 #if !__OBJC2__ && !TARGET_OS_WIN32
189
190 /*************************************************************************
191 * Unique strings and buffers
192 *************************************************************************/
193
194 /* Unique strings allows C users to enjoy the benefits of Lisp's atoms:
195 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. */
196
197 typedef const char *NXAtom OBJC_HASH_AVAILABILITY;
198
199 OBJC_EXPORT NXAtom NXUniqueString(const char *buffer) OBJC_HASH_AVAILABILITY;
200 /* assumes that buffer is \0 terminated, and returns
201 a previously created string or a new string that is a copy of buffer.
202 If NULL is passed returns NULL.
203 Returned string should never be modified. To ensure this invariant,
204 allocations are made in a special read only zone. */
205
206 OBJC_EXPORT NXAtom NXUniqueStringWithLength(const char *buffer, int length) OBJC_HASH_AVAILABILITY;
207 /* assumes that buffer is a non NULL buffer of at least
208 length characters. Returns a previously created string or
209 a new string that is a copy of buffer.
210 If buffer contains \0, string will be truncated.
211 As for NXUniqueString, returned string should never be modified. */
212
213 OBJC_EXPORT NXAtom NXUniqueStringNoCopy(const char *string) OBJC_HASH_AVAILABILITY;
214 /* If there is already a unique string equal to string, returns the original.
215 Otherwise, string is entered in the table, without making a copy. Argument should then never be modified. */
216
217 OBJC_EXPORT char *NXCopyStringBuffer(const char *buffer) OBJC_HASH_AVAILABILITY;
218 /* given a buffer, allocates a new string copy of buffer.
219 Buffer should be \0 terminated; returned string is \0 terminated. */
220
221 OBJC_EXPORT char *NXCopyStringBufferFromZone(const char *buffer, void *z) OBJC_HASH_AVAILABILITY;
222 /* given a buffer, allocates a new string copy of buffer.
223 Buffer should be \0 terminated; returned string is \0 terminated. */
224
225 #endif
226
227 __END_DECLS
228
229 #endif /* _OBJC_LITTLE_HASHTABLE_H_ */