Jspice3
hash.c File Reference
#include "spice.h"
#include <stdio.h>
#include "cpstd.h"
Include dependency graph for hash.c:

Go to the source code of this file.

Data Structures

struct  s_hashlist
 

Macros

#define HTAB_SIZE   256
 
#define HASH(s, n)   {int j=0; n=0; while(s[j]) n+=s[j++]; n%=HTAB_SIZE;}
 
#define COMP(ss, tt)   {char *s=ss, *t=tt; while(*s && *s == *t){s++; t++;} i = *s - *t;}
 

Typedefs

typedef struct s_hashlist hashlist
 

Functions

void * htab_init ()
 
void htab_add (char *name, void *data, void *listp)
 
void htab_delete (char *name, void *listp)
 
void * htab_get (char *name, void *listp)
 
void htab_free (void *listp, int freedata)
 
void htab_print (void *listp, char *datafmt)
 
void * htab_wl (void *listp)
 
void * htab_list (void *listp)
 
int htab_empty (void *listp)
 

Macro Definition Documentation

#define COMP (   ss,
  tt 
)    {char *s=ss, *t=tt; while(*s && *s == *t){s++; t++;} i = *s - *t;}

Definition at line 22 of file hash.c.

#define HASH (   s,
 
)    {int j=0; n=0; while(s[j]) n+=s[j++]; n%=HTAB_SIZE;}

Definition at line 20 of file hash.c.

#define HTAB_SIZE   256

Definition at line 11 of file hash.c.

Typedef Documentation

typedef struct s_hashlist hashlist

Definition at line 18 of file hash.c.

Function Documentation

void htab_add ( char *  name,
void *  data,
void *  listp 
)

Definition at line 37 of file hash.c.

42 {
43  hashlist **list = (hashlist **)listp;
44  hashlist *h, *hh;
45  int n, i;
46 
47  if (list == NULL)
48  return;
49  HASH(name,n);
50  h = list[n];
51 
52  if (h == NULL) {
53  h = list[n] = (hashlist*)tmalloc(sizeof(hashlist));
54  h->h_name = copy(name);
55  h->h_what = data;
56  }
57  else {
58  for (hh = NULL; h; hh = h, h = h->h_next) {
59  COMP(name,h->h_name);
60  /*
61  i = strcmp(name,h->h_name);
62  */
63  if (i < 0)
64  continue;
65  if (i == 0)
66  return;
67  if (hh) {
68  hh->h_next = (hashlist*)tmalloc(sizeof(hashlist));
69  hh = hh->h_next;
70  }
71  else {
72  hh = (hashlist*)tmalloc(sizeof(hashlist));
73  list[n] = hh;
74  }
75  hh->h_name = copy(name);
76  hh->h_what = data;
77  hh->h_next = h;
78  return;
79  }
80  hh->h_next = (hashlist*)tmalloc(sizeof(hashlist));
81  hh = hh->h_next;
82  hh->h_name = copy(name);
83  hh->h_what = data;
84  hh->h_next = h;
85  }
86 }
char * copy()
#define COMP(ss, tt)
Definition: hash.c:22
char * tmalloc()
#define NULL
Definition: spdefs.h:121
#define HASH(s, n)
Definition: hash.c:20
char * h_name
Definition: hash.c:14
Definition: dir.c:53
struct s_hashlist * h_next
Definition: hash.c:16
void * h_what
Definition: hash.c:15
void htab_delete ( char *  name,
void *  listp 
)

Definition at line 90 of file hash.c.

94 {
95  hashlist **list = (hashlist**)listp;
96  hashlist *h, *hh;
97  int n, i;
98 
99  if (list == NULL)
100  return;
101  HASH(name,n);
102  h = list[n];
103 
104  if (h) {
105  for (hh = NULL; h; hh = h, h = h->h_next) {
106  COMP(name,h->h_name);
107  /*
108  i = strcmp(name,h->h_name);
109  */
110  if (i < 0)
111  continue;
112  if (i == 0) {
113  if (hh)
114  hh->h_next = h->h_next;
115  else
116  list[n] = h->h_next;
117  txfree(h->h_name);
118  txfree((char*)h);
119  return;
120  }
121  return;
122  }
123  }
124 }
#define COMP(ss, tt)
Definition: hash.c:22
void txfree()
#define NULL
Definition: spdefs.h:121
#define HASH(s, n)
Definition: hash.c:20
Definition: dir.c:53
struct s_hashlist * h_next
Definition: hash.c:16
int htab_empty ( void *  listp)

Definition at line 268 of file hash.c.

271 {
272  hashlist **list = (hashlist **)listp;
273  int i;
274 
275  if (list == NULL)
276  return (1);
277  for (i = 0; i < HTAB_SIZE; i++)
278  if (list[i])
279  return (0);
280  return (1);
281 }
#define NULL
Definition: spdefs.h:121
Definition: dir.c:53
#define HTAB_SIZE
Definition: hash.c:11
void htab_free ( void *  listp,
int  freedata 
)

Definition at line 161 of file hash.c.

165 {
166  hashlist **list = (hashlist **)listp;
167  hashlist *h, *hh;
168  int i;
169 
170  if (list == NULL)
171  return;
172 
173  for (i = 0; i < HTAB_SIZE; i++) {
174  for (h = list[i]; h; h = hh) {
175  hh = h->h_next;
176  txfree(h->h_name);
177  if (freedata) txfree((char*)h->h_what);
178  txfree((char*)h);
179  }
180  list[i] = NULL;
181  }
182 }
void txfree()
#define NULL
Definition: spdefs.h:121
char * h_name
Definition: hash.c:14
Definition: dir.c:53
struct s_hashlist * h_next
Definition: hash.c:16
void * h_what
Definition: hash.c:15
#define HTAB_SIZE
Definition: hash.c:11
void* htab_get ( char *  name,
void *  listp 
)

Definition at line 128 of file hash.c.

132 {
133  hashlist **list = (hashlist **)listp;
134  hashlist *h, *hh;
135  int n, i;
136 
137  if (list == NULL)
138  return (NULL);
139  HASH(name,n);
140  h = list[n];
141 
142  if (h) {
143  for (hh = NULL; h; hh = h, h = h->h_next) {
144  COMP(name,h->h_name);
145  /*
146  i = strcmp(name,h->h_name);
147  */
148  if (i < 0)
149  continue;
150  if (i == 0) {
151  return (h->h_what);
152  }
153  return (NULL);
154  }
155  }
156  return (NULL);
157 }
#define COMP(ss, tt)
Definition: hash.c:22
#define NULL
Definition: spdefs.h:121
#define HASH(s, n)
Definition: hash.c:20
Definition: dir.c:53
struct s_hashlist * h_next
Definition: hash.c:16
void* htab_init ( )

Definition at line 27 of file hash.c.

28 {
29  hashlist **h;
30 
31  h = (hashlist **)tmalloc(HTAB_SIZE * sizeof(hashlist*));
32  return ((void*)h);
33 }
char * tmalloc()
#define HTAB_SIZE
Definition: hash.c:11
void* htab_list ( void *  listp)

Definition at line 236 of file hash.c.

239 {
240  hashlist **list = (hashlist **)listp;
241  hashlist *h;
242  wordlist *wl, *wl0 = NULL;
243  int i;
244  char **s;
245 
246  if (list == NULL)
247  return (NULL);
248  for (i = 0; i < HTAB_SIZE; i++) {
249  for (h = list[i]; h; h = h->h_next) {
250  if (wl0 == NULL)
251  wl0 = wl = (wordlist*)tmalloc(sizeof(wordlist));
252  else {
253  wl->wl_next = (wordlist*)tmalloc(sizeof(wordlist));
254  wl->wl_next->wl_prev = wl;
255  wl = wl->wl_next;
256  }
257  s = (char**)tmalloc(2*sizeof(char*));
258  s[0] = h->h_name;
259  s[1] = (char*)h->h_what;
260  wl->wl_word = (char*)s;
261  }
262  }
263  return ((void*)wl0);
264 }
Definition: cddefs.h:119
Definition: library.c:18
char * tmalloc()
struct wordlist * wl_prev
Definition: cpstd.h:24
#define NULL
Definition: spdefs.h:121
Definition: cpstd.h:21
char * h_name
Definition: hash.c:14
Definition: dir.c:53
struct s_hashlist * h_next
Definition: hash.c:16
struct wordlist * wl_next
Definition: cpstd.h:23
char * wl_word
Definition: cpstd.h:22
void * h_what
Definition: hash.c:15
#define HTAB_SIZE
Definition: hash.c:11
void htab_print ( void *  listp,
char *  datafmt 
)

Definition at line 186 of file hash.c.

190 {
191  hashlist **list = (hashlist **)listp;
192  hashlist *h;
193  int i;
194 
195  if (list == NULL)
196  return;
197  if (datafmt == NULL)
198  datafmt = "hash=%d name=%s\n";
199  for (i = 0; i < HTAB_SIZE; i++) {
200  for (h = list[i]; h; h = h->h_next) {
201  fprintf(stderr, datafmt, i, h->h_name, h->h_what);
202  }
203  }
204 }
#define NULL
Definition: spdefs.h:121
char * h_name
Definition: hash.c:14
Definition: dir.c:53
struct s_hashlist * h_next
Definition: hash.c:16
void * h_what
Definition: hash.c:15
#define HTAB_SIZE
Definition: hash.c:11
void* htab_wl ( void *  listp)

Definition at line 208 of file hash.c.

211 {
212  hashlist **list = (hashlist **)listp;
213  hashlist *h;
214  wordlist *wl, *wl0 = NULL;
215  int i;
216 
217  if (list == NULL)
218  return (NULL);
219  for (i = 0; i < HTAB_SIZE; i++) {
220  for (h = list[i]; h; h = h->h_next) {
221  if (wl0 == NULL)
222  wl0 = wl = (wordlist*)tmalloc(sizeof(wordlist));
223  else {
224  wl->wl_next = (wordlist*)tmalloc(sizeof(wordlist));
225  wl->wl_next->wl_prev = wl;
226  wl = wl->wl_next;
227  }
228  wl->wl_word = copy(h->h_name);
229  }
230  }
231  return ((void*)wl0);
232 }
Definition: library.c:18
char * copy()
char * tmalloc()
struct wordlist * wl_prev
Definition: cpstd.h:24
#define NULL
Definition: spdefs.h:121
Definition: cpstd.h:21
char * h_name
Definition: hash.c:14
Definition: dir.c:53
struct s_hashlist * h_next
Definition: hash.c:16
struct wordlist * wl_next
Definition: cpstd.h:23
char * wl_word
Definition: cpstd.h:22
#define HTAB_SIZE
Definition: hash.c:11