Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace mergeSort with Array.prototype.sort #355

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
54 changes: 28 additions & 26 deletions lib/scope.js
Expand Up @@ -46,7 +46,6 @@
import {
defaults,
keep_name,
mergeSort,
push_uniq,
make_node,
return_false,
Expand Down Expand Up @@ -918,35 +917,29 @@ AST_Toplevel.DEFMETHOD("compute_char_frequency", function(options) {
}
});

const base54 = (() => {
const leading = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_".split("");
const digits = "0123456789".split("");
let chars;
let frequency;
function reset() {
frequency = new Map();
leading.forEach(function(ch) {
frequency.set(ch, 0);
});
digits.forEach(function(ch) {
frequency.set(ch, 0);
});
}
base54.consider = function(str, delta) {
for (var i = str.length; --i >= 0;) {
const base54 = (function() {
const freq = new Map();

const digits = init("0123456789");
const leading = init("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sort method only used for two arrays above which have 10 and 54 elements

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the threshold between insert sort (stable) and quick sort (unstable) is 10 elements. So digits would always use insertion sort, but leading wouldn't. 😕

let chars, frequency = new Map(freq);

base54.consider = (str, delta) => {
for (let i = str.length; --i >= 0;) {
frequency.set(str[i], frequency.get(str[i]) + delta);
}
};
function compare(a, b) {
return frequency.get(b) - frequency.get(a);
}
base54.sort = function() {
chars = mergeSort(leading, compare).concat(mergeSort(digits, compare));
base54.sort = () => {
chars = leading.sort(compare).concat(digits.sort(compare));
};
base54.reset = reset;
reset();
base54.reset = () => {
frequency = new Map(freq);
};

return base54;

function base54(num) {
var ret = "", base = 54;
let ret = "", base = 54;
num++;
do {
num--;
Expand All @@ -956,7 +949,16 @@ const base54 = (() => {
} while (num > 0);
return ret;
}
return base54;

function init(chars) {
const array = chars.split("");
array.forEach((ch, i) => freq.set(ch, -1e-2 * i));
return array;
}

function compare(a, b) {
return frequency.get(b) - frequency.get(a);
}
})();

export {
Expand Down
25 changes: 0 additions & 25 deletions lib/utils/index.js
Expand Up @@ -169,30 +169,6 @@ function remove(array, el) {
}
}

function mergeSort(array, cmp) {
if (array.length < 2) return array.slice();
function merge(a, b) {
var r = [], ai = 0, bi = 0, i = 0;
while (ai < a.length && bi < b.length) {
cmp(a[ai], b[bi]) <= 0
? r[i++] = a[ai++]
: r[i++] = b[bi++];
}
if (ai < a.length) r.push.apply(r, a.slice(ai));
if (bi < b.length) r.push.apply(r, b.slice(bi));
return r;
}
function _ms(a) {
if (a.length <= 1)
return a;
var m = Math.floor(a.length / 2), left = a.slice(0, m), right = a.slice(m);
left = _ms(left);
right = _ms(right);
return merge(left, right);
}
return _ms(array);
}

function makePredicate(words) {
if (!Array.isArray(words)) words = words.split(" ");

Expand Down Expand Up @@ -286,7 +262,6 @@ export {
map_to_object,
MAP,
member,
mergeSort,
noop,
push_uniq,
regexp_source_fix,
Expand Down