Jspice3
hash.c
Go to the documentation of this file.
1 /***************************************************************************
2 JSPICE3 adaptation of Spice3e2 - Copyright (c) Stephen R. Whiteley 1992
3 Copyright 1990 Regents of the University of California. All rights reserved.
4 Authors: 1993 Stephen R. Whiteley
5 ****************************************************************************/
6 
7 #include "spice.h"
8 #include <stdio.h>
9 #include "cpstd.h"
10 
11 #define HTAB_SIZE 256
12 
13 struct s_hashlist {
14  char *h_name;
15  void *h_what;
16  struct s_hashlist *h_next;
17 };
18 typedef struct s_hashlist hashlist;
19 
20 #define HASH(s,n) {int j=0; n=0; while(s[j]) n+=s[j++]; n%=HTAB_SIZE;}
21 
22 #define COMP(ss,tt) \
23  {char *s=ss, *t=tt; while(*s && *s == *t){s++; t++;} i = *s - *t;}
24 
25 
26 void *
28 {
29  hashlist **h;
30 
31  h = (hashlist **)tmalloc(HTAB_SIZE * sizeof(hashlist*));
32  return ((void*)h);
33 }
34 
35 
36 void
37 htab_add(name,data,listp)
38 
39 char *name;
40 void *data;
41 void *listp;
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 }
87 
88 
89 void
90 htab_delete(name,listp)
91 
92 char *name;
93 void *listp;
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 }
125 
126 
127 void *
128 htab_get(name,listp)
129 
130 char *name;
131 void *listp;
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 }
158 
159 
160 void
161 htab_free(listp,freedata)
162 
163 void *listp;
164 int freedata;
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 }
183 
184 
185 void
186 htab_print(listp,datafmt)
187 
188 void *listp;
189 char *datafmt;
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 }
205 
206 
207 void *
208 htab_wl(listp)
209 
210 void *listp;
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 }
233 
234 
235 void *
236 htab_list(listp)
237 
238 void *listp;
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 }
265 
266 
267 int
269 
270 void *listp;
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 }
void htab_free(void *listp, int freedata)
Definition: hash.c:161
Definition: cddefs.h:119
void * htab_wl(void *listp)
Definition: hash.c:208
void * htab_get(char *name, void *listp)
Definition: hash.c:128
Definition: library.c:18
void * htab_init()
Definition: hash.c:27
char * copy()
#define COMP(ss, tt)
Definition: hash.c:22
char * tmalloc()
struct wordlist * wl_prev
Definition: cpstd.h:24
void txfree()
#define NULL
Definition: spdefs.h:121
void htab_print(void *listp, char *datafmt)
Definition: hash.c:186
#define HASH(s, n)
Definition: hash.c:20
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
void htab_delete(char *name, void *listp)
Definition: hash.c:90
void htab_add(char *name, void *data, void *listp)
Definition: hash.c:37
void * htab_list(void *listp)
Definition: hash.c:236
#define HTAB_SIZE
Definition: hash.c:11
int htab_empty(void *listp)
Definition: hash.c:268