]> err.no Git - sope/blob - gnustep-objc/hash.c
added --enable-pch to configure
[sope] / gnustep-objc / hash.c
1 /* Hash tables for Objective C internal structures
2    Copyright (C) 1993, 1996, 1997 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
6 GNU CC 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 2, or (at your option)
9 any later version.
10
11 GNU CC 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.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING.  If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.  */
20
21 /* As a special exception, if you link this library with files
22    compiled with GCC to produce an executable, this does not cause
23    the resulting executable to be covered by the GNU General Public License.
24    This exception does not however invalidate any other reasons why
25    the executable file might be covered by the GNU General Public License.  */
26
27 #include "assert.h"
28 #include "hash.h"
29 #include "runtime.h"            /* for DEBUG_PRINTF */
30 #include "objc-api.h"
31
32 /* These two macros determine when a hash table is full and
33    by how much it should be expanded respectively.
34
35    These equations are percentages.  */
36 #define FULLNESS(cache)    ((((cache)->size * 75) / 100) <= (cache)->used)
37 #define EXPANSION(cache)   ((cache)->size * 2)
38
39 cache_ptr hash_new (unsigned int size, char hashType)
40 {
41   cache_ptr cache;
42
43   /* Pass me a value greater than 0 and a power of 2.  */
44   assert (size);
45   assert (!(size & (size - 1)));
46
47   /* Allocate the cache structure.  calloc insures
48      its initialization for default values.  */
49   cache = (cache_ptr)OBJC_CALLOC_UNCOLLECTABLE(sizeof (struct cache));
50   assert (cache);
51
52   /* Allocate the array of buckets for the cache.
53      calloc initializes all of the pointers to NULL.  */
54   cache->node_table =
55     (node_ptr *)OBJC_CALLOC_UNCOLLECTABLE(size * sizeof (node_ptr));
56
57   assert (cache->node_table);
58
59   cache->size  = size;
60
61   /* This should work for all processor architectures? */
62   cache->mask = (size - 1);
63
64   cache->hashType = hashType;
65   
66   return cache;
67 }
68
69
70 void
71 hash_add (cache_ptr *cachep, const void *key, void *value)
72 {
73   size_t indx = objc_call_hash(*cachep, key);
74   //hh: size_t indx = (*(*cachep)->hash_func)(*cachep, key);
75   node_ptr node;
76
77   node = (node_ptr)OBJC_CALLOC_UNCOLLECTABLE(sizeof (struct cache_node));
78   assert (node);
79
80   /* Initialize the new node.  */
81   node->key    = key;
82   node->value  = value;
83   node->next  = (*cachep)->node_table[indx];
84
85   /* Debugging.
86      Check the list for another key.  */
87 #if DEBUG_OBJC
88   { node_ptr node1 = (*cachep)->node_table[indx];
89
90     while (node1) {
91
92       assert (node1->key != key);
93       node1 = node1->next;
94     }
95   }
96 #endif
97
98   /* Install the node as the first element on the list.  */
99   (*cachep)->node_table[indx] = node;
100
101   /* Bump the number of entries in the cache.  */
102   ++(*cachep)->used;
103
104   /* Check the hash table's fullness.   We're going
105      to expand if it is above the fullness level.  */
106   if (FULLNESS (*cachep)) {
107
108     /* The hash table has reached its fullness level.  Time to
109        expand it.
110
111        I'm using a slow method here but is built on other
112        primitive functions thereby increasing its
113        correctness.  */
114     node_ptr node1 = NULL;
115     cache_ptr new = hash_new (EXPANSION (*cachep), (*cachep)->hashType);
116     
117     DEBUG_PRINTF ("Expanding cache 0x%08X from %d to %d\n",
118                   (size_t)*cachep, (*cachep)->size, new->size);
119
120     /* Copy the nodes from the first hash table to the new one.  */
121     while ((node1 = hash_next (*cachep, node1)))
122       hash_add (&new, node1->key, node1->value);
123
124     /* Trash the old cache.  */
125     hash_delete (*cachep);
126
127     /* Return a pointer to the new hash table.  */
128     *cachep = new;
129   }
130 }