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