Skip to content

Commit 7a58757

Browse files
committed
feat: add internal C utilities
--- 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: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: passed - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 4b2a5de commit 7a58757

File tree

3 files changed

+147
-0
lines changed

3 files changed

+147
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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+
#include "stdlib/ndarray/base/every/internal/permute.h"
20+
#include <stdint.h>
21+
22+
/**
23+
* Permutes an input array according to a provided index array.
24+
*
25+
* @param n number of elements to permute
26+
* @param arr input array
27+
* @param idx permutation indices
28+
* @param out output array
29+
*/
30+
void stdlib_ndarray_base_every_internal_permute( const int64_t n, const int64_t *arr, const int64_t *idx, int64_t *out ) {
31+
int64_t i;
32+
for ( i = 0; i < n; i++ ) {
33+
out[ i ] = arr[ idx[i] ];
34+
}
35+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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+
#include "stdlib/ndarray/base/every/internal/range.h"
20+
#include <stdint.h>
21+
22+
/**
23+
* Writes `n` evenly spaced values from `0` to `n-1` to an output array.
24+
*
25+
* @param n number of values to write
26+
* @param out output array
27+
*/
28+
void stdlib_ndarray_base_every_internal_range( const int64_t n, int64_t *out ) {
29+
int64_t i;
30+
for ( i = 0; i < n; i++ ) {
31+
out[ i ] = i;
32+
}
33+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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+
#include "stdlib/ndarray/base/every/internal/sort2ins.h"
20+
#include <stdint.h>
21+
22+
/**
23+
* Simultaneously sorts two arrays based on the sort order of the first array using insertion sort.
24+
*
25+
* ## Notes
26+
*
27+
* - The first array is sorted in increasing order according to absolute value.
28+
* - The algorithm has space complexity `O(1)` and worst case time complexity `O(N^2)`.
29+
* - The algorithm is efficient for small arrays (typically `N <= 20``) and is particularly efficient for sorting arrays which are already substantially sorted.
30+
* - The algorithm is **stable**, meaning that the algorithm does **not** change the order of array elements which are equal or equivalent.
31+
* - The input arrays are sorted in-place (i.e., the input arrays are mutated).
32+
*
33+
* @param n number of elements
34+
* @param x first array
35+
* @param y second array
36+
*/
37+
void stdlib_ndarray_base_every_internal_sort2ins( const int64_t n, int64_t *x, int64_t *y ) {
38+
int64_t avx;
39+
int64_t aux;
40+
int64_t ix;
41+
int64_t iy;
42+
int64_t jx;
43+
int64_t jy;
44+
int64_t vx;
45+
int64_t vy;
46+
int64_t ux;
47+
int64_t i;
48+
49+
ix = 1;
50+
iy = 1;
51+
52+
// Sort in increasing order...
53+
for ( i = 1; i < n; i++ ) {
54+
vx = x[ ix ];
55+
avx = ( vx < 0 ) ? -vx : vx;
56+
57+
vy = y[ iy ];
58+
59+
jx = ix - 1;
60+
jy = iy - 1;
61+
62+
// Shift all larger values to the left of the current element to the right...
63+
while ( jx >= 0 ) {
64+
ux = x[ jx ];
65+
aux = ( ux < 0 ) ? -ux : ux;
66+
if ( aux <= avx ) {
67+
break;
68+
}
69+
x[ jx+1 ] = ux;
70+
y[ jy+1 ] = y[ jy ];
71+
jx -= 1;
72+
jy -= 1;
73+
}
74+
x[ jx+1 ] = vx;
75+
y[ jy+1 ] = vy;
76+
ix += 1;
77+
iy += 1;
78+
}
79+
}

0 commit comments

Comments
 (0)