00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "mit-copyright.h"
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #include "port_dictionary.h"
00041 #include "new_string.h"
00042
00043 #include "main.h"
00044
00045 #ifndef NULL
00046 #define NULL 0
00047 #endif
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 port_dictionary
00062 port_dictionary_Create(size)
00063 int size;
00064 {
00065 int i;
00066 port_dictionary result;
00067
00068 result = (port_dictionary) malloc(sizeof(struct _port_dictionary));
00069 result->size = size;
00070 result->slots = (port_dictionary_binding **) malloc(
00071 size * sizeof(port_dictionary_binding *));
00072
00073 for (i = 0; i < size; i++)
00074 result->slots[i] = NULL;
00075
00076 return (result);
00077 }
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090 void
00091 port_dictionary_Destroy(d)
00092 port_dictionary d;
00093 {
00094 int i;
00095 port_dictionary_binding *binding_ptr, *new_binding_ptr;
00096
00097 for (i = 0; i < d->size; i++) {
00098 binding_ptr = d->slots[i];
00099 while (binding_ptr) {
00100 new_binding_ptr = binding_ptr->next;
00101 free(binding_ptr->key);
00102 free(binding_ptr);
00103 binding_ptr = new_binding_ptr;
00104 }
00105 }
00106 free(d->slots);
00107 free(d);
00108 }
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121 void
00122 port_dictionary_Enumerate(d, proc)
00123 port_dictionary d;
00124 void (*proc) ( );
00125 {
00126 int i;
00127 port_dictionary_binding *binding_ptr;
00128
00129 for (i = 0; i < d->size; i++) {
00130 binding_ptr = d->slots[i];
00131 while (binding_ptr) {
00132 proc(binding_ptr);
00133 binding_ptr = binding_ptr->next;
00134 }
00135 }
00136 }
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147 static unsigned int
00148 dictionary__hash(s)
00149 char *s;
00150 {
00151 unsigned int result = 0;
00152
00153 if (!s)
00154 return (result);
00155
00156 while (s[0]) {
00157 result <<= 1;
00158 result += s[0];
00159 s++;
00160 }
00161
00162 return (result);
00163 }
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173 port_dictionary_binding *
00174 port_dictionary_Lookup(d, key)
00175 port_dictionary d;
00176 char *key;
00177 {
00178 port_dictionary_binding *binding_ptr;
00179
00180 binding_ptr = d->slots[dictionary__hash(key) % (d->size)];
00181 while (binding_ptr) {
00182 if (string_Eq(key, binding_ptr->key))
00183 return (binding_ptr);
00184 binding_ptr = binding_ptr->next;
00185 }
00186
00187 return (NULL);
00188 }
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206 port_dictionary_binding *
00207 port_dictionary_Define(d, key, already_existed)
00208 port_dictionary d;
00209 char *key;
00210 int *already_existed;
00211 {
00212 port_dictionary_binding **ptr_to_the_slot, *binding_ptr;
00213
00214 ptr_to_the_slot = &(d->slots[dictionary__hash(key) % (d->size)]);
00215
00216 binding_ptr = *ptr_to_the_slot;
00217 while (binding_ptr) {
00218 if (string_Eq(binding_ptr->key, key)) {
00219 if (already_existed)
00220 *already_existed = 1;
00221 return (binding_ptr);
00222 }
00223 binding_ptr = binding_ptr->next;
00224 }
00225
00226 if (already_existed)
00227 *already_existed = 0;
00228 binding_ptr = (port_dictionary_binding *) malloc(
00229 sizeof(port_dictionary_binding));
00230 binding_ptr->next = *ptr_to_the_slot;
00231 binding_ptr->key = string_Copy(key);
00232 *ptr_to_the_slot = binding_ptr;
00233 return (binding_ptr);
00234 }
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246 void
00247 port_dictionary_Delete(d, b)
00248 port_dictionary d;
00249 port_dictionary_binding *b;
00250 {
00251 port_dictionary_binding **ptr_to_binding_ptr;
00252
00253 ptr_to_binding_ptr = &(d->slots[dictionary__hash(b->key) % (d->size)]);
00254
00255 while (*ptr_to_binding_ptr != b)
00256 ptr_to_binding_ptr = &((*ptr_to_binding_ptr)->next);
00257
00258 *ptr_to_binding_ptr = b->next;
00259 free(b->key);
00260 free(b);
00261 }