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

Constructor & Destructor Documentation

◆ CLIST()

CLIST::CLIST ( )
inline

Definition at line 79 of file clst.h.

79 { //constructor
80 last = nullptr;
81 }

◆ ~CLIST()

CLIST::~CLIST ( )
inline

Definition at line 83 of file clst.h.

83 { //destructor
85 }
void shallow_clear()
Definition: clst.cpp:67

Member Function Documentation

◆ add_sorted()

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

Definition at line 169 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);
195 else
196 it.add_before_then_move(new_data);
197 return true;
198 }
199 return false;
200}

◆ assign_to_sublist()

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

Definition at line 96 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 ())
103 LIST_NOT_EMPTY.error ("CLIST.assign_to_sublist", ABORT, nullptr);
104
105 last = start_it->extract_sublist (end_it);
106}
@ ABORT
Definition: errcode.h:29
bool empty() const
Definition: clst.h:93

◆ empty()

bool CLIST::empty ( ) const
inline

Definition at line 93 of file clst.h.

93 { //is list empty?
94 return !last;
95 }

◆ internal_deep_clear()

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

Definition at line 40 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 }
55 }
56}

◆ length()

int32_t CLIST::length ( ) const

Definition at line 114 of file clst.cpp.

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

◆ set_subtract()

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

Definition at line 207 of file clst.cpp.

209 {
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();
224 }
225 }
226 if (subtra == nullptr || comparator(&subtra, &minu) != 0)
227 add_sorted(comparator, unique, minu);
228 }
229}
bool add_sorted(int comparator(const void *, const void *), bool unique, void *new_data)
Definition: clst.cpp:169

◆ shallow_clear()

void CLIST::shallow_clear ( )

Definition at line 67 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;
79 }
80 }
81}

◆ shallow_copy()

void CLIST::shallow_copy ( CLIST from_list)
inline

Definition at line 101 of file clst.h.

102 { //beware destructors!!
103 last = from_list->last;
104 }

◆ singleton()

bool CLIST::singleton ( ) const
inline

Definition at line 97 of file clst.h.

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

◆ sort()

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

Definition at line 130 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;
155 for (i = 0; i < count; i++) {
156 it.add_to_end (*current);
157 current++;
158 }
159 free(base);
160}
int32_t length() const
Definition: clst.cpp:114

Friends And Related Function Documentation

◆ CLIST_ITERATOR

friend class CLIST_ITERATOR
friend

Definition at line 70 of file clst.h.


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