00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <GKlib.h>
00012
00013
00014
00015
00016 gk_HTable_t *HTable_Create(int nelements)
00017 {
00018 gk_HTable_t *htable;
00019
00020 htable = gk_malloc(sizeof(gk_HTable_t), "HTable_Create: htable");
00021 htable->harray = gk_ikvmalloc(nelements, "HTable_Create: harray");
00022 htable->nelements = nelements;
00023
00024 HTable_Reset(htable);
00025
00026 return htable;
00027 }
00028
00029
00030
00031
00032
00033 void HTable_Reset(gk_HTable_t *htable)
00034 {
00035 int i;
00036
00037 for (i=0; i<htable->nelements; i++)
00038 htable->harray[i].key = HTABLE_EMPTY;
00039 htable->htsize = 0;
00040
00041 }
00042
00043
00044
00045
00046 void HTable_Resize(gk_HTable_t *htable, int nelements)
00047 {
00048 int i, old_nelements;
00049 gk_ikv_t *old_harray;
00050
00051 old_nelements = htable->nelements;
00052 old_harray = htable->harray;
00053
00054
00055 htable->nelements = nelements;
00056 htable->htsize = 0;
00057 htable->harray = gk_ikvmalloc(nelements, "HTable_Resize: harray");
00058 for (i=0; i<nelements; i++)
00059 htable->harray[i].key = HTABLE_EMPTY;
00060
00061
00062 for (i=0; i<old_nelements; i++)
00063 if (old_harray[i].key != HTABLE_EMPTY)
00064 HTable_Insert(htable, old_harray[i].key, old_harray[i].val);
00065
00066
00067 gk_free((void **)&old_harray, LTERM);
00068 }
00069
00070
00071
00072
00073
00074 void HTable_Insert(gk_HTable_t *htable, int key, int val)
00075 {
00076 int i, first;
00077
00078 if (htable->htsize > htable->nelements/2)
00079 HTable_Resize(htable, 2*htable->nelements);
00080
00081 first = HTable_HFunction(htable->nelements, key);
00082
00083 for (i=first; i<htable->nelements; i++) {
00084 if (htable->harray[i].key == HTABLE_EMPTY || htable->harray[i].key == HTABLE_DELETED) {
00085 htable->harray[i].key = key;
00086 htable->harray[i].val = val;
00087 htable->htsize++;
00088 return;
00089 }
00090 }
00091
00092 for (i=0; i<first; i++) {
00093 if (htable->harray[i].key == HTABLE_EMPTY || htable->harray[i].key == HTABLE_DELETED) {
00094 htable->harray[i].key = key;
00095 htable->harray[i].val = val;
00096 htable->htsize++;
00097 return;
00098 }
00099 }
00100
00101 }
00102
00103
00104
00105
00106
00107 void HTable_Delete(gk_HTable_t *htable, int key)
00108 {
00109 int i, first;
00110
00111 first = HTable_HFunction(htable->nelements, key);
00112
00113 for (i=first; i<htable->nelements; i++) {
00114 if (htable->harray[i].key == key) {
00115 htable->harray[i].key = HTABLE_DELETED;
00116 htable->htsize--;
00117 return;
00118 }
00119 }
00120
00121 for (i=0; i<first; i++) {
00122 if (htable->harray[i].key == key) {
00123 htable->harray[i].key = HTABLE_DELETED;
00124 htable->htsize--;
00125 return;
00126 }
00127 }
00128
00129 }
00130
00131
00132
00133
00134
00135 int HTable_Search(gk_HTable_t *htable, int key)
00136 {
00137 int i, first;
00138
00139 first = HTable_HFunction(htable->nelements, key);
00140
00141 for (i=first; i<htable->nelements; i++) {
00142 if (htable->harray[i].key == key)
00143 return htable->harray[i].val;
00144 else if (htable->harray[i].key == HTABLE_EMPTY)
00145 return -1;
00146 }
00147
00148 for (i=0; i<first; i++) {
00149 if (htable->harray[i].key == key)
00150 return htable->harray[i].val;
00151 else if (htable->harray[i].key == HTABLE_EMPTY)
00152 return -1;
00153 }
00154
00155 return -1;
00156 }
00157
00158
00159
00160
00161
00162 int HTable_GetNext(gk_HTable_t *htable, int key, int *r_val, int type)
00163 {
00164 int i;
00165 static int first, last;
00166
00167 if (type == HTABLE_FIRST)
00168 first = last = HTable_HFunction(htable->nelements, key);
00169
00170 if (first > last) {
00171 for (i=first; i<htable->nelements; i++) {
00172 if (htable->harray[i].key == key) {
00173 *r_val = htable->harray[i].val;
00174 first = i+1;
00175 return 1;
00176 }
00177 else if (htable->harray[i].key == HTABLE_EMPTY)
00178 return -1;
00179 }
00180 first = 0;
00181 }
00182
00183 for (i=first; i<last; i++) {
00184 if (htable->harray[i].key == key) {
00185 *r_val = htable->harray[i].val;
00186 first = i+1;
00187 return 1;
00188 }
00189 else if (htable->harray[i].key == HTABLE_EMPTY)
00190 return -1;
00191 }
00192
00193 return -1;
00194 }
00195
00196
00197
00198
00199
00200 int HTable_SearchAndDelete(gk_HTable_t *htable, int key)
00201 {
00202 int i, first;
00203
00204 first = HTable_HFunction(htable->nelements, key);
00205
00206 for (i=first; i<htable->nelements; i++) {
00207 if (htable->harray[i].key == key) {
00208 htable->harray[i].key = HTABLE_DELETED;
00209 htable->htsize--;
00210 return htable->harray[i].val;
00211 }
00212 else if (htable->harray[i].key == HTABLE_EMPTY)
00213 gk_errexit(SIGERR, "HTable_SearchAndDelete: Failed to find the key!\n");
00214 }
00215
00216 for (i=0; i<first; i++) {
00217 if (htable->harray[i].key == key) {
00218 htable->harray[i].key = HTABLE_DELETED;
00219 htable->htsize--;
00220 return htable->harray[i].val;
00221 }
00222 else if (htable->harray[i].key == HTABLE_EMPTY)
00223 gk_errexit(SIGERR, "HTable_SearchAndDelete: Failed to find the key!\n");
00224 }
00225
00226 return -1;
00227
00228 }
00229
00230
00231
00232
00233
00234
00235 void HTable_Destroy(gk_HTable_t *htable)
00236 {
00237 gk_free((void **)&htable->harray, &htable, LTERM);
00238 }
00239
00240
00241
00242
00243
00244 int HTable_HFunction(int nelements, int key)
00245 {
00246 return (int)(key%nelements);
00247 }