(C) 1998 AROS - The Amiga Research OS


NAME

ULONG QSort (baseptr , region_size , byte_size , user_function , region_size , )

LOCATION
In ArpBase at offset 71

SYNOPSIS
void * baseptr
ULONG region_size
ULONG byte_size
LONG_FUNC user_function
stkerr = QSort( baseptr region_size
d0 a0 d0 d1 a1

FUNCTION
QSort -- Quickly sort whatever you want.

QSort is an implementation of Hoares sorting algorithm. It uses a constant amount of stack no matter what the size of the file.

INPUTS
baseptr
pointer to the start of a memory region to be sorted
region_size
size of region to be sorted, in number of elements (not bytes!)
byte_size
size of sorted elements, in bytes.
user_function
function to be provided by caller, which compares two elements from the set to be sorted. QSort will call the user function like so:

return = user_function(el1, el2) d0 a0 a1

Your function must return the following in D0:

if (el1 < el2) return < 0 if (el1 > el2) return > 0 if (el1 == el2) return = 0

You must save all registers except a0, a1, d0, d1. (See below for an example of a C calling sequence)

QSort will also pass A5 to you unchanged. You can use this register to point to pass information to your comparison routine.

RETURNS -1 if everything is cool, 0 if there was an internal recursion stack overflow (not too likely).

EXAMPLE: Here is an example of a calling sequence from C, which is to sort an array of pointers to strings:

char **names; long numEls; extern Cmp();

if (QSort(names, numELs, 4L, Cmp)) do whatever else STACK_ERROR

the Cmp function would look like this:

Cmp() { { #asm public _geta4 movem.l d2-d3/a4/a6,-(sp) ; save important registers movem.l a0/a1,-(sp) ; push args bsr _geta4 bsr _cmp ; call real compare function addq.l #8,sp ; clean up args movem.l (sp)+,d2-d3/a4/a6 ; restore registers #endasm } }

The cmp function called by the above is a normal C function, it can be whatever you like. Here is a sample one:

cmp(s1,s2) char **s1, **s2; { return strcmp(*a, *b); }

RESULT

EXAMPLE
SEE ALSO

NOTES

BUGS
None known.

INTERNALS

HISTORY
09.05.1999 bergers
Compile correctly now.
20.10.1998 hkiel
Amiga Research OS
14.04.1998 bergers
Bugfix.
06.04.1998 bergers
New function.