view ueda/libuschem/compinst.c @ 139:bf188727e606

donl-rename-parts reader: no tEDAx-style escapes
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 07 Sep 2020 04:25:11 +0000
parents cd92449fdb51
children
line wrap: on
line source

/*
 * Component instance hash table logic
 */

#include <sys/types.h>
#include <stdio.h>
#include <strings.h>
#include "schemstruct.h"

extern char *malloc();

#define	HASH_SIZE	1103

static int
hash_instname(str)
	char *str;
{
	register u_long accum;
	register char *cp;
	register int c;

	for (cp = str, accum = 0; c = *cp; cp++) {
		accum <<= 4;
		accum |= c & 0x0F;
	}
	return(accum % HASH_SIZE);
}

hash_component_instances(schem)
	struct schem *schem;
{
	struct schemobj **hashtab;
	register struct schemobj *obj, *hc, **hcp;
	int errflag = 0, errstat = 0;

	hashtab = (struct schemobj **)
				malloc(sizeof(struct schemobj *) * HASH_SIZE);
	if (!hashtab) {
		perror("malloc");
		exit(1);
	}
	bzero(hashtab, sizeof(struct schemobj *) * HASH_SIZE);

	for (obj = schem->obj_next; obj != (struct schemobj *)schem;
	     obj = obj->obj_next) {
		if (obj->obj_type != OBJTYPE_COMPINST)
			continue;
		hcp = hashtab + hash_instname(obj->compobj_instname);
		for (; hc = *hcp; hcp = &hc->compobj_nextinhash)
		    if (!strcmp(obj->compobj_instname, hc->compobj_instname)) {
			fprintf(stderr,
	"%s: %s: duplicate component instance name (line %d, line %d)\n",
				schem->orig_filename, obj->compobj_instname,
				hc->obj_lineno, obj->obj_lineno);
			errflag = 1;
			break;
		    }
		if (errflag) {
			errflag = 0;
			errstat = -1;
		} else
			*hcp = obj;
	}
	schem->compinst_hash = hashtab;
	return(errstat);
}

report_compinst_hash_quality(schem)
	struct schem *schem;
{
	struct schemobj **hashtab;
	int maxchain, total;
	register int hb, curchain;
	register struct schemobj *obj;

	hashtab = schem->compinst_hash;
	for (hb = 0, total = maxchain = 0; hb < HASH_SIZE; hb++) {
		for (obj = hashtab[hb], curchain = 0; obj;
		     obj = obj->compobj_nextinhash) {
			curchain++;
			total++;
		}
		if (curchain > maxchain)
			maxchain = curchain;
	}
	printf("%s: %d component instances total, longest hash chain is %d\n",
		schem->orig_filename, total, maxchain);
}

struct schemobj *
find_component_instance(schem, instname)
	struct schem *schem;
	register char *instname;
{
	register struct schemobj *obj;

	for (obj = schem->compinst_hash[hash_instname(instname)]; obj;
	     obj = obj->compobj_nextinhash)
		if (!strcmp(obj->compobj_instname, instname))
			return(obj);
	return(NULL);
}