All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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
CLIST::~CLIST ( )
inline

Definition at line 85 of file clst.h.

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

Member Function Documentation

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

Definition at line 198 of file clst.cpp.

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

Definition at line 108 of file clst.cpp.

110  { //from list end
111  const ERRCODE LIST_NOT_EMPTY =
112  "Destination list must be empty before extracting a sublist";
113 
114  #ifndef NDEBUG
115  if (!this)
116  NULL_OBJECT.error ("CLIST::assign_to_sublist", ABORT, NULL);
117  #endif
118 
119  if (!empty ())
120  LIST_NOT_EMPTY.error ("CLIST.assign_to_sublist", ABORT, NULL);
121 
122  last = start_it->extract_sublist (end_it);
123 }
Definition: errcode.h:30
void error(const char *caller, TessErrorLogCode action, const char *format,...) const
Definition: errcode.cpp:40
bool empty() const
Definition: clst.h:95
#define NULL
Definition: host.h:144
const ERRCODE NULL_OBJECT
Definition: lsterr.h:33
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  #ifndef NDEBUG
47  if (!this)
48  NULL_OBJECT.error ("CLIST::internal_deep_clear", ABORT, NULL);
49  #endif
50 
51  if (!empty ()) {
52  ptr = last->next; //set to first
53  last->next = NULL; //break circle
54  last = NULL; //set list empty
55  while (ptr) {
56  next = ptr->next;
57  zapper (ptr->data);
58  delete(ptr);
59  ptr = next;
60  }
61  }
62 }
Definition: errcode.h:30
void error(const char *caller, TessErrorLogCode action, const char *format,...) const
Definition: errcode.cpp:40
bool empty() const
Definition: clst.h:95
#define NULL
Definition: host.h:144
const ERRCODE NULL_OBJECT
Definition: lsterr.h:33
inT32 CLIST::length ( ) const

Definition at line 132 of file clst.cpp.

132  { //count elements
133  CLIST_ITERATOR it(const_cast<CLIST*>(this));
134  inT32 count = 0;
135 
136  #ifndef NDEBUG
137  if (!this)
138  NULL_OBJECT.error ("CLIST::length", ABORT, NULL);
139  #endif
140 
141  for (it.mark_cycle_pt(); !it.cycled_list(); it.forward())
142  count++;
143  return count;
144 }
Definition: errcode.h:30
void error(const char *caller, TessErrorLogCode action, const char *format,...) const
Definition: errcode.cpp:40
int count(LIST var_list)
Definition: oldlist.cpp:108
#define NULL
Definition: host.h:144
const ERRCODE NULL_OBJECT
Definition: lsterr.h:33
int inT32
Definition: host.h:102
void CLIST::set_subtract ( int   comparatorconst void *, const void *,
bool  unique,
CLIST minuend,
CLIST subtrahend 
)

Definition at line 236 of file clst.cpp.

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

Definition at line 74 of file clst.cpp.

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

Definition at line 154 of file clst.cpp.

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

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: