timsort.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. // https://github.com/mziccard/node-timsort
  2. var DEFAULT_MIN_MERGE = 32;
  3. var DEFAULT_MIN_GALLOPING = 7;
  4. var DEFAULT_TMP_STORAGE_LENGTH = 256;
  5. function minRunLength(n) {
  6. var r = 0;
  7. while (n >= DEFAULT_MIN_MERGE) {
  8. r |= n & 1;
  9. n >>= 1;
  10. }
  11. return n + r;
  12. }
  13. function makeAscendingRun(array, lo, hi, compare) {
  14. var runHi = lo + 1;
  15. if (runHi === hi) {
  16. return 1;
  17. }
  18. if (compare(array[runHi++], array[lo]) < 0) {
  19. while (runHi < hi && compare(array[runHi], array[runHi - 1]) < 0) {
  20. runHi++;
  21. }
  22. reverseRun(array, lo, runHi);
  23. } else {
  24. while (runHi < hi && compare(array[runHi], array[runHi - 1]) >= 0) {
  25. runHi++;
  26. }
  27. }
  28. return runHi - lo;
  29. }
  30. function reverseRun(array, lo, hi) {
  31. hi--;
  32. while (lo < hi) {
  33. var t = array[lo];
  34. array[lo++] = array[hi];
  35. array[hi--] = t;
  36. }
  37. }
  38. function binaryInsertionSort(array, lo, hi, start, compare) {
  39. if (start === lo) {
  40. start++;
  41. }
  42. for (; start < hi; start++) {
  43. var pivot = array[start];
  44. var left = lo;
  45. var right = start;
  46. var mid;
  47. while (left < right) {
  48. mid = left + right >>> 1;
  49. if (compare(pivot, array[mid]) < 0) {
  50. right = mid;
  51. } else {
  52. left = mid + 1;
  53. }
  54. }
  55. var n = start - left;
  56. switch (n) {
  57. case 3:
  58. array[left + 3] = array[left + 2];
  59. case 2:
  60. array[left + 2] = array[left + 1];
  61. case 1:
  62. array[left + 1] = array[left];
  63. break;
  64. default:
  65. while (n > 0) {
  66. array[left + n] = array[left + n - 1];
  67. n--;
  68. }
  69. }
  70. array[left] = pivot;
  71. }
  72. }
  73. function gallopLeft(value, array, start, length, hint, compare) {
  74. var lastOffset = 0;
  75. var maxOffset = 0;
  76. var offset = 1;
  77. if (compare(value, array[start + hint]) > 0) {
  78. maxOffset = length - hint;
  79. while (offset < maxOffset && compare(value, array[start + hint + offset]) > 0) {
  80. lastOffset = offset;
  81. offset = (offset << 1) + 1;
  82. if (offset <= 0) {
  83. offset = maxOffset;
  84. }
  85. }
  86. if (offset > maxOffset) {
  87. offset = maxOffset;
  88. }
  89. lastOffset += hint;
  90. offset += hint;
  91. } else {
  92. maxOffset = hint + 1;
  93. while (offset < maxOffset && compare(value, array[start + hint - offset]) <= 0) {
  94. lastOffset = offset;
  95. offset = (offset << 1) + 1;
  96. if (offset <= 0) {
  97. offset = maxOffset;
  98. }
  99. }
  100. if (offset > maxOffset) {
  101. offset = maxOffset;
  102. }
  103. var tmp = lastOffset;
  104. lastOffset = hint - offset;
  105. offset = hint - tmp;
  106. }
  107. lastOffset++;
  108. while (lastOffset < offset) {
  109. var m = lastOffset + (offset - lastOffset >>> 1);
  110. if (compare(value, array[start + m]) > 0) {
  111. lastOffset = m + 1;
  112. } else {
  113. offset = m;
  114. }
  115. }
  116. return offset;
  117. }
  118. function gallopRight(value, array, start, length, hint, compare) {
  119. var lastOffset = 0;
  120. var maxOffset = 0;
  121. var offset = 1;
  122. if (compare(value, array[start + hint]) < 0) {
  123. maxOffset = hint + 1;
  124. while (offset < maxOffset && compare(value, array[start + hint - offset]) < 0) {
  125. lastOffset = offset;
  126. offset = (offset << 1) + 1;
  127. if (offset <= 0) {
  128. offset = maxOffset;
  129. }
  130. }
  131. if (offset > maxOffset) {
  132. offset = maxOffset;
  133. }
  134. var tmp = lastOffset;
  135. lastOffset = hint - offset;
  136. offset = hint - tmp;
  137. } else {
  138. maxOffset = length - hint;
  139. while (offset < maxOffset && compare(value, array[start + hint + offset]) >= 0) {
  140. lastOffset = offset;
  141. offset = (offset << 1) + 1;
  142. if (offset <= 0) {
  143. offset = maxOffset;
  144. }
  145. }
  146. if (offset > maxOffset) {
  147. offset = maxOffset;
  148. }
  149. lastOffset += hint;
  150. offset += hint;
  151. }
  152. lastOffset++;
  153. while (lastOffset < offset) {
  154. var m = lastOffset + (offset - lastOffset >>> 1);
  155. if (compare(value, array[start + m]) < 0) {
  156. offset = m;
  157. } else {
  158. lastOffset = m + 1;
  159. }
  160. }
  161. return offset;
  162. }
  163. function TimSort(array, compare) {
  164. var minGallop = DEFAULT_MIN_GALLOPING;
  165. var length = 0;
  166. var tmpStorageLength = DEFAULT_TMP_STORAGE_LENGTH;
  167. var stackLength = 0;
  168. var runStart;
  169. var runLength;
  170. var stackSize = 0;
  171. length = array.length;
  172. if (length < 2 * DEFAULT_TMP_STORAGE_LENGTH) {
  173. tmpStorageLength = length >>> 1;
  174. }
  175. var tmp = [];
  176. stackLength = length < 120 ? 5 : length < 1542 ? 10 : length < 119151 ? 19 : 40;
  177. runStart = [];
  178. runLength = [];
  179. function pushRun(_runStart, _runLength) {
  180. runStart[stackSize] = _runStart;
  181. runLength[stackSize] = _runLength;
  182. stackSize += 1;
  183. }
  184. function mergeRuns() {
  185. while (stackSize > 1) {
  186. var n = stackSize - 2;
  187. if (n >= 1 && runLength[n - 1] <= runLength[n] + runLength[n + 1] || n >= 2 && runLength[n - 2] <= runLength[n] + runLength[n - 1]) {
  188. if (runLength[n - 1] < runLength[n + 1]) {
  189. n--;
  190. }
  191. } else if (runLength[n] > runLength[n + 1]) {
  192. break;
  193. }
  194. mergeAt(n);
  195. }
  196. }
  197. function forceMergeRuns() {
  198. while (stackSize > 1) {
  199. var n = stackSize - 2;
  200. if (n > 0 && runLength[n - 1] < runLength[n + 1]) {
  201. n--;
  202. }
  203. mergeAt(n);
  204. }
  205. }
  206. function mergeAt(i) {
  207. var start1 = runStart[i];
  208. var length1 = runLength[i];
  209. var start2 = runStart[i + 1];
  210. var length2 = runLength[i + 1];
  211. runLength[i] = length1 + length2;
  212. if (i === stackSize - 3) {
  213. runStart[i + 1] = runStart[i + 2];
  214. runLength[i + 1] = runLength[i + 2];
  215. }
  216. stackSize--;
  217. var k = gallopRight(array[start2], array, start1, length1, 0, compare);
  218. start1 += k;
  219. length1 -= k;
  220. if (length1 === 0) {
  221. return;
  222. }
  223. length2 = gallopLeft(array[start1 + length1 - 1], array, start2, length2, length2 - 1, compare);
  224. if (length2 === 0) {
  225. return;
  226. }
  227. if (length1 <= length2) {
  228. mergeLow(start1, length1, start2, length2);
  229. } else {
  230. mergeHigh(start1, length1, start2, length2);
  231. }
  232. }
  233. function mergeLow(start1, length1, start2, length2) {
  234. var i = 0;
  235. for (i = 0; i < length1; i++) {
  236. tmp[i] = array[start1 + i];
  237. }
  238. var cursor1 = 0;
  239. var cursor2 = start2;
  240. var dest = start1;
  241. array[dest++] = array[cursor2++];
  242. if (--length2 === 0) {
  243. for (i = 0; i < length1; i++) {
  244. array[dest + i] = tmp[cursor1 + i];
  245. }
  246. return;
  247. }
  248. if (length1 === 1) {
  249. for (i = 0; i < length2; i++) {
  250. array[dest + i] = array[cursor2 + i];
  251. }
  252. array[dest + length2] = tmp[cursor1];
  253. return;
  254. }
  255. var _minGallop = minGallop;
  256. var count1;
  257. var count2;
  258. var exit;
  259. while (1) {
  260. count1 = 0;
  261. count2 = 0;
  262. exit = false;
  263. do {
  264. if (compare(array[cursor2], tmp[cursor1]) < 0) {
  265. array[dest++] = array[cursor2++];
  266. count2++;
  267. count1 = 0;
  268. if (--length2 === 0) {
  269. exit = true;
  270. break;
  271. }
  272. } else {
  273. array[dest++] = tmp[cursor1++];
  274. count1++;
  275. count2 = 0;
  276. if (--length1 === 1) {
  277. exit = true;
  278. break;
  279. }
  280. }
  281. } while ((count1 | count2) < _minGallop);
  282. if (exit) {
  283. break;
  284. }
  285. do {
  286. count1 = gallopRight(array[cursor2], tmp, cursor1, length1, 0, compare);
  287. if (count1 !== 0) {
  288. for (i = 0; i < count1; i++) {
  289. array[dest + i] = tmp[cursor1 + i];
  290. }
  291. dest += count1;
  292. cursor1 += count1;
  293. length1 -= count1;
  294. if (length1 <= 1) {
  295. exit = true;
  296. break;
  297. }
  298. }
  299. array[dest++] = array[cursor2++];
  300. if (--length2 === 0) {
  301. exit = true;
  302. break;
  303. }
  304. count2 = gallopLeft(tmp[cursor1], array, cursor2, length2, 0, compare);
  305. if (count2 !== 0) {
  306. for (i = 0; i < count2; i++) {
  307. array[dest + i] = array[cursor2 + i];
  308. }
  309. dest += count2;
  310. cursor2 += count2;
  311. length2 -= count2;
  312. if (length2 === 0) {
  313. exit = true;
  314. break;
  315. }
  316. }
  317. array[dest++] = tmp[cursor1++];
  318. if (--length1 === 1) {
  319. exit = true;
  320. break;
  321. }
  322. _minGallop--;
  323. } while (count1 >= DEFAULT_MIN_GALLOPING || count2 >= DEFAULT_MIN_GALLOPING);
  324. if (exit) {
  325. break;
  326. }
  327. if (_minGallop < 0) {
  328. _minGallop = 0;
  329. }
  330. _minGallop += 2;
  331. }
  332. minGallop = _minGallop;
  333. minGallop < 1 && (minGallop = 1);
  334. if (length1 === 1) {
  335. for (i = 0; i < length2; i++) {
  336. array[dest + i] = array[cursor2 + i];
  337. }
  338. array[dest + length2] = tmp[cursor1];
  339. } else if (length1 === 0) {
  340. throw new Error(); // throw new Error('mergeLow preconditions were not respected');
  341. } else {
  342. for (i = 0; i < length1; i++) {
  343. array[dest + i] = tmp[cursor1 + i];
  344. }
  345. }
  346. }
  347. function mergeHigh(start1, length1, start2, length2) {
  348. var i = 0;
  349. for (i = 0; i < length2; i++) {
  350. tmp[i] = array[start2 + i];
  351. }
  352. var cursor1 = start1 + length1 - 1;
  353. var cursor2 = length2 - 1;
  354. var dest = start2 + length2 - 1;
  355. var customCursor = 0;
  356. var customDest = 0;
  357. array[dest--] = array[cursor1--];
  358. if (--length1 === 0) {
  359. customCursor = dest - (length2 - 1);
  360. for (i = 0; i < length2; i++) {
  361. array[customCursor + i] = tmp[i];
  362. }
  363. return;
  364. }
  365. if (length2 === 1) {
  366. dest -= length1;
  367. cursor1 -= length1;
  368. customDest = dest + 1;
  369. customCursor = cursor1 + 1;
  370. for (i = length1 - 1; i >= 0; i--) {
  371. array[customDest + i] = array[customCursor + i];
  372. }
  373. array[dest] = tmp[cursor2];
  374. return;
  375. }
  376. var _minGallop = minGallop;
  377. while (true) {
  378. var count1 = 0;
  379. var count2 = 0;
  380. var exit = false;
  381. do {
  382. if (compare(tmp[cursor2], array[cursor1]) < 0) {
  383. array[dest--] = array[cursor1--];
  384. count1++;
  385. count2 = 0;
  386. if (--length1 === 0) {
  387. exit = true;
  388. break;
  389. }
  390. } else {
  391. array[dest--] = tmp[cursor2--];
  392. count2++;
  393. count1 = 0;
  394. if (--length2 === 1) {
  395. exit = true;
  396. break;
  397. }
  398. }
  399. } while ((count1 | count2) < _minGallop);
  400. if (exit) {
  401. break;
  402. }
  403. do {
  404. count1 = length1 - gallopRight(tmp[cursor2], array, start1, length1, length1 - 1, compare);
  405. if (count1 !== 0) {
  406. dest -= count1;
  407. cursor1 -= count1;
  408. length1 -= count1;
  409. customDest = dest + 1;
  410. customCursor = cursor1 + 1;
  411. for (i = count1 - 1; i >= 0; i--) {
  412. array[customDest + i] = array[customCursor + i];
  413. }
  414. if (length1 === 0) {
  415. exit = true;
  416. break;
  417. }
  418. }
  419. array[dest--] = tmp[cursor2--];
  420. if (--length2 === 1) {
  421. exit = true;
  422. break;
  423. }
  424. count2 = length2 - gallopLeft(array[cursor1], tmp, 0, length2, length2 - 1, compare);
  425. if (count2 !== 0) {
  426. dest -= count2;
  427. cursor2 -= count2;
  428. length2 -= count2;
  429. customDest = dest + 1;
  430. customCursor = cursor2 + 1;
  431. for (i = 0; i < count2; i++) {
  432. array[customDest + i] = tmp[customCursor + i];
  433. }
  434. if (length2 <= 1) {
  435. exit = true;
  436. break;
  437. }
  438. }
  439. array[dest--] = array[cursor1--];
  440. if (--length1 === 0) {
  441. exit = true;
  442. break;
  443. }
  444. _minGallop--;
  445. } while (count1 >= DEFAULT_MIN_GALLOPING || count2 >= DEFAULT_MIN_GALLOPING);
  446. if (exit) {
  447. break;
  448. }
  449. if (_minGallop < 0) {
  450. _minGallop = 0;
  451. }
  452. _minGallop += 2;
  453. }
  454. minGallop = _minGallop;
  455. if (minGallop < 1) {
  456. minGallop = 1;
  457. }
  458. if (length2 === 1) {
  459. dest -= length1;
  460. cursor1 -= length1;
  461. customDest = dest + 1;
  462. customCursor = cursor1 + 1;
  463. for (i = length1 - 1; i >= 0; i--) {
  464. array[customDest + i] = array[customCursor + i];
  465. }
  466. array[dest] = tmp[cursor2];
  467. } else if (length2 === 0) {
  468. throw new Error(); // throw new Error('mergeHigh preconditions were not respected');
  469. } else {
  470. customCursor = dest - (length2 - 1);
  471. for (i = 0; i < length2; i++) {
  472. array[customCursor + i] = tmp[i];
  473. }
  474. }
  475. }
  476. this.mergeRuns = mergeRuns;
  477. this.forceMergeRuns = forceMergeRuns;
  478. this.pushRun = pushRun;
  479. }
  480. function sort(array, compare, lo, hi) {
  481. if (!lo) {
  482. lo = 0;
  483. }
  484. if (!hi) {
  485. hi = array.length;
  486. }
  487. var remaining = hi - lo;
  488. if (remaining < 2) {
  489. return;
  490. }
  491. var runLength = 0;
  492. if (remaining < DEFAULT_MIN_MERGE) {
  493. runLength = makeAscendingRun(array, lo, hi, compare);
  494. binaryInsertionSort(array, lo, hi, lo + runLength, compare);
  495. return;
  496. }
  497. var ts = new TimSort(array, compare);
  498. var minRun = minRunLength(remaining);
  499. do {
  500. runLength = makeAscendingRun(array, lo, hi, compare);
  501. if (runLength < minRun) {
  502. var force = remaining;
  503. if (force > minRun) {
  504. force = minRun;
  505. }
  506. binaryInsertionSort(array, lo, lo + force, lo + runLength, compare);
  507. runLength = force;
  508. }
  509. ts.pushRun(lo, runLength);
  510. ts.mergeRuns();
  511. remaining -= runLength;
  512. lo += runLength;
  513. } while (remaining !== 0);
  514. ts.forceMergeRuns();
  515. }
  516. module.exports = sort;