tesseract  3.04.01
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 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 ( )
inline

Definition at line 81 of file clst.h.

81  { //constructor
82  last = NULL;
83  }
CLIST::~CLIST ( )
inline

Definition at line 85 of file clst.h.

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

Member Function Documentation

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

Definition at line 173 of file clst.cpp.

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

Definition at line 98 of file clst.cpp.

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

Definition at line 95 of file clst.h.

95  { //is list empty?
96  return !last;
97  }
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 = NULL; //break circle
49  last = NULL; //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
inT32 CLIST::length ( ) const

Definition at line 117 of file clst.cpp.

117  { //count elements
118  CLIST_ITERATOR it(const_cast<CLIST*>(this));
119  inT32 count = 0;
120 
121  for (it.mark_cycle_pt(); !it.cycled_list(); it.forward())
122  count++;
123  return count;
124 }
int inT32
Definition: host.h:102
int count(LIST var_list)
Definition: oldlist.cpp:108
void CLIST::set_subtract ( int   comparatorconst void *, const void *,
bool  unique,
CLIST minuend,
CLIST subtrahend 
)

Definition at line 211 of file clst.cpp.

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

Definition at line 69 of file clst.cpp.

69  { //destroy all links
70  CLIST_LINK *ptr;
71  CLIST_LINK *next;
72 
73  if (!empty ()) {
74  ptr = last->next; //set to first
75  last->next = NULL; //break circle
76  last = NULL; //set list empty
77  while (ptr) {
78  next = ptr->next;
79  delete(ptr);
80  ptr = next;
81  }
82  }
83 }
bool empty() const
Definition: clst.h:95
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  }
bool CLIST::singleton ( ) const
inline

Definition at line 99 of file clst.h.

99  {
100  return last != NULL ? (last == last->next) : false;
101  }
void CLIST::sort ( int   comparatorconst void *, const void *)

Definition at line 134 of file clst.cpp.

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

Friends And Related Function Documentation

friend class CLIST_ITERATOR
friend

Definition at line 72 of file clst.h.


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