|
| 1 | +/** |
| 2 | +* @license Apache-2.0 |
| 3 | +* |
| 4 | +* Copyright (c) 2025 The Stdlib Authors. |
| 5 | +* |
| 6 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +* you may not use this file except in compliance with the License. |
| 8 | +* You may obtain a copy of the License at |
| 9 | +* |
| 10 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +* |
| 12 | +* Unless required by applicable law or agreed to in writing, software |
| 13 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +* See the License for the specific language governing permissions and |
| 16 | +* limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +'use strict'; |
| 20 | + |
| 21 | +// MODULES // |
| 22 | + |
| 23 | +var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' ); |
| 24 | +var isnan = require( '@stdlib/math/base/assert/is-nan' ); |
| 25 | +var floor = require( '@stdlib/math/base/special/floor' ); |
| 26 | + |
| 27 | + |
| 28 | +// MAIN // |
| 29 | + |
| 30 | +/** |
| 31 | +* Simultaneously sorts two double-precision floating-point strided arrays based on the sort order of the first array using heapsort. |
| 32 | +* |
| 33 | +* ## Notes |
| 34 | +* |
| 35 | +* - This implementation uses an in-place algorithm derived from the work of Floyd (1964). |
| 36 | +* |
| 37 | +* ## References |
| 38 | +* |
| 39 | +* - Williams, John William Joseph. 1964. "Algorithm 232: Heapsort." _Communications of the ACM_ 7 (6). New York, NY, USA: Association for Computing Machinery: 347–49. doi:[10.1145/512274.512284](https://doi.org/10.1145/512274.512284). |
| 40 | +* - Floyd, Robert W. 1964. "Algorithm 245: Treesort." _Communications of the ACM_ 7 (12). New York, NY, USA: Association for Computing Machinery: 701. doi:[10.1145/355588.365103](https://doi.org/10.1145/355588.365103). |
| 41 | +* |
| 42 | +* @private |
| 43 | +* @param {PositiveInteger} N - number of indexed elements |
| 44 | +* @param {number} order - sort order |
| 45 | +* @param {Object} x - input array object |
| 46 | +* @param {Collection} x.data - input array data |
| 47 | +* @param {Array<Function>} x.accessors - array element accessors |
| 48 | +* @param {integer} strideX - stride length for `x` |
| 49 | +* @param {NonNegativeInteger} offsetX - starting index for `x` |
| 50 | +* @param {Object} y - second input array object |
| 51 | +* @param {Collection} y.data - second input array data |
| 52 | +* @param {Array<Function>} y.accessors - array element accessors |
| 53 | +* @param {integer} strideY - stride length for `y` |
| 54 | +* @param {NonNegativeInteger} offsetY - starting index for `y` |
| 55 | +* @returns {Object} `x` |
| 56 | +* |
| 57 | +* @example |
| 58 | +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); |
| 59 | +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); |
| 60 | +* |
| 61 | +* var x = [ 1.0, -2.0, 3.0, -4.0 ]; |
| 62 | +* var y = [ 0.0, 1.0, 2.0, 3.0 ]; |
| 63 | +* |
| 64 | +* gsort2hp( x.length, 1.0, arraylike2object( toAccessorArray( x ) ), 1, 0, arraylike2object( toAccessorArray( y ) ), 1, 0 ); |
| 65 | +* |
| 66 | +* console.log( x ); |
| 67 | +* // => [ -4.0, -2.0, 1.0, 3.0 ] |
| 68 | +* |
| 69 | +* console.log( y ); |
| 70 | +* // => [ 3.0, 1.0, 0.0, 2.0 ] |
| 71 | +*/ |
| 72 | +function gsort2hp( N, order, x, strideX, offsetX, y, strideY, offsetY ) { |
| 73 | + var parent; |
| 74 | + var child; |
| 75 | + var xbuf; |
| 76 | + var ybuf; |
| 77 | + var xget; |
| 78 | + var yget; |
| 79 | + var xset; |
| 80 | + var yset; |
| 81 | + var v1; |
| 82 | + var v2; |
| 83 | + var tx; |
| 84 | + var ty; |
| 85 | + var ix; |
| 86 | + var iy; |
| 87 | + var n; |
| 88 | + var j; |
| 89 | + var k; |
| 90 | + |
| 91 | + // Cache reference to array data: |
| 92 | + xbuf = x.data; |
| 93 | + ybuf = y.data; |
| 94 | + |
| 95 | + // Cache reference to the element accessors: |
| 96 | + xget = x.accessors[ 0 ]; |
| 97 | + xset = x.accessors[ 1 ]; |
| 98 | + yget = y.accessors[ 0 ]; |
| 99 | + yset = y.accessors[ 1 ]; |
| 100 | + |
| 101 | + // For a positive stride, sorting in decreasing order is equivalent to providing a negative stride and sorting in increasing order, and, for a negative stride, sorting in decreasing order is equivalent to providing a positive stride and sorting in increasing order... |
| 102 | + if ( order < 0.0 ) { |
| 103 | + strideX *= -1; |
| 104 | + strideY *= -1; |
| 105 | + offsetX -= (N-1) * strideX; |
| 106 | + offsetY -= (N-1) * strideY; |
| 107 | + } |
| 108 | + // Set the initial heap size: |
| 109 | + n = N; |
| 110 | + |
| 111 | + // Specify an initial "parent" index for building the heap: |
| 112 | + parent = floor( N / 2 ); |
| 113 | + |
| 114 | + // Continue looping until the array is sorted... |
| 115 | + while ( true ) { |
| 116 | + if ( parent > 0 ) { |
| 117 | + // We need to build the heap... |
| 118 | + parent -= 1; |
| 119 | + tx = xget( xbuf, offsetX+(parent*strideX) ); |
| 120 | + ty = yget( ybuf, offsetY+(parent*strideY) ); |
| 121 | + } else { |
| 122 | + // Reduce the heap size: |
| 123 | + n -= 1; |
| 124 | + |
| 125 | + // Check if the heap is empty, and, if so, we are finished sorting... |
| 126 | + if ( n === 0 ) { |
| 127 | + return x; |
| 128 | + } |
| 129 | + // Store the last heap value in a temporary variable in order to make room for the heap root being placed into its sorted position: |
| 130 | + ix = offsetX + (n*strideX); |
| 131 | + tx = xget( xbuf, ix ); |
| 132 | + iy = offsetY + (n*strideY); |
| 133 | + ty = yget( ybuf, iy ); |
| 134 | + |
| 135 | + // Move the heap root to its sorted position: |
| 136 | + xset( xbuf, ix, xget( xbuf, offsetX ) ); |
| 137 | + yset( ybuf, iy, yget( ybuf, offsetY ) ); |
| 138 | + } |
| 139 | + // We need to "sift down", pushing `t` down the heap to in order to replace the parent and satisfy the heap property... |
| 140 | + |
| 141 | + // Start at the parent index: |
| 142 | + j = parent; |
| 143 | + |
| 144 | + // Get the "left" child index: |
| 145 | + child = (j*2) + 1; |
| 146 | + |
| 147 | + while ( child < n ) { |
| 148 | + // Find the largest child... |
| 149 | + k = child + 1; |
| 150 | + if ( k < n ) { |
| 151 | + v1 = xget( xbuf, offsetX+(k*strideX) ); |
| 152 | + v2 = xget( xbuf, offsetX+(child*strideX) ); |
| 153 | + |
| 154 | + // Check if a "right" child exists and is "bigger"... |
| 155 | + if ( v1 > v2 || isnan( v1 ) || (v1 === v2 && isPositiveZero( v1 ) ) ) { // eslint-disable-line max-len |
| 156 | + child += 1; |
| 157 | + } |
| 158 | + } |
| 159 | + // Check if the largest child is bigger than `t`... |
| 160 | + v1 = xget( xbuf, offsetX+(child*strideX) ); |
| 161 | + if ( v1 > tx || isnan( v1 ) || ( v1 === tx && isPositiveZero( v1 ) ) ) { // eslint-disable-line max-len |
| 162 | + // Insert the larger child value: |
| 163 | + xset( xbuf, offsetX+(j*strideX), v1 ); |
| 164 | + yset( ybuf, offsetY+(j*strideY), yget( ybuf, offsetY+(child*strideY) ) ); // eslint-disable-line max-len |
| 165 | + |
| 166 | + // Update `j` to point to the child index: |
| 167 | + j = child; |
| 168 | + |
| 169 | + // Get the "left" child index and repeat... |
| 170 | + child = (j*2) + 1; |
| 171 | + } else { |
| 172 | + // We've found `t`'s place in the heap... |
| 173 | + break; |
| 174 | + } |
| 175 | + } |
| 176 | + // Insert `t` into the heap: |
| 177 | + xset( xbuf, offsetX+(j*strideX), tx ); |
| 178 | + yset( ybuf, offsetY+(j*strideY), ty ); |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | + |
| 183 | +// EXPORTS // |
| 184 | + |
| 185 | +module.exports = gsort2hp; |
0 commit comments