_qsort.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include "asmc_types.h"
  2. // All code in this file was taken from PDClib
  3. /* This implementation is taken from Paul Edward's PDPCLIB.
  4. Original code is credited to Raymond Gardner, Englewood CO.
  5. Minor mods are credited to Paul Edwards.
  6. Some reformatting and simplification done by Martin Baute.
  7. All code is still Public Domain.
  8. */
  9. #define _memswp( i, j, size ) char tmp; do { tmp = *i; *i++ = *j; *j++ = tmp; } while ( --size );
  10. /* Wrapper for _memswp protects against multiple argument evaluation. */
  11. static void memswp( char * i, char * j, size_t size )
  12. {
  13. _memswp( i, j, size );
  14. }
  15. /* For small sets, insertion sort is faster than quicksort.
  16. T is the threshold below which insertion sort will be used.
  17. Must be 3 or larger.
  18. */
  19. #define T 7
  20. /* Macros for handling the QSort stack */
  21. #define PREPARE_STACK char * stack[STACKSIZE]; char * * stackptr = stack
  22. #define PUSH( base, limit ) stackptr[0] = base; stackptr[1] = limit; stackptr += 2
  23. #define POP( base, limit ) stackptr -= 2; base = stackptr[0]; limit = stackptr[1]
  24. /* TODO: Stack usage is log2( nmemb ) (minus what T shaves off the worst case).
  25. Worst-case nmemb is platform dependent and should probably be
  26. configured through _PDCLIB_config.h.
  27. */
  28. #define STACKSIZE 64
  29. void qsort( void * base, size_t nmemb, size_t size, int (*compar)( const void *, const void * ) )
  30. {
  31. char * i;
  32. char * j;
  33. size_t thresh = T * size;
  34. char * base_ = (char *)base;
  35. char * limit = base_ + nmemb * size;
  36. PREPARE_STACK;
  37. for ( ;; )
  38. {
  39. if ( (size_t)( limit - base_ ) > thresh ) /* QSort for more than T elements. */
  40. {
  41. /* We work from second to last - first will be pivot element. */
  42. i = base_ + size;
  43. j = limit - size;
  44. /* We swap first with middle element, then sort that with second
  45. and last element so that eventually first element is the median
  46. of the three - avoiding pathological pivots.
  47. TODO: Instead of middle element, chose one randomly.
  48. */
  49. memswp( ( ( ( (size_t)( limit - base_ ) ) / size ) / 2 ) * size + base_, base_, size );
  50. if ( compar( i, j ) > 0 ) memswp( i, j, size );
  51. if ( compar( base_, j ) > 0 ) memswp( base_, j, size );
  52. if ( compar( i, base_ ) > 0 ) memswp( i, base_, size );
  53. /* Now we have the median for pivot element, entering main Quicksort. */
  54. for ( ;; )
  55. {
  56. do
  57. {
  58. /* move i right until *i >= pivot */
  59. i += size;
  60. } while ( compar( i, base_ ) < 0 );
  61. do
  62. {
  63. /* move j left until *j <= pivot */
  64. j -= size;
  65. } while ( compar( j, base_ ) > 0 );
  66. if ( i > j )
  67. {
  68. /* break loop if pointers crossed */
  69. break;
  70. }
  71. /* else swap elements, keep scanning */
  72. memswp( i, j, size );
  73. }
  74. /* move pivot into correct place */
  75. memswp( base_, j, size );
  76. /* larger subfile base / limit to stack, sort smaller */
  77. if ( j - base_ > limit - i )
  78. {
  79. /* left is larger */
  80. PUSH( base_, j );
  81. base_ = i;
  82. }
  83. else
  84. {
  85. /* right is larger */
  86. PUSH( i, limit );
  87. limit = j;
  88. }
  89. }
  90. else /* insertion sort for less than T elements */
  91. {
  92. for ( j = base_, i = j + size; i < limit; j = i, i += size )
  93. {
  94. for ( ; compar( j, j + size ) > 0; j -= size )
  95. {
  96. memswp( j, j + size, size );
  97. if ( j == base_ )
  98. {
  99. break;
  100. }
  101. }
  102. }
  103. if ( stackptr != stack ) /* if any entries on stack */
  104. {
  105. POP( base_, limit );
  106. }
  107. else /* else stack empty, done */
  108. {
  109. break;
  110. }
  111. }
  112. }
  113. }
  114. #undef T
  115. #undef PREPARE_STACK
  116. #undef PUSH
  117. #undef POP