tesseract  5.0.0-alpha-619-ge9db
ELIST_ITERATOR Class Reference

#include <elst.h>

Public Member Functions

 ELIST_ITERATOR ()
 
 ELIST_ITERATOR (ELIST *list_to_iterate)
 
void set_to_list (ELIST *list_to_iterate)
 
void add_after_then_move (ELIST_LINK *new_link)
 
void add_after_stay_put (ELIST_LINK *new_link)
 
void add_before_then_move (ELIST_LINK *new_link)
 
void add_before_stay_put (ELIST_LINK *new_link)
 
void add_list_after (ELIST *list_to_add)
 
void add_list_before (ELIST *list_to_add)
 
ELIST_LINKdata ()
 
ELIST_LINKdata_relative (int8_t offset)
 
ELIST_LINKforward ()
 
ELIST_LINKextract ()
 
ELIST_LINKmove_to_first ()
 
ELIST_LINKmove_to_last ()
 
void mark_cycle_pt ()
 
bool empty ()
 
bool current_extracted ()
 
bool at_first ()
 
bool at_last ()
 
bool cycled_list ()
 
void add_to_end (ELIST_LINK *new_link)
 
void exchange (ELIST_ITERATOR *other_it)
 
int32_t length ()
 
void sort (int comparator(const void *, const void *))
 

Friends

void ELIST::assign_to_sublist (ELIST_ITERATOR *, ELIST_ITERATOR *)
 

Detailed Description

Definition at line 175 of file elst.h.

Constructor & Destructor Documentation

◆ ELIST_ITERATOR() [1/2]

ELIST_ITERATOR::ELIST_ITERATOR ( )
inline

Definition at line 195 of file elst.h.

