#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STACKSIZE 20
#define HSZ 100
#define OPS 4
#define MAXSTRLEN 50
enum { PUSH, POP, FILL, EMPTY };
typedef struct {
int len[STACKSIZE];
void (*method[OPS]) (int[]);
} stackObj;
struct htable {
char *id;
struct htable *nxt;
stackObj *stack;
};
static int listsz = 0;
static struct htable *hlist[HSZ];
void pop(int[]);
void push(int[]);
void randomfill(int[]);
void empty(int[]);
int populatehash(char *);
void walkhash(char *);
void assignops(stackObj *);
unsigned int ahash(char *);
struct htable *alookup(char *);
struct htable *ainstall(char *, char *);
int main(int argc, char **argv)
{
if (argc != 2) {
printf("Please enter filename.\n");
return 1;
}
if (populatehash(argv[1]) < 0) {
printf("Error populating hashtable.\n");
return 1;
}
printf("Table size: %d\n", listsz);
return 0;
}
/*helpers & templates*/
void pop(int arr[])
{
return;
}
void push(int arr[])
{
return;
}
void empty(int arr[])
{
return;
}
void randomfill(int arr[])
{
return;
}
int populatehash(char *fname)
{
FILE *pt;
struct htable *hpt;
char linename[MAXSTRLEN];
if ((pt = fopen(fname, "r")) == NULL) {
return -1;
}
while (fgets(linename, MAXSTRLEN, pt) != NULL) {
hpt = ainstall(linename, NULL);
printf
("Key for node at %p is %s.\nstackObj pointer at %p.\nFunctional assignments at pop - %p push - %p fill - %p empty - %p.\n",
hpt, hpt->id, hpt->stack, hpt->stack->method[POP],
hpt->stack->method[PUSH], hpt->stack->method[FILL],
hpt->stack->method[EMPTY]);
listsz++;
}
fclose(pt);
return 0;
}
/*hashtable specific*/
void assignops(stackObj * pp)
{
int p;
pp->method[PUSH] = push;
pp->method[POP] = pop;
pp->method[FILL] = randomfill;
pp->method[EMPTY] = empty;
}
unsigned int ahash(char *value)
{
unsigned int hval;
for (hval = 0; *value != '\0'; value++) {
hval = *value + 31 * hval;
}
return hval % HSZ;
}
struct htable *alookup(char *value)
{
struct htable *walk;
for (walk = hlist[ahash(value)]; walk != NULL; walk = walk->nxt) {
if (strcmp(value, walk->id) == 0) {
return walk;
}
}
return NULL;
}
struct htable *ainstall(char *value, char *nvalue)
{
struct htable *new;
unsigned int hval;
if ((new = alookup(value)) != NULL) {
if (nvalue != NULL) {
free(new->id);
new->id = strdup(nvalue);
}
return new;
}
if ((new = alookup(value)) == NULL) {
/*needs error checking */
new = malloc(sizeof(*new));
new->id = strdup(value);
new->stack = malloc(sizeof(*new->stack));
assignops(new->stack);
hval = ahash(new->id);
new->nxt = hlist[hval];
hlist[hval] = new;
return new;
}
}