2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
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
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.
23 * @APPLE_LICENSE_HEADER_END@
26 Copyright 1990-1996 NeXT Software, Inc.
27 Created by Bertrand Serlet, August 1990
31 #import "objc-private.h"
37 #import <objc/Object.h>
38 #import <objc/hashtable2.h>
41 /****** Macros and utilities ****************************/
49 typedef struct _MapPair {
54 static unsigned log2(unsigned x) { return (x<2) ? 0 : log2(x>>1)+1; };
56 static INLINE unsigned exp2m1(unsigned x) { return (1 << x) - 1; };
58 /* iff necessary this modulo can be optimized since the nbBuckets is of the form 2**n-1 */
59 static INLINE unsigned bucketOf(NXMapTable *table, const void *key) {
60 unsigned hash = (table->prototype->hash)(table, key);
61 unsigned xored = (hash & 0xffff) ^ (hash >> 16);
62 return ((xored * 65521) + hash) % table->nbBuckets;
65 static INLINE int isEqual(NXMapTable *table, const void *key1, const void *key2) {
66 return (key1 == key2) ? 1 : (table->prototype->isEqual)(table, key1, key2);
69 static INLINE unsigned nextIndex(NXMapTable *table, unsigned index) {
70 return (index+1 >= table->nbBuckets) ? 0 : index+1;
73 static INLINE void *allocBuckets(void *z, unsigned nb) {
74 MapPair *pairs = malloc_zone_malloc(z, (nb * sizeof(MapPair)));
75 MapPair *pair = pairs;
76 while (nb--) { pair->key = NX_MAPNOTAKEY; pair->value = NULL; pair++; }
80 /***** Global data and bootstrap **********************/
82 static int isEqualPrototype (const void *info, const void *data1, const void *data2) {
83 NXHashTablePrototype *proto1 = (NXHashTablePrototype *) data1;
84 NXHashTablePrototype *proto2 = (NXHashTablePrototype *) data2;
86 return (proto1->hash == proto2->hash) && (proto1->isEqual == proto2->isEqual) && (proto1->free == proto2->free) && (proto1->style == proto2->style);
89 static uarith_t hashPrototype (const void *info, const void *data) {
90 NXHashTablePrototype *proto = (NXHashTablePrototype *) data;
92 return NXPtrHash(info, proto->hash) ^ NXPtrHash(info, proto->isEqual) ^ NXPtrHash(info, proto->free) ^ (uarith_t) proto->style;
95 static NXHashTablePrototype protoPrototype = {
96 hashPrototype, isEqualPrototype, NXNoEffectFree, 0
99 static NXHashTable *prototypes = NULL;
100 /* table of all prototypes */
102 /**** Fundamentals Operations **************/
104 NXMapTable *NXCreateMapTableFromZone(NXMapTablePrototype prototype, unsigned capacity, void *z) {
105 NXMapTable *table = malloc_zone_malloc(z, sizeof(NXMapTable));
106 NXMapTablePrototype *proto;
107 if (! prototypes) prototypes = NXCreateHashTable(protoPrototype, 0, NULL);
108 if (! prototype.hash || ! prototype.isEqual || ! prototype.free || prototype.style) {
109 _objc_syslog("*** NXCreateMapTable: invalid creation parameters\n");
112 proto = NXHashGet(prototypes, &prototype);
114 proto = malloc(sizeof(NXMapTablePrototype));
116 (void)NXHashInsert(prototypes, proto);
118 table->prototype = proto; table->count = 0;
119 table->nbBuckets = exp2m1(log2(capacity)+1);
120 table->buckets = allocBuckets(z, table->nbBuckets);
124 NXMapTable *NXCreateMapTable(NXMapTablePrototype prototype, unsigned capacity) {
125 return NXCreateMapTableFromZone(prototype, capacity, malloc_default_zone());
128 void NXFreeMapTable(NXMapTable *table) {
129 NXResetMapTable(table);
130 free(table->buckets);
134 void NXResetMapTable(NXMapTable *table) {
135 MapPair *pairs = table->buckets;
136 void (*freeProc)(struct _NXMapTable *, void *, void *) = table->prototype->free;
137 unsigned index = table->nbBuckets;
139 if (pairs->key != NX_MAPNOTAKEY) {
140 freeProc(table, (void *)pairs->key, (void *)pairs->value);
141 pairs->key = NX_MAPNOTAKEY; pairs->value = NULL;
148 BOOL NXCompareMapTables(NXMapTable *table1, NXMapTable *table2) {
149 if (table1 == table2) return YES;
150 if (table1->count != table2->count) return NO;
154 NXMapState state = NXInitMapState(table1);
155 while (NXNextMapState(table1, &state, &key, &value)) {
156 if (NXMapMember(table2, key, (void**)&value) == NX_MAPNOTAKEY) return NO;
162 unsigned NXCountMapTable(NXMapTable *table) { return table->count; }
164 static int mapSearch = 0;
165 static int mapSearchHit = 0;
166 static int mapSearchLoop = 0;
168 static INLINE void *_NXMapMember(NXMapTable *table, const void *key, void **value) {
169 MapPair *pairs = table->buckets;
170 unsigned index = bucketOf(table, key);
171 MapPair *pair = pairs + index;
172 if (pair->key == NX_MAPNOTAKEY) return NX_MAPNOTAKEY;
174 if (isEqual(table, pair->key, key)) {
175 *value = (void *)pair->value;
177 return (void *)pair->key;
179 unsigned index2 = index;
180 while ((index2 = nextIndex(table, index2)) != index) {
182 pair = pairs + index2;
183 if (pair->key == NX_MAPNOTAKEY) return NX_MAPNOTAKEY;
184 if (isEqual(table, pair->key, key)) {
185 *value = (void *)pair->value;
186 return (void *)pair->key;
189 return NX_MAPNOTAKEY;
193 void *NXMapMember(NXMapTable *table, const void *key, void **value) {
194 return _NXMapMember(table, key, value);
197 void *NXMapGet(NXMapTable *table, const void *key) {
199 return (_NXMapMember(table, key, &value) != NX_MAPNOTAKEY) ? value : NULL;
202 static int mapRehash = 0;
203 static int mapRehashSum = 0;
205 static void _NXMapRehash(NXMapTable *table) {
206 MapPair *pairs = table->buckets;
207 MapPair *pair = pairs;
208 unsigned index = table->nbBuckets;
209 unsigned oldCount = table->count;
210 table->nbBuckets += table->nbBuckets + 1; /* 2 times + 1 */
212 table->buckets = allocBuckets(malloc_zone_from_ptr(table), table->nbBuckets);
214 mapRehashSum += table->count;
216 if (pair->key != NX_MAPNOTAKEY) {
217 (void)NXMapInsert(table, pair->key, pair->value);
221 if (oldCount != table->count)
222 _objc_syslog("*** maptable: count differs after rehashing; probably indicates a broken invariant: there are x and y such as isEqual(x, y) is TRUE but hash(x) != hash (y)\n");
226 static int mapInsert = 0;
227 static int mapInsertHit = 0;
228 static int mapInsertLoop = 0;
230 void *NXMapInsert(NXMapTable *table, const void *key, const void *value) {
231 MapPair *pairs = table->buckets;
232 unsigned index = bucketOf(table, key);
233 MapPair *pair = pairs + index;
234 if (key == NX_MAPNOTAKEY) {
235 _objc_syslog("*** NXMapInsert: invalid key: -1\n");
239 if (pair->key == NX_MAPNOTAKEY) {
241 pair->key = key; pair->value = value;
245 if (isEqual(table, pair->key, key)) {
246 const void *old = pair->value;
248 if (old != value) pair->value = value;/* avoid writing unless needed! */
250 } else if (table->count == table->nbBuckets) {
251 /* no room: rehash and retry */
253 return NXMapInsert(table, key, value);
255 unsigned index2 = index;
256 while ((index2 = nextIndex(table, index2)) != index) {
258 pair = pairs + index2;
259 if (pair->key == NX_MAPNOTAKEY) {
261 pair->key = key; pair->value = value;
263 MapPair current = {key, value};
265 while (current.key != NX_MAPNOTAKEY) {
267 pair = pairs + index2;
271 index2 = nextIndex(table, index2);
275 if (table->count * 4 > table->nbBuckets * 3) _NXMapRehash(table);
278 if (isEqual(table, pair->key, key)) {
279 const void *old = pair->value;
280 if (old != value) pair->value = value;/* avoid writing unless needed! */
284 /* no room: can't happen! */
285 _objc_syslog("**** NXMapInsert: bug\n");
290 static int mapRemove = 0;
292 void *NXMapRemove(NXMapTable *table, const void *key) {
293 MapPair *pairs = table->buckets;
294 unsigned index = bucketOf(table, key);
295 MapPair *pair = pairs + index;
296 unsigned chain = 1; /* number of non-nil pairs in a row */
298 const void *old = NULL;
299 if (pair->key == NX_MAPNOTAKEY) return NULL;
303 unsigned index2 = index;
304 if (isEqual(table, pair->key, key)) {found ++; old = pair->value; }
305 while ((index2 = nextIndex(table, index2)) != index) {
306 pair = pairs + index2;
307 if (pair->key == NX_MAPNOTAKEY) break;
308 if (isEqual(table, pair->key, key)) {found ++; old = pair->value; }
312 if (! found) return NULL;
313 if (found != 1) _objc_syslog("**** NXMapRemove: incorrect table\n");
314 /* remove then reinsert */
317 MapPair *aux = (chain > 16) ? malloc(sizeof(MapPair)*(chain-1)) : buffer;
320 unsigned index2 = index;
322 pair = pairs + index2;
323 if (! isEqual(table, pair->key, key)) aux[auxnb++] = *pair;
324 pair->key = NX_MAPNOTAKEY; pair->value = NULL;
325 index2 = nextIndex(table, index2);
327 table->count -= chain;
328 if (auxnb != chain-1) _objc_syslog("**** NXMapRemove: bug\n");
329 while (auxnb--) NXMapInsert(table, aux[auxnb].key, aux[auxnb].value);
330 if (chain > 16) free(aux);
335 NXMapState NXInitMapState(NXMapTable *table) {
337 state.index = table->nbBuckets;
341 int NXNextMapState(NXMapTable *table, NXMapState *state, const void **key, const void **value) {
342 MapPair *pairs = table->buckets;
343 while (state->index--) {
344 MapPair *pair = pairs + state->index;
345 if (pair->key != NX_MAPNOTAKEY) {
346 *key = pair->key; *value = pair->value;
353 /**** Conveniences *************************************/
355 static unsigned _mapPtrHash(NXMapTable *table, const void *key) {
356 return (((uarith_t) key) >> ARITH_SHIFT) ^ ((uarith_t) key);
359 static unsigned _mapStrHash(NXMapTable *table, const void *key) {
361 unsigned char *s = (unsigned char *)key;
362 /* unsigned to avoid a sign-extend */
363 /* unroll the loop */
365 if (*s == '\0') break;
367 if (*s == '\0') break;
369 if (*s == '\0') break;
371 if (*s == '\0') break;
377 static unsigned _mapObjectHash(NXMapTable *table, const void *key) {
378 return [(id)key hash];
381 static int _mapPtrIsEqual(NXMapTable *table, const void *key1, const void *key2) {
385 static int _mapStrIsEqual(NXMapTable *table, const void *key1, const void *key2) {
386 if (key1 == key2) return YES;
387 if (! key1) return ! strlen ((char *) key2);
388 if (! key2) return ! strlen ((char *) key1);
389 if (((char *) key1)[0] != ((char *) key2)[0]) return NO;
390 return (strcmp((char *) key1, (char *) key2)) ? NO : YES;
393 static int _mapObjectIsEqual(NXMapTable *table, const void *key1, const void *key2) {
394 return [(id)key1 isEqual:(id)key2];
397 static void _mapNoFree(NXMapTable *table, void *key, void *value) {}
399 static void _mapObjectFree(NXMapTable *table, void *key, void *value) {
403 const NXMapTablePrototype NXPtrValueMapPrototype = {
404 _mapPtrHash, _mapPtrIsEqual, _mapNoFree, 0
407 const NXMapTablePrototype NXStrValueMapPrototype = {
408 _mapStrHash, _mapStrIsEqual, _mapNoFree, 0
411 const NXMapTablePrototype NXObjectMapPrototype = {
412 _mapObjectHash, _mapObjectIsEqual, _mapObjectFree, 0