Skip to content

Commit

Permalink
refactor(heaps): update destructuring
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Nov 14, 2020
1 parent d944b54 commit 43e7ab7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
5 changes: 2 additions & 3 deletions packages/heaps/src/dheap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,11 @@ export class DHeap<T>

protected percolateUp(i: number, vals = this.values) {
const node = vals[i];
const d = this.d;
const cmp = this.compare;
const { d, compare } = this;
while (i > 0) {
const pi = ((i - 1) / d) | 0;
const parent = vals[pi];
if (cmp(node, parent) >= 0) {
if (compare(node, parent) >= 0) {
break;
}
vals[pi] = node;
Expand Down
24 changes: 11 additions & 13 deletions packages/heaps/src/heap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,17 +152,16 @@ export class Heap<T>
* @param n - number of values
*/
max(n = this.values.length) {
const vals = this.values;
const cmp = this.compare;
const res = vals.slice(0, n);
const { compare, values } = this;
const res = values.slice(0, n);
if (!n) {
return res;
}
this.heapify(res);
for (let m = vals.length; n < m; n++) {
this.pushPop(vals[n], res);
for (let m = values.length; n < m; n++) {
this.pushPop(values[n], res);
}
return res.sort((a, b) => cmp(b, a));
return res.sort((a, b) => compare(b, a));
}

/**
Expand All @@ -172,18 +171,17 @@ export class Heap<T>
* @param n - number of values
*/
min(n = this.values.length) {
const vals = this.values;
const cmp = this.compare;
const res = vals.slice(0, n).sort(cmp);
const { compare, values } = this;
const res = values.slice(0, n).sort(compare);
if (!n) {
return res;
}
let x = res[n - 1],
y: T;
for (let i = n, m = vals.length; i < m; i++) {
y = vals[i];
if (cmp(y, x) < 0) {
res.splice(binarySearch(y, res, 0, n, cmp), 0, y);
for (let i = n, m = values.length; i < m; i++) {
y = values[i];
if (compare(y, x) < 0) {
res.splice(binarySearch(y, res, 0, n, compare), 0, y);
res.pop();
x = res[n - 1];
}
Expand Down

0 comments on commit 43e7ab7

Please sign in to comment.