1 /* Hash tables for Objective C internal structures
2 Copyright (C) 1993, 1996, 1997, 2004, 2009 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 <http://www.gnu.org/licenses/>. */
27 #include "objc/hash.h"
29 #include "objc/runtime.h" /* for DEBUG_PRINTF */
31 /* These two macros determine when a hash table is full and
32 by how much it should be expanded respectively.
34 These equations are percentages. */
35 #define FULLNESS(cache) \
36 ((((cache)->size * 75) / 100) <= (cache)->used)
37 #define EXPANSION(cache) \
41 objc_hash_new (unsigned int size
, hash_func_type hash_func
,
42 compare_func_type compare_func
)
46 /* Pass me a value greater than 0 and a power of 2. */
48 assert (! (size
& (size
- 1)));
50 /* Allocate the cache structure. calloc insures
51 its initialization for default values. */
52 cache
= (cache_ptr
) objc_calloc (1, sizeof (struct cache
));
55 /* Allocate the array of buckets for the cache.
56 calloc initializes all of the pointers to NULL. */
58 = (node_ptr
*) objc_calloc (size
, sizeof (node_ptr
));
59 assert (cache
->node_table
);
63 /* This should work for all processor architectures? */
64 cache
->mask
= (size
- 1);
66 /* Store the hashing function so that codes can be computed. */
67 cache
->hash_func
= hash_func
;
69 /* Store the function that compares hash keys to
70 determine if they are equal. */
71 cache
->compare_func
= compare_func
;
78 objc_hash_delete (cache_ptr cache
)
84 /* Purge all key/value pairs from the table. */
85 /* Step through the nodes one by one and remove every node WITHOUT
86 using objc_hash_next. this makes objc_hash_delete much more efficient. */
87 for (i
= 0;i
< cache
->size
;i
++) {
88 if ((node
= cache
->node_table
[i
])) {
89 /* an entry in the hash table has been found, now step through the
90 nodes next in the list and free them. */
91 while ((next_node
= node
->next
)) {
92 objc_hash_remove (cache
,node
->key
);
96 objc_hash_remove (cache
,node
->key
);
100 /* Release the array of nodes and the cache itself. */
101 objc_free(cache
->node_table
);
107 objc_hash_add (cache_ptr
*cachep
, const void *key
, void *value
)
109 size_t indx
= (*(*cachep
)->hash_func
)(*cachep
, key
);
110 node_ptr node
= (node_ptr
) objc_calloc (1, sizeof (struct cache_node
));
115 /* Initialize the new node. */
118 node
->next
= (*cachep
)->node_table
[indx
];
121 Check the list for another key. */
123 { node_ptr node1
= (*cachep
)->node_table
[indx
];
127 assert (node1
->key
!= key
);
133 /* Install the node as the first element on the list. */
134 (*cachep
)->node_table
[indx
] = node
;
136 /* Bump the number of entries in the cache. */
139 /* Check the hash table's fullness. We're going
140 to expand if it is above the fullness level. */
141 if (FULLNESS (*cachep
)) {
143 /* The hash table has reached its fullness level. Time to
146 I'm using a slow method here but is built on other
147 primitive functions thereby increasing its
149 node_ptr node1
= NULL
;
150 cache_ptr
new = objc_hash_new (EXPANSION (*cachep
),
151 (*cachep
)->hash_func
,
152 (*cachep
)->compare_func
);
154 DEBUG_PRINTF ("Expanding cache %#x from %d to %d\n",
155 (int) *cachep
, (*cachep
)->size
, new->size
);
157 /* Copy the nodes from the first hash table to the new one. */
158 while ((node1
= objc_hash_next (*cachep
, node1
)))
159 objc_hash_add (&new, node1
->key
, node1
->value
);
161 /* Trash the old cache. */
162 objc_hash_delete (*cachep
);
164 /* Return a pointer to the new hash table. */
171 objc_hash_remove (cache_ptr cache
, const void *key
)
173 size_t indx
= (*cache
->hash_func
)(cache
, key
);
174 node_ptr node
= cache
->node_table
[indx
];
177 /* We assume there is an entry in the table. Error if it is not. */
180 /* Special case. First element is the key/value pair to be removed. */
181 if ((*cache
->compare_func
)(node
->key
, key
)) {
182 cache
->node_table
[indx
] = node
->next
;
186 /* Otherwise, find the hash entry. */
187 node_ptr prev
= node
;
192 if ((*cache
->compare_func
)(node
->key
, key
)) {
193 prev
->next
= node
->next
, removed
= YES
;
196 prev
= node
, node
= node
->next
;
197 } while (! removed
&& node
);
201 /* Decrement the number of entries in the hash table. */
207 objc_hash_next (cache_ptr cache
, node_ptr node
)
209 /* If the scan is being started then reset the last node
210 visitied pointer and bucket index. */
212 cache
->last_bucket
= 0;
214 /* If there is a node visited last then check for another
215 entry in the same bucket; Otherwise step to the next bucket. */
218 /* There is a node which follows the last node
219 returned. Step to that node and retun it. */
222 ++cache
->last_bucket
;
225 /* If the list isn't exhausted then search the buckets for
227 if (cache
->last_bucket
< cache
->size
) {
228 /* Scan the remainder of the buckets looking for an entry
229 at the head of the list. Return the first item found. */
230 while (cache
->last_bucket
< cache
->size
)
231 if (cache
->node_table
[cache
->last_bucket
])
232 return cache
->node_table
[cache
->last_bucket
];
234 ++cache
->last_bucket
;
236 /* No further nodes were found in the hash table. */
243 /* Given KEY, return corresponding value for it in CACHE.
244 Return NULL if the KEY is not recorded. */
247 objc_hash_value_for_key (cache_ptr cache
, const void *key
)
249 node_ptr node
= cache
->node_table
[(*cache
->hash_func
)(cache
, key
)];
254 if ((*cache
->compare_func
)(node
->key
, key
)) {
255 retval
= node
->value
;
259 } while (! retval
&& node
);
264 /* Given KEY, return YES if it exists in the CACHE.
265 Return NO if it does not */
268 objc_hash_is_key_in_hash (cache_ptr cache
, const void *key
)
270 node_ptr node
= cache
->node_table
[(*cache
->hash_func
)(cache
, key
)];
274 if ((*cache
->compare_func
)(node
->key
, key
))