NUMsort2.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /* NUMsort.cpp
  2. *
  3. * Copyright (C) 1993-2012 David Weenink
  4. *
  5. * This code is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This code is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this work. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /*
  19. djmw 20030121 Initial version
  20. djmw 20030627 Removed bug in MACRO_NUMindex
  21. djmw 20120305 Latest modification
  22. */
  23. #include "NUM2.h"
  24. #include "melder.h"
  25. /*
  26. NUMsort2 uses heapsort to sort the second array in parallel with the first one.
  27. Algorithm follows p. 145 and 642 in:
  28. Donald E. Knuth (1998): The art of computer programming. Third edition. Vol. 3: sorting and searching.
  29. Boston: Addison-Wesley, printed may 2002.
  30. Modification: there is no distinction between record and key and
  31. Floyd's optimization (page 642) is used.
  32. */
  33. void NUMrankColumns (double **m, integer rb, integer re, integer cb, integer ce) {
  34. integer nr = re - rb + 1;
  35. autoNUMvector <double> v (1, nr);
  36. autoNUMvector <integer> index (1, nr);
  37. for (integer j = cb; j <= ce; j ++) {
  38. for (integer i = 1; i <= nr; i ++) {
  39. v [i] = m [rb + i - 1] [j];
  40. }
  41. for (integer i = 1; i <= nr; i ++) {
  42. index [i] = i;
  43. }
  44. NUMsort2 (nr, v.peek(), index.peek());
  45. NUMrank (nr, v.peek());
  46. for (integer i = 1; i <= nr; i ++) {
  47. m [rb + index [i] - 1] [j] = v [i];
  48. }
  49. }
  50. }
  51. template <class T>
  52. void NUMindexx (const T a[], integer n, integer index[], int (*compare) (void *, void *)) {
  53. integer ii, imin;
  54. T min;
  55. for (integer j = 1; j <= n; j ++) {
  56. index [j] = j;
  57. }
  58. if (n < 2) return; // Already sorted
  59. if (n == 2) {
  60. if (COMPARELT (a [2], a [1])) {
  61. index [1] = 2; index [2] = 1;
  62. }
  63. return;
  64. }
  65. if (n <= 12) {
  66. for (integer i = 1; i < n; i ++) {
  67. imin = i;
  68. min = a [index [imin]];
  69. for (integer j = i + 1; j <= n; j ++) {
  70. if (COMPARELT (a[index [j]], min)) {
  71. imin = j;
  72. min = a [index [j]];
  73. }
  74. }
  75. ii = index [imin]; index [imin] = index [i]; index [i] = ii;
  76. }
  77. return;
  78. }
  79. // H1
  80. integer l = n / 2 + 1, r = n;
  81. for (;;) { // H2
  82. integer k, i;
  83. if (l > 1) {
  84. l --;
  85. k = index [l];
  86. } else { // l == 1
  87. k = index [r];
  88. index [r] = index [1];
  89. r --;
  90. if (r == 1) {
  91. index [1] = k; break;
  92. }
  93. }
  94. // H3
  95. integer j = l;
  96. for (;;) {
  97. // H4
  98. i = j;
  99. j *= 2;
  100. if (j > r) {
  101. break;
  102. }
  103. if (j < r && COMPARELT (a [index [j]], a [index [j + 1]])) {
  104. j++; // H5
  105. }
  106. index [i] = index [j]; // H7
  107. }
  108. for (;;) { // H8'
  109. j = i;
  110. i = j >> 1;
  111. // H9'
  112. if (j == l || COMPARELT (a [k], a [index [i]])) {
  113. index [j] = k; break;
  114. }
  115. index[j] = index[i];
  116. }
  117. }
  118. }
  119. #define MACRO_NUMindex(TYPE,n) \
  120. {\
  121. integer l, r, j, i, ii, k, imin; \
  122. TYPE min; \
  123. autoINTVEC index = INTVECraw (n); \
  124. for (j = 1; j <= n; j ++) index[j] = j; \
  125. if (n < 2) return index; /* Already sorted. */ \
  126. if (n == 2) \
  127. { \
  128. if (COMPARELT (a [2], a [1])) \
  129. {\
  130. index [1] = 2; index [2] = 1; \
  131. } \
  132. return index; \
  133. } \
  134. if (n <= 12) \
  135. { \
  136. for (i = 1; i < n; i ++) \
  137. { \
  138. imin = i; \
  139. min = a [index [imin]]; \
  140. for (j = i + 1; j <= n; j ++) \
  141. {\
  142. if (COMPARELT (a [index [j]], min))\
  143. { \
  144. imin = j; \
  145. min = a [index [j]]; \
  146. } \
  147. } \
  148. ii = index [imin]; index [imin] = index [i]; index [i] = ii; \
  149. } \
  150. return index; \
  151. } \
  152. /* H1 */\
  153. l = n / 2 + 1; \
  154. r = n; \
  155. for (;;) /* H2 */\
  156. { \
  157. if (l > 1) \
  158. { \
  159. l --; \
  160. k = index[l]; \
  161. } \
  162. else /* l == 1 */ \
  163. { \
  164. k = index [r]; \
  165. index [r] = index [1]; \
  166. r --; \
  167. if (r == 1) \
  168. { \
  169. index [1] = k; break; \
  170. } \
  171. } \
  172. /* H3 */ \
  173. j = l; \
  174. for (;;) \
  175. { \
  176. /* H4 */ \
  177. i = j; \
  178. j *= 2; \
  179. if (j > r) break; \
  180. if (j < r && COMPARELT (a [index [j]], a [index [j + 1]])) j ++; /* H5 */\
  181. index [i] = index [j]; /* H7 */\
  182. } \
  183. for (;;) /*H8' */\
  184. {\
  185. j = i; \
  186. i = j >> 1; \
  187. /* H9' */ \
  188. if (j == l || COMPARELT (a[k], a[index[i]])) \
  189. { \
  190. index [j] = k; break; \
  191. } \
  192. index [j] = index [i]; \
  193. }\
  194. } \
  195. return index; \
  196. }
  197. #define COMPARELT(x,y) ((x) < (y))
  198. autoINTVEC NUMindexx (constVEC a)
  199. MACRO_NUMindex (double, a.size)
  200. //void NUMindexx (const double a[], integer n, integer index[])
  201. //MACRO_NUMindex (double, n)
  202. #undef COMPARELT
  203. #define COMPARELT(x,y) (Melder_cmp (x,y) < 0)
  204. //void NUMindexx_s (char32 **a, integer n, integer index[])
  205. autoINTVEC NUMindexx_s (constSTRVEC a)
  206. MACRO_NUMindex (const char32_t *, a.size)
  207. #undef COMPARELT
  208. #undef MACRO_INDEXX
  209. template <class T>
  210. void NUMsort1 (integer n, T a[]) {
  211. /*
  212. Knuth's heapsort algorithm (vol. 3, page 145),
  213. modified with Floyd's optimization (vol. 3, page 642).
  214. */
  215. T min;
  216. if (n < 2) return; // Already sorted.
  217. /*
  218. This n<2 step is absent from Press et al.'s implementation,
  219. which will therefore not terminate on if(--ir==1).
  220. Knuth's initial assumption is now fulfilled: n >= 2.
  221. */
  222. if (n == 2) {
  223. if (a [1] > a [2]) {
  224. min = a [2]; a [2] = a [1]; a [1] = min;
  225. }
  226. return;
  227. }
  228. if (n <= 12) {
  229. integer imin;
  230. for (integer i = 1; i < n; i ++) {
  231. min = a [i];
  232. imin = i;
  233. for (integer j = i + 1; j <= n; j ++) {
  234. if (a [j] < min) {
  235. min = a [j];
  236. imin = j;
  237. }
  238. }
  239. a [imin] = a [i];
  240. a [i] = min;
  241. }
  242. return;
  243. }
  244. // H1
  245. integer l = (n >> 1) + 1, r = n;
  246. for (;;) { // H2
  247. T ak;
  248. if (l > 1) {
  249. l --;
  250. ak = a [l];
  251. } else { // l == 1
  252. ak = a [r];
  253. a [r] = a [1];
  254. r --;
  255. if (r == 1) {
  256. a [1] = ak; return;
  257. }
  258. }
  259. // H3
  260. integer i, j = l;
  261. for (;;) { // H4
  262. i = j;
  263. j = j << 1;
  264. if (j > r) {
  265. break;
  266. }
  267. if (j < r && a [j] < a [j + 1]) {
  268. j ++; // H5
  269. }
  270. // if (k >= a[j]) break;
  271. a [i] = a [j]; // H7
  272. }
  273. // a[i] = k; H8
  274. for (;;) { // H8'
  275. j = i;
  276. i = j >> 1;
  277. // H9'
  278. if (j == l || ak <= a [i]) {
  279. a [j] = ak; break;
  280. }
  281. a [j] = a [i];
  282. }
  283. }
  284. }
  285. void NUMsort3 (VEC a, INTVEC iv1, INTVEC iv2, bool descending) {
  286. Melder_assert (a.size == iv1.size && a.size == iv2.size);
  287. if (a.size == 1) {
  288. return;
  289. }
  290. autoVEC atmp = VECcopy (a);
  291. autoINTVEC index = NUMindexx (atmp.get());
  292. if (descending) {
  293. for (integer j = 1; j <= a.size / 2; j ++) {
  294. integer tmp = index [j];
  295. index [j] = index [a.size - j + 1];
  296. index [a.size - j + 1] = tmp;
  297. }
  298. }
  299. for (integer j = 1; j <= a.size; j ++) a [j] = atmp [index [j]];
  300. autoINTVEC itmp = INTVECraw (a.size);
  301. vectorcopy_preallocated (itmp.get(), iv1);
  302. for (integer j = 1; j <= a.size; j ++) iv1 [j ] = itmp [index [j]];
  303. vectorcopy_preallocated (itmp.get(), iv2);
  304. for (integer j = 1; j <= a.size; j ++) iv2 [j] = itmp [index [j]];
  305. }
  306. /* End of file NUMsort.cpp */