|
7 | 7 | /* js/src/merge/binarymerge.js */
|
8 | 8 |
|
9 | 9 |
|
10 |
| -/** |
11 |
| - * Merges 2 arrays using the Hwang Lin algorithm. |
12 |
| - * |
13 |
| - * /!\ aj - ai >= bj - bi |
14 |
| - */ |
| 10 | +var __binarymerge__ = function ( binarysearch , copy ) { |
15 | 11 |
|
16 |
| -var __binarymerge__ = function ( binarysearch, copy ) { |
| 12 | + /** |
| 13 | + * Merges 2 arrays using the Hwang Lin algorithm. |
| 14 | + * |
| 15 | + * /!\ aj - ai >= bj - bi |
| 16 | + */ |
17 | 17 |
|
18 |
| - var hwanglin = function ( compare, a, ai, aj, b, bi, bj, c, ci ) { |
| 18 | + var hwanglin = function ( compare , a , ai , aj , b , bi , bj , c , ci ) { |
19 | 19 |
|
20 |
| - var o, t, x, q, d, z; |
| 20 | + var m , n , o , t , g , q , d , z ; |
21 | 21 |
|
22 |
| - o = ci - ai - bi; |
23 |
| - t = ai; |
| 22 | + o = ci - ai - bi ; |
| 23 | + t = ai ; |
24 | 24 |
|
25 |
| - x = Math.pow( 2, Math.floor( Math.log( ( aj - ai ) / ( bj - bi ) ) / Math.log( 2 ) ) ); |
| 25 | + m = aj - ai ; |
| 26 | + n = bj - ai ; |
26 | 27 |
|
27 |
| - while ( bi < bj && ( ai + x < aj || ( x = aj - ai - 1 ) ) ) { |
| 28 | + // g is the size of a block |
28 | 29 |
|
29 |
| - t = ai; |
30 |
| - ai = t + x; |
| 30 | + g = Math.pow( 2 , Math.floor( Math.log( m / n ) / Math.log( 2 ) ) ) ; |
| 31 | + |
| 32 | + blocks : while ( bi < bj && ( ai + g < aj || ( g = aj - ai - 1 ) ) ) { |
| 33 | + |
| 34 | + // for each block |
| 35 | + |
| 36 | + // t is the inner left bound |
| 37 | + t = ai ; |
| 38 | + |
| 39 | + // ai is the inner right bound |
| 40 | + ai = t + g ; |
31 | 41 |
|
32 | 42 | while ( bi < bj ) {
|
33 | 43 |
|
34 |
| - if ( compare( b[bi], a[ai] ) >= 0 ) { |
35 |
| - copy( a, t, ai, c, o + t + bi ); |
36 |
| - break; |
| 44 | + // we will try to insert b_i in this block |
| 45 | + |
| 46 | + if ( compare( b[bi] , a[ai] ) >= 0 ) { |
| 47 | + |
| 48 | + // b_i must be inserted to the left of this block |
| 49 | + // we copy the block to the output array and continue |
| 50 | + // with the next block |
| 51 | + copy( a , t , ai , c , o + t + bi ) ; |
| 52 | + continue blocks ; |
| 53 | + |
37 | 54 | }
|
38 | 55 |
|
39 |
| - q = binarysearch( compare, a, t, ai, b[bi] ); |
40 |
| - z = q[0] + q[1]; |
| 56 | + // b_i is inside this block and smaller than a_i |
| 57 | + // we search its insertion position using a binary search algorithm |
| 58 | + q = binarysearch( compare , a , t , ai , b[bi] ) ; |
| 59 | + |
| 60 | + // z is the insertion position |
| 61 | + z = q[0] + q[1] ; |
| 62 | + |
| 63 | + // copy everything to the left of the insertion position |
| 64 | + // to the output array |
| 65 | + copy( a , t , z , c , o + t + bi ) ; |
| 66 | + |
| 67 | + // copy b_i to its insertion position in the output array |
| 68 | + c[o + bi + z] = b[bi] ; |
| 69 | + |
| 70 | + // update the inner left bound of the block |
| 71 | + t = z ; |
| 72 | + |
| 73 | + // go to the next b_i |
| 74 | + ++bi ; |
41 | 75 |
|
42 |
| - copy( a, t, z, c, o + t + bi ); |
43 |
| - c[o + z + bi] = b[bi]; |
44 |
| - t = z; |
45 |
| - ++bi; |
46 | 76 | }
|
| 77 | + |
47 | 78 | }
|
48 | 79 |
|
49 |
| - copy( a, t, aj, c, o + t + bi ); |
50 |
| - copy( b, bi, bj, c, o + aj + bi ); |
| 80 | + // remaining elements are copied to the output array |
| 81 | + copy( a , t , aj , c , o + t + bi ) ; |
| 82 | + copy( b , bi , bj , c , o + aj + bi ) ; |
51 | 83 |
|
52 |
| - }; |
| 84 | + } ; |
53 | 85 |
|
54 |
| - return hwanglin; |
| 86 | + return hwanglin ; |
55 | 87 |
|
56 |
| -}; |
| 88 | +} ; |
57 | 89 |
|
58 |
| -exports.__binarymerge__ = __binarymerge__; |
| 90 | +exports.__binarymerge__ = __binarymerge__ ; |
59 | 91 |
|
60 | 92 | /* js/src/merge/merge.js */
|
61 | 93 |
|
|
0 commit comments