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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 198x 198x 198x 198x 198x 198x 198x 198x 198x 198x 198x 198x 198x 198x 3x 3x 135x 135x 135x 198x 55x 55x 6x 6x 6x 49x 55x 3x 3x 46x 55x 6x 2x 6x 2x 2x 2x 2x 4x 4x 40x 40x 40x 40x 40x 40x 80x 80x 80x 80x 120x 198x 38x 38x 23x 23x 38x 82x 82x 73x 48x 73x 25x 25x 69x 69x 9x 9x 3x 1x 3x 1x 1x 1x 1x 3x 1x 1x 1x 1x 2x 9x 6x 6x 107x 198x 1x 1x 1x 1x 1x | /** * @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 broadcastScalar = require( '@stdlib/ndarray/base/broadcast-scalar' ); var maybeBroadcastArray = require( '@stdlib/ndarray/base/maybe-broadcast-array' ); var getShape = require( '@stdlib/ndarray/shape' ); var getOrder = require( '@stdlib/ndarray/order' ); var format = require( '@stdlib/string/format' ); var nonCoreShape = require( './non_core_shape.js' ); var base = require( './base.js' ); // MAIN // /** * Sorts an input ndarray along one or more ndarray dimensions using heapsort. * * @param {ndarrayLike} x - input ndarray * @param {(ndarrayLike|number|string)} [sortOrder] - 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 valid 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 = sorthp( x ); * // returns <ndarray> * * var arr = ndarray2array( out ); * // returns [ [ [ -5.0, -3.0 ] ], [ [ 1.0, 2.0 ] ], [ [ 4.0, 6.0 ] ] ] */ function sorthp( x ) { var nargs; var opts; var ord; var sh; var o; nargs = arguments.length; // Resolve input ndarray meta data: ord = getOrder( x ); // Case: sorthp( x ) if ( nargs < 2 ) { return base( x, broadcastScalar( 1.0, 'generic', [], ord ) ); } o = arguments[ 1 ]; // Case: sorthp( x, ??? ) if ( nargs === 2 ) { // Case: sorthp( x, sortOrder_ndarray ) if ( isndarrayLike( o ) ) { // As the operation is performed across all dimensions, `o` is assumed to be a zero-dimensional ndarray... return base( x, o ); } // Case: sorthp( x, sortOrder_scalar ) if ( isNumber( o ) ) { return base( x, broadcastScalar( o, 'generic', [], ord ) ); } // Case: sorthp( x, sortOrder_string ) if ( isString( o ) ) { if ( o === 'asc' || o === 'ascending' ) { o = 1.0; } else if ( o === 'dsc' || o === 'descending' ) { o = -1.0; } else { throw new TypeError( format( 'invalid argument. Second argument must be a valid sort order. Value: `%s`.', o ) ); } return base( x, broadcastScalar( o, 'generic', [], ord ) ); } // Case: sorthp( x, opts ) opts = o; o = 1.0; // Intentionally fall through... } // Case: sorthp( x, sortOrder, opts ) else { // nargs > 2 opts = arguments[ 2 ]; } // Case: sorthp( x, sortOrder_ndarray, opts ) 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( x ), opts.dims ) ); // eslint-disable-line max-len } } // Case: sorthp( x, sortOrder_scalar, opts ) else if ( isNumber( o ) ) { if ( hasOwnProp( opts, 'dims' ) ) { sh = nonCoreShape( getShape( x ), opts.dims ); } else { sh = []; } o = broadcastScalar( o, 'generic', sh, getOrder( x ) ); } // Case: sorthp( x, sortOrder_string, opts ) else if ( isString( o ) ) { if ( o === 'asc' || o === 'ascending' ) { o = 1.0; } else if ( o === 'dsc' || o === 'descending' ) { o = -1.0; } else { throw new TypeError( format( 'invalid argument. Second argument must be a valid sort order. Value: `%s`.', o ) ); } if ( hasOwnProp( opts, 'dims' ) ) { sh = nonCoreShape( getShape( x ), opts.dims ); } else { sh = []; } o = broadcastScalar( o, 'generic', sh, getOrder( x ) ); } else { throw new TypeError( format( 'invalid argument. Second argument must be either an ndarray, a numeric scalar value, or a valid sort order string. Value: `%s`.', o ) ); } return base( x, o, opts ); } // EXPORTS // module.exports = sorthp; |