11import binarySearch from 'binary-search' ;
22import { numberSortAscending } from 'num-sort' ;
33
4+ /**
5+ * @typedef {import("../types").Tree } Tree
6+ * @typedef {import("../types").CreateTreeOptions } CreateTreeOptions
7+ * @typedef {import("../types").Spectrum } Spectrum
8+ */
9+
410/**
511 * 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 }
1115 */
1216export function createTree ( spectrum , options = { } ) {
13- const X = spectrum [ 0 ] ;
17+ const { x , y } = spectrum ;
1418 const {
1519 minWindow = 0.16 ,
1620 threshold = 0.01 ,
17- from = X [ 0 ] ,
18- to = X [ X . length - 1 ] ,
21+ from = x [ 0 ] ,
22+ to = x [ x . length - 1 ] ,
1923 } = options ;
2024
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 ) ;
2926}
3027
31- function mainCreateTree ( X , Y , from , to , minWindow , threshold ) {
28+ function mainCreateTree ( x , y , from , to , minWindow , threshold ) {
3229 if ( to - from < minWindow ) {
3330 return null ;
3431 }
3532
3633 // search first point
37- let start = binarySearch ( X , from , numberSortAscending ) ;
34+ let start = binarySearch ( x , from , numberSortAscending ) ;
3835 if ( start < 0 ) {
3936 start = ~ start ;
4037 }
4138
4239 // stop at last point
4340 let sum = 0 ;
4441 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 ) {
4744 break ;
4845 }
49- sum += Y [ i ] ;
50- center += X [ i ] * Y [ i ] ;
46+ sum += y [ i ] ;
47+ center += x [ i ] * y [ i ] ;
5148 }
5249
5350 if ( sum < threshold ) {
@@ -59,24 +56,15 @@ function mainCreateTree(X, Y, from, to, minWindow, threshold) {
5956 return null ;
6057 }
6158 if ( center - from < minWindow / 4 ) {
62- return mainCreateTree ( X , Y , center , to , minWindow , threshold ) ;
59+ return mainCreateTree ( x , y , center , to , minWindow , threshold ) ;
6360 } else if ( to - center < minWindow / 4 ) {
64- return mainCreateTree ( X , Y , from , center , minWindow , threshold ) ;
61+ return mainCreateTree ( x , y , from , center , minWindow , threshold ) ;
6562 } else {
66- return new Tree (
63+ return {
6764 sum,
6865 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+ } ;
8169 }
8270}
0 commit comments