196  :
197  ELIST_ITERATOR() { //constructor

◆ ELIST_ITERATOR() [2/2]

ELIST_ITERATOR::ELIST_ITERATOR ( ELIST list_to_iterate)
inlineexplicit

Definition at line 304 of file elst.h.

Member Function Documentation

◆ add_after_stay_put()

void ELIST_ITERATOR::add_after_stay_put ( ELIST_LINK new_link)
inline

Definition at line 359 of file elst.h.

369  {
370  #ifndef NDEBUG
371  if (!list)
372  NO_LIST.error ("ELIST_ITERATOR::add_after_stay_put", ABORT, nullptr);
373  if (!new_element)
374  BAD_PARAMETER.error ("ELIST_ITERATOR::add_after_stay_put", ABORT,
375  "new_element is nullptr");
376  if (new_element->next)
377  STILL_LINKED.error ("ELIST_ITERATOR::add_after_stay_put", ABORT, nullptr);
378  #endif
379 
380  if (list->empty ()) {
381  new_element->next = new_element;
382  list->last = new_element;
383  prev = next = new_element;
384  ex_current_was_last = false;
385  current = nullptr;
386  }
387  else {
388  new_element->next = next;
389 
390  if (current) { //not extracted
391  current->next = new_element;
392  if (prev == current)
393  prev = new_element;
394  if (current == list->last)
395  list->last = new_element;
396  }
397  else { //current extracted

◆ add_after_then_move()

void ELIST_ITERATOR::add_after_then_move ( ELIST_LINK new_link)
inline

Definition at line 315 of file elst.h.

324  {
325  #ifndef NDEBUG
326  if (!list)
327  NO_LIST.error ("ELIST_ITERATOR::add_after_then_move", ABORT, nullptr);
328  if (!new_element)
329  BAD_PARAMETER.error ("ELIST_ITERATOR::add_after_then_move", ABORT,
330  "new_element is nullptr");
331  if (new_element->next)
332  STILL_LINKED.error ("ELIST_ITERATOR::add_after_then_move", ABORT, nullptr);
333  #endif
334 
335  if (list->empty ()) {
336  new_element->next = new_element;
337  list->last = new_element;
338  prev = next = new_element;
339  }
340  else {
341  new_element->next = next;
342 
343  if (current) { //not extracted
344  current->next = new_element;
345  prev = current;
346  if (current == list->last)
347  list->last = new_element;
348  }
349  else { //current extracted
350  prev->next = new_element;

◆ add_before_stay_put()

void ELIST_ITERATOR::add_before_stay_put ( ELIST_LINK new_link)
inline

Definition at line 446 of file elst.h.

458  {
459  #ifndef NDEBUG
460  if (!list)
461  NO_LIST.error ("ELIST_ITERATOR::add_before_stay_put", ABORT, nullptr);
462  if (!new_element)
463  BAD_PARAMETER.error ("ELIST_ITERATOR::add_before_stay_put", ABORT,
464  "new_element is nullptr");
465  if (new_element->next)
466  STILL_LINKED.error ("ELIST_ITERATOR::add_before_stay_put", ABORT, nullptr);
467  #endif
468 
469  if (list->empty ()) {
470  new_element->next = new_element;
471  list->last = new_element;
472  prev = next = new_element;
473  ex_current_was_last = true;
474  current = nullptr;
475  }
476  else {
477  prev->next = new_element;
478  if (current) { //not extracted
479  new_element->next = current;

◆ add_before_then_move()

void ELIST_ITERATOR::add_before_then_move ( ELIST_LINK new_link)
inline

Definition at line 406 of file elst.h.

417  {
418  #ifndef NDEBUG
419  if (!list)
420  NO_LIST.error ("ELIST_ITERATOR::add_before_then_move", ABORT, nullptr);
421  if (!new_element)
422  BAD_PARAMETER.error ("ELIST_ITERATOR::add_before_then_move", ABORT,
423  "new_element is nullptr");
424  if (new_element->next)
425  STILL_LINKED.error ("ELIST_ITERATOR::add_before_then_move", ABORT, nullptr);
426  #endif
427 
428  if (list->empty ()) {
429  new_element->next = new_element;
430  list->last = new_element;
431  prev = next = new_element;
432  }
433  else {
434  prev->next = new_element;
435  if (current) { //not extracted
436  new_element->next = current;
437  next = current;
438  }

◆ add_list_after()

void ELIST_ITERATOR::add_list_after ( ELIST list_to_add)
inline

Definition at line 488 of file elst.h.

500  {
501  #ifndef NDEBUG
502  if (!list)
503  NO_LIST.error ("ELIST_ITERATOR::add_list_after", ABORT, nullptr);
504  if (!list_to_add)
505  BAD_PARAMETER.error ("ELIST_ITERATOR::add_list_after", ABORT,
506  "list_to_add is nullptr");
507  #endif
508 
509  if (!list_to_add->empty ()) {
510  if (list->empty ()) {
511  list->last = list_to_add->last;
512  prev = list->last;
513  next = list->First ();
514  ex_current_was_last = true;
515  current = nullptr;
516  }
517  else {
518  if (current) { //not extracted
519  current->next = list_to_add->First ();
520  if (current == list->last)
521  list->last = list_to_add->last;
522  list_to_add->last->next = next;
523  next = current->next;
524  }
525  else { //current extracted

◆ add_list_before()

void ELIST_ITERATOR::add_list_before ( ELIST list_to_add)
inline

Definition at line 535 of file elst.h.

548  {
549  #ifndef NDEBUG
550  if (!list)
551  NO_LIST.error ("ELIST_ITERATOR::add_list_before", ABORT, nullptr);
552  if (!list_to_add)
553  BAD_PARAMETER.error ("ELIST_ITERATOR::add_list_before", ABORT,
554  "list_to_add is nullptr");
555  #endif
556 
557  if (!list_to_add->empty ()) {
558  if (list->empty ()) {
559  list->last = list_to_add->last;
560  prev = list->last;
561  current = list->First ();
562  next = current->next;
563  ex_current_was_last = false;
564  }
565  else {
566  prev->next = list_to_add->First ();
567  if (current) { //not extracted
568  list_to_add->last->next = current;
569  }

◆ add_to_end()

void ELIST_ITERATOR::add_to_end ( ELIST_LINK new_link)
inline

Definition at line 753 of file elst.h.

754  {
755  #ifndef NDEBUG
756  if (!list)
757  NO_LIST.error ("ELIST_ITERATOR::sort", ABORT, nullptr);
758  #endif
759 
760  list->sort (comparator);
761  move_to_first();
762 }
763 
764 
765 /***********************************************************************
766  * ELIST_ITERATOR::add_to_end
767  *
768  * Add a new element to the end of the list without moving the iterator.
769  * This is provided because a single linked list cannot move to the last as
770  * the iterator couldn't set its prev pointer. Adding to the end is
771  * essential for implementing
772  queues.
773 **********************************************************************/
774 
775 inline void ELIST_ITERATOR::add_to_end( // element to add
776  ELIST_LINK *new_element) {
777  #ifndef NDEBUG
778  if (!list)
779  NO_LIST.error ("ELIST_ITERATOR::add_to_end", ABORT, nullptr);

◆ at_first()

bool ELIST_ITERATOR::at_first ( )
inline

Definition at line 659 of file elst.h.

◆ at_last()

bool ELIST_ITERATOR::at_last ( )
inline

Definition at line 678 of file elst.h.

◆ current_extracted()

bool ELIST_ITERATOR::current_extracted ( )
inline

Definition at line 253 of file elst.h.

255  { //current extracted?

◆ cycled_list()

bool ELIST_ITERATOR::cycled_list ( )
inline

Definition at line 697 of file elst.h.

◆ data()

ELIST_LINK* ELIST_ITERATOR::data ( )
inline

Definition at line 222 of file elst.h.

224  { //get current data
225  #ifndef NDEBUG
226  if (!list)
227  NO_LIST.error ("ELIST_ITERATOR::data", ABORT, nullptr);
228  if (!current)
229  NULL_DATA.error ("ELIST_ITERATOR::data", ABORT, nullptr);
230  #endif

◆ data_relative()

ELIST_LINK * ELIST_ITERATOR::data_relative ( int8_t  offset)

Definition at line 225 of file elst.cpp.

235  { //offset from current
236  ELIST_LINK *ptr;
237 
238  #ifndef NDEBUG
239  if (!list)
240  NO_LIST.error ("ELIST_ITERATOR::data_relative", ABORT, nullptr);
241  if (list->empty ())
242  EMPTY_LIST.error ("ELIST_ITERATOR::data_relative", ABORT, nullptr);
243  if (offset < -1)
244  BAD_PARAMETER.error ("ELIST_ITERATOR::data_relative", ABORT,
245  "offset < -l");
246  #endif
247 
248  if (offset == -1)
249  ptr = prev;
250  else

◆ empty()

bool ELIST_ITERATOR::empty ( )
inline

Definition at line 245 of file elst.h.

247  { //is list empty?
248  #ifndef NDEBUG
249  if (!list)
250  NO_LIST.error ("ELIST_ITERATOR::empty", ABORT, nullptr);
251  #endif

◆ exchange()

void ELIST_ITERATOR::exchange ( ELIST_ITERATOR other_it)

Definition at line 280 of file elst.cpp.

292  { //other iterator
293  constexpr ERRCODE DONT_EXCHANGE_DELETED(
294  "Can't exchange deleted elements of lists");
295 
296  ELIST_LINK *old_current;
297 
298  #ifndef NDEBUG
299  if (!list)
300  NO_LIST.error ("ELIST_ITERATOR::exchange", ABORT, nullptr);
301  if (!other_it)
302  BAD_PARAMETER.error ("ELIST_ITERATOR::exchange", ABORT, "other_it nullptr");
303  if (!(other_it->list))
304  NO_LIST.error ("ELIST_ITERATOR::exchange", ABORT, "other_it");
305  #endif
306 
307  /* Do nothing if either list is empty or if both iterators reference the same
308  link */
309 
310  if ((list->empty ()) ||
311  (other_it->list->empty ()) || (current == other_it->current))
312  return;
313 
314  /* Error if either current element is deleted */
315 
316  if (!current || !other_it->current)
317  DONT_EXCHANGE_DELETED.error ("ELIST_ITERATOR.exchange", ABORT, nullptr);
318 
319  /* Now handle the 4 cases: doubleton list; non-doubleton adjacent elements
320  (other before this); non-doubleton adjacent elements (this before other);
321  non-adjacent elements. */
322 
323  //adjacent links
324  if ((next == other_it->current) ||
325  (other_it->next == current)) {
326  //doubleton list
327  if ((next == other_it->current) &&
328  (other_it->next == current)) {
329  prev = next = current;
330  other_it->prev = other_it->next = other_it->current;
331  }
332  else { //non-doubleton with
333  //adjacent links
334  //other before this
335  if (other_it->next == current) {
336  other_it->prev->next = current;
337  other_it->current->next = next;
338  current->next = other_it->current;
339  other_it->next = other_it->current;
340  prev = current;
341  }
342  else { //this before other
343  prev->next = other_it->current;
344  current->next = other_it->next;
345  other_it->current->next = current;
346  next = current;
347  other_it->prev = other_it->current;
348  }
349  }
350  }
351  else { //no overlap
352  prev->next = other_it->current;
353  current->next = other_it->next;
354  other_it->prev->next = current;
355  other_it->current->next = next;
356  }
357 
358  /* update end of list pointer when necessary (remember that the 2 iterators
359  may iterate over different lists!) */
360 
361  if (list->last == current)
362  list->last = other_it->current;
363  if (other_it->list->last == other_it->current)
364  other_it->list->last = current;
365 

◆ extract()

ELIST_LINK * ELIST_ITERATOR::extract ( )
inline

Definition at line 580 of file elst.h.

594  {
595  ELIST_LINK *extracted_link;
596 
597  #ifndef NDEBUG
598  if (!list)
599  NO_LIST.error ("ELIST_ITERATOR::extract", ABORT, nullptr);
600  if (!current) //list empty or
601  //element extracted
602  NULL_CURRENT.error ("ELIST_ITERATOR::extract",
603  ABORT, nullptr);
604  #endif
605 
606  if (list->singleton()) {
607  // Special case where we do need to change the iterator.

◆ forward()

ELIST_LINK * ELIST_ITERATOR::forward ( )

Definition at line 185 of file elst.cpp.

193  {
194  #ifndef NDEBUG
195  if (!list)
196  NO_LIST.error ("ELIST_ITERATOR::forward", ABORT, nullptr);
197  #endif
198  if (list->empty ())
199  return nullptr;
200 
201  if (current) { //not removed so
202  //set previous
203  prev = current;
204  started_cycling = true;
205  // In case next is deleted by another iterator, get next from current.
206  current = current->next;
207  } else {
208  if (ex_current_was_cycle_pt)
209  cycle_pt = next;
210  current = next;
211  }
212 #ifndef NDEBUG
213  if (!current)
214  NULL_DATA.error ("ELIST_ITERATOR::forward", ABORT, nullptr);
215 #endif
216  next = current->next;

◆ length()

int32_t ELIST_ITERATOR::length ( )
inline

Definition at line 714 of file elst.h.

716  {
717  #ifndef NDEBUG
718  if (!list)
719  NO_LIST.error ("ELIST_ITERATOR::cycled_list", ABORT, nullptr);
720  #endif
721 

◆ mark_cycle_pt()

void ELIST_ITERATOR::mark_cycle_pt ( )
inline

Definition at line 639 of file elst.h.

639  : nullptr;
640  return current;
641 }
642 
643 
644 /***********************************************************************
645  * ELIST_ITERATOR::mark_cycle_pt()
646  *
647  * Remember the current location so that we can tell whether we've returned
648  * to this point later.
649  *
650  * If the current point is deleted either now, or in the future, the cycle

◆ move_to_first()

ELIST_LINK * ELIST_ITERATOR::move_to_first ( )
inline

Definition at line 616 of file elst.h.

◆ move_to_last()

ELIST_LINK * ELIST_ITERATOR::move_to_last ( )

Definition at line 259 of file elst.cpp.

269  {

◆ set_to_list()

void ELIST_ITERATOR::set_to_list ( ELIST list_to_iterate)
inline

Definition at line 280 of file elst.h.

287  {
288  #ifndef NDEBUG
289  if (!list_to_iterate)
290  BAD_PARAMETER.error ("ELIST_ITERATOR::set_to_list", ABORT,
291  "list_to_iterate is nullptr");
292  #endif
293 
294  list = list_to_iterate;
295  prev = list->last;
296  current = list->First ();

◆ sort()

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

Definition at line 731 of file elst.h.

734  {
735  #ifndef NDEBUG
736  if (!list)
737  NO_LIST.error ("ELIST_ITERATOR::length", ABORT, nullptr);
738  #endif
739 
740  return list->length ();
741 }

Friends And Related Function Documentation

◆ ELIST::assign_to_sublist


The documentation for this class was generated from the following files:
BAD_PARAMETER
constexpr ERRCODE BAD_PARAMETER("List parameter error")
ERRCODE
Definition: errcode.h:67
NULL_CURRENT
constexpr ERRCODE NULL_CURRENT("List current position is nullptr")
ELIST::length
int32_t length() const
Definition: elst.cpp:84
ELIST::sort
void sort(int comparator(const void *, const void *))
Definition: elst.cpp:101
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")
ELIST::singleton
bool singleton() const
Definition: elst.h:128
EMPTY_LIST
constexpr ERRCODE EMPTY_LIST("List is empty")
STILL_LINKED
constexpr ERRCODE STILL_LINKED("Attempting to add an element with non nullptr links, to a list")
ELIST_ITERATOR::add_to_end
void add_to_end(ELIST_LINK *new_link)
Definition: elst.h:753
ELIST_LINK
Definition: elst.h:74
ABORT
Definition: errcode.h:43
ELIST_ITERATOR::move_to_first
ELIST_LINK * move_to_first()
Definition: elst.h:616
NO_LIST
constexpr ERRCODE NO_LIST("Iterator not set to a list")
ELIST_ITERATOR::ELIST_ITERATOR
ELIST_ITERATOR()
Definition: elst.h:195
ELIST::empty
bool empty() const
Definition: elst.h:124