tesseract  4.00.00dev
clst.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * File: clst.c (Formerly clist.c)
3  * Description: CONS cell list handling code which is not in the include file.
4  * Author: Phil Cheatle
5  * Created: Mon Jan 28 08:33:13 GMT 1991
6  *
7  * (C) Copyright 1991, Hewlett-Packard Ltd.
8  ** Licensed under the Apache License, Version 2.0 (the "License");
9  ** you may not use this file except in compliance with the License.
10  ** You may obtain a copy of the License at
11  ** http://www.apache.org/licenses/LICENSE-2.0
12  ** Unless required by applicable law or agreed to in writing, software
13  ** distributed under the License is distributed on an "AS IS" BASIS,
14  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  ** See the License for the specific language governing permissions and
16  ** limitations under the License.
17  *
18  **********************************************************************/
19 
20 #include <stdlib.h>
21 #include "clst.h"
22 
23 /***********************************************************************
24  * MEMBER FUNCTIONS OF CLASS: CLIST
25  * ================================
26  **********************************************************************/
27 
28 /***********************************************************************
29  * CLIST::internal_deep_clear
30  *
31  * Used by the "deep_clear" member function of derived list
32  * classes to destroy all the elements on the list.
33  * The calling function passes a "zapper" function which can be called to
34  * delete each data element of the list, regardless of its class. This
35  * technique permits a generic clear function to destroy elements of
36  * different derived types correctly, without requiring virtual functions and
37  * the consequential memory overhead.
38  **********************************************************************/
39 
40 void
41 CLIST::internal_deep_clear ( //destroy all links
42 void (*zapper) (void *)) { //ptr to zapper functn
43  CLIST_LINK *ptr;
44  CLIST_LINK *next;
45 
46  if (!empty ()) {
47  ptr = last->next; //set to first
48  last->next = NULL; //break circle
49  last = NULL; //set list empty
50  while (ptr) {
51  next = ptr->next;
52  zapper (ptr->data);
53  delete(ptr);
54  ptr = next;
55  }
56  }
57 }
58 
59 /***********************************************************************
60  * CLIST::shallow_clear
61  *
62  * Used by the destructor and the "shallow_clear" member function of derived
63  * list classes to destroy the list.
64  * The data elements are NOT destroyed.
65  *
66  **********************************************************************/
67 
68 void CLIST::shallow_clear() { //destroy all links
69  CLIST_LINK *ptr;
70  CLIST_LINK *next;
71 
72  if (!empty ()) {
73  ptr = last->next; //set to first
74  last->next = NULL; //break circle
75  last = NULL; //set list empty
76  while (ptr) {
77  next = ptr->next;
78  delete(ptr);
79  ptr = next;
80  }
81  }
82 }
83 
84 /***********************************************************************
85  * CLIST::assign_to_sublist
86  *
87  * The list is set to a sublist of another list. "This" list must be empty
88  * before this function is invoked. The two iterators passed must refer to
89  * the same list, different from "this" one. The sublist removed is the
90  * inclusive list from start_it's current position to end_it's current
91  * position. If this range passes over the end of the source list then the
92  * source list has its end set to the previous element of start_it. The
93  * extracted sublist is unaffected by the end point of the source list, its
94  * end point is always the end_it position.
95  **********************************************************************/
96 
97 void CLIST::assign_to_sublist( //to this list
98  CLIST_ITERATOR *start_it, //from list start
99  CLIST_ITERATOR *end_it) { //from list end
100  const ERRCODE LIST_NOT_EMPTY =
101  "Destination list must be empty before extracting a sublist";
102 
103  if (!empty ())
104  LIST_NOT_EMPTY.error ("CLIST.assign_to_sublist", ABORT, NULL);
105 
106  last = start_it->extract_sublist (end_it);
107 }
108 
109 /***********************************************************************
110  * CLIST::length
111  *
112  * Return count of elements on list
113  **********************************************************************/
114 
115 inT32 CLIST::length() const { //count elements
116  CLIST_ITERATOR it(const_cast<CLIST*>(this));
117  inT32 count = 0;
118 
119  for (it.mark_cycle_pt(); !it.cycled_list(); it.forward())
120  count++;
121  return count;
122 }
123 
124 /***********************************************************************
125  * CLIST::sort
126  *
127  * Sort elements on list
128  **********************************************************************/
129 
130 void
131 CLIST::sort ( //sort elements
132 int comparator ( //comparison routine
133 const void *, const void *)) {
134  CLIST_ITERATOR it(this);
135  inT32 count;
136  void **base; //ptr array to sort
137  void **current;
138  inT32 i;
139 
140  /* Allocate an array of pointers, one per list element */
141  count = length ();
142  base = (void **) malloc (count * sizeof (void *));
143 
144  /* Extract all elements, putting the pointers in the array */
145  current = base;
146  for (it.mark_cycle_pt (); !it.cycled_list (); it.forward ()) {
147  *current = it.extract ();
148  current++;
149  }
150 
151  /* Sort the pointer array */
152  qsort ((char *) base, count, sizeof (*base), comparator);
153 
154  /* Rebuild the list from the sorted pointers */
155  current = base;
156  for (i = 0; i < count; i++) {
157  it.add_to_end (*current);
158  current++;
159  }
160  free(base);
161 }
162 
163 // Assuming list has been sorted already, insert new_data to
164 // keep the list sorted according to the same comparison function.
165 // Comparison function is the same as used by sort, i.e. uses double
166 // indirection. Time is O(1) to add to beginning or end.
167 // Time is linear to add pre-sorted items to an empty list.
168 // If unique, then don't add duplicate entries.
169 // Returns true if the element was added to the list.
170 bool CLIST::add_sorted(int comparator(const void*, const void*),
171  bool unique, void* new_data) {
172  // Check for adding at the end.
173  if (last == NULL || comparator(&last->data, &new_data) < 0) {
174  CLIST_LINK* new_element = new CLIST_LINK;
175  new_element->data = new_data;
176  if (last == NULL) {
177  new_element->next = new_element;
178  } else {
179  new_element->next = last->next;
180  last->next = new_element;
181  }
182  last = new_element;
183  return true;
184  } else if (!unique || last->data != new_data) {
185  // Need to use an iterator.
186  CLIST_ITERATOR it(this);
187  for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
188  void* data = it.data();
189  if (data == new_data && unique)
190  return false;
191  if (comparator(&data, &new_data) > 0)
192  break;
193  }
194  if (it.cycled_list())
195  it.add_to_end(new_data);
196  else
197  it.add_before_then_move(new_data);
198  return true;
199  }
200  return false;
201 }
202 
203 // Assuming that the minuend and subtrahend are already sorted with
204 // the same comparison function, shallow clears this and then copies
205 // the set difference minuend - subtrahend to this, being the elements
206 // of minuend that do not compare equal to anything in subtrahend.
207 // If unique is true, any duplicates in minuend are also eliminated.
208 void CLIST::set_subtract(int comparator(const void*, const void*),
209  bool unique,
210  CLIST* minuend, CLIST* subtrahend) {
211  shallow_clear();
212  CLIST_ITERATOR m_it(minuend);
213  CLIST_ITERATOR s_it(subtrahend);
214  // Since both lists are sorted, finding the subtras that are not
215  // minus is a case of a parallel iteration.
216  for (m_it.mark_cycle_pt(); !m_it.cycled_list(); m_it.forward()) {
217  void* minu = m_it.data();
218  void* subtra = NULL;
219  if (!s_it.empty()) {
220  subtra = s_it.data();
221  while (!s_it.at_last() &&
222  comparator(&subtra, &minu) < 0) {
223  s_it.forward();
224  subtra = s_it.data();
225  }
226  }
227  if (subtra == NULL || comparator(&subtra, &minu) != 0)
228  add_sorted(comparator, unique, minu);
229  }
230 }
231 
232 
233 /***********************************************************************
234  * MEMBER FUNCTIONS OF CLASS: CLIST_ITERATOR
235  * =========================================
236  **********************************************************************/
237 
238 /***********************************************************************
239  * CLIST_ITERATOR::forward
240  *
241  * Move the iterator to the next element of the list.
242  * REMEMBER: ALL LISTS ARE CIRCULAR.
243  **********************************************************************/
244 
246  #ifndef NDEBUG
247  if (!list)
248  NO_LIST.error ("CLIST_ITERATOR::forward", ABORT, NULL);
249  #endif
250  if (list->empty ())
251  return NULL;
252 
253  if (current) { //not removed so
254  //set previous
255  prev = current;
256  started_cycling = TRUE;
257  // In case next is deleted by another iterator, get next from current.
258  current = current->next;
259  } else {
260  if (ex_current_was_cycle_pt)
261  cycle_pt = next;
262  current = next;
263  }
264  next = current->next;
265 
266  #ifndef NDEBUG
267  if (!current)
268  NULL_DATA.error ("CLIST_ITERATOR::forward", ABORT, NULL);
269  if (!next)
270  NULL_NEXT.error ("CLIST_ITERATOR::forward", ABORT,
271  "This is: %p Current is: %p", this, current);
272  #endif
273  return current->data;
274 }
275 
276 /***********************************************************************
277  * CLIST_ITERATOR::data_relative
278  *
279  * Return the data pointer to the element "offset" elements from current.
280  * "offset" must not be less than -1.
281  * (This function can't be INLINEd because it contains a loop)
282  **********************************************************************/
283 
284 void *CLIST_ITERATOR::data_relative( //get data + or - ...
285  inT8 offset) { //offset from current
286  CLIST_LINK *ptr;
287 
288  #ifndef NDEBUG
289  if (!list)
290  NO_LIST.error ("CLIST_ITERATOR::data_relative", ABORT, NULL);
291  if (list->empty ())
292  EMPTY_LIST.error ("CLIST_ITERATOR::data_relative", ABORT, NULL);
293  if (offset < -1)
294  BAD_PARAMETER.error ("CLIST_ITERATOR::data_relative", ABORT,
295  "offset < -l");
296  #endif
297 
298  if (offset == -1)
299  ptr = prev;
300  else
301  for (ptr = current ? current : prev; offset-- > 0; ptr = ptr->next);
302 
303  #ifndef NDEBUG
304  if (!ptr)
305  NULL_DATA.error ("CLIST_ITERATOR::data_relative", ABORT, NULL);
306  #endif
307 
308  return ptr->data;
309 }
310 
311 /***********************************************************************
312  * CLIST_ITERATOR::move_to_last()
313  *
314  * Move current so that it is set to the end of the list.
315  * Return data just in case anyone wants it.
316  * (This function can't be INLINEd because it contains a loop)
317  **********************************************************************/
318 
320  #ifndef NDEBUG
321  if (!list)
322  NO_LIST.error ("CLIST_ITERATOR::move_to_last", ABORT, NULL);
323  #endif
324 
325  while (current != list->last)
326  forward();
327 
328  if (current == NULL)
329  return NULL;
330  else
331  return current->data;
332 }
333 
334 /***********************************************************************
335  * CLIST_ITERATOR::exchange()
336  *
337  * Given another iterator, whose current element is a different element on
338  * the same list list OR an element of another list, exchange the two current
339  * elements. On return, each iterator points to the element which was the
340  * other iterators current on entry.
341  * (This function hasn't been in-lined because its a bit big!)
342  **********************************************************************/
343 
344 void CLIST_ITERATOR::exchange( //positions of 2 links
345  CLIST_ITERATOR *other_it) { //other iterator
346  const ERRCODE DONT_EXCHANGE_DELETED =
347  "Can't exchange deleted elements of lists";
348 
349  CLIST_LINK *old_current;
350 
351  #ifndef NDEBUG
352  if (!list)
353  NO_LIST.error ("CLIST_ITERATOR::exchange", ABORT, NULL);
354  if (!other_it)
355  BAD_PARAMETER.error ("CLIST_ITERATOR::exchange", ABORT, "other_it NULL");
356  if (!(other_it->list))
357  NO_LIST.error ("CLIST_ITERATOR::exchange", ABORT, "other_it");
358  #endif
359 
360  /* Do nothing if either list is empty or if both iterators reference the same
361  link */
362 
363  if ((list->empty ()) ||
364  (other_it->list->empty ()) || (current == other_it->current))
365  return;
366 
367  /* Error if either current element is deleted */
368 
369  if (!current || !other_it->current)
370  DONT_EXCHANGE_DELETED.error ("CLIST_ITERATOR.exchange", ABORT, NULL);
371 
372  /* Now handle the 4 cases: doubleton list; non-doubleton adjacent elements
373  (other before this); non-doubleton adjacent elements (this before other);
374  non-adjacent elements. */
375 
376  //adjacent links
377  if ((next == other_it->current) ||
378  (other_it->next == current)) {
379  //doubleton list
380  if ((next == other_it->current) &&
381  (other_it->next == current)) {
382  prev = next = current;
383  other_it->prev = other_it->next = other_it->current;
384  }
385  else { //non-doubleton with
386  //adjacent links
387  //other before this
388  if (other_it->next == current) {
389  other_it->prev->next = current;
390  other_it->current->next = next;
391  current->next = other_it->current;
392  other_it->next = other_it->current;
393  prev = current;
394  }
395  else { //this before other
396  prev->next = other_it->current;
397  current->next = other_it->next;
398  other_it->current->next = current;
399  next = current;
400  other_it->prev = other_it->current;
401  }
402  }
403  }
404  else { //no overlap
405  prev->next = other_it->current;
406  current->next = other_it->next;
407  other_it->prev->next = current;
408  other_it->current->next = next;
409  }
410 
411  /* update end of list pointer when necessary (remember that the 2 iterators
412  may iterate over different lists!) */
413 
414  if (list->last == current)
415  list->last = other_it->current;
416  if (other_it->list->last == other_it->current)
417  other_it->list->last = current;
418 
419  if (current == cycle_pt)
420  cycle_pt = other_it->cycle_pt;
421  if (other_it->current == other_it->cycle_pt)
422  other_it->cycle_pt = cycle_pt;
423 
424  /* The actual exchange - in all cases*/
425 
426  old_current = current;
427  current = other_it->current;
428  other_it->current = old_current;
429 }
430 
431 /***********************************************************************
432  * CLIST_ITERATOR::extract_sublist()
433  *
434  * This is a private member, used only by CLIST::assign_to_sublist.
435  * Given another iterator for the same list, extract the links from THIS to
436  * OTHER inclusive, link them into a new circular list, and return a
437  * pointer to the last element.
438  * (Can't inline this function because it contains a loop)
439  **********************************************************************/
440 
441 CLIST_LINK *CLIST_ITERATOR::extract_sublist( //from this current
442  CLIST_ITERATOR *other_it) { //to other current
443  CLIST_ITERATOR temp_it = *this;
444  CLIST_LINK *end_of_new_list;
445 
446  const ERRCODE BAD_SUBLIST = "Can't find sublist end point in original list";
447  #ifndef NDEBUG
448  const ERRCODE BAD_EXTRACTION_PTS =
449  "Can't extract sublist from points on different lists";
450  const ERRCODE DONT_EXTRACT_DELETED =
451  "Can't extract a sublist marked by deleted points";
452 
453  if (!other_it)
454  BAD_PARAMETER.error ("CLIST_ITERATOR::extract_sublist", ABORT,
455  "other_it NULL");
456  if (!list)
457  NO_LIST.error ("CLIST_ITERATOR::extract_sublist", ABORT, NULL);
458  if (list != other_it->list)
459  BAD_EXTRACTION_PTS.error ("CLIST_ITERATOR.extract_sublist", ABORT, NULL);
460  if (list->empty ())
461  EMPTY_LIST.error ("CLIST_ITERATOR::extract_sublist", ABORT, NULL);
462 
463  if (!current || !other_it->current)
464  DONT_EXTRACT_DELETED.error ("CLIST_ITERATOR.extract_sublist", ABORT,
465  NULL);
466  #endif
467 
468  ex_current_was_last = other_it->ex_current_was_last = FALSE;
469  ex_current_was_cycle_pt = FALSE;
470  other_it->ex_current_was_cycle_pt = FALSE;
471 
472  temp_it.mark_cycle_pt ();
473  do { //walk sublist
474  if (temp_it.cycled_list()) // can't find end pt
475  BAD_SUBLIST.error ("CLIST_ITERATOR.extract_sublist", ABORT, NULL);
476 
477  if (temp_it.at_last ()) {
478  list->last = prev;
479  ex_current_was_last = other_it->ex_current_was_last = TRUE;
480  }
481 
482  if (temp_it.current == cycle_pt)
483  ex_current_was_cycle_pt = TRUE;
484 
485  if (temp_it.current == other_it->cycle_pt)
486  other_it->ex_current_was_cycle_pt = TRUE;
487 
488  temp_it.forward ();
489  }
490  while (temp_it.prev != other_it->current);
491 
492  //circularise sublist
493  other_it->current->next = current;
494  end_of_new_list = other_it->current;
495 
496  //sublist = whole list
497  if (prev == other_it->current) {
498  list->last = NULL;
499  prev = current = next = NULL;
500  other_it->prev = other_it->current = other_it->next = NULL;
501  }
502  else {
503  prev->next = other_it->next;
504  current = other_it->current = NULL;
505  next = other_it->next;
506  other_it->prev = prev;
507  }
508  return end_of_new_list;
509 }
void add_to_end(void *new_data)
Definition: clst.h:747
BOOL8 at_last()
Definition: clst.h:672
#define TRUE
Definition: capi.h:45
int32_t inT32
Definition: host.h:38
void internal_deep_clear(void(*zapper)(void *))
Definition: clst.cpp:41
const ERRCODE NULL_NEXT
Definition: lsterr.h:36
const ERRCODE NULL_DATA
Definition: lsterr.h:34
void sort(int comparator(const void *, const void *))
Definition: clst.cpp:131
void add_before_then_move(void *new_data)
Definition: clst.h:388
Definition: clst.h:70
BOOL8 empty()
Definition: clst.h:217
voidpf uLong offset
Definition: ioapi.h:42
void * move_to_last()
Definition: clst.cpp:319
bool add_sorted(int comparator(const void *, const void *), bool unique, void *new_data)
Definition: clst.cpp:170
const ERRCODE BAD_PARAMETER
Definition: lsterr.h:39
void * forward()
Definition: clst.cpp:245
#define FALSE
Definition: capi.h:46
void error(const char *caller, TessErrorLogCode action, const char *format,...) const
Definition: errcode.cpp:40
void mark_cycle_pt()
Definition: clst.h:633
void * data_relative(inT8 offset)
Definition: clst.cpp:284
void * extract()
Definition: clst.h:570
BOOL8 cycled_list()
Definition: clst.h:691
int8_t inT8
Definition: host.h:34
const ERRCODE EMPTY_LIST
Definition: lsterr.h:38
inT32 length() const
Definition: clst.cpp:115
void exchange(CLIST_ITERATOR *other_it)
Definition: clst.cpp:344
Definition: errcode.h:30
void set_subtract(int comparator(const void *, const void *), bool unique, CLIST *minuend, CLIST *subtrahend)
Definition: clst.cpp:208
void * data()
Definition: clst.h:194
void shallow_clear()
Definition: clst.cpp:68
void assign_to_sublist(CLIST_ITERATOR *start_it, CLIST_ITERATOR *end_it)
Definition: clst.cpp:97
const ERRCODE NO_LIST
Definition: lsterr.h:32
int count(LIST var_list)
Definition: oldlist.cpp:103
bool empty() const
Definition: clst.h:95