Commit 80b0e34e by Richard Stallman

Fix indentation. Carry out renamings from hash.h.

Rename args and locals also.

From-SVN: r2193
parent d0bd180d
...@@ -23,10 +23,13 @@ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ ...@@ -23,10 +23,13 @@ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
the executable file might be covered by the GNU General Public License. */ the executable file might be covered by the GNU General Public License. */
/* /*
$Header: /home/fsf/rms/c-runtime/dispatch/RCS/hash.c,v 0.14 1992/08/31 21:09:15 dglattin Exp rms $ $Header: /home/fsf/rms/c-runtime/dispatch/RCS/hash.c,v 0.15 1992/09/02 02:04:32 rms Exp rms $
$Author: dglattin $ $Author: rms $
$Date: 1992/08/31 21:09:15 $ $Date: 1992/09/02 02:04:32 $
$Log: hash.c,v $ $Log: hash.c,v $
* Revision 0.15 1992/09/02 02:04:32 rms
* Changed some decls.
*
* Revision 0.14 1992/08/31 21:09:15 dglattin * Revision 0.14 1992/08/31 21:09:15 dglattin
* minor documentation changes. * minor documentation changes.
* *
...@@ -99,99 +102,99 @@ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ ...@@ -99,99 +102,99 @@ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
These equations are percentages. */ These equations are percentages. */
#define FULLNESS(cache) \ #define FULLNESS(cache) \
((((cache)->sizeOfHash * 75) / 100) <= (cache)->entriesInHash) ((((cache)->size * 75) / 100) <= (cache)->used)
#define EXPANSION(cache) \ #define EXPANSION(cache) \
((cache)->sizeOfHash * 2) ((cache)->size * 2)
Cache_t cache_ptr
hash_new (u_int sizeOfHash, HashFunc aHashFunc, CompareFunc aCompareFunc) hash_new (u_int size, hash_func_type hash_func, compare_func_type compare_func)
{ {
Cache_t retCache; cache_ptr cache;
/* Pass me a value greater than 0 and a power of 2. */ /* Pass me a value greater than 0 and a power of 2. */
assert (sizeOfHash); assert (size);
assert (!(sizeOfHash & (sizeOfHash - 1))); assert (!(size & (size - 1)));
/* Allocate the cache structure. calloc insures /* Allocate the cache structure. calloc insures
its initialization for default values. */ its initialization for default values. */
retCache = (Cache_t) calloc (1, sizeof (Cache)); cache = (cache_ptr) calloc (1, sizeof (struct cache));
assert (retCache); assert (cache);
/* Allocate the array of buckets for the cache. /* Allocate the array of buckets for the cache.
calloc initializes all of the pointers to NULL. */ calloc initializes all of the pointers to NULL. */
retCache->theNodeTable cache->node_table
= (CacheNode_t *) calloc (sizeOfHash, sizeof (CacheNode_t)); = (node_ptr *) calloc (size, sizeof (node_ptr));
assert (retCache->theNodeTable); assert (cache->node_table);
retCache->sizeOfHash = sizeOfHash; cache->size = size;
/* This should work for all processor architectures? */ /* This should work for all processor architectures? */
retCache->mask = (sizeOfHash - 1); cache->mask = (size - 1);
/* Store the hashing function so that codes can be computed. */ /* Store the hashing function so that codes can be computed. */
retCache->hashFunc = aHashFunc; cache->hash_func = hash_func;
/* Store the function that compares hash keys to /* Store the function that compares hash keys to
determine if they are equal. */ determine if they are equal. */
retCache->compareFunc = aCompareFunc; cache->compare_func = compare_func;
return retCache; return cache;
} }
void void
hash_delete (Cache_t theCache) hash_delete (cache_ptr cache)
{ {
CacheNode_t aNode; node_ptr node;
/* Purge all key/value pairs from the table. */ /* Purge all key/value pairs from the table. */
while (aNode = hash_next (theCache, NULL)) while (node = hash_next (cache, NULL))
hash_remove (theCache, aNode->theKey); hash_remove (cache, node->key);
/* Release the array of nodes and the cache itself. */ /* Release the array of nodes and the cache itself. */
free (theCache->theNodeTable); free (cache->node_table);
free (theCache); free (cache);
} }
void void
hash_add (Cache_t *theCache, void *aKey, void *aValue) hash_add (cache_ptr *cachep, void *key, void *value)
{ {
u_int indx = (*(*theCache)->hashFunc)(*theCache, aKey); u_int indx = (*(*cachep)->hash_func)(*cachep, key);
CacheNode_t aCacheNode = (CacheNode_t) calloc (1, sizeof (CacheNode)); node_ptr node = (node_ptr) calloc (1, sizeof (struct cache_node));
assert (aCacheNode); assert (node);
/* Initialize the new node. */ /* Initialize the new node. */
aCacheNode->theKey = aKey; node->key = key;
aCacheNode->theValue = aValue; node->value = value;
aCacheNode->nextNode = (*(*theCache)->theNodeTable) [indx]; node->next = (*(*cachep)->node_table)[indx];
/* Debugging. /* Debugging.
Check the list for another key. */ Check the list for another key. */
#ifdef DEBUG #ifdef DEBUG
{ CacheNode_t checkHashNode = (*(*theCache)->theNodeTable) [indx]; { node_ptr node1 = (*(*cachep)->node_table)[indx];
while (checkHashNode) { while (node1) {
assert (checkHashNode->theKey != aKey); assert (node1->key != key);
checkHashNode = checkHashNode->nextNode; node1 = node1->next;
} }
} }
#endif #endif
/* Install the node as the first element on the list. */ /* Install the node as the first element on the list. */
(*(*theCache)->theNodeTable) [indx] = aCacheNode; (*(*cachep)->node_table)[indx] = node;
/* Bump the number of entries in the cache. */ /* Bump the number of entries in the cache. */
++(*theCache)->entriesInHash; ++(*cachep)->used;
/* Check the hash table's fullness. We're going /* Check the hash table's fullness. We're going
to expand if it is above the fullness level. */ to expand if it is above the fullness level. */
if (FULLNESS (*theCache)) { if (FULLNESS (*cachep)) {
/* The hash table has reached its fullness level. Time to /* The hash table has reached its fullness level. Time to
expand it. expand it.
...@@ -199,94 +202,92 @@ hash_add (Cache_t *theCache, void *aKey, void *aValue) ...@@ -199,94 +202,92 @@ hash_add (Cache_t *theCache, void *aKey, void *aValue)
I'm using a slow method here but is built on other I'm using a slow method here but is built on other
primitive functions thereby increasing its primitive functions thereby increasing its
correctness. */ correctness. */
CacheNode_t aNode = NULL; node_ptr node1 = NULL;
Cache_t newCache = hash_new (EXPANSION (*theCache), cache_ptr new = hash_new (EXPANSION (*cachep),
(*theCache)->hashFunc, (*cachep)->hash_func,
(*theCache)->compareFunc); (*cachep)->compare_func);
DEBUG_PRINTF (stderr, "Expanding cache %#x from %d to %d\n", DEBUG_PRINTF (stderr, "Expanding cache %#x from %d to %d\n",
*theCache, (*theCache)->sizeOfHash, newCache->sizeOfHash); *cachep, (*cachep)->size, new->size);
/* Copy the nodes from the first hash table to the new one. */ /* Copy the nodes from the first hash table to the new one. */
while (aNode = hash_next (*theCache, aNode)) while (node1 = hash_next (*cachep, node1))
hash_add (&newCache, aNode->theKey, aNode->theValue); hash_add (&new, node1->key, node1->value);
/* Trash the old cache. */ /* Trash the old cache. */
hash_delete (*theCache); hash_delete (*cachep);
/* Return a pointer to the new hash table. */ /* Return a pointer to the new hash table. */
*theCache = newCache; *cachep = new;
} }
} }
void void
hash_remove (Cache_t theCache, void *aKey) hash_remove (cache_ptr cache, void *key)
{ {
u_int indx = (*theCache->hashFunc)(theCache, aKey); u_int indx = (*cache->hash_func)(cache, key);
CacheNode_t aCacheNode = (*theCache->theNodeTable) [indx]; node_ptr node = (*cache->node_table)[indx];
/* We assume there is an entry in the table. Error if it is not. */ /* We assume there is an entry in the table. Error if it is not. */
assert (aCacheNode); assert (node);
/* Special case. First element is the key/value pair to be removed. */ /* Special case. First element is the key/value pair to be removed. */
if ((*theCache->compareFunc)(aCacheNode->theKey, aKey)) { if ((*cache->compare_func)(node->key, key)) {
(*theCache->theNodeTable) [indx] = aCacheNode->nextNode; (*cache->node_table)[indx] = node->next;
free (aCacheNode); free (node);
} else { } else {
/* Otherwise, find the hash entry. */ /* Otherwise, find the hash entry. */
CacheNode_t prevHashNode = aCacheNode; node_ptr prev = node;
BOOL removed = NO; BOOL removed = NO;
do { do {
if ((*theCache->compareFunc)(aCacheNode->theKey, aKey)) { if ((*cache->compare_func)(node->key, key)) {
prevHashNode->nextNode = aCacheNode->nextNode, removed = YES; prev->next = node->next, removed = YES;
free (aCacheNode); free (node);
} else } else
prevHashNode = aCacheNode, aCacheNode = aCacheNode->nextNode; prev = node, node = node->next;
} while (!removed && aCacheNode); } while (!removed && node);
assert (removed); assert (removed);
} }
/* Decrement the number of entries in the hash table. */ /* Decrement the number of entries in the hash table. */
--theCache->entriesInHash; --cache->used;
} }
CacheNode_t node_ptr
hash_next (Cache_t theCache, CacheNode_t aCacheNode) hash_next (cache_ptr cache, node_ptr node)
{ {
CacheNode_t theCacheNode = aCacheNode;
/* If the scan is being started then reset the last node /* If the scan is being started then reset the last node
visitied pointer and bucket index. */ visitied pointer and bucket index. */
if (!theCacheNode) if (!node)
theCache->lastBucket = 0; cache->last_bucket = 0;
/* If there is a node visited last then check for another /* If there is a node visited last then check for another
entry in the same bucket; Otherwise step to the next bucket. */ entry in the same bucket; Otherwise step to the next bucket. */
if (theCacheNode) if (node) {
if (theCacheNode->nextNode) if (node->next)
/* There is a node which follows the last node /* There is a node which follows the last node
returned. Step to that node and retun it. */ returned. Step to that node and retun it. */
return theCacheNode->nextNode; return node->next;
else else
++theCache->lastBucket; ++cache->last_bucket;
}
/* If the list isn't exhausted then search the buckets for /* If the list isn't exhausted then search the buckets for
other nodes. */ other nodes. */
if (theCache->lastBucket < theCache->sizeOfHash) { if (cache->last_bucket < cache->size) {
/* Scan the remainder of the buckets looking for an entry /* Scan the remainder of the buckets looking for an entry
at the head of the list. Return the first item found. */ at the head of the list. Return the first item found. */
while (theCache->lastBucket < theCache->sizeOfHash) while (cache->last_bucket < cache->size)
if ((*theCache->theNodeTable) [theCache->lastBucket]) if ((*cache->node_table)[cache->last_bucket])
return (*theCache->theNodeTable) [theCache->lastBucket]; return (*cache->node_table)[cache->last_bucket];
else else
++theCache->lastBucket; ++cache->last_bucket;
/* No further nodes were found in the hash table. */ /* No further nodes were found in the hash table. */
return NULL; return NULL;
...@@ -300,20 +301,20 @@ hash_next (Cache_t theCache, CacheNode_t aCacheNode) ...@@ -300,20 +301,20 @@ hash_next (Cache_t theCache, CacheNode_t aCacheNode)
* key/value pair isn't in the hash. * key/value pair isn't in the hash.
*/ */
void * void *
hash_value_for_key (Cache_t theCache, void *aKey) hash_value_for_key (cache_ptr cache, void *key)
{ {
CacheNode_t aCacheNode node_ptr node
= (*theCache->theNodeTable) [(*theCache->hashFunc)(theCache, aKey)]; = (*cache->node_table)[(*cache->hash_func)(cache, key)];
void *retVal = NULL; void *retval = NULL;
if (aCacheNode) if (node)
do { do {
if ((*theCache->compareFunc)(aCacheNode->theKey, aKey)) if ((*cache->compare_func)(node->key, key))
retVal = aCacheNode->theValue; retval = node->value;
else else
aCacheNode = aCacheNode->nextNode; node = node->next;
} while (!retVal && aCacheNode); } while (!retval && node);
return retVal; return retval;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment