Tesseract  3.02
 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  }
#define NULL
Definition: host.h:144
LIST last(LIST var_list)
Definition: oldlist.cpp:277
CLIST::~CLIST ( )
inline

Definition at line 85 of file clst.h.

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

Member Function Documentation

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

Definition at line 199 of file clst.cpp.

200  {
201  // Check for adding at the end.
202  if (last == NULL || comparator(&last->data, &new_data) < 0) {
203  CLIST_LINK* new_element = new CLIST_LINK;
204  new_element->data = new_data;
205  if (last == NULL) {
206  new_element->next = new_element;
207  } else {
208  new_element->next = last->next;
209  last->next = new_element;
210  }
211  last = new_element;
212  return true;
213  } else if (!unique || last->data != new_data) {
214  // Need to use an iterator.
215  CLIST_ITERATOR it(this);
216  for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
217  void* data = it.data();
218  if (data == new_data && unique)
219  return false;
220  if (comparator(&data, &new_data) > 0)
221  break;
222  }
223  if (it.cycled_list())
224  it.add_to_end(new_data);
225  else
226  it.add_before_then_move(new_data);
227  return true;
228  }
229  return false;
230 }
#define NULL
Definition: host.h:144
LIST last(LIST var_list)
Definition: oldlist.cpp:277
struct list_rec * next
Definition: oldlist.h:130
void CLIST::assign_to_sublist ( CLIST_ITERATOR start_it,
CLIST_ITERATOR end_it 
)

Definition at line 109 of file clst.cpp.

111  { //from list end
112  const ERRCODE LIST_NOT_EMPTY =
113  "Destination list must be empty before extracting a sublist";
114 
115  #ifndef NDEBUG
116  if (!this)
117  NULL_OBJECT.error ("CLIST::assign_to_sublist", ABORT, NULL);
118  #endif
119 
120  if (!empty ())
121  LIST_NOT_EMPTY.error ("CLIST.assign_to_sublist", ABORT, NULL);
122 
123  last = start_it->extract_sublist (end_it);
124 }
Definition: errcode.h:30
#define NULL
Definition: host.h:144
const ERRCODE NULL_OBJECT
Definition: lsterr.h:33
LIST last(LIST var_list)
Definition: oldlist.cpp:277
void error(const char *caller, TessErrorLogCode action, const char *format,...) const
Definition: errcode.cpp:41
bool empty() const
Definition: clst.h:95
bool CLIST::empty ( ) const
inline

Definition at line 95 of file clst.h.

95  { //is list empty?
96  return !last;
97  }
LIST last(LIST var_list)
Definition: oldlist.cpp:277
void CLIST::internal_deep_clear ( void(*)(void *)  zapper)

Definition at line 42 of file clst.cpp.

43  { //ptr to zapper functn
44  CLIST_LINK *ptr;
45  CLIST_LINK *next;
46 
47  #ifndef NDEBUG
48  if (!this)
49  NULL_OBJECT.error ("CLIST::internal_deep_clear", ABORT, NULL);
50  #endif
51 
52  if (!empty ()) {
53  ptr = last->next; //set to first
54  last->next = NULL; //break circle
55  last = NULL; //set list empty
56  while (ptr) {
57  next = ptr->next;
58  zapper (ptr->data);
59  delete(ptr);
60  ptr = next;
61  }
62  }
63 }
Definition: errcode.h:30
#define NULL
Definition: host.h:144
const ERRCODE NULL_OBJECT
Definition: lsterr.h:33
LIST last(LIST var_list)
Definition: oldlist.cpp:277
void error(const char *caller, TessErrorLogCode action, const char *format,...) const
Definition: errcode.cpp:41
struct list_rec * next
Definition: oldlist.h:130
bool empty() const
Definition: clst.h:95
inT32 CLIST::length ( ) const

Definition at line 133 of file clst.cpp.

133  { //count elements
134  CLIST_ITERATOR it(const_cast<CLIST*>(this));
135  inT32 count = 0;
136 
137  #ifndef NDEBUG
138  if (!this)
139  NULL_OBJECT.error ("CLIST::length", ABORT, NULL);
140  #endif
141 
142  for (it.mark_cycle_pt(); !it.cycled_list(); it.forward())
143  count++;
144  return count;
145 }
Definition: errcode.h:30
#define NULL
Definition: host.h:144
int inT32
Definition: host.h:102
const ERRCODE NULL_OBJECT
Definition: lsterr.h:33
void error(const char *caller, TessErrorLogCode action, const char *format,...) const
Definition: errcode.cpp:41
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 237 of file clst.cpp.

239  {
240  shallow_clear();
241  CLIST_ITERATOR m_it(minuend);
242  CLIST_ITERATOR s_it(subtrahend);
243  // Since both lists are sorted, finding the subtras that are not
244  // minus is a case of a parallel iteration.
245  for (m_it.mark_cycle_pt(); !m_it.cycled_list(); m_it.forward()) {
246  void* minu = m_it.data();
247  void* subtra = NULL;
248  if (!s_it.empty()) {
249  subtra = s_it.data();
250  while (!s_it.at_last() &&
251  comparator(&subtra, &minu) < 0) {
252  s_it.forward();
253  subtra = s_it.data();
254  }
255  }
256  if (subtra == NULL || comparator(&subtra, &minu) != 0)
257  add_sorted(comparator, unique, minu);
258  }
259 }
#define NULL
Definition: host.h:144
bool add_sorted(int comparator(const void *, const void *), bool unique, void *new_data)
Definition: clst.cpp:199
void shallow_clear()
Definition: clst.cpp:75
void CLIST::shallow_clear ( )

Definition at line 75 of file clst.cpp.

75  { //destroy all links
76  CLIST_LINK *ptr;
77  CLIST_LINK *next;
78 
79  #ifndef NDEBUG
80  if (!this)
81  NULL_OBJECT.error ("CLIST::shallow_clear", ABORT, NULL);
82  #endif
83 
84  if (!empty ()) {
85  ptr = last->next; //set to first
86  last->next = NULL; //break circle
87  last = NULL; //set list empty
88  while (ptr) {
89  next = ptr->next;
90  delete(ptr);
91  ptr = next;
92  }
93  }
94 }
Definition: errcode.h:30
#define NULL
Definition: host.h:144
const ERRCODE NULL_OBJECT
Definition: lsterr.h:33
LIST last(LIST var_list)
Definition: oldlist.cpp:277
void error(const char *caller, TessErrorLogCode action, const char *format,...) const
Definition: errcode.cpp:41
struct list_rec * next
Definition: oldlist.h:130
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  }
LIST last(LIST var_list)
Definition: oldlist.cpp:277
bool CLIST::singleton ( ) const
inline

Definition at line 99 of file clst.h.

99  {
100  return last != NULL ? (last == last->next) : false;
101  }
#define NULL
Definition: host.h:144
LIST last(LIST var_list)
Definition: oldlist.cpp:277
struct list_rec * next
Definition: oldlist.h:130
void CLIST::sort ( int   comparatorconst void *, const void *)

Definition at line 155 of file clst.cpp.

157  {
158  CLIST_ITERATOR it(this);
159  inT32 count;
160  void **base; //ptr array to sort
161  void **current;
162  inT32 i;
163 
164  #ifndef NDEBUG
165  if (!this)
166  NULL_OBJECT.error ("CLIST::sort", ABORT, NULL);
167  #endif
168 
169  /* Allocate an array of pointers, one per list element */
170  count = length ();
171  base = (void **) malloc (count * sizeof (void *));
172 
173  /* Extract all elements, putting the pointers in the array */
174  current = base;
175  for (it.mark_cycle_pt (); !it.cycled_list (); it.forward ()) {
176  *current = it.extract ();
177  current++;
178  }
179 
180  /* Sort the pointer array */
181  qsort ((char *) base, count, sizeof (*base), comparator);
182 
183  /* Rebuild the list from the sorted pointers */
184  current = base;
185  for (i = 0; i < count; i++) {
186  it.add_to_end (*current);
187  current++;
188  }
189  free(base);
190 }
void * base[2]
Definition: tessarray.h:53
Definition: errcode.h:30
#define NULL
Definition: host.h:144
int inT32
Definition: host.h:102
inT32 length() const
Definition: clst.cpp:133
const ERRCODE NULL_OBJECT
Definition: lsterr.h:33
void error(const char *caller, TessErrorLogCode action, const char *format,...) const
Definition: errcode.cpp:41
int count(LIST var_list)
Definition: oldlist.cpp:108

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: