qsort.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #ifndef AVUTIL_QSORT_H
  21. #define AVUTIL_QSORT_H
  22. #include "common.h"
  23. /**
  24. * Quicksort
  25. * This sort is fast, and fully inplace but not stable and it is possible
  26. * to construct input that requires O(n^2) time but this is very unlikely to
  27. * happen with non constructed input.
  28. */
  29. #define AV_QSORT(p, num, type, cmp) do {\
  30. void *stack[64][2];\
  31. int sp= 1;\
  32. stack[0][0] = p;\
  33. stack[0][1] = (p)+(num)-1;\
  34. while(sp){\
  35. type *start= stack[--sp][0];\
  36. type *end = stack[ sp][1];\
  37. while(start < end){\
  38. if(start < end-1) {\
  39. int checksort=0;\
  40. type *right = end-2;\
  41. type *left = start+1;\
  42. type *mid = start + ((end-start)>>1);\
  43. if(cmp(start, end) > 0) {\
  44. if(cmp( end, mid) > 0) FFSWAP(type, *start, *mid);\
  45. else FFSWAP(type, *start, *end);\
  46. }else{\
  47. if(cmp(start, mid) > 0) FFSWAP(type, *start, *mid);\
  48. else checksort= 1;\
  49. }\
  50. if(cmp(mid, end) > 0){ \
  51. FFSWAP(type, *mid, *end);\
  52. checksort=0;\
  53. }\
  54. if(start == end-2) break;\
  55. FFSWAP(type, end[-1], *mid);\
  56. while(left <= right){\
  57. while(left<=right && cmp(left, end-1) < 0)\
  58. left++;\
  59. while(left<=right && cmp(right, end-1) > 0)\
  60. right--;\
  61. if(left <= right){\
  62. FFSWAP(type, *left, *right);\
  63. left++;\
  64. right--;\
  65. }\
  66. }\
  67. FFSWAP(type, end[-1], *left);\
  68. if(checksort && (mid == left-1 || mid == left)){\
  69. mid= start;\
  70. while(mid<end && cmp(mid, mid+1) <= 0)\
  71. mid++;\
  72. if(mid==end)\
  73. break;\
  74. }\
  75. if(end-left < left-start){\
  76. stack[sp ][0]= start;\
  77. stack[sp++][1]= right;\
  78. start = left+1;\
  79. }else{\
  80. stack[sp ][0]= left+1;\
  81. stack[sp++][1]= end;\
  82. end = right;\
  83. }\
  84. }else{\
  85. if(cmp(start, end) > 0)\
  86. FFSWAP(type, *start, *end);\
  87. break;\
  88. }\
  89. }\
  90. }\
  91. } while (0)
  92. /**
  93. * Merge sort, this sort requires a temporary buffer and is stable, its worst
  94. * case time is O(n log n)
  95. * @param p must be a lvalue pointer, this function may exchange it with tmp
  96. * @param tmp must be a lvalue pointer, this function may exchange it with p
  97. */
  98. #define AV_MSORT(p, tmp, num, type, cmp) do {\
  99. unsigned i, j, step;\
  100. for(step=1; step<(num); step+=step){\
  101. for(i=0; i<(num); i+=2*step){\
  102. unsigned a[2] = {i, i+step};\
  103. unsigned end = FFMIN(i+2*step, (num));\
  104. for(j=i; a[0]<i+step && a[1]<end; j++){\
  105. int idx= cmp(p+a[0], p+a[1]) > 0;\
  106. tmp[j] = p[ a[idx]++ ];\
  107. }\
  108. if(a[0]>=i+step) a[0] = a[1];\
  109. for(; j<end; j++){\
  110. tmp[j] = p[ a[0]++ ];\
  111. }\
  112. }\
  113. FFSWAP(type*, p, tmp);\
  114. }\
  115. } while (0)
  116. #endif /* AVUTIL_QSORT_H */