|
tesseract 3.04.01
|
00001 /********************************************************************** 00002 * File: elst.c (Formerly elist.c) 00003 * Description: Embedded list handling code which is not in the include file. 00004 * Author: Phil Cheatle 00005 * Created: Fri Jan 04 13:55:49 GMT 1991 00006 * 00007 * (C) Copyright 1991, Hewlett-Packard Ltd. 00008 ** Licensed under the Apache License, Version 2.0 (the "License"); 00009 ** you may not use this file except in compliance with the License. 00010 ** You may obtain a copy of the License at 00011 ** http://www.apache.org/licenses/LICENSE-2.0 00012 ** Unless required by applicable law or agreed to in writing, software 00013 ** distributed under the License is distributed on an "AS IS" BASIS, 00014 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00015 ** See the License for the specific language governing permissions and 00016 ** limitations under the License. 00017 * 00018 **********************************************************************/ 00019 00020 #include <stdlib.h> 00021 #include "elst.h" 00022 00023 /*********************************************************************** 00024 * MEMBER FUNCTIONS OF CLASS: ELIST 00025 * ================================ 00026 **********************************************************************/ 00027 00028 /*********************************************************************** 00029 * ELIST::internal_clear 00030 * 00031 * Used by the destructor and the "clear" member function of derived list 00032 * classes to destroy all the elements on the list. 00033 * The calling function passes a "zapper" function which can be called to 00034 * delete each element of the list, regardless of its derived type. This 00035 * technique permits a generic clear function to destroy elements of 00036 * different derived types correctly, without requiring virtual functions and 00037 * the consequential memory overhead. 00038 **********************************************************************/ 00039 00040 void 00041 ELIST::internal_clear ( //destroy all links 00042 void (*zapper) (ELIST_LINK *)) { 00043 //ptr to zapper functn 00044 ELIST_LINK *ptr; 00045 ELIST_LINK *next; 00046 00047 if (!empty ()) { 00048 ptr = last->next; //set to first 00049 last->next = NULL; //break circle 00050 last = NULL; //set list empty 00051 while (ptr) { 00052 next = ptr->next; 00053 zapper(ptr); 00054 ptr = next; 00055 } 00056 } 00057 } 00058 00059 /*********************************************************************** 00060 * ELIST::assign_to_sublist 00061 * 00062 * The list is set to a sublist of another list. "This" list must be empty 00063 * before this function is invoked. The two iterators passed must refer to 00064 * the same list, different from "this" one. The sublist removed is the 00065 * inclusive list from start_it's current position to end_it's current 00066 * position. If this range passes over the end of the source list then the 00067 * source list has its end set to the previous element of start_it. The 00068 * extracted sublist is unaffected by the end point of the source list, its 00069 * end point is always the end_it position. 00070 **********************************************************************/ 00071 00072 void ELIST::assign_to_sublist( //to this list 00073 ELIST_ITERATOR *start_it, //from list start 00074 ELIST_ITERATOR *end_it) { //from list end 00075 const ERRCODE LIST_NOT_EMPTY = 00076 "Destination list must be empty before extracting a sublist"; 00077 00078 if (!empty ()) 00079 LIST_NOT_EMPTY.error ("ELIST.assign_to_sublist", ABORT, NULL); 00080 00081 last = start_it->extract_sublist (end_it); 00082 } 00083 00084 00085 /*********************************************************************** 00086 * ELIST::length 00087 * 00088 * Return count of elements on list 00089 **********************************************************************/ 00090 00091 inT32 ELIST::length() const { // count elements 00092 ELIST_ITERATOR it(const_cast<ELIST*>(this)); 00093 inT32 count = 0; 00094 00095 for (it.mark_cycle_pt (); !it.cycled_list (); it.forward ()) 00096 count++; 00097 return count; 00098 } 00099 00100 00101 /*********************************************************************** 00102 * ELIST::sort 00103 * 00104 * Sort elements on list 00105 * NB If you don't like the const declarations in the comparator, coerce yours: 00106 * ( int (*)(const void *, const void *) 00107 **********************************************************************/ 00108 00109 void 00110 ELIST::sort ( //sort elements 00111 int comparator ( //comparison routine 00112 const void *, const void *)) { 00113 ELIST_ITERATOR it(this); 00114 inT32 count; 00115 ELIST_LINK **base; //ptr array to sort 00116 ELIST_LINK **current; 00117 inT32 i; 00118 00119 /* Allocate an array of pointers, one per list element */ 00120 count = length (); 00121 base = (ELIST_LINK **) malloc (count * sizeof (ELIST_LINK *)); 00122 00123 /* Extract all elements, putting the pointers in the array */ 00124 current = base; 00125 for (it.mark_cycle_pt (); !it.cycled_list (); it.forward ()) { 00126 *current = it.extract (); 00127 current++; 00128 } 00129 00130 /* Sort the pointer array */ 00131 qsort ((char *) base, count, sizeof (*base), comparator); 00132 00133 /* Rebuild the list from the sorted pointers */ 00134 current = base; 00135 for (i = 0; i < count; i++) { 00136 it.add_to_end (*current); 00137 current++; 00138 } 00139 free(base); 00140 } 00141 00142 // Assuming list has been sorted already, insert new_link to 00143 // keep the list sorted according to the same comparison function. 00144 // Comparison function is the same as used by sort, i.e. uses double 00145 // indirection. Time is O(1) to add to beginning or end. 00146 // Time is linear to add pre-sorted items to an empty list. 00147 // If unique is set to true and comparator() returns 0 (an entry with the 00148 // same information as the one contained in new_link is already in the 00149 // list) - new_link is not added to the list and the function returns the 00150 // pointer to the identical entry that already exists in the list 00151 // (otherwise the function returns new_link). 00152 ELIST_LINK *ELIST::add_sorted_and_find( 00153 int comparator(const void*, const void*), 00154 bool unique, ELIST_LINK* new_link) { 00155 // Check for adding at the end. 00156 if (last == NULL || comparator(&last, &new_link) < 0) { 00157 if (last == NULL) { 00158 new_link->next = new_link; 00159 } else { 00160 new_link->next = last->next; 00161 last->next = new_link; 00162 } 00163 last = new_link; 00164 } else { 00165 // Need to use an iterator. 00166 ELIST_ITERATOR it(this); 00167 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) { 00168 ELIST_LINK* link = it.data(); 00169 int compare = comparator(&link, &new_link); 00170 if (compare > 0) { 00171 break; 00172 } else if (unique && compare == 0) { 00173 return link; 00174 } 00175 } 00176 if (it.cycled_list()) 00177 it.add_to_end(new_link); 00178 else 00179 it.add_before_then_move(new_link); 00180 } 00181 return new_link; 00182 } 00183 00184 /*********************************************************************** 00185 * MEMBER FUNCTIONS OF CLASS: ELIST_ITERATOR 00186 * ========================================= 00187 **********************************************************************/ 00188 00189 /*********************************************************************** 00190 * ELIST_ITERATOR::forward 00191 * 00192 * Move the iterator to the next element of the list. 00193 * REMEMBER: ALL LISTS ARE CIRCULAR. 00194 **********************************************************************/ 00195 00196 ELIST_LINK *ELIST_ITERATOR::forward() { 00197 #ifndef NDEBUG 00198 if (!list) 00199 NO_LIST.error ("ELIST_ITERATOR::forward", ABORT, NULL); 00200 #endif 00201 if (list->empty ()) 00202 return NULL; 00203 00204 if (current) { //not removed so 00205 //set previous 00206 prev = current; 00207 started_cycling = TRUE; 00208 // In case next is deleted by another iterator, get next from current. 00209 current = current->next; 00210 } else { 00211 if (ex_current_was_cycle_pt) 00212 cycle_pt = next; 00213 current = next; 00214 } 00215 next = current->next; 00216 00217 #ifndef NDEBUG 00218 if (!current) 00219 NULL_DATA.error ("ELIST_ITERATOR::forward", ABORT, NULL); 00220 if (!next) 00221 NULL_NEXT.error ("ELIST_ITERATOR::forward", ABORT, 00222 "This is: %p Current is: %p", this, current); 00223 #endif 00224 return current; 00225 } 00226 00227 00228 /*********************************************************************** 00229 * ELIST_ITERATOR::data_relative 00230 * 00231 * Return the data pointer to the element "offset" elements from current. 00232 * "offset" must not be less than -1. 00233 * (This function can't be INLINEd because it contains a loop) 00234 **********************************************************************/ 00235 00236 ELIST_LINK *ELIST_ITERATOR::data_relative( //get data + or - ... 00237 inT8 offset) { //offset from current 00238 ELIST_LINK *ptr; 00239 00240 #ifndef NDEBUG 00241 if (!list) 00242 NO_LIST.error ("ELIST_ITERATOR::data_relative", ABORT, NULL); 00243 if (list->empty ()) 00244 EMPTY_LIST.error ("ELIST_ITERATOR::data_relative", ABORT, NULL); 00245 if (offset < -1) 00246 BAD_PARAMETER.error ("ELIST_ITERATOR::data_relative", ABORT, 00247 "offset < -l"); 00248 #endif 00249 00250 if (offset == -1) 00251 ptr = prev; 00252 else 00253 for (ptr = current ? current : prev; offset-- > 0; ptr = ptr->next); 00254 00255 #ifndef NDEBUG 00256 if (!ptr) 00257 NULL_DATA.error ("ELIST_ITERATOR::data_relative", ABORT, NULL); 00258 #endif 00259 00260 return ptr; 00261 } 00262 00263 00264 /*********************************************************************** 00265 * ELIST_ITERATOR::move_to_last() 00266 * 00267 * Move current so that it is set to the end of the list. 00268 * Return data just in case anyone wants it. 00269 * (This function can't be INLINEd because it contains a loop) 00270 **********************************************************************/ 00271 00272 ELIST_LINK *ELIST_ITERATOR::move_to_last() { 00273 #ifndef NDEBUG 00274 if (!list) 00275 NO_LIST.error ("ELIST_ITERATOR::move_to_last", ABORT, NULL); 00276 #endif 00277 00278 while (current != list->last) 00279 forward(); 00280 00281 return current; 00282 } 00283 00284 00285 /*********************************************************************** 00286 * ELIST_ITERATOR::exchange() 00287 * 00288 * Given another iterator, whose current element is a different element on 00289 * the same list list OR an element of another list, exchange the two current 00290 * elements. On return, each iterator points to the element which was the 00291 * other iterators current on entry. 00292 * (This function hasn't been in-lined because its a bit big!) 00293 **********************************************************************/ 00294 00295 void ELIST_ITERATOR::exchange( //positions of 2 links 00296 ELIST_ITERATOR *other_it) { //other iterator 00297 const ERRCODE DONT_EXCHANGE_DELETED = 00298 "Can't exchange deleted elements of lists"; 00299 00300 ELIST_LINK *old_current; 00301 00302 #ifndef NDEBUG 00303 if (!list) 00304 NO_LIST.error ("ELIST_ITERATOR::exchange", ABORT, NULL); 00305 if (!other_it) 00306 BAD_PARAMETER.error ("ELIST_ITERATOR::exchange", ABORT, "other_it NULL"); 00307 if (!(other_it->list)) 00308 NO_LIST.error ("ELIST_ITERATOR::exchange", ABORT, "other_it"); 00309 #endif 00310 00311 /* Do nothing if either list is empty or if both iterators reference the same 00312 link */ 00313 00314 if ((list->empty ()) || 00315 (other_it->list->empty ()) || (current == other_it->current)) 00316 return; 00317 00318 /* Error if either current element is deleted */ 00319 00320 if (!current || !other_it->current) 00321 DONT_EXCHANGE_DELETED.error ("ELIST_ITERATOR.exchange", ABORT, NULL); 00322 00323 /* Now handle the 4 cases: doubleton list; non-doubleton adjacent elements 00324 (other before this); non-doubleton adjacent elements (this before other); 00325 non-adjacent elements. */ 00326 00327 //adjacent links 00328 if ((next == other_it->current) || 00329 (other_it->next == current)) { 00330 //doubleton list 00331 if ((next == other_it->current) && 00332 (other_it->next == current)) { 00333 prev = next = current; 00334 other_it->prev = other_it->next = other_it->current; 00335 } 00336 else { //non-doubleton with 00337 //adjacent links 00338 //other before this 00339 if (other_it->next == current) { 00340 other_it->prev->next = current; 00341 other_it->current->next = next; 00342 current->next = other_it->current; 00343 other_it->next = other_it->current; 00344 prev = current; 00345 } 00346 else { //this before other 00347 prev->next = other_it->current; 00348 current->next = other_it->next; 00349 other_it->current->next = current; 00350 next = current; 00351 other_it->prev = other_it->current; 00352 } 00353 } 00354 } 00355 else { //no overlap 00356 prev->next = other_it->current; 00357 current->next = other_it->next; 00358 other_it->prev->next = current; 00359 other_it->current->next = next; 00360 } 00361 00362 /* update end of list pointer when necessary (remember that the 2 iterators 00363 may iterate over different lists!) */ 00364 00365 if (list->last == current) 00366 list->last = other_it->current; 00367 if (other_it->list->last == other_it->current) 00368 other_it->list->last = current; 00369 00370 if (current == cycle_pt) 00371 cycle_pt = other_it->cycle_pt; 00372 if (other_it->current == other_it->cycle_pt) 00373 other_it->cycle_pt = cycle_pt; 00374 00375 /* The actual exchange - in all cases*/ 00376 00377 old_current = current; 00378 current = other_it->current; 00379 other_it->current = old_current; 00380 } 00381 00382 00383 /*********************************************************************** 00384 * ELIST_ITERATOR::extract_sublist() 00385 * 00386 * This is a private member, used only by ELIST::assign_to_sublist. 00387 * Given another iterator for the same list, extract the links from THIS to 00388 * OTHER inclusive, link them into a new circular list, and return a 00389 * pointer to the last element. 00390 * (Can't inline this function because it contains a loop) 00391 **********************************************************************/ 00392 00393 ELIST_LINK *ELIST_ITERATOR::extract_sublist( //from this current 00394 ELIST_ITERATOR *other_it) { //to other current 00395 #ifndef NDEBUG 00396 const ERRCODE BAD_EXTRACTION_PTS = 00397 "Can't extract sublist from points on different lists"; 00398 const ERRCODE DONT_EXTRACT_DELETED = 00399 "Can't extract a sublist marked by deleted points"; 00400 #endif 00401 const ERRCODE BAD_SUBLIST = "Can't find sublist end point in original list"; 00402 00403 ELIST_ITERATOR temp_it = *this; 00404 ELIST_LINK *end_of_new_list; 00405 00406 #ifndef NDEBUG 00407 if (!other_it) 00408 BAD_PARAMETER.error ("ELIST_ITERATOR::extract_sublist", ABORT, 00409 "other_it NULL"); 00410 if (!list) 00411 NO_LIST.error ("ELIST_ITERATOR::extract_sublist", ABORT, NULL); 00412 if (list != other_it->list) 00413 BAD_EXTRACTION_PTS.error ("ELIST_ITERATOR.extract_sublist", ABORT, NULL); 00414 if (list->empty ()) 00415 EMPTY_LIST.error ("ELIST_ITERATOR::extract_sublist", ABORT, NULL); 00416 00417 if (!current || !other_it->current) 00418 DONT_EXTRACT_DELETED.error ("ELIST_ITERATOR.extract_sublist", ABORT, 00419 NULL); 00420 #endif 00421 00422 ex_current_was_last = other_it->ex_current_was_last = FALSE; 00423 ex_current_was_cycle_pt = FALSE; 00424 other_it->ex_current_was_cycle_pt = FALSE; 00425 00426 temp_it.mark_cycle_pt (); 00427 do { //walk sublist 00428 if (temp_it.cycled_list ()) //can't find end pt 00429 BAD_SUBLIST.error ("ELIST_ITERATOR.extract_sublist", ABORT, NULL); 00430 00431 if (temp_it.at_last ()) { 00432 list->last = prev; 00433 ex_current_was_last = other_it->ex_current_was_last = TRUE; 00434 } 00435 00436 if (temp_it.current == cycle_pt) 00437 ex_current_was_cycle_pt = TRUE; 00438 00439 if (temp_it.current == other_it->cycle_pt) 00440 other_it->ex_current_was_cycle_pt = TRUE; 00441 00442 temp_it.forward (); 00443 } 00444 while (temp_it.prev != other_it->current); 00445 00446 //circularise sublist 00447 other_it->current->next = current; 00448 end_of_new_list = other_it->current; 00449 00450 //sublist = whole list 00451 if (prev == other_it->current) { 00452 list->last = NULL; 00453 prev = current = next = NULL; 00454 other_it->prev = other_it->current = other_it->next = NULL; 00455 } 00456 else { 00457 prev->next = other_it->next; 00458 current = other_it->current = NULL; 00459 next = other_it->next; 00460 other_it->prev = prev; 00461 } 00462 return end_of_new_list; 00463 }