Skip to content

Commit f7aec06

Browse files
👕 refactor: Initial lint.
1 parent 12a7b8b commit f7aec06

18 files changed

+296
-402
lines changed

doc/scripts/header.js

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,31 @@
1-
var domReady = function(callback) {
2-
var state = document.readyState ;
3-
if ( state === 'interactive' || state === 'complete' ) {
4-
callback() ;
5-
}
6-
else {
1+
const domReady = function (callback) {
2+
const state = document.readyState;
3+
if (state === 'interactive' || state === 'complete') {
4+
callback();
5+
} else {
76
document.addEventListener('DOMContentLoaded', callback);
87
}
9-
} ;
10-
8+
};
119

12-
domReady(function(){
13-
14-
var projectname = document.createElement('a');
10+
domReady(() => {
11+
const projectname = document.createElement('a');
1512
projectname.classList.add('project-name');
1613
projectname.text = 'data-structures-and-algorithms/heapq';
17-
projectname.href = './index.html' ;
14+
projectname.href = './index.html';
1815

19-
var header = document.getElementsByTagName('header')[0] ;
20-
header.insertBefore(projectname,header.firstChild);
16+
const header = document.querySelectorAll('header')[0];
17+
header.insertBefore(projectname, header.firstChild);
2118

22-
var testlink = document.querySelector('header > a[data-ice="testLink"]') ;
23-
testlink.href = 'https://coveralls.io/github/data-structures-and-algorithms/heapq' ;
24-
testlink.target = '_BLANK' ;
19+
const testlink = document.querySelector('header > a[data-ice="testLink"]');
20+
testlink.href =
21+
'https://coveralls.io/github/data-structures-and-algorithms/heapq';
22+
testlink.target = '_BLANK';
2523

26-
var searchBox = document.querySelector('.search-box');
27-
var input = document.querySelector('.search-input');
24+
const searchBox = document.querySelector('.search-box');
25+
const input = document.querySelector('.search-input');
2826

29-
// active search box when focus on searchBox.
30-
input.addEventListener('focus', function(){
27+
// Active search box when focus on searchBox.
28+
input.addEventListener('focus', () => {
3129
searchBox.classList.add('active');
3230
});
33-
3431
});

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@
198198
"unicorn"
199199
],
200200
"rules": {
201+
"unicorn/prefer-node-protocol": "off",
201202
"unicorn/filename-case": "off",
202203
"camelcase": "off",
203204
"unicorn/prevent-abbreviations": "off",

src/core/Heap.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
2-
export default function Heap ( compare , data ) {
3-
4-
this.compare = compare ;
5-
this.data = data ;
6-
1+
export default function Heap(compare, data) {
2+
this.compare = compare;
3+
this.data = data;
74
}

src/core/index.js

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,7 @@
1-
import Heap from './Heap.js' ;
2-
import keeporder from './keeporder.js' ;
3-
import nextchild from './nextchild.js' ;
4-
import siftdown from './siftdown.js' ;
5-
import siftup from './siftup.js' ;
1+
import Heap from './Heap.js';
2+
import keeporder from './keeporder.js';
3+
import nextchild from './nextchild.js';
4+
import siftdown from './siftdown.js';
5+
import siftup from './siftup.js';
66

7-
export default {
8-
Heap ,
9-
keeporder ,
10-
nextchild ,
11-
siftdown ,
12-
siftup ,
13-
} ;
14-
15-
export {
16-
Heap ,
17-
keeporder ,
18-
nextchild ,
19-
siftdown ,
20-
siftup ,
21-
} ;
7+
export {Heap, keeporder, nextchild, siftdown, siftup};

src/core/keeporder.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { increasing } from '@total-order/primitive' ;
1+
import {increasing} from '@total-order/primitive';
22

3-
const keeporder = compare => ( a , b ) => compare( a[0] , b[0] ) || increasing( a[1] , b[1] ) ;
3+
const keeporder = (compare) => (a, b) =>
4+
compare(a[0], b[0]) || increasing(a[1], b[1]);
45

5-
export default keeporder ;
6+
export default keeporder;

src/core/nextchild.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
import assert from 'assert';
22

33
/**
44
* Computes which child is the smallest according
@@ -8,16 +8,15 @@
88
*
99
* @param {function} compare the comparison function
1010
* @param {array} a the array where the heap is stored
11-
* @param {int} i is the first child
12-
* @param {int} j - 1 is the last leaf
11+
* @param {number} i is the first child
12+
* @param {number} j - 1 is the last leaf
13+
*
14+
* @return {number}
1315
*/
16+
export default function nextchild(compare, a, i, j) {
17+
assert(i < j);
18+
if (j - i === 1) return i;
1419

15-
export default function nextchild ( compare , a , i , j ) {
16-
17-
if ( j - i < 2 ) return i ;
18-
19-
if ( compare( a[i] , a[i+1] ) <= 0 ) return i ;
20-
21-
return i + 1 ;
22-
20+
assert(j - i >= 2);
21+
return compare(a[i], a[i + 1]) <= 0 ? i : i + 1;
2322
}

src/core/siftdown.js

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,45 @@
1-
import nextchild from './nextchild.js' ;
1+
import nextchild from './nextchild.js';
22

33
/**
44
* Sifts down a node.
55
*
66
* @param {function} compare the comparison function
77
* @param {array} a the array where the heap is stored
8-
* @param {int} i is the root element
9-
* @param {int} j - 1 is the last leaf
10-
* @param {int} k is the target node
8+
* @param {number} i is the root element
9+
* @param {number} j - 1 is the last leaf
10+
* @param {number} k is the target node
1111
*/
1212

13-
export default function siftdown ( compare, a, i, j, k ) {
14-
13+
export default function siftdown(compare, a, i, j, k) {
1514
let current = k - i;
1615

17-
while ( true ) {
18-
19-
// address of the first child in a zero-based
16+
while (true) {
17+
// Address of the first child in a zero-based
2018
// binary heap
2119

2220
const firstchild = 2 * current + 1;
2321

24-
// if current node has no children
22+
// If current node has no children
2523
// then we are done
2624

27-
if ( firstchild >= j - i ) break ;
25+
if (firstchild >= j - i) break;
2826

29-
// if current value is smaller than its smallest
27+
// If current value is smaller than its smallest
3028
// child then we are done
3129

32-
const candidate = nextchild( compare, a, i + firstchild, j );
30+
const candidate = nextchild(compare, a, i + firstchild, j);
3331

34-
if ( compare( a[i + current], a[candidate] ) <= 0 ) break ;
32+
if (compare(a[i + current], a[candidate]) <= 0) break;
3533

36-
// otherwise
34+
// Otherwise
3735
// swap with smallest child
3836

39-
const tmp = a[i+current] ;
40-
a[i+current] = a[candidate] ;
41-
a[candidate] = tmp ;
37+
const tmp = a[i + current];
38+
a[i + current] = a[candidate];
39+
a[candidate] = tmp;
4240

4341
current = candidate - i;
44-
4542
}
4643

4744
return i + current;
48-
4945
}

src/core/siftup.js

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,38 @@
1-
21
/**
32
* Sifts up a node.
43
*
54
* @param {function} compare the comparison function
65
* @param {array} a the array where the heap is stored
7-
* @param {int} i is the root element
8-
* @param {int} j - 1 is the last leaf
9-
* @param {int} k is the target node
6+
* @param {number} i is the root element
7+
* @param {number} j - 1 is the last leaf
8+
* @param {number} k is the target node
109
*/
1110

12-
export default function siftup ( compare , a , i , j , k ) {
13-
14-
let current = k - i ;
15-
16-
// while we are not the root
11+
export default function siftup(compare, a, i, j, k) {
12+
let current = k - i;
1713

18-
while ( current !== 0 ) {
14+
// While we are not the root
1915

20-
// address of the parent in a zero-based
16+
while (current !== 0) {
17+
// Address of the parent in a zero-based
2118
// d-ary heap
2219

23-
const parent = i + ( ( current - 1 ) >>> 1 ) ;
20+
const parent = i + ((current - 1) >>> 1);
2421

25-
// if current value is greater than its parent
22+
// If current value is greater than its parent
2623
// then we are done
2724

28-
if ( compare( a[i + current], a[parent] ) >= 0 ) return i + current ;
25+
if (compare(a[i + current], a[parent]) >= 0) return i + current;
2926

30-
// otherwise
27+
// Otherwise
3128
// swap with parent
3229

33-
const tmp = a[i+current] ;
34-
a[i+current] = a[parent] ;
35-
a[parent] = tmp ;
36-
37-
current = parent - i ;
30+
const tmp = a[i + current];
31+
a[i + current] = a[parent];
32+
a[parent] = tmp;
3833

34+
current = parent - i;
3935
}
4036

41-
return i + current ;
42-
37+
return i + current;
4338
}

src/heapify.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { siftdown , Heap } from './core/index.js' ;
1+
import {siftdown, Heap} from './core/index.js';
22

33
/**
44
* Builds a heap in O(n) operations.
@@ -8,16 +8,12 @@ import { siftdown , Heap } from './core/index.js' ;
88
*
99
*/
1010

11-
export default function heapify ( compare , x ) {
12-
13-
const n = x.length ;
14-
15-
for ( let k = n / 2 | 0 ; k ; ) {
16-
17-
siftdown( compare , x , 0 , n , --k ) ;
11+
export default function heapify(compare, x) {
12+
const n = x.length;
1813

14+
for (let k = (n / 2) | 0; k; ) {
15+
siftdown(compare, x, 0, n, --k);
1916
}
2017

21-
return new Heap( compare , x ) ;
22-
18+
return new Heap(compare, x);
2319
}

src/heappop.js

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
1-
import { IndexError } from '@failure-abstraction/error' ;
2-
import { siftdown } from './core/index.js' ;
1+
import {IndexError} from '@failure-abstraction/error';
2+
import {siftdown} from './core/index.js';
33

4-
export default function heappop ( heap ) {
4+
export default function heappop(heap) {
5+
const x = heap.data;
6+
const n = x.length;
57

6-
const x = heap.data ;
7-
const n = x.length ;
8+
if (n === 0) throw new IndexError('heappop');
89

9-
if ( n === 0 ) throw new IndexError( "heappop" ) ;
10+
const last = n - 1;
1011

11-
const last = n - 1 ;
12+
// Swap last leaf and root
1213

13-
// swap last leaf and root
14+
const tmp = x[0];
15+
x[0] = x[last];
16+
x[last] = tmp;
1417

15-
const tmp = x[0] ;
16-
x[0] = x[last] ;
17-
x[last] = tmp ;
18+
// Sift down the new root
1819

19-
// sift down the new root
20+
siftdown(heap.compare, x, 0, last, 0);
2021

21-
siftdown( heap.compare , x , 0 , last , 0 ) ;
22-
23-
// return old root
24-
25-
return x.pop( ) ;
22+
// Return old root
2623

24+
return x.pop();
2725
}

src/heappush.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
import { siftup } from './core/index.js' ;
1+
import {siftup} from './core/index.js';
22

3-
export default function heappush ( heap , item ) {
3+
export default function heappush(heap, item) {
4+
const x = heap.data;
5+
const n = x.length;
46

5-
const x = heap.data ;
6-
const n = x.length ;
7+
x.push(item);
78

8-
x.push( item ) ;
9-
10-
// sift up the new leaf
11-
12-
siftup( heap.compare , x , 0 , n + 1 , n ) ;
9+
// Sift up the new leaf
1310

11+
siftup(heap.compare, x, 0, n + 1, n);
1412
}

src/heappushpop.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
1-
import { siftdown } from './core/index.js' ;
1+
import {siftdown} from './core/index.js';
22

3-
export default function heappushpop ( heap , item ) {
3+
export default function heappushpop(heap, item) {
4+
const x = heap.data;
5+
const n = x.length;
46

5-
const x = heap.data ;
6-
const n = x.length ;
7+
if (n === 0) return item;
78

8-
if ( n === 0 ) return item ;
9+
const compare = heap.compare;
910

10-
const compare = heap.compare ;
11+
if (compare(item, x[0]) <= 0) return item;
1112

12-
if ( compare( item , x[0] ) <= 0 ) return item ;
13+
const smallest = x[0];
1314

14-
const smallest = x[0] ;
15+
x[0] = item;
1516

16-
x[0] = item ;
17+
// Sift down the new root
1718

18-
// sift down the new root
19-
20-
siftdown( compare , x , 0 , n , 0 ) ;
21-
22-
return smallest ;
19+
siftdown(compare, x, 0, n, 0);
2320

21+
return smallest;
2422
}

0 commit comments

Comments
 (0)