tesseract  4.0.0-1-g2a2b
CLIST Class Reference

#include <clst.h>

Public Member Functions

 CLIST ()
 
 ~CLIST ()
 
void internal_deep_clear (void(*zapper)(void *))
 
void shallow_clear ()
 
bool empty () const
 
bool singleton () const
 
void shallow_copy (CLIST *from_list)
 
void assign_to_sublist (CLIST_ITERATOR *start_it, CLIST_ITERATOR *end_it)
 
int32_t length () const
 
void sort (int comparator(const void *, const void *))
 
bool add_sorted (int comparator(const void *, const void *), bool unique, void *new_data)
 
void set_subtract (int comparator(const void *, const void *), bool unique, CLIST *minuend, CLIST *subtrahend)
 

Friends

class CLIST_ITERATOR
 

Detailed Description

Definition at line 70 of file clst.h.

Constructor & Destructor Documentation

◆ CLIST()

CLIST::CLIST ( )
inline

Definition at line 81 of file clst.h.

81  { //constructor
82  last = nullptr;
83  }

◆ ~CLIST()

CLIST::~CLIST ( )
inline

Definition at line 85 of file clst.h.

85  { //destructor
86  shallow_clear();
87  }
void shallow_clear()
Definition: clst.cpp:68

Member Function Documentation

◆ add_sorted()

bool CLIST::add_sorted ( int   comparatorconst void *, const void *,
bool  unique,
void *  new_data 
)

Definition at line 170 of file clst.cpp.

171  {
172  // Check for adding at the end.
173  if (last == nullptr || comparator(&last->data, &new_data) < 0) {
174  CLIST_LINK* new_element = new CLIST_LINK;
175  new_element->data = new_data;
176  if (last == nullptr) {
177  new_element->next = new_element;
178  } else {
179  new_element->next = last->next;
180  last->next = new_element;
181  }
182  last = new_element;
183  return true;
184  } else if (!unique || last->data != new_data) {
185  // Need to use an iterator.
186  CLIST_ITERATOR it(this);
187  for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
188  void* data = it.data();
189  if (data == new_data && unique)
190  return false;
191  if (comparator(&data, &new_data) > 0)
192  break;
193  }
194  if (it.cycled_list())
195  it.add_to_end(new_data);
196  else
197  it.add_before_then_move(new_data);
198  return true;
199  }
200  return false;
201 }

◆ assign_to_sublist()

void CLIST::assign_to_sublist ( CLIST_ITERATOR start_it,
CLIST_ITERATOR end_it 
)

Definition at line 97 of file clst.cpp.

99  { //from list end
100  const ERRCODE LIST_NOT_EMPTY =
101  "Destination list must be empty before extracting a sublist";
102 
103  if (!empty ())
104  LIST_NOT_EMPTY.error ("CLIST.assign_to_sublist", ABORT, nullptr);
105 
106  last = start_it->extract_sublist (end_it);
107 }
bool empty() const
Definition: clst.h:95
Definition: errcode.h:30
void error(const char *caller, TessErrorLogCode action, const char *format,...) const
Definition: errcode.cpp:37

◆ empty()

bool CLIST::empty ( ) const
inline

Definition at line 95 of file clst.h.

95  { //is list empty?
96  return !last;
97  }

◆ internal_deep_clear()

void CLIST::internal_deep_clear ( void(*)(void *)  zapper)

Definition at line 41 of file clst.cpp.

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

◆ length()

int32_t CLIST::length ( ) const

Definition at line 115 of file clst.cpp.

115  { //count elements
116  CLIST_ITERATOR it(const_cast<CLIST*>(this));
117  int32_t count = 0;
118 
119  for (it.mark_cycle_pt(); !it.cycled_list(); it.forward())
120  count++;
121  return count;
122 }
int count(LIST var_list)
Definition: oldlist.cpp:98

◆ set_subtract()

void CLIST::set_subtract ( int   comparatorconst void *, const void *,
bool  unique,
CLIST minuend,
CLIST subtrahend 
)

Definition at line 208 of file clst.cpp.

210  {
211  shallow_clear();
212  CLIST_ITERATOR m_it(minuend);
213  CLIST_ITERATOR s_it(subtrahend);
214  // Since both lists are sorted, finding the subtras that are not
215  // minus is a case of a parallel iteration.
216  for (m_it.mark_cycle_pt(); !m_it.cycled_list(); m_it.forward()) {
217  void* minu = m_it.data();
218  void* subtra = nullptr;
219  if (!s_it.empty()) {
220  subtra = s_it.data();
221  while (!s_it.at_last() &&
222  comparator(&subtra, &minu) < 0) {
223  s_it.forward();
224  subtra = s_it.data();
225  }
226  }
227  if (subtra == nullptr || comparator(&subtra, &minu) != 0)
228  add_sorted(comparator, unique, minu);
229  }
230 }
void shallow_clear()
Definition: clst.cpp:68
bool add_sorted(int comparator(const void *, const void *), bool unique, void *new_data)
Definition: clst.cpp:170

◆ shallow_clear()

void CLIST::shallow_clear ( )

Definition at line 68 of file clst.cpp.

68  { //destroy all links
69  CLIST_LINK *ptr;
70  CLIST_LINK *next;
71 
72  if (!empty ()) {
73  ptr = last->next; //set to first
74  last->next = nullptr; //break circle
75  last = nullptr; //set list empty
76  while (ptr) {
77  next = ptr->next;
78  delete(ptr);
79  ptr = next;
80  }
81  }
82 }
bool empty() const
Definition: clst.h:95

◆ shallow_copy()

void CLIST::shallow_copy ( CLIST from_list)
inline

Definition at line 103 of file clst.h.

104  { //beware destructors!!
105  last = from_list->last;
106  }

◆ singleton()

bool CLIST::singleton ( ) const
inline

Definition at line 99 of file clst.h.

99  {
100  return last != nullptr ? (last == last->next) : false;
101  }

◆ sort()

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

Definition at line 131 of file clst.cpp.

133  {
134  CLIST_ITERATOR it(this);
135  int32_t count;
136  void **base; //ptr array to sort
137  void **current;
138  int32_t i;
139 
140  /* Allocate an array of pointers, one per list element */
141  count = length ();
142  base = (void **) malloc (count * sizeof (void *));
143 
144  /* Extract all elements, putting the pointers in the array */
145  current = base;
146  for (it.mark_cycle_pt (); !it.cycled_list (); it.forward ()) {
147  *current = it.extract ();
148  current++;
149  }
150 
151  /* Sort the pointer array */
152  qsort(base, count, sizeof(*base), comparator);
153 
154  /* Rebuild the list from the sorted pointers */
155  current = base;
156  for (i = 0; i < count; i++) {
157  it.add_to_end (*current);
158  current++;
159  }
160  free(base);
161 }
int count(LIST var_list)
Definition: oldlist.cpp:98
int32_t length() const
Definition: clst.cpp:115

Friends And Related Function Documentation

◆ CLIST_ITERATOR

friend class CLIST_ITERATOR
friend

Definition at line 72 of file clst.h.


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