tesseract  4.1.1
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 143 of file clst.h.

Constructor & Destructor Documentation

◆ CLIST_ITERATOR() [1/2]

CLIST_ITERATOR::CLIST_ITERATOR ( )
inline

Definition at line 165 of file clst.h.

188  { //get current data

◆ CLIST_ITERATOR() [2/2]

CLIST_ITERATOR::CLIST_ITERATOR ( CLIST list_to_iterate)
inline

Definition at line 274 of file clst.h.

274  {
275  set_to_list(list_to_iterate);
276 }

Member Function Documentation

◆ add_after_stay_put()

void CLIST_ITERATOR::add_after_stay_put ( void *  new_data)
inline

Definition at line 332 of file clst.h.

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

◆ add_after_then_move()

void CLIST_ITERATOR::add_after_then_move ( void *  new_data)
inline

Definition at line 285 of file clst.h.

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

◆ add_before_stay_put()

void CLIST_ITERATOR::add_before_stay_put ( void *  new_data)
inline

Definition at line 426 of file clst.h.

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

◆ add_before_then_move()

void CLIST_ITERATOR::add_before_then_move ( void *  new_data)
inline

Definition at line 382 of file clst.h.

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

◆ add_list_after()

void CLIST_ITERATOR::add_list_after ( CLIST list_to_add)
inline

Definition at line 472 of file clst.h.

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

◆ add_list_before()

void CLIST_ITERATOR::add_list_before ( CLIST list_to_add)
inline

Definition at line 519 of file clst.h.

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

◆ add_to_end()

void CLIST_ITERATOR::add_to_end ( void *  new_data)
inline

Definition at line 741 of file clst.h.

742  {
743  CLIST_LINK *new_element;
744 
745  #ifndef NDEBUG
746  if (!list)
747  NO_LIST.error ("CLIST_ITERATOR::add_to_end", ABORT, nullptr);
748  if (!new_data)
749  BAD_PARAMETER.error ("CLIST_ITERATOR::add_to_end", ABORT,
750  "new_data is nullptr");
751  #endif
752 
753  if (this->at_last ()) {
754  this->add_after_stay_put (new_data);
755  }
756  else {
757  if (this->at_first ()) {
758  this->add_before_stay_put (new_data);
759  list->last = prev;
760  }
761  else { //Iteratr is elsewhere
762  new_element = new CLIST_LINK;
763  new_element->data = new_data;
764 
765  new_element->next = list->last->next;
766  list->last->next = new_element;
767  list->last = new_element;
768  }
769  }
770 }

◆ at_first()

bool CLIST_ITERATOR::at_first ( )
inline

Definition at line 647 of file clst.h.

647  {
648  #ifndef NDEBUG
649  if (!list)
650  NO_LIST.error ("CLIST_ITERATOR::at_first", ABORT, nullptr);
651  #endif
652 
653  //we're at a deleted
654  return ((list->empty ()) || (current == list->First ()) || ((current == nullptr) &&
655  (prev == list->last) && //NON-last pt between
656  !ex_current_was_last)); //first and last
657 }

◆ at_last()

bool CLIST_ITERATOR::at_last ( )
inline

Definition at line 666 of file clst.h.

666  {
667  #ifndef NDEBUG
668  if (!list)
669  NO_LIST.error ("CLIST_ITERATOR::at_last", ABORT, nullptr);
670  #endif
671 
672  //we're at a deleted
673  return ((list->empty ()) || (current == list->last) || ((current == nullptr) &&
674  (prev == list->last) && //last point between
675  ex_current_was_last)); //first and last
676 }

◆ current_extracted()

bool CLIST_ITERATOR::current_extracted ( )
inline

Definition at line 224 of file clst.h.

◆ cycled_list()

bool CLIST_ITERATOR::cycled_list ( )
inline

Definition at line 685 of file clst.h.

685  {
686  #ifndef NDEBUG
687  if (!list)
688  NO_LIST.error ("CLIST_ITERATOR::cycled_list", ABORT, nullptr);
689  #endif
690 
691  return ((list->empty ()) || ((current == cycle_pt) && started_cycling));
692 
693 }

◆ data()

void* CLIST_ITERATOR::data ( )
inline

Definition at line 193 of file clst.h.

211  { //is list empty?

◆ data_relative()

void * CLIST_ITERATOR::data_relative ( int8_t  offset)

Definition at line 284 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
301  for (ptr = current ? current : prev; offset-- > 0; ptr = ptr->next);
302 
303  #ifndef NDEBUG
304  if (!ptr)
305  NULL_DATA.error ("CLIST_ITERATOR::data_relative", ABORT, nullptr);
306  #endif
307 
308  return ptr->data;
309 }

◆ empty()

bool CLIST_ITERATOR::empty ( )
inline

Definition at line 216 of file clst.h.

219  { //current extracted?
220  return !current;
221  }
222 

◆ exchange()

void CLIST_ITERATOR::exchange ( CLIST_ITERATOR other_it)

Definition at line 344 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 
419  if (current == cycle_pt)
420  cycle_pt = other_it->cycle_pt;
421  if (other_it->current == other_it->cycle_pt)
422  other_it->cycle_pt = cycle_pt;
423 
424  /* The actual exchange - in all cases*/
425 
426  old_current = current;
427  current = other_it->current;
428  other_it->current = old_current;
429 }

◆ extract()

void * CLIST_ITERATOR::extract ( )
inline

Definition at line 564 of file clst.h.

564  {
565  void *extracted_data;
566 
567  #ifndef NDEBUG
568  if (!list)
569  NO_LIST.error ("CLIST_ITERATOR::extract", ABORT, nullptr);
570  if (!current) //list empty or
571  //element extracted
572  NULL_CURRENT.error ("CLIST_ITERATOR::extract",
573  ABORT, nullptr);
574  #endif
575 
576  if (list->singleton()) {
577  // Special case where we do need to change the iterator.
578  prev = next = list->last = nullptr;
579  } else {
580  prev->next = next; //remove from list
581 
582  if (current == list->last) {
583  list->last = prev;
584  ex_current_was_last = true;
585  } else {
586  ex_current_was_last = false;
587  }
588  }
589  // Always set ex_current_was_cycle_pt so an add/forward will work in a loop.
590  ex_current_was_cycle_pt = (current == cycle_pt);
591  extracted_data = current->data;
592  delete(current); //destroy CONS cell
593  current = nullptr;
594  return extracted_data;
595 }

◆ forward()

void * CLIST_ITERATOR::forward ( )

Definition at line 244 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);
267  if (!next)
268  NULL_NEXT.error ("CLIST_ITERATOR::forward", ABORT,
269  "This is: %p Current is: %p", this, current);
270  #endif
271 
272  next = current->next;
273  return current->data;
274 }

◆ length()

int32_t CLIST_ITERATOR::length ( )
inline

Definition at line 702 of file clst.h.

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

◆ mark_cycle_pt()

void CLIST_ITERATOR::mark_cycle_pt ( )
inline

Definition at line 627 of file clst.h.

627  {
628  #ifndef NDEBUG
629  if (!list)
630  NO_LIST.error ("CLIST_ITERATOR::mark_cycle_pt", ABORT, nullptr);
631  #endif
632 
633  if (current)
634  cycle_pt = current;
635  else
636  ex_current_was_cycle_pt = true;
637  started_cycling = false;
638 }

◆ move_to_first()

void * CLIST_ITERATOR::move_to_first ( )
inline

Definition at line 604 of file clst.h.

604  {
605  #ifndef NDEBUG
606  if (!list)
607  NO_LIST.error ("CLIST_ITERATOR::move_to_first", ABORT, nullptr);
608  #endif
609 
610  current = list->First ();
611  prev = list->last;
612  next = current != nullptr ? current->next : nullptr;
613  return current != nullptr ? current->data : nullptr;
614 }

◆ move_to_last()

void * CLIST_ITERATOR::move_to_last ( )

Definition at line 319 of file clst.cpp.

319  {
320  #ifndef NDEBUG
321  if (!list)
322  NO_LIST.error ("CLIST_ITERATOR::move_to_last", ABORT, nullptr);
323  #endif
324 
325  while (current != list->last)
326  forward();
327 
328  if (current == nullptr)
329  return nullptr;
330  else
331  return current->data;
332 }

◆ set_to_list()

void CLIST_ITERATOR::set_to_list ( CLIST list_to_iterate)
inline

Definition at line 250 of file clst.h.

251  {
252  #ifndef NDEBUG
253  if (!list_to_iterate)
254  BAD_PARAMETER.error ("CLIST_ITERATOR::set_to_list", ABORT,
255  "list_to_iterate is nullptr");
256  #endif
257 
258  list = list_to_iterate;
259  prev = list->last;
260  current = list->First ();
261  next = current != nullptr ? current->next : nullptr;
262  cycle_pt = nullptr; //await explicit set
263  started_cycling = false;
264  ex_current_was_last = false;
265  ex_current_was_cycle_pt = false;
266 }

◆ sort()

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

Definition at line 719 of file clst.h.

721  {
722  #ifndef NDEBUG
723  if (!list)
724  NO_LIST.error ("CLIST_ITERATOR::sort", ABORT, nullptr);
725  #endif
726 
727  list->sort (comparator);
728  move_to_first();
729 }

Friends And Related Function Documentation

◆ CLIST::assign_to_sublist


The documentation for this class was generated from the following files:
ERRCODE
Definition: errcode.h:68
CLIST_ITERATOR::add_after_stay_put
void add_after_stay_put(void *new_data)
Definition: clst.h:332
EMPTY_LIST
constexpr ERRCODE EMPTY_LIST("List is empty")
CLIST::singleton
bool singleton() const
Definition: clst.h:101
CLIST::empty
bool empty() const
Definition: clst.h:97
ERRCODE::error
void error(const char *caller, TessErrorLogCode action, const char *format,...) const
Definition: errcode.cpp:35
CLIST_ITERATOR::at_last
bool at_last()
Definition: clst.h:666
BAD_PARAMETER
constexpr ERRCODE BAD_PARAMETER("List parameter error")
CLIST_LINK
Definition: clst.h:39
NO_LIST
constexpr ERRCODE NO_LIST("Iterator not set to a list")
NULL_CURRENT
constexpr ERRCODE NULL_CURRENT("List current position is nullptr")
CLIST::length
int32_t length() const
Definition: clst.cpp:114
CLIST_ITERATOR::set_to_list
void set_to_list(CLIST *list_to_iterate)
Definition: clst.h:250
CLIST_ITERATOR::add_before_stay_put
void add_before_stay_put(void *new_data)
Definition: clst.h:426
CLIST::sort
void sort(int comparator(const void *, const void *))
Definition: clst.cpp:130
CLIST_ITERATOR::at_first
bool at_first()
Definition: clst.h:647
CLIST_ITERATOR::forward
void * forward()
Definition: clst.cpp:244
CLIST_ITERATOR::move_to_first
void * move_to_first()
Definition: clst.h:604
NULL_DATA
constexpr ERRCODE NULL_DATA("List would have returned a nullptr data pointer")
NULL_NEXT
constexpr ERRCODE NULL_NEXT("Next element on the list is nullptr")
ABORT
@ ABORT
Definition: errcode.h:45