tesseract  3.03
/usr/local/google/home/jbreiden/tesseract-ocr-read-only/ccutil/elst2.cpp
Go to the documentation of this file.
00001 /**********************************************************************
00002  * File:        elst2.c  (Formerly elist2.c)
00003  * Description: Doubly linked embedded list code not in the include file.
00004  * Author:      Phil Cheatle
00005  * Created:     Wed Jan 23 11:04:47 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 "host.h"
00022 #include "elst2.h"
00023 
00024 /***********************************************************************
00025  *  MEMBER FUNCTIONS OF CLASS: ELIST2
00026  *  =================================
00027  **********************************************************************/
00028 
00029 /***********************************************************************
00030  *                                                      ELIST2::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 ELIST2::internal_clear (         //destroy all links
00043 void (*zapper) (ELIST2_LINK *)) {
00044                                  //ptr to zapper functn
00045   ELIST2_LINK *ptr;
00046   ELIST2_LINK *next;
00047 
00048   #ifndef NDEBUG
00049   if (!this)
00050     NULL_OBJECT.error ("ELIST2::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  *                                                      ELIST2::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 ELIST2::assign_to_sublist(                            //to this list
00079                                ELIST2_ITERATOR *start_it,  //from list start
00080                                ELIST2_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 ("ELIST2::assign_to_sublist", ABORT, NULL);
00087   #endif
00088 
00089   if (!empty ())
00090     LIST_NOT_EMPTY.error ("ELIST2.assign_to_sublist", ABORT, NULL);
00091 
00092   last = start_it->extract_sublist (end_it);
00093 }
00094 
00095 
00096 /***********************************************************************
00097  *                                                      ELIST2::length
00098  *
00099  *  Return count of elements on list
00100  **********************************************************************/
00101 
00102 inT32 ELIST2::length() const {  // count elements
00103   ELIST2_ITERATOR it(const_cast<ELIST2*>(this));
00104   inT32 count = 0;
00105 
00106   #ifndef NDEBUG
00107   if (!this)
00108     NULL_OBJECT.error ("ELIST2::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  *                                                      ELIST2::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 ELIST2::sort (                   //sort elements
00127 int comparator (                 //comparison routine
00128 const void *, const void *)) {
00129   ELIST2_ITERATOR it(this);
00130   inT32 count;
00131   ELIST2_LINK **base;            //ptr array to sort
00132   ELIST2_LINK **current;
00133   inT32 i;
00134 
00135   #ifndef NDEBUG
00136   if (!this)
00137     NULL_OBJECT.error ("ELIST2::sort", ABORT, NULL);
00138   #endif
00139 
00140   /* Allocate an array of pointers, one per list element */
00141   count = length ();
00142   base = (ELIST2_LINK **) malloc (count * sizeof (ELIST2_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 void ELIST2::add_sorted(int comparator(const void*, const void*),
00169                         ELIST2_LINK* new_link) {
00170   // Check for adding at the end.
00171   if (last == NULL || comparator(&last, &new_link) < 0) {
00172     if (last == NULL) {
00173       new_link->next = new_link;
00174       new_link->prev = new_link;
00175     } else {
00176       new_link->next = last->next;
00177       new_link->prev = last;
00178       last->next = new_link;
00179       new_link->next->prev = new_link;
00180     }
00181     last = new_link;
00182   } else {
00183     // Need to use an iterator.
00184     ELIST2_ITERATOR it(this);
00185     for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
00186       ELIST2_LINK* link = it.data();
00187       if (comparator(&link, &new_link) > 0)
00188         break;
00189     }
00190     if (it.cycled_list())
00191       it.add_to_end(new_link);
00192     else
00193       it.add_before_then_move(new_link);
00194   }
00195 }
00196 
00197 /***********************************************************************
00198  *  MEMBER FUNCTIONS OF CLASS: ELIST2_ITERATOR
00199  *  ==========================================
00200  **********************************************************************/
00201 
00202 /***********************************************************************
00203  *                                                      ELIST2_ITERATOR::forward
00204  *
00205  *  Move the iterator to the next element of the list.
00206  *  REMEMBER: ALL LISTS ARE CIRCULAR.
00207  **********************************************************************/
00208 
00209 ELIST2_LINK *ELIST2_ITERATOR::forward() {
00210   #ifndef NDEBUG
00211   if (!this)
00212     NULL_OBJECT.error ("ELIST2_ITERATOR::forward", ABORT, NULL);
00213   if (!list)
00214     NO_LIST.error ("ELIST2_ITERATOR::forward", ABORT, NULL);
00215   #endif
00216   if (list->empty ())
00217     return NULL;
00218 
00219   if (current) {                 //not removed so
00220                                  //set previous
00221     prev = current;
00222     started_cycling = TRUE;
00223     // In case next is deleted by another iterator, get it from the current.
00224     current = current->next;
00225   }
00226   else {
00227     if (ex_current_was_cycle_pt)
00228       cycle_pt = next;
00229     current = next;
00230   }
00231   next = current->next;
00232 
00233   #ifndef NDEBUG
00234   if (!current)
00235     NULL_DATA.error ("ELIST2_ITERATOR::forward", ABORT, NULL);
00236   if (!next)
00237     NULL_NEXT.error ("ELIST2_ITERATOR::forward", ABORT,
00238                      "This is: %p  Current is: %p", this, current);
00239   #endif
00240   return current;
00241 }
00242 
00243 
00244 /***********************************************************************
00245  *                                                      ELIST2_ITERATOR::backward
00246  *
00247  *  Move the iterator to the previous element of the list.
00248  *  REMEMBER: ALL LISTS ARE CIRCULAR.
00249  **********************************************************************/
00250 
00251 ELIST2_LINK *ELIST2_ITERATOR::backward() {
00252   #ifndef NDEBUG
00253   if (!this)
00254     NULL_OBJECT.error ("ELIST2_ITERATOR::backward", ABORT, NULL);
00255   if (!list)
00256     NO_LIST.error ("ELIST2_ITERATOR::backward", ABORT, NULL);
00257   #endif
00258   if (list->empty ())
00259     return NULL;
00260 
00261   if (current) {                 //not removed so
00262                                  //set previous
00263     next = current;
00264     started_cycling = TRUE;
00265     // In case prev is deleted by another iterator, get it from current.
00266     current = current->prev;
00267   } else {
00268     if (ex_current_was_cycle_pt)
00269       cycle_pt = prev;
00270     current = prev;
00271   }
00272   prev = current->prev;
00273 
00274   #ifndef NDEBUG
00275   if (!current)
00276     NULL_DATA.error ("ELIST2_ITERATOR::backward", ABORT, NULL);
00277   if (!prev)
00278     NULL_PREV.error ("ELIST2_ITERATOR::backward", ABORT,
00279       "This is: %p  Current is: %p", this, current);
00280   #endif
00281   return current;
00282 }
00283 
00284 
00285 /***********************************************************************
00286  *                                                      ELIST2_ITERATOR::data_relative
00287  *
00288  *  Return the data pointer to the element "offset" elements from current.
00289  *  (This function can't be INLINEd because it contains a loop)
00290  **********************************************************************/
00291 
00292 ELIST2_LINK *ELIST2_ITERATOR::data_relative(                //get data + or - ..
00293                                             inT8 offset) {  //offset from current
00294   ELIST2_LINK *ptr;
00295 
00296   #ifndef NDEBUG
00297   if (!this)
00298     NULL_OBJECT.error ("ELIST2_ITERATOR::data_relative", ABORT, NULL);
00299   if (!list)
00300     NO_LIST.error ("ELIST2_ITERATOR::data_relative", ABORT, NULL);
00301   if (list->empty ())
00302     EMPTY_LIST.error ("ELIST2_ITERATOR::data_relative", ABORT, NULL);
00303   #endif
00304 
00305   if (offset < 0)
00306     for (ptr = current ? current : next; offset++ < 0; ptr = ptr->prev);
00307   else
00308     for (ptr = current ? current : prev; offset-- > 0; ptr = ptr->next);
00309 
00310   #ifndef NDEBUG
00311   if (!ptr)
00312     NULL_DATA.error ("ELIST2_ITERATOR::data_relative", ABORT, NULL);
00313   #endif
00314 
00315   return ptr;
00316 }
00317 
00318 
00319 /***********************************************************************
00320  *                                                      ELIST2_ITERATOR::exchange()
00321  *
00322  *  Given another iterator, whose current element is a different element on
00323  *  the same list list OR an element of another list, exchange the two current
00324  *  elements.  On return, each iterator points to the element which was the
00325  *  other iterators current on entry.
00326  *  (This function hasn't been in-lined because its a bit big!)
00327  **********************************************************************/
00328 
00329 void ELIST2_ITERATOR::exchange(                              //positions of 2 links
00330                                ELIST2_ITERATOR *other_it) {  //other iterator
00331   const ERRCODE DONT_EXCHANGE_DELETED =
00332     "Can't exchange deleted elements of lists";
00333 
00334   ELIST2_LINK *old_current;
00335 
00336   #ifndef NDEBUG
00337   if (!this)
00338     NULL_OBJECT.error ("ELIST2_ITERATOR::exchange", ABORT, NULL);
00339   if (!list)
00340     NO_LIST.error ("ELIST2_ITERATOR::exchange", ABORT, NULL);
00341   if (!other_it)
00342     BAD_PARAMETER.error ("ELIST2_ITERATOR::exchange", ABORT, "other_it NULL");
00343   if (!(other_it->list))
00344     NO_LIST.error ("ELIST2_ITERATOR::exchange", ABORT, "other_it");
00345   #endif
00346 
00347   /* Do nothing if either list is empty or if both iterators reference the same
00348   link */
00349 
00350   if ((list->empty ()) ||
00351     (other_it->list->empty ()) || (current == other_it->current))
00352     return;
00353 
00354   /* Error if either current element is deleted */
00355 
00356   if (!current || !other_it->current)
00357     DONT_EXCHANGE_DELETED.error ("ELIST2_ITERATOR.exchange", ABORT, NULL);
00358 
00359   /* Now handle the 4 cases: doubleton list; non-doubleton adjacent elements
00360   (other before this); non-doubleton adjacent elements (this before other);
00361   non-adjacent elements. */
00362 
00363                                  //adjacent links
00364   if ((next == other_it->current) ||
00365   (other_it->next == current)) {
00366                                  //doubleton list
00367     if ((next == other_it->current) &&
00368     (other_it->next == current)) {
00369       prev = next = current;
00370       other_it->prev = other_it->next = other_it->current;
00371     }
00372     else {                       //non-doubleton with
00373                                  //adjacent links
00374                                  //other before this
00375       if (other_it->next == current) {
00376         other_it->prev->next = current;
00377         other_it->current->next = next;
00378         other_it->current->prev = current;
00379         current->next = other_it->current;
00380         current->prev = other_it->prev;
00381         next->prev = other_it->current;
00382 
00383         other_it->next = other_it->current;
00384         prev = current;
00385       }
00386       else {                     //this before other
00387         prev->next = other_it->current;
00388         current->next = other_it->next;
00389         current->prev = other_it->current;
00390         other_it->current->next = current;
00391         other_it->current->prev = prev;
00392         other_it->next->prev = current;
00393 
00394         next = current;
00395         other_it->prev = other_it->current;
00396       }
00397     }
00398   }
00399   else {                         //no overlap
00400     prev->next = other_it->current;
00401     current->next = other_it->next;
00402     current->prev = other_it->prev;
00403     next->prev = other_it->current;
00404     other_it->prev->next = current;
00405     other_it->current->next = next;
00406     other_it->current->prev = prev;
00407     other_it->next->prev = current;
00408   }
00409 
00410   /* update end of list pointer when necessary (remember that the 2 iterators
00411     may iterate over different lists!) */
00412 
00413   if (list->last == current)
00414     list->last = other_it->current;
00415   if (other_it->list->last == other_it->current)
00416     other_it->list->last = current;
00417 
00418   if (current == cycle_pt)
00419     cycle_pt = other_it->cycle_pt;
00420   if (other_it->current == other_it->cycle_pt)
00421     other_it->cycle_pt = cycle_pt;
00422 
00423   /* The actual exchange - in all cases*/
00424 
00425   old_current = current;
00426   current = other_it->current;
00427   other_it->current = old_current;
00428 }
00429 
00430 
00431 /***********************************************************************
00432  *                                                      ELIST2_ITERATOR::extract_sublist()
00433  *
00434  *  This is a private member, used only by ELIST2::assign_to_sublist.
00435  *  Given another iterator for the same list, extract the links from THIS to
00436  *  OTHER inclusive, link them into a new circular list, and return a
00437  *  pointer to the last element.
00438  *  (Can't inline this function because it contains a loop)
00439  **********************************************************************/
00440 
00441 ELIST2_LINK *ELIST2_ITERATOR::extract_sublist(                              //from this current
00442                                               ELIST2_ITERATOR *other_it) {  //to other current
00443   #ifndef NDEBUG
00444   const ERRCODE BAD_EXTRACTION_PTS =
00445     "Can't extract sublist from points on different lists";
00446   const ERRCODE DONT_EXTRACT_DELETED =
00447     "Can't extract a sublist marked by deleted points";
00448   #endif
00449   const ERRCODE BAD_SUBLIST = "Can't find sublist end point in original list";
00450 
00451   ELIST2_ITERATOR temp_it = *this;
00452   ELIST2_LINK *end_of_new_list;
00453 
00454   #ifndef NDEBUG
00455   if (!this)
00456     NULL_OBJECT.error ("ELIST2_ITERATOR::extract_sublist", ABORT, NULL);
00457   if (!other_it)
00458     BAD_PARAMETER.error ("ELIST2_ITERATOR::extract_sublist", ABORT,
00459       "other_it NULL");
00460   if (!list)
00461     NO_LIST.error ("ELIST2_ITERATOR::extract_sublist", ABORT, NULL);
00462   if (list != other_it->list)
00463     BAD_EXTRACTION_PTS.error ("ELIST2_ITERATOR.extract_sublist", ABORT, NULL);
00464   if (list->empty ())
00465     EMPTY_LIST.error ("ELIST2_ITERATOR::extract_sublist", ABORT, NULL);
00466 
00467   if (!current || !other_it->current)
00468     DONT_EXTRACT_DELETED.error ("ELIST2_ITERATOR.extract_sublist", ABORT,
00469       NULL);
00470   #endif
00471 
00472   ex_current_was_last = other_it->ex_current_was_last = FALSE;
00473   ex_current_was_cycle_pt = FALSE;
00474   other_it->ex_current_was_cycle_pt = FALSE;
00475 
00476   temp_it.mark_cycle_pt ();
00477   do {                           //walk sublist
00478     if (temp_it.cycled_list ())  //cant find end pt
00479       BAD_SUBLIST.error ("ELIST2_ITERATOR.extract_sublist", ABORT, NULL);
00480 
00481     if (temp_it.at_last ()) {
00482       list->last = prev;
00483       ex_current_was_last = other_it->ex_current_was_last = TRUE;
00484     }
00485 
00486     if (temp_it.current == cycle_pt)
00487       ex_current_was_cycle_pt = TRUE;
00488 
00489     if (temp_it.current == other_it->cycle_pt)
00490       other_it->ex_current_was_cycle_pt = TRUE;
00491 
00492     temp_it.forward ();
00493   }
00494                                  //do INCLUSIVE list
00495   while (temp_it.prev != other_it->current);
00496 
00497                                  //circularise sublist
00498   other_it->current->next = current;
00499                                  //circularise sublist
00500   current->prev = other_it->current;
00501   end_of_new_list = other_it->current;
00502 
00503                                  //sublist = whole list
00504   if (prev == other_it->current) {
00505     list->last = NULL;
00506     prev = current = next = NULL;
00507     other_it->prev = other_it->current = other_it->next = NULL;
00508   }
00509   else {
00510     prev->next = other_it->next;
00511     other_it->next->prev = prev;
00512 
00513     current = other_it->current = NULL;
00514     next = other_it->next;
00515     other_it->prev = prev;
00516   }
00517   return end_of_new_list;
00518 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines