tesseract  5.0.0-alpha-619-ge9db
ELIST2_ITERATOR Class Reference

#include <elst2.h>

Public Member Functions

 ELIST2_ITERATOR (ELIST2 *list_to_iterate)
 
void set_to_list (ELIST2 *list_to_iterate)
 
void add_after_then_move (ELIST2_LINK *new_link)
 
void add_after_stay_put (ELIST2_LINK *new_link)
 
void add_before_then_move (ELIST2_LINK *new_link)
 
void add_before_stay_put (ELIST2_LINK *new_link)
 
void add_list_after (ELIST2 *list_to_add)
 
void add_list_before (ELIST2 *list_to_add)
 
ELIST2_LINKdata ()
 
ELIST2_LINKdata_relative (int8_t offset)
 
ELIST2_LINKforward ()
 
ELIST2_LINKbackward ()
 
ELIST2_LINKextract ()
 
ELIST2_LINKmove_to_first ()
 
ELIST2_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 (ELIST2_LINK *new_link)
 
void exchange (ELIST2_ITERATOR *other_it)
 
int32_t length ()
 
void sort (int comparator(const void *, const void *))
 

Friends

void ELIST2::assign_to_sublist (ELIST2_ITERATOR *, ELIST2_ITERATOR *)
 

Detailed Description

Definition at line 144 of file elst2.h.

Constructor & Destructor Documentation

◆ ELIST2_ITERATOR()

ELIST2_ITERATOR::ELIST2_ITERATOR ( ELIST2 list_to_iterate)
inline

Definition at line 275 of file elst2.h.

Member Function Documentation

◆ add_after_stay_put()

void ELIST2_ITERATOR::add_after_stay_put ( ELIST2_LINK new_link)
inline

Definition at line 332 of file elst2.h.

