tesseract  4.0.0-1-g2a2b
ELIST Class Reference

#include <elst.h>

Public Member Functions

 ELIST ()
 
void internal_clear (void(*zapper)(ELIST_LINK *))
 
bool empty () const
 
bool singleton () const
 
void shallow_copy (ELIST *from_list)
 
void internal_deep_copy (ELIST_LINK *(*copier)(ELIST_LINK *), const ELIST *list)
 
void assign_to_sublist (ELIST_ITERATOR *start_it, ELIST_ITERATOR *end_it)
 
int32_t length () const
 
void sort (int comparator(const void *, const void *))
 
ELIST_LINKadd_sorted_and_find (int comparator(const void *, const void *), bool unique, ELIST_LINK *new_link)
 
bool add_sorted (int comparator(const void *, const void *), bool unique, ELIST_LINK *new_link)
 

Friends

class ELIST_ITERATOR
 

Detailed Description

Definition at line 113 of file elst.h.

Constructor & Destructor Documentation

◆ ELIST()

ELIST::ELIST ( )
inline

Definition at line 124 of file elst.h.

124  { //constructor
125  last = nullptr;
126  }

Member Function Documentation

◆ add_sorted()

bool ELIST::add_sorted ( int   comparatorconst void *, const void *,
bool  unique,
ELIST_LINK new_link 
)
inline

Definition at line 174 of file elst.h.

175  {
176  return (add_sorted_and_find(comparator, unique, new_link) == new_link);
177  }
ELIST_LINK * add_sorted_and_find(int comparator(const void *, const void *), bool unique, ELIST_LINK *new_link)
Definition: elst.cpp:150

◆ add_sorted_and_find()

ELIST_LINK * ELIST::add_sorted_and_find ( int   comparatorconst void *, const void *,
bool  unique,
ELIST_LINK new_link 
)

Definition at line 150 of file elst.cpp.

152  {
153  // Check for adding at the end.
154  if (last == nullptr || comparator(&last, &new_link) < 0) {
155  if (last == nullptr) {
156  new_link->next = new_link;
157  } else {
158  new_link->next = last->next;
159  last->next = new_link;
160  }
161  last = new_link;
162  } else {
163  // Need to use an iterator.
164  ELIST_ITERATOR it(this);
165  for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
166  ELIST_LINK* link = it.data();
167  int compare = comparator(&link, &new_link);
168  if (compare > 0) {
169  break;
170  } else if (unique && compare == 0) {
171  return link;
172  }
173  }
174  if (it.cycled_list())
175  it.add_to_end(new_link);
176  else
177  it.add_before_then_move(new_link);
178  }
179  return new_link;
180 }

◆ assign_to_sublist()

void ELIST::assign_to_sublist ( ELIST_ITERATOR start_it,
ELIST_ITERATOR end_it 
)

Definition at line 72 of file elst.cpp.

74  { //from list end
75  const ERRCODE LIST_NOT_EMPTY =
76  "Destination list must be empty before extracting a sublist";
77 
78  if (!empty ())
79  LIST_NOT_EMPTY.error ("ELIST.assign_to_sublist", ABORT, nullptr);
80 
81  last = start_it->extract_sublist (end_it);
82 }
bool empty() const
Definition: elst.h:132
Definition: errcode.h:30
void error(const char *caller, TessErrorLogCode action, const char *format,...) const
Definition: errcode.cpp:37

◆ empty()

bool ELIST::empty ( ) const
inline

Definition at line 132 of file elst.h.

132  { //is list empty?
133  return !last;
134  }

◆ internal_clear()

void ELIST::internal_clear ( void(*)(ELIST_LINK *)  zapper)

Definition at line 41 of file elst.cpp.

42  {
43  //ptr to zapper functn
44  ELIST_LINK *ptr;
45  ELIST_LINK *next;
46 
47  if (!empty ()) {
48  ptr = last->next; //set to first
49  last->next = nullptr; //break circle
50  last = nullptr; //set list empty
51  while (ptr) {
52  next = ptr->next;
53  zapper(ptr);
54  ptr = next;
55  }
56  }
57 }
bool empty() const
Definition: elst.h:132

◆ internal_deep_copy()

void ELIST::internal_deep_copy ( ELIST_LINK *(*)(ELIST_LINK *)  copier,
const ELIST list 
)

◆ length()

int32_t ELIST::length ( ) const

Definition at line 90 of file elst.cpp.

90  { // count elements
91  ELIST_ITERATOR it(const_cast<ELIST*>(this));
92  int32_t count = 0;
93 
94  for (it.mark_cycle_pt (); !it.cycled_list (); it.forward ())
95  count++;
96  return count;
97 }
int count(LIST var_list)
Definition: oldlist.cpp:98

◆ shallow_copy()

void ELIST::shallow_copy ( ELIST from_list)
inline

Definition at line 140 of file elst.h.

141  { //beware destructors!!
142  last = from_list->last;
143  }

◆ singleton()

bool ELIST::singleton ( ) const
inline

Definition at line 136 of file elst.h.

136  {
137  return last ? (last == last->next) : false;
138  }
LIST last(LIST var_list)
Definition: oldlist.cpp:242
struct list_rec * next
Definition: oldlist.h:132

◆ sort()

void ELIST::sort ( int   comparator const void *, const void *)

Definition at line 108 of file elst.cpp.

110  {
111  ELIST_ITERATOR it(this);
112  int32_t count;
113  ELIST_LINK **base; //ptr array to sort
114  ELIST_LINK **current;
115  int32_t i;
116 
117  /* Allocate an array of pointers, one per list element */
118  count = length ();
119  base = (ELIST_LINK **) malloc (count * sizeof (ELIST_LINK *));
120 
121  /* Extract all elements, putting the pointers in the array */
122  current = base;
123  for (it.mark_cycle_pt (); !it.cycled_list (); it.forward ()) {
124  *current = it.extract ();
125  current++;
126  }
127 
128  /* Sort the pointer array */
129  qsort(base, count, sizeof(*base), comparator);
130 
131  /* Rebuild the list from the sorted pointers */
132  current = base;
133  for (i = 0; i < count; i++) {
134  it.add_to_end (*current);
135  current++;
136  }
137  free(base);
138 }
int count(LIST var_list)
Definition: oldlist.cpp:98
int32_t length() const
Definition: elst.cpp:90

Friends And Related Function Documentation

◆ ELIST_ITERATOR

friend class ELIST_ITERATOR
friend

Definition at line 115 of file elst.h.


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