home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c221 / 5.ddi / MWHC.005 / L1 < prev    next >
Encoding:
Text File  |  1992-01-07  |  2.5 KB  |  84 lines

  1. /*
  2.  *   sorts.cf -- non-ANSI
  3.  *
  4.  *   Sorting algorithms.
  5.  *
  6.  *           Copyright (c) 1990, MetaWare Incorporated
  7.  */
  8.  
  9. #ifndef _SORTS_CF
  10. #define _SORTS_CF
  11.  
  12. #include <implemen.cf>
  13. #include <language.cf>
  14.  
  15. #pragma Global_aliasing_convention(_Private_routine_prefix "%r");
  16. #pragma Calling_convention(PASCAL);
  17.  
  18. #if 0
  19. { Sort items L..H.  Less_than returns whether item I is less than item    }
  20. { J.  Swap swaps items I and J.  After the sort,            }
  21. {    item I <= item J    for L <= I < J <= H.            }
  22. { Less_than and Swap can be nested functions (non-level-1) if desired.    }
  23. { NOTE:  The parametric routines Less_than and Swap passed to qsort    }
  24. { must obey the Pascal calling convention.  Surround your declarations    }
  25. { of the passed routines with the same calling convention pragmas that    }
  26. { appear at the beginning and end of this file.             }
  27. #endif
  28.  
  29. extern void qsort(unsigned L, unsigned H,
  30.        int Less_than(unsigned I, unsigned J)!,
  31.        void Swap(unsigned I, unsigned J)!
  32.       );
  33.  
  34. #pragma Global_aliasing_convention();
  35. #pragma Calling_convention();
  36.  
  37. /* For example:
  38.    main () {
  39.       #pragma Calling_convention(PASCAL);
  40.       int less_than(unsigned I,unsigned J) {...}
  41.       void swap (unsigned I,unsigned J) {...}
  42.       #pragma Calling_convention();
  43.  
  44.       qsort(0,99,less_than,swap);
  45.       }
  46. */
  47.  
  48. /* NOTE to others that are enamored with the UNIX C qsort:  Here's an
  49.    implementation using ours:
  50.    void qsort(char *base, int N_elements, int Sizeof_element,
  51.              int (*compare)(char *a, char *b) ) {
  52.       #include <sorts.cf>
  53.       extern void *malloc();
  54.       #define base(x) (base+Sizeof_element*(x))
  55.       #pragma calling_convention(PASCAL);
  56.       int Less_than(unsigned I, unsigned J)! {
  57.            return compare(base(I),base(J)) < 0;
  58.            } 
  59.       void *Temp;
  60.       void Swap(unsigned I, unsigned J) {
  61.            _move(base(I),Temp,Sizeof_element);
  62.            _move(base(J),base(I),Sizeof_element);
  63.            _move(Temp,base(J),Sizeof_element);
  64.            }
  65.       #pragma calling_convention();
  66.       Temp = malloc(Sizeof_element);
  67.       qsort(0,N_elements-1,Less_than,Swap);
  68.       }
  69.    int a[10] = {9,7,8,4,5,10,3,2,1,6};
  70.    int compare(char *a,char *b) { 
  71.       if (*(int*)a < *(int*)b) return -1;
  72.       else if (*(int*)a == *(int*)b) return 0;
  73.       else return 1;
  74.       }
  75.    void main() {
  76.       int i;
  77.       printf("Test UNIX qsort.\n");
  78.       qsort((char *)a,10,sizeof(*a),compare);
  79.       for (i = 0; i < sizeof(a)/sizeof(*a); i++) printf("%d ",a[i]);
  80.       printf("\n");
  81.       }
  82.  */
  83. #endif /* _SORTS_CF */
  84.