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.

815 lines
18 KiB

4 years ago
  1. /****
  2. * The MIT License
  3. *
  4. * Copyright (c) 2015 Marco Ziccardi
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. *
  24. ****/
  25. (function (global, factory) {
  26. if (typeof define === 'function' && define.amd) {
  27. define('timsort', ['exports'], factory);
  28. } else if (typeof exports !== 'undefined') {
  29. factory(exports);
  30. } else {
  31. var mod = {
  32. exports: {}
  33. };
  34. factory(mod.exports);
  35. global.timsort = mod.exports;
  36. }
  37. })(this, function (exports) {
  38. 'use strict';
  39. exports.__esModule = true;
  40. exports.sort = sort;
  41. function _classCallCheck(instance, Constructor) {
  42. if (!(instance instanceof Constructor)) {
  43. throw new TypeError('Cannot call a class as a function');
  44. }
  45. }
  46. var DEFAULT_MIN_MERGE = 32;
  47. var DEFAULT_MIN_GALLOPING = 7;
  48. var DEFAULT_TMP_STORAGE_LENGTH = 256;
  49. var POWERS_OF_TEN = [1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9];
  50. function log10(x) {
  51. if (x < 1e5) {
  52. if (x < 1e2) {
  53. return x < 1e1 ? 0 : 1;
  54. }
  55. if (x < 1e4) {
  56. return x < 1e3 ? 2 : 3;
  57. }
  58. return 4;
  59. }
  60. if (x < 1e7) {
  61. return x < 1e6 ? 5 : 6;
  62. }
  63. if (x < 1e9) {
  64. return x < 1e8 ? 7 : 8;
  65. }
  66. return 9;
  67. }
  68. function alphabeticalCompare(a, b) {
  69. if (a === b) {
  70. return 0;
  71. }
  72. if (~ ~a === a && ~ ~b === b) {
  73. if (a === 0 || b === 0) {
  74. return a < b ? -1 : 1;
  75. }
  76. if (a < 0 || b < 0) {
  77. if (b >= 0) {
  78. return -1;
  79. }
  80. if (a >= 0) {
  81. return 1;
  82. }
  83. a = -a;
  84. b = -b;
  85. }
  86. var al = log10(a);
  87. var bl = log10(b);
  88. var t = 0;
  89. if (al < bl) {
  90. a *= POWERS_OF_TEN[bl - al - 1];
  91. b /= 10;
  92. t = -1;
  93. } else if (al > bl) {
  94. b *= POWERS_OF_TEN[al - bl - 1];
  95. a /= 10;
  96. t = 1;
  97. }
  98. if (a === b) {
  99. return t;
  100. }
  101. return a < b ? -1 : 1;
  102. }
  103. var aStr = String(a);
  104. var bStr = String(b);
  105. if (aStr === bStr) {
  106. return 0;
  107. }
  108. return aStr < bStr ? -1 : 1;
  109. }
  110. function minRunLength(n) {
  111. var r = 0;
  112. while (n >= DEFAULT_MIN_MERGE) {
  113. r |= n & 1;
  114. n >>= 1;
  115. }
  116. return n + r;
  117. }
  118. function makeAscendingRun(array, lo, hi, compare) {
  119. var runHi = lo + 1;
  120. if (runHi === hi) {
  121. return 1;
  122. }
  123. if (compare(array[runHi++], array[lo]) < 0) {
  124. while (runHi < hi && compare(array[runHi], array[runHi - 1]) < 0) {
  125. runHi++;
  126. }
  127. reverseRun(array, lo, runHi);
  128. } else {
  129. while (runHi < hi && compare(array[runHi], array[runHi - 1]) >= 0) {
  130. runHi++;
  131. }
  132. }
  133. return runHi - lo;
  134. }
  135. function reverseRun(array, lo, hi) {
  136. hi--;
  137. while (lo < hi) {
  138. var t = array[lo];
  139. array[lo++] = array[hi];
  140. array[hi--] = t;
  141. }
  142. }
  143. function binaryInsertionSort(array, lo, hi, start, compare) {
  144. if (start === lo) {
  145. start++;
  146. }
  147. for (; start < hi; start++) {
  148. var pivot = array[start];
  149. var left = lo;
  150. var right = start;
  151. while (left < right) {
  152. var mid = left + right >>> 1;
  153. if (compare(pivot, array[mid]) < 0) {
  154. right = mid;
  155. } else {
  156. left = mid + 1;
  157. }
  158. }
  159. var n = start - left;
  160. switch (n) {
  161. case 3:
  162. array[left + 3] = array[left + 2];
  163. case 2:
  164. array[left + 2] = array[left + 1];
  165. case 1:
  166. array[left + 1] = array[left];
  167. break;
  168. default:
  169. while (n > 0) {
  170. array[left + n] = array[left + n - 1];
  171. n--;
  172. }
  173. }
  174. array[left] = pivot;
  175. }
  176. }
  177. function gallopLeft(value, array, start, length, hint, compare) {
  178. var lastOffset = 0;
  179. var maxOffset = 0;
  180. var offset = 1;
  181. if (compare(value, array[start + hint]) > 0) {
  182. maxOffset = length - hint;
  183. while (offset < maxOffset && compare(value, array[start + hint + offset]) > 0) {
  184. lastOffset = offset;
  185. offset = (offset << 1) + 1;
  186. if (offset <= 0) {
  187. offset = maxOffset;
  188. }
  189. }
  190. if (offset > maxOffset) {
  191. offset = maxOffset;
  192. }
  193. lastOffset += hint;
  194. offset += hint;
  195. } else {
  196. maxOffset = hint + 1;
  197. while (offset < maxOffset && compare(value, array[start + hint - offset]) <= 0) {
  198. lastOffset = offset;
  199. offset = (offset << 1) + 1;
  200. if (offset <= 0) {
  201. offset = maxOffset;
  202. }
  203. }
  204. if (offset > maxOffset) {
  205. offset = maxOffset;
  206. }
  207. var tmp = lastOffset;
  208. lastOffset = hint - offset;
  209. offset = hint - tmp;
  210. }
  211. lastOffset++;
  212. while (lastOffset < offset) {
  213. var m = lastOffset + (offset - lastOffset >>> 1);
  214. if (compare(value, array[start + m]) > 0) {
  215. lastOffset = m + 1;
  216. } else {
  217. offset = m;
  218. }
  219. }
  220. return offset;
  221. }
  222. function gallopRight(value, array, start, length, hint, compare) {
  223. var lastOffset = 0;
  224. var maxOffset = 0;
  225. var offset = 1;
  226. if (compare(value, array[start + hint]) < 0) {
  227. maxOffset = hint + 1;
  228. while (offset < maxOffset && compare(value, array[start + hint - offset]) < 0) {
  229. lastOffset = offset;
  230. offset = (offset << 1) + 1;
  231. if (offset <= 0) {
  232. offset = maxOffset;
  233. }
  234. }
  235. if (offset > maxOffset) {
  236. offset = maxOffset;
  237. }
  238. var tmp = lastOffset;
  239. lastOffset = hint - offset;
  240. offset = hint - tmp;
  241. } else {
  242. maxOffset = length - hint;
  243. while (offset < maxOffset && compare(value, array[start + hint + offset]) >= 0) {
  244. lastOffset = offset;
  245. offset = (offset << 1) + 1;
  246. if (offset <= 0) {
  247. offset = maxOffset;
  248. }
  249. }
  250. if (offset > maxOffset) {
  251. offset = maxOffset;
  252. }
  253. lastOffset += hint;
  254. offset += hint;
  255. }
  256. lastOffset++;
  257. while (lastOffset < offset) {
  258. var m = lastOffset + (offset - lastOffset >>> 1);
  259. if (compare(value, array[start + m]) < 0) {
  260. offset = m;
  261. } else {
  262. lastOffset = m + 1;
  263. }
  264. }
  265. return offset;
  266. }
  267. var TimSort = (function () {
  268. function TimSort(array, compare) {
  269. _classCallCheck(this, TimSort);
  270. this.array = null;
  271. this.compare = null;
  272. this.minGallop = DEFAULT_MIN_GALLOPING;
  273. this.length = 0;
  274. this.tmpStorageLength = DEFAULT_TMP_STORAGE_LENGTH;
  275. this.stackLength = 0;
  276. this.runStart = null;
  277. this.runLength = null;
  278. this.stackSize = 0;
  279. this.array = array;
  280. this.compare = compare;
  281. this.length = array.length;
  282. if (this.length < 2 * DEFAULT_TMP_STORAGE_LENGTH) {
  283. this.tmpStorageLength = this.length >>> 1;
  284. }
  285. this.tmp = new Array(this.tmpStorageLength);
  286. this.stackLength = this.length < 120 ? 5 : this.length < 1542 ? 10 : this.length < 119151 ? 19 : 40;
  287. this.runStart = new Array(this.stackLength);
  288. this.runLength = new Array(this.stackLength);
  289. }
  290. TimSort.prototype.pushRun = function pushRun(runStart, runLength) {
  291. this.runStart[this.stackSize] = runStart;
  292. this.runLength[this.stackSize] = runLength;
  293. this.stackSize += 1;
  294. };
  295. TimSort.prototype.mergeRuns = function mergeRuns() {
  296. while (this.stackSize > 1) {
  297. var n = this.stackSize - 2;
  298. if (n >= 1 && this.runLength[n - 1] <= this.runLength[n] + this.runLength[n + 1] || n >= 2 && this.runLength[n - 2] <= this.runLength[n] + this.runLength[n - 1]) {
  299. if (this.runLength[n - 1] < this.runLength[n + 1]) {
  300. n--;
  301. }
  302. } else if (this.runLength[n] > this.runLength[n + 1]) {
  303. break;
  304. }
  305. this.mergeAt(n);
  306. }
  307. };
  308. TimSort.prototype.forceMergeRuns = function forceMergeRuns() {
  309. while (this.stackSize > 1) {
  310. var n = this.stackSize - 2;
  311. if (n > 0 && this.runLength[n - 1] < this.runLength[n + 1]) {
  312. n--;
  313. }
  314. this.mergeAt(n);
  315. }
  316. };
  317. TimSort.prototype.mergeAt = function mergeAt(i) {
  318. var compare = this.compare;
  319. var array = this.array;
  320. var start1 = this.runStart[i];
  321. var length1 = this.runLength[i];
  322. var start2 = this.runStart[i + 1];
  323. var length2 = this.runLength[i + 1];
  324. this.runLength[i] = length1 + length2;
  325. if (i === this.stackSize - 3) {
  326. this.runStart[i + 1] = this.runStart[i + 2];
  327. this.runLength[i + 1] = this.runLength[i + 2];
  328. }
  329. this.stackSize--;
  330. var k = gallopRight(array[start2], array, start1, length1, 0, compare);
  331. start1 += k;
  332. length1 -= k;
  333. if (length1 === 0) {
  334. return;
  335. }
  336. length2 = gallopLeft(array[start1 + length1 - 1], array, start2, length2, length2 - 1, compare);
  337. if (length2 === 0) {
  338. return;
  339. }
  340. if (length1 <= length2) {
  341. this.mergeLow(start1, length1, start2, length2);
  342. } else {
  343. this.mergeHigh(start1, length1, start2, length2);
  344. }
  345. };
  346. TimSort.prototype.mergeLow = function mergeLow(start1, length1, start2, length2) {
  347. var compare = this.compare;
  348. var array = this.array;
  349. var tmp = this.tmp;
  350. var i = 0;
  351. for (i = 0; i < length1; i++) {
  352. tmp[i] = array[start1 + i];
  353. }
  354. var cursor1 = 0;
  355. var cursor2 = start2;
  356. var dest = start1;
  357. array[dest++] = array[cursor2++];
  358. if (--length2 === 0) {
  359. for (i = 0; i < length1; i++) {
  360. array[dest + i] = tmp[cursor1 + i];
  361. }
  362. return;
  363. }
  364. if (length1 === 1) {
  365. for (i = 0; i < length2; i++) {
  366. array[dest + i] = array[cursor2 + i];
  367. }
  368. array[dest + length2] = tmp[cursor1];
  369. return;
  370. }
  371. var minGallop = this.minGallop;
  372. while (true) {
  373. var count1 = 0;
  374. var count2 = 0;
  375. var exit = false;
  376. do {
  377. if (compare(array[cursor2], tmp[cursor1]) < 0) {
  378. array[dest++] = array[cursor2++];
  379. count2++;
  380. count1 = 0;
  381. if (--length2 === 0) {
  382. exit = true;
  383. break;
  384. }
  385. } else {
  386. array[dest++] = tmp[cursor1++];
  387. count1++;
  388. count2 = 0;
  389. if (--length1 === 1) {
  390. exit = true;
  391. break;
  392. }
  393. }
  394. } while ((count1 | count2) < minGallop);
  395. if (exit) {
  396. break;
  397. }
  398. do {
  399. count1 = gallopRight(array[cursor2], tmp, cursor1, length1, 0, compare);
  400. if (count1 !== 0) {
  401. for (i = 0; i < count1; i++) {
  402. array[dest + i] = tmp[cursor1 + i];
  403. }
  404. dest += count1;
  405. cursor1 += count1;
  406. length1 -= count1;
  407. if (length1 <= 1) {
  408. exit = true;
  409. break;
  410. }
  411. }
  412. array[dest++] = array[cursor2++];
  413. if (--length2 === 0) {
  414. exit = true;
  415. break;
  416. }
  417. count2 = gallopLeft(tmp[cursor1], array, cursor2, length2, 0, compare);
  418. if (count2 !== 0) {
  419. for (i = 0; i < count2; i++) {
  420. array[dest + i] = array[cursor2 + i];
  421. }
  422. dest += count2;
  423. cursor2 += count2;
  424. length2 -= count2;
  425. if (length2 === 0) {
  426. exit = true;
  427. break;
  428. }
  429. }
  430. array[dest++] = tmp[cursor1++];
  431. if (--length1 === 1) {
  432. exit = true;
  433. break;
  434. }
  435. minGallop--;
  436. } while (count1 >= DEFAULT_MIN_GALLOPING || count2 >= DEFAULT_MIN_GALLOPING);
  437. if (exit) {
  438. break;
  439. }
  440. if (minGallop < 0) {
  441. minGallop = 0;
  442. }
  443. minGallop += 2;
  444. }
  445. this.minGallop = minGallop;
  446. if (minGallop < 1) {
  447. this.minGallop = 1;
  448. }
  449. if (length1 === 1) {
  450. for (i = 0; i < length2; i++) {
  451. array[dest + i] = array[cursor2 + i];
  452. }
  453. array[dest + length2] = tmp[cursor1];
  454. } else if (length1 === 0) {
  455. throw new Error('mergeLow preconditions were not respected');
  456. } else {
  457. for (i = 0; i < length1; i++) {
  458. array[dest + i] = tmp[cursor1 + i];
  459. }
  460. }
  461. };
  462. TimSort.prototype.mergeHigh = function mergeHigh(start1, length1, start2, length2) {
  463. var compare = this.compare;
  464. var array = this.array;
  465. var tmp = this.tmp;
  466. var i = 0;
  467. for (i = 0; i < length2; i++) {
  468. tmp[i] = array[start2 + i];
  469. }
  470. var cursor1 = start1 + length1 - 1;
  471. var cursor2 = length2 - 1;
  472. var dest = start2 + length2 - 1;
  473. var customCursor = 0;
  474. var customDest = 0;
  475. array[dest--] = array[cursor1--];
  476. if (--length1 === 0) {
  477. customCursor = dest - (length2 - 1);
  478. for (i = 0; i < length2; i++) {
  479. array[customCursor + i] = tmp[i];
  480. }
  481. return;
  482. }
  483. if (length2 === 1) {
  484. dest -= length1;
  485. cursor1 -= length1;
  486. customDest = dest + 1;
  487. customCursor = cursor1 + 1;
  488. for (i = length1 - 1; i >= 0; i--) {
  489. array[customDest + i] = array[customCursor + i];
  490. }
  491. array[dest] = tmp[cursor2];
  492. return;
  493. }
  494. var minGallop = this.minGallop;
  495. while (true) {
  496. var count1 = 0;
  497. var count2 = 0;
  498. var exit = false;
  499. do {
  500. if (compare(tmp[cursor2], array[cursor1]) < 0) {
  501. array[dest--] = array[cursor1--];
  502. count1++;
  503. count2 = 0;
  504. if (--length1 === 0) {
  505. exit = true;
  506. break;
  507. }
  508. } else {
  509. array[dest--] = tmp[cursor2--];
  510. count2++;
  511. count1 = 0;
  512. if (--length2 === 1) {
  513. exit = true;
  514. break;
  515. }
  516. }
  517. } while ((count1 | count2) < minGallop);
  518. if (exit) {
  519. break;
  520. }
  521. do {
  522. count1 = length1 - gallopRight(tmp[cursor2], array, start1, length1, length1 - 1, compare);
  523. if (count1 !== 0) {
  524. dest -= count1;
  525. cursor1 -= count1;
  526. length1 -= count1;
  527. customDest = dest + 1;
  528. customCursor = cursor1 + 1;
  529. for (i = count1 - 1; i >= 0; i--) {
  530. array[customDest + i] = array[customCursor + i];
  531. }
  532. if (length1 === 0) {
  533. exit = true;
  534. break;
  535. }
  536. }
  537. array[dest--] = tmp[cursor2--];
  538. if (--length2 === 1) {
  539. exit = true;
  540. break;
  541. }
  542. count2 = length2 - gallopLeft(array[cursor1], tmp, 0, length2, length2 - 1, compare);
  543. if (count2 !== 0) {
  544. dest -= count2;
  545. cursor2 -= count2;
  546. length2 -= count2;
  547. customDest = dest + 1;
  548. customCursor = cursor2 + 1;
  549. for (i = 0; i < count2; i++) {
  550. array[customDest + i] = tmp[customCursor + i];
  551. }
  552. if (length2 <= 1) {
  553. exit = true;
  554. break;
  555. }
  556. }
  557. array[dest--] = array[cursor1--];
  558. if (--length1 === 0) {
  559. exit = true;
  560. break;
  561. }
  562. minGallop--;
  563. } while (count1 >= DEFAULT_MIN_GALLOPING || count2 >= DEFAULT_MIN_GALLOPING);
  564. if (exit) {
  565. break;
  566. }
  567. if (minGallop < 0) {
  568. minGallop = 0;
  569. }
  570. minGallop += 2;
  571. }
  572. this.minGallop = minGallop;
  573. if (minGallop < 1) {
  574. this.minGallop = 1;
  575. }
  576. if (length2 === 1) {
  577. dest -= length1;
  578. cursor1 -= length1;
  579. customDest = dest + 1;
  580. customCursor = cursor1 + 1;
  581. for (i = length1 - 1; i >= 0; i--) {
  582. array[customDest + i] = array[customCursor + i];
  583. }
  584. array[dest] = tmp[cursor2];
  585. } else if (length2 === 0) {
  586. throw new Error('mergeHigh preconditions were not respected');
  587. } else {
  588. customCursor = dest - (length2 - 1);
  589. for (i = 0; i < length2; i++) {
  590. array[customCursor + i] = tmp[i];
  591. }
  592. }
  593. };
  594. return TimSort;
  595. })();
  596. function sort(array, compare, lo, hi) {
  597. if (!Array.isArray(array)) {
  598. throw new TypeError('Can only sort arrays');
  599. }
  600. if (!compare) {
  601. compare = alphabeticalCompare;
  602. } else if (typeof compare !== 'function') {
  603. hi = lo;
  604. lo = compare;
  605. compare = alphabeticalCompare;
  606. }
  607. if (!lo) {
  608. lo = 0;
  609. }
  610. if (!hi) {
  611. hi = array.length;
  612. }
  613. var remaining = hi - lo;
  614. if (remaining < 2) {
  615. return;
  616. }
  617. var runLength = 0;
  618. if (remaining < DEFAULT_MIN_MERGE) {
  619. runLength = makeAscendingRun(array, lo, hi, compare);
  620. binaryInsertionSort(array, lo, hi, lo + runLength, compare);
  621. return;
  622. }
  623. var ts = new TimSort(array, compare);
  624. var minRun = minRunLength(remaining);
  625. do {
  626. runLength = makeAscendingRun(array, lo, hi, compare);
  627. if (runLength < minRun) {
  628. var force = remaining;
  629. if (force > minRun) {
  630. force = minRun;
  631. }
  632. binaryInsertionSort(array, lo, lo + force, lo + runLength, compare);
  633. runLength = force;
  634. }
  635. ts.pushRun(lo, runLength);
  636. ts.mergeRuns();
  637. remaining -= runLength;
  638. lo += runLength;
  639. } while (remaining !== 0);
  640. ts.forceMergeRuns();
  641. }
  642. });