All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 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 113 of file elst.h.

Constructor & Destructor Documentation

ELIST::ELIST ( )
inline

Definition at line 124 of file elst.h.

124  { //constructor
125  last = NULL;
126  }
#define NULL
Definition: host.h:144

Member Function Documentation

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

Definition at line 174 of file elst.h.

175  {
176  return (add_sorted_and_find(comparator, unique, new_link) == new_link);
177  }
ELIST_LINK * add_sorted_and_find(int comparator(const void *, const void *), bool unique, ELIST_LINK *new_link)
Definition: elst.cpp:172
ELIST_LINK * ELIST::add_sorted_and_find ( int   comparatorconst void *, const void *,
bool  unique,
ELIST_LINK new_link 
)

Definition at line 172 of file elst.cpp.

174  {
175  // Check for adding at the end.
176  if (last == NULL || comparator(&last, &new_link) < 0) {
177  if (last == NULL) {
178  new_link->next = new_link;
179  } else {
180  new_link->next = last->next;
181  last->next = new_link;
182  }
183  last = new_link;
184  } else {
185  // Need to use an iterator.
186  ELIST_ITERATOR it(this);
187  for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
188  ELIST_LINK* link = it.data();
189  int compare = comparator(&link, &new_link);
190  if (compare > 0) {
191  break;
192  } else if (unique && compare == 0) {
193  return link;
194  }
195  }
196  if (it.cycled_list())
197  it.add_to_end(new_link);
198  else
199  it.add_before_then_move(new_link);
200  }
201  return new_link;
202 }
#define NULL
Definition: host.h:144
void ELIST::assign_to_sublist ( ELIST_ITERATOR start_it,
ELIST_ITERATOR end_it 
)

Definition at line 77 of file elst.cpp.

79  { //from list end
80  const ERRCODE LIST_NOT_EMPTY =
81  "Destination list must be empty before extracting a sublist";
82 
83  #ifndef NDEBUG
84  if (!this)
85  NULL_OBJECT.error ("ELIST::assign_to_sublist", ABORT, NULL);
86  #endif
87 
88  if (!empty ())
89  LIST_NOT_EMPTY.error ("ELIST.assign_to_sublist", ABORT, NULL);
90 
91  last = start_it->extract_sublist (end_it);
92 }
Definition: errcode.h:30
bool empty() const
Definition: elst.h:132
void error(const char *caller, TessErrorLogCode action, const char *format,...) const
Definition: errcode.cpp:40
#define NULL
Definition: host.h:144
const ERRCODE NULL_OBJECT
Definition: lsterr.h:33
bool ELIST::empty ( ) const
inline

Definition at line 132 of file elst.h.

132  { //is list empty?
133  return !last;
134  }
void ELIST::internal_clear ( void(*)(ELIST_LINK *)  zapper)

Definition at line 41 of file elst.cpp.

42  {
43  //ptr to zapper functn
44  ELIST_LINK *ptr;
45  ELIST_LINK *next;
46 
47  #ifndef NDEBUG
48  if (!this)
49  NULL_OBJECT.error ("ELIST::internal_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);
59  ptr = next;
60  }
61  }
62 }
Definition: errcode.h:30
bool empty() const
Definition: elst.h:132
void error(const char *caller, TessErrorLogCode action, const char *format,...) const
Definition: errcode.cpp:40
#define NULL
Definition: host.h:144
const ERRCODE NULL_OBJECT
Definition: lsterr.h:33
void ELIST::internal_deep_copy ( ELIST_LINK *(*)(ELIST_LINK *)  copier,
const ELIST list 
)
inT32 ELIST::length ( ) const

Definition at line 101 of file elst.cpp.

101  { // count elements
102  ELIST_ITERATOR it(const_cast<ELIST*>(this));
103  inT32 count = 0;
104 
105  #ifndef NDEBUG
106  if (!this)
107  NULL_OBJECT.error ("ELIST::length", ABORT, NULL);
108  #endif
109 
110  for (it.mark_cycle_pt (); !it.cycled_list (); it.forward ())
111  count++;
112  return count;
113 }
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 ELIST::shallow_copy ( ELIST from_list)
inline

Definition at line 140 of file elst.h.

141  { //beware destructors!!
142  last = from_list->last;
143  }
bool ELIST::singleton ( ) const
inline

Definition at line 136 of file elst.h.

136  {
137  return last ? (last == last->next) : false;
138  }
void ELIST::sort ( int   comparatorconst void *, const void *)

Definition at line 125 of file elst.cpp.

127  {
128  ELIST_ITERATOR it(this);
129  inT32 count;
130  ELIST_LINK **base; //ptr array to sort
131  ELIST_LINK **current;
132  inT32 i;
133 
134  #ifndef NDEBUG
135  if (!this)
136  NULL_OBJECT.error ("ELIST::sort", ABORT, NULL);
137  #endif
138 
139  /* Allocate an array of pointers, one per list element */
140  count = length ();
141  base = (ELIST_LINK **) malloc (count * sizeof (ELIST_LINK *));
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 ((char *) 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 }
Definition: errcode.h:30
inT32 length() const
Definition: elst.cpp:101
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 ELIST_ITERATOR
friend

Definition at line 115 of file elst.h.


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