tesseract  4.00.00dev
elst.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * File: elst.c (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 <stdlib.h>
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 = NULL; //break circle
50  last = NULL; //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, NULL);
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 ELIST::length() const { // count elements
91  ELIST_ITERATOR it(const_cast<ELIST*>(this));
92  inT32 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 count;
113  ELIST_LINK **base; //ptr array to sort
114  ELIST_LINK **current;
115  inT32 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 ((char *) 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 == NULL || comparator(&last, &new_link) < 0) {
155  if (last == NULL) {
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, NULL);
198  #endif
199  if (list->empty ())
200  return NULL;
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  next = current->next;
214 
215  #ifndef NDEBUG
216  if (!current)
217  NULL_DATA.error ("ELIST_ITERATOR::forward", ABORT, NULL);
218  if (!next)
219  NULL_NEXT.error ("ELIST_ITERATOR::forward", ABORT,
220  "This is: %p Current is: %p", this, current);
221  #endif
222  return current;
223 }
224 
225 /***********************************************************************
226  * ELIST_ITERATOR::data_relative
227  *
228  * Return the data pointer to the element "offset" elements from current.
229  * "offset" must not be less than -1.
230  * (This function can't be INLINEd because it contains a loop)
231  **********************************************************************/
232 
234  inT8 offset) { //offset from current
235  ELIST_LINK *ptr;
236 
237  #ifndef NDEBUG
238  if (!list)
239  NO_LIST.error ("ELIST_ITERATOR::data_relative", ABORT, NULL);
240  if (list->empty ())
241  EMPTY_LIST.error ("ELIST_ITERATOR::data_relative", ABORT, NULL);
242  if (offset < -1)
243  BAD_PARAMETER.error ("ELIST_ITERATOR::data_relative", ABORT,
244  "offset < -l");
245  #endif
246 
247  if (offset == -1)
248  ptr = prev;
249  else
250  for (ptr = current ? current : prev; offset-- > 0; ptr = ptr->next);
251 
252  #ifndef NDEBUG
253  if (!ptr)
254  NULL_DATA.error ("ELIST_ITERATOR::data_relative", ABORT, NULL);
255  #endif
256 
257  return ptr;
258 }
259 
260 /***********************************************************************
261  * ELIST_ITERATOR::move_to_last()
262  *
263  * Move current so that it is set to the end of the list.
264  * Return data just in case anyone wants it.
265  * (This function can't be INLINEd because it contains a loop)
266  **********************************************************************/
267 
269  #ifndef NDEBUG
270  if (!list)
271  NO_LIST.error ("ELIST_ITERATOR::move_to_last", ABORT, NULL);
272  #endif
273 
274  while (current != list->last)
275  forward();
276 
277  return current;
278 }
279 
280 /***********************************************************************
281  * ELIST_ITERATOR::exchange()
282  *
283  * Given another iterator, whose current element is a different element on
284  * the same list list OR an element of another list, exchange the two current
285  * elements. On return, each iterator points to the element which was the
286  * other iterators current on entry.
287  * (This function hasn't been in-lined because its a bit big!)
288  **********************************************************************/
289 
290 void ELIST_ITERATOR::exchange( //positions of 2 links
291  ELIST_ITERATOR *other_it) { //other iterator
292  const ERRCODE DONT_EXCHANGE_DELETED =
293  "Can't exchange deleted elements of lists";
294 
295  ELIST_LINK *old_current;
296 
297  #ifndef NDEBUG
298  if (!list)
299  NO_LIST.error ("ELIST_ITERATOR::exchange", ABORT, NULL);
300  if (!other_it)
301  BAD_PARAMETER.error ("ELIST_ITERATOR::exchange", ABORT, "other_it NULL");
302  if (!(other_it->list))
303  NO_LIST.error ("ELIST_ITERATOR::exchange", ABORT, "other_it");
304  #endif
305 
306  /* Do nothing if either list is empty or if both iterators reference the same
307  link */
308 
309  if ((list->empty ()) ||
310  (other_it->list->empty ()) || (current == other_it->current))
311  return;
312 
313  /* Error if either current element is deleted */
314 
315  if (!current || !other_it->current)
316  DONT_EXCHANGE_DELETED.error ("ELIST_ITERATOR.exchange", ABORT, NULL);
317 
318  /* Now handle the 4 cases: doubleton list; non-doubleton adjacent elements
319  (other before this); non-doubleton adjacent elements (this before other);
320  non-adjacent elements. */
321 
322  //adjacent links
323  if ((next == other_it->current) ||
324  (other_it->next == current)) {
325  //doubleton list
326  if ((next == other_it->current) &&
327  (other_it->next == current)) {
328  prev = next = current;
329  other_it->prev = other_it->next = other_it->current;
330  }
331  else { //non-doubleton with
332  //adjacent links
333  //other before this
334  if (other_it->next == current) {
335  other_it->prev->next = current;
336  other_it->current->next = next;
337  current->next = other_it->current;
338  other_it->next = other_it->current;
339  prev = current;
340  }
341  else { //this before other
342  prev->next = other_it->current;
343  current->next = other_it->next;
344  other_it->current->next = current;
345  next = current;
346  other_it->prev = other_it->current;
347  }
348  }
349  }
350  else { //no overlap
351  prev->next = other_it->current;
352  current->next = other_it->next;
353  other_it->prev->next = current;
354  other_it->current->next = next;
355  }
356 
357  /* update end of list pointer when necessary (remember that the 2 iterators
358  may iterate over different lists!) */
359 
360  if (list->last == current)
361  list->last = other_it->current;
362  if (other_it->list->last == other_it->current)
363  other_it->list->last = current;
364 
365  if (current == cycle_pt)
366  cycle_pt = other_it->cycle_pt;
367  if (other_it->current == other_it->cycle_pt)
368  other_it->cycle_pt = cycle_pt;
369 
370  /* The actual exchange - in all cases*/
371 
372  old_current = current;
373  current = other_it->current;
374  other_it->current = old_current;
375 }
376 
377 /***********************************************************************
378  * ELIST_ITERATOR::extract_sublist()
379  *
380  * This is a private member, used only by ELIST::assign_to_sublist.
381  * Given another iterator for the same list, extract the links from THIS to
382  * OTHER inclusive, link them into a new circular list, and return a
383  * pointer to the last element.
384  * (Can't inline this function because it contains a loop)
385  **********************************************************************/
386 
387 ELIST_LINK *ELIST_ITERATOR::extract_sublist( //from this current
388  ELIST_ITERATOR *other_it) { //to other current
389  #ifndef NDEBUG
390  const ERRCODE BAD_EXTRACTION_PTS =
391  "Can't extract sublist from points on different lists";
392  const ERRCODE DONT_EXTRACT_DELETED =
393  "Can't extract a sublist marked by deleted points";
394  #endif
395  const ERRCODE BAD_SUBLIST = "Can't find sublist end point in original list";
396 
397  ELIST_ITERATOR temp_it = *this;
398  ELIST_LINK *end_of_new_list;
399 
400  #ifndef NDEBUG
401  if (!other_it)
402  BAD_PARAMETER.error ("ELIST_ITERATOR::extract_sublist", ABORT,
403  "other_it NULL");
404  if (!list)
405  NO_LIST.error ("ELIST_ITERATOR::extract_sublist", ABORT, NULL);
406  if (list != other_it->list)
407  BAD_EXTRACTION_PTS.error ("ELIST_ITERATOR.extract_sublist", ABORT, NULL);
408  if (list->empty ())
409  EMPTY_LIST.error ("ELIST_ITERATOR::extract_sublist", ABORT, NULL);
410 
411  if (!current || !other_it->current)
412  DONT_EXTRACT_DELETED.error ("ELIST_ITERATOR.extract_sublist", ABORT,
413  NULL);
414  #endif
415 
416  ex_current_was_last = other_it->ex_current_was_last = FALSE;
417  ex_current_was_cycle_pt = FALSE;
418  other_it->ex_current_was_cycle_pt = FALSE;
419 
420  temp_it.mark_cycle_pt ();
421  do { //walk sublist
422  if (temp_it.cycled_list()) // can't find end pt
423  BAD_SUBLIST.error ("ELIST_ITERATOR.extract_sublist", ABORT, NULL);
424 
425  if (temp_it.at_last ()) {
426  list->last = prev;
427  ex_current_was_last = other_it->ex_current_was_last = TRUE;
428  }
429 
430  if (temp_it.current == cycle_pt)
431  ex_current_was_cycle_pt = TRUE;
432 
433  if (temp_it.current == other_it->cycle_pt)
434  other_it->ex_current_was_cycle_pt = TRUE;
435 
436  temp_it.forward ();
437  }
438  while (temp_it.prev != other_it->current);
439 
440  //circularise sublist
441  other_it->current->next = current;
442  end_of_new_list = other_it->current;
443 
444  //sublist = whole list
445  if (prev == other_it->current) {
446  list->last = NULL;
447  prev = current = next = NULL;
448  other_it->prev = other_it->current = other_it->next = NULL;
449  }
450  else {
451  prev->next = other_it->next;
452  current = other_it->current = NULL;
453  next = other_it->next;
454  other_it->prev = prev;
455  }
456  return end_of_new_list;
457 }
void assign_to_sublist(ELIST_ITERATOR *start_it, ELIST_ITERATOR *end_it)
Definition: elst.cpp:72
bool at_last()
Definition: elst.h:711
#define TRUE
Definition: capi.h:45
int32_t inT32
Definition: host.h:38
const ERRCODE NULL_NEXT
Definition: lsterr.h:36
ELIST_LINK * forward()
Definition: elst.cpp:194
void internal_clear(void(*zapper)(ELIST_LINK *))
Definition: elst.cpp:41
const ERRCODE NULL_DATA
Definition: lsterr.h:34
void exchange(ELIST_ITERATOR *other_it)
Definition: elst.cpp:290
void add_to_end(ELIST_LINK *new_link)
Definition: elst.h:790
voidpf uLong offset
Definition: ioapi.h:42
ELIST_LINK * data()
Definition: elst.h:235
ELIST_LINK * data_relative(inT8 offset)
Definition: elst.cpp:233
void mark_cycle_pt()
Definition: elst.h:670
const ERRCODE BAD_PARAMETER
Definition: lsterr.h:39
inT32 length() const
Definition: elst.cpp:90
ELIST_LINK * move_to_last()
Definition: elst.cpp:268
#define FALSE
Definition: capi.h:46
void error(const char *caller, TessErrorLogCode action, const char *format,...) const
Definition: errcode.cpp:40
int8_t inT8
Definition: host.h:34
void add_before_then_move(ELIST_LINK *new_link)
Definition: elst.h:427
const ERRCODE EMPTY_LIST
Definition: lsterr.h:38
ELIST_LINK * extract()
Definition: elst.h:605
Definition: errcode.h:30
ELIST_LINK * add_sorted_and_find(int comparator(const void *, const void *), bool unique, ELIST_LINK *new_link)
Definition: elst.cpp:150
void sort(int comparator(const void *, const void *))
Definition: elst.cpp:108
const ERRCODE NO_LIST
Definition: lsterr.h:32
int count(LIST var_list)
Definition: oldlist.cpp:103
bool cycled_list()
Definition: elst.h:731
bool empty() const
Definition: elst.h:132