qsort.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * copyright (c) 2012 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "common.h"
  21. /**
  22. * Quicksort
  23. * This sort is fast, and fully inplace but not stable and it is possible
  24. * to construct input that requires O(n^2) time but this is very unlikely to
  25. * happen with non constructed input.
  26. */
  27. #define AV_QSORT(p, num, type, cmp) {\
  28. void *stack[64][2];\
  29. int sp= 1;\
  30. stack[0][0] = p;\
  31. stack[0][1] = (p)+(num)-1;\
  32. while(sp){\
  33. type *start= stack[--sp][0];\
  34. type *end = stack[ sp][1];\
  35. while(start < end){\
  36. if(start < end-1) {\
  37. int checksort=0;\
  38. type *right = end-2;\
  39. type *left = start+1;\
  40. type *mid = start + ((end-start)>>1);\
  41. if(cmp(start, end) > 0) {\
  42. if(cmp( end, mid) > 0) FFSWAP(type, *start, *mid);\
  43. else FFSWAP(type, *start, *end);\
  44. }else{\
  45. if(cmp(start, mid) > 0) FFSWAP(type, *start, *mid);\
  46. else checksort= 1;\
  47. }\
  48. if(cmp(mid, end) > 0){ \
  49. FFSWAP(type, *mid, *end);\
  50. checksort=0;\
  51. }\
  52. if(start == end-2) break;\
  53. FFSWAP(type, end[-1], *mid);\
  54. while(left <= right){\
  55. while(left<=right && cmp(left, end-1) < 0)\
  56. left++;\
  57. while(left<=right && cmp(right, end-1) > 0)\
  58. right--;\
  59. if(left <= right){\
  60. FFSWAP(type, *left, *right);\
  61. left++;\
  62. right--;\
  63. }\
  64. }\
  65. FFSWAP(type, end[-1], *left);\
  66. if(checksort && (mid == left-1 || mid == left)){\
  67. mid= start;\
  68. while(mid<end && cmp(mid, mid+1) <= 0)\
  69. mid++;\
  70. if(mid==end)\
  71. break;\
  72. }\
  73. if(end-left < left-start){\
  74. stack[sp ][0]= start;\
  75. stack[sp++][1]= right;\
  76. start = left+1;\
  77. }else{\
  78. stack[sp ][0]= left+1;\
  79. stack[sp++][1]= end;\
  80. end = right;\
  81. }\
  82. }else{\
  83. if(cmp(start, end) > 0)\
  84. FFSWAP(type, *start, *end);\
  85. break;\
  86. }\
  87. }\
  88. }\
  89. }
  90. /**
  91. * Merge sort, this sort requires a temporary buffer and is stable, its worst
  92. * case time is O(n log n)
  93. * @param p must be a lvalue pointer, this function may exchange it with tmp
  94. * @param tmp must be a lvalue pointer, this function may exchange it with p
  95. */
  96. #define AV_MSORT(p, tmp, num, type, cmp) {\
  97. unsigned i, j, step;\
  98. for(step=1; step<(num); step+=step){\
  99. for(i=0; i<(num); i+=2*step){\
  100. unsigned a[2] = {i, i+step};\
  101. unsigned end = FFMIN(i+2*step, (num));\
  102. for(j=i; a[0]<i+step && a[1]<end; j++){\
  103. int idx= cmp(p+a[0], p+a[1]) > 0;\
  104. tmp[j] = p[ a[idx]++ ];\
  105. }\
  106. if(a[0]>=i+step) a[0] = a[1];\
  107. for(; j<end; j++){\
  108. tmp[j] = p[ a[0]++ ];\
  109. }\
  110. }\
  111. FFSWAP(type*, p, tmp);\
  112. }\
  113. }