/trunk/old132/src/e_search.h

# · C Header · 178 lines · 142 code · 6 blank · 30 comment · 23 complexity · dcf1fc513b25fadb326e692b97c40f3a MD5 · raw file

  1. //----------------------------------------------------------------------------
  2. // EDGE Search Definition
  3. //----------------------------------------------------------------------------
  4. //
  5. // Copyright (c) 1999-2008 The EDGE Team.
  6. //
  7. // This program is free software; you can redistribute it and/or
  8. // modify it under the terms of the GNU General Public License
  9. // as published by the Free Software Foundation; either version 2
  10. // of the License, or (at your option) any later version.
  11. //
  12. // This program is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. // GNU General Public License for more details.
  16. //
  17. //----------------------------------------------------------------------------
  18. //
  19. // Based on the DOOM source code, released by Id Software under the
  20. // following copyright:
  21. //
  22. // Copyright (C) 1993-1996 by id Software, Inc.
  23. //
  24. //----------------------------------------------------------------------------
  25. #ifndef __E_SEARCH_H__
  26. #define __E_SEARCH_H__
  27. // QSORT routine QuickSorts array in arr of type type, number of elements n
  28. // and stops when elements are cutoff sorted. Then does an insertion sort
  29. // to complete data
  30. // -ES- 2000/02/15 Removed iQSORT: Unneeded.
  31. #define CUTOFF 10
  32. #define QSORT(type, arr, n, cutoff)\
  33. { \
  34. int *stk; \
  35. type pivot; \
  36. type t; \
  37. int i, j, c, top; \
  38. unsigned int a, b; \
  39. \
  40. stk = new int[n+1]; \
  41. \
  42. a = top = 0; \
  43. b = n - 1; \
  44. \
  45. while (1) \
  46. { \
  47. while (b > a + cutoff) \
  48. { \
  49. c = (a + b) / 2; \
  50. \
  51. if (CMP(arr[b], arr[a])) \
  52. { \
  53. t = arr[a]; \
  54. arr[a] = arr[b]; \
  55. arr[b] = t; \
  56. } \
  57. \
  58. if (CMP(arr[c], arr[a])) \
  59. { \
  60. t = arr[a]; \
  61. arr[a] = arr[c]; \
  62. arr[c] = t; \
  63. } \
  64. \
  65. if (CMP(arr[c], arr[b])) \
  66. { \
  67. t = arr[c]; \
  68. arr[c] = arr[b]; \
  69. arr[b] = t; \
  70. } \
  71. \
  72. pivot = arr[c]; \
  73. arr[c] = arr[b-1]; \
  74. arr[b-1] = pivot; \
  75. \
  76. i = a, j = b-1; \
  77. while (1) \
  78. { \
  79. do { i++; } \
  80. while (CMP(arr[i], arr[b-1])); \
  81. \
  82. do { j--; } \
  83. while (CMP(arr[b-1], arr[j])); \
  84. \
  85. if (j < i) \
  86. break; \
  87. \
  88. t = arr[i]; \
  89. arr[i] = arr[j]; \
  90. arr[j] = t; \
  91. } \
  92. \
  93. pivot = arr[i]; \
  94. arr[i] = arr[b-1]; \
  95. arr[b-1] = pivot; \
  96. \
  97. if (j - a > b - 1) \
  98. { \
  99. stk[top++] = a; \
  100. stk[top++] = j; \
  101. a = i+1; \
  102. } \
  103. else \
  104. { \
  105. stk[top++] = i+1; \
  106. stk[top++] = b; \
  107. b = j; \
  108. } \
  109. } \
  110. \
  111. if (!top) \
  112. break; \
  113. \
  114. b = stk[--top]; \
  115. a = stk[--top]; \
  116. } \
  117. \
  118. for (i = 1; i < n; i++) \
  119. { \
  120. t = arr[i]; \
  121. j = i; \
  122. while (j >= 1 && CMP(t, arr[j-1])) \
  123. { \
  124. arr[j] = arr[j-1]; \
  125. j--; \
  126. } \
  127. arr[j] = t; \
  128. } \
  129. \
  130. delete [] stk; \
  131. }
  132. #define MAPSORT(type, arr, map, n) \
  133. { \
  134. type tmp; \
  135. int i, j, k; \
  136. \
  137. for (i = 0; i < n; i++) \
  138. { \
  139. if (map[i] != i) \
  140. { \
  141. tmp = arr[i]; \
  142. k = i; \
  143. do \
  144. { \
  145. j = k; \
  146. k = map[j]; \
  147. arr[j] = arr[k]; \
  148. map[j] = j; \
  149. } while (k != i); \
  150. arr[j] = tmp; \
  151. } \
  152. } \
  153. }
  154. #define BSEARCH(n, pos) \
  155. { \
  156. int hi, mid; \
  157. \
  158. pos = 0; \
  159. hi = n-1; \
  160. \
  161. while (pos <= hi) \
  162. { \
  163. mid = (pos + hi) >> 1; \
  164. if (CMP(mid)) \
  165. pos = mid + 1; \
  166. else \
  167. hi = mid - 1; \
  168. } \
  169. }
  170. #endif
  171. //--- editor settings ---
  172. // vi:ts=4:sw=4:noexpandtab