Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

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 void WvListBase::reverse()
00036 {
00037     WvLink *prev, *curr, *next;
00038 
00039     if (!head.next || !head.next->next)
00040         return;
00041     
00042     prev = head.next;
00043     curr = prev->next; 
00044    
00045     do {
00046         next = curr->next;
00047         curr->next = prev;
00048         prev = curr;
00049         curr = next;
00050     } while(curr);
00051     
00052     tail = head.next;
00053     tail->next = NULL;
00054     head.next = prev;
00055 }
00056 
00057 
00058 WvLink *WvListBase::IterBase::find(const void *data)
00059 {
00060     for (rewind(); next(); )
00061     {
00062         if (link->data == data)
00063             break;
00064     }
00065     
00066     return link;
00067 }
00068 
00069 
00070 #if 0
00071 static WvListBase::SorterBase::CompareFunc *actual_compare = NULL;
00072 
00073 static int magic_compare(const void *_a, const void *_b)
00074 {
00075     WvLink *a = *(WvLink **)_a, *b = *(WvLink **)_b;
00076     return actual_compare(a->data, b->data);
00077 }
00078 
00079 void WvListBase::SorterBase::rewind(CompareFunc *cmp)
00080 {
00081     if (array)
00082         delete array;
00083     array = lptr = NULL;
00084 
00085     int n = list->count();
00086     array = new WvLink * [n+1];
00087     WvLink **aptr = array;
00088 
00089     // fill the array with data pointers for sorting, so that the user doesn't
00090     // have to deal with the WvLink objects.  Put the WvLink pointers back 
00091     // in after sorting.
00092     IterBase i(*list);
00093     aptr = array;
00094     for (i.rewind(); i.next(); )
00095     {
00096         *aptr = i.cur();
00097         aptr++;
00098     }
00099     
00100     *aptr = NULL;
00101 
00102     // sort the array.  "Very nearly re-entrant" (unless the compare function
00103     // ends up being called recursively or something really weird...)
00104     CompareFunc *old_compare = actual_compare;
00105     actual_compare = cmp;
00106     qsort(array, n, sizeof(WvLink *), magic_compare);
00107     actual_compare = old_compare;
00108 
00109     lptr = NULL;    // subsequent next() will set it to first element.
00110 }
00111 #endif

Generated on Wed Dec 15 15:08:11 2004 for WvStreams by  doxygen 1.3.9.1