Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 9x 9x 9x 2x 2x 2x 2x 2x 2x 2x 2x 6x 6x 6x 2x 2x 2x 2x 2x 2x 2x 2x 2x 9x 9x 3x 3x 9x 3x 3x 3x 9x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 74x 74x 3x 3x 74x 69x 69x 2x 74x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 204x 204x 204x 204x 204x 204x 204x 204x 204x 204x 204x 60x 60x 144x 144x 144x 144x 144x 144x 144x 144x 144x 144x 204x 144x 144x 144x 144x 144x 144x 204x 3x 3x 141x 141x 141x 204x 56x 56x 56x 9x 9x 47x 56x 7x 7x 7x 40x 40x 40x 40x 40x 40x 85x 85x 85x 85x 125x 125x 204x 78x 50x 78x 28x 28x 78x 78x 47x 47x 41x 41x 25x 25x 47x 6x 6x 112x 204x 2x 2x 2x 2x 2x | /** * @license Apache-2.0 * * Copyright (c) 2025 The Stdlib Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ 'use strict'; // MODULES // var hasOwnProp = require( '@stdlib/assert/has-own-property' ); var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; var isString = require( '@stdlib/assert/is-string' ).isPrimitive; var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); var isRealFloatingDataType = require( '@stdlib/ndarray/base/assert/is-real-floating-point-data-type' ); var isSignedIntegerDataType = require( '@stdlib/ndarray/base/assert/is-signed-integer-data-type' ); var broadcastScalar = require( '@stdlib/ndarray/base/broadcast-scalar' ); var maybeBroadcastArray = require( '@stdlib/ndarray/base/maybe-broadcast-array' ); var getDType = require( '@stdlib/ndarray/dtype' ); var getShape = require( '@stdlib/ndarray/shape' ); var getOrder = require( '@stdlib/ndarray/order' ); var format = require( '@stdlib/string/format' ); var emptyLike = require( '@stdlib/ndarray/empty-like' ); var assign = require( '@stdlib/ndarray/base/assign' ); var sorthp = require( '@stdlib/blas/ext/sorthp' ); var nonCoreShape = require( './non_core_shape.js' ); // FUNCTIONS // /** * Returns a boolean indicating if a value is a string literal specifying ascending sort order. * * @private * @param {*} value - input value * @returns {boolean} boolean result */ function isAscending( value ) { return ( value === 'asc' || value === 'ascending' ); } /** * Returns a boolean indicating if a value is a string literal specifying descending sort order. * * @private * @param {*} value - input value * @returns {boolean} boolean result */ function isDescending( value ) { return ( value === 'desc' || value === 'descending' ); } /** * Converts a string literal to a numeric sort order value. * * @private * @param {string} value - input value * @throws {TypeError} must provide a supported string * @returns {number} sort order */ function string2order( value ) { if ( isAscending( value ) ) { return 1; } if ( isDescending( value ) ) { return -1; } throw new TypeError( format( 'invalid argument. Second argument must be a valid sort order. Value: `%s`.', value ) ); } /** * Normalize a numeric sort order value. * * ## Notes * * - Normalizing numeric sort order values to canonical values `-1`, `+1`, and `0` ensures that we can avoid truncation rounding errors when casting a provided sort order to the data type of the input ndarray. * * @private * @param {number} value - input value * @returns {number} normalized value */ function normalizeOrder( value ) { if ( value < 0 ) { return -1; } if ( value > 0 ) { return 1; } return value; } // MAIN // /** * Returns a new ndarray containing the elements of an input ndarray sorted along one or more ndarray dimensions using heapsort. * * @param {ndarrayLike} x - input ndarray * @param {(ndarrayLike|number|string)} [sortOrder=1.0] - sort order * @param {Options} [options] - function options * @param {IntegerArray} [options.dims] - list of dimensions over which to perform operation * @throws {TypeError} first argument must be an ndarray-like object * @throws {TypeError} sort order argument must be either an ndarray-like object, a numeric value, or a supported string * @throws {TypeError} options argument must be an object * @throws {RangeError} dimension indices must not exceed input ndarray bounds * @throws {RangeError} number of dimension indices must not exceed the number of input ndarray dimensions * @throws {Error} must provide valid options * @returns {ndarray} output ndarray * * @example * var Float64Array = require( '@stdlib/array/float64' ); * var ndarray2array = require( '@stdlib/ndarray/to-array' ); * var ndarray = require( '@stdlib/ndarray/ctor' ); * * // Create a data buffer: * var xbuf = new Float64Array( [ 1.0, 2.0, -3.0, 4.0, -5.0, 6.0 ] ); * * // Define the shape of the input array: * var sh = [ 3, 1, 2 ]; * * // Define the array strides: * var sx = [ 2, 2, 1 ]; * * // Define the index offset: * var ox = 0; * * // Create an input ndarray: * var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); * * // Perform operation: * var out = toSortedhp( x ); * // returns <ndarray> * * var arr = ndarray2array( out ); * // returns [ [ [ -5.0, -3.0 ] ], [ [ 1.0, 2.0 ] ], [ [ 4.0, 6.0 ] ] ] */ function toSortedhp( x ) { var isStr; var nargs; var opts; var ord; var dt; var sh; var o; var y; if ( !isndarrayLike( x ) ) { throw new TypeError( format( 'invalid argument. First argument must be an ndarray-like object. Value: `%s`.', x ) ); } nargs = arguments.length; // Create an output ndarray: y = emptyLike( x ); // Assign elements from the input ndarray to the output ndarray: assign( [ x, y ] ); // Resolve output ndarray meta data: dt = getDType( y ); if ( !isRealFloatingDataType( dt ) && !isSignedIntegerDataType( dt ) ) { // Fallback to "generic" only if we cannot safely cast `-1` to the data type of the input ndarray: dt = 'generic'; } ord = getOrder( y ); // Case: toSortedhp( x ) if ( nargs < 2 ) { return sorthp( y, broadcastScalar( 1, dt, [], ord ) ); } o = arguments[ 1 ]; // Case: toSortedhp( x, ??? ) if ( nargs === 2 ) { // Case: toSortedhp( x, sortOrder_scalar || sortOrder_string ) isStr = isString( o ); if ( isStr || isNumber( o ) ) { return sorthp( y, broadcastScalar( ( isStr ) ? string2order( o ) : normalizeOrder( o ), dt, [], ord ) ); } // Case: toSortedhp( x, sortOrder_ndarray ) if ( isndarrayLike( o ) ) { // As the operation is performed across all dimensions, `o` is assumed to be a zero-dimensional ndarray... return sorthp( y, o ); } // Case: toSortedhp( x, opts ) opts = o; o = 1; // Intentionally fall through... } // Case: toSortedhp( x, sortOrder, opts ) else { // nargs > 2 opts = arguments[ 2 ]; } // Case: toSortedhp( x, sortOrder_scalar || sortOrder_string, opts ) isStr = isString( o ); if ( isStr || isNumber( o ) ) { if ( hasOwnProp( opts, 'dims' ) ) { sh = nonCoreShape( getShape( y ), opts.dims ); } else { sh = []; } o = broadcastScalar( ( isStr ) ? string2order( o ) : normalizeOrder( o ), dt, sh, getOrder( y ) ); } // Case: toSortedhp( x, sortOrder_ndarray, opts ) else if ( isndarrayLike( o ) ) { // When not provided `dims`, the operation is performed across all dimensions and `o` is assumed to be a zero-dimensional ndarray; when `dims` is provided, we need to broadcast `o` to match the shape of the non-core dimensions... if ( hasOwnProp( opts, 'dims' ) ) { o = maybeBroadcastArray( o, nonCoreShape( getShape( y ), opts.dims ) ); } } else { throw new TypeError( format( 'invalid argument. Second argument must be either an ndarray, a numeric scalar value, or a supported string. Value: `%s`.', o ) ); } return sorthp( y, o, opts ); } // EXPORTS // module.exports = toSortedhp; |