CLIST Class Reference

#include <clst.h>

List of all members.


Detailed Description

Generic list class for singly linked CONS cell lists.

Definition at line 104 of file clst.h.

Public Member Functions

Private Member Functions

Private Attributes

Friends


Constructor & Destructor Documentation

CLIST::CLIST (  )  [inline]

Definition at line 115 of file clst.h.

References last(), and NULL.

00115             {  //constructor
00116       last = NULL;
00117     }

CLIST::~CLIST (  )  [inline]

Definition at line 119 of file clst.h.

00119               {                  //destructor
00120       shallow_clear();
00121     }


Member Function Documentation

void CLIST::assign_to_sublist ( CLIST_ITERATOR start_it,
CLIST_ITERATOR end_it 
)

The list is set to a sublist of another list.

"This" list must be empty before this function is invoked.

The two iterators passed must refer to the same list, different from "this" one. The sublist removed is the inclusive list from start_it's current position to end_it's current position. If this range passes over the end of the source list then the source list has its end set to the previous element of start_it.

The extracted sublist is unaffected by the end point of the source list, its end point is always the end_it position.

Definition at line 136 of file clst.cpp.

References ABORT, empty(), ERRCODE::error(), CLIST_ITERATOR::extract_sublist(), last, and NULL.

00138                                                       {  //from list end
00139   const ERRCODE LIST_NOT_EMPTY =
00140     "Destination list must be empty before extracting a sublist";
00141 
00142   #ifdef _DEBUG
00143   if (!this)
00144     NULL_OBJECT.error ("CLIST::assign_to_sublist", ABORT, NULL);
00145   #endif
00146 
00147   if (!empty ())
00148     LIST_NOT_EMPTY.error ("CLIST.assign_to_sublist", ABORT, NULL);
00149 
00150   last = start_it->extract_sublist (end_it);
00151 }

BOOL8 CLIST::empty (  )  [inline]

Definition at line 129 of file clst.h.

References last().

Referenced by CLIST_ITERATOR::add_after_stay_put(), CLIST_ITERATOR::add_after_then_move(), CLIST_ITERATOR::add_before_stay_put(), CLIST_ITERATOR::add_before_then_move(), CLIST_ITERATOR::add_list_after(), CLIST_ITERATOR::add_list_before(), assign_to_sublist(), CLIST_ITERATOR::at_first(), CLIST_ITERATOR::at_last(), CLIST_ITERATOR::cycled_list(), CLIST_ITERATOR::data_relative(), CLIST_ITERATOR::exchange(), CLIST_ITERATOR::extract_sublist(), CLIST_ITERATOR::forward(), internal_deep_clear(), internal_dump(), prep_serialise(), and shallow_clear().

00129                   {  //is list empty?
00130       return !last;
00131     }

CLIST_LINK* CLIST::First (  )  [inline, private]

Definition at line 110 of file clst.h.

References last(), list_rec::next, and NULL.

Referenced by CLIST_ITERATOR::add_list_after(), CLIST_ITERATOR::add_list_before(), CLIST_ITERATOR::at_first(), CLIST_ITERATOR::move_to_first(), and CLIST_ITERATOR::set_to_list().

00110                       {  // return first
00111     return last != NULL ? last->next : NULL;
00112   }

void CLIST::internal_de_dump ( FILE *  f,
void *  element_de_serialiser(FILE *) 
)

Cause each element on the list to be de_serialised by extracting the count of elements on the list, (held in the last member of the dumped version of the list object), and then de-serialising that number of list elements, adding each to the end of the reconstructed list.

Definition at line 271 of file clst.cpp.

References ABORT, CLIST_ITERATOR::add_to_end(), count(), last, NULL, and CLIST_ITERATOR::set_to_list().

00271                                                                        {
00272   INT32 count = (ptrdiff_t) last;
00273   CLIST_ITERATOR this_it;
00274 
00275   #ifdef _DEBUG
00276   if (!this)
00277     NULL_OBJECT.error ("CLIST::internal_de_dump", ABORT, NULL);
00278   #endif
00279 
00280   last = NULL;
00281   this_it.set_to_list (this);
00282   for (; count > 0; count--)
00283     this_it.add_to_end (element_de_serialiser (f));
00284 }

void CLIST::internal_deep_clear ( void(*)(void *)  zapper  ) 

Used by the "deep_clear" member function of derived list classes to destroy all the elements on the list.

The calling function passes a "zapper" function which can be called to delete each data element of the list, regardless of its class. This technique permits a generic clear function to destroy elements of different derived types correctly, without requiring virtual functions and the consequential memory overhead.

Definition at line 40 of file clst.cpp.

References ABORT, CLIST_LINK::data, empty(), last, CLIST_LINK::next, and NULL.

00041                          {       //ptr to zapper functn
00042   CLIST_LINK *ptr;
00043   CLIST_LINK *next;
00044 
00045   #ifdef _DEBUG
00046   if (!this)
00047     NULL_OBJECT.error ("CLIST::internal_deep_clear", ABORT, NULL);
00048   #endif
00049 
00050   if (!empty ()) {
00051     ptr = last->next;            //set to first
00052     last->next = NULL;           //break circle
00053     last = NULL;                 //set list empty
00054     while (ptr) {
00055       next = ptr->next;
00056       zapper (ptr->data);
00057       delete(ptr);
00058       ptr = next;
00059     }
00060   }
00061 }

void CLIST::internal_deep_copy ( void *(*)(void *)  copier,
const CLIST list 
)

Used during explict deep copy of a list.

The "copier" function passed allows each element to be correctly deep copied (assuming that each class in the inheritance hierarchy does properly deep copies its members). The function passing technique is as for "internal_clear".

Definition at line 102 of file clst.cpp.

References ABORT, CLIST_ITERATOR::add_after_then_move(), CLIST_ITERATOR::cycled_list(), CLIST_ITERATOR::data(), CLIST_ITERATOR::forward(), CLIST_ITERATOR::mark_cycle_pt(), and NULL.

00104                        {            //list being copied
00105   CLIST_ITERATOR from_it ((CLIST *) list);
00106   CLIST_ITERATOR to_it(this);
00107 
00108   #ifdef _DEBUG
00109   if (!this)
00110     NULL_OBJECT.error ("CLIST::internal_deep_copy", ABORT, NULL);
00111   if (!list)
00112     BAD_PARAMETER.error ("CLIST::internal_deep_copy", ABORT,
00113       "source list is NULL");
00114   #endif
00115 
00116   for (from_it.mark_cycle_pt (); !from_it.cycled_list (); from_it.forward ())
00117     to_it.add_after_then_move (copier (from_it.data ()));
00118 }

void CLIST::internal_dump ( FILE *  f,
void   element_serialiser(FILE *, void *) 
)

Cause each element on the list to be serialised by walking the list and calling the element_serialiser function for each element.

The element_serialiser simply does the appropriate coercion of the element to its real type and then invokes the elements serialise function

Definition at line 249 of file clst.cpp.

References ABORT, CLIST_ITERATOR::cycled_list(), CLIST_ITERATOR::data(), empty(), CLIST_ITERATOR::forward(), CLIST_ITERATOR::mark_cycle_pt(), and NULL.

00249                                                                         {
00250   CLIST_ITERATOR this_it(this);
00251 
00252   #ifdef _DEBUG
00253   if (!this)
00254     NULL_OBJECT.error ("CLIST::internal_dump", ABORT, NULL);
00255   #endif
00256 
00257   if (!empty ())
00258     for (this_it.mark_cycle_pt ();
00259     !this_it.cycled_list (); this_it.forward ())
00260   element_serialiser (f, this_it.data ());
00261 }

INT32 CLIST::length (  ) 

Return count of elements on list.

Definition at line 157 of file clst.cpp.

References ABORT, count(), CLIST_ITERATOR::cycled_list(), CLIST_ITERATOR::forward(), CLIST_ITERATOR::mark_cycle_pt(), and NULL.

Referenced by CLIST_ITERATOR::length(), and sort().

00157                     {  //count elements
00158   CLIST_ITERATOR it(this);
00159   INT32 count = 0;
00160 
00161   #ifdef _DEBUG
00162   if (!this)
00163     NULL_OBJECT.error ("CLIST::length", ABORT, NULL);
00164   #endif
00165 
00166   for (it.mark_cycle_pt (); !it.cycled_list (); it.forward ())
00167     count++;
00168   return count;
00169 }

void CLIST::prep_serialise (  ) 

Replace the last member with a count of elements for serialisation.

This is used on list objects which are members of objects being serialised. The containing object has been shallow copied and this member function is invoked on the COPY.

Definition at line 221 of file clst.cpp.

References ABORT, count(), CLIST_ITERATOR::cycled_list(), empty(), CLIST_ITERATOR::forward(), last, CLIST_ITERATOR::mark_cycle_pt(), and NULL.

00221                            {
00222   CLIST_ITERATOR this_it(this);
00223   INT32 count = 0;
00224 
00225   #ifdef _DEBUG
00226   if (!this)
00227     NULL_OBJECT.error ("CLIST::prep_serialise", ABORT, NULL);
00228   #endif
00229 
00230   count = 0;
00231   if (!empty ())
00232     for (this_it.mark_cycle_pt ();
00233     !this_it.cycled_list (); this_it.forward ())
00234   count++;
00235   last = (CLIST_LINK *) count;
00236 }

void CLIST::shallow_clear (  ) 

Destroy all links.

Used by the destructor and the "shallow_clear" member function of derived list classes to destroy the list.

The data elements are NOT destroyed.

Definition at line 72 of file clst.cpp.

References ABORT, empty(), last, CLIST_LINK::next, and NULL.

00072                           {  //destroy all links
00073   CLIST_LINK *ptr;
00074   CLIST_LINK *next;
00075 
00076   #ifdef _DEBUG
00077   if (!this)
00078     NULL_OBJECT.error ("CLIST::shallow_clear", ABORT, NULL);
00079   #endif
00080 
00081   if (!empty ()) {
00082     ptr = last->next;            //set to first
00083     last->next = NULL;           //break circle
00084     last = NULL;                 //set list empty
00085     while (ptr) {
00086       next = ptr->next;
00087       delete(ptr);
00088       ptr = next;
00089     }
00090   }
00091 }

void CLIST::shallow_copy ( CLIST from_list  )  [inline]

Definition at line 137 of file clst.h.

References last, and last().

00138                                         {  //beware destructors!!
00139       last = from_list->last;
00140     }

BOOL8 CLIST::singleton (  )  [inline]

Definition at line 133 of file clst.h.

References FALSE, last(), list_rec::next, and NULL.

Referenced by CLIST_ITERATOR::extract().

00133                       {
00134       return last != NULL ? (last == last->next) : FALSE;
00135     }

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

Sort elements on list.

Definition at line 176 of file clst.cpp.

References ABORT, CLIST_ITERATOR::add_to_end(), count(), CLIST_ITERATOR::cycled_list(), CLIST_ITERATOR::extract(), CLIST_ITERATOR::forward(), length(), CLIST_ITERATOR::mark_cycle_pt(), and NULL.

Referenced by CLIST_ITERATOR::sort().

00178                              {
00179   CLIST_ITERATOR it(this);
00180   INT32 count;
00181   void **base;                   //ptr array to sort
00182   void **current;
00183   INT32 i;
00184 
00185   #ifdef _DEBUG
00186   if (!this)
00187     NULL_OBJECT.error ("CLIST::sort", ABORT, NULL);
00188   #endif
00189 
00190   /* Allocate an array of pointers, one per list element */
00191   count = length ();
00192   base = (void **) malloc (count * sizeof (void *));
00193 
00194   /* Extract all elements, putting the pointers in the array */
00195   current = base;
00196   for (it.mark_cycle_pt (); !it.cycled_list (); it.forward ()) {
00197     *current = it.extract ();
00198     current++;
00199   }
00200 
00201   /* Sort the pointer array */
00202   qsort ((char *) base, count, sizeof (*base), comparator);
00203 
00204   /* Rebuild the list from the sorted pointers */
00205   current = base;
00206   for (i = 0; i < count; i++) {
00207     it.add_to_end (*current);
00208     current++;
00209   }
00210   free(base);
00211 }


Friends And Related Function Documentation

friend class CLIST_ITERATOR [friend]

Definition at line 106 of file clst.h.


Member Data Documentation

CLIST_LINK* CLIST::last [private]

Definition at line 108 of file clst.h.

Referenced by CLIST_ITERATOR::add_after_stay_put(), CLIST_ITERATOR::add_after_then_move(), CLIST_ITERATOR::add_before_stay_put(), CLIST_ITERATOR::add_before_then_move(), CLIST_ITERATOR::add_list_after(), CLIST_ITERATOR::add_list_before(), CLIST_ITERATOR::add_to_end(), assign_to_sublist(), CLIST_ITERATOR::at_first(), CLIST_ITERATOR::at_last(), CLIST_ITERATOR::exchange(), CLIST_ITERATOR::extract(), CLIST_ITERATOR::extract_sublist(), internal_de_dump(), internal_deep_clear(), CLIST_ITERATOR::move_to_first(), CLIST_ITERATOR::move_to_last(), prep_serialise(), CLIST_ITERATOR::set_to_list(), shallow_clear(), and shallow_copy().


The documentation for this class was generated from the following files:
Generated on Wed Feb 28 19:49:30 2007 for Tesseract by  doxygen 1.5.1