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