Skip to content

Commit

Permalink
feat: making timsort work with typed arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
prodikos committed Jan 14, 2021
1 parent 39562c8 commit 8637104
Show file tree
Hide file tree
Showing 5 changed files with 3,362 additions and 48 deletions.
76 changes: 34 additions & 42 deletions build/timsort.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
/****
* The MIT License
*
*
* Copyright (c) 2015 Marco Ziccardi
*
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*
****/
(function (global, factory) {
if (typeof define === 'function' && define.amd) {
Expand All @@ -40,11 +40,7 @@
exports.__esModule = true;
exports.sort = sort;

function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError('Cannot call a class as a function');
}
}
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }

var DEFAULT_MIN_MERGE = 32;

Expand Down Expand Up @@ -158,10 +154,10 @@

reverseRun(array, lo, runHi);
} else {
while (runHi < hi && compare(array[runHi], array[runHi - 1]) >= 0) {
runHi++;
while (runHi < hi && compare(array[runHi], array[runHi - 1]) >= 0) {
runHi++;
}
}
}

return runHi - lo;
}
Expand Down Expand Up @@ -244,23 +240,23 @@
lastOffset += hint;
offset += hint;
} else {
maxOffset = hint + 1;
while (offset < maxOffset && compare(value, array[start + hint - offset]) <= 0) {
lastOffset = offset;
offset = (offset << 1) + 1;
maxOffset = hint + 1;
while (offset < maxOffset && compare(value, array[start + hint - offset]) <= 0) {
lastOffset = offset;
offset = (offset << 1) + 1;

if (offset <= 0) {
if (offset <= 0) {
offset = maxOffset;
}
}
if (offset > maxOffset) {
offset = maxOffset;
}
}
if (offset > maxOffset) {
offset = maxOffset;
}

var tmp = lastOffset;
lastOffset = hint - offset;
offset = hint - tmp;
}
var tmp = lastOffset;
lastOffset = hint - offset;
offset = hint - tmp;
}

lastOffset++;
while (lastOffset < offset) {
Expand Down Expand Up @@ -300,25 +296,25 @@
lastOffset = hint - offset;
offset = hint - tmp;
} else {
maxOffset = length - hint;
maxOffset = length - hint;

while (offset < maxOffset && compare(value, array[start + hint + offset]) >= 0) {
lastOffset = offset;
offset = (offset << 1) + 1;
while (offset < maxOffset && compare(value, array[start + hint + offset]) >= 0) {
lastOffset = offset;
offset = (offset << 1) + 1;

if (offset <= 0) {
if (offset <= 0) {
offset = maxOffset;
}
}

if (offset > maxOffset) {
offset = maxOffset;
}
}

if (offset > maxOffset) {
offset = maxOffset;
lastOffset += hint;
offset += hint;
}

lastOffset += hint;
offset += hint;
}

lastOffset++;

while (lastOffset < offset) {
Expand Down Expand Up @@ -754,10 +750,6 @@
})();

function sort(array, compare, lo, hi) {
if (!Array.isArray(array)) {
throw new TypeError('Can only sort arrays');
}

if (!compare) {
compare = alphabeticalCompare;
} else if (typeof compare !== 'function') {
Expand Down
2 changes: 1 addition & 1 deletion build/timsort.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "timsort",
"version": "0.3.0",
"version": "0.3.0-r1",
"author": {
"name": "Marco Ziccardi",
"url": "http://mziccard.me/"
Expand All @@ -20,6 +20,7 @@
"grunt": "^0.4.5",
"grunt-babel": "^5.0.1",
"grunt-banner": "^0.5.0",
"grunt-cli": "^1.3.2",
"grunt-contrib-clean": "^0.6.0",
"grunt-contrib-uglify": "^0.9.1",
"mocha": "~2.2.5"
Expand Down
4 changes: 0 additions & 4 deletions src/timsort.js
Original file line number Diff line number Diff line change
Expand Up @@ -908,10 +908,6 @@ class TimSort {
* comparator.
*/
export function sort(array, compare, lo, hi) {
if (!Array.isArray(array)) {
throw new TypeError('Can only sort arrays');
}

/*
* Handle the case where a comparison function is not provided. We do
* lexicographic sorting
Expand Down
Loading

0 comments on commit 8637104

Please sign in to comment.