Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

wvlinklist.cc

Go to the documentation of this file.
00001 /*
00002  * Worldvisions Weaver Software:
00003  *   Copyright (C) 1997-2002 Net Integration Technologies, Inc.
00004  * 
00005  * Implementation of a Linked List management class, or rather, macros that
00006  * declare arbitrary linked list management classes.
00007  * 
00008  * wvlinklist.h does all the real work.
00009  */
00010 #include "wvlinklist.h"
00011 
00012 WvLink::WvLink(void *_data, WvLink *prev, WvLink *&tail, bool _auto_free,
00013                char *_id)
00014 {
00015     data = _data;
00016     next = prev->next;
00017     if (!next) tail = this;
00018     prev->next = this;
00019     auto_free = (unsigned)_auto_free;
00020     id = _id;
00021 }
00022 
00023 
00024 size_t WvListBase::count() const
00025 {
00026     WvLink *l;
00027     size_t n = 0;
00028     
00029     for (l = head.next; l; l = l->next)
00030         n++;
00031     return n;
00032 }
00033 
00034 
00035 WvLink *WvListBase::IterBase::find(const void *data)
00036 {
00037     for (rewind(); next(); )
00038     {
00039         if (link->data == data)
00040             break;
00041     }
00042     
00043     return link;
00044 }
00045 
00046 
00047 #if 0
00048 static WvListBase::SorterBase::CompareFunc *actual_compare = NULL;
00049 
00050 static int magic_compare(const void *_a, const void *_b)
00051 {
00052     WvLink *a = *(WvLink **)_a, *b = *(WvLink **)_b;
00053     return actual_compare(a->data, b->data);
00054 }
00055 
00056 void WvListBase::SorterBase::rewind(CompareFunc *cmp)
00057 {
00058     if (array)
00059         delete array;
00060     array = lptr = NULL;
00061 
00062     int n = list->count();
00063     array = new WvLink * [n+1];
00064     WvLink **aptr = array;
00065 
00066     // fill the array with data pointers for sorting, so that the user doesn't
00067     // have to deal with the WvLink objects.  Put the WvLink pointers back 
00068     // in after sorting.
00069     IterBase i(*list);
00070     aptr = array;
00071     for (i.rewind(); i.next(); )
00072     {
00073         *aptr = i.cur();
00074         aptr++;
00075     }
00076     
00077     *aptr = NULL;
00078 
00079     // sort the array.  "Very nearly re-entrant" (unless the compare function
00080     // ends up being called recursively or something really weird...)
00081     CompareFunc *old_compare = actual_compare;
00082     actual_compare = cmp;
00083     qsort(array, n, sizeof(WvLink *), magic_compare);
00084     actual_compare = old_compare;
00085 
00086     lptr = NULL;    // subsequent next() will set it to first element.
00087 }
00088 #endif

Generated on Sat Aug 24 21:09:34 2002 for WvStreams by doxygen1.2.15