tesseract  4.0.0-1-g2a2b
clst.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * File: clst.cpp (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 <cstdlib>
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 = nullptr; //break circle
49  last = nullptr; //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 = nullptr; //break circle
75  last = nullptr; //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, nullptr);
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_t CLIST::length() const { //count elements
116  CLIST_ITERATOR it(const_cast<CLIST*>(this));
117  int32_t 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_t count;
136  void **base; //ptr array to sort
137  void **current;
138  int32_t 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(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 == nullptr || comparator(&last->data, &new_data) < 0) {
174  CLIST_LINK* new_element = new CLIST_LINK;
175  new_element->data = new_data;
176  if (last == nullptr) {
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 = nullptr;
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 == nullptr || 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, nullptr);
249  #endif
250  if (list->empty ())
251  return nullptr;
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 
265  #ifndef NDEBUG
266  if (!current)
267  NULL_DATA.error ("CLIST_ITERATOR::forward", ABORT, nullptr);
268  if (!next)
269  NULL_NEXT.error ("CLIST_ITERATOR::forward", ABORT,
270  "This is: %p Current is: %p", this, current);
271  #endif
272 
273  next = current->next;
274  return current->data;
275 }
276 
277 /***********************************************************************
278  * CLIST_ITERATOR::data_relative
279  *
280  * Return the data pointer to the element "offset" elements from current.
281  * "offset" must not be less than -1.
282  * (This function can't be INLINEd because it contains a loop)
283  **********************************************************************/
284 
285 void *CLIST_ITERATOR::data_relative( //get data + or - ...
286  int8_t offset) { //offset from current
287  CLIST_LINK *ptr;
288 
289  #ifndef NDEBUG
290  if (!list)
291  NO_LIST.error ("CLIST_ITERATOR::data_relative", ABORT, nullptr);
292  if (list->empty ())
293  EMPTY_LIST.error ("CLIST_ITERATOR::data_relative", ABORT, nullptr);
294  if (offset < -1)
295  BAD_PARAMETER.error ("CLIST_ITERATOR::data_relative", ABORT,
296  "offset < -l");
297  #endif
298 
299  if (offset == -1)
300  ptr = prev;
301  else
302  for (ptr = current ? current : prev; offset-- > 0; ptr = ptr->next);
303 
304  #ifndef NDEBUG
305  if (!ptr)
306  NULL_DATA.error ("CLIST_ITERATOR::data_relative", ABORT, nullptr);
307  #endif
308 
309  return ptr->data;
310 }
311 
312 /***********************************************************************
313  * CLIST_ITERATOR::move_to_last()
314  *
315  * Move current so that it is set to the end of the list.
316  * Return data just in case anyone wants it.
317  * (This function can't be INLINEd because it contains a loop)
318  **********************************************************************/
319 
321  #ifndef NDEBUG
322  if (!list)
323  NO_LIST.error ("CLIST_ITERATOR::move_to_last", ABORT, nullptr);
324  #endif
325 
326  while (current != list->last)
327  forward();
328 
329  if (current == nullptr)
330  return nullptr;
331  else
332  return current->data;
333 }
334 
335 /***********************************************************************
336  * CLIST_ITERATOR::exchange()
337  *
338  * Given another iterator, whose current element is a different element on
339  * the same list list OR an element of another list, exchange the two current
340  * elements. On return, each iterator points to the element which was the
341  * other iterators current on entry.
342  * (This function hasn't been in-lined because its a bit big!)
343  **********************************************************************/
344 
345 void CLIST_ITERATOR::exchange( //positions of 2 links
346  CLIST_ITERATOR *other_it) { //other iterator
347  const ERRCODE DONT_EXCHANGE_DELETED =
348  "Can't exchange deleted elements of lists";
349 
350  CLIST_LINK *old_current;
351 
352  #ifndef NDEBUG
353  if (!list)
354  NO_LIST.error ("CLIST_ITERATOR::exchange", ABORT, nullptr);
355  if (!other_it)
356  BAD_PARAMETER.error ("CLIST_ITERATOR::exchange", ABORT, "other_it nullptr");
357  if (!(other_it->list))
358  NO_LIST.error ("CLIST_ITERATOR::exchange", ABORT, "other_it");
359  #endif
360 
361  /* Do nothing if either list is empty or if both iterators reference the same
362  link */
363 
364  if ((list->empty ()) ||
365  (other_it->list->empty ()) || (current == other_it->current))
366  return;
367 
368  /* Error if either current element is deleted */
369 
370  if (!current || !other_it->current)
371  DONT_EXCHANGE_DELETED.error ("CLIST_ITERATOR.exchange", ABORT, nullptr);
372 
373  /* Now handle the 4 cases: doubleton list; non-doubleton adjacent elements
374  (other before this); non-doubleton adjacent elements (this before other);
375  non-adjacent elements. */
376 
377  //adjacent links
378  if ((next == other_it->current) ||
379  (other_it->next == current)) {
380  //doubleton list
381  if ((next == other_it->current) &&
382  (other_it->next == current)) {
383  prev = next = current;
384  other_it->prev = other_it->next = other_it->current;
385  }
386  else { //non-doubleton with
387  //adjacent links
388  //other before this
389  if (other_it->next == current) {
390  other_it->prev->next = current;
391  other_it->current->next = next;
392  current->next = other_it->current;
393  other_it->next = other_it->current;
394  prev = current;
395  }
396  else { //this before other
397  prev->next = other_it->current;
398  current->next = other_it->next;
399  other_it->current->next = current;
400  next = current;
401  other_it->prev = other_it->current;
402  }
403  }
404  }
405  else { //no overlap
406  prev->next = other_it->current;
407  current->next = other_it->next;
408  other_it->prev->next = current;
409  other_it->current->next = next;
410  }
411 
412  /* update end of list pointer when necessary (remember that the 2 iterators
413  may iterate over different lists!) */
414 
415  if (list->last == current)
416  list->last = other_it->current;
417  if (other_it->list->last == other_it->current)
418  other_it->list->last = current;
419 
420  if (current == cycle_pt)
421  cycle_pt = other_it->cycle_pt;
422  if (other_it->current == other_it->cycle_pt)
423  other_it->cycle_pt = cycle_pt;
424 
425  /* The actual exchange - in all cases*/
426 
427  old_current = current;
428  current = other_it->current;
429  other_it->current = old_current;
430 }
431 
432 /***********************************************************************
433  * CLIST_ITERATOR::extract_sublist()
434  *
435  * This is a private member, used only by CLIST::assign_to_sublist.
436  * Given another iterator for the same list, extract the links from THIS to
437  * OTHER inclusive, link them into a new circular list, and return a
438  * pointer to the last element.
439  * (Can't inline this function because it contains a loop)
440  **********************************************************************/
441 
442 CLIST_LINK *CLIST_ITERATOR::extract_sublist( //from this current
443  CLIST_ITERATOR *other_it) { //to other current
444  CLIST_ITERATOR temp_it = *this;
445  CLIST_LINK *end_of_new_list;
446 
447  const ERRCODE BAD_SUBLIST = "Can't find sublist end point in original list";
448  #ifndef NDEBUG
449  const ERRCODE BAD_EXTRACTION_PTS =
450  "Can't extract sublist from points on different lists";
451  const ERRCODE DONT_EXTRACT_DELETED =
452  "Can't extract a sublist marked by deleted points";
453 
454  if (!other_it)
455  BAD_PARAMETER.error ("CLIST_ITERATOR::extract_sublist", ABORT,
456  "other_it nullptr");
457  if (!list)
458  NO_LIST.error ("CLIST_ITERATOR::extract_sublist", ABORT, nullptr);
459  if (list != other_it->list)
460  BAD_EXTRACTION_PTS.error ("CLIST_ITERATOR.extract_sublist", ABORT, nullptr);
461  if (list->empty ())
462  EMPTY_LIST.error ("CLIST_ITERATOR::extract_sublist", ABORT, nullptr);
463 
464  if (!current || !other_it->current)
465  DONT_EXTRACT_DELETED.error ("CLIST_ITERATOR.extract_sublist", ABORT,
466  nullptr);
467  #endif
468 
469  ex_current_was_last = other_it->ex_current_was_last = false;
470  ex_current_was_cycle_pt = false;
471  other_it->ex_current_was_cycle_pt = false;
472 
473  temp_it.mark_cycle_pt ();
474  do { //walk sublist
475  if (temp_it.cycled_list()) // can't find end pt
476  BAD_SUBLIST.error ("CLIST_ITERATOR.extract_sublist", ABORT, nullptr);
477 
478  if (temp_it.at_last ()) {
479  list->last = prev;
480  ex_current_was_last = other_it->ex_current_was_last = true;
481  }
482 
483  if (temp_it.current == cycle_pt)
484  ex_current_was_cycle_pt = true;
485 
486  if (temp_it.current == other_it->cycle_pt)
487  other_it->ex_current_was_cycle_pt = true;
488 
489  temp_it.forward ();
490  }
491  while (temp_it.prev != other_it->current);
492 
493  //circularise sublist
494  other_it->current->next = current;
495  end_of_new_list = other_it->current;
496 
497  //sublist = whole list
498  if (prev == other_it->current) {
499  list->last = nullptr;
500  prev = current = next = nullptr;
501  other_it->prev = other_it->current = other_it->next = nullptr;
502  }
503  else {
504  prev->next = other_it->next;
505  current = other_it->current = nullptr;
506  next = other_it->next;
507  other_it->prev = prev;
508  }
509  return end_of_new_list;
510 }
bool empty() const
Definition: clst.h:95
bool cycled_list()
Definition: clst.h:691
#define TRUE
Definition: capi.h:51
void shallow_clear()
Definition: clst.cpp:68
int count(LIST var_list)
Definition: oldlist.cpp:98
bool empty()
Definition: clst.h:217
bool at_last()
Definition: clst.h:672
bool add_sorted(int comparator(const void *, const void *), bool unique, void *new_data)
Definition: clst.cpp:170
void assign_to_sublist(CLIST_ITERATOR *start_it, CLIST_ITERATOR *end_it)
Definition: clst.cpp:97
const ERRCODE NO_LIST
Definition: lsterr.h:32
void * data()
Definition: clst.h:194
Definition: errcode.h:30
const ERRCODE NULL_NEXT
Definition: lsterr.h:36
int32_t length() const
Definition: clst.cpp:115
void add_before_then_move(void *new_data)
Definition: clst.h:388
const ERRCODE NULL_DATA
Definition: lsterr.h:34
void internal_deep_clear(void(*zapper)(void *))
Definition: clst.cpp:41
void * data_relative(int8_t offset)
Definition: clst.cpp:285
void sort(int comparator(const void *, const void *))
Definition: clst.cpp:131
void * extract()
Definition: clst.h:570
void exchange(CLIST_ITERATOR *other_it)
Definition: clst.cpp:345
void * move_to_last()
Definition: clst.cpp:320
const ERRCODE EMPTY_LIST
Definition: lsterr.h:38
const ERRCODE BAD_PARAMETER
Definition: lsterr.h:39
void * forward()
Definition: clst.cpp:245
void mark_cycle_pt()
Definition: clst.h:633
void set_subtract(int comparator(const void *, const void *), bool unique, CLIST *minuend, CLIST *subtrahend)
Definition: clst.cpp:208
Definition: clst.h:70
void add_to_end(void *new_data)
Definition: clst.h:747
void error(const char *caller, TessErrorLogCode action, const char *format,...) const
Definition: errcode.cpp:37