tesseract  5.0.0
tesseract::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 () const
 
bool current_extracted () const
 
bool at_first () const
 
bool at_last () const
 
bool cycled_list () const
 
void add_to_end (void *new_data)
 
void exchange (CLIST_ITERATOR *other_it)
 
int32_t length () const
 
void sort (int comparator(const void *, const void *))
 

Friends

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

Detailed Description

Definition at line 146 of file clst.h.

Constructor & Destructor Documentation

◆ CLIST_ITERATOR() [1/2]

tesseract::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]

tesseract::CLIST_ITERATOR::CLIST_ITERATOR ( CLIST list_to_iterate)
inline

Definition at line 264 of file clst.h.

264  {
265  set_to_list(list_to_iterate);
266 }
void set_to_list(CLIST *list_to_iterate)
Definition: clst.h:246

Member Function Documentation

◆ add_after_stay_put()

void tesseract::CLIST_ITERATOR::add_after_stay_put ( void *  new_data)
inline

Definition at line 319 of file clst.h.

320  {
321 #ifndef NDEBUG
322  if (!new_data) {
323  BAD_PARAMETER.error("CLIST_ITERATOR::add_after_stay_put", ABORT, "new_data is nullptr");
324  }
325 #endif
326 
327  auto new_element = new CLIST_LINK;
328  new_element->data = new_data;
329 
330  if (list->empty()) {
331  new_element->next = new_element;
332  list->last = new_element;
333  prev = next = new_element;
334  ex_current_was_last = false;
335  current = nullptr;
336  } else {
337  new_element->next = next;
338 
339  if (current) { // not extracted
340  current->next = new_element;
341  if (prev == current) {
342  prev = new_element;
343  }
344  if (current == list->last) {
345  list->last = new_element;
346  }
347  } else { // current extracted
348  prev->next = new_element;
349  if (ex_current_was_last) {
350  list->last = new_element;
351  ex_current_was_last = false;
352  }
353  }
354  next = new_element;
355  }
356 }
constexpr ERRCODE BAD_PARAMETER("List parameter error")
@ ABORT
Definition: errcode.h:31
bool empty() const
Definition: clst.h:89
void error(const char *caller, TessErrorLogCode action, const char *format,...) const __attribute__((format(printf
Definition: errcode.cpp:38

◆ add_after_then_move()

void tesseract::CLIST_ITERATOR::add_after_then_move ( void *  new_data)
inline

Definition at line 275 of file clst.h.

276  {
277 #ifndef NDEBUG
278  if (!new_data) {
279  BAD_PARAMETER.error("CLIST_ITERATOR::add_after_then_move", ABORT, "new_data is nullptr");
280  }
281 #endif
282 
283  auto new_element = new CLIST_LINK;
284  new_element->data = new_data;
285 
286  if (list->empty()) {
287  new_element->next = new_element;
288  list->last = new_element;
289  prev = next = new_element;
290  } else {
291  new_element->next = next;
292 
293  if (current) { // not extracted
294  current->next = new_element;
295  prev = current;
296  if (current == list->last) {
297  list->last = new_element;
298  }
299  } else { // current extracted
300  prev->next = new_element;
301  if (ex_current_was_last) {
302  list->last = new_element;
303  }
304  if (ex_current_was_cycle_pt) {
305  cycle_pt = new_element;
306  }
307  }
308  }
309  current = new_element;
310 }

◆ add_before_stay_put()

void tesseract::CLIST_ITERATOR::add_before_stay_put ( void *  new_data)
inline

Definition at line 405 of file clst.h.

406  {
407 #ifndef NDEBUG
408  if (!new_data) {
409  BAD_PARAMETER.error("CLIST_ITERATOR::add_before_stay_put", ABORT, "new_data is nullptr");
410  }
411 #endif
412 
413  auto new_element = new CLIST_LINK;
414  new_element->data = new_data;
415 
416  if (list->empty()) {
417  new_element->next = new_element;
418  list->last = new_element;
419  prev = next = new_element;
420  ex_current_was_last = true;
421  current = nullptr;
422  } else {
423  prev->next = new_element;
424  if (current) { // not extracted
425  new_element->next = current;
426  if (next == current) {
427  next = new_element;
428  }
429  } else { // current extracted
430  new_element->next = next;
431  if (ex_current_was_last) {
432  list->last = new_element;
433  }
434  }
435  prev = new_element;
436  }
437 }

◆ add_before_then_move()

void tesseract::CLIST_ITERATOR::add_before_then_move ( void *  new_data)
inline

Definition at line 365 of file clst.h.

366  {
367 #ifndef NDEBUG
368  if (!new_data) {
369  BAD_PARAMETER.error("CLIST_ITERATOR::add_before_then_move", ABORT, "new_data is nullptr");
370  }
371 #endif
372 
373  auto new_element = new CLIST_LINK;
374  new_element->data = new_data;
375 
376  if (list->empty()) {
377  new_element->next = new_element;
378  list->last = new_element;
379  prev = next = new_element;
380  } else {
381  prev->next = new_element;
382  if (current) { // not extracted
383  new_element->next = current;
384  next = current;
385  } else { // current extracted
386  new_element->next = next;
387  if (ex_current_was_last) {
388  list->last = new_element;
389  }
390  if (ex_current_was_cycle_pt) {
391  cycle_pt = new_element;
392  }
393  }
394  }
395  current = new_element;
396 }

◆ add_list_after()

void tesseract::CLIST_ITERATOR::add_list_after ( CLIST list_to_add)
inline

Definition at line 447 of file clst.h.

447  {
448  if (!list_to_add->empty()) {
449  if (list->empty()) {
450  list->last = list_to_add->last;
451  prev = list->last;
452  next = list->First();
453  ex_current_was_last = true;
454  current = nullptr;
455  } else {
456  if (current) { // not extracted
457  current->next = list_to_add->First();
458  if (current == list->last) {
459  list->last = list_to_add->last;
460  }
461  list_to_add->last->next = next;
462  next = current->next;
463  } else { // current extracted
464  prev->next = list_to_add->First();
465  if (ex_current_was_last) {
466  list->last = list_to_add->last;
467  ex_current_was_last = false;
468  }
469  list_to_add->last->next = next;
470  next = prev->next;
471  }
472  }
473  list_to_add->last = nullptr;
474  }
475 }

◆ add_list_before()

void tesseract::CLIST_ITERATOR::add_list_before ( CLIST list_to_add)
inline

Definition at line 485 of file clst.h.

485  {
486  if (!list_to_add->empty()) {
487  if (list->empty()) {
488  list->last = list_to_add->last;
489  prev = list->last;
490  current = list->First();
491  next = current->next;
492  ex_current_was_last = false;
493  } else {
494  prev->next = list_to_add->First();
495  if (current) { // not extracted
496  list_to_add->last->next = current;
497  } else { // current extracted
498  list_to_add->last->next = next;
499  if (ex_current_was_last) {
500  list->last = list_to_add->last;
501  }
502  if (ex_current_was_cycle_pt) {
503  cycle_pt = prev->next;
504  }
505  }
506  current = prev->next;
507  next = current->next;
508  }
509  list_to_add->last = nullptr;
510  }
511 }

◆ add_to_end()

void tesseract::CLIST_ITERATOR::add_to_end ( void *  new_data)
inline

Definition at line 665 of file clst.h.

666  {
667 #ifndef NDEBUG
668  if (!list) {
669  NO_LIST.error("CLIST_ITERATOR::add_to_end", ABORT, nullptr);
670  }
671  if (!new_data) {
672  BAD_PARAMETER.error("CLIST_ITERATOR::add_to_end", ABORT, "new_data is nullptr");
673  }
674 #endif
675 
676  if (this->at_last()) {
677  this->add_after_stay_put(new_data);
678  } else {
679  if (this->at_first()) {
680  this->add_before_stay_put(new_data);
681  list->last = prev;
682  } else { // Iteratr is elsewhere
683  auto new_element = new CLIST_LINK;
684  new_element->data = new_data;
685 
686  new_element->next = list->last->next;
687  list->last->next = new_element;
688  list->last = new_element;
689  }
690  }
691 }
constexpr ERRCODE NO_LIST("Iterator not set to a list")
void add_after_stay_put(void *new_data)
Definition: clst.h:319
bool at_last() const
Definition: clst.h:612
bool at_first() const
Definition: clst.h:598
void add_before_stay_put(void *new_data)
Definition: clst.h:405

◆ at_first()

bool tesseract::CLIST_ITERATOR::at_first ( ) const
inline

Definition at line 598 of file clst.h.

598  {
599  // we're at a deleted
600  return ((list->empty()) || (current == list->First()) ||
601  ((current == nullptr) && (prev == list->last) && // NON-last pt between
602  !ex_current_was_last)); // first and last
603 }

◆ at_last()

bool tesseract::CLIST_ITERATOR::at_last ( ) const
inline

Definition at line 612 of file clst.h.

612  {
613  // we're at a deleted
614  return ((list->empty()) || (current == list->last) ||
615  ((current == nullptr) && (prev == list->last) && // last point between
616  ex_current_was_last)); // first and last
617 }

◆ current_extracted()

bool tesseract::CLIST_ITERATOR::current_extracted ( ) const
inline

Definition at line 216 of file clst.h.

216  { // current extracted?
217  return !current;
218  }

◆ cycled_list()

bool tesseract::CLIST_ITERATOR::cycled_list ( ) const
inline

Definition at line 626 of file clst.h.

626  {
627  return ((list->empty()) || ((current == cycle_pt) && started_cycling));
628 }

◆ data()

void* tesseract::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  }
195 #endif
196  return current->data;
197  }

◆ data_relative()

void * tesseract::CLIST_ITERATOR::data_relative ( int8_t  offset)

Definition at line 243 of file clst.cpp.

244  { // offset from current
245  CLIST_LINK *ptr;
246 
247 #ifndef NDEBUG
248  if (!list)
249  NO_LIST.error("CLIST_ITERATOR::data_relative", ABORT, nullptr);
250  if (list->empty())
251  EMPTY_LIST.error("CLIST_ITERATOR::data_relative", ABORT, nullptr);
252  if (offset < -1)
253  BAD_PARAMETER.error("CLIST_ITERATOR::data_relative", ABORT, "offset < -l");
254 #endif
255 
256  if (offset == -1) {
257  ptr = prev;
258  } else {
259  for (ptr = current ? current : prev; offset-- > 0; ptr = ptr->next) {
260  ;
261  }
262  }
263 
264  return ptr->data;
265 }
constexpr ERRCODE EMPTY_LIST("List is empty")

◆ empty()

bool tesseract::CLIST_ITERATOR::empty ( ) const
inline

Definition at line 212 of file clst.h.

212  { // is list empty?
213  return list->empty();
214  }

◆ exchange()

void tesseract::CLIST_ITERATOR::exchange ( CLIST_ITERATOR other_it)

Definition at line 297 of file clst.cpp.

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

◆ extract()

void * tesseract::CLIST_ITERATOR::extract ( )
inline

Definition at line 522 of file clst.h.

522  {
523 #ifndef NDEBUG
524  if (!current) { // list empty or
525  // element extracted
526  NULL_CURRENT.error("CLIST_ITERATOR::extract", ABORT, nullptr);
527  }
528 #endif
529 
530  if (list->singleton()) {
531  // Special case where we do need to change the iterator.
532  prev = next = list->last = nullptr;
533  } else {
534  prev->next = next; // remove from list
535 
536  if (current == list->last) {
537  list->last = prev;
538  ex_current_was_last = true;
539  } else {
540  ex_current_was_last = false;
541  }
542  }
543  // Always set ex_current_was_cycle_pt so an add/forward will work in a loop.
544  ex_current_was_cycle_pt = (current == cycle_pt);
545  auto extracted_data = current->data;
546  delete (current); // destroy CONS cell
547  current = nullptr;
548  return extracted_data;
549 }
constexpr ERRCODE NULL_CURRENT("List current position is nullptr")
bool singleton() const
Definition: clst.h:93

◆ forward()

void * tesseract::CLIST_ITERATOR::forward ( )

Definition at line 213 of file clst.cpp.

213  {
214  if (list->empty()) {
215  return nullptr;
216  }
217 
218  if (current) { // not removed so
219  // set previous
220  prev = current;
221  started_cycling = true;
222  // In case next is deleted by another iterator, get next from current.
223  current = current->next;
224  } else {
225  if (ex_current_was_cycle_pt) {
226  cycle_pt = next;
227  }
228  current = next;
229  }
230 
231  next = current->next;
232  return current->data;
233 }

◆ length()

int32_t tesseract::CLIST_ITERATOR::length ( ) const
inline

Definition at line 637 of file clst.h.

637  {
638  return list->length();
639 }
int32_t length() const
Definition: clst.h:106

◆ mark_cycle_pt()

void tesseract::CLIST_ITERATOR::mark_cycle_pt ( )
inline

Definition at line 576 of file clst.h.

576  {
577 #ifndef NDEBUG
578  if (!list) {
579  NO_LIST.error("CLIST_ITERATOR::mark_cycle_pt", ABORT, nullptr);
580  }
581 #endif
582 
583  if (current) {
584  cycle_pt = current;
585  } else {
586  ex_current_was_cycle_pt = true;
587  }
588  started_cycling = false;
589 }

◆ move_to_first()

void * tesseract::CLIST_ITERATOR::move_to_first ( )
inline

Definition at line 558 of file clst.h.

558  {
559  current = list->First();
560  prev = list->last;
561  next = current != nullptr ? current->next : nullptr;
562  return current != nullptr ? current->data : nullptr;
563 }

◆ move_to_last()

void * tesseract::CLIST_ITERATOR::move_to_last ( )

Definition at line 275 of file clst.cpp.

275  {
276  while (current != list->last) {
277  forward();
278  }
279 
280  if (current == nullptr) {
281  return nullptr;
282  } else {
283  return current->data;
284  }
285 }

◆ set_to_list()

void tesseract::CLIST_ITERATOR::set_to_list ( CLIST list_to_iterate)
inline

Definition at line 246 of file clst.h.

247  {
248  list = list_to_iterate;
249  prev = list->last;
250  current = list->First();
251  next = current != nullptr ? current->next : nullptr;
252  cycle_pt = nullptr; // await explicit set
253  started_cycling = false;
254  ex_current_was_last = false;
255  ex_current_was_cycle_pt = false;
256 }

◆ sort()

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

Definition at line 648 of file clst.h.

650  {
651  list->sort(comparator);
652  move_to_first();
653 }
void sort(int comparator(const void *, const void *))
Definition: clst.cpp:104
void * move_to_first()
Definition: clst.h:558

Friends And Related Function Documentation

◆ CLIST::assign_to_sublist


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