2 * Worldvisions Weaver Software:
3 * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
5 * Implementation of a Linked List management class, or rather, macros that
6 * declare arbitrary linked list management classes.
8 * wvlinklist.h does all the real work.
10 #include "wvlinklist.h"
12 WvLink::WvLink(void *_data
, WvLink
*prev
, WvLink
*&tail
, bool _autofree
,
17 if (!next
) tail
= this;
24 size_t WvListBase::count() const
29 for (l
= head
.next
; l
; l
= l
->next
)
35 void WvListBase::reverse()
37 WvLink
*prev
, *curr
, *next
;
39 if (!head
.next
|| !head
.next
->next
)
58 WvLink
*WvListBase::IterBase::find(const void *data
)
60 for (rewind(); next(); )
61 if (link
->data
== data
)
66 WvLink
*WvListBase::IterBase::find_next(const void *data
)
70 if (link
->data
== data
)
73 for (rewind(); next(); )
74 if (link
->data
== data
)
82 static WvListBase::SorterBase::CompareFunc
*actual_compare
= NULL
;
84 static int magic_compare(const void *_a
, const void *_b
)
86 WvLink
*a
= *(WvLink
**)_a
, *b
= *(WvLink
**)_b
;
87 return actual_compare(a
->data
, b
->data
);
90 void WvListBase::SorterBase::rewind(CompareFunc
*cmp
)
96 int n
= list
->count();
97 array
= new WvLink
* [n
+1];
98 WvLink
**aptr
= array
;
100 // fill the array with data pointers for sorting, so that the user doesn't
101 // have to deal with the WvLink objects. Put the WvLink pointers back
105 for (i
.rewind(); i
.next(); )
113 // sort the array. "Very nearly re-entrant" (unless the compare function
114 // ends up being called recursively or something really weird...)
115 CompareFunc
*old_compare
= actual_compare
;
116 actual_compare
= cmp
;
117 qsort(array
, n
, sizeof(WvLink
*), magic_compare
);
118 actual_compare
= old_compare
;
120 lptr
= NULL
; // subsequent next() will set it to first element.