| 1 |
// { dg-do run } |
| 2 |
// { dg-skip-if "requires hosted libstdc++ for stdlib qsort" { ! hostedlib } } |
| 3 |
|
| 4 |
#include <stdlib.h> |
| 5 |
#include <string.h> |
| 6 |
|
| 7 |
template <class T> |
| 8 |
class List { |
| 9 |
public: |
| 10 |
int len; |
| 11 |
T *array; |
| 12 |
|
| 13 |
int length() const { return( len ); } |
| 14 |
|
| 15 |
List() : len( 0 ), array( 0 ) {} |
| 16 |
}; |
| 17 |
|
| 18 |
template <class T> |
| 19 |
int AlgoStdCompare(const T* a, const T* b) { |
| 20 |
if (*a < *b) |
| 21 |
return -1; |
| 22 |
else |
| 23 |
return (*a > *b); // 0 if equal, 1 if greater |
| 24 |
} |
| 25 |
|
| 26 |
int AlgoStdCompare(const char* const* a, const char * const*b) |
| 27 |
{ |
| 28 |
return strcmp(*a,*b); |
| 29 |
} |
| 30 |
|
| 31 |
template <class T> |
| 32 |
void AlgoFixupSort(List< T >* , int, int ) { |
| 33 |
} |
| 34 |
|
| 35 |
template <class T> |
| 36 |
void AlgoSort(int (*compare)(const T *, const T *), |
| 37 |
void (*fixup)( List<T> *, int first, int last), |
| 38 |
List< T >* theList, int first, int last) { |
| 39 |
if (last < 0) |
| 40 |
last = theList->length()-1; |
| 41 |
|
| 42 |
qsort(theList->array+first, last-first+1, sizeof(T), |
| 43 |
(int (*)(const void *, const void *))compare); |
| 44 |
if (fixup) |
| 45 |
fixup(theList, first, last); |
| 46 |
} |
| 47 |
|
| 48 |
template <class T> |
| 49 |
void AlgoSort(List< T >* theList, int first = 0, int last = -1) { |
| 50 |
int (*compare)(const T*, const T*) = AlgoStdCompare; |
| 51 |
void (*fixup)( List<T> *, int first, int last) = AlgoFixupSort; |
| 52 |
|
| 53 |
AlgoSort(compare, fixup, theList, first, last); |
| 54 |
} |
| 55 |
|
| 56 |
int |
| 57 |
main() |
| 58 |
{ |
| 59 |
List<const char *> slist; |
| 60 |
AlgoSort( &slist ); |
| 61 |
|
| 62 |
List<int> ilist; |
| 63 |
AlgoSort( &ilist ); |
| 64 |
} |