-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.c
119 lines (105 loc) · 2.92 KB
/
hash.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* $Id: hash.c,v 1.5 2006-12-13 01:11:37 heas Exp $
*
* Copyright (c) 1995-1998 by Cisco systems, Inc.
*
* Permission to use, copy, modify, and distribute this software for
* any purpose and without fee is hereby granted, provided that this
* copyright and permission notice appear on all copies of the
* software and supporting documentation, the name of Cisco Systems,
* Inc. not be used in advertising or publicity pertaining to
* distribution of the program without specific prior permission, and
* notice be given in supporting documentation that modification,
* copying and distribution is by permission of Cisco Systems, Inc.
*
* Cisco Systems, Inc. makes no representations about the suitability
* of this software for any purpose. THIS SOFTWARE IS PROVIDED ``AS
* IS'' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
* WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE.
*/
#include "tac_plus.h"
struct entry {
char *name;
void *hash;
};
typedef struct entry ENTRY;
static int calculate_hash(char *);
/* Calculate hash value from a string */
static int
calculate_hash(char *name)
{
int i;
size_t len = strlen(name);
int hashval = 0;
for (i = 0; i < len; i++) {
hashval += name[i] * (i + 1);
}
hashval += name[0];
hashval = hashval > 0 ? hashval : -hashval;
return(hashval);
}
/* Lookup a name in a hash table. Return its node if it exists, else NULL */
void *
hash_lookup(void **hashtab, char *name)
{
ENTRY *entry;
int hashval = calculate_hash(name);
entry = hashtab[hashval % HASH_TAB_SIZE];
while (entry) {
if (STREQ(name, entry->name))
/* Node exists in table. return it */
return(entry);
entry = entry->hash;
}
return(NULL);
}
/* Add a node to a hash table. Return node if it exists, NULL otherwise */
void *
hash_add_entry(void **hashtab, struct entry *newentry)
{
ENTRY *entry;
int hashval;
entry = hash_lookup(hashtab, newentry->name);
if (entry)
return(entry);
/* Node does not exist in table. Add it */
hashval = calculate_hash(newentry->name);
newentry->hash = hashtab[hashval % HASH_TAB_SIZE];
hashtab[hashval % HASH_TAB_SIZE] = newentry;
return(NULL);
}
/* Return an array of pointers to all the entries in a hash table */
void **
hash_get_entries(void **hashtab)
{
int i;
int cnt;
ENTRY *entry;
void **entries, **p;
int n, longest;
longest = 0;
cnt = 0;
for (i = 0; i < HASH_TAB_SIZE; i++) {
entry = hashtab[i];
n = 0;
while (entry) {
cnt++;
n++;
entry = entry->hash;
}
if (n > longest)
longest = n;
}
cnt++; /* Add space for NULL entry at end */
p = entries = (void **) tac_malloc(cnt * sizeof(void *));
for (i = 0; i < HASH_TAB_SIZE; i++) {
entry = hashtab[i];
while (entry) {
*p++ = entry;
entry = entry->hash;
}
}
*p++ = NULL;
return(entries);
}