tesseract  5.0.0-alpha-619-ge9db
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 67 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  }

Member Function Documentation

◆ add_sorted()

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

Definition at line 163 of file clst.cpp.

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

◆ assign_to_sublist()

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

Definition at line 92 of file clst.cpp.

98  { //from list end
99  constexpr ERRCODE LIST_NOT_EMPTY(
100  "Destination list must be empty before extracting a sublist");
101 
102  if (!empty ())

◆ 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 38 of file clst.cpp.

41  { //ptr to zapper functn
42  CLIST_LINK *ptr;
43  CLIST_LINK *next;
44 
45  if (!empty ()) {
46  ptr = last->next; //set to first
47  last->next = nullptr; //break circle
48  last = nullptr; //set list empty
49  while (ptr) {
50  next = ptr->next;
51  zapper (ptr->data);
52  delete(ptr);
53  ptr = next;
54  }

◆ length()

int32_t CLIST::length ( ) const

Definition at line 109 of file clst.cpp.

114  { //count elements
115  CLIST_ITERATOR it(const_cast<CLIST*>(this));
116  int32_t count = 0;

◆ set_subtract()

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

Definition at line 201 of file clst.cpp.

209  {
210  shallow_clear();
211  CLIST_ITERATOR m_it(minuend);
212  CLIST_ITERATOR s_it(subtrahend);
213  // Since both lists are sorted, finding the subtras that are not
214  // minus is a case of a parallel iteration.
215  for (m_it.mark_cycle_pt(); !m_it.cycled_list(); m_it.forward()) {
216  void* minu = m_it.data();
217  void* subtra = nullptr;
218  if (!s_it.empty()) {
219  subtra = s_it.data();
220  while (!s_it.at_last() &&
221  comparator(&subtra, &minu) < 0) {
222  s_it.forward();
223  subtra = s_it.data();

◆ shallow_clear()

void CLIST::shallow_clear ( )

Definition at line 64 of file clst.cpp.

67  { //destroy all links
68  CLIST_LINK *ptr;
69  CLIST_LINK *next;
70 
71  if (!empty ()) {
72  ptr = last->next; //set to first
73  last->next = nullptr; //break circle
74  last = nullptr; //set list empty
75  while (ptr) {
76  next = ptr->next;
77  delete(ptr);
78  ptr = next;

◆ 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 124 of file clst.cpp.

132  {
133  CLIST_ITERATOR it(this);
134  int32_t count;
135  void **base; //ptr array to sort
136  void **current;
137  int32_t i;
138 
139  /* Allocate an array of pointers, one per list element */
140  count = length ();
141  base = static_cast<void **>(malloc (count * sizeof (void *)));
142 
143  /* Extract all elements, putting the pointers in the array */
144  current = base;
145  for (it.mark_cycle_pt (); !it.cycled_list (); it.forward ()) {
146  *current = it.extract ();
147  current++;
148  }
149 
150  /* Sort the pointer array */
151  qsort(base, count, sizeof(*base), comparator);
152 
153  /* Rebuild the list from the sorted pointers */
154  current = base;

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:
ERRCODE
Definition: errcode.h:67
CLIST_LINK
Definition: clst.h:38
CLIST::empty
bool empty() const
Definition: clst.h:95
CLIST::length
int32_t length() const
Definition: clst.cpp:109
count
int count(LIST var_list)
Definition: oldlist.cpp:79
CLIST::shallow_clear
void shallow_clear()
Definition: clst.cpp:64
CLIST_ITERATOR
Definition: clst.h:141