@stdlib/blas-ext-base-dsorthp 0.2.2 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/NOTICE +1 -1
- package/README.md +136 -32
- package/dist/index.js +7 -7
- package/dist/index.js.map +4 -4
- package/docs/types/index.d.ts +6 -6
- package/include/stdlib/blas/ext/base/dsorthp.h +7 -5
- package/lib/dsorthp.js +5 -94
- package/lib/dsorthp.native.js +3 -3
- package/lib/ndarray.js +13 -13
- package/lib/ndarray.native.js +5 -15
- package/manifest.json +42 -8
- package/package.json +5 -4
- package/src/addon.c +22 -4
- package/src/{dsorthp.c → main.c} +52 -32
package/NOTICE
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Copyright (c) 2016-
|
|
1
|
+
Copyright (c) 2016-2026 The Stdlib Authors.
|
package/README.md
CHANGED
|
@@ -53,9 +53,9 @@ npm install @stdlib/blas-ext-base-dsorthp
|
|
|
53
53
|
var dsorthp = require( '@stdlib/blas-ext-base-dsorthp' );
|
|
54
54
|
```
|
|
55
55
|
|
|
56
|
-
#### dsorthp( N, order, x,
|
|
56
|
+
#### dsorthp( N, order, x, strideX )
|
|
57
57
|
|
|
58
|
-
Sorts a double-precision floating-point strided array
|
|
58
|
+
Sorts a double-precision floating-point strided array using heapsort.
|
|
59
59
|
|
|
60
60
|
```javascript
|
|
61
61
|
var Float64Array = require( '@stdlib/array-float64' );
|
|
@@ -71,9 +71,9 @@ The function has the following parameters:
|
|
|
71
71
|
- **N**: number of indexed elements.
|
|
72
72
|
- **order**: sort order. If `order < 0.0`, the input strided array is sorted in **decreasing** order. If `order > 0.0`, the input strided array is sorted in **increasing** order. If `order == 0.0`, the input strided array is left unchanged.
|
|
73
73
|
- **x**: input [`Float64Array`][@stdlib/array/float64].
|
|
74
|
-
- **
|
|
74
|
+
- **strideX**: stride length.
|
|
75
75
|
|
|
76
|
-
The `N` and
|
|
76
|
+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to sort every other element:
|
|
77
77
|
|
|
78
78
|
```javascript
|
|
79
79
|
var Float64Array = require( '@stdlib/array-float64' );
|
|
@@ -100,9 +100,9 @@ dsorthp( 2, -1.0, x1, 2 );
|
|
|
100
100
|
// x0 => <Float64Array>[ 1.0, 4.0, 3.0, 2.0 ]
|
|
101
101
|
```
|
|
102
102
|
|
|
103
|
-
#### dsorthp.ndarray( N, order, x,
|
|
103
|
+
#### dsorthp.ndarray( N, order, x, strideX, offsetX )
|
|
104
104
|
|
|
105
|
-
Sorts a double-precision floating-point strided array
|
|
105
|
+
Sorts a double-precision floating-point strided array using heapsort and alternative indexing semantics.
|
|
106
106
|
|
|
107
107
|
```javascript
|
|
108
108
|
var Float64Array = require( '@stdlib/array-float64' );
|
|
@@ -115,9 +115,9 @@ dsorthp.ndarray( x.length, 1.0, x, 1, 0 );
|
|
|
115
115
|
|
|
116
116
|
The function has the following additional parameters:
|
|
117
117
|
|
|
118
|
-
- **
|
|
118
|
+
- **offsetX**: starting index.
|
|
119
119
|
|
|
120
|
-
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying
|
|
120
|
+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameter supports indexing semantics based on a starting index. For example, to access only the last three elements:
|
|
121
121
|
|
|
122
122
|
```javascript
|
|
123
123
|
var Float64Array = require( '@stdlib/array-float64' );
|
|
@@ -154,27 +154,12 @@ dsorthp.ndarray( 3, 1.0, x, 1, x.length-3 );
|
|
|
154
154
|
<!-- eslint no-undef: "error" -->
|
|
155
155
|
|
|
156
156
|
```javascript
|
|
157
|
-
var
|
|
158
|
-
var randu = require( '@stdlib/random-base-randu' );
|
|
159
|
-
var Float64Array = require( '@stdlib/array-float64' );
|
|
157
|
+
var discreteUniform = require( '@stdlib/random-array-discrete-uniform' );
|
|
160
158
|
var dsorthp = require( '@stdlib/blas-ext-base-dsorthp' );
|
|
161
159
|
|
|
162
|
-
var
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
var i;
|
|
166
|
-
|
|
167
|
-
x = new Float64Array( 10 );
|
|
168
|
-
for ( i = 0; i < x.length; i++ ) {
|
|
169
|
-
rand = round( randu()*100.0 );
|
|
170
|
-
sign = randu();
|
|
171
|
-
if ( sign < 0.5 ) {
|
|
172
|
-
sign = -1.0;
|
|
173
|
-
} else {
|
|
174
|
-
sign = 1.0;
|
|
175
|
-
}
|
|
176
|
-
x[ i ] = sign * rand;
|
|
177
|
-
}
|
|
160
|
+
var x = discreteUniform( 10, -100, 100, {
|
|
161
|
+
'dtype': 'float64'
|
|
162
|
+
});
|
|
178
163
|
console.log( x );
|
|
179
164
|
|
|
180
165
|
dsorthp( x.length, -1.0, x, -1 );
|
|
@@ -187,6 +172,125 @@ console.log( x );
|
|
|
187
172
|
|
|
188
173
|
* * *
|
|
189
174
|
|
|
175
|
+
<section class="c">
|
|
176
|
+
|
|
177
|
+
## C APIs
|
|
178
|
+
|
|
179
|
+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
|
|
180
|
+
|
|
181
|
+
<section class="intro">
|
|
182
|
+
|
|
183
|
+
</section>
|
|
184
|
+
|
|
185
|
+
<!-- /.intro -->
|
|
186
|
+
|
|
187
|
+
<!-- C usage documentation. -->
|
|
188
|
+
|
|
189
|
+
<section class="usage">
|
|
190
|
+
|
|
191
|
+
### Usage
|
|
192
|
+
|
|
193
|
+
```c
|
|
194
|
+
#include "stdlib/blas/ext/base/dsorthp.h"
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
#### stdlib_strided_dsorthp( N, order, \*X, strideX )
|
|
198
|
+
|
|
199
|
+
Sorts a double-precision floating-point strided array using heapsort.
|
|
200
|
+
|
|
201
|
+
```c
|
|
202
|
+
double x[] = { 1.0, -2.0, 3.0, -4.0 };
|
|
203
|
+
|
|
204
|
+
stdlib_strided_dsorthp( 2, -1.0, x, 1 );
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
The function accepts the following arguments:
|
|
208
|
+
|
|
209
|
+
- **N**: `[in] CBLAS_INT` number of indexed elements.
|
|
210
|
+
- **order**: `[in] double` sort order. If `order < 0.0`, the input strided array is sorted in **decreasing** order. If `order > 0.0`, the input strided array is sorted in **increasing** order. If `order == 0.0`, the input strided array is left unchanged.
|
|
211
|
+
- **X**: `[inout] double*` input array.
|
|
212
|
+
- **strideX**: `[in] CBLAS_INT` stride length.
|
|
213
|
+
|
|
214
|
+
```c
|
|
215
|
+
stdlib_strided_dsorthp( const CBLAS_INT N, const double order, double *X, const CBLAS_INT strideX );
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
<!--lint disable maximum-heading-length-->
|
|
219
|
+
|
|
220
|
+
#### stdlib_strided_dsorthp_ndarray( N, order, \*X, strideX, offsetX )
|
|
221
|
+
|
|
222
|
+
<!--lint enable maximum-heading-length-->
|
|
223
|
+
|
|
224
|
+
Sorts a double-precision floating-point strided array using heapsort and alternative indexing semantics.
|
|
225
|
+
|
|
226
|
+
```c
|
|
227
|
+
double x[] = { 1.0, -2.0, 3.0, -4.0 };
|
|
228
|
+
|
|
229
|
+
stdlib_strided_dsorthp_ndarray( 4, 1.0, x, 1, 0 );
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
The function accepts the following arguments:
|
|
233
|
+
|
|
234
|
+
- **N**: `[in] CBLAS_INT` number of indexed elements.
|
|
235
|
+
- **order**: `[in] double` sort order. If `order < 0.0`, the input strided array is sorted in **decreasing** order. If `order > 0.0`, the input strided array is sorted in **increasing** order. If `order == 0.0`, the input strided array is left unchanged.
|
|
236
|
+
- **X**: `[inout] double*` input array.
|
|
237
|
+
- **strideX**: `[in] CBLAS_INT` stride length.
|
|
238
|
+
- **offsetX**: `[in] CBLAS_INT` starting index.
|
|
239
|
+
|
|
240
|
+
```c
|
|
241
|
+
stdlib_strided_dsorthp_ndarray( const CBLAS_INT N, const double order, double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
</section>
|
|
245
|
+
|
|
246
|
+
<!-- /.usage -->
|
|
247
|
+
|
|
248
|
+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
|
|
249
|
+
|
|
250
|
+
<section class="notes">
|
|
251
|
+
|
|
252
|
+
</section>
|
|
253
|
+
|
|
254
|
+
<!-- /.notes -->
|
|
255
|
+
|
|
256
|
+
<!-- C API usage examples. -->
|
|
257
|
+
|
|
258
|
+
<section class="examples">
|
|
259
|
+
|
|
260
|
+
### Examples
|
|
261
|
+
|
|
262
|
+
```c
|
|
263
|
+
#include "stdlib/blas/ext/base/dsorthp.h"
|
|
264
|
+
#include <stdio.h>
|
|
265
|
+
|
|
266
|
+
int main( void ) {
|
|
267
|
+
// Create a strided array:
|
|
268
|
+
double x[] = { 1.0, -2.0, 3.0, -4.0, 5.0, -6.0, 7.0, -8.0 };
|
|
269
|
+
|
|
270
|
+
// Specify the number of elements:
|
|
271
|
+
int N = 8;
|
|
272
|
+
|
|
273
|
+
// Specify a stride:
|
|
274
|
+
int strideX = 1;
|
|
275
|
+
|
|
276
|
+
// Sort the array:
|
|
277
|
+
stdlib_strided_dsorthp( N, 1.0, x, strideX );
|
|
278
|
+
|
|
279
|
+
// Print the result:
|
|
280
|
+
for ( int i = 0; i < 8; i++ ) {
|
|
281
|
+
printf( "x[ %i ] = %lf\n", i, x[ i ] );
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
</section>
|
|
287
|
+
|
|
288
|
+
<!-- /.examples -->
|
|
289
|
+
|
|
290
|
+
</section>
|
|
291
|
+
|
|
292
|
+
<!-- /.c -->
|
|
293
|
+
|
|
190
294
|
<section class="references">
|
|
191
295
|
|
|
192
296
|
## References
|
|
@@ -240,7 +344,7 @@ See [LICENSE][stdlib-license].
|
|
|
240
344
|
|
|
241
345
|
## Copyright
|
|
242
346
|
|
|
243
|
-
Copyright © 2016-
|
|
347
|
+
Copyright © 2016-2026. The Stdlib [Authors][stdlib-authors].
|
|
244
348
|
|
|
245
349
|
</section>
|
|
246
350
|
|
|
@@ -253,8 +357,8 @@ Copyright © 2016-2024. The Stdlib [Authors][stdlib-authors].
|
|
|
253
357
|
[npm-image]: http://img.shields.io/npm/v/@stdlib/blas-ext-base-dsorthp.svg
|
|
254
358
|
[npm-url]: https://npmjs.org/package/@stdlib/blas-ext-base-dsorthp
|
|
255
359
|
|
|
256
|
-
[test-image]: https://github.com/stdlib-js/blas-ext-base-dsorthp/actions/workflows/test.yml/badge.svg?branch=v0.
|
|
257
|
-
[test-url]: https://github.com/stdlib-js/blas-ext-base-dsorthp/actions/workflows/test.yml?query=branch:v0.
|
|
360
|
+
[test-image]: https://github.com/stdlib-js/blas-ext-base-dsorthp/actions/workflows/test.yml/badge.svg?branch=v0.3.0
|
|
361
|
+
[test-url]: https://github.com/stdlib-js/blas-ext-base-dsorthp/actions/workflows/test.yml?query=branch:v0.3.0
|
|
258
362
|
|
|
259
363
|
[coverage-image]: https://img.shields.io/codecov/c/github/stdlib-js/blas-ext-base-dsorthp/main.svg
|
|
260
364
|
[coverage-url]: https://codecov.io/github/stdlib-js/blas-ext-base-dsorthp?branch=main
|
|
@@ -266,8 +370,8 @@ Copyright © 2016-2024. The Stdlib [Authors][stdlib-authors].
|
|
|
266
370
|
|
|
267
371
|
-->
|
|
268
372
|
|
|
269
|
-
[chat-image]: https://img.shields.io/
|
|
270
|
-
[chat-url]: https://
|
|
373
|
+
[chat-image]: https://img.shields.io/badge/zulip-join_chat-brightgreen.svg
|
|
374
|
+
[chat-url]: https://stdlib.zulipchat.com
|
|
271
375
|
|
|
272
376
|
[stdlib]: https://github.com/stdlib-js/stdlib
|
|
273
377
|
|
package/dist/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
"use strict";var
|
|
2
|
-
var
|
|
3
|
-
});var
|
|
4
|
-
var
|
|
5
|
-
});var E=
|
|
6
|
-
var
|
|
7
|
-
});var
|
|
1
|
+
"use strict";var h=function(i,v){return function(){return v||i((v={exports:{}}).exports,v),v.exports}};var y=h(function(J,w){
|
|
2
|
+
var m=require('@stdlib/math-base-assert-is-positive-zero/dist'),k=require('@stdlib/math-base-assert-is-nan/dist'),P=require('@stdlib/math-base-special-floor/dist');function Z(i,v,r,e,u){var p,n,a,o,t,s,l,q,c;if(i<=0||v===0)return r;for(v<0&&(e*=-1,u-=(i-1)*e),t=i,p=P(i/2);;){if(p>0)p-=1,s=r[u+p*e];else{if(t-=1,t===0)return r;l=u+t*e,s=r[l],r[l]=r[u]}for(q=p,n=q*2+1;n<t&&(c=n+1,c<t&&(a=r[u+c*e],o=r[u+n*e],(a>o||k(a)||a===o&&m(a))&&(n+=1)),a=r[u+n*e],a>s||k(a)||a===s&&m(a));)r[u+q*e]=a,q=n,n=q*2+1;r[u+q*e]=s}}w.exports=Z
|
|
3
|
+
});var _=h(function(K,R){
|
|
4
|
+
var g=require('@stdlib/strided-base-stride2offset/dist'),z=y();function A(i,v,r,e){return z(i,v,r,e,g(i,e))}R.exports=A
|
|
5
|
+
});var E=h(function(L,d){
|
|
6
|
+
var B=require('@stdlib/utils-define-nonenumerable-read-only-property/dist'),b=_(),C=y();B(b,"ndarray",C);d.exports=b
|
|
7
|
+
});var D=require("path").join,F=require('@stdlib/utils-try-require/dist'),G=require('@stdlib/assert-is-error/dist'),H=E(),j,O=F(D(__dirname,"./native.js"));G(O)?j=H:j=O;module.exports=j;
|
|
8
8
|
/** @license Apache-2.0 */
|
|
9
9
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../lib/
|
|
4
|
-
"sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2020 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isPositiveZero = require( '@stdlib/math-base-assert-is-positive-zero' );\nvar isnan = require( '@stdlib/math-base-assert-is-nan' );\nvar floor = require( '@stdlib/math-base-special-floor' );\n\n\n// MAIN //\n\n/**\n* Sorts a double-precision floating-point strided array using heapsort.\n*\n* ## Notes\n*\n* - This implementation uses an in-place algorithm derived from the work of Floyd (1964).\n*\n* ## References\n*\n* - Williams, John William Joseph. 1964. \"Algorithm 232: Heapsort.\" _Communications of the ACM_ 7 (6). New York, NY, USA: Association for Computing Machinery: 347\u201349. doi:[10.1145/512274.512284](https://doi.org/10.1145/512274.512284).\n* - 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).\n*\n* @param {PositiveInteger} N - number of indexed elements\n* @param {number} order - sort order\n* @param {Float64Array} x - input array\n* @param {integer} stride - index increment\n* @returns {Float64Array} input array\n*\n* @example\n* var Float64Array = require( '@stdlib/array-float64' );\n*\n* var x = new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] );\n*\n* dsorthp( x.length, 1.0, x, 1 );\n* // x => <Float64Array>[ -4.0, -2.0, 1.0, 3.0 ]\n*/\nfunction dsorthp( N, order, x, stride ) {\n\tvar offset;\n\tvar parent;\n\tvar child;\n\tvar v1;\n\tvar v2;\n\tvar n;\n\tvar t;\n\tvar i;\n\tvar j;\n\tvar k;\n\n\tif ( N <= 0 || order === 0.0 ) {\n\t\treturn x;\n\t}\n\t// 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...\n\tif ( order < 0.0 ) {\n\t\tstride *= -1;\n\t}\n\tif ( stride < 0 ) {\n\t\toffset = (1-N) * stride;\n\t} else {\n\t\toffset = 0;\n\t}\n\t// Set the initial heap size:\n\tn = N;\n\n\t// Specify an initial \"parent\" index for building the heap:\n\tparent = floor( N / 2 );\n\n\t// Continue looping until the array is sorted...\n\twhile ( true ) {\n\t\tif ( parent > 0 ) {\n\t\t\t// We need to build the heap...\n\t\t\tparent -= 1;\n\t\t\tt = x[ offset+(parent*stride) ];\n\t\t} else {\n\t\t\t// Reduce the heap size:\n\t\t\tn -= 1;\n\n\t\t\t// Check if the heap is empty, and, if so, we are finished sorting...\n\t\t\tif ( n === 0 ) {\n\t\t\t\treturn x;\n\t\t\t}\n\t\t\t// Store the last heap value in a temporary variable in order to make room for the heap root being placed into its sorted position:\n\t\t\ti = offset + (n*stride);\n\t\t\tt = x[ i ];\n\n\t\t\t// Move the heap root to its sorted position:\n\t\t\tx[ i ] = x[ offset ];\n\t\t}\n\t\t// We need to \"sift down\", pushing `t` down the heap to in order to replace the parent and satisfy the heap property...\n\n\t\t// Start at the parent index:\n\t\tj = parent;\n\n\t\t// Get the \"left\" child index:\n\t\tchild = (j*2) + 1;\n\n\t\twhile ( child < n ) {\n\t\t\t// Find the largest child...\n\t\t\tk = child + 1;\n\t\t\tif ( k < n ) {\n\t\t\t\tv1 = x[ offset+(k*stride) ];\n\t\t\t\tv2 = x[ offset+(child*stride) ];\n\n\t\t\t\t// Check if a \"right\" child exists and is \"bigger\"...\n\t\t\t\tif ( v1 > v2 || isnan( v1 ) || (v1 === v2 && isPositiveZero( v1 ) ) ) { // eslint-disable-line max-len\n\t\t\t\t\tchild += 1;\n\t\t\t\t}\n\t\t\t}\n\t\t\t// Check if the largest child is bigger than `t`...\n\t\t\tv1 = x[ offset+(child*stride) ];\n\t\t\tif ( v1 > t || isnan( v1 ) || ( v1 === t && isPositiveZero( v1 ) ) ) { // eslint-disable-line max-len\n\t\t\t\t// Insert the larger child value:\n\t\t\t\tx[ offset+(j*stride) ] = v1;\n\n\t\t\t\t// Update `j` to point to the child index:\n\t\t\t\tj = child;\n\n\t\t\t\t// Get the \"left\" child index and repeat...\n\t\t\t\tchild = (j*2) + 1;\n\t\t\t} else {\n\t\t\t\t// We've found `t`'s place in the heap...\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\t// Insert `t` into the heap:\n\t\tx[ offset+(j*stride) ] = t;\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = dsorthp;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2020 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isPositiveZero = require( '@stdlib/math-base-assert-is-positive-zero' );\nvar isnan = require( '@stdlib/math-base-assert-is-nan' );\nvar floor = require( '@stdlib/math-base-special-floor' );\n\n\n// MAIN //\n\n/**\n* Sorts a double-precision floating-point strided array using heapsort.\n*\n* ## Notes\n*\n* - This implementation uses an in-place algorithm derived from the work of Floyd (1964).\n*\n* ## References\n*\n* - Williams, John William Joseph. 1964. \"Algorithm 232: Heapsort.\" _Communications of the ACM_ 7 (6). New York, NY, USA: Association for Computing Machinery: 347\u201349. doi:[10.1145/512274.512284](https://doi.org/10.1145/512274.512284).\n* - 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).\n*\n* @param {PositiveInteger} N - number of indexed elements\n* @param {number} order - sort order\n* @param {Float64Array} x - input array\n* @param {integer} stride - index increment\n* @param {NonNegativeInteger} offset - starting index\n* @returns {Float64Array} input array\n*\n* @example\n* var Float64Array = require( '@stdlib/array-float64' );\n*\n* var x = new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] );\n*\n* dsorthp( x.length, 1.0, x, 1, 0 );\n* // x => <Float64Array>[ -4.0, -2.0, 1.0, 3.0 ]\n*/\nfunction dsorthp( N, order, x, stride, offset ) {\n\tvar parent;\n\tvar child;\n\tvar v1;\n\tvar v2;\n\tvar n;\n\tvar t;\n\tvar i;\n\tvar j;\n\tvar k;\n\n\tif ( N <= 0 || order === 0.0 ) {\n\t\treturn x;\n\t}\n\t// 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...\n\tif ( order < 0.0 ) {\n\t\tstride *= -1;\n\t\toffset -= (N-1) * stride;\n\t}\n\t// Set the initial heap size:\n\tn = N;\n\n\t// Specify an initial \"parent\" index for building the heap:\n\tparent = floor( N / 2 );\n\n\t// Continue looping until the array is sorted...\n\twhile ( true ) {\n\t\tif ( parent > 0 ) {\n\t\t\t// We need to build the heap...\n\t\t\tparent -= 1;\n\t\t\tt = x[ offset+(parent*stride) ];\n\t\t} else {\n\t\t\t// Reduce the heap size:\n\t\t\tn -= 1;\n\n\t\t\t// Check if the heap is empty, and, if so, we are finished sorting...\n\t\t\tif ( n === 0 ) {\n\t\t\t\treturn x;\n\t\t\t}\n\t\t\t// Store the last heap value in a temporary variable in order to make room for the heap root being placed into its sorted position:\n\t\t\ti = offset + (n*stride);\n\t\t\tt = x[ i ];\n\n\t\t\t// Move the heap root to its sorted position:\n\t\t\tx[ i ] = x[ offset ];\n\t\t}\n\t\t// We need to \"sift down\", pushing `t` down the heap to in order to replace the parent and satisfy the heap property...\n\n\t\t// Start at the parent index:\n\t\tj = parent;\n\n\t\t// Get the \"left\" child index:\n\t\tchild = (j*2) + 1;\n\n\t\twhile ( child < n ) {\n\t\t\t// Find the largest child...\n\t\t\tk = child + 1;\n\t\t\tif ( k < n ) {\n\t\t\t\tv1 = x[ offset+(k*stride) ];\n\t\t\t\tv2 = x[ offset+(child*stride) ];\n\n\t\t\t\t// Check if a \"right\" child exists and is \"bigger\"...\n\t\t\t\tif ( v1 > v2 || isnan( v1 ) || (v1 === v2 && isPositiveZero( v1 ) ) ) { // eslint-disable-line max-len\n\t\t\t\t\tchild += 1;\n\t\t\t\t}\n\t\t\t}\n\t\t\t// Check if the largest child is bigger than `t`...\n\t\t\tv1 = x[ offset+(child*stride) ];\n\t\t\tif ( v1 > t || isnan( v1 ) || ( v1 === t && isPositiveZero( v1 ) ) ) { // eslint-disable-line max-len\n\t\t\t\t// Insert the larger child value:\n\t\t\t\tx[ offset+(j*stride) ] = v1;\n\n\t\t\t\t// Update `j` to point to the child index:\n\t\t\t\tj = child;\n\n\t\t\t\t// Get the \"left\" child index and repeat...\n\t\t\t\tchild = (j*2) + 1;\n\t\t\t} else {\n\t\t\t\t// We've found `t`'s place in the heap...\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\t// Insert `t` into the heap:\n\t\tx[ offset+(j*stride) ] = t;\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = dsorthp;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2020 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );\nvar dsorthp = require( './dsorthp.js' );\nvar ndarray = require( './ndarray.js' );\n\n\n// MAIN //\n\nsetReadOnly( dsorthp, 'ndarray', ndarray );\n\n\n// EXPORTS //\n\nmodule.exports = dsorthp;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2020 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Sort a double-precision floating-point strided array using heapsort.\n*\n* @module @stdlib/blas-ext-base-dsorthp\n*\n* @example\n* var Float64Array = require( '@stdlib/array-float64' );\n* var dsorthp = require( '@stdlib/blas-ext-base-dsorthp' );\n*\n* var x = new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] );\n*\n* dsorthp( x.length, 1.0, x, 1 );\n* // x => <Float64Array>[ -4.0, -2.0, 1.0, 3.0 ]\n*\n* @example\n* var Float64Array = require( '@stdlib/array-float64' );\n* var dsorthp = require( '@stdlib/blas-ext-base-dsorthp' );\n*\n* var x = new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] );\n*\n* dsorthp.ndarray( x.length, 1.0, x, 1, 0 );\n* // x => <Float64Array>[ -4.0, -2.0, 1.0, 3.0 ]\n*/\n\n// MODULES //\n\nvar join = require( 'path' ).join;\nvar tryRequire = require( '@stdlib/utils-try-require' );\nvar isError = require( '@stdlib/assert-is-error' );\nvar main = require( './main.js' );\n\n\n// MAIN //\n\nvar dsorthp;\nvar tmp = tryRequire( join( __dirname, './native.js' ) );\nif ( isError( tmp ) ) {\n\tdsorthp = main;\n} else {\n\tdsorthp = tmp;\n}\n\n\n// EXPORTS //\n\nmodule.exports = dsorthp;\n\n// exports: { \"ndarray\": \"dsorthp.ndarray\" }\n"],
|
|
5
|
-
"mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAiB,QAAS,2CAA4C,EACtEC,EAAQ,QAAS,iCAAkC,EACnDC,EAAQ,QAAS,iCAAkC,
|
|
6
|
-
"names": ["
|
|
3
|
+
"sources": ["../lib/ndarray.js", "../lib/dsorthp.js", "../lib/main.js", "../lib/index.js"],
|
|
4
|
+
"sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2020 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isPositiveZero = require( '@stdlib/math-base-assert-is-positive-zero' );\nvar isnan = require( '@stdlib/math-base-assert-is-nan' );\nvar floor = require( '@stdlib/math-base-special-floor' );\n\n\n// MAIN //\n\n/**\n* Sorts a double-precision floating-point strided array using heapsort.\n*\n* ## Notes\n*\n* - This implementation uses an in-place algorithm derived from the work of Floyd (1964).\n*\n* ## References\n*\n* - Williams, John William Joseph. 1964. \"Algorithm 232: Heapsort.\" _Communications of the ACM_ 7 (6). New York, NY, USA: Association for Computing Machinery: 347\u201349. doi:[10.1145/512274.512284](https://doi.org/10.1145/512274.512284).\n* - 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).\n*\n* @param {PositiveInteger} N - number of indexed elements\n* @param {number} order - sort order\n* @param {Float64Array} x - input array\n* @param {integer} strideX - stride length\n* @param {NonNegativeInteger} offsetX - starting index\n* @returns {Float64Array} input array\n*\n* @example\n* var Float64Array = require( '@stdlib/array-float64' );\n*\n* var x = new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] );\n*\n* dsorthp( x.length, 1.0, x, 1, 0 );\n* // x => <Float64Array>[ -4.0, -2.0, 1.0, 3.0 ]\n*/\nfunction dsorthp( N, order, x, strideX, offsetX ) {\n\tvar parent;\n\tvar child;\n\tvar v1;\n\tvar v2;\n\tvar n;\n\tvar t;\n\tvar i;\n\tvar j;\n\tvar k;\n\n\tif ( N <= 0 || order === 0.0 ) {\n\t\treturn x;\n\t}\n\t// 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...\n\tif ( order < 0.0 ) {\n\t\tstrideX *= -1;\n\t\toffsetX -= (N-1) * strideX;\n\t}\n\t// Set the initial heap size:\n\tn = N;\n\n\t// Specify an initial \"parent\" index for building the heap:\n\tparent = floor( N / 2 );\n\n\t// Continue looping until the array is sorted...\n\twhile ( true ) {\n\t\tif ( parent > 0 ) {\n\t\t\t// We need to build the heap...\n\t\t\tparent -= 1;\n\t\t\tt = x[ offsetX+(parent*strideX) ];\n\t\t} else {\n\t\t\t// Reduce the heap size:\n\t\t\tn -= 1;\n\n\t\t\t// Check if the heap is empty, and, if so, we are finished sorting...\n\t\t\tif ( n === 0 ) {\n\t\t\t\treturn x;\n\t\t\t}\n\t\t\t// Store the last heap value in a temporary variable in order to make room for the heap root being placed into its sorted position:\n\t\t\ti = offsetX + (n*strideX);\n\t\t\tt = x[ i ];\n\n\t\t\t// Move the heap root to its sorted position:\n\t\t\tx[ i ] = x[ offsetX ];\n\t\t}\n\t\t// We need to \"sift down\", pushing `t` down the heap to in order to replace the parent and satisfy the heap property...\n\n\t\t// Start at the parent index:\n\t\tj = parent;\n\n\t\t// Get the \"left\" child index:\n\t\tchild = (j*2) + 1;\n\n\t\twhile ( child < n ) {\n\t\t\t// Find the largest child...\n\t\t\tk = child + 1;\n\t\t\tif ( k < n ) {\n\t\t\t\tv1 = x[ offsetX+(k*strideX) ];\n\t\t\t\tv2 = x[ offsetX+(child*strideX) ];\n\n\t\t\t\t// Check if a \"right\" child exists and is \"bigger\"...\n\t\t\t\tif ( v1 > v2 || isnan( v1 ) || (v1 === v2 && isPositiveZero( v1 ) ) ) { // eslint-disable-line max-len\n\t\t\t\t\tchild += 1;\n\t\t\t\t}\n\t\t\t}\n\t\t\t// Check if the largest child is bigger than `t`...\n\t\t\tv1 = x[ offsetX+(child*strideX) ];\n\t\t\tif ( v1 > t || isnan( v1 ) || ( v1 === t && isPositiveZero( v1 ) ) ) { // eslint-disable-line max-len\n\t\t\t\t// Insert the larger child value:\n\t\t\t\tx[ offsetX+(j*strideX) ] = v1;\n\n\t\t\t\t// Update `j` to point to the child index:\n\t\t\t\tj = child;\n\n\t\t\t\t// Get the \"left\" child index and repeat...\n\t\t\t\tchild = (j*2) + 1;\n\t\t\t} else {\n\t\t\t\t// We've found `t`'s place in the heap...\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\t// Insert `t` into the heap:\n\t\tx[ offsetX+(j*strideX) ] = t;\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = dsorthp;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2020 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar stride2offset = require( '@stdlib/strided-base-stride2offset' );\nvar ndarray = require( './ndarray.js' );\n\n\n// MAIN //\n\n/**\n* Sorts a double-precision floating-point strided array using heapsort.\n*\n* ## Notes\n*\n* - This implementation uses an in-place algorithm derived from the work of Floyd (1964).\n*\n* ## References\n*\n* - Williams, John William Joseph. 1964. \"Algorithm 232: Heapsort.\" _Communications of the ACM_ 7 (6). New York, NY, USA: Association for Computing Machinery: 347\u201349. doi:[10.1145/512274.512284](https://doi.org/10.1145/512274.512284).\n* - 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).\n*\n* @param {PositiveInteger} N - number of indexed elements\n* @param {number} order - sort order\n* @param {Float64Array} x - input array\n* @param {integer} strideX - stride length\n* @returns {Float64Array} input array\n*\n* @example\n* var Float64Array = require( '@stdlib/array-float64' );\n*\n* var x = new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] );\n*\n* dsorthp( x.length, 1.0, x, 1 );\n* // x => <Float64Array>[ -4.0, -2.0, 1.0, 3.0 ]\n*/\nfunction dsorthp( N, order, x, strideX ) {\n\treturn ndarray( N, order, x, strideX, stride2offset( N, strideX ) );\n}\n\n\n// EXPORTS //\n\nmodule.exports = dsorthp;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2020 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );\nvar dsorthp = require( './dsorthp.js' );\nvar ndarray = require( './ndarray.js' );\n\n\n// MAIN //\n\nsetReadOnly( dsorthp, 'ndarray', ndarray );\n\n\n// EXPORTS //\n\nmodule.exports = dsorthp;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2020 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Sort a double-precision floating-point strided array using heapsort.\n*\n* @module @stdlib/blas-ext-base-dsorthp\n*\n* @example\n* var Float64Array = require( '@stdlib/array-float64' );\n* var dsorthp = require( '@stdlib/blas-ext-base-dsorthp' );\n*\n* var x = new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] );\n*\n* dsorthp( x.length, 1.0, x, 1 );\n* // x => <Float64Array>[ -4.0, -2.0, 1.0, 3.0 ]\n*\n* @example\n* var Float64Array = require( '@stdlib/array-float64' );\n* var dsorthp = require( '@stdlib/blas-ext-base-dsorthp' );\n*\n* var x = new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] );\n*\n* dsorthp.ndarray( x.length, 1.0, x, 1, 0 );\n* // x => <Float64Array>[ -4.0, -2.0, 1.0, 3.0 ]\n*/\n\n// MODULES //\n\nvar join = require( 'path' ).join;\nvar tryRequire = require( '@stdlib/utils-try-require' );\nvar isError = require( '@stdlib/assert-is-error' );\nvar main = require( './main.js' );\n\n\n// MAIN //\n\nvar dsorthp;\nvar tmp = tryRequire( join( __dirname, './native.js' ) );\nif ( isError( tmp ) ) {\n\tdsorthp = main;\n} else {\n\tdsorthp = tmp;\n}\n\n\n// EXPORTS //\n\nmodule.exports = dsorthp;\n\n// exports: { \"ndarray\": \"dsorthp.ndarray\" }\n"],
|
|
5
|
+
"mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAiB,QAAS,2CAA4C,EACtEC,EAAQ,QAAS,iCAAkC,EACnDC,EAAQ,QAAS,iCAAkC,EAgCvD,SAASC,EAASC,EAAGC,EAAOC,EAAGC,EAASC,EAAU,CACjD,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAEJ,GAAKb,GAAK,GAAKC,IAAU,EACxB,OAAOC,EAcR,IAXKD,EAAQ,IACZE,GAAW,GACXC,IAAYJ,EAAE,GAAKG,GAGpBM,EAAIT,EAGJK,EAASP,EAAOE,EAAI,CAAE,IAGP,CACd,GAAKK,EAAS,EAEbA,GAAU,EACVK,EAAIR,EAAGE,EAASC,EAAOF,CAAS,MAC1B,CAKN,GAHAM,GAAK,EAGAA,IAAM,EACV,OAAOP,EAGRS,EAAIP,EAAWK,EAAEN,EACjBO,EAAIR,EAAGS,CAAE,EAGTT,EAAGS,CAAE,EAAIT,EAAGE,CAAQ,CACrB,CASA,IALAQ,EAAIP,EAGJC,EAASM,EAAE,EAAK,EAERN,EAAQG,IAEfI,EAAIP,EAAQ,EACPO,EAAIJ,IACRF,EAAKL,EAAGE,EAASS,EAAEV,CAAS,EAC5BK,EAAKN,EAAGE,EAASE,EAAMH,CAAS,GAG3BI,EAAKC,GAAMX,EAAOU,CAAG,GAAMA,IAAOC,GAAMZ,EAAgBW,CAAG,KAC/DD,GAAS,IAIXC,EAAKL,EAAGE,EAASE,EAAMH,CAAS,EAC3BI,EAAKG,GAAKb,EAAOU,CAAG,GAAOA,IAAOG,GAAKd,EAAgBW,CAAG,IAE9DL,EAAGE,EAASQ,EAAET,CAAS,EAAII,EAG3BK,EAAIN,EAGJA,EAASM,EAAE,EAAK,EAOlBV,EAAGE,EAASQ,EAAET,CAAS,EAAIO,CAC5B,CACD,CAKAf,EAAO,QAAUI,IClJjB,IAAAe,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAgB,QAAS,oCAAqC,EAC9DC,EAAU,IA+Bd,SAASC,EAASC,EAAGC,EAAOC,EAAGC,EAAU,CACxC,OAAOL,EAASE,EAAGC,EAAOC,EAAGC,EAASN,EAAeG,EAAGG,CAAQ,CAAE,CACnE,CAKAP,EAAO,QAAUG,IC7DjB,IAAAK,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAc,QAAS,uDAAwD,EAC/EC,EAAU,IACVC,EAAU,IAKdF,EAAaC,EAAS,UAAWC,CAAQ,EAKzCH,EAAO,QAAUE,ICYjB,IAAIE,EAAO,QAAS,MAAO,EAAE,KACzBC,EAAa,QAAS,2BAA4B,EAClDC,EAAU,QAAS,yBAA0B,EAC7CC,EAAO,IAKPC,EACAC,EAAMJ,EAAYD,EAAM,UAAW,aAAc,CAAE,EAClDE,EAASG,CAAI,EACjBD,EAAUD,EAEVC,EAAUC,EAMX,OAAO,QAAUD",
|
|
6
|
+
"names": ["require_ndarray", "__commonJSMin", "exports", "module", "isPositiveZero", "isnan", "floor", "dsorthp", "N", "order", "x", "strideX", "offsetX", "parent", "child", "v1", "v2", "n", "t", "i", "j", "k", "require_dsorthp", "__commonJSMin", "exports", "module", "stride2offset", "ndarray", "dsorthp", "N", "order", "x", "strideX", "require_main", "__commonJSMin", "exports", "module", "setReadOnly", "dsorthp", "ndarray", "join", "tryRequire", "isError", "main", "dsorthp", "tmp"]
|
|
7
7
|
}
|
package/docs/types/index.d.ts
CHANGED
|
@@ -28,7 +28,7 @@ interface Routine {
|
|
|
28
28
|
* @param N - number of indexed elements
|
|
29
29
|
* @param order - sort order
|
|
30
30
|
* @param x - input array
|
|
31
|
-
* @param
|
|
31
|
+
* @param strideX - stride length
|
|
32
32
|
* @returns `x`
|
|
33
33
|
*
|
|
34
34
|
* @example
|
|
@@ -39,7 +39,7 @@ interface Routine {
|
|
|
39
39
|
* dsorthp( x.length, 1, x, 1 );
|
|
40
40
|
* // x => <Float64Array>[ -4.0, -2.0, 1.0, 3.0 ]
|
|
41
41
|
*/
|
|
42
|
-
( N: number, order: number, x: Float64Array,
|
|
42
|
+
( N: number, order: number, x: Float64Array, strideX: number ): Float64Array;
|
|
43
43
|
|
|
44
44
|
/**
|
|
45
45
|
* Sorts a double-precision floating-point strided array using heapsort and alternative indexing semantics.
|
|
@@ -47,8 +47,8 @@ interface Routine {
|
|
|
47
47
|
* @param N - number of indexed elements
|
|
48
48
|
* @param order - sort order
|
|
49
49
|
* @param x - input array
|
|
50
|
-
* @param
|
|
51
|
-
* @param
|
|
50
|
+
* @param strideX - stride length
|
|
51
|
+
* @param offsetX - starting index
|
|
52
52
|
* @returns `x`
|
|
53
53
|
*
|
|
54
54
|
* @example
|
|
@@ -59,7 +59,7 @@ interface Routine {
|
|
|
59
59
|
* dsorthp.ndarray( x.length, 1, x, 1, 0 );
|
|
60
60
|
* // x => <Float64Array>[ -4.0, -2.0, 1.0, 3.0 ]
|
|
61
61
|
*/
|
|
62
|
-
ndarray( N: number, order: number, x: Float64Array,
|
|
62
|
+
ndarray( N: number, order: number, x: Float64Array, strideX: number, offsetX: number ): Float64Array;
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
/**
|
|
@@ -68,7 +68,7 @@ interface Routine {
|
|
|
68
68
|
* @param N - number of indexed elements
|
|
69
69
|
* @param order - sort order
|
|
70
70
|
* @param x - input array
|
|
71
|
-
* @param
|
|
71
|
+
* @param strideX - stride length
|
|
72
72
|
* @returns `x`
|
|
73
73
|
*
|
|
74
74
|
* @example
|
|
@@ -16,13 +16,10 @@
|
|
|
16
16
|
* limitations under the License.
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
|
-
/**
|
|
20
|
-
* Header file containing function declarations.
|
|
21
|
-
*/
|
|
22
19
|
#ifndef STDLIB_BLAS_EXT_BASE_DSORTHP_H
|
|
23
20
|
#define STDLIB_BLAS_EXT_BASE_DSORTHP_H
|
|
24
21
|
|
|
25
|
-
#include
|
|
22
|
+
#include "stdlib/blas/base/shared.h"
|
|
26
23
|
|
|
27
24
|
/*
|
|
28
25
|
* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
|
|
@@ -34,7 +31,12 @@ extern "C" {
|
|
|
34
31
|
/**
|
|
35
32
|
* Sorts a double-precision floating-point strided array using heapsort.
|
|
36
33
|
*/
|
|
37
|
-
void
|
|
34
|
+
void API_SUFFIX(stdlib_strided_dsorthp)( const CBLAS_INT N, const double order, double *X, const CBLAS_INT strideX );
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Sorts a double-precision floating-point strided array using heapsort and alternative indexing semantics.
|
|
38
|
+
*/
|
|
39
|
+
void API_SUFFIX(stdlib_strided_dsorthp_ndarray)( const CBLAS_INT N, const double order, double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
|
|
38
40
|
|
|
39
41
|
#ifdef __cplusplus
|
|
40
42
|
}
|
package/lib/dsorthp.js
CHANGED
|
@@ -20,9 +20,8 @@
|
|
|
20
20
|
|
|
21
21
|
// MODULES //
|
|
22
22
|
|
|
23
|
-
var
|
|
24
|
-
var
|
|
25
|
-
var floor = require( '@stdlib/math-base-special-floor' );
|
|
23
|
+
var stride2offset = require( '@stdlib/strided-base-stride2offset' );
|
|
24
|
+
var ndarray = require( './ndarray.js' );
|
|
26
25
|
|
|
27
26
|
|
|
28
27
|
// MAIN //
|
|
@@ -42,7 +41,7 @@ var floor = require( '@stdlib/math-base-special-floor' );
|
|
|
42
41
|
* @param {PositiveInteger} N - number of indexed elements
|
|
43
42
|
* @param {number} order - sort order
|
|
44
43
|
* @param {Float64Array} x - input array
|
|
45
|
-
* @param {integer}
|
|
44
|
+
* @param {integer} strideX - stride length
|
|
46
45
|
* @returns {Float64Array} input array
|
|
47
46
|
*
|
|
48
47
|
* @example
|
|
@@ -53,96 +52,8 @@ var floor = require( '@stdlib/math-base-special-floor' );
|
|
|
53
52
|
* dsorthp( x.length, 1.0, x, 1 );
|
|
54
53
|
* // x => <Float64Array>[ -4.0, -2.0, 1.0, 3.0 ]
|
|
55
54
|
*/
|
|
56
|
-
function dsorthp( N, order, x,
|
|
57
|
-
|
|
58
|
-
var parent;
|
|
59
|
-
var child;
|
|
60
|
-
var v1;
|
|
61
|
-
var v2;
|
|
62
|
-
var n;
|
|
63
|
-
var t;
|
|
64
|
-
var i;
|
|
65
|
-
var j;
|
|
66
|
-
var k;
|
|
67
|
-
|
|
68
|
-
if ( N <= 0 || order === 0.0 ) {
|
|
69
|
-
return x;
|
|
70
|
-
}
|
|
71
|
-
// 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...
|
|
72
|
-
if ( order < 0.0 ) {
|
|
73
|
-
stride *= -1;
|
|
74
|
-
}
|
|
75
|
-
if ( stride < 0 ) {
|
|
76
|
-
offset = (1-N) * stride;
|
|
77
|
-
} else {
|
|
78
|
-
offset = 0;
|
|
79
|
-
}
|
|
80
|
-
// Set the initial heap size:
|
|
81
|
-
n = N;
|
|
82
|
-
|
|
83
|
-
// Specify an initial "parent" index for building the heap:
|
|
84
|
-
parent = floor( N / 2 );
|
|
85
|
-
|
|
86
|
-
// Continue looping until the array is sorted...
|
|
87
|
-
while ( true ) {
|
|
88
|
-
if ( parent > 0 ) {
|
|
89
|
-
// We need to build the heap...
|
|
90
|
-
parent -= 1;
|
|
91
|
-
t = x[ offset+(parent*stride) ];
|
|
92
|
-
} else {
|
|
93
|
-
// Reduce the heap size:
|
|
94
|
-
n -= 1;
|
|
95
|
-
|
|
96
|
-
// Check if the heap is empty, and, if so, we are finished sorting...
|
|
97
|
-
if ( n === 0 ) {
|
|
98
|
-
return x;
|
|
99
|
-
}
|
|
100
|
-
// Store the last heap value in a temporary variable in order to make room for the heap root being placed into its sorted position:
|
|
101
|
-
i = offset + (n*stride);
|
|
102
|
-
t = x[ i ];
|
|
103
|
-
|
|
104
|
-
// Move the heap root to its sorted position:
|
|
105
|
-
x[ i ] = x[ offset ];
|
|
106
|
-
}
|
|
107
|
-
// We need to "sift down", pushing `t` down the heap to in order to replace the parent and satisfy the heap property...
|
|
108
|
-
|
|
109
|
-
// Start at the parent index:
|
|
110
|
-
j = parent;
|
|
111
|
-
|
|
112
|
-
// Get the "left" child index:
|
|
113
|
-
child = (j*2) + 1;
|
|
114
|
-
|
|
115
|
-
while ( child < n ) {
|
|
116
|
-
// Find the largest child...
|
|
117
|
-
k = child + 1;
|
|
118
|
-
if ( k < n ) {
|
|
119
|
-
v1 = x[ offset+(k*stride) ];
|
|
120
|
-
v2 = x[ offset+(child*stride) ];
|
|
121
|
-
|
|
122
|
-
// Check if a "right" child exists and is "bigger"...
|
|
123
|
-
if ( v1 > v2 || isnan( v1 ) || (v1 === v2 && isPositiveZero( v1 ) ) ) { // eslint-disable-line max-len
|
|
124
|
-
child += 1;
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
// Check if the largest child is bigger than `t`...
|
|
128
|
-
v1 = x[ offset+(child*stride) ];
|
|
129
|
-
if ( v1 > t || isnan( v1 ) || ( v1 === t && isPositiveZero( v1 ) ) ) { // eslint-disable-line max-len
|
|
130
|
-
// Insert the larger child value:
|
|
131
|
-
x[ offset+(j*stride) ] = v1;
|
|
132
|
-
|
|
133
|
-
// Update `j` to point to the child index:
|
|
134
|
-
j = child;
|
|
135
|
-
|
|
136
|
-
// Get the "left" child index and repeat...
|
|
137
|
-
child = (j*2) + 1;
|
|
138
|
-
} else {
|
|
139
|
-
// We've found `t`'s place in the heap...
|
|
140
|
-
break;
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
// Insert `t` into the heap:
|
|
144
|
-
x[ offset+(j*stride) ] = t;
|
|
145
|
-
}
|
|
55
|
+
function dsorthp( N, order, x, strideX ) {
|
|
56
|
+
return ndarray( N, order, x, strideX, stride2offset( N, strideX ) );
|
|
146
57
|
}
|
|
147
58
|
|
|
148
59
|
|
package/lib/dsorthp.native.js
CHANGED
|
@@ -31,7 +31,7 @@ var addon = require( './../src/addon.node' );
|
|
|
31
31
|
* @param {PositiveInteger} N - number of indexed elements
|
|
32
32
|
* @param {number} order - sort order
|
|
33
33
|
* @param {Float64Array} x - input array
|
|
34
|
-
* @param {integer}
|
|
34
|
+
* @param {integer} strideX - stride length
|
|
35
35
|
* @returns {Float64Array} input array
|
|
36
36
|
*
|
|
37
37
|
* @example
|
|
@@ -42,8 +42,8 @@ var addon = require( './../src/addon.node' );
|
|
|
42
42
|
* dsorthp( x.length, 1.0, x, 1 );
|
|
43
43
|
* // x => <Float64Array>[ -4.0, -2.0, 1.0, 3.0 ]
|
|
44
44
|
*/
|
|
45
|
-
function dsorthp( N, order, x,
|
|
46
|
-
addon( N, order, x,
|
|
45
|
+
function dsorthp( N, order, x, strideX ) {
|
|
46
|
+
addon( N, order, x, strideX );
|
|
47
47
|
return x;
|
|
48
48
|
}
|
|
49
49
|
|
package/lib/ndarray.js
CHANGED
|
@@ -42,8 +42,8 @@ var floor = require( '@stdlib/math-base-special-floor' );
|
|
|
42
42
|
* @param {PositiveInteger} N - number of indexed elements
|
|
43
43
|
* @param {number} order - sort order
|
|
44
44
|
* @param {Float64Array} x - input array
|
|
45
|
-
* @param {integer}
|
|
46
|
-
* @param {NonNegativeInteger}
|
|
45
|
+
* @param {integer} strideX - stride length
|
|
46
|
+
* @param {NonNegativeInteger} offsetX - starting index
|
|
47
47
|
* @returns {Float64Array} input array
|
|
48
48
|
*
|
|
49
49
|
* @example
|
|
@@ -54,7 +54,7 @@ var floor = require( '@stdlib/math-base-special-floor' );
|
|
|
54
54
|
* dsorthp( x.length, 1.0, x, 1, 0 );
|
|
55
55
|
* // x => <Float64Array>[ -4.0, -2.0, 1.0, 3.0 ]
|
|
56
56
|
*/
|
|
57
|
-
function dsorthp( N, order, x,
|
|
57
|
+
function dsorthp( N, order, x, strideX, offsetX ) {
|
|
58
58
|
var parent;
|
|
59
59
|
var child;
|
|
60
60
|
var v1;
|
|
@@ -70,8 +70,8 @@ function dsorthp( N, order, x, stride, offset ) {
|
|
|
70
70
|
}
|
|
71
71
|
// 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...
|
|
72
72
|
if ( order < 0.0 ) {
|
|
73
|
-
|
|
74
|
-
|
|
73
|
+
strideX *= -1;
|
|
74
|
+
offsetX -= (N-1) * strideX;
|
|
75
75
|
}
|
|
76
76
|
// Set the initial heap size:
|
|
77
77
|
n = N;
|
|
@@ -84,7 +84,7 @@ function dsorthp( N, order, x, stride, offset ) {
|
|
|
84
84
|
if ( parent > 0 ) {
|
|
85
85
|
// We need to build the heap...
|
|
86
86
|
parent -= 1;
|
|
87
|
-
t = x[
|
|
87
|
+
t = x[ offsetX+(parent*strideX) ];
|
|
88
88
|
} else {
|
|
89
89
|
// Reduce the heap size:
|
|
90
90
|
n -= 1;
|
|
@@ -94,11 +94,11 @@ function dsorthp( N, order, x, stride, offset ) {
|
|
|
94
94
|
return x;
|
|
95
95
|
}
|
|
96
96
|
// Store the last heap value in a temporary variable in order to make room for the heap root being placed into its sorted position:
|
|
97
|
-
i =
|
|
97
|
+
i = offsetX + (n*strideX);
|
|
98
98
|
t = x[ i ];
|
|
99
99
|
|
|
100
100
|
// Move the heap root to its sorted position:
|
|
101
|
-
x[ i ] = x[
|
|
101
|
+
x[ i ] = x[ offsetX ];
|
|
102
102
|
}
|
|
103
103
|
// We need to "sift down", pushing `t` down the heap to in order to replace the parent and satisfy the heap property...
|
|
104
104
|
|
|
@@ -112,8 +112,8 @@ function dsorthp( N, order, x, stride, offset ) {
|
|
|
112
112
|
// Find the largest child...
|
|
113
113
|
k = child + 1;
|
|
114
114
|
if ( k < n ) {
|
|
115
|
-
v1 = x[
|
|
116
|
-
v2 = x[
|
|
115
|
+
v1 = x[ offsetX+(k*strideX) ];
|
|
116
|
+
v2 = x[ offsetX+(child*strideX) ];
|
|
117
117
|
|
|
118
118
|
// Check if a "right" child exists and is "bigger"...
|
|
119
119
|
if ( v1 > v2 || isnan( v1 ) || (v1 === v2 && isPositiveZero( v1 ) ) ) { // eslint-disable-line max-len
|
|
@@ -121,10 +121,10 @@ function dsorthp( N, order, x, stride, offset ) {
|
|
|
121
121
|
}
|
|
122
122
|
}
|
|
123
123
|
// Check if the largest child is bigger than `t`...
|
|
124
|
-
v1 = x[
|
|
124
|
+
v1 = x[ offsetX+(child*strideX) ];
|
|
125
125
|
if ( v1 > t || isnan( v1 ) || ( v1 === t && isPositiveZero( v1 ) ) ) { // eslint-disable-line max-len
|
|
126
126
|
// Insert the larger child value:
|
|
127
|
-
x[
|
|
127
|
+
x[ offsetX+(j*strideX) ] = v1;
|
|
128
128
|
|
|
129
129
|
// Update `j` to point to the child index:
|
|
130
130
|
j = child;
|
|
@@ -137,7 +137,7 @@ function dsorthp( N, order, x, stride, offset ) {
|
|
|
137
137
|
}
|
|
138
138
|
}
|
|
139
139
|
// Insert `t` into the heap:
|
|
140
|
-
x[
|
|
140
|
+
x[ offsetX+(j*strideX) ] = t;
|
|
141
141
|
}
|
|
142
142
|
}
|
|
143
143
|
|
package/lib/ndarray.native.js
CHANGED
|
@@ -20,9 +20,7 @@
|
|
|
20
20
|
|
|
21
21
|
// MODULES //
|
|
22
22
|
|
|
23
|
-
var
|
|
24
|
-
var offsetView = require( '@stdlib/strided-base-offset-view' );
|
|
25
|
-
var addon = require( './dsorthp.native.js' );
|
|
23
|
+
var addon = require( './../src/addon.node' );
|
|
26
24
|
|
|
27
25
|
|
|
28
26
|
// MAIN //
|
|
@@ -33,8 +31,8 @@ var addon = require( './dsorthp.native.js' );
|
|
|
33
31
|
* @param {PositiveInteger} N - number of indexed elements
|
|
34
32
|
* @param {number} order - sort order
|
|
35
33
|
* @param {Float64Array} x - input array
|
|
36
|
-
* @param {integer}
|
|
37
|
-
* @param {NonNegativeInteger}
|
|
34
|
+
* @param {integer} strideX - stride length
|
|
35
|
+
* @param {NonNegativeInteger} offsetX - starting index
|
|
38
36
|
* @returns {Float64Array} input array
|
|
39
37
|
*
|
|
40
38
|
* @example
|
|
@@ -44,16 +42,8 @@ var addon = require( './dsorthp.native.js' );
|
|
|
44
42
|
*
|
|
45
43
|
* dsorthp( x.length, 1.0, x, 1, 0 );
|
|
46
44
|
*/
|
|
47
|
-
function dsorthp( N, order, x,
|
|
48
|
-
|
|
49
|
-
offset = minViewBufferIndex( N, stride, offset );
|
|
50
|
-
if ( stride < 0 ) {
|
|
51
|
-
order *= -1.0;
|
|
52
|
-
stride *= -1;
|
|
53
|
-
}
|
|
54
|
-
view = offsetView( x, offset );
|
|
55
|
-
|
|
56
|
-
addon( N, order, view, stride );
|
|
45
|
+
function dsorthp( N, order, x, strideX, offsetX ) {
|
|
46
|
+
addon.ndarray( N, order, x, strideX, offsetX );
|
|
57
47
|
return x;
|
|
58
48
|
}
|
|
59
49
|
|
package/manifest.json
CHANGED
|
@@ -28,23 +28,57 @@
|
|
|
28
28
|
{
|
|
29
29
|
"task": "build",
|
|
30
30
|
"src": [
|
|
31
|
-
"./src/
|
|
31
|
+
"./src/main.c"
|
|
32
32
|
],
|
|
33
33
|
"include": [
|
|
34
34
|
"./include"
|
|
35
35
|
],
|
|
36
|
-
"libraries": [
|
|
37
|
-
"-lm"
|
|
38
|
-
],
|
|
36
|
+
"libraries": [],
|
|
39
37
|
"libpath": [],
|
|
40
38
|
"dependencies": [
|
|
41
|
-
"@stdlib/math-base-assert-is-nan",
|
|
42
|
-
"@stdlib/math-base-assert-is-positive-zero",
|
|
43
39
|
"@stdlib/napi-export",
|
|
44
40
|
"@stdlib/napi-argv",
|
|
45
|
-
"@stdlib/napi-argv-double",
|
|
46
41
|
"@stdlib/napi-argv-int64",
|
|
47
|
-
"@stdlib/napi-argv-
|
|
42
|
+
"@stdlib/napi-argv-double",
|
|
43
|
+
"@stdlib/napi-argv-strided-float64array",
|
|
44
|
+
"@stdlib/math-base-assert-is-nan",
|
|
45
|
+
"@stdlib/math-base-assert-is-positive-zero",
|
|
46
|
+
"@stdlib/strided-base-stride2offset",
|
|
47
|
+
"@stdlib/blas-base-shared"
|
|
48
|
+
]
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
"task": "benchmark",
|
|
52
|
+
"src": [
|
|
53
|
+
"./src/main.c"
|
|
54
|
+
],
|
|
55
|
+
"include": [
|
|
56
|
+
"./include"
|
|
57
|
+
],
|
|
58
|
+
"libraries": [],
|
|
59
|
+
"libpath": [],
|
|
60
|
+
"dependencies": [
|
|
61
|
+
"@stdlib/math-base-assert-is-nan",
|
|
62
|
+
"@stdlib/math-base-assert-is-positive-zero",
|
|
63
|
+
"@stdlib/strided-base-stride2offset",
|
|
64
|
+
"@stdlib/blas-base-shared"
|
|
65
|
+
]
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
"task": "examples",
|
|
69
|
+
"src": [
|
|
70
|
+
"./src/main.c"
|
|
71
|
+
],
|
|
72
|
+
"include": [
|
|
73
|
+
"./include"
|
|
74
|
+
],
|
|
75
|
+
"libraries": [],
|
|
76
|
+
"libpath": [],
|
|
77
|
+
"dependencies": [
|
|
78
|
+
"@stdlib/math-base-assert-is-nan",
|
|
79
|
+
"@stdlib/math-base-assert-is-positive-zero",
|
|
80
|
+
"@stdlib/strided-base-stride2offset",
|
|
81
|
+
"@stdlib/blas-base-shared"
|
|
48
82
|
]
|
|
49
83
|
}
|
|
50
84
|
]
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stdlib/blas-ext-base-dsorthp",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Sort a double-precision floating-point strided array using heapsort.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": {
|
|
@@ -35,6 +35,7 @@
|
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
37
|
"@stdlib/assert-is-error": "^0.2.2",
|
|
38
|
+
"@stdlib/blas-base-shared": "^0.1.0",
|
|
38
39
|
"@stdlib/math-base-assert-is-nan": "^0.2.2",
|
|
39
40
|
"@stdlib/math-base-assert-is-positive-zero": "^0.2.2",
|
|
40
41
|
"@stdlib/math-base-special-floor": "^0.2.3",
|
|
@@ -42,9 +43,10 @@
|
|
|
42
43
|
"@stdlib/napi-argv-double": "^0.2.1",
|
|
43
44
|
"@stdlib/napi-argv-int64": "^0.2.2",
|
|
44
45
|
"@stdlib/napi-argv-strided-float64array": "^0.2.2",
|
|
45
|
-
"@stdlib/napi-export": "^0.
|
|
46
|
+
"@stdlib/napi-export": "^0.3.0",
|
|
47
|
+
"@stdlib/strided-base-stride2offset": "^0.1.0",
|
|
46
48
|
"@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2",
|
|
47
|
-
"@stdlib/utils-library-manifest": "^0.2.
|
|
49
|
+
"@stdlib/utils-library-manifest": "^0.2.3",
|
|
48
50
|
"@stdlib/utils-try-require": "^0.2.2"
|
|
49
51
|
},
|
|
50
52
|
"devDependencies": {},
|
|
@@ -83,7 +85,6 @@
|
|
|
83
85
|
"double",
|
|
84
86
|
"float64array"
|
|
85
87
|
],
|
|
86
|
-
"__stdlib__": {},
|
|
87
88
|
"funding": {
|
|
88
89
|
"type": "opencollective",
|
|
89
90
|
"url": "https://opencollective.com/stdlib"
|
package/src/addon.c
CHANGED
|
@@ -35,10 +35,28 @@ static napi_value addon( napi_env env, napi_callback_info info ) {
|
|
|
35
35
|
STDLIB_NAPI_ARGV( env, info, argv, argc, 4 );
|
|
36
36
|
STDLIB_NAPI_ARGV_INT64( env, N, argv, 0 );
|
|
37
37
|
STDLIB_NAPI_ARGV_DOUBLE( env, order, argv, 1 );
|
|
38
|
-
STDLIB_NAPI_ARGV_INT64( env,
|
|
39
|
-
STDLIB_NAPI_ARGV_STRIDED_FLOAT64ARRAY( env, X, N,
|
|
40
|
-
|
|
38
|
+
STDLIB_NAPI_ARGV_INT64( env, strideX, argv, 3 );
|
|
39
|
+
STDLIB_NAPI_ARGV_STRIDED_FLOAT64ARRAY( env, X, N, strideX, argv, 2 );
|
|
40
|
+
API_SUFFIX(stdlib_strided_dsorthp)( N, order, X, strideX );
|
|
41
41
|
return NULL;
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
|
|
44
|
+
/**
|
|
45
|
+
* Receives JavaScript callback invocation data.
|
|
46
|
+
*
|
|
47
|
+
* @param env environment under which the function is invoked
|
|
48
|
+
* @param info callback data
|
|
49
|
+
* @return Node-API value
|
|
50
|
+
*/
|
|
51
|
+
static napi_value addon_method( napi_env env, napi_callback_info info ) {
|
|
52
|
+
STDLIB_NAPI_ARGV( env, info, argv, argc, 5 );
|
|
53
|
+
STDLIB_NAPI_ARGV_INT64( env, N, argv, 0 );
|
|
54
|
+
STDLIB_NAPI_ARGV_INT64( env, strideX, argv, 3 );
|
|
55
|
+
STDLIB_NAPI_ARGV_INT64( env, offsetX, argv, 4 );
|
|
56
|
+
STDLIB_NAPI_ARGV_DOUBLE( env, order, argv, 1 );
|
|
57
|
+
STDLIB_NAPI_ARGV_STRIDED_FLOAT64ARRAY( env, X, N, strideX, argv, 2 );
|
|
58
|
+
API_SUFFIX(stdlib_strided_dsorthp_ndarray)( N, order, X, strideX, offsetX );
|
|
59
|
+
return NULL;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
STDLIB_NAPI_MODULE_EXPORT_FCN_WITH_METHOD( addon, "ndarray", addon_method );
|
package/src/{dsorthp.c → main.c}
RENAMED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @license Apache-2.0
|
|
3
3
|
*
|
|
4
|
-
* Copyright (c)
|
|
4
|
+
* Copyright (c) 2025 The Stdlib Authors.
|
|
5
5
|
*
|
|
6
6
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
7
|
* you may not use this file except in compliance with the License.
|
|
@@ -19,8 +19,8 @@
|
|
|
19
19
|
#include "stdlib/blas/ext/base/dsorthp.h"
|
|
20
20
|
#include "stdlib/math/base/assert/is_positive_zero.h"
|
|
21
21
|
#include "stdlib/math/base/assert/is_nan.h"
|
|
22
|
-
#include
|
|
23
|
-
#include
|
|
22
|
+
#include "stdlib/blas/base/shared.h"
|
|
23
|
+
#include "stdlib/strided/base/stride2offset.h"
|
|
24
24
|
|
|
25
25
|
/**
|
|
26
26
|
* Sorts a double-precision floating-point strided array using heapsort.
|
|
@@ -34,20 +34,43 @@
|
|
|
34
34
|
* - 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).
|
|
35
35
|
* - 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).
|
|
36
36
|
*
|
|
37
|
-
* @param N
|
|
38
|
-
* @param order
|
|
39
|
-
* @param X
|
|
40
|
-
* @param stride
|
|
37
|
+
* @param N number of indexed elements
|
|
38
|
+
* @param order sort order
|
|
39
|
+
* @param X input array
|
|
40
|
+
* @param strideX stride length
|
|
41
41
|
*/
|
|
42
|
-
void
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
42
|
+
void API_SUFFIX(stdlib_strided_dsorthp)( const CBLAS_INT N, const double order, double *X, const CBLAS_INT strideX) {
|
|
43
|
+
CBLAS_INT ox = stdlib_strided_stride2offset( N, strideX );
|
|
44
|
+
API_SUFFIX(stdlib_strided_dsorthp_ndarray)( N, order, X, strideX, ox );
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Sorts a double-precision floating-point strided array using heapsort and alternative indexing semantics.
|
|
49
|
+
*
|
|
50
|
+
* ## Notes
|
|
51
|
+
*
|
|
52
|
+
* - This implementation uses an in-place algorithm derived from the work of Floyd (1964).
|
|
53
|
+
*
|
|
54
|
+
* ## References
|
|
55
|
+
*
|
|
56
|
+
* - 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).
|
|
57
|
+
* - 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).
|
|
58
|
+
*
|
|
59
|
+
* @param N number of indexed elements
|
|
60
|
+
* @param order sort order
|
|
61
|
+
* @param X input array
|
|
62
|
+
* @param strideX stride length
|
|
63
|
+
* @param offsetX starting index
|
|
64
|
+
*/
|
|
65
|
+
void API_SUFFIX(stdlib_strided_dsorthp_ndarray)( const CBLAS_INT N, const double order, double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX ) {
|
|
66
|
+
CBLAS_INT parent;
|
|
67
|
+
CBLAS_INT child;
|
|
68
|
+
CBLAS_INT ox;
|
|
69
|
+
CBLAS_INT sx;
|
|
70
|
+
CBLAS_INT n;
|
|
71
|
+
CBLAS_INT i;
|
|
72
|
+
CBLAS_INT j;
|
|
73
|
+
CBLAS_INT k;
|
|
51
74
|
double v1;
|
|
52
75
|
double v2;
|
|
53
76
|
double t;
|
|
@@ -57,27 +80,24 @@ void c_dsorthp( const int64_t N, const double order, double *X, const int64_t st
|
|
|
57
80
|
}
|
|
58
81
|
// 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...
|
|
59
82
|
if ( order < 0.0 ) {
|
|
60
|
-
sx = -
|
|
61
|
-
|
|
62
|
-
sx = stride;
|
|
63
|
-
}
|
|
64
|
-
if ( sx < 0 ) {
|
|
65
|
-
offset = (1-N) * sx;
|
|
83
|
+
sx = -strideX;
|
|
84
|
+
ox = offsetX - ( (N-1)*sx );
|
|
66
85
|
} else {
|
|
67
|
-
|
|
86
|
+
sx = strideX;
|
|
87
|
+
ox = offsetX;
|
|
68
88
|
}
|
|
69
89
|
// Set the initial heap size:
|
|
70
90
|
n = N;
|
|
71
91
|
|
|
72
92
|
// Specify an initial "parent" index for building the heap:
|
|
73
|
-
parent =
|
|
93
|
+
parent = N / 2;
|
|
74
94
|
|
|
75
95
|
// Continue looping until the array is sorted...
|
|
76
96
|
while ( true ) {
|
|
77
97
|
if ( parent > 0 ) {
|
|
78
98
|
// We need to build the heap...
|
|
79
99
|
parent -= 1;
|
|
80
|
-
t = X[
|
|
100
|
+
t = X[ ox+(parent*sx) ];
|
|
81
101
|
} else {
|
|
82
102
|
// Reduce the heap size:
|
|
83
103
|
n -= 1;
|
|
@@ -87,11 +107,11 @@ void c_dsorthp( const int64_t N, const double order, double *X, const int64_t st
|
|
|
87
107
|
return;
|
|
88
108
|
}
|
|
89
109
|
// Store the last heap value in a temporary variable in order to make room for the heap root being placed into its sorted position:
|
|
90
|
-
i =
|
|
110
|
+
i = ox + (n*sx);
|
|
91
111
|
t = X[ i ];
|
|
92
112
|
|
|
93
113
|
// Move the heap root to its sorted position:
|
|
94
|
-
X[ i ] = X[
|
|
114
|
+
X[ i ] = X[ ox ];
|
|
95
115
|
}
|
|
96
116
|
// We need to "sift down", pushing `t` down the heap to in order to replace the parent and satisfy the heap property...
|
|
97
117
|
|
|
@@ -105,8 +125,8 @@ void c_dsorthp( const int64_t N, const double order, double *X, const int64_t st
|
|
|
105
125
|
// Find the largest child...
|
|
106
126
|
k = child + 1;
|
|
107
127
|
if ( k < n ) {
|
|
108
|
-
v1 = X[
|
|
109
|
-
v2 = X[
|
|
128
|
+
v1 = X[ ox+(k*sx) ];
|
|
129
|
+
v2 = X[ ox+(child*sx) ];
|
|
110
130
|
|
|
111
131
|
// Check if a "right" child exists and is "bigger"...
|
|
112
132
|
if ( v1 > v2 || stdlib_base_is_nan( v1 ) || (v1 == v2 && stdlib_base_is_positive_zero( v1 ) ) ) {
|
|
@@ -114,10 +134,10 @@ void c_dsorthp( const int64_t N, const double order, double *X, const int64_t st
|
|
|
114
134
|
}
|
|
115
135
|
}
|
|
116
136
|
// Check if the largest child is bigger than `t`...
|
|
117
|
-
v1 = X[
|
|
137
|
+
v1 = X[ ox+(child*sx) ];
|
|
118
138
|
if ( v1 > t || stdlib_base_is_nan( v1 ) || ( v1 == t && stdlib_base_is_positive_zero( v1 ) ) ) {
|
|
119
139
|
// Insert the larger child value:
|
|
120
|
-
X[
|
|
140
|
+
X[ ox+(j*sx) ] = v1;
|
|
121
141
|
|
|
122
142
|
// Update `j` to point to the child index:
|
|
123
143
|
j = child;
|
|
@@ -130,6 +150,6 @@ void c_dsorthp( const int64_t N, const double order, double *X, const int64_t st
|
|
|
130
150
|
}
|
|
131
151
|
}
|
|
132
152
|
// Insert `t` into the heap:
|
|
133
|
-
X[
|
|
153
|
+
X[ ox+(j*sx) ] = t;
|
|
134
154
|
}
|
|
135
155
|
}
|