Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
5.5 KiB

  1. /******************************************************************/
  2. /* qsort.c -- Non-Recursive ANSI Quicksort function */
  3. /* */
  4. /* Public domain by Raymond Gardner, Englewood CO February 1991 */
  5. /* */
  6. /* Usage: */
  7. /* qsort(base, nbr_elements, width_bytes, compare_function); */
  8. /* void *base; */
  9. /* size_t nbr_elements, width_bytes; */
  10. /* int (*compare_function)(const void *, const void *); */
  11. /* */
  12. /* Sorts an array starting at base, of length nbr_elements, each */
  13. /* element of size width_bytes, ordered via compare_function, */
  14. /* which is called as (*compare_function)(ptr_to_element1, */
  15. /* ptr_to_element2) and returns < 0 if element1 < element2, */
  16. /* 0 if element1 = element2, > 0 if element1 > element2. */
  17. /* Most refinements are due to R. Sedgewick. See "Implementing */
  18. /* Quicksort Programs", Comm. ACM, Oct. 1978, and Corrigendum, */
  19. /* Comm. ACM, June 1979. */
  20. /******************************************************************/
  21. // modified to take (and use) a context object, ala Microsoft's qsort_s
  22. // "extension" to the stdlib
  23. #include <stddef.h> /* for size_t definition */
  24. /*
  25. ** swap nbytes between a and b
  26. */
  27. static void swap_bytes(char *a, char *b, size_t nbytes)
  28. {
  29. char tmp;
  30. do {
  31. tmp = *a; *a++ = *b; *b++ = tmp;
  32. } while ( --nbytes );
  33. }
  34. #define SWAP(a, b) (swap_bytes((char *)(a), (char *)(b), size))
  35. #define COMP(ctx, a, b) ((*comp)((void *)ctx, (void *)(a), (void *)(b)))
  36. #define T 7 /* subfiles of T or fewer elements will */
  37. /* be sorted by a simple insertion sort */
  38. /* Note! T must be at least 3 */
  39. extern "C" void qsort_s(void *basep, size_t nelems, size_t size,
  40. int (*comp)(void *, const void *, const void *),
  41. void *ctx)
  42. {
  43. char *stack[40], **sp; /* stack and stack pointer */
  44. char *i, *j, *limit; /* scan and limit pointers */
  45. size_t thresh; /* size of T elements in bytes */
  46. char *base; /* base pointer as char * */
  47. base = (char *)basep; /* set up char * base pointer */
  48. thresh = T * size; /* init threshold */
  49. sp = stack; /* init stack pointer */
  50. limit = base + nelems * size;/* pointer past end of array */
  51. for ( ;; ) { /* repeat until break... */
  52. if ( limit - base > thresh ) { /* if more than T elements */
  53. /* swap base with middle */
  54. SWAP((((limit-base)/size)/2)*size+base, base);
  55. i = base + size; /* i scans left to right */
  56. j = limit - size; /* j scans right to left */
  57. if ( COMP(ctx, i, j) > 0 ) /* Sedgewick's */
  58. SWAP(i, j); /* three-element sort */
  59. if ( COMP(ctx, base, j) > 0 )/* sets things up */
  60. SWAP(base, j); /* so that */
  61. if ( COMP(ctx, i, base) > 0 )/* *i <= *base <= *j */
  62. SWAP(i, base); /* *base is pivot element */
  63. for ( ;; ) { /* loop until break */
  64. do /* move i right */
  65. i += size; /* until *i >= pivot */
  66. while ( COMP(ctx, i, base) < 0 );
  67. do /* move j left */
  68. j -= size; /* until *j <= pivot */
  69. while ( COMP(ctx, j, base) > 0 );
  70. if ( i > j ) /* if pointers crossed */
  71. break; /* break loop */
  72. SWAP(i, j); /* else swap elements, keep scanning*/
  73. }
  74. SWAP(base, j); /* move pivot into correct place */
  75. if ( j - base > limit - i ) { /* if left subfile larger */
  76. sp[0] = base; /* stack left subfile base */
  77. sp[1] = j; /* and limit */
  78. base = i; /* sort the right subfile */
  79. } else { /* else right subfile larger*/
  80. sp[0] = i; /* stack right subfile base */
  81. sp[1] = limit; /* and limit */
  82. limit = j; /* sort the left subfile */
  83. }
  84. sp += 2; /* increment stack pointer */
  85. } else { /* else subfile is small, use insertion sort */
  86. for ( j = base, i = j+size; i < limit; j = i, i += size )
  87. for ( ; COMP(ctx, j, j+size) > 0; j -= size ) {
  88. SWAP(j, j+size);
  89. if ( j == base )
  90. break;
  91. }
  92. if ( sp != stack ) { /* if any entries on stack */
  93. sp -= 2; /* pop the base and limit */
  94. base = sp[0];
  95. limit = sp[1];
  96. } else /* else stack empty, done */
  97. break;
  98. }
  99. }
  100. }