Team Fortress 2 Source Code as on 22/4/2020
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.

141 lines
2.4 KiB

  1. /* Sort.c -- Sort functions
  2. 2014-04-05 : Igor Pavlov : Public domain */
  3. #include "Precomp.h"
  4. #include "Sort.h"
  5. #define HeapSortDown(p, k, size, temp) \
  6. { for (;;) { \
  7. size_t s = (k << 1); \
  8. if (s > size) break; \
  9. if (s < size && p[s + 1] > p[s]) s++; \
  10. if (temp >= p[s]) break; \
  11. p[k] = p[s]; k = s; \
  12. } p[k] = temp; }
  13. void HeapSort(UInt32 *p, size_t size)
  14. {
  15. if (size <= 1)
  16. return;
  17. p--;
  18. {
  19. size_t i = size / 2;
  20. do
  21. {
  22. UInt32 temp = p[i];
  23. size_t k = i;
  24. HeapSortDown(p, k, size, temp)
  25. }
  26. while (--i != 0);
  27. }
  28. /*
  29. do
  30. {
  31. size_t k = 1;
  32. UInt32 temp = p[size];
  33. p[size--] = p[1];
  34. HeapSortDown(p, k, size, temp)
  35. }
  36. while (size > 1);
  37. */
  38. while (size > 3)
  39. {
  40. UInt32 temp = p[size];
  41. size_t k = (p[3] > p[2]) ? 3 : 2;
  42. p[size--] = p[1];
  43. p[1] = p[k];
  44. HeapSortDown(p, k, size, temp)
  45. }
  46. {
  47. UInt32 temp = p[size];
  48. p[size] = p[1];
  49. if (size > 2 && p[2] < temp)
  50. {
  51. p[1] = p[2];
  52. p[2] = temp;
  53. }
  54. else
  55. p[1] = temp;
  56. }
  57. }
  58. void HeapSort64(UInt64 *p, size_t size)
  59. {
  60. if (size <= 1)
  61. return;
  62. p--;
  63. {
  64. size_t i = size / 2;
  65. do
  66. {
  67. UInt64 temp = p[i];
  68. size_t k = i;
  69. HeapSortDown(p, k, size, temp)
  70. }
  71. while (--i != 0);
  72. }
  73. /*
  74. do
  75. {
  76. size_t k = 1;
  77. UInt64 temp = p[size];
  78. p[size--] = p[1];
  79. HeapSortDown(p, k, size, temp)
  80. }
  81. while (size > 1);
  82. */
  83. while (size > 3)
  84. {
  85. UInt64 temp = p[size];
  86. size_t k = (p[3] > p[2]) ? 3 : 2;
  87. p[size--] = p[1];
  88. p[1] = p[k];
  89. HeapSortDown(p, k, size, temp)
  90. }
  91. {
  92. UInt64 temp = p[size];
  93. p[size] = p[1];
  94. if (size > 2 && p[2] < temp)
  95. {
  96. p[1] = p[2];
  97. p[2] = temp;
  98. }
  99. else
  100. p[1] = temp;
  101. }
  102. }
  103. /*
  104. #define HeapSortRefDown(p, vals, n, size, temp) \
  105. { size_t k = n; UInt32 val = vals[temp]; for (;;) { \
  106. size_t s = (k << 1); \
  107. if (s > size) break; \
  108. if (s < size && vals[p[s + 1]] > vals[p[s]]) s++; \
  109. if (val >= vals[p[s]]) break; \
  110. p[k] = p[s]; k = s; \
  111. } p[k] = temp; }
  112. void HeapSortRef(UInt32 *p, UInt32 *vals, size_t size)
  113. {
  114. if (size <= 1)
  115. return;
  116. p--;
  117. {
  118. size_t i = size / 2;
  119. do
  120. {
  121. UInt32 temp = p[i];
  122. HeapSortRefDown(p, vals, i, size, temp);
  123. }
  124. while (--i != 0);
  125. }
  126. do
  127. {
  128. UInt32 temp = p[size];
  129. p[size--] = p[1];
  130. HeapSortRefDown(p, vals, 1, size, temp);
  131. }
  132. while (size > 1);
  133. }
  134. */