342  {
343  #ifndef NDEBUG
344  if (!list)
345  NO_LIST.error ("ELIST2_ITERATOR::add_after_stay_put", ABORT, nullptr);
346  if (!new_element)
347  BAD_PARAMETER.error ("ELIST2_ITERATOR::add_after_stay_put", ABORT,
348  "new_element is nullptr");
349  if (new_element->next)
350  STILL_LINKED.error ("ELIST2_ITERATOR::add_after_stay_put", ABORT, nullptr);
351  #endif
352 
353  if (list->empty ()) {
354  new_element->next = new_element;
355  new_element->prev = new_element;
356  list->last = new_element;
357  prev = next = new_element;
358  ex_current_was_last = false;
359  current = nullptr;
360  }
361  else {
362  new_element->next = next;
363  next->prev = new_element;
364 
365  if (current) { //not extracted
366  new_element->prev = current;
367  current->next = new_element;
368  if (prev == current)
369  prev = new_element;
370  if (current == list->last)
371  list->last = new_element;
372  }
373  else { //current extracted
374  new_element->prev = prev;

◆ add_after_then_move()

void ELIST2_ITERATOR::add_after_then_move ( ELIST2_LINK new_link)
inline

Definition at line 285 of file elst2.h.

294  {
295  #ifndef NDEBUG
296  if (!list)
297  NO_LIST.error ("ELIST2_ITERATOR::add_after_then_move", ABORT, nullptr);
298  if (!new_element)
299  BAD_PARAMETER.error ("ELIST2_ITERATOR::add_after_then_move", ABORT,
300  "new_element is nullptr");
301  if (new_element->next)
302  STILL_LINKED.error ("ELIST2_ITERATOR::add_after_then_move", ABORT, nullptr);
303  #endif
304 
305  if (list->empty ()) {
306  new_element->next = new_element;
307  new_element->prev = new_element;
308  list->last = new_element;
309  prev = next = new_element;
310  }
311  else {
312  new_element->next = next;
313  next->prev = new_element;
314 
315  if (current) { //not extracted
316  new_element->prev = current;
317  current->next = new_element;
318  prev = current;
319  if (current == list->last)
320  list->last = new_element;
321  }
322  else { //current extracted
323  new_element->prev = prev;
324  prev->next = new_element;

◆ add_before_stay_put()

void ELIST2_ITERATOR::add_before_stay_put ( ELIST2_LINK new_link)
inline

Definition at line 427 of file elst2.h.

439  {
440  #ifndef NDEBUG
441  if (!list)
442  NO_LIST.error ("ELIST2_ITERATOR::add_before_stay_put", ABORT, nullptr);
443  if (!new_element)
444  BAD_PARAMETER.error ("ELIST2_ITERATOR::add_before_stay_put", ABORT,
445  "new_element is nullptr");
446  if (new_element->next)
447  STILL_LINKED.error ("ELIST2_ITERATOR::add_before_stay_put", ABORT, nullptr);
448  #endif
449 
450  if (list->empty ()) {
451  new_element->next = new_element;
452  new_element->prev = new_element;
453  list->last = new_element;
454  prev = next = new_element;
455  ex_current_was_last = true;
456  current = nullptr;
457  }
458  else {
459  prev->next = new_element;
460  new_element->prev = prev;
461 
462  if (current) { //not extracted
463  new_element->next = current;
464  current->prev = new_element;
465  if (next == current)

◆ add_before_then_move()

void ELIST2_ITERATOR::add_before_then_move ( ELIST2_LINK new_link)
inline

Definition at line 382 of file elst2.h.

393  {
394  #ifndef NDEBUG
395  if (!list)
396  NO_LIST.error ("ELIST2_ITERATOR::add_before_then_move", ABORT, nullptr);
397  if (!new_element)
398  BAD_PARAMETER.error ("ELIST2_ITERATOR::add_before_then_move", ABORT,
399  "new_element is nullptr");
400  if (new_element->next)
401  STILL_LINKED.error ("ELIST2_ITERATOR::add_before_then_move", ABORT, nullptr);
402  #endif
403 
404  if (list->empty ()) {
405  new_element->next = new_element;
406  new_element->prev = new_element;
407  list->last = new_element;
408  prev = next = new_element;
409  }
410  else {
411  prev->next = new_element;
412  new_element->prev = prev;
413 
414  if (current) { //not extracted
415  new_element->next = current;
416  current->prev = new_element;
417  next = current;
418  }
419  else { //current extracted

◆ add_list_after()

void ELIST2_ITERATOR::add_list_after ( ELIST2 list_to_add)
inline

Definition at line 474 of file elst2.h.

486  {
487  #ifndef NDEBUG
488  if (!list)
489  NO_LIST.error ("ELIST2_ITERATOR::add_list_after", ABORT, nullptr);
490  if (!list_to_add)
491  BAD_PARAMETER.error ("ELIST2_ITERATOR::add_list_after", ABORT,
492  "list_to_add is nullptr");
493  #endif
494 
495  if (!list_to_add->empty ()) {
496  if (list->empty ()) {
497  list->last = list_to_add->last;
498  prev = list->last;
499  next = list->First ();
500  ex_current_was_last = true;
501  current = nullptr;
502  }
503  else {
504  if (current) { //not extracted
505  current->next = list_to_add->First ();
506  current->next->prev = current;
507  if (current == list->last)
508  list->last = list_to_add->last;
509  list_to_add->last->next = next;
510  next->prev = list_to_add->last;
511  next = current->next;
512  }
513  else { //current extracted
514  prev->next = list_to_add->First ();
515  prev->next->prev = prev;

◆ add_list_before()

void ELIST2_ITERATOR::add_list_before ( ELIST2 list_to_add)
inline

Definition at line 524 of file elst2.h.

537  {
538  #ifndef NDEBUG
539  if (!list)
540  NO_LIST.error ("ELIST2_ITERATOR::add_list_before", ABORT, nullptr);
541  if (!list_to_add)
542  BAD_PARAMETER.error ("ELIST2_ITERATOR::add_list_before", ABORT,
543  "list_to_add is nullptr");
544  #endif
545 
546  if (!list_to_add->empty ()) {
547  if (list->empty ()) {
548  list->last = list_to_add->last;
549  prev = list->last;
550  current = list->First ();
551  next = current->next;
552  ex_current_was_last = false;
553  }
554  else {
555  prev->next = list_to_add->First ();
556  prev->next->prev = prev;
557 
558  if (current) { //not extracted
559  list_to_add->last->next = current;
560  current->prev = list_to_add->last;
561  }
562  else { //current extracted

◆ add_to_end()

void ELIST2_ITERATOR::add_to_end ( ELIST2_LINK new_link)
inline

Definition at line 761 of file elst2.h.

764  {
765  #ifndef NDEBUG
766  if (!list)
767  NO_LIST.error ("ELIST2_ITERATOR::sort", ABORT, nullptr);
768  #endif
769 
770  list->sort (comparator);
771  move_to_first();
772 }
773 
774 /***********************************************************************
775  * ELIST2_ITERATOR::add_to_end
776  *
777  * Add a new element to the end of the list without moving the iterator.
778  * This is provided because a single linked list cannot move to the last as
779  * the iterator couldn't set its prev pointer. Adding to the end is
780  * essential for implementing
781  queues.
782 **********************************************************************/
783 
784 inline void ELIST2_ITERATOR::add_to_end( // element to add
785  ELIST2_LINK *new_element) {
786  #ifndef NDEBUG
787  if (!list)
788  NO_LIST.error ("ELIST2_ITERATOR::add_to_end", ABORT, nullptr);
789  if (!new_element)

◆ at_first()

bool ELIST2_ITERATOR::at_first ( )
inline

Definition at line 672 of file elst2.h.

◆ at_last()

bool ELIST2_ITERATOR::at_last ( )
inline

Definition at line 690 of file elst2.h.

690  {
691  #ifndef NDEBUG
692  if (!list)
693  NO_LIST.error ("ELIST2_ITERATOR::at_first", ABORT, nullptr);
694  #endif
695 
696  //we're at a deleted
697  return ((list->empty ()) || (current == list->First ()) || ((current == nullptr) &&
698  (prev == list->last) && //NON-last pt between
699  !ex_current_was_last)); //first and last
700 }

◆ backward()

ELIST2_LINK * ELIST2_ITERATOR::backward ( )

Definition at line 221 of file elst2.cpp.

229  {
230  #ifndef NDEBUG
231  if (!list)
232  NO_LIST.error ("ELIST2_ITERATOR::backward", ABORT, nullptr);
233  #endif
234  if (list->empty ())
235  return nullptr;
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 
249  #ifndef NDEBUG
250  if (!current)
251  NULL_DATA.error ("ELIST2_ITERATOR::backward", ABORT, nullptr);

◆ current_extracted()

bool ELIST2_ITERATOR::current_extracted ( )
inline

Definition at line 223 of file elst2.h.

224  { //current extracted?
225  return !current;

◆ cycled_list()

bool ELIST2_ITERATOR::cycled_list ( )
inline

Definition at line 708 of file elst2.h.

709  {
710  #ifndef NDEBUG
711  if (!list)
712  NO_LIST.error ("ELIST2_ITERATOR::at_last", ABORT, nullptr);
713  #endif
714 
715  //we're at a deleted
716  return ((list->empty ()) || (current == list->last) || ((current == nullptr) &&

◆ data()

ELIST2_LINK* ELIST2_ITERATOR::data ( )
inline

Definition at line 189 of file elst2.h.

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

◆ data_relative()

ELIST2_LINK * ELIST2_ITERATOR::data_relative ( int8_t  offset)

Definition at line 259 of file elst2.cpp.

269  { //offset from current
270  ELIST2_LINK *ptr;
271 
272  #ifndef NDEBUG
273  if (!list)
274  NO_LIST.error ("ELIST2_ITERATOR::data_relative", ABORT, nullptr);
275  if (list->empty ())
276  EMPTY_LIST.error ("ELIST2_ITERATOR::data_relative", ABORT, nullptr);
277  #endif
278 
279  if (offset < 0)
280  for (ptr = current ? current : next; offset++ < 0; ptr = ptr->prev);
281  else

◆ empty()

bool ELIST2_ITERATOR::empty ( )
inline

Definition at line 215 of file elst2.h.

216  { //is list empty?
217  #ifndef NDEBUG
218  if (!list)
219  NO_LIST.error ("ELIST2_ITERATOR::empty", ABORT, nullptr);
220  #endif
221  return list->empty ();

◆ exchange()

void ELIST2_ITERATOR::exchange ( ELIST2_ITERATOR other_it)

Definition at line 292 of file elst2.cpp.

303  { //other iterator
304  constexpr ERRCODE DONT_EXCHANGE_DELETED(
305  "Can't exchange deleted elements of lists");
306 
307  ELIST2_LINK *old_current;
308 
309  #ifndef NDEBUG
310  if (!list)
311  NO_LIST.error ("ELIST2_ITERATOR::exchange", ABORT, nullptr);
312  if (!other_it)
313  BAD_PARAMETER.error ("ELIST2_ITERATOR::exchange", ABORT, "other_it nullptr");
314  if (!(other_it->list))
315  NO_LIST.error ("ELIST2_ITERATOR::exchange", ABORT, "other_it");
316  #endif
317 
318  /* Do nothing if either list is empty or if both iterators reference the same
319  link */
320 
321  if ((list->empty ()) ||
322  (other_it->list->empty ()) || (current == other_it->current))
323  return;
324 
325  /* Error if either current element is deleted */
326 
327  if (!current || !other_it->current)
328  DONT_EXCHANGE_DELETED.error ("ELIST2_ITERATOR.exchange", ABORT, nullptr);
329 
330  /* Now handle the 4 cases: doubleton list; non-doubleton adjacent elements
331  (other before this); non-doubleton adjacent elements (this before other);
332  non-adjacent elements. */
333 
334  //adjacent links
335  if ((next == other_it->current) ||
336  (other_it->next == current)) {
337  //doubleton list
338  if ((next == other_it->current) &&
339  (other_it->next == current)) {
340  prev = next = current;
341  other_it->prev = other_it->next = other_it->current;
342  }
343  else { //non-doubleton with
344  //adjacent links
345  //other before this
346  if (other_it->next == current) {
347  other_it->prev->next = current;
348  other_it->current->next = next;
349  other_it->current->prev = current;
350  current->next = other_it->current;
351  current->prev = other_it->prev;
352  next->prev = other_it->current;
353 
354  other_it->next = other_it->current;
355  prev = current;
356  }
357  else { //this before other
358  prev->next = other_it->current;
359  current->next = other_it->next;
360  current->prev = other_it->current;
361  other_it->current->next = current;
362  other_it->current->prev = prev;
363  other_it->next->prev = current;
364 
365  next = current;
366  other_it->prev = other_it->current;
367  }
368  }
369  }
370  else { //no overlap
371  prev->next = other_it->current;
372  current->next = other_it->next;
373  current->prev = other_it->prev;
374  next->prev = other_it->current;
375  other_it->prev->next = current;
376  other_it->current->next = next;
377  other_it->current->prev = prev;
378  other_it->next->prev = current;
379  }
380 
381  /* update end of list pointer when necessary (remember that the 2 iterators
382  may iterate over different lists!) */
383 
384  if (list->last == current)
385  list->last = other_it->current;
386  if (other_it->list->last == other_it->current)
387  other_it->list->last = current;
388 
389  if (current == cycle_pt)

◆ extract()

ELIST2_LINK * ELIST2_ITERATOR::extract ( )
inline

Definition at line 572 of file elst2.h.

586  {
587  ELIST2_LINK *extracted_link;
588 
589  #ifndef NDEBUG
590  if (!list)
591  NO_LIST.error ("ELIST2_ITERATOR::extract", ABORT, nullptr);
592  if (!current) //list empty or
593  //element extracted
594  NULL_CURRENT.error ("ELIST2_ITERATOR::extract",
595  ABORT, nullptr);
596  #endif
597 
598  if (list->singleton()) {
599  // Special case where we do need to change the iterator.
600  prev = next = list->last = nullptr;
601  } else {
602  prev->next = next; //remove from list
603  next->prev = prev;
604 
605  if (current == list->last) {

◆ forward()

ELIST2_LINK * ELIST2_ITERATOR::forward ( )

Definition at line 178 of file elst2.cpp.

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

◆ length()

int32_t ELIST2_ITERATOR::length ( )
inline

Definition at line 724 of file elst2.h.

728  {
729  #ifndef NDEBUG
730  if (!list)
731  NO_LIST.error ("ELIST2_ITERATOR::cycled_list", ABORT, nullptr);

◆ mark_cycle_pt()

void ELIST2_ITERATOR::mark_cycle_pt ( )
inline

Definition at line 653 of file elst2.h.

654  : nullptr;
655  next = current ? current->next : nullptr;
656  return current;
657 }
658 
659 /***********************************************************************
660  * ELIST2_ITERATOR::mark_cycle_pt()
661  *
662  * Remember the current location so that we can tell whether we've returned
663  * to this point later.
664  *

◆ move_to_first()

ELIST2_LINK * ELIST2_ITERATOR::move_to_first ( )
inline

Definition at line 613 of file elst2.h.

◆ move_to_last()

ELIST2_LINK * ELIST2_ITERATOR::move_to_last ( )
inline

Definition at line 631 of file elst2.h.

636  : nullptr;
637  return current;
638 }
639 
640 /***********************************************************************
641  * ELIST2_ITERATOR::move_to_last()

◆ set_to_list()

void ELIST2_ITERATOR::set_to_list ( ELIST2 list_to_iterate)
inline

Definition at line 252 of file elst2.h.

259  {
260  #ifndef NDEBUG
261  if (!list_to_iterate)
262  BAD_PARAMETER.error ("ELIST2_ITERATOR::set_to_list", ABORT,
263  "list_to_iterate is nullptr");
264  #endif
265 
266  list = list_to_iterate;
267  prev = list->last;
268  current = list->First ();

◆ sort()

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

Definition at line 740 of file elst2.h.

745  {
746  #ifndef NDEBUG
747  if (!list)
748  NO_LIST.error ("ELIST2_ITERATOR::length", ABORT, nullptr);
749  #endif
750 

Friends And Related Function Documentation

◆ ELIST2::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")
ELIST2::sort
void sort(int comparator(const void *, const void *))
Definition: elst2.cpp:102
ELIST2::empty
bool empty() const
Definition: elst2.h:104
ELIST2_ITERATOR::move_to_first
ELIST2_LINK * move_to_first()
Definition: elst2.h:613
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")
ELIST2::singleton
bool singleton() const
Definition: elst2.h:108
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")
ELIST2_LINK
Definition: elst2.h:53
ABORT
Definition: errcode.h:43
NO_LIST
constexpr ERRCODE NO_LIST("Iterator not set to a list")
ELIST2_ITERATOR::add_to_end
void add_to_end(ELIST2_LINK *new_link)
Definition: elst2.h:761