tesseract 4.1.1
Loading...
Searching...
No Matches
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 180 of file elst.h.

Constructor & Destructor Documentation

◆ ELIST_ITERATOR() [1/2]

ELIST_ITERATOR::ELIST_ITERATOR ( )
inline

Definition at line 197 of file elst.h.

197 { //constructor
198 list = nullptr;
199 } //unassigned list

◆ ELIST_ITERATOR() [2/2]

ELIST_ITERATOR::ELIST_ITERATOR ( ELIST list_to_iterate)
inlineexplicit

Definition at line 311 of file elst.h.

311 {
312 set_to_list(list_to_iterate);
313}
void set_to_list(ELIST *list_to_iterate)
Definition: elst.h:286

Member Function Documentation

◆ add_after_stay_put()

void ELIST_ITERATOR::add_after_stay_put ( ELIST_LINK new_link)
inline

Definition at line 368 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
398 prev->next = new_element;
399 if (ex_current_was_last) {
400 list->last = new_element;
401 ex_current_was_last = false;
402 }
403 }
404 next = new_element;
405 }
406}
@ ABORT
Definition: errcode.h:29
constexpr ERRCODE BAD_PARAMETER("List parameter error")
constexpr ERRCODE STILL_LINKED("Attempting to add an element with non nullptr links, to a list")
constexpr ERRCODE NO_LIST("Iterator not set to a list")
bool empty() const
Definition: elst.h:125
void error(const char *caller, TessErrorLogCode action, const char *format,...) const
Definition: errcode.cpp:35

◆ add_after_then_move()

void ELIST_ITERATOR::add_after_then_move ( ELIST_LINK new_link)
inline

Definition at line 323 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;
351 if (ex_current_was_last)
352 list->last = new_element;
353 if (ex_current_was_cycle_pt)
354 cycle_pt = new_element;
355 }
356 }
357 current = new_element;
358}

◆ add_before_stay_put()

void ELIST_ITERATOR::add_before_stay_put ( ELIST_LINK new_link)
inline

Definition at line 457 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;
480 if (next == current)
481 next = new_element;
482 }
483 else { //current extracted
484 new_element->next = next;
485 if (ex_current_was_last)
486 list->last = new_element;
487 }
488 prev = new_element;
489 }
490}

◆ add_before_then_move()

void ELIST_ITERATOR::add_before_then_move ( ELIST_LINK new_link)
inline

Definition at line 416 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 }
439 else { //current extracted
440 new_element->next = next;
441 if (ex_current_was_last)
442 list->last = new_element;
443 if (ex_current_was_cycle_pt)
444 cycle_pt = new_element;
445 }
446 }
447 current = new_element;
448}

◆ add_list_after()

void ELIST_ITERATOR::add_list_after ( ELIST list_to_add)
inline

Definition at line 500 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
526 prev->next = list_to_add->First ();
527 if (ex_current_was_last) {
528 list->last = list_to_add->last;
529 ex_current_was_last = false;
530 }
531 list_to_add->last->next = next;
532 next = prev->next;
533 }
534 }
535 list_to_add->last = nullptr;
536 }
537}

◆ add_list_before()

void ELIST_ITERATOR::add_list_before ( ELIST list_to_add)
inline

Definition at line 548 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 }
570 else { //current extracted
571 list_to_add->last->next = next;
572 if (ex_current_was_last)
573 list->last = list_to_add->last;
574 if (ex_current_was_cycle_pt)
575 cycle_pt = prev->next;
576 }
577 current = prev->next;
578 next = current->next;
579 }
580 list_to_add->last = nullptr;
581 }
582}

◆ add_to_end()

void ELIST_ITERATOR::add_to_end ( ELIST_LINK new_link)
inline

Definition at line 775 of file elst.h.

776 {
777 #ifndef NDEBUG
778 if (!list)
779 NO_LIST.error ("ELIST_ITERATOR::add_to_end", ABORT, nullptr);
780 if (!new_element)
781 BAD_PARAMETER.error ("ELIST_ITERATOR::add_to_end", ABORT,
782 "new_element is nullptr");
783 if (new_element->next)
784 STILL_LINKED.error ("ELIST_ITERATOR::add_to_end", ABORT, nullptr);
785 #endif
786
787 if (this->at_last ()) {
788 this->add_after_stay_put (new_element);
789 }
790 else {
791 if (this->at_first ()) {
792 this->add_before_stay_put (new_element);
793 list->last = new_element;
794 }
795 else { //Iteratr is elsewhere
796 new_element->next = list->last->next;
797 list->last->next = new_element;
798 list->last = new_element;
799 }
800 }
801}
void add_before_stay_put(ELIST_LINK *new_link)
Definition: elst.h:457
void add_after_stay_put(ELIST_LINK *new_link)
Definition: elst.h:368
bool at_first()
Definition: elst.h:676
bool at_last()
Definition: elst.h:696

◆ at_first()

bool ELIST_ITERATOR::at_first ( )
inline

Definition at line 676 of file elst.h.

676 {
677 #ifndef NDEBUG
678 if (!list)
679 NO_LIST.error ("ELIST_ITERATOR::at_first", ABORT, nullptr);
680 #endif
681
682 //we're at a deleted
683 return ((list->empty ()) || (current == list->First ()) || ((current == nullptr) &&
684 (prev == list->last) && //NON-last pt between
685 !ex_current_was_last)); //first and last
686}

◆ at_last()

bool ELIST_ITERATOR::at_last ( )
inline

Definition at line 696 of file elst.h.

696 {
697 #ifndef NDEBUG
698 if (!list)
699 NO_LIST.error ("ELIST_ITERATOR::at_last", ABORT, nullptr);
700 #endif
701
702 //we're at a deleted
703 return ((list->empty ()) || (current == list->last) || ((current == nullptr) &&
704 (prev == list->last) && //last point between
705 ex_current_was_last)); //first and last
706}

◆ current_extracted()

bool ELIST_ITERATOR::current_extracted ( )
inline

Definition at line 255 of file elst.h.

255 { //current extracted?
256 return !current;
257 }

◆ cycled_list()

bool ELIST_ITERATOR::cycled_list ( )
inline

Definition at line 716 of file elst.h.

716 {
717 #ifndef NDEBUG
718 if (!list)
719 NO_LIST.error ("ELIST_ITERATOR::cycled_list", ABORT, nullptr);
720 #endif
721
722 return ((list->empty ()) || ((current == cycle_pt) && started_cycling));
723
724}

◆ data()

ELIST_LINK * ELIST_ITERATOR::data ( )
inline

Definition at line 224 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
231 return current;
232 }
constexpr ERRCODE NULL_DATA("List would have returned a nullptr data pointer")

◆ data_relative()

ELIST_LINK * ELIST_ITERATOR::data_relative ( int8_t  offset)

Definition at line 234 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
251 for (ptr = current ? current : prev; offset-- > 0; ptr = ptr->next);
252
253 #ifndef NDEBUG
254 if (!ptr)
255 NULL_DATA.error ("ELIST_ITERATOR::data_relative", ABORT, nullptr);
256 #endif
257
258 return ptr;
259}
constexpr ERRCODE EMPTY_LIST("List is empty")

◆ empty()

bool ELIST_ITERATOR::empty ( )
inline

Definition at line 247 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
252 return list->empty ();
253 }

◆ exchange()

void ELIST_ITERATOR::exchange ( ELIST_ITERATOR other_it)

Definition at line 291 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
366 if (current == cycle_pt)
367 cycle_pt = other_it->cycle_pt;
368 if (other_it->current == other_it->cycle_pt)
369 other_it->cycle_pt = cycle_pt;
370
371 /* The actual exchange - in all cases*/
372
373 old_current = current;
374 current = other_it->current;
375 other_it->current = old_current;
376}

◆ extract()

ELIST_LINK * ELIST_ITERATOR::extract ( )
inline

Definition at line 594 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.
608 prev = next = list->last = nullptr;
609 } else {
610 prev->next = next; //remove from list
611
612 ex_current_was_last = (current == list->last);
613 if (ex_current_was_last) list->last = prev;
614 }
615 // Always set ex_current_was_cycle_pt so an add/forward will work in a loop.
616 ex_current_was_cycle_pt = (current == cycle_pt);
617 extracted_link = current;
618 extracted_link->next = nullptr; //for safety
619 current = nullptr;
620 return extracted_link;
621}
constexpr ERRCODE NULL_CURRENT("List current position is nullptr")
bool singleton() const
Definition: elst.h:129

◆ forward()

ELIST_LINK * ELIST_ITERATOR::forward ( )

Definition at line 193 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;
217
218 #ifndef NDEBUG
219 if (!next)
220 NULL_NEXT.error ("ELIST_ITERATOR::forward", ABORT,
221 "This is: %p Current is: %p", this, current);
222 #endif
223 return current;
224}
constexpr ERRCODE NULL_NEXT("Next element on the list is nullptr")

◆ length()

int32_t ELIST_ITERATOR::length ( )
inline

Definition at line 734 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}
int32_t length() const
Definition: elst.cpp:89

◆ mark_cycle_pt()

void ELIST_ITERATOR::mark_cycle_pt ( )
inline

Definition at line 655 of file elst.h.

655 {
656 #ifndef NDEBUG
657 if (!list)
658 NO_LIST.error ("ELIST_ITERATOR::mark_cycle_pt", ABORT, nullptr);
659 #endif
660
661 if (current)
662 cycle_pt = current;
663 else
664 ex_current_was_cycle_pt = true;
665 started_cycling = false;
666}

◆ move_to_first()

ELIST_LINK * ELIST_ITERATOR::move_to_first ( )
inline

Definition at line 631 of file elst.h.

631 {
632 #ifndef NDEBUG
633 if (!list)
634 NO_LIST.error ("ELIST_ITERATOR::move_to_first", ABORT, nullptr);
635 #endif
636
637 current = list->First ();
638 prev = list->last;
639 next = current ? current->next : nullptr;
640 return current;
641}

◆ move_to_last()

ELIST_LINK * ELIST_ITERATOR::move_to_last ( )

Definition at line 269 of file elst.cpp.

269 {
270 #ifndef NDEBUG
271 if (!list)
272 NO_LIST.error ("ELIST_ITERATOR::move_to_last", ABORT, nullptr);
273 #endif
274
275 while (current != list->last)
276 forward();
277
278 return current;
279}
ELIST_LINK * forward()
Definition: elst.cpp:193

◆ set_to_list()

void ELIST_ITERATOR::set_to_list ( ELIST list_to_iterate)
inline

Definition at line 286 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 ();
297 next = current ? current->next : nullptr;
298 cycle_pt = nullptr; //await explicit set
299 started_cycling = false;
300 ex_current_was_last = false;
301 ex_current_was_cycle_pt = false;
302}

◆ sort()

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

Definition at line 752 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);
762}
void sort(int comparator(const void *, const void *))
Definition: elst.cpp:107
ELIST_LINK * move_to_first()
Definition: elst.h:631

Friends And Related Function Documentation

◆ ELIST::assign_to_sublist


The documentation for this class was generated from the following files: