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