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