diff options
Diffstat (limited to 'hash.c')
-rw-r--r-- | hash.c | 31 |
1 files changed, 11 insertions, 20 deletions
@@ -37,10 +37,9 @@ static unsigned hash(const char *string) { - int i; unsigned u = 0; - for (i = 0; string[i] != '\0'; i++) + for (int i = 0; string[i] != '\0'; i++) u = (u << 5) + (u >> (LOG2_NUMBUCKETS - 5)) + (unsigned char) string[i]; return (u & (NUMBUCKETS - 1)); } @@ -63,12 +62,9 @@ makeHashTable(void) void destroyHashTable(HashTablePtr table) { - int i; - HashBucketPtr bp; - - for (i = 0; i < NUMBUCKETS; i++) { + for (int i = 0; i < NUMBUCKETS; i++) { while (table[i]) { - bp = table[i]; + HashBucketPtr bp = table[i]; table[i] = table[i]->next; free(bp->key); free(bp->value); @@ -82,9 +78,8 @@ char * getHash(HashTablePtr table, const char *key) { unsigned int i = hash(key); - HashBucketPtr bp; - for (bp = table[i]; bp; bp = bp->next) { + for (HashBucketPtr bp = table[i]; bp; bp = bp->next) { if (strcasecmp(bp->key, key) == 0) return bp->value; } @@ -144,12 +139,10 @@ putHash(HashTablePtr table, char *key, char *value, int prio) int hashElements(HashTablePtr table) { - int i, n; - HashBucketPtr bp; + int n = 0; - n = 0; - for (i = 0; i < NUMBUCKETS; i++) { - for (bp = table[i]; bp; bp = bp->next) { + for (int i = 0; i < NUMBUCKETS; i++) { + for (HashBucketPtr bp = table[i]; bp; bp = bp->next) { n++; } } @@ -181,16 +174,14 @@ value_first_cmp(const void *v1, const void *v2) HashBucketPtr * hashArray(HashTablePtr table, int value_first) { - int i, j, n; - HashBucketPtr *dst; - - n = hashElements(table); - dst = malloc((n + 1) * sizeof(HashBucketPtr)); + int j; + int n = hashElements(table); + HashBucketPtr *dst = malloc((n + 1) * sizeof(HashBucketPtr)); if (dst == NULL) return NULL; j = 0; - for (i = 0; i < NUMBUCKETS; i++) { + for (int i = 0; i < NUMBUCKETS; i++) { while (table[i]) { dst[j++] = table[i]; table[i] = table[i]->next; |