00001 00002 00003 00004 00005 00006 00007 00008 00009 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 00041 00042 00043 00044 00045 00046 00047 00048 00049 00050 00051 00052 00053 00054 00055
00056
00057 #include <stdio.h>
00058 #include <stdlib.h>
00059 #include "list.h"
00060 #include "_gocr.h"
00061
00062 void list_init( List *l ) {
00063 if ( !l )
00064 return;
00065
00066 l->header = l->tail = NULL;
00067 l->current = NULL;
00068 l->fix = NULL;
00069 l->level = -1;
00070 l->n = 0;
00071 }
00072
00073
00074 int list_app( List *l, void *data ) {
00075 Element *e;
00076
00077 if ( !l || !data )
00078 return 1;
00079 if ( !(e = (Element *)malloc(sizeof(Element))) )
00080 return 1;
00081
00082 e->data = data;
00083 if ( !l->header ) {
00084 l->header = l->tail = e;
00085 l->n = 1;
00086 e->previous = e->next = NULL;
00087 return 0;
00088 }
00089
00090 l->tail->next = e;
00091 e->previous = l->tail;
00092 l->tail = e;
00093 e->next = NULL;
00094 l->n++;
00095 return 0;
00096 }
00097
00098 00099
00100 int list_ins( List *l, void *data_after, void *data) {
00101 Element *e, *after_element;
00102
00103
00104 if ( !l || !data )
00105 return 1;
00106
00107 if ( !data_after || !l->header )
00108 return list_app(l, data);
00109
00110
00111 if( !(e = (Element *)malloc(sizeof(Element))) )
00112 return 1;
00113 e->data = data;
00114
00115
00116 if ( !(after_element = list_element_from_data(l, data_after)) )
00117 return 1;
00118
00119 e->next = after_element;
00120 e->previous = after_element->previous;
00121 if ( after_element->previous )
00122 after_element->previous->next = e;
00123 else
00124 l->header = e;
00125 after_element->previous = e;
00126 l->n++;
00127
00128 return 0;
00129 }
00130
00131
00132 Element *list_element_from_data( List *l, void *data ) {
00133 Element *temp;
00134
00135 if ( !l || !data )
00136 return NULL;
00137
00138 if ( !(temp = l->header) )
00139 return NULL;
00140
00141 while ( temp->data != data ) {
00142 temp = temp->next;
00143 if ( !temp )
00144 return NULL;
00145 }
00146 return temp;
00147 }
00148
00149 00150 00151 00152
00153 int list_del( List *l, void *data ) {
00154 Element *temp;
00155 int i;
00156
00157
00158 if ( !(temp = list_element_from_data(l, data)) )
00159 return 1;
00160
00161
00162 for ( i = l->level; i >= 0; i-- ) {
00163 if ( l->current[i] == temp ) {
00164 l->fix[i] = temp;
00165 }
00166 }
00167
00168
00169 if ( temp == l->header ) {
00170 l->header = temp->next;
00171 l->header->previous = NULL;
00172 }
00173 else
00174 temp->previous->next = temp->next;
00175
00176
00177 if ( temp == l->tail ) {
00178 l->tail = temp->previous;
00179 l->tail->next = NULL;
00180 }
00181 else
00182 temp->next->previous = temp->previous;
00183
00184
00185 l->n--;
00186 return 0;
00187 }
00188
00189
00190 void list_free( List *l ) {
00191 Element *temp, *temp2;
00192
00193 if ( !l )
00194 return;
00195 if ( !l->header )
00196 return;
00197
00198 if ( l->current ) {
00199 free(l->current);
00200 }
00201 l->current = NULL;
00202
00203 if ( l->fix ) {
00204 free(l->fix);
00205 }
00206 l->fix = NULL;
00207
00208 temp = l->header;
00209 while ( temp ) {
00210 temp2 = temp->next;
00211 free(temp);
00212 temp = temp2;
00213 }
00214 l->header = l->tail = NULL;
00215 }
00216
00217
00218 int list_higher_level( List *l ) {
00219 Element **newcur;
00220 Element **newfix;
00221
00222 if ( !l || !l->header )
00223 return 1;
00224
00225 newcur = (Element **)realloc(l->current, (l->level+1)*sizeof(Element *));
00226 newfix = (Element **)realloc(l->fix , (l->level+1)*sizeof(Element *));
00227 if ( !newcur || !newfix ) {
00228 00229
00230 return 1;
00231 }
00232 l->current = newcur;
00233 l->fix = newfix;
00234 l->level++;
00235 l->current[l->level] = l->header;
00236 l->fix[l->level] = NULL;
00237 00238
00239 return 0;
00240 }
00241
00242 void list_lower_level( List *l ) {
00243 if ( !l )
00244 return;
00245
00246 l->current = (Element **)realloc(l->current, l->level*sizeof(Element *));
00247 l->fix = (Element **)realloc(l->fix , l->level*sizeof(Element *));
00248 l->level--;
00249 00250
00251 }
00252
00253
00254 void *list_next( List *l, void *data ) {
00255 Element *temp;
00256
00257 if ( !l || !(temp = list_element_from_data(l, data)) )
00258 return NULL;
00259 if( !temp->next ) return NULL;
00260 return (temp->next->data);
00261 }
00262
00263
00264 void *list_prev( List *l, void *data ) {
00265 Element *temp;
00266
00267 if ( !l || !(temp = list_element_from_data(l, data)) )
00268 return NULL;
00269 if( !temp->previous ) return NULL;
00270 return (temp->previous->data);
00271 }
00272
00273 00274 00275 00276 00277 00278
00279 void list_sort( List *l, int (*compare)(const void *, const void *) ) {
00280 Element *temp, *prev;
00281 int i;
00282
00283 if ( !l )
00284 return;
00285
00286 for (i = 0; i < l->n; i++ ) {
00287 for ( temp = l->header->next; temp != NULL; temp = temp->next ) {
00288 if ( compare((const void *)temp->previous->data, (const void *)temp->data) > 0 ) {
00289
00290
00291 prev = temp->previous;
00292
00293 if ( prev->previous )
00294 prev->previous->next = temp;
00295 else
00296 l->header = temp;
00297
00298 temp->previous = prev->previous;
00299 prev->previous = temp;
00300 prev->next = temp->next;
00301 if ( temp->next )
00302 temp->next->previous = prev;
00303 else
00304 l->tail = prev;
00305 temp->next = prev;
00306
00307
00308 temp = prev;
00309
00310 #ifdef SLOWER_BUT_KEEP_BY_NOW
00311
00312 void *data;
00313
00314 data = temp->data;
00315 prev = temp->previous;
00316 list_del(l, data);
00317 list_ins(l, prev->data, data);
00318 temp = prev;
00319 #endif
00320 }
00321 }
00322 }
00323
00324
00325 }