diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ueda/sverp-bind/insthash.c	Sun Aug 02 00:00:15 2015 +0000
@@ -0,0 +1,60 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include "struct.h"
+
+#define	HASH_SIZE	1103
+static struct instance *hashtab[HASH_SIZE];
+
+static int
+hash_instname(str)
+	char *str;
+{
+	register u_long accum = 0;
+	register char *cp;
+	register int c, i;
+
+	for (cp = str, i = 1; c = *cp; cp++, i++)
+		accum += c * i;
+	return(accum % HASH_SIZE);
+}
+
+struct instance *
+enter_instance(newname)
+	char *newname;
+{
+	register struct instance *n, **np;
+	int namelen;
+
+	for (np = hashtab + hash_instname(newname); n = *np;
+	     np = &n->nextinhash)
+		if (!strcmp(n->name, newname)) {
+			fprintf(stderr, "error: duplicate instance name %s\n",
+				newname);
+			exit(1);
+		}
+	namelen = strlen(newname);
+	n = (struct instance *) malloc(sizeof(struct instance) + namelen + 1);
+	if (!n) {
+		perror("malloc");
+		exit(1);
+	}
+	bzero(n, sizeof(struct instance));
+	n->name = (char *)(n + 1);
+	strcpy(n->name, newname);
+	*np = n;
+	return n;
+}
+
+struct instance *
+find_instance(soughtname)
+	register char *soughtname;
+{
+	register struct instance *n;
+
+	for (n = hashtab[hash_instname(soughtname)]; n; n = n->nextinhash)
+		if (!strcmp(n->name, soughtname))
+			return(n);
+	return(0);
+}