This repository has been archived by the owner on Jun 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
tableLookup.c
108 lines (87 loc) · 2.75 KB
/
tableLookup.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define HASHSIZE 101
struct nlist { /* table entry */
struct nlist *next; /* next entry in chain */
char *name;
char *defn;
};
static struct nlist *hashtab[HASHSIZE]; /* pointer table */
struct nlist *install(char *name, char *defn);
struct nlist *lookup(char *s);
int undef(char * name);
int main() {
}
unsigned hash(char *s);
char *_strdup(char *s);
struct nlist *install(char *name, char *defn) {
struct nlist *np;
unsigned hashval;
if ((np = lookup(name)) == NULL) { /* not found */
np = (struct nlist *) malloc(sizeof(*np));
if (np == NULL || (np->name = _strdup(name)) == NULL)
return NULL;
hashval = hash(name);
np->next = hashtab[hashval];
hashtab[hashval] = np;
} else /* already there */
free((void *) np->defn); /* free previous defn */
if ((np->defn = _strdup(defn)) == NULL)
return NULL;
return np;
}
int undef(char * name) {
struct nlist * np1, * np2;
/* If we don't find an existing record then there is
* nothing to remove. */
if ((np1 = lookup(name)) == NULL)
return 1;
/* Walk along the list, keeping track of the previous
* record we had. */
for (np1 = np2 = hashtab[hash(name)]; np1 != NULL;
np2 = np1, np1 = np1->next ) {
/* We found the record to remove */
if (strcmp(name, np1->name) == 0) {
/* Remove node from list */
if (np1 == np2)
/* If there was no previous record, ie. we're
* at the root of the table, then set the table
* pointer to np1's next. */
hashtab[hash(name)] = np1->next;
else
/* Replace the previous node's next pointer
* (that was pointing at np1) to the record
* that np1's next pointer is pointing at. */
np2->next = np1->next;
/* Free up memory. */
free(np1->name);
free(np1->defn);
free(np1);
return 0;
}
}
return 1; /* Name not found. */
}
/* hash: form hash value for string s */
unsigned hash(char *s) {
unsigned hashval;
for (hashval = 0; *s != '\0'; s++)
hashval = *s + 31 * hashval;
return hashval % HASHSIZE;
}
/* lookup: look for s in hashtab */
struct nlist *lookup(char *s) {
struct nlist *np;
for (np = hashtab[hash(s)]; np != NULL; np = np->next)
if (strcmp(s, np->name) == 0)
return np; /* found */
return NULL; /* not found */
}
char *_strdup(char *s) { /* make a duplicate of s */
char *p;
p = (char *) malloc(strlen(s) + 1); /* +1 for '\0' */
if (p != NULL)
strcpy(p, s);
return p;
}