Skip to content

Commit bdc1c39

Browse files
committed
feat: add blas/ext/base/dsort
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: missing_dependencies - task: lint_c_examples status: missing_dependencies - task: lint_c_benchmarks status: missing_dependencies - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent caea326 commit bdc1c39

43 files changed

Lines changed: 6262 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 301 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,301 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# dsort
22+
23+
> Sort a double-precision floating-point strided array.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var dsort = require( '@stdlib/blas/ext/base/dsort' );
31+
```
32+
33+
#### dsort( N, order, x, strideX )
34+
35+
Sorts a double-precision floating-point strided array.
36+
37+
```javascript
38+
var Float64Array = require( '@stdlib/array/float64' );
39+
40+
var x = new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] );
41+
42+
dsort( x.length, 1.0, x, 1 );
43+
// x => <Float64Array>[ -4.0, -2.0, 1.0, 3.0 ]
44+
```
45+
46+
The function has the following parameters:
47+
48+
- **N**: number of indexed elements.
49+
- **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.
50+
- **x**: input [`Float64Array`][@stdlib/array/float64].
51+
- **strideX**: stride length.
52+
53+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to sort every other element:
54+
55+
```javascript
56+
var Float64Array = require( '@stdlib/array/float64' );
57+
58+
var x = new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] );
59+
60+
dsort( 2, -1.0, x, 2 );
61+
// x => <Float64Array>[ 3.0, -2.0, 1.0, -4.0 ]
62+
```
63+
64+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
65+
66+
```javascript
67+
var Float64Array = require( '@stdlib/array/float64' );
68+
69+
// Initial array...
70+
var x0 = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] );
71+
72+
// Create an offset view...
73+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
74+
75+
// Sort every other element...
76+
dsort( 2, -1.0, x1, 2 );
77+
// x0 => <Float64Array>[ 1.0, 4.0, 3.0, 2.0 ]
78+
```
79+
80+
#### dsort.ndarray( N, order, x, strideX, offsetX )
81+
82+
Sorts a double-precision floating-point strided array using alternative indexing semantics.
83+
84+
```javascript
85+
var Float64Array = require( '@stdlib/array/float64' );
86+
87+
var x = new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] );
88+
89+
dsort.ndarray( x.length, 1.0, x, 1, 0 );
90+
// x => <Float64Array>[ -4.0, -2.0, 1.0, 3.0 ]
91+
```
92+
93+
The function has the following additional parameters:
94+
95+
- **offsetX**: starting index.
96+
97+
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:
98+
99+
```javascript
100+
var Float64Array = require( '@stdlib/array/float64' );
101+
102+
var x = new Float64Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] );
103+
104+
dsort.ndarray( 3, 1.0, x, 1, 3 );
105+
// x => <Float64Array>[ 1.0, -2.0, 3.0, -6.0, -4.0, 5.0 ]
106+
```
107+
108+
</section>
109+
110+
<!-- /.usage -->
111+
112+
<section class="notes">
113+
114+
## Notes
115+
116+
- If `N <= 0` or `order == 0.0`, both functions return `x` unchanged.
117+
- The algorithm distinguishes between `-0` and `+0`. When sorted in increasing order, `-0` is sorted before `+0`. When sorted in decreasing order, `-0` is sorted after `+0`.
118+
- The algorithm sorts `NaN` values to the end. When sorted in increasing order, `NaN` values are sorted last. When sorted in decreasing order, `NaN` values are sorted first.
119+
- The algorithm has space complexity `O(1)` and time complexity `O(N log2 N)`.
120+
- The algorithm is **unstable**, meaning that the algorithm may change the order of strided array elements which are equal or equivalent (e.g., `NaN` values).
121+
- The input strided array is sorted **in-place** (i.e., the input strided array is **mutated**).
122+
123+
</section>
124+
125+
<!-- /.notes -->
126+
127+
<section class="examples">
128+
129+
## Examples
130+
131+
<!-- eslint no-undef: "error" -->
132+
133+
```javascript
134+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
135+
var dsort = require( '@stdlib/blas/ext/base/dsort' );
136+
137+
var x = discreteUniform( 10, -100, 100, {
138+
'dtype': 'float64'
139+
});
140+
console.log( x );
141+
142+
dsort( x.length, 1.0, x, 1 );
143+
console.log( x );
144+
```
145+
146+
</section>
147+
148+
<!-- /.examples -->
149+
150+
<!-- C interface documentation. -->
151+
152+
* * *
153+
154+
<section class="c">
155+
156+
## C APIs
157+
158+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
159+
160+
<section class="intro">
161+
162+
</section>
163+
164+
<!-- /.intro -->
165+
166+
<!-- C usage documentation. -->
167+
168+
<section class="usage">
169+
170+
### Usage
171+
172+
```c
173+
#include "stdlib/blas/ext/base/dsort.h"
174+
```
175+
176+
#### stdlib_strided_dsort( N, order, \*X, strideX )
177+
178+
Sorts a double-precision floating-point strided array.
179+
180+
```c
181+
const double x[] = { 1.0, 2.0, 3.0, 4.0 };
182+
183+
stdlib_strided_dsort( 4, 1.0, x, 1 );
184+
```
185+
186+
The function accepts the following arguments:
187+
188+
- **N**: `[in] CBLAS_INT` number of indexed elements.
189+
- **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.
190+
- **X**: `[inout] double*` input array.
191+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
192+
193+
```c
194+
void stdlib_strided_dsort( const CBLAS_INT N, const double order, double *X, const CBLAS_INT strideX );
195+
```
196+
197+
<!--lint disable maximum-heading-length-->
198+
199+
#### stdlib_strided_dsort_ndarray( N, order, \*X, strideX, offsetX )
200+
201+
<!--lint enable maximum-heading-length-->
202+
203+
Sorts a double-precision floating-point strided array using alternative indexing semantics.
204+
205+
```c
206+
const double x[] = { 1.0, 2.0, 3.0, 4.0 };
207+
208+
stdlib_strided_dsort_ndarray( 4, 1.0, x, 1, 0 );
209+
```
210+
211+
The function accepts the following arguments:
212+
213+
- **N**: `[in] CBLAS_INT` number of indexed elements.
214+
- **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.
215+
- **X**: `[inout] double*` input array.
216+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
217+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
218+
219+
```c
220+
void stdlib_strided_dsort_ndarray( const CBLAS_INT N, const double order, double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
221+
```
222+
223+
</section>
224+
225+
<!-- /.usage -->
226+
227+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
228+
229+
<section class="notes">
230+
231+
</section>
232+
233+
<!-- /.notes -->
234+
235+
<!-- C API usage examples. -->
236+
237+
<section class="examples">
238+
239+
### Examples
240+
241+
```c
242+
#include "stdlib/blas/ext/base/dsort.h"
243+
#include <stdio.h>
244+
245+
int main( void ) {
246+
// Create a strided array:
247+
double x[] = { 1.0, -2.0, 3.0, -4.0, 5.0, -6.0, 7.0, -8.0 };
248+
249+
// Specify the number of elements:
250+
const int N = 8;
251+
252+
// Specify the stride length:
253+
const int strideX = 1;
254+
255+
// Sort the array:
256+
stdlib_strided_dsort( N, 1.0, x, strideX );
257+
258+
// Print the result:
259+
for ( int i = 0; i < 8; i++ ) {
260+
printf( "x[ %i ] = %lf\n", i, x[ i ] );
261+
}
262+
}
263+
```
264+
265+
</section>
266+
267+
<!-- /.examples -->
268+
269+
</section>
270+
271+
<!-- /.c -->
272+
273+
<section class="references">
274+
275+
</section>
276+
277+
<!-- /.references -->
278+
279+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
280+
281+
<section class="related">
282+
283+
</section>
284+
285+
<!-- /.related -->
286+
287+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
288+
289+
<section class="links">
290+
291+
[@stdlib/array/float64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float64
292+
293+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
294+
295+
<!-- <related-links> -->
296+
297+
<!-- </related-links> -->
298+
299+
</section>
300+
301+
<!-- /.links -->

0 commit comments

Comments
 (0)