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