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.

87 lines
3.1KB

  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. #define AV_QSORT(p, num, type, cmp) {\
  22. void *stack[64][2];\
  23. int sp= 1;\
  24. stack[0][0] = p;\
  25. stack[0][1] = (p)+(num)-1;\
  26. while(sp){\
  27. type *start= stack[--sp][0];\
  28. type *end = stack[ sp][1];\
  29. while(start < end){\
  30. if(start < end-1) {\
  31. int checksort=0;\
  32. type *right = end-2;\
  33. type *left = start+1;\
  34. type *mid = start + ((end-start)>>1);\
  35. if(cmp(start, end) < 0) {\
  36. if(cmp( end, mid) < 0) FFSWAP(type, *start, *mid);\
  37. else FFSWAP(type, *start, *end);\
  38. }else{\
  39. if(cmp(start, mid) < 0) FFSWAP(type, *start, *mid);\
  40. else checksort= 1;\
  41. }\
  42. if(cmp(mid, end) < 0){ \
  43. FFSWAP(type, *mid, *end);\
  44. checksort=0;\
  45. }\
  46. if(start == end-2) break;\
  47. FFSWAP(type, end[-1], *mid);\
  48. while(left <= right){\
  49. while(left<=right && cmp(left, end-1) > 0)\
  50. left++;\
  51. while(left<=right && cmp(right, end-1) < 0)\
  52. right--;\
  53. if(left <= right){\
  54. FFSWAP(type, *left, *right);\
  55. left++;\
  56. right--;\
  57. }\
  58. }\
  59. FFSWAP(type, end[-1], *left);\
  60. if(checksort && (mid == left-1 || mid == left)){\
  61. mid= start;\
  62. while(mid<end && cmp(mid, mid+1) >= 0)\
  63. mid++;\
  64. if(mid==end)\
  65. break;\
  66. }\
  67. if(end-left < left-start){\
  68. stack[sp ][0]= start;\
  69. stack[sp++][1]= right;\
  70. start = left+1;\
  71. }else{\
  72. stack[sp ][0]= left+1;\
  73. stack[sp++][1]= end;\
  74. end = right;\
  75. }\
  76. }else{\
  77. if(cmp(start, end) < 0)\
  78. FFSWAP(type, *start, *end);\
  79. break;\
  80. }\
  81. }\
  82. }\
  83. }