00001
00002
00003
00004
00005
00006
00007
00008
00009
#ifndef __WVSORTER_H
00010
#define __WVSORTER_H
00011
00012
#include "wvlink.h"
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 class WvSorterBase
00023 {
00024
public:
00025 typedef int (CompareFunc)(
const void *a,
const void *b);
00026
00027 void *
list;
00028 void **
array;
00029 void **
lptr;
00030
00031 WvSorterBase(
void *_list)
00032 {
list = _list;
array =
lptr = NULL; }
00033 ~WvSorterBase()
00034 {
if (
array)
delete[]
array; }
00035 bool next()
00036 {
return *(++
lptr) != 0; }
00037 bool cur()
00038 {
return *
lptr != 0; }
00039
00040
protected:
00041
template <
class _list_,
class _iter_>
void rewind(CompareFunc *cmp);
00042
00043
static int magic_compare(
const void *_a,
const void *_b);
00044
static CompareFunc *actual_compare;
00045 };
00046
00047
00048
00049
00050
00051
00052
00053
00054
template <
class _type_,
class _list_,
class _iter_>
00055 class WvSorter :
public WvSorterBase
00056 {
00057
public:
00058 typedef int (RealCompareFunc)(
const _type_ *a,
const _type_ *b);
00059 RealCompareFunc *
cmp;
00060
00061 WvSorter(_list_ &_list, RealCompareFunc *_cmp)
00062 :
WvSorterBase(&_list)
00063 {
cmp = _cmp; }
00064 _type_ *
ptr()
const
00065
{
return (_type_ *)(*lptr); }
00066
00067
00068
WvIterStuff(_type_);
00069
00070 void rewind()
00071 { WvSorterBase::rewind<_list_,_iter_>((CompareFunc *)
cmp); }
00072 };
00073
00074
00075
00076
00077
template <
class _list_,
class _iter_>
00078 void WvSorterBase::rewind(CompareFunc *cmp)
00079 {
00080
int n, remaining;
00081
00082
if (
array)
00083
delete[]
array;
00084
array =
lptr = NULL;
00085
00086 _iter_ i(*(_list_ *)
list);
00087
00088
00089 n = 0;
00090
for (i.rewind(); i.next(); )
00091 n++;
00092
00093
array =
new void * [n+2];
00094
void **aptr =
array;
00095
00096 *aptr++ = NULL;
00097
00098
for (remaining = n, i.rewind(); i.next() && remaining; remaining--)
00099 {
00100 *aptr = i.vptr();
00101 aptr++;
00102 }
00103
00104
00105
00106
if (remaining)
00107 n -= remaining;
00108
00109 *aptr = NULL;
00110
00111
00112
00113
CompareFunc *old_compare =
actual_compare;
00114 actual_compare = cmp;
00115 qsort(array+1, n,
sizeof(
void *),
magic_compare);
00116 actual_compare = old_compare;
00117
00118 lptr = array;
00119 }
00120
00121
00122
#endif // __WVSORTER_H