numo-narray-alt 0.9.14 → 0.10.1

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.
@@ -0,0 +1,80 @@
1
+ #ifndef NUMO_NARRAY_MH_S_CAST_H
2
+ #define NUMO_NARRAY_MH_S_CAST_H 1
3
+
4
+ #define DEF_CAST_ARRAY_FUNC(tDType, tNAryClass) \
5
+ static VALUE tDType##_cast_array(VALUE rary) { \
6
+ VALUE nary = na_s_new_like(tNAryClass, rary); \
7
+ narray_t* na; \
8
+ GetNArray(nary, na); \
9
+ if (na->size > 0) { \
10
+ tDType##_store_array(nary, rary); \
11
+ } \
12
+ return nary; \
13
+ }
14
+
15
+ #define DEF_NARRAY_S_CAST_METHOD_FUNC(tDType, tNAryClass) \
16
+ DEF_CAST_ARRAY_FUNC(tDType, tNAryClass) \
17
+ static VALUE tDType##_s_cast(VALUE type, VALUE obj) { \
18
+ if (rb_obj_class(obj) == tNAryClass) { \
19
+ return obj; \
20
+ } \
21
+ if (RTEST(rb_obj_is_kind_of(obj, rb_cNumeric))) { \
22
+ tDType x = m_num_to_data(obj); \
23
+ return tDType##_new_dim0(x); \
24
+ } \
25
+ if (RTEST(rb_obj_is_kind_of(obj, rb_cArray))) { \
26
+ return tDType##_cast_array(obj); \
27
+ } \
28
+ if (IsNArray(obj)) { \
29
+ narray_t* na; \
30
+ GetNArray(obj, na); \
31
+ VALUE v = nary_new(cT, NA_NDIM(na), NA_SHAPE(na)); \
32
+ if (NA_SIZE(na) > 0) { \
33
+ tDType##_store(v, obj); \
34
+ } \
35
+ return v; \
36
+ } \
37
+ if (rb_respond_to(obj, id_to_a)) { \
38
+ obj = rb_funcall(obj, id_to_a, 0); \
39
+ if (TYPE(obj) != T_ARRAY) { \
40
+ rb_raise(rb_eTypeError, "`to_a' did not return Array"); \
41
+ } \
42
+ return tDType##_cast_array(obj); \
43
+ } \
44
+ rb_raise(nary_eCastError, "cannot cast to %s", rb_class2name(type)); \
45
+ return Qnil; \
46
+ }
47
+
48
+ #define DEF_NARRAY_ROBJ_S_CAST_METHOD_FUNC() \
49
+ DEF_CAST_ARRAY_FUNC(robject, numo_cRObject) \
50
+ static VALUE robject_s_cast(VALUE type, VALUE obj) { \
51
+ if (rb_obj_class(obj) == numo_cRObject) { \
52
+ return obj; \
53
+ } \
54
+ if (RTEST(rb_obj_is_kind_of(obj, rb_cNumeric))) { \
55
+ robject x = m_num_to_data(obj); \
56
+ return robject_new_dim0(x); \
57
+ } \
58
+ if (RTEST(rb_obj_is_kind_of(obj, rb_cArray))) { \
59
+ return robject_cast_array(obj); \
60
+ } \
61
+ if (IsNArray(obj)) { \
62
+ narray_t* na; \
63
+ GetNArray(obj, na); \
64
+ VALUE v = nary_new(numo_cRObject, NA_NDIM(na), NA_SHAPE(na)); \
65
+ if (NA_SIZE(na) > 0) { \
66
+ robject_store(v, obj); \
67
+ } \
68
+ return v; \
69
+ } \
70
+ if (rb_respond_to(obj, id_to_a)) { \
71
+ obj = rb_funcall(obj, id_to_a, 0); \
72
+ if (TYPE(obj) != T_ARRAY) { \
73
+ rb_raise(rb_eTypeError, "`to_a' did not return Array"); \
74
+ } \
75
+ return robject_cast_array(obj); \
76
+ } \
77
+ return robject_new_dim0(obj); \
78
+ }
79
+
80
+ #endif /* NUMO_NARRAY_MH_S_CAST_H */
@@ -0,0 +1,484 @@
1
+ #ifndef NUMO_NARRAY_MH_SORT_H
2
+ #define NUMO_NARRAY_MH_SORT_H 1
3
+
4
+ /**
5
+ * qsort.c
6
+ * Ruby/Numo::NArray - Numerical Array class for Ruby
7
+ * modified by Masahiro TANAKA
8
+ */
9
+
10
+ /**
11
+ * qsort.c: standard quicksort algorithm
12
+ *
13
+ * Modifications from vanilla NetBSD source:
14
+ * Add do ... while() macro fix
15
+ * Remove __inline, _DIAGASSERTs, __P
16
+ * Remove ill-considered "swap_cnt" switch to insertion sort,
17
+ * in favor of a simple check for presorted input.
18
+ *
19
+ * CAUTION: if you change this file, see also qsort_arg.c
20
+ *
21
+ * $PostgreSQL: pgsql/src/port/qsort.c,v 1.12 2006/10/19 20:56:22 tgl Exp $
22
+ */
23
+
24
+ /**
25
+ * Copyright (c) 1992, 1993
26
+ * The Regents of the University of California. All rights reserved.
27
+ *
28
+ * Redistribution and use in source and binary forms, with or without
29
+ * modification, are permitted provided that the following conditions
30
+ * are met:
31
+ * 1. Redistributions of source code must retain the above copyright
32
+ * notice, this list of conditions and the following disclaimer.
33
+ * 2. Redistributions in binary form must reproduce the above copyright
34
+ * notice, this list of conditions and the following disclaimer in the
35
+ * documentation and/or other materials provided with the distribution.
36
+ * 3. Neither the name of the University nor the names of its contributors
37
+ * may be used to endorse or promote products derived from this software
38
+ * without specific prior written permission.
39
+ *
40
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
41
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
44
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50
+ * SUCH DAMAGE.
51
+ */
52
+
53
+ #ifndef QSORT_INCL
54
+ #define QSORT_INCL
55
+ #define Min(x, y) ((x) < (y) ? (x) : (y))
56
+
57
+ /**
58
+ * Qsort routine based on J. L. Bentley and M. D. McIlroy,
59
+ * "Engineering a sort function",
60
+ * Software--Practice and Experience 23 (1993) 1249-1265.
61
+ * We have modified their original by adding a check for already-sorted input,
62
+ * which seems to be a win per discussions on pgsql-hackers around 2006-03-21.
63
+ */
64
+ #define swapcode(TYPE, parmi, parmj, n) \
65
+ do { \
66
+ size_t i = (n) / sizeof(TYPE); \
67
+ TYPE* pi = (TYPE*)(void*)(parmi); \
68
+ TYPE* pj = (TYPE*)(void*)(parmj); \
69
+ do { \
70
+ TYPE t = *pi; \
71
+ *pi++ = *pj; \
72
+ *pj++ = t; \
73
+ } while (--i > 0); \
74
+ } while (0)
75
+
76
+ #ifdef HAVE_STDINT_H
77
+ #define SWAPINIT(a, es) \
78
+ swaptype = (uintptr_t)(a) % sizeof(long) || (es) % sizeof(long) ? 2 \
79
+ : (es) == sizeof(long) ? 0 \
80
+ : 1;
81
+ #else
82
+ #define SWAPINIT(a, es) \
83
+ swaptype = ((char*)(a) - (char*)0) % sizeof(long) || (es) % sizeof(long) ? 2 \
84
+ : (es) == sizeof(long) ? 0 \
85
+ : 1;
86
+ #endif
87
+
88
+ static inline void swapfunc(char* a, char* b, size_t n, int swaptype) {
89
+ if (swaptype <= 1)
90
+ swapcode(long, a, b, n);
91
+ else
92
+ swapcode(char, a, b, n);
93
+ }
94
+
95
+ #define swap(a, b) \
96
+ if (swaptype == 0) { \
97
+ long t = *(long*)(void*)(a); \
98
+ *(long*)(void*)(a) = *(long*)(void*)(b); \
99
+ *(long*)(void*)(b) = t; \
100
+ } else \
101
+ swapfunc(a, b, es, swaptype)
102
+
103
+ #define vecswap(a, b, n) \
104
+ if ((n) > 0) swapfunc((a), (b), (size_t)(n), swaptype)
105
+
106
+ #define med3(a, b, c, _cmpgt) \
107
+ (_cmpgt(b, a) ? (_cmpgt(c, b) ? b : (_cmpgt(c, a) ? c : a)) \
108
+ : (_cmpgt(b, c) ? b : (_cmpgt(c, a) ? a : c)))
109
+ #endif
110
+
111
+ #define DEF_TYPED_QSORT_FUNC(tDType, fQsort, fCmp, fCmpGt) \
112
+ static void tDType##_##fQsort(void* a, size_t n, ssize_t es) { \
113
+ char *pa, *pb, *pc, *pd, *pl, *pm, *pn; \
114
+ int d, r, swaptype, presorted; \
115
+ \
116
+ loop: \
117
+ SWAPINIT(a, es); \
118
+ if (n < 7) { \
119
+ for (pm = (char*)a + es; pm < (char*)a + n * es; pm += es) \
120
+ for (pl = pm; pl > (char*)a && fCmpGt(pl - es, pl); pl -= es) swap(pl, pl - es); \
121
+ return; \
122
+ } \
123
+ presorted = 1; \
124
+ for (pm = (char*)a + es; pm < (char*)a + n * es; pm += es) { \
125
+ if (fCmpGt(pm - es, pm)) { \
126
+ presorted = 0; \
127
+ break; \
128
+ } \
129
+ } \
130
+ if (presorted) return; \
131
+ pm = (char*)a + (n / 2) * es; \
132
+ if (n > 7) { \
133
+ pl = (char*)a; \
134
+ pn = (char*)a + (n - 1) * es; \
135
+ if (n > 40) { \
136
+ d = (int)((n / 8) * es); \
137
+ pl = med3(pl, pl + d, pl + 2 * d, fCmpGt); \
138
+ pm = med3(pm - d, pm, pm + d, fCmpGt); \
139
+ pn = med3(pn - 2 * d, pn - d, pn, fCmpGt); \
140
+ } \
141
+ pm = med3(pl, pm, pn, fCmpGt); \
142
+ } \
143
+ swap(a, pm); \
144
+ for (pa = pb = (char*)a + es, pc = pd = (char*)a + (n - 1) * es; pb <= pc; \
145
+ pb += es, pc -= es) { \
146
+ while (pb <= pc && (r = fCmp(pb, a)) <= 0) { \
147
+ if (r == 0) { \
148
+ swap(pa, pb); \
149
+ pa += es; \
150
+ } \
151
+ pb += es; \
152
+ } \
153
+ while (pb <= pc && (r = fCmp(pc, a)) >= 0) { \
154
+ if (r == 0) { \
155
+ swap(pc, pd); \
156
+ pd -= es; \
157
+ } \
158
+ pc -= es; \
159
+ } \
160
+ if (pb > pc) break; \
161
+ swap(pb, pc); \
162
+ } \
163
+ pn = (char*)a + n * es; \
164
+ r = (int)Min(pa - (char*)a, pb - pa); \
165
+ vecswap(a, pb - r, r); \
166
+ r = (int)Min(pd - pc, pn - pd - es); \
167
+ vecswap(pb, pn - r, r); \
168
+ if ((r = (int)(pb - pa)) > es) tDType##_##fQsort(a, r / es, es); \
169
+ if ((r = (int)(pd - pc)) > es) { \
170
+ a = pn - r; \
171
+ n = r / es; \
172
+ goto loop; \
173
+ } \
174
+ }
175
+
176
+ #define DEF_NARRAY_INT_SORT_METHOD_FUNC(tDType) \
177
+ DEF_TYPED_QSORT_FUNC(tDType, qsort, cmp, cmpgt) \
178
+ \
179
+ static void iter_##tDType##_sort(na_loop_t* const lp) { \
180
+ size_t n; \
181
+ char* ptr; \
182
+ ssize_t step; \
183
+ INIT_COUNTER(lp, n); \
184
+ INIT_PTR(lp, 0, ptr, step); \
185
+ tDType##_qsort(ptr, n, step); \
186
+ } \
187
+ \
188
+ static VALUE tDType##_sort(int argc, VALUE* argv, VALUE self) { \
189
+ if (!TEST_INPLACE(self)) { \
190
+ self = na_copy(self); \
191
+ } \
192
+ ndfunc_arg_in_t ain[2] = { { OVERWRITE, 0 }, { sym_reduce, 0 } }; \
193
+ ndfunc_t ndf = { iter_##tDType##_sort, NDF_HAS_LOOP | NDF_FLAT_REDUCE, 2, 0, ain, 0 }; \
194
+ VALUE reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0); \
195
+ na_ndloop(&ndf, 2, self, reduce); \
196
+ return self; \
197
+ }
198
+
199
+ #define DEF_NARRAY_FLT_SORT_METHOD_FUNC(tDType) \
200
+ DEF_TYPED_QSORT_FUNC(tDType, qsort_prnan, cmp_prnan, cmpgt_prnan) \
201
+ \
202
+ static void iter_##tDType##_sort_prnan(na_loop_t* const lp) { \
203
+ size_t n; \
204
+ char* ptr; \
205
+ ssize_t step; \
206
+ INIT_COUNTER(lp, n); \
207
+ INIT_PTR(lp, 0, ptr, step); \
208
+ tDType##_qsort_prnan(ptr, n, step); \
209
+ } \
210
+ \
211
+ DEF_TYPED_QSORT_FUNC(tDType, qsort_ignan, cmp_ignan, cmpgt_ignan) \
212
+ \
213
+ static void iter_##tDType##_sort_ignan(na_loop_t* const lp) { \
214
+ size_t n; \
215
+ char* ptr; \
216
+ ssize_t step; \
217
+ INIT_COUNTER(lp, n); \
218
+ INIT_PTR(lp, 0, ptr, step); \
219
+ tDType##_qsort_ignan(ptr, n, step); \
220
+ } \
221
+ \
222
+ static VALUE tDType##_sort(int argc, VALUE* argv, VALUE self) { \
223
+ if (!TEST_INPLACE(self)) { \
224
+ self = na_copy(self); \
225
+ } \
226
+ ndfunc_arg_in_t ain[2] = { { OVERWRITE, 0 }, { sym_reduce, 0 } }; \
227
+ ndfunc_t ndf = { \
228
+ iter_##tDType##_sort_ignan, NDF_HAS_LOOP | NDF_FLAT_REDUCE, 2, 0, ain, 0 \
229
+ }; \
230
+ VALUE reduce = \
231
+ na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_##tDType##_sort_prnan); \
232
+ na_ndloop(&ndf, 2, self, reduce); \
233
+ return self; \
234
+ }
235
+
236
+ #define DEF_NARRAY_INT_SORT_INDEX_METHOD_FUNC(tDType, tNAryClass) \
237
+ DEF_TYPED_QSORT_FUNC(tDType, index_qsort, cmp, cmpgt) \
238
+ \
239
+ static void tDType##_index64_qsort(na_loop_t* const lp) { \
240
+ size_t n; \
241
+ char* d_ptr; \
242
+ char* i_ptr; \
243
+ char* o_ptr; \
244
+ ssize_t d_step; \
245
+ ssize_t i_step; \
246
+ ssize_t o_step; \
247
+ INIT_COUNTER(lp, n); \
248
+ INIT_PTR(lp, 0, d_ptr, d_step); \
249
+ INIT_PTR(lp, 1, i_ptr, i_step); \
250
+ INIT_PTR(lp, 2, o_ptr, o_step); \
251
+ if (n == 1) { \
252
+ *(int64_t*)o_ptr = *(int64_t*)(i_ptr); \
253
+ return; \
254
+ } \
255
+ char** ptr = (char**)(lp->opt_ptr); \
256
+ for (size_t i = 0; i < n; i++) { \
257
+ ptr[i] = d_ptr + d_step * i; \
258
+ } \
259
+ tDType##_index_qsort(ptr, n, sizeof(tDType*)); \
260
+ size_t idx; \
261
+ for (size_t i = 0; i < n; i++) { \
262
+ idx = (ptr[i] - d_ptr) / d_step; \
263
+ *(int64_t*)o_ptr = *(int64_t*)(i_ptr + i_step * idx); \
264
+ o_ptr += o_step; \
265
+ } \
266
+ } \
267
+ \
268
+ static void tDType##_index32_qsort(na_loop_t* const lp) { \
269
+ size_t n; \
270
+ char* d_ptr; \
271
+ char* i_ptr; \
272
+ char* o_ptr; \
273
+ ssize_t d_step; \
274
+ ssize_t i_step; \
275
+ ssize_t o_step; \
276
+ INIT_COUNTER(lp, n); \
277
+ INIT_PTR(lp, 0, d_ptr, d_step); \
278
+ INIT_PTR(lp, 1, i_ptr, i_step); \
279
+ INIT_PTR(lp, 2, o_ptr, o_step); \
280
+ if (n == 1) { \
281
+ *(int32_t*)o_ptr = *(int32_t*)(i_ptr); \
282
+ return; \
283
+ } \
284
+ char** ptr = (char**)(lp->opt_ptr); \
285
+ for (size_t i = 0; i < n; i++) { \
286
+ ptr[i] = d_ptr + d_step * i; \
287
+ } \
288
+ tDType##_index_qsort(ptr, n, sizeof(tDType*)); \
289
+ size_t idx; \
290
+ for (size_t i = 0; i < n; i++) { \
291
+ idx = (ptr[i] - d_ptr) / d_step; \
292
+ *(int32_t*)o_ptr = *(int32_t*)(i_ptr + i_step * idx); \
293
+ o_ptr += o_step; \
294
+ } \
295
+ } \
296
+ \
297
+ static VALUE tDType##_sort_index(int argc, VALUE* argv, VALUE self) { \
298
+ narray_t* na; \
299
+ GetNArray(self, na); \
300
+ if (na->ndim == 0) { \
301
+ return INT2FIX(0); \
302
+ } \
303
+ ndfunc_arg_in_t ain[3] = { { tNAryClass, 0 }, { 0, 0 }, { sym_reduce, 0 } }; \
304
+ ndfunc_arg_out_t aout[1] = { { 0, 0, 0 } }; \
305
+ ndfunc_t ndf = { 0, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE | NDF_CUM, 3, 1, ain, aout }; \
306
+ VALUE idx; \
307
+ VALUE reduce; \
308
+ if (na->size > (~(u_int32_t)0)) { \
309
+ ain[1].type = numo_cInt64; \
310
+ aout[0].type = numo_cInt64; \
311
+ idx = nary_new(numo_cInt64, na->ndim, na->shape); \
312
+ ndf.func = tDType##_index64_qsort; \
313
+ reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0); \
314
+ } else { \
315
+ ain[1].type = numo_cInt32; \
316
+ aout[0].type = numo_cInt32; \
317
+ idx = nary_new(numo_cInt32, na->ndim, na->shape); \
318
+ ndf.func = tDType##_index32_qsort; \
319
+ reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0); \
320
+ } \
321
+ rb_funcall(idx, rb_intern("seq"), 0); \
322
+ size_t size = na->size * sizeof(void*); \
323
+ VALUE tmp; \
324
+ char* buf = rb_alloc_tmp_buffer(&tmp, size); \
325
+ VALUE res = na_ndloop3(&ndf, buf, 3, self, idx, reduce); \
326
+ rb_free_tmp_buffer(&tmp); \
327
+ return res; \
328
+ }
329
+
330
+ #define DEF_NARRAY_FLT_SORT_INDEX_METHOD_FUNC(tDType, tNAryClass) \
331
+ DEF_TYPED_QSORT_FUNC(tDType, index_qsort_ignan, cmp_ignan, cmpgt_ignan) \
332
+ \
333
+ static void tDType##_index64_qsort_ignan(na_loop_t* const lp) { \
334
+ size_t n; \
335
+ char* d_ptr; \
336
+ char* i_ptr; \
337
+ char* o_ptr; \
338
+ ssize_t d_step; \
339
+ ssize_t i_step; \
340
+ ssize_t o_step; \
341
+ INIT_COUNTER(lp, n); \
342
+ INIT_PTR(lp, 0, d_ptr, d_step); \
343
+ INIT_PTR(lp, 1, i_ptr, i_step); \
344
+ INIT_PTR(lp, 2, o_ptr, o_step); \
345
+ if (n == 1) { \
346
+ *(int64_t*)o_ptr = *(int64_t*)(i_ptr); \
347
+ return; \
348
+ } \
349
+ char** ptr = (char**)(lp->opt_ptr); \
350
+ for (size_t i = 0; i < n; i++) { \
351
+ ptr[i] = d_ptr + d_step * i; \
352
+ } \
353
+ tDType##_index_qsort_ignan(ptr, n, sizeof(tDType*)); \
354
+ size_t idx; \
355
+ for (size_t i = 0; i < n; i++) { \
356
+ idx = (ptr[i] - d_ptr) / d_step; \
357
+ *(int64_t*)o_ptr = *(int64_t*)(i_ptr + i_step * idx); \
358
+ o_ptr += o_step; \
359
+ } \
360
+ } \
361
+ \
362
+ static void tDType##_index32_qsort_ignan(na_loop_t* const lp) { \
363
+ size_t n; \
364
+ char* d_ptr; \
365
+ char* i_ptr; \
366
+ char* o_ptr; \
367
+ ssize_t d_step; \
368
+ ssize_t i_step; \
369
+ ssize_t o_step; \
370
+ INIT_COUNTER(lp, n); \
371
+ INIT_PTR(lp, 0, d_ptr, d_step); \
372
+ INIT_PTR(lp, 1, i_ptr, i_step); \
373
+ INIT_PTR(lp, 2, o_ptr, o_step); \
374
+ if (n == 1) { \
375
+ *(int32_t*)o_ptr = *(int32_t*)(i_ptr); \
376
+ return; \
377
+ } \
378
+ char** ptr = (char**)(lp->opt_ptr); \
379
+ for (size_t i = 0; i < n; i++) { \
380
+ ptr[i] = d_ptr + d_step * i; \
381
+ } \
382
+ tDType##_index_qsort_ignan(ptr, n, sizeof(tDType*)); \
383
+ size_t idx; \
384
+ for (size_t i = 0; i < n; i++) { \
385
+ idx = (ptr[i] - d_ptr) / d_step; \
386
+ *(int32_t*)o_ptr = *(int32_t*)(i_ptr + i_step * idx); \
387
+ o_ptr += o_step; \
388
+ } \
389
+ } \
390
+ \
391
+ DEF_TYPED_QSORT_FUNC(tDType, index_qsort_prnan, cmp_prnan, cmpgt_prnan) \
392
+ \
393
+ static void tDType##_index64_qsort_prnan(na_loop_t* const lp) { \
394
+ size_t n; \
395
+ char* d_ptr; \
396
+ char* i_ptr; \
397
+ char* o_ptr; \
398
+ ssize_t d_step; \
399
+ ssize_t i_step; \
400
+ ssize_t o_step; \
401
+ INIT_COUNTER(lp, n); \
402
+ INIT_PTR(lp, 0, d_ptr, d_step); \
403
+ INIT_PTR(lp, 1, i_ptr, i_step); \
404
+ INIT_PTR(lp, 2, o_ptr, o_step); \
405
+ if (n == 1) { \
406
+ *(int64_t*)o_ptr = *(int64_t*)(i_ptr); \
407
+ return; \
408
+ } \
409
+ char** ptr = (char**)(lp->opt_ptr); \
410
+ for (size_t i = 0; i < n; i++) { \
411
+ ptr[i] = d_ptr + d_step * i; \
412
+ } \
413
+ tDType##_index_qsort_prnan(ptr, n, sizeof(tDType*)); \
414
+ size_t idx; \
415
+ for (size_t i = 0; i < n; i++) { \
416
+ idx = (ptr[i] - d_ptr) / d_step; \
417
+ *(int64_t*)o_ptr = *(int64_t*)(i_ptr + i_step * idx); \
418
+ o_ptr += o_step; \
419
+ } \
420
+ } \
421
+ \
422
+ static void tDType##_index32_qsort_prnan(na_loop_t* const lp) { \
423
+ size_t n; \
424
+ char* d_ptr; \
425
+ char* i_ptr; \
426
+ char* o_ptr; \
427
+ ssize_t d_step; \
428
+ ssize_t i_step; \
429
+ ssize_t o_step; \
430
+ INIT_COUNTER(lp, n); \
431
+ INIT_PTR(lp, 0, d_ptr, d_step); \
432
+ INIT_PTR(lp, 1, i_ptr, i_step); \
433
+ INIT_PTR(lp, 2, o_ptr, o_step); \
434
+ if (n == 1) { \
435
+ *(int32_t*)o_ptr = *(int32_t*)(i_ptr); \
436
+ return; \
437
+ } \
438
+ char** ptr = (char**)(lp->opt_ptr); \
439
+ for (size_t i = 0; i < n; i++) { \
440
+ ptr[i] = d_ptr + d_step * i; \
441
+ } \
442
+ tDType##_index_qsort_prnan(ptr, n, sizeof(tDType*)); \
443
+ size_t idx; \
444
+ for (size_t i = 0; i < n; i++) { \
445
+ idx = (ptr[i] - d_ptr) / d_step; \
446
+ *(int32_t*)o_ptr = *(int32_t*)(i_ptr + i_step * idx); \
447
+ o_ptr += o_step; \
448
+ } \
449
+ } \
450
+ \
451
+ static VALUE tDType##_sort_index(int argc, VALUE* argv, VALUE self) { \
452
+ narray_t* na; \
453
+ GetNArray(self, na); \
454
+ if (na->ndim == 0) { \
455
+ return INT2FIX(0); \
456
+ } \
457
+ ndfunc_arg_in_t ain[3] = { { tNAryClass, 0 }, { 0, 0 }, { sym_reduce, 0 } }; \
458
+ ndfunc_arg_out_t aout[1] = { { 0, 0, 0 } }; \
459
+ ndfunc_t ndf = { 0, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE | NDF_CUM, 3, 1, ain, aout }; \
460
+ VALUE idx; \
461
+ VALUE reduce; \
462
+ if (na->size > (~(u_int32_t)0)) { \
463
+ ain[1].type = numo_cInt64; \
464
+ aout[0].type = numo_cInt64; \
465
+ idx = nary_new(numo_cInt64, na->ndim, na->shape); \
466
+ ndf.func = tDType##_index64_qsort_ignan; \
467
+ reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, tDType##_index64_qsort_prnan); \
468
+ } else { \
469
+ ain[1].type = numo_cInt32; \
470
+ aout[0].type = numo_cInt32; \
471
+ idx = nary_new(numo_cInt32, na->ndim, na->shape); \
472
+ ndf.func = tDType##_index32_qsort_ignan; \
473
+ reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, tDType##_index32_qsort_prnan); \
474
+ } \
475
+ rb_funcall(idx, rb_intern("seq"), 0); \
476
+ size_t size = na->size * sizeof(void*); \
477
+ VALUE tmp; \
478
+ char* buf = rb_alloc_tmp_buffer(&tmp, size); \
479
+ VALUE res = na_ndloop3(&ndf, buf, 3, self, idx, reduce); \
480
+ rb_free_tmp_buffer(&tmp); \
481
+ return res; \
482
+ }
483
+
484
+ #endif /* NUMO_NARRAY_MH_SORT_H */