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