/[ascend]/trunk/ascend/general/table.c
ViewVC logotype

Contents of /trunk/ascend/general/table.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2018 - (show annotations) (download) (as text)
Wed Apr 29 03:38:10 2009 UTC (15 years ago) by jpye
File MIME type: text/x-csrc
File size: 6852 byte(s)
Fixed compile for new header file locations <ascend/compiler/xxx.h> etc.
1 /*
2 * Table Module
3 * by Kirk A. Abbott
4 * Created December 29, 1994.
5 * Version: $Revision: 1.2 $
6 * Version control file: $RCSfile: table.c,v $
7 * Date last modified: $Date: 1998/06/16 15:47:46 $
8 * Last modified by: $Author: mthomas $
9 *
10 * This file is part of the Ascend Language Interpreter.
11 *
12 * Copyright (C) 1994 Kirk Andre Abbott
13 *
14 * The Ascend Language Interpreter is free software; you can
15 * redistribute it and/or modify it under the terms of the GNU
16 * General Public License as published by the Free Software
17 * Foundation; either version 2 of the License, or (at your option)
18 * any later version.
19 *
20 * The Ascend Language Interpreter is distributed in hope that it
21 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
22 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
23 * See the GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with the program; if not, write to the Free Software
27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139 USA. Check
28 * the file named COPYING.
29 */
30
31 #include <ascend/utilities/ascConfig.h>
32
33 #include <ascend/utilities/ascMalloc.h>
34 #include <ascend/utilities/ascPanic.h>
35 #include "hashpjw.h"
36 #include "table.h"
37
38 struct TableEntry {
39 char *id; /* id string used to hash entry */
40 void *data; /* the actual data */
41 struct TableEntry *next; /* pointer to next entry in linked list */
42 };
43
44
45 struct Table {
46 unsigned long hashsize; /* number of buckets */
47 unsigned long size; /* the current number of entries */
48 struct TableEntry **buckets; /* the actual buckets */
49 struct TableEntry *lastfind; /* a cached pointer into the last thing found */
50 };
51
52
53 struct Table *CreateTable(unsigned long hashsize)
54 {
55 struct Table *result;
56 struct TableEntry **buckets;
57 result = ASC_NEW(struct Table);
58 buckets = ASC_NEW_ARRAY_CLEAR(struct TableEntry *,hashsize);
59 result->buckets = buckets;
60 result->size = 0;
61 result->hashsize = hashsize;
62 result->lastfind = NULL;
63 return result;
64 }
65
66 static void DestroyTableData(void *data, int dispose)
67 {
68 if (dispose)
69 ascfree((char *)data);
70 }
71
72 void DestroyTable(struct Table *table, int dispose)
73 {
74 struct TableEntry *ptr, *next;
75 unsigned long hashsize,c;
76
77 if (table==NULL) return;
78 hashsize = table->hashsize;
79 for (c=0 ; c<hashsize ; c++) {
80 if (table->buckets[c] != NULL) {
81 ptr = table->buckets[c];
82 while(ptr!=NULL) {
83 DestroyTableData(ptr->data, dispose); /* deallocate the data */
84 next = ptr->next;
85 ascfree((char *)ptr->id); /* deallocate the string */
86 ascfree((char *)ptr); /* deallocate the node */
87 table->size--;
88 ptr = next;
89 }
90 table->buckets[c] = NULL;
91 }
92 }
93 ascfree((char *)table->buckets); /* deallocate the table */
94 ascfree((char *)table); /* deallocate the head */
95 }
96
97
98 void AddTableData(struct Table *table, void *data, CONST char *id)
99 {
100 unsigned long c;
101 struct TableEntry *ptr;
102
103 asc_assert((NULL != table) && (NULL != id));
104 c = hashpjw(id,table->hashsize);
105 ptr = table->buckets[c];
106 /* search for name collisions */
107 while (ptr) {
108 if (strcmp(id,ptr->id)==0)
109 return;
110 ptr = ptr->next;
111 }
112 /* add new information node to the head of the list. */
113 ptr = ASC_NEW(struct TableEntry);
114 ptr->id = ASC_NEW_ARRAY(char,strlen(id)+1);
115 strcpy(ptr->id,id); /* we will copy the string */
116 ptr->next = table->buckets[c];
117 ptr->data = data;
118 table->buckets[c] = ptr;
119 table->size++;
120 table->lastfind = ptr;
121 }
122
123 void *LookupTableData(struct Table *table, CONST char *id)
124 {
125 unsigned long c;
126 struct TableEntry *ptr;
127
128 asc_assert((NULL != table) && (NULL != id));
129 c = hashpjw(id,table->hashsize);
130 ptr = table->buckets[c];
131 while (ptr) {
132 if (strcmp(id,ptr->id)==0) {
133 table->lastfind = ptr;
134 return ptr->data;
135 }
136 ptr = ptr->next;
137 }
138 table->lastfind = NULL;
139 return NULL; /* id not found */
140 }
141
142 void *RemoveTableData(struct Table *table, char *id)
143 {
144 unsigned long c;
145 struct TableEntry *ptr, **tmp;
146 void *result;
147
148 asc_assert((NULL != table) && (NULL != id));
149 c = hashpjw(id,table->hashsize);
150 tmp = &table->buckets[c];
151 ptr = table->buckets[c];
152 while (ptr) {
153 if (strcmp(id,ptr->id)==0) {
154 *tmp = ptr->next;
155 result = ptr->data;
156 if (table->lastfind==ptr) table->lastfind = NULL;
157 ascfree((char *)ptr->id); /* deallocate the string */
158 ascfree((char *)ptr); /* deallocate the node */
159 table->size--;
160 return result;
161 }
162 tmp = &ptr->next;
163 ptr = ptr->next;
164 }
165 return NULL; /* node id not found */
166 }
167
168 /*
169 * Check our cached pointer first. If we don't find a match,
170 * then do a Lookup. The lookup will reset the cached pointer.
171 */
172 void TableApplyOne(struct Table *table,
173 TableIteratorOne applyfunc,
174 char *id)
175 {
176 void *data;
177
178 asc_assert((NULL != table) && (NULL != applyfunc) && (NULL != id));
179 if (table->lastfind) {
180 if (strcmp(table->lastfind->id,id)==0) {
181 (*applyfunc)(table->lastfind->data);
182 return;
183 }
184 }
185 data = LookupTableData(table,id);
186 if (data)
187 (*applyfunc)(data);
188 return;
189 }
190
191 void TableApplyAll(struct Table *table, TableIteratorOne applyfunc)
192 {
193 unsigned long c,hashsize;
194 struct TableEntry *ptr;
195
196 asc_assert((NULL != table) && (NULL != applyfunc));
197
198 hashsize = table->hashsize;
199 for (c=0;c<hashsize;c++) {
200 ptr = table->buckets[c];
201 while (ptr) {
202 (*applyfunc)(ptr->data);
203 ptr = ptr->next;
204 }
205 }
206 }
207
208 void TableApplyAllTwo(struct Table *table,
209 TableIteratorTwo applyfunc,
210 void *arg2)
211 {
212 unsigned long c,hashsize;
213 struct TableEntry *ptr;
214
215 asc_assert((NULL != table) && (NULL != applyfunc));
216
217 hashsize = table->hashsize;
218 for (c=0;c<hashsize;c++) {
219 ptr = table->buckets[c];
220 while (ptr) {
221 (*applyfunc)(ptr->data,arg2);
222 ptr = ptr->next;
223 }
224 }
225 }
226
227 void PrintTable(FILE *f, struct Table *table)
228 {
229 unsigned long c,hashsize;
230 struct TableEntry *ptr;
231 unsigned long entrynum = 1;
232
233 asc_assert((NULL != table) && (NULL != f));
234
235 hashsize = table->hashsize;
236 for (c=0;c<hashsize;c++) {
237 ptr = table->buckets[c];
238 while (ptr) {
239 FPRINTF(f,"Entry %lu\tBucket %lu\tId %s\n",
240 entrynum++,c,ptr->id);
241 ptr = ptr->next;
242 }
243 }
244 }
245
246 unsigned long TableSize(struct Table *table)
247 {
248 asc_assert(table!=NULL);
249 return table->size;
250 }
251
252 unsigned long TableHashSize(struct Table *table)
253 {
254 asc_assert(table!=NULL);
255 return table->hashsize;
256 }
257
258 void *TableLastFind(struct Table *table)
259 {
260 asc_assert(table!=NULL);
261 return (NULL == table->lastfind) ?
262 NULL : table->lastfind->data;
263 }
264

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22