home *** CD-ROM | disk | FTP | other *** search
- /*
- * sorts.cf -- non-ANSI
- *
- * Sorting algorithms.
- *
- * Copyright (c) 1990, MetaWare Incorporated
- */
-
- #ifndef _SORTS_CF
- #define _SORTS_CF
-
- #include <implemen.cf>
- #include <language.cf>
-
- #pragma Global_aliasing_convention(_Private_routine_prefix "%r");
- #pragma Calling_convention(PASCAL);
-
- #if 0
- { Sort items L..H. Less_than returns whether item I is less than item }
- { J. Swap swaps items I and J. After the sort, }
- { item I <= item J for L <= I < J <= H. }
- { Less_than and Swap can be nested functions (non-level-1) if desired. }
- { NOTE: The parametric routines Less_than and Swap passed to qsort }
- { must obey the Pascal calling convention. Surround your declarations }
- { of the passed routines with the same calling convention pragmas that }
- { appear at the beginning and end of this file. }
- #endif
-
- extern void qsort(unsigned L, unsigned H,
- int Less_than(unsigned I, unsigned J)!,
- void Swap(unsigned I, unsigned J)!
- );
-
- #pragma Global_aliasing_convention();
- #pragma Calling_convention();
-
- /* For example:
- main () {
- #pragma Calling_convention(PASCAL);
- int less_than(unsigned I,unsigned J) {...}
- void swap (unsigned I,unsigned J) {...}
- #pragma Calling_convention();
-
- qsort(0,99,less_than,swap);
- }
- */
-
- /* NOTE to others that are enamored with the UNIX C qsort: Here's an
- implementation using ours:
- void qsort(char *base, int N_elements, int Sizeof_element,
- int (*compare)(char *a, char *b) ) {
- #include <sorts.cf>
- extern void *malloc();
- #define base(x) (base+Sizeof_element*(x))
- #pragma calling_convention(PASCAL);
- int Less_than(unsigned I, unsigned J)! {
- return compare(base(I),base(J)) < 0;
- }
- void *Temp;
- void Swap(unsigned I, unsigned J) {
- _move(base(I),Temp,Sizeof_element);
- _move(base(J),base(I),Sizeof_element);
- _move(Temp,base(J),Sizeof_element);
- }
- #pragma calling_convention();
- Temp = malloc(Sizeof_element);
- qsort(0,N_elements-1,Less_than,Swap);
- }
- int a[10] = {9,7,8,4,5,10,3,2,1,6};
- int compare(char *a,char *b) {
- if (*(int*)a < *(int*)b) return -1;
- else if (*(int*)a == *(int*)b) return 0;
- else return 1;
- }
- void main() {
- int i;
- printf("Test UNIX qsort.\n");
- qsort((char *)a,10,sizeof(*a),compare);
- for (i = 0; i < sizeof(a)/sizeof(*a); i++) printf("%d ",a[i]);
- printf("\n");
- }
- */
- #endif /* _SORTS_CF */
-