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

Constructor & Destructor Documentation

◆ ELIST()

ELIST::ELIST ( )
inline

Definition at line 117 of file elst.h.

117 { //constructor
118 last = nullptr;
119 }

Member Function Documentation

◆ add_sorted()

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

Definition at line 167 of file elst.h.

168 {
169 return (add_sorted_and_find(comparator, unique, new_link) == new_link);
170 }
ELIST_LINK * add_sorted_and_find(int comparator(const void *, const void *), bool unique, ELIST_LINK *new_link)
Definition: elst.cpp:149

◆ 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 149 of file elst.cpp.

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

◆ assign_to_sublist()

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

Definition at line 71 of file elst.cpp.

73 { //from list end
74 constexpr ERRCODE LIST_NOT_EMPTY(
75 "Destination list must be empty before extracting a sublist");
76
77 if (!empty ())
78 LIST_NOT_EMPTY.error ("ELIST.assign_to_sublist", ABORT, nullptr);
79
80 last = start_it->extract_sublist (end_it);
81}
@ ABORT
Definition: errcode.h:29
bool empty() const
Definition: elst.h:125

◆ empty()

bool ELIST::empty ( ) const
inline

Definition at line 125 of file elst.h.

125 { //is list empty?
126 return !last;
127 }

◆ internal_clear()

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

Definition at line 40 of file elst.cpp.

41 {
42 //ptr to zapper functn
43 ELIST_LINK *ptr;
44 ELIST_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);
53 ptr = next;
54 }
55 }
56}

◆ 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 89 of file elst.cpp.

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

◆ shallow_copy()

void ELIST::shallow_copy ( ELIST from_list)
inline

Definition at line 133 of file elst.h.

134 { //beware destructors!!
135 last = from_list->last;
136 }

◆ singleton()

bool ELIST::singleton ( ) const
inline

Definition at line 129 of file elst.h.

129 {
130 return last ? (last == last->next) : false;
131 }
LIST last(LIST var_list)
Definition: oldlist.cpp:190
list_rec * next
Definition: oldlist.h:83

◆ sort()

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

Definition at line 107 of file elst.cpp.

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

Friends And Related Function Documentation

◆ ELIST_ITERATOR

friend class ELIST_ITERATOR
friend

Definition at line 108 of file elst.h.


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