tesseract  4.0.0-1-g2a2b
elst.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * File: elst.cpp (Formerly elist.c)
3  * Description: Embedded list handling code which is not in the include file.
4  * Author: Phil Cheatle
5  * Created: Fri Jan 04 13:55:49 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 "elst.h"
22 
23 /***********************************************************************
24  * MEMBER FUNCTIONS OF CLASS: ELIST
25  * ================================
26  **********************************************************************/
27 
28 /***********************************************************************
29  * ELIST::internal_clear
30  *
31  * Used by the destructor and the "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 element of the list, regardless of its derived type. 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 ELIST::internal_clear ( //destroy all links
42 void (*zapper) (ELIST_LINK *)) {
43  //ptr to zapper functn
44  ELIST_LINK *ptr;
45  ELIST_LINK *next;
46 
47  if (!empty ()) {
48  ptr = last->next; //set to first
49  last->next = nullptr; //break circle
50  last = nullptr; //set list empty
51  while (ptr) {
52  next = ptr->next;
53  zapper(ptr);
54  ptr = next;
55  }
56  }
57 }
58 
59 /***********************************************************************
60  * ELIST::assign_to_sublist
61  *
62  * The list is set to a sublist of another list. "This" list must be empty
63  * before this function is invoked. The two iterators passed must refer to
64  * the same list, different from "this" one. The sublist removed is the
65  * inclusive list from start_it's current position to end_it's current
66  * position. If this range passes over the end of the source list then the
67  * source list has its end set to the previous element of start_it. The
68  * extracted sublist is unaffected by the end point of the source list, its
69  * end point is always the end_it position.
70  **********************************************************************/
71 
72 void ELIST::assign_to_sublist( //to this list
73  ELIST_ITERATOR *start_it, //from list start
74  ELIST_ITERATOR *end_it) { //from list end
75  const ERRCODE LIST_NOT_EMPTY =
76  "Destination list must be empty before extracting a sublist";
77 
78  if (!empty ())
79  LIST_NOT_EMPTY.error ("ELIST.assign_to_sublist", ABORT, nullptr);
80 
81  last = start_it->extract_sublist (end_it);
82 }
83 
84 /***********************************************************************
85  * ELIST::length
86  *
87  * Return count of elements on list
88  **********************************************************************/
89 
90 int32_t ELIST::length() const { // count elements
91  ELIST_ITERATOR it(const_cast<ELIST*>(this));
92  int32_t count = 0;
93 
94  for (it.mark_cycle_pt (); !it.cycled_list (); it.forward ())
95  count++;
96  return count;
97 }
98 
99 /***********************************************************************
100  * ELIST::sort
101  *
102  * Sort elements on list
103  * NB If you don't like the const declarations in the comparator, coerce yours:
104  * ( int (*)(const void *, const void *)
105  **********************************************************************/
106 
107 void
108 ELIST::sort ( //sort elements
109 int comparator ( //comparison routine
110 const void *, const void *)) {
111  ELIST_ITERATOR it(this);
112  int32_t count;
113  ELIST_LINK **base; //ptr array to sort
114  ELIST_LINK **current;
115  int32_t i;
116 
117  /* Allocate an array of pointers, one per list element */
118  count = length ();
119  base = (ELIST_LINK **) malloc (count * sizeof (ELIST_LINK *));
120 
121  /* Extract all elements, putting the pointers in the array */
122  current = base;
123  for (it.mark_cycle_pt (); !it.cycled_list (); it.forward ()) {
124  *current = it.extract ();
125  current++;
126  }
127 
128  /* Sort the pointer array */
129  qsort(base, count, sizeof(*base), comparator);
130 
131  /* Rebuild the list from the sorted pointers */
132  current = base;
133  for (i = 0; i < count; i++) {
134  it.add_to_end (*current);
135  current++;
136  }
137  free(base);
138 }
139 
140 // Assuming list has been sorted already, insert new_link to
141 // keep the list sorted according to the same comparison function.
142 // Comparison function is the same as used by sort, i.e. uses double
143 // indirection. Time is O(1) to add to beginning or end.
144 // Time is linear to add pre-sorted items to an empty list.
145 // If unique is set to true and comparator() returns 0 (an entry with the
146 // same information as the one contained in new_link is already in the
147 // list) - new_link is not added to the list and the function returns the
148 // pointer to the identical entry that already exists in the list
149 // (otherwise the function returns new_link).
151  int comparator(const void*, const void*),
152  bool unique, ELIST_LINK* new_link) {
153  // Check for adding at the end.
154  if (last == nullptr || comparator(&last, &new_link) < 0) {
155  if (last == nullptr) {
156  new_link->next = new_link;
157  } else {
158  new_link->next = last->next;
159  last->next = new_link;
160  }
161  last = new_link;
162  } else {
163  // Need to use an iterator.
164  ELIST_ITERATOR it(this);
165  for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
166  ELIST_LINK* link = it.data();
167  int compare = comparator(&link, &new_link);
168  if (compare > 0) {
169  break;
170  } else if (unique && compare == 0) {
171  return link;
172  }
173  }
174  if (it.cycled_list())
175  it.add_to_end(new_link);
176  else
177  it.add_before_then_move(new_link);
178  }
179  return new_link;
180 }
181 
182 /***********************************************************************
183  * MEMBER FUNCTIONS OF CLASS: ELIST_ITERATOR
184  * =========================================
185  **********************************************************************/
186 
187 /***********************************************************************
188  * ELIST_ITERATOR::forward
189  *
190  * Move the iterator to the next element of the list.
191  * REMEMBER: ALL LISTS ARE CIRCULAR.
192  **********************************************************************/
193 
195  #ifndef NDEBUG
196  if (!list)
197  NO_LIST.error ("ELIST_ITERATOR::forward", ABORT, nullptr);
198  #endif
199  if (list->empty ())
200  return nullptr;
201 
202  if (current) { //not removed so
203  //set previous
204  prev = current;
205  started_cycling = TRUE;
206  // In case next is deleted by another iterator, get next from current.
207  current = current->next;
208  } else {
209  if (ex_current_was_cycle_pt)
210  cycle_pt = next;
211  current = next;
212  }
213 #ifndef NDEBUG
214  if (!current)
215  NULL_DATA.error ("ELIST_ITERATOR::forward", ABORT, nullptr);
216 #endif
217  next = current->next;
218 
219  #ifndef NDEBUG
220  if (!next)
221  NULL_NEXT.error ("ELIST_ITERATOR::forward", ABORT,
222  "This is: %p Current is: %p", this, current);
223  #endif
224  return current;
225 }
226 
227 /***********************************************************************
228  * ELIST_ITERATOR::data_relative
229  *
230  * Return the data pointer to the element "offset" elements from current.
231  * "offset" must not be less than -1.
232  * (This function can't be INLINEd because it contains a loop)
233  **********************************************************************/
234 
236  int8_t offset) { //offset from current
237  ELIST_LINK *ptr;
238 
239  #ifndef NDEBUG
240  if (!list)
241  NO_LIST.error ("ELIST_ITERATOR::data_relative", ABORT, nullptr);
242  if (list->empty ())
243  EMPTY_LIST.error ("ELIST_ITERATOR::data_relative", ABORT, nullptr);
244  if (offset < -1)
245  BAD_PARAMETER.error ("ELIST_ITERATOR::data_relative", ABORT,
246  "offset < -l");
247  #endif
248 
249  if (offset == -1)
250  ptr = prev;
251  else
252  for (ptr = current ? current : prev; offset-- > 0; ptr = ptr->next);
253 
254  #ifndef NDEBUG
255  if (!ptr)
256  NULL_DATA.error ("ELIST_ITERATOR::data_relative", ABORT, nullptr);
257  #endif
258 
259  return ptr;
260 }
261 
262 /***********************************************************************
263  * ELIST_ITERATOR::move_to_last()
264  *
265  * Move current so that it is set to the end of the list.
266  * Return data just in case anyone wants it.
267  * (This function can't be INLINEd because it contains a loop)
268  **********************************************************************/
269 
271  #ifndef NDEBUG
272  if (!list)
273  NO_LIST.error ("ELIST_ITERATOR::move_to_last", ABORT, nullptr);
274  #endif
275 
276  while (current != list->last)
277  forward();
278 
279  return current;
280 }
281 
282 /***********************************************************************
283  * ELIST_ITERATOR::exchange()
284  *
285  * Given another iterator, whose current element is a different element on
286  * the same list list OR an element of another list, exchange the two current
287  * elements. On return, each iterator points to the element which was the
288  * other iterators current on entry.
289  * (This function hasn't been in-lined because its a bit big!)
290  **********************************************************************/
291 
292 void ELIST_ITERATOR::exchange( //positions of 2 links
293  ELIST_ITERATOR *other_it) { //other iterator
294  const ERRCODE DONT_EXCHANGE_DELETED =
295  "Can't exchange deleted elements of lists";
296 
297  ELIST_LINK *old_current;
298 
299  #ifndef NDEBUG
300  if (!list)
301  NO_LIST.error ("ELIST_ITERATOR::exchange", ABORT, nullptr);
302  if (!other_it)
303  BAD_PARAMETER.error ("ELIST_ITERATOR::exchange", ABORT, "other_it nullptr");
304  if (!(other_it->list))
305  NO_LIST.error ("ELIST_ITERATOR::exchange", ABORT, "other_it");
306  #endif
307 
308  /* Do nothing if either list is empty or if both iterators reference the same
309  link */
310 
311  if ((list->empty ()) ||
312  (other_it->list->empty ()) || (current == other_it->current))
313  return;
314 
315  /* Error if either current element is deleted */
316 
317  if (!current || !other_it->current)
318  DONT_EXCHANGE_DELETED.error ("ELIST_ITERATOR.exchange", ABORT, nullptr);
319 
320  /* Now handle the 4 cases: doubleton list; non-doubleton adjacent elements
321  (other before this); non-doubleton adjacent elements (this before other);
322  non-adjacent elements. */
323 
324  //adjacent links
325  if ((next == other_it->current) ||
326  (other_it->next == current)) {
327  //doubleton list
328  if ((next == other_it->current) &&
329  (other_it->next == current)) {
330  prev = next = current;
331  other_it->prev = other_it->next = other_it->current;
332  }
333  else { //non-doubleton with
334  //adjacent links
335  //other before this
336  if (other_it->next == current) {
337  other_it->prev->next = current;
338  other_it->current->next = next;
339  current->next = other_it->current;
340  other_it->next = other_it->current;
341  prev = current;
342  }
343  else { //this before other
344  prev->next = other_it->current;
345  current->next = other_it->next;
346  other_it->current->next = current;
347  next = current;
348  other_it->prev = other_it->current;
349  }
350  }
351  }
352  else { //no overlap
353  prev->next = other_it->current;
354  current->next = other_it->next;
355  other_it->prev->next = current;
356  other_it->current->next = next;
357  }
358 
359  /* update end of list pointer when necessary (remember that the 2 iterators
360  may iterate over different lists!) */
361 
362  if (list->last == current)
363  list->last = other_it->current;
364  if (other_it->list->last == other_it->current)
365  other_it->list->last = current;
366 
367  if (current == cycle_pt)
368  cycle_pt = other_it->cycle_pt;
369  if (other_it->current == other_it->cycle_pt)
370  other_it->cycle_pt = cycle_pt;
371 
372  /* The actual exchange - in all cases*/
373 
374  old_current = current;
375  current = other_it->current;
376  other_it->current = old_current;
377 }
378 
379 /***********************************************************************
380  * ELIST_ITERATOR::extract_sublist()
381  *
382  * This is a private member, used only by ELIST::assign_to_sublist.
383  * Given another iterator for the same list, extract the links from THIS to
384  * OTHER inclusive, link them into a new circular list, and return a
385  * pointer to the last element.
386  * (Can't inline this function because it contains a loop)
387  **********************************************************************/
388 
389 ELIST_LINK *ELIST_ITERATOR::extract_sublist( //from this current
390  ELIST_ITERATOR *other_it) { //to other current
391  #ifndef NDEBUG
392  const ERRCODE BAD_EXTRACTION_PTS =
393  "Can't extract sublist from points on different lists";
394  const ERRCODE DONT_EXTRACT_DELETED =
395  "Can't extract a sublist marked by deleted points";
396  #endif
397  const ERRCODE BAD_SUBLIST = "Can't find sublist end point in original list";
398 
399  ELIST_ITERATOR temp_it = *this;
400  ELIST_LINK *end_of_new_list;
401 
402  #ifndef NDEBUG
403  if (!other_it)
404  BAD_PARAMETER.error ("ELIST_ITERATOR::extract_sublist", ABORT,
405  "other_it nullptr");
406  if (!list)
407  NO_LIST.error ("ELIST_ITERATOR::extract_sublist", ABORT, nullptr);
408  if (list != other_it->list)
409  BAD_EXTRACTION_PTS.error ("ELIST_ITERATOR.extract_sublist", ABORT, nullptr);
410  if (list->empty ())
411  EMPTY_LIST.error ("ELIST_ITERATOR::extract_sublist", ABORT, nullptr);
412 
413  if (!current || !other_it->current)
414  DONT_EXTRACT_DELETED.error ("ELIST_ITERATOR.extract_sublist", ABORT,
415  nullptr);
416  #endif
417 
418  ex_current_was_last = other_it->ex_current_was_last = FALSE;
419  ex_current_was_cycle_pt = FALSE;
420  other_it->ex_current_was_cycle_pt = FALSE;
421 
422  temp_it.mark_cycle_pt ();
423  do { //walk sublist
424  if (temp_it.cycled_list()) // can't find end pt
425  BAD_SUBLIST.error ("ELIST_ITERATOR.extract_sublist", ABORT, nullptr);
426 
427  if (temp_it.at_last ()) {
428  list->last = prev;
429  ex_current_was_last = other_it->ex_current_was_last = TRUE;
430  }
431 
432  if (temp_it.current == cycle_pt)
433  ex_current_was_cycle_pt = TRUE;
434 
435  if (temp_it.current == other_it->cycle_pt)
436  other_it->ex_current_was_cycle_pt = TRUE;
437 
438  temp_it.forward ();
439  }
440  while (temp_it.prev != other_it->current);
441 
442  //circularise sublist
443  other_it->current->next = current;
444  end_of_new_list = other_it->current;
445 
446  //sublist = whole list
447  if (prev == other_it->current) {
448  list->last = nullptr;
449  prev = current = next = nullptr;
450  other_it->prev = other_it->current = other_it->next = nullptr;
451  }
452  else {
453  prev->next = other_it->next;
454  current = other_it->current = nullptr;
455  next = other_it->next;
456  other_it->prev = prev;
457  }
458  return end_of_new_list;
459 }
void add_before_then_move(ELIST_LINK *new_link)
Definition: elst.h:427
void assign_to_sublist(ELIST_ITERATOR *start_it, ELIST_ITERATOR *end_it)
Definition: elst.cpp:72
ELIST_LINK * move_to_last()
Definition: elst.cpp:270
#define TRUE
Definition: capi.h:51
int count(LIST var_list)
Definition: oldlist.cpp:98
void add_to_end(ELIST_LINK *new_link)
Definition: elst.h:790
ELIST_LINK * data()
Definition: elst.h:235
bool empty() const
Definition: elst.h:132
const ERRCODE NO_LIST
Definition: lsterr.h:32
Definition: errcode.h:30
const ERRCODE NULL_NEXT
Definition: lsterr.h:36
ELIST_LINK * extract()
Definition: elst.h:605
void sort(int comparator(const void *, const void *))
Definition: elst.cpp:108
#define FALSE
Definition: capi.h:52
const ERRCODE NULL_DATA
Definition: lsterr.h:34
ELIST_LINK * add_sorted_and_find(int comparator(const void *, const void *), bool unique, ELIST_LINK *new_link)
Definition: elst.cpp:150
void mark_cycle_pt()
Definition: elst.h:670
const ERRCODE EMPTY_LIST
Definition: lsterr.h:38
bool at_last()
Definition: elst.h:711
const ERRCODE BAD_PARAMETER
Definition: lsterr.h:39
ELIST_LINK * data_relative(int8_t offset)
Definition: elst.cpp:235
void exchange(ELIST_ITERATOR *other_it)
Definition: elst.cpp:292
int32_t length() const
Definition: elst.cpp:90
bool cycled_list()
Definition: elst.h:731
void internal_clear(void(*zapper)(ELIST_LINK *))
Definition: elst.cpp:41
ELIST_LINK * forward()
Definition: elst.cpp:194
void error(const char *caller, TessErrorLogCode action, const char *format,...) const
Definition: errcode.cpp:37