File: hash.c

package info (click to toggle)
joe 2.8-15.2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,184 kB
  • ctags: 1,764
  • sloc: ansic: 18,821; asm: 224; makefile: 122; sh: 9
file content (60 lines) | stat: -rw-r--r-- 937 bytes parent folder | download | duplicates (3)
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
/* Simple hash table */

#include "zstr.h"
#include "hash.h"

static HENTRY *freentry=0;

unsigned long hash(s)
char *s;
 {
 unsigned long accu=0;
 while(*s) accu=hnext(accu,*s++);
 return accu;
 }

HASH *htmk(len)
 {
 HASH *t=(HASH *)malloc(sizeof(HASH));
 t->len=len-1;
 t->tab=(HENTRY **)calloc(sizeof(HENTRY *),len);
 return t;
 }

void htrm(ht)
HASH *ht;
 {
 free(ht->tab);
 free(ht);
 }

void *htadd(ht,name,val)
HASH *ht;
char *name;
void *val;
 {
 int idx=hash(name)&ht->len;
 HENTRY *entry;
 if(!freentry)
  {
  int x;
  entry=(HENTRY *)malloc(sizeof(HENTRY)*64);
  for(x=0;x!=64;++x) entry[x].next=freentry, freentry=entry+x;
  }
 entry=freentry;
 freentry=entry->next;
 entry->next=ht->tab[idx];
 ht->tab[idx]=entry;
 entry->name=name;
 return entry->val=val;
 }

void *htfind(ht,name)
HASH *ht;
char *name;
 {
 HENTRY *e;
 for(e=ht->tab[hash(name)&ht->len];e;e=e->next)
  if(!zcmp(e->name,name)) return e->val;
 return 0;
 }