1
1
import binarySearch from 'binary-search' ;
2
2
import { numberSortAscending } from 'num-sort' ;
3
3
4
+ /**
5
+ * @typedef {import("../types").Tree } Tree
6
+ * @typedef {import("../types").CreateTreeOptions } CreateTreeOptions
7
+ * @typedef {import("../types").Spectrum } Spectrum
8
+ */
9
+
4
10
/**
5
11
* Function that creates the tree
6
- * @param {Array<Array<number>> } spectrum
7
- * @param {object } [options]
8
- * @return {Tree|null }
9
- * left and right have the same structure than the parent,
10
- * or are null if they are leaves
12
+ * @param {Spectrum } spectrum
13
+ * @param {CreateTreeOptions } [options]
14
+ * @return { Tree | null }
11
15
*/
12
16
export function createTree ( spectrum , options = { } ) {
13
- const X = spectrum [ 0 ] ;
17
+ const { x , y } = spectrum ;
14
18
const {
15
19
minWindow = 0.16 ,
16
20
threshold = 0.01 ,
17
- from = X [ 0 ] ,
18
- to = X [ X . length - 1 ] ,
21
+ from = x [ 0 ] ,
22
+ to = x [ x . length - 1 ] ,
19
23
} = options ;
20
24
21
- return mainCreateTree (
22
- spectrum [ 0 ] ,
23
- spectrum [ 1 ] ,
24
- from ,
25
- to ,
26
- minWindow ,
27
- threshold ,
28
- ) ;
25
+ return mainCreateTree ( x , y , from , to , minWindow , threshold ) ;
29
26
}
30
27
31
- function mainCreateTree ( X , Y , from , to , minWindow , threshold ) {
28
+ function mainCreateTree ( x , y , from , to , minWindow , threshold ) {
32
29
if ( to - from < minWindow ) {
33
30
return null ;
34
31
}
35
32
36
33
// search first point
37
- let start = binarySearch ( X , from , numberSortAscending ) ;
34
+ let start = binarySearch ( x , from , numberSortAscending ) ;
38
35
if ( start < 0 ) {
39
36
start = ~ start ;
40
37
}
41
38
42
39
// stop at last point
43
40
let sum = 0 ;
44
41
let center = 0 ;
45
- for ( let i = start ; i < X . length ; i ++ ) {
46
- if ( X [ i ] >= to ) {
42
+ for ( let i = start ; i < x . length ; i ++ ) {
43
+ if ( x [ i ] >= to ) {
47
44
break ;
48
45
}
49
- sum += Y [ i ] ;
50
- center += X [ i ] * Y [ i ] ;
46
+ sum += y [ i ] ;
47
+ center += x [ i ] * y [ i ] ;
51
48
}
52
49
53
50
if ( sum < threshold ) {
@@ -59,24 +56,15 @@ function mainCreateTree(X, Y, from, to, minWindow, threshold) {
59
56
return null ;
60
57
}
61
58
if ( center - from < minWindow / 4 ) {
62
- return mainCreateTree ( X , Y , center , to , minWindow , threshold ) ;
59
+ return mainCreateTree ( x , y , center , to , minWindow , threshold ) ;
63
60
} else if ( to - center < minWindow / 4 ) {
64
- return mainCreateTree ( X , Y , from , center , minWindow , threshold ) ;
61
+ return mainCreateTree ( x , y , from , center , minWindow , threshold ) ;
65
62
} else {
66
- return new Tree (
63
+ return {
67
64
sum,
68
65
center,
69
- mainCreateTree ( X , Y , from , center , minWindow , threshold ) ,
70
- mainCreateTree ( X , Y , center , to , minWindow , threshold ) ,
71
- ) ;
72
- }
73
- }
74
-
75
- class Tree {
76
- constructor ( sum , center , left , right ) {
77
- this . sum = sum ;
78
- this . center = center ;
79
- this . left = left ;
80
- this . right = right ;
66
+ left : mainCreateTree ( x , y , from , center , minWindow , threshold ) ,
67
+ right : mainCreateTree ( x , y , center , to , minWindow , threshold ) ,
68
+ } ;
81
69
}
82
70
}
0 commit comments