comparison ueda/sverp-bind/insthash.c @ 13:1f3283f8e482

unet-bind: instance hash implemented
author Space Falcon <falcon@ivan.Harhan.ORG>
date Sun, 02 Aug 2015 00:00:15 +0000
parents
children f7b09a54c2ce
comparison
equal deleted inserted replaced
12:51893347bc42 13:1f3283f8e482
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <strings.h>
5 #include "struct.h"
6
7 #define HASH_SIZE 1103
8 static struct instance *hashtab[HASH_SIZE];
9
10 static int
11 hash_instname(str)
12 char *str;
13 {
14 register u_long accum = 0;
15 register char *cp;
16 register int c, i;
17
18 for (cp = str, i = 1; c = *cp; cp++, i++)
19 accum += c * i;
20 return(accum % HASH_SIZE);
21 }
22
23 struct instance *
24 enter_instance(newname)
25 char *newname;
26 {
27 register struct instance *n, **np;
28 int namelen;
29
30 for (np = hashtab + hash_instname(newname); n = *np;
31 np = &n->nextinhash)
32 if (!strcmp(n->name, newname)) {
33 fprintf(stderr, "error: duplicate instance name %s\n",
34 newname);
35 exit(1);
36 }
37 namelen = strlen(newname);
38 n = (struct instance *) malloc(sizeof(struct instance) + namelen + 1);
39 if (!n) {
40 perror("malloc");
41 exit(1);
42 }
43 bzero(n, sizeof(struct instance));
44 n->name = (char *)(n + 1);
45 strcpy(n->name, newname);
46 *np = n;
47 return n;
48 }
49
50 struct instance *
51 find_instance(soughtname)
52 register char *soughtname;
53 {
54 register struct instance *n;
55
56 for (n = hashtab[hash_instname(soughtname)]; n; n = n->nextinhash)
57 if (!strcmp(n->name, soughtname))
58 return(n);
59 return(0);
60 }