timsort.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. /*
  2. * Taken from https://github.com/swenson/sort
  3. * Revision: 05fd77bfec049ce8b7c408c4d3dd2d51ee061a15
  4. * Removed all code unrelated to Timsort and made minor adjustments for
  5. * cross-platform compatibility.
  6. */
  7. /*
  8. * The MIT License (MIT)
  9. *
  10. * Copyright (c) 2010-2017 Christopher Swenson.
  11. * Copyright (c) 2012 Vojtech Fried.
  12. * Copyright (c) 2012 Google Inc. All Rights Reserved.
  13. *
  14. * Permission is hereby granted, free of charge, to any person obtaining a
  15. * copy of this software and associated documentation files (the "Software"),
  16. * to deal in the Software without restriction, including without limitation
  17. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  18. * and/or sell copies of the Software, and to permit persons to whom the
  19. * Software is furnished to do so, subject to the following conditions:
  20. *
  21. * The above copyright notice and this permission notice shall be included in
  22. * all copies or substantial portions of the Software.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  25. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  26. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  27. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  28. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  29. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  30. * DEALINGS IN THE SOFTWARE.
  31. */
  32. #include <stdlib.h>
  33. #include <stdio.h>
  34. #include <string.h>
  35. #ifdef HAVE_STDINT_H
  36. #include <stdint.h>
  37. #elif defined(_WIN32)
  38. typedef unsigned __int64 uint64_t;
  39. #endif
  40. #ifndef SORT_NAME
  41. #error "Must declare SORT_NAME"
  42. #endif
  43. #ifndef SORT_TYPE
  44. #error "Must declare SORT_TYPE"
  45. #endif
  46. #ifndef SORT_CMP
  47. #define SORT_CMP(x, y) ((x) < (y) ? -1 : ((x) == (y) ? 0 : 1))
  48. #endif
  49. #ifndef TIM_SORT_STACK_SIZE
  50. #define TIM_SORT_STACK_SIZE 128
  51. #endif
  52. #define SORT_SWAP(x,y) {SORT_TYPE __SORT_SWAP_t = (x); (x) = (y); (y) = __SORT_SWAP_t;}
  53. /* Common, type-agnostic functions and constants that we don't want to declare twice. */
  54. #ifndef SORT_COMMON_H
  55. #define SORT_COMMON_H
  56. #ifndef MAX
  57. #define MAX(x,y) (((x) > (y) ? (x) : (y)))
  58. #endif
  59. #ifndef MIN
  60. #define MIN(x,y) (((x) < (y) ? (x) : (y)))
  61. #endif
  62. static int compute_minrun(const uint64_t);
  63. #ifndef CLZ
  64. #if defined(__GNUC__) && ((__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ > 3))
  65. #define CLZ __builtin_clzll
  66. #else
  67. static int clzll(uint64_t);
  68. /* adapted from Hacker's Delight */
  69. static int clzll(uint64_t x) {
  70. int n;
  71. if (x == 0) {
  72. return 64;
  73. }
  74. n = 0;
  75. if (x <= 0x00000000FFFFFFFFL) {
  76. n = n + 32;
  77. x = x << 32;
  78. }
  79. if (x <= 0x0000FFFFFFFFFFFFL) {
  80. n = n + 16;
  81. x = x << 16;
  82. }
  83. if (x <= 0x00FFFFFFFFFFFFFFL) {
  84. n = n + 8;
  85. x = x << 8;
  86. }
  87. if (x <= 0x0FFFFFFFFFFFFFFFL) {
  88. n = n + 4;
  89. x = x << 4;
  90. }
  91. if (x <= 0x3FFFFFFFFFFFFFFFL) {
  92. n = n + 2;
  93. x = x << 2;
  94. }
  95. if (x <= 0x7FFFFFFFFFFFFFFFL) {
  96. n = n + 1;
  97. }
  98. return n;
  99. }
  100. #define CLZ clzll
  101. #endif
  102. #endif
  103. static __inline int compute_minrun(const uint64_t size) {
  104. const int top_bit = 64 - CLZ(size);
  105. const int shift = MAX(top_bit, 6) - 6;
  106. const int minrun = size >> shift;
  107. const uint64_t mask = (1ULL << shift) - 1;
  108. if (mask & size) {
  109. return minrun + 1;
  110. }
  111. return minrun;
  112. }
  113. #endif /* SORT_COMMON_H */
  114. #define SORT_CONCAT(x, y) x ## _ ## y
  115. #define SORT_MAKE_STR1(x, y) SORT_CONCAT(x,y)
  116. #define SORT_MAKE_STR(x) SORT_MAKE_STR1(SORT_NAME,x)
  117. #define BINARY_INSERTION_FIND SORT_MAKE_STR(binary_insertion_find)
  118. #define BINARY_INSERTION_SORT_START SORT_MAKE_STR(binary_insertion_sort_start)
  119. #define BINARY_INSERTION_SORT SORT_MAKE_STR(binary_insertion_sort)
  120. #define REVERSE_ELEMENTS SORT_MAKE_STR(reverse_elements)
  121. #define COUNT_RUN SORT_MAKE_STR(count_run)
  122. #define CHECK_INVARIANT SORT_MAKE_STR(check_invariant)
  123. #define TIM_SORT SORT_MAKE_STR(tim_sort)
  124. #define TIM_SORT_RESIZE SORT_MAKE_STR(tim_sort_resize)
  125. #define TIM_SORT_MERGE SORT_MAKE_STR(tim_sort_merge)
  126. #define TIM_SORT_COLLAPSE SORT_MAKE_STR(tim_sort_collapse)
  127. #ifndef MAX
  128. #define MAX(x,y) (((x) > (y) ? (x) : (y)))
  129. #endif
  130. #ifndef MIN
  131. #define MIN(x,y) (((x) < (y) ? (x) : (y)))
  132. #endif
  133. typedef struct {
  134. size_t start;
  135. size_t length;
  136. } TIM_SORT_RUN_T;
  137. void BINARY_INSERTION_SORT(SORT_TYPE *dst, const size_t size);
  138. void TIM_SORT(SORT_TYPE *dst, const size_t size);
  139. /* Function used to do a binary search for binary insertion sort */
  140. static __inline size_t BINARY_INSERTION_FIND(SORT_TYPE *dst, const SORT_TYPE x,
  141. const size_t size) {
  142. size_t l, c, r;
  143. SORT_TYPE cx;
  144. l = 0;
  145. r = size - 1;
  146. c = r >> 1;
  147. /* check for out of bounds at the beginning. */
  148. if (SORT_CMP(x, dst[0]) < 0) {
  149. return 0;
  150. } else if (SORT_CMP(x, dst[r]) > 0) {
  151. return r;
  152. }
  153. cx = dst[c];
  154. while (1) {
  155. const int val = SORT_CMP(x, cx);
  156. if (val < 0) {
  157. if (c - l <= 1) {
  158. return c;
  159. }
  160. r = c;
  161. } else { /* allow = for stability. The binary search favors the right. */
  162. if (r - c <= 1) {
  163. return c + 1;
  164. }
  165. l = c;
  166. }
  167. c = l + ((r - l) >> 1);
  168. cx = dst[c];
  169. }
  170. }
  171. /* Binary insertion sort, but knowing that the first "start" entries are sorted. Used in timsort. */
  172. static void BINARY_INSERTION_SORT_START(SORT_TYPE *dst, const size_t start, const size_t size) {
  173. size_t i;
  174. for (i = start; i < size; i++) {
  175. size_t j;
  176. SORT_TYPE x;
  177. size_t location;
  178. /* If this entry is already correct, just move along */
  179. if (SORT_CMP(dst[i - 1], dst[i]) <= 0) {
  180. continue;
  181. }
  182. /* Else we need to find the right place, shift everything over, and squeeze in */
  183. x = dst[i];
  184. location = BINARY_INSERTION_FIND(dst, x, i);
  185. for (j = i - 1; j >= location; j--) {
  186. dst[j + 1] = dst[j];
  187. if (j == 0) { /* check edge case because j is unsigned */
  188. break;
  189. }
  190. }
  191. dst[location] = x;
  192. }
  193. }
  194. /* Binary insertion sort */
  195. void BINARY_INSERTION_SORT(SORT_TYPE *dst, const size_t size) {
  196. /* don't bother sorting an array of size <= 1 */
  197. if (size <= 1) {
  198. return;
  199. }
  200. BINARY_INSERTION_SORT_START(dst, 1, size);
  201. }
  202. /* timsort implementation, based on timsort.txt */
  203. static __inline void REVERSE_ELEMENTS(SORT_TYPE *dst, size_t start, size_t end) {
  204. while (1) {
  205. if (start >= end) {
  206. return;
  207. }
  208. SORT_SWAP(dst[start], dst[end]);
  209. start++;
  210. end--;
  211. }
  212. }
  213. static size_t COUNT_RUN(SORT_TYPE *dst, const size_t start, const size_t size) {
  214. size_t curr;
  215. if (size - start == 1) {
  216. return 1;
  217. }
  218. if (start >= size - 2) {
  219. if (SORT_CMP(dst[size - 2], dst[size - 1]) > 0) {
  220. SORT_SWAP(dst[size - 2], dst[size - 1]);
  221. }
  222. return 2;
  223. }
  224. curr = start + 2;
  225. if (SORT_CMP(dst[start], dst[start + 1]) <= 0) {
  226. /* increasing run */
  227. while (1) {
  228. if (curr == size - 1) {
  229. break;
  230. }
  231. if (SORT_CMP(dst[curr - 1], dst[curr]) > 0) {
  232. break;
  233. }
  234. curr++;
  235. }
  236. return curr - start;
  237. } else {
  238. /* decreasing run */
  239. while (1) {
  240. if (curr == size - 1) {
  241. break;
  242. }
  243. if (SORT_CMP(dst[curr - 1], dst[curr]) <= 0) {
  244. break;
  245. }
  246. curr++;
  247. }
  248. /* reverse in-place */
  249. REVERSE_ELEMENTS(dst, start, curr - 1);
  250. return curr - start;
  251. }
  252. }
  253. static int CHECK_INVARIANT(TIM_SORT_RUN_T *stack, const int stack_curr) {
  254. size_t A, B, C;
  255. if (stack_curr < 2) {
  256. return 1;
  257. }
  258. if (stack_curr == 2) {
  259. const size_t A1 = stack[stack_curr - 2].length;
  260. const size_t B1 = stack[stack_curr - 1].length;
  261. if (A1 <= B1) {
  262. return 0;
  263. }
  264. return 1;
  265. }
  266. A = stack[stack_curr - 3].length;
  267. B = stack[stack_curr - 2].length;
  268. C = stack[stack_curr - 1].length;
  269. if ((A <= B + C) || (B <= C)) {
  270. return 0;
  271. }
  272. return 1;
  273. }
  274. typedef struct {
  275. size_t alloc;
  276. SORT_TYPE *storage;
  277. } TEMP_STORAGE_T;
  278. static void TIM_SORT_RESIZE(TEMP_STORAGE_T *store, const size_t new_size) {
  279. if (store->alloc < new_size) {
  280. SORT_TYPE *tempstore = (SORT_TYPE *)realloc(store->storage, new_size * sizeof(SORT_TYPE));
  281. if (tempstore == NULL) {
  282. fprintf(stderr, "Error allocating temporary storage for tim sort: need %lu bytes",
  283. (unsigned long)(sizeof(SORT_TYPE) * new_size));
  284. exit(1);
  285. }
  286. store->storage = tempstore;
  287. store->alloc = new_size;
  288. }
  289. }
  290. static void TIM_SORT_MERGE(SORT_TYPE *dst, const TIM_SORT_RUN_T *stack, const int stack_curr,
  291. TEMP_STORAGE_T *store) {
  292. const size_t A = stack[stack_curr - 2].length;
  293. const size_t B = stack[stack_curr - 1].length;
  294. const size_t curr = stack[stack_curr - 2].start;
  295. SORT_TYPE *storage;
  296. size_t i, j, k;
  297. TIM_SORT_RESIZE(store, MIN(A, B));
  298. storage = store->storage;
  299. /* left merge */
  300. if (A < B) {
  301. memcpy(storage, &dst[curr], A * sizeof(SORT_TYPE));
  302. i = 0;
  303. j = curr + A;
  304. for (k = curr; k < curr + A + B; k++) {
  305. if ((i < A) && (j < curr + A + B)) {
  306. if (SORT_CMP(storage[i], dst[j]) <= 0) {
  307. dst[k] = storage[i++];
  308. } else {
  309. dst[k] = dst[j++];
  310. }
  311. } else if (i < A) {
  312. dst[k] = storage[i++];
  313. } else {
  314. break;
  315. }
  316. }
  317. } else {
  318. /* right merge */
  319. memcpy(storage, &dst[curr + A], B * sizeof(SORT_TYPE));
  320. i = B;
  321. j = curr + A;
  322. k = curr + A + B;
  323. while (k > curr) {
  324. k--;
  325. if ((i > 0) && (j > curr)) {
  326. if (SORT_CMP(dst[j - 1], storage[i - 1]) > 0) {
  327. dst[k] = dst[--j];
  328. } else {
  329. dst[k] = storage[--i];
  330. }
  331. } else if (i > 0) {
  332. dst[k] = storage[--i];
  333. } else {
  334. break;
  335. }
  336. }
  337. }
  338. }
  339. static int TIM_SORT_COLLAPSE(SORT_TYPE *dst, TIM_SORT_RUN_T *stack, int stack_curr,
  340. TEMP_STORAGE_T *store, const size_t size) {
  341. while (1) {
  342. size_t A, B, C, D;
  343. int ABC, BCD, CD;
  344. /* if the stack only has one thing on it, we are done with the collapse */
  345. if (stack_curr <= 1) {
  346. break;
  347. }
  348. /* if this is the last merge, just do it */
  349. if ((stack_curr == 2) && (stack[0].length + stack[1].length == size)) {
  350. TIM_SORT_MERGE(dst, stack, stack_curr, store);
  351. stack[0].length += stack[1].length;
  352. stack_curr--;
  353. break;
  354. }
  355. /* check if the invariant is off for a stack of 2 elements */
  356. else if ((stack_curr == 2) && (stack[0].length <= stack[1].length)) {
  357. TIM_SORT_MERGE(dst, stack, stack_curr, store);
  358. stack[0].length += stack[1].length;
  359. stack_curr--;
  360. break;
  361. } else if (stack_curr == 2) {
  362. break;
  363. }
  364. B = stack[stack_curr - 3].length;
  365. C = stack[stack_curr - 2].length;
  366. D = stack[stack_curr - 1].length;
  367. if (stack_curr >= 4) {
  368. A = stack[stack_curr - 4].length;
  369. ABC = (A <= B + C);
  370. } else {
  371. ABC = 0;
  372. }
  373. BCD = (B <= C + D) || ABC;
  374. CD = (C <= D);
  375. /* Both invariants are good */
  376. if (!BCD && !CD) {
  377. break;
  378. }
  379. /* left merge */
  380. if (BCD && !CD) {
  381. TIM_SORT_MERGE(dst, stack, stack_curr - 1, store);
  382. stack[stack_curr - 3].length += stack[stack_curr - 2].length;
  383. stack[stack_curr - 2] = stack[stack_curr - 1];
  384. stack_curr--;
  385. } else {
  386. /* right merge */
  387. TIM_SORT_MERGE(dst, stack, stack_curr, store);
  388. stack[stack_curr - 2].length += stack[stack_curr - 1].length;
  389. stack_curr--;
  390. }
  391. }
  392. return stack_curr;
  393. }
  394. static __inline int PUSH_NEXT(SORT_TYPE *dst,
  395. const size_t size,
  396. TEMP_STORAGE_T *store,
  397. const size_t minrun,
  398. TIM_SORT_RUN_T *run_stack,
  399. size_t *stack_curr,
  400. size_t *curr) {
  401. size_t len = COUNT_RUN(dst, *curr, size);
  402. size_t run = minrun;
  403. if (run > size - *curr) {
  404. run = size - *curr;
  405. }
  406. if (run > len) {
  407. BINARY_INSERTION_SORT_START(&dst[*curr], len, run);
  408. len = run;
  409. }
  410. run_stack[*stack_curr].start = *curr;
  411. run_stack[*stack_curr].length = len;
  412. (*stack_curr)++;
  413. *curr += len;
  414. if (*curr == size) {
  415. /* finish up */
  416. while (*stack_curr > 1) {
  417. TIM_SORT_MERGE(dst, run_stack, *stack_curr, store);
  418. run_stack[*stack_curr - 2].length += run_stack[*stack_curr - 1].length;
  419. (*stack_curr)--;
  420. }
  421. if (store->storage != NULL) {
  422. free(store->storage);
  423. store->storage = NULL;
  424. }
  425. return 0;
  426. }
  427. return 1;
  428. }
  429. void TIM_SORT(SORT_TYPE *dst, const size_t size) {
  430. size_t minrun;
  431. TEMP_STORAGE_T _store, *store;
  432. TIM_SORT_RUN_T run_stack[TIM_SORT_STACK_SIZE];
  433. size_t stack_curr = 0;
  434. size_t curr = 0;
  435. /* don't bother sorting an array of size 1 */
  436. if (size <= 1) {
  437. return;
  438. }
  439. if (size < 64) {
  440. BINARY_INSERTION_SORT(dst, size);
  441. return;
  442. }
  443. /* compute the minimum run length */
  444. minrun = compute_minrun(size);
  445. /* temporary storage for merges */
  446. store = &_store;
  447. store->alloc = 0;
  448. store->storage = NULL;
  449. if (!PUSH_NEXT(dst, size, store, minrun, run_stack, &stack_curr, &curr)) {
  450. return;
  451. }
  452. if (!PUSH_NEXT(dst, size, store, minrun, run_stack, &stack_curr, &curr)) {
  453. return;
  454. }
  455. if (!PUSH_NEXT(dst, size, store, minrun, run_stack, &stack_curr, &curr)) {
  456. return;
  457. }
  458. while (1) {
  459. if (!CHECK_INVARIANT(run_stack, stack_curr)) {
  460. stack_curr = TIM_SORT_COLLAPSE(dst, run_stack, stack_curr, store, size);
  461. continue;
  462. }
  463. if (!PUSH_NEXT(dst, size, store, minrun, run_stack, &stack_curr, &curr)) {
  464. return;
  465. }
  466. }
  467. }
  468. #undef SORT_CONCAT
  469. #undef SORT_MAKE_STR1
  470. #undef SORT_MAKE_STR
  471. #undef SORT_NAME
  472. #undef SORT_TYPE
  473. #undef SORT_CMP
  474. #undef TEMP_STORAGE_T
  475. #undef TIM_SORT_RUN_T
  476. #undef PUSH_NEXT
  477. #undef SORT_SWAP
  478. #undef SORT_CONCAT
  479. #undef SORT_MAKE_STR1
  480. #undef SORT_MAKE_STR
  481. #undef BINARY_INSERTION_FIND
  482. #undef BINARY_INSERTION_SORT_START
  483. #undef BINARY_INSERTION_SORT
  484. #undef REVERSE_ELEMENTS
  485. #undef COUNT_RUN
  486. #undef TIM_SORT
  487. #undef TIM_SORT_RESIZE
  488. #undef TIM_SORT_COLLAPSE
  489. #undef TIM_SORT_RUN_T
  490. #undef TEMP_STORAGE_T