tesseract  5.0.0-alpha-619-ge9db
CLIST_ITERATOR Class Reference

#include <clst.h>

Public Member Functions

 CLIST_ITERATOR ()
 
 CLIST_ITERATOR (CLIST *list_to_iterate)
 
void set_to_list (CLIST *list_to_iterate)
 
void add_after_then_move (void *new_data)
 
void add_after_stay_put (void *new_data)
 
void add_before_then_move (void *new_data)
 
void add_before_stay_put (void *new_data)
 
void add_list_after (CLIST *list_to_add)
 
void add_list_before (CLIST *list_to_add)
 
void * data ()
 
void * data_relative (int8_t offset)
 
void * forward ()
 
void * extract ()
 
void * move_to_first ()
 
void * move_to_last ()
 
void mark_cycle_pt ()
 
bool empty ()
 
bool current_extracted ()
 
bool at_first ()
 
bool at_last ()
 
bool cycled_list ()
 
void add_to_end (void *new_data)
 
void exchange (CLIST_ITERATOR *other_it)
 
int32_t length ()
 
void sort (int comparator(const void *, const void *))
 

Friends

void CLIST::assign_to_sublist (CLIST_ITERATOR *, CLIST_ITERATOR *)
 

Detailed Description

Definition at line 141 of file clst.h.

Constructor & Destructor Documentation

◆ CLIST_ITERATOR() [1/2]

CLIST_ITERATOR::CLIST_ITERATOR ( )
inline

Definition at line 162 of file clst.h.

162  { //constructor
163  list = nullptr;
164  } //unassigned list

◆ CLIST_ITERATOR() [2/2]

CLIST_ITERATOR::CLIST_ITERATOR ( CLIST list_to_iterate)
inline

Definition at line 270 of file clst.h.

Member Function Documentation

◆ add_after_stay_put()

void CLIST_ITERATOR::add_after_stay_put ( void *  new_data)
inline

Definition at line 326 of file clst.h.

335  {
336  CLIST_LINK *new_element;
337 
338  #ifndef NDEBUG
339  if (!list)
340  NO_LIST.error ("CLIST_ITERATOR::add_after_stay_put", ABORT, nullptr);
341  if (!new_data)
342  BAD_PARAMETER.error ("CLIST_ITERATOR::add_after_stay_put", ABORT,
343  "new_data is nullptr");
344  #endif
345 
346  new_element = new CLIST_LINK;
347  new_element->data = new_data;
348 
349  if (list->empty ()) {
350  new_element->next = new_element;
351  list->last = new_element;
352  prev = next = new_element;
353  ex_current_was_last = false;
354  current = nullptr;
355  }
356  else {
357  new_element->next = next;
358 
359  if (current) { //not extracted
360  current->next = new_element;
361  if (prev == current)
362  prev = new_element;
363  if (current == list->last)
364  list->last = new_element;
365  }
366  else { //current extracted
367  prev->next = new_element;

◆ add_after_then_move()

void CLIST_ITERATOR::add_after_then_move ( void *  new_data)
inline

Definition at line 280 of file clst.h.

288  {
289  CLIST_LINK *new_element;
290 
291  #ifndef NDEBUG
292  if (!list)
293  NO_LIST.error ("CLIST_ITERATOR::add_after_then_move", ABORT, nullptr);
294  if (!new_data)
295  BAD_PARAMETER.error ("CLIST_ITERATOR::add_after_then_move", ABORT,
296  "new_data is nullptr");
297  #endif
298 
299  new_element = new CLIST_LINK;
300  new_element->data = new_data;
301 
302  if (list->empty ()) {
303  new_element->next = new_element;
304  list->last = new_element;
305  prev = next = new_element;
306  }
307  else {
308  new_element->next = next;
309 
310  if (current) { //not extracted
311  current->next = new_element;
312  prev = current;
313  if (current == list->last)
314  list->last = new_element;
315  }
316  else { //current extracted
317  prev->next = new_element;
318  if (ex_current_was_last)

◆ add_before_stay_put()

void CLIST_ITERATOR::add_before_stay_put ( void *  new_data)
inline

Definition at line 418 of file clst.h.

429  {
430  CLIST_LINK *new_element;
431 
432  #ifndef NDEBUG
433  if (!list)
434  NO_LIST.error ("CLIST_ITERATOR::add_before_stay_put", ABORT, nullptr);
435  if (!new_data)
436  BAD_PARAMETER.error ("CLIST_ITERATOR::add_before_stay_put", ABORT,
437  "new_data is nullptr");
438  #endif
439 
440  new_element = new CLIST_LINK;
441  new_element->data = new_data;
442 
443  if (list->empty ()) {
444  new_element->next = new_element;
445  list->last = new_element;
446  prev = next = new_element;
447  ex_current_was_last = true;
448  current = nullptr;
449  }
450  else {
451  prev->next = new_element;
452  if (current) { //not extracted
453  new_element->next = current;
454  if (next == current)

◆ add_before_then_move()

void CLIST_ITERATOR::add_before_then_move ( void *  new_data)
inline

Definition at line 375 of file clst.h.

385  {
386  CLIST_LINK *new_element;
387 
388  #ifndef NDEBUG
389  if (!list)
390  NO_LIST.error ("CLIST_ITERATOR::add_before_then_move", ABORT, nullptr);
391  if (!new_data)
392  BAD_PARAMETER.error ("CLIST_ITERATOR::add_before_then_move", ABORT,
393  "new_data is nullptr");
394  #endif
395 
396  new_element = new CLIST_LINK;
397  new_element->data = new_data;
398 
399  if (list->empty ()) {
400  new_element->next = new_element;
401  list->last = new_element;
402  prev = next = new_element;
403  }
404  else {
405  prev->next = new_element;
406  if (current) { //not extracted
407  new_element->next = current;
408  next = current;
409  }
410  else { //current extracted

◆ add_list_after()

void CLIST_ITERATOR::add_list_after ( CLIST list_to_add)
inline

Definition at line 463 of file clst.h.

474  {
475  #ifndef NDEBUG
476  if (!list)
477  NO_LIST.error ("CLIST_ITERATOR::add_list_after", ABORT, nullptr);
478  if (!list_to_add)
479  BAD_PARAMETER.error ("CLIST_ITERATOR::add_list_after", ABORT,
480  "list_to_add is nullptr");
481  #endif
482 
483  if (!list_to_add->empty ()) {
484  if (list->empty ()) {
485  list->last = list_to_add->last;
486  prev = list->last;
487  next = list->First ();
488  ex_current_was_last = true;
489  current = nullptr;
490  }
491  else {
492  if (current) { //not extracted
493  current->next = list_to_add->First ();
494  if (current == list->last)
495  list->last = list_to_add->last;
496  list_to_add->last->next = next;
497  next = current->next;
498  }
499  else { //current extracted
500  prev->next = list_to_add->First ();

◆ add_list_before()

void CLIST_ITERATOR::add_list_before ( CLIST list_to_add)
inline

Definition at line 509 of file clst.h.

521  {
522  #ifndef NDEBUG
523  if (!list)
524  NO_LIST.error ("CLIST_ITERATOR::add_list_before", ABORT, nullptr);
525  if (!list_to_add)
526  BAD_PARAMETER.error ("CLIST_ITERATOR::add_list_before", ABORT,
527  "list_to_add is nullptr");
528  #endif
529 
530  if (!list_to_add->empty ()) {
531  if (list->empty ()) {
532  list->last = list_to_add->last;
533  prev = list->last;
534  current = list->First ();
535  next = current->next;
536  ex_current_was_last = false;
537  }
538  else {
539  prev->next = list_to_add->First ();
540  if (current) { //not extracted
541  list_to_add->last->next = current;
542  }
543  else { //current extracted

◆ add_to_end()

void CLIST_ITERATOR::add_to_end ( void *  new_data)
inline

Definition at line 722 of file clst.h.

723  {
724  #ifndef NDEBUG
725  if (!list)
726  NO_LIST.error ("CLIST_ITERATOR::sort", ABORT, nullptr);
727  #endif
728 
729  list->sort (comparator);
730  move_to_first();
731 }
732 
733 /***********************************************************************
734  * CLIST_ITERATOR::add_to_end
735  *
736  * Add a new element to the end of the list without moving the iterator.
737  * This is provided because a single linked list cannot move to the last as
738  * the iterator couldn't set its prev pointer. Adding to the end is
739  * essential for implementing
740  queues.
741 **********************************************************************/
742 
743 inline void CLIST_ITERATOR::add_to_end( // element to add
744  void *new_data) {
745  CLIST_LINK *new_element;
746 
747  #ifndef NDEBUG
748  if (!list)
749  NO_LIST.error ("CLIST_ITERATOR::add_to_end", ABORT, nullptr);
750  if (!new_data)
751  BAD_PARAMETER.error ("CLIST_ITERATOR::add_to_end", ABORT,

◆ at_first()

bool CLIST_ITERATOR::at_first ( )
inline

Definition at line 633 of file clst.h.

◆ at_last()

bool CLIST_ITERATOR::at_last ( )
inline

Definition at line 651 of file clst.h.

◆ current_extracted()

bool CLIST_ITERATOR::current_extracted ( )
inline

Definition at line 221 of file clst.h.

221  { //current extracted?
222  return !current;
223  }

◆ cycled_list()

bool CLIST_ITERATOR::cycled_list ( )
inline

Definition at line 669 of file clst.h.

◆ data()

void* CLIST_ITERATOR::data ( )
inline

Definition at line 190 of file clst.h.

190  { //get current data
191  #ifndef NDEBUG
192  if (!list)
193  NO_LIST.error ("CLIST_ITERATOR::data", ABORT, nullptr);
194  if (!current)
195  NULL_DATA.error ("CLIST_ITERATOR::data", ABORT, nullptr);
196  #endif
197  return current->data;
198  }

◆ data_relative()

void * CLIST_ITERATOR::data_relative ( int8_t  offset)

Definition at line 275 of file clst.cpp.

285  { //offset from current
286  CLIST_LINK *ptr;
287 
288  #ifndef NDEBUG
289  if (!list)
290  NO_LIST.error ("CLIST_ITERATOR::data_relative", ABORT, nullptr);
291  if (list->empty ())
292  EMPTY_LIST.error ("CLIST_ITERATOR::data_relative", ABORT, nullptr);
293  if (offset < -1)
294  BAD_PARAMETER.error ("CLIST_ITERATOR::data_relative", ABORT,
295  "offset < -l");
296  #endif
297 
298  if (offset == -1)
299  ptr = prev;
300  else

◆ empty()

bool CLIST_ITERATOR::empty ( )
inline

Definition at line 213 of file clst.h.

213  { //is list empty?
214  #ifndef NDEBUG
215  if (!list)
216  NO_LIST.error ("CLIST_ITERATOR::empty", ABORT, nullptr);
217  #endif
218  return list->empty ();
219  }

◆ exchange()

void CLIST_ITERATOR::exchange ( CLIST_ITERATOR other_it)

Definition at line 333 of file clst.cpp.

345  { //other iterator
346  constexpr ERRCODE DONT_EXCHANGE_DELETED(
347  "Can't exchange deleted elements of lists");
348 
349  CLIST_LINK *old_current;
350 
351  #ifndef NDEBUG
352  if (!list)
353  NO_LIST.error ("CLIST_ITERATOR::exchange", ABORT, nullptr);
354  if (!other_it)
355  BAD_PARAMETER.error ("CLIST_ITERATOR::exchange", ABORT, "other_it nullptr");
356  if (!(other_it->list))
357  NO_LIST.error ("CLIST_ITERATOR::exchange", ABORT, "other_it");
358  #endif
359 
360  /* Do nothing if either list is empty or if both iterators reference the same
361  link */
362 
363  if ((list->empty ()) ||
364  (other_it->list->empty ()) || (current == other_it->current))
365  return;
366 
367  /* Error if either current element is deleted */
368 
369  if (!current || !other_it->current)
370  DONT_EXCHANGE_DELETED.error ("CLIST_ITERATOR.exchange", ABORT, nullptr);
371 
372  /* Now handle the 4 cases: doubleton list; non-doubleton adjacent elements
373  (other before this); non-doubleton adjacent elements (this before other);
374  non-adjacent elements. */
375 
376  //adjacent links
377  if ((next == other_it->current) ||
378  (other_it->next == current)) {
379  //doubleton list
380  if ((next == other_it->current) &&
381  (other_it->next == current)) {
382  prev = next = current;
383  other_it->prev = other_it->next = other_it->current;
384  }
385  else { //non-doubleton with
386  //adjacent links
387  //other before this
388  if (other_it->next == current) {
389  other_it->prev->next = current;
390  other_it->current->next = next;
391  current->next = other_it->current;
392  other_it->next = other_it->current;
393  prev = current;
394  }
395  else { //this before other
396  prev->next = other_it->current;
397  current->next = other_it->next;
398  other_it->current->next = current;
399  next = current;
400  other_it->prev = other_it->current;
401  }
402  }
403  }
404  else { //no overlap
405  prev->next = other_it->current;
406  current->next = other_it->next;
407  other_it->prev->next = current;
408  other_it->current->next = next;
409  }
410 
411  /* update end of list pointer when necessary (remember that the 2 iterators
412  may iterate over different lists!) */
413 
414  if (list->last == current)
415  list->last = other_it->current;
416  if (other_it->list->last == other_it->current)
417  other_it->list->last = current;
418 

◆ extract()

void * CLIST_ITERATOR::extract ( )
inline

Definition at line 553 of file clst.h.

566  {
567  void *extracted_data;
568 
569  #ifndef NDEBUG
570  if (!list)
571  NO_LIST.error ("CLIST_ITERATOR::extract", ABORT, nullptr);
572  if (!current) //list empty or
573  //element extracted
574  NULL_CURRENT.error ("CLIST_ITERATOR::extract",
575  ABORT, nullptr);
576  #endif
577 
578  if (list->singleton()) {
579  // Special case where we do need to change the iterator.
580  prev = next = list->last = nullptr;
581  } else {
582  prev->next = next; //remove from list
583 
584  if (current == list->last) {

◆ forward()

void * CLIST_ITERATOR::forward ( )

Definition at line 236 of file clst.cpp.

244  {
245  #ifndef NDEBUG
246  if (!list)
247  NO_LIST.error ("CLIST_ITERATOR::forward", ABORT, nullptr);
248  #endif
249  if (list->empty ())
250  return nullptr;
251 
252  if (current) { //not removed so
253  //set previous
254  prev = current;
255  started_cycling = true;
256  // In case next is deleted by another iterator, get next from current.
257  current = current->next;
258  } else {
259  if (ex_current_was_cycle_pt)
260  cycle_pt = next;
261  current = next;
262  }
263 
264  #ifndef NDEBUG
265  if (!current)
266  NULL_DATA.error ("CLIST_ITERATOR::forward", ABORT, nullptr);

◆ length()

int32_t CLIST_ITERATOR::length ( )
inline

Definition at line 685 of file clst.h.

687  {
688  #ifndef NDEBUG
689  if (!list)
690  NO_LIST.error ("CLIST_ITERATOR::cycled_list", ABORT, nullptr);
691  #endif
692 

◆ mark_cycle_pt()

void CLIST_ITERATOR::mark_cycle_pt ( )
inline

Definition at line 614 of file clst.h.

614  : nullptr;
615  return current != nullptr ? current->data : nullptr;
616 }
617 
618 /***********************************************************************
619  * CLIST_ITERATOR::mark_cycle_pt()
620  *
621  * Remember the current location so that we can tell whether we've returned
622  * to this point later.
623  *
624  * If the current point is deleted either now, or in the future, the cycle
625  * point will be set to the next item which is set to current. This could be

◆ move_to_first()

void * CLIST_ITERATOR::move_to_first ( )
inline

Definition at line 592 of file clst.h.

◆ move_to_last()

void * CLIST_ITERATOR::move_to_last ( )

Definition at line 309 of file clst.cpp.

319  {
320  #ifndef NDEBUG
321  if (!list)
322  NO_LIST.error ("CLIST_ITERATOR::move_to_last", ABORT, nullptr);

◆ set_to_list()

void CLIST_ITERATOR::set_to_list ( CLIST list_to_iterate)
inline

Definition at line 247 of file clst.h.

253  {
254  #ifndef NDEBUG
255  if (!list_to_iterate)
256  BAD_PARAMETER.error ("CLIST_ITERATOR::set_to_list", ABORT,
257  "list_to_iterate is nullptr");
258  #endif
259 
260  list = list_to_iterate;
261  prev = list->last;
262  current = list->First ();
263  next = current != nullptr ? current->next : nullptr;

◆ sort()

void CLIST_ITERATOR::sort ( int   comparator const void *, const void *)
inline

Definition at line 701 of file clst.h.

704  {
705  #ifndef NDEBUG
706  if (!list)
707  NO_LIST.error ("CLIST_ITERATOR::length", ABORT, nullptr);
708  #endif
709 
710  return list->length ();
711 }

Friends And Related Function Documentation

◆ CLIST::assign_to_sublist


The documentation for this class was generated from the following files:
BAD_PARAMETER
constexpr ERRCODE BAD_PARAMETER("List parameter error")
CLIST::sort
void sort(int comparator(const void *, const void *))
Definition: clst.cpp:124
ERRCODE
Definition: errcode.h:67
NULL_CURRENT
constexpr ERRCODE NULL_CURRENT("List current position is nullptr")
CLIST_LINK
Definition: clst.h:38
CLIST::empty
bool empty() const
Definition: clst.h:95
ERRCODE::error
void error(const char *caller, TessErrorLogCode action, const char *format,...) const
Definition: errcode.cpp:33
NULL_DATA
constexpr ERRCODE NULL_DATA("List would have returned a nullptr data pointer")
CLIST::singleton
bool singleton() const
Definition: clst.h:99
CLIST_ITERATOR::move_to_first
void * move_to_first()
Definition: clst.h:592
CLIST::length
int32_t length() const
Definition: clst.cpp:109
EMPTY_LIST
constexpr ERRCODE EMPTY_LIST("List is empty")
CLIST_ITERATOR::add_to_end
void add_to_end(void *new_data)
Definition: clst.h:722
ABORT
Definition: errcode.h:43
NO_LIST
constexpr ERRCODE NO_LIST("Iterator not set to a list")