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.
@@ -43,8 +43,11 @@ static ID id_to_a;
43
43
  VALUE cT;
44
44
  extern VALUE cRT;
45
45
 
46
+ #include "mh/store.h"
47
+ #include "mh/s_cast.h"
46
48
  #include "mh/extract.h"
47
49
  #include "mh/aref.h"
50
+ #include "mh/aset.h"
48
51
  #include "mh/coerce_cast.h"
49
52
  #include "mh/to_a.h"
50
53
  #include "mh/fill.h"
@@ -100,6 +103,8 @@ extern VALUE cRT;
100
103
  #include "mh/eye.h"
101
104
  #include "mh/rand.h"
102
105
  #include "mh/poly.h"
106
+ #include "mh/sort.h"
107
+ #include "mh/median.h"
103
108
  #include "mh/mean.h"
104
109
  #include "mh/var.h"
105
110
  #include "mh/stddev.h"
@@ -107,8 +112,12 @@ extern VALUE cRT;
107
112
 
108
113
  typedef int16_t int16; // Type aliases for shorter notation
109
114
  // following the codebase naming convention.
115
+ DEF_NARRAY_STORE_METHOD_FUNC(int16, numo_cInt16)
116
+ DEF_NARRAY_S_CAST_METHOD_FUNC(int16, numo_cInt16)
110
117
  DEF_NARRAY_EXTRACT_METHOD_FUNC(int16)
111
118
  DEF_NARRAY_AREF_METHOD_FUNC(int16)
119
+ DEF_EXTRACT_DATA_FUNC(int16, numo_cInt16)
120
+ DEF_NARRAY_ASET_METHOD_FUNC(int16)
112
121
  DEF_NARRAY_COERCE_CAST_METHOD_FUNC(int16)
113
122
  DEF_NARRAY_TO_A_METHOD_FUNC(int16)
114
123
  DEF_NARRAY_FILL_METHOD_FUNC(int16)
@@ -164,13 +173,22 @@ DEF_NARRAY_INT_SEQ_METHOD_FUNC(int16)
164
173
  DEF_NARRAY_EYE_METHOD_FUNC(int16)
165
174
  DEF_NARRAY_INT_RAND_METHOD_FUNC(int16)
166
175
  DEF_NARRAY_POLY_METHOD_FUNC(int16, numo_cInt16)
176
+ #undef qsort_dtype
177
+ #define qsort_dtype int16
178
+ #undef qsort_cast
179
+ #define qsort_cast *(int16*)
180
+ DEF_NARRAY_INT_SORT_METHOD_FUNC(int16)
181
+ #undef qsort_dtype
182
+ #define qsort_dtype int16*
183
+ #undef qsort_cast
184
+ #define qsort_cast **(int16**)
185
+ DEF_NARRAY_INT_SORT_INDEX_METHOD_FUNC(int16, numo_cInt16)
186
+ DEF_NARRAY_INT_MEDIAN_METHOD_FUNC(int16)
167
187
  DEF_NARRAY_INT_MEAN_METHOD_FUNC(int16, numo_cInt16)
168
188
  DEF_NARRAY_INT_VAR_METHOD_FUNC(int16, numo_cInt16)
169
189
  DEF_NARRAY_INT_STDDEV_METHOD_FUNC(int16, numo_cInt16)
170
190
  DEF_NARRAY_INT_RMS_METHOD_FUNC(int16, numo_cInt16)
171
191
 
172
- static VALUE int16_store(VALUE, VALUE);
173
-
174
192
  static size_t int16_memsize(const void* ptr) {
175
193
  size_t size = sizeof(narray_data_t);
176
194
  const narray_data_t* na = (const narray_data_t*)ptr;
@@ -200,11 +218,9 @@ static void int16_free(void* ptr) {
200
218
  }
201
219
  na->ptr = NULL;
202
220
  }
203
- if (na->base.size > 0) {
204
- if (na->base.shape != NULL && na->base.shape != &(na->base.size)) {
205
- xfree(na->base.shape);
206
- na->base.shape = NULL;
207
- }
221
+ if (na->base.shape != NULL && na->base.shape != &(na->base.size)) {
222
+ xfree(na->base.shape);
223
+ na->base.shape = NULL;
208
224
  }
209
225
  xfree(na);
210
226
  }
@@ -272,1519 +288,6 @@ static VALUE int16_allocate(VALUE self) {
272
288
  return self;
273
289
  }
274
290
 
275
- static VALUE int16_new_dim0(dtype x) {
276
- VALUE v;
277
- dtype* ptr;
278
-
279
- v = nary_new(cT, 0, NULL);
280
- ptr = (dtype*)(char*)na_get_pointer_for_write(v);
281
- *ptr = x;
282
- na_release_lock(v);
283
- return v;
284
- }
285
-
286
- static VALUE int16_store_numeric(VALUE self, VALUE obj) {
287
- dtype x;
288
- x = m_num_to_data(obj);
289
- obj = int16_new_dim0(x);
290
- int16_store(self, obj);
291
- return self;
292
- }
293
-
294
- static void iter_int16_store_bit(na_loop_t* const lp) {
295
- size_t i;
296
- char* p1;
297
- size_t p2;
298
- ssize_t s1, s2;
299
- size_t *idx1, *idx2;
300
- BIT_DIGIT *a2, x;
301
- dtype y;
302
-
303
- INIT_COUNTER(lp, i);
304
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
305
- INIT_PTR_BIT_IDX(lp, 1, a2, p2, s2, idx2);
306
- if (idx2) {
307
- if (idx1) {
308
- for (; i--;) {
309
- LOAD_BIT(a2, p2 + *idx2, x);
310
- idx2++;
311
- y = m_from_sint(x);
312
- SET_DATA_INDEX(p1, idx1, dtype, y);
313
- }
314
- } else {
315
- for (; i--;) {
316
- LOAD_BIT(a2, p2 + *idx2, x);
317
- idx2++;
318
- y = m_from_sint(x);
319
- SET_DATA_STRIDE(p1, s1, dtype, y);
320
- }
321
- }
322
- } else {
323
- if (idx1) {
324
- for (; i--;) {
325
- LOAD_BIT(a2, p2, x);
326
- p2 += s2;
327
- y = m_from_sint(x);
328
- SET_DATA_INDEX(p1, idx1, dtype, y);
329
- }
330
- } else {
331
- for (; i--;) {
332
- LOAD_BIT(a2, p2, x);
333
- p2 += s2;
334
- y = m_from_sint(x);
335
- SET_DATA_STRIDE(p1, s1, dtype, y);
336
- }
337
- }
338
- }
339
- }
340
-
341
- static VALUE int16_store_bit(VALUE self, VALUE obj) {
342
- ndfunc_arg_in_t ain[2] = { { OVERWRITE, 0 }, { Qnil, 0 } };
343
- ndfunc_t ndf = { iter_int16_store_bit, FULL_LOOP, 2, 0, ain, 0 };
344
-
345
- na_ndloop(&ndf, 2, self, obj);
346
- return self;
347
- }
348
-
349
- static void iter_int16_store_dfloat(na_loop_t* const lp) {
350
- size_t i, s1, s2;
351
- char *p1, *p2;
352
- size_t *idx1, *idx2;
353
- double x;
354
- dtype y;
355
-
356
- INIT_COUNTER(lp, i);
357
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
358
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
359
- if (idx2) {
360
- if (idx1) {
361
- for (; i--;) {
362
- GET_DATA_INDEX(p2, idx2, double, x);
363
- y = m_from_real(x);
364
- SET_DATA_INDEX(p1, idx1, dtype, y);
365
- }
366
- } else {
367
- for (; i--;) {
368
- GET_DATA_INDEX(p2, idx2, double, x);
369
- y = m_from_real(x);
370
- SET_DATA_STRIDE(p1, s1, dtype, y);
371
- }
372
- }
373
- } else {
374
- if (idx1) {
375
- for (; i--;) {
376
- GET_DATA_STRIDE(p2, s2, double, x);
377
- y = m_from_real(x);
378
- SET_DATA_INDEX(p1, idx1, dtype, y);
379
- }
380
- } else {
381
- for (; i--;) {
382
- GET_DATA_STRIDE(p2, s2, double, x);
383
- y = m_from_real(x);
384
- SET_DATA_STRIDE(p1, s1, dtype, y);
385
- }
386
- }
387
- }
388
- }
389
-
390
- static VALUE int16_store_dfloat(VALUE self, VALUE obj) {
391
- ndfunc_arg_in_t ain[2] = { { OVERWRITE, 0 }, { Qnil, 0 } };
392
- ndfunc_t ndf = { iter_int16_store_dfloat, FULL_LOOP, 2, 0, ain, 0 };
393
-
394
- na_ndloop(&ndf, 2, self, obj);
395
- return self;
396
- }
397
-
398
- static void iter_int16_store_sfloat(na_loop_t* const lp) {
399
- size_t i, s1, s2;
400
- char *p1, *p2;
401
- size_t *idx1, *idx2;
402
- float x;
403
- dtype y;
404
-
405
- INIT_COUNTER(lp, i);
406
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
407
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
408
- if (idx2) {
409
- if (idx1) {
410
- for (; i--;) {
411
- GET_DATA_INDEX(p2, idx2, float, x);
412
- y = m_from_real(x);
413
- SET_DATA_INDEX(p1, idx1, dtype, y);
414
- }
415
- } else {
416
- for (; i--;) {
417
- GET_DATA_INDEX(p2, idx2, float, x);
418
- y = m_from_real(x);
419
- SET_DATA_STRIDE(p1, s1, dtype, y);
420
- }
421
- }
422
- } else {
423
- if (idx1) {
424
- for (; i--;) {
425
- GET_DATA_STRIDE(p2, s2, float, x);
426
- y = m_from_real(x);
427
- SET_DATA_INDEX(p1, idx1, dtype, y);
428
- }
429
- } else {
430
- for (; i--;) {
431
- GET_DATA_STRIDE(p2, s2, float, x);
432
- y = m_from_real(x);
433
- SET_DATA_STRIDE(p1, s1, dtype, y);
434
- }
435
- }
436
- }
437
- }
438
-
439
- static VALUE int16_store_sfloat(VALUE self, VALUE obj) {
440
- ndfunc_arg_in_t ain[2] = { { OVERWRITE, 0 }, { Qnil, 0 } };
441
- ndfunc_t ndf = { iter_int16_store_sfloat, FULL_LOOP, 2, 0, ain, 0 };
442
-
443
- na_ndloop(&ndf, 2, self, obj);
444
- return self;
445
- }
446
-
447
- static void iter_int16_store_int64(na_loop_t* const lp) {
448
- size_t i, s1, s2;
449
- char *p1, *p2;
450
- size_t *idx1, *idx2;
451
- int64_t x;
452
- dtype y;
453
-
454
- INIT_COUNTER(lp, i);
455
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
456
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
457
- if (idx2) {
458
- if (idx1) {
459
- for (; i--;) {
460
- GET_DATA_INDEX(p2, idx2, int64_t, x);
461
- y = m_from_int64(x);
462
- SET_DATA_INDEX(p1, idx1, dtype, y);
463
- }
464
- } else {
465
- for (; i--;) {
466
- GET_DATA_INDEX(p2, idx2, int64_t, x);
467
- y = m_from_int64(x);
468
- SET_DATA_STRIDE(p1, s1, dtype, y);
469
- }
470
- }
471
- } else {
472
- if (idx1) {
473
- for (; i--;) {
474
- GET_DATA_STRIDE(p2, s2, int64_t, x);
475
- y = m_from_int64(x);
476
- SET_DATA_INDEX(p1, idx1, dtype, y);
477
- }
478
- } else {
479
- for (; i--;) {
480
- GET_DATA_STRIDE(p2, s2, int64_t, x);
481
- y = m_from_int64(x);
482
- SET_DATA_STRIDE(p1, s1, dtype, y);
483
- }
484
- }
485
- }
486
- }
487
-
488
- static VALUE int16_store_int64(VALUE self, VALUE obj) {
489
- ndfunc_arg_in_t ain[2] = { { OVERWRITE, 0 }, { Qnil, 0 } };
490
- ndfunc_t ndf = { iter_int16_store_int64, FULL_LOOP, 2, 0, ain, 0 };
491
-
492
- na_ndloop(&ndf, 2, self, obj);
493
- return self;
494
- }
495
-
496
- static void iter_int16_store_int32(na_loop_t* const lp) {
497
- size_t i, s1, s2;
498
- char *p1, *p2;
499
- size_t *idx1, *idx2;
500
- int32_t x;
501
- dtype y;
502
-
503
- INIT_COUNTER(lp, i);
504
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
505
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
506
- if (idx2) {
507
- if (idx1) {
508
- for (; i--;) {
509
- GET_DATA_INDEX(p2, idx2, int32_t, x);
510
- y = m_from_int32(x);
511
- SET_DATA_INDEX(p1, idx1, dtype, y);
512
- }
513
- } else {
514
- for (; i--;) {
515
- GET_DATA_INDEX(p2, idx2, int32_t, x);
516
- y = m_from_int32(x);
517
- SET_DATA_STRIDE(p1, s1, dtype, y);
518
- }
519
- }
520
- } else {
521
- if (idx1) {
522
- for (; i--;) {
523
- GET_DATA_STRIDE(p2, s2, int32_t, x);
524
- y = m_from_int32(x);
525
- SET_DATA_INDEX(p1, idx1, dtype, y);
526
- }
527
- } else {
528
- for (; i--;) {
529
- GET_DATA_STRIDE(p2, s2, int32_t, x);
530
- y = m_from_int32(x);
531
- SET_DATA_STRIDE(p1, s1, dtype, y);
532
- }
533
- }
534
- }
535
- }
536
-
537
- static VALUE int16_store_int32(VALUE self, VALUE obj) {
538
- ndfunc_arg_in_t ain[2] = { { OVERWRITE, 0 }, { Qnil, 0 } };
539
- ndfunc_t ndf = { iter_int16_store_int32, FULL_LOOP, 2, 0, ain, 0 };
540
-
541
- na_ndloop(&ndf, 2, self, obj);
542
- return self;
543
- }
544
-
545
- static void iter_int16_store_int16(na_loop_t* const lp) {
546
- size_t i, s1, s2;
547
- char *p1, *p2;
548
- size_t *idx1, *idx2;
549
- int16_t x;
550
- dtype y;
551
-
552
- INIT_COUNTER(lp, i);
553
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
554
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
555
- if (idx2) {
556
- if (idx1) {
557
- for (; i--;) {
558
- GET_DATA_INDEX(p2, idx2, int16_t, x);
559
- y = m_from_sint(x);
560
- SET_DATA_INDEX(p1, idx1, dtype, y);
561
- }
562
- } else {
563
- for (; i--;) {
564
- GET_DATA_INDEX(p2, idx2, int16_t, x);
565
- y = m_from_sint(x);
566
- SET_DATA_STRIDE(p1, s1, dtype, y);
567
- }
568
- }
569
- } else {
570
- if (idx1) {
571
- for (; i--;) {
572
- GET_DATA_STRIDE(p2, s2, int16_t, x);
573
- y = m_from_sint(x);
574
- SET_DATA_INDEX(p1, idx1, dtype, y);
575
- }
576
- } else {
577
- for (; i--;) {
578
- GET_DATA_STRIDE(p2, s2, int16_t, x);
579
- y = m_from_sint(x);
580
- SET_DATA_STRIDE(p1, s1, dtype, y);
581
- }
582
- }
583
- }
584
- }
585
-
586
- static VALUE int16_store_int16(VALUE self, VALUE obj) {
587
- ndfunc_arg_in_t ain[2] = { { OVERWRITE, 0 }, { Qnil, 0 } };
588
- ndfunc_t ndf = { iter_int16_store_int16, FULL_LOOP, 2, 0, ain, 0 };
589
-
590
- na_ndloop(&ndf, 2, self, obj);
591
- return self;
592
- }
593
-
594
- static void iter_int16_store_int8(na_loop_t* const lp) {
595
- size_t i, s1, s2;
596
- char *p1, *p2;
597
- size_t *idx1, *idx2;
598
- int8_t x;
599
- dtype y;
600
-
601
- INIT_COUNTER(lp, i);
602
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
603
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
604
- if (idx2) {
605
- if (idx1) {
606
- for (; i--;) {
607
- GET_DATA_INDEX(p2, idx2, int8_t, x);
608
- y = m_from_sint(x);
609
- SET_DATA_INDEX(p1, idx1, dtype, y);
610
- }
611
- } else {
612
- for (; i--;) {
613
- GET_DATA_INDEX(p2, idx2, int8_t, x);
614
- y = m_from_sint(x);
615
- SET_DATA_STRIDE(p1, s1, dtype, y);
616
- }
617
- }
618
- } else {
619
- if (idx1) {
620
- for (; i--;) {
621
- GET_DATA_STRIDE(p2, s2, int8_t, x);
622
- y = m_from_sint(x);
623
- SET_DATA_INDEX(p1, idx1, dtype, y);
624
- }
625
- } else {
626
- for (; i--;) {
627
- GET_DATA_STRIDE(p2, s2, int8_t, x);
628
- y = m_from_sint(x);
629
- SET_DATA_STRIDE(p1, s1, dtype, y);
630
- }
631
- }
632
- }
633
- }
634
-
635
- static VALUE int16_store_int8(VALUE self, VALUE obj) {
636
- ndfunc_arg_in_t ain[2] = { { OVERWRITE, 0 }, { Qnil, 0 } };
637
- ndfunc_t ndf = { iter_int16_store_int8, FULL_LOOP, 2, 0, ain, 0 };
638
-
639
- na_ndloop(&ndf, 2, self, obj);
640
- return self;
641
- }
642
-
643
- static void iter_int16_store_uint64(na_loop_t* const lp) {
644
- size_t i, s1, s2;
645
- char *p1, *p2;
646
- size_t *idx1, *idx2;
647
- u_int64_t x;
648
- dtype y;
649
-
650
- INIT_COUNTER(lp, i);
651
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
652
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
653
- if (idx2) {
654
- if (idx1) {
655
- for (; i--;) {
656
- GET_DATA_INDEX(p2, idx2, u_int64_t, x);
657
- y = m_from_uint64(x);
658
- SET_DATA_INDEX(p1, idx1, dtype, y);
659
- }
660
- } else {
661
- for (; i--;) {
662
- GET_DATA_INDEX(p2, idx2, u_int64_t, x);
663
- y = m_from_uint64(x);
664
- SET_DATA_STRIDE(p1, s1, dtype, y);
665
- }
666
- }
667
- } else {
668
- if (idx1) {
669
- for (; i--;) {
670
- GET_DATA_STRIDE(p2, s2, u_int64_t, x);
671
- y = m_from_uint64(x);
672
- SET_DATA_INDEX(p1, idx1, dtype, y);
673
- }
674
- } else {
675
- for (; i--;) {
676
- GET_DATA_STRIDE(p2, s2, u_int64_t, x);
677
- y = m_from_uint64(x);
678
- SET_DATA_STRIDE(p1, s1, dtype, y);
679
- }
680
- }
681
- }
682
- }
683
-
684
- static VALUE int16_store_uint64(VALUE self, VALUE obj) {
685
- ndfunc_arg_in_t ain[2] = { { OVERWRITE, 0 }, { Qnil, 0 } };
686
- ndfunc_t ndf = { iter_int16_store_uint64, FULL_LOOP, 2, 0, ain, 0 };
687
-
688
- na_ndloop(&ndf, 2, self, obj);
689
- return self;
690
- }
691
-
692
- static void iter_int16_store_uint32(na_loop_t* const lp) {
693
- size_t i, s1, s2;
694
- char *p1, *p2;
695
- size_t *idx1, *idx2;
696
- u_int32_t x;
697
- dtype y;
698
-
699
- INIT_COUNTER(lp, i);
700
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
701
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
702
- if (idx2) {
703
- if (idx1) {
704
- for (; i--;) {
705
- GET_DATA_INDEX(p2, idx2, u_int32_t, x);
706
- y = m_from_uint32(x);
707
- SET_DATA_INDEX(p1, idx1, dtype, y);
708
- }
709
- } else {
710
- for (; i--;) {
711
- GET_DATA_INDEX(p2, idx2, u_int32_t, x);
712
- y = m_from_uint32(x);
713
- SET_DATA_STRIDE(p1, s1, dtype, y);
714
- }
715
- }
716
- } else {
717
- if (idx1) {
718
- for (; i--;) {
719
- GET_DATA_STRIDE(p2, s2, u_int32_t, x);
720
- y = m_from_uint32(x);
721
- SET_DATA_INDEX(p1, idx1, dtype, y);
722
- }
723
- } else {
724
- for (; i--;) {
725
- GET_DATA_STRIDE(p2, s2, u_int32_t, x);
726
- y = m_from_uint32(x);
727
- SET_DATA_STRIDE(p1, s1, dtype, y);
728
- }
729
- }
730
- }
731
- }
732
-
733
- static VALUE int16_store_uint32(VALUE self, VALUE obj) {
734
- ndfunc_arg_in_t ain[2] = { { OVERWRITE, 0 }, { Qnil, 0 } };
735
- ndfunc_t ndf = { iter_int16_store_uint32, FULL_LOOP, 2, 0, ain, 0 };
736
-
737
- na_ndloop(&ndf, 2, self, obj);
738
- return self;
739
- }
740
-
741
- static void iter_int16_store_uint16(na_loop_t* const lp) {
742
- size_t i, s1, s2;
743
- char *p1, *p2;
744
- size_t *idx1, *idx2;
745
- u_int16_t x;
746
- dtype y;
747
-
748
- INIT_COUNTER(lp, i);
749
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
750
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
751
- if (idx2) {
752
- if (idx1) {
753
- for (; i--;) {
754
- GET_DATA_INDEX(p2, idx2, u_int16_t, x);
755
- y = m_from_sint(x);
756
- SET_DATA_INDEX(p1, idx1, dtype, y);
757
- }
758
- } else {
759
- for (; i--;) {
760
- GET_DATA_INDEX(p2, idx2, u_int16_t, x);
761
- y = m_from_sint(x);
762
- SET_DATA_STRIDE(p1, s1, dtype, y);
763
- }
764
- }
765
- } else {
766
- if (idx1) {
767
- for (; i--;) {
768
- GET_DATA_STRIDE(p2, s2, u_int16_t, x);
769
- y = m_from_sint(x);
770
- SET_DATA_INDEX(p1, idx1, dtype, y);
771
- }
772
- } else {
773
- for (; i--;) {
774
- GET_DATA_STRIDE(p2, s2, u_int16_t, x);
775
- y = m_from_sint(x);
776
- SET_DATA_STRIDE(p1, s1, dtype, y);
777
- }
778
- }
779
- }
780
- }
781
-
782
- static VALUE int16_store_uint16(VALUE self, VALUE obj) {
783
- ndfunc_arg_in_t ain[2] = { { OVERWRITE, 0 }, { Qnil, 0 } };
784
- ndfunc_t ndf = { iter_int16_store_uint16, FULL_LOOP, 2, 0, ain, 0 };
785
-
786
- na_ndloop(&ndf, 2, self, obj);
787
- return self;
788
- }
789
-
790
- static void iter_int16_store_uint8(na_loop_t* const lp) {
791
- size_t i, s1, s2;
792
- char *p1, *p2;
793
- size_t *idx1, *idx2;
794
- u_int8_t x;
795
- dtype y;
796
-
797
- INIT_COUNTER(lp, i);
798
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
799
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
800
- if (idx2) {
801
- if (idx1) {
802
- for (; i--;) {
803
- GET_DATA_INDEX(p2, idx2, u_int8_t, x);
804
- y = m_from_sint(x);
805
- SET_DATA_INDEX(p1, idx1, dtype, y);
806
- }
807
- } else {
808
- for (; i--;) {
809
- GET_DATA_INDEX(p2, idx2, u_int8_t, x);
810
- y = m_from_sint(x);
811
- SET_DATA_STRIDE(p1, s1, dtype, y);
812
- }
813
- }
814
- } else {
815
- if (idx1) {
816
- for (; i--;) {
817
- GET_DATA_STRIDE(p2, s2, u_int8_t, x);
818
- y = m_from_sint(x);
819
- SET_DATA_INDEX(p1, idx1, dtype, y);
820
- }
821
- } else {
822
- for (; i--;) {
823
- GET_DATA_STRIDE(p2, s2, u_int8_t, x);
824
- y = m_from_sint(x);
825
- SET_DATA_STRIDE(p1, s1, dtype, y);
826
- }
827
- }
828
- }
829
- }
830
-
831
- static VALUE int16_store_uint8(VALUE self, VALUE obj) {
832
- ndfunc_arg_in_t ain[2] = { { OVERWRITE, 0 }, { Qnil, 0 } };
833
- ndfunc_t ndf = { iter_int16_store_uint8, FULL_LOOP, 2, 0, ain, 0 };
834
-
835
- na_ndloop(&ndf, 2, self, obj);
836
- return self;
837
- }
838
-
839
- static void iter_int16_store_robject(na_loop_t* const lp) {
840
- size_t i, s1, s2;
841
- char *p1, *p2;
842
- size_t *idx1, *idx2;
843
- VALUE x;
844
- dtype y;
845
-
846
- INIT_COUNTER(lp, i);
847
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
848
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
849
- if (idx2) {
850
- if (idx1) {
851
- for (; i--;) {
852
- GET_DATA_INDEX(p2, idx2, VALUE, x);
853
- y = m_num_to_data(x);
854
- SET_DATA_INDEX(p1, idx1, dtype, y);
855
- }
856
- } else {
857
- for (; i--;) {
858
- GET_DATA_INDEX(p2, idx2, VALUE, x);
859
- y = m_num_to_data(x);
860
- SET_DATA_STRIDE(p1, s1, dtype, y);
861
- }
862
- }
863
- } else {
864
- if (idx1) {
865
- for (; i--;) {
866
- GET_DATA_STRIDE(p2, s2, VALUE, x);
867
- y = m_num_to_data(x);
868
- SET_DATA_INDEX(p1, idx1, dtype, y);
869
- }
870
- } else {
871
- for (; i--;) {
872
- GET_DATA_STRIDE(p2, s2, VALUE, x);
873
- y = m_num_to_data(x);
874
- SET_DATA_STRIDE(p1, s1, dtype, y);
875
- }
876
- }
877
- }
878
- }
879
-
880
- static VALUE int16_store_robject(VALUE self, VALUE obj) {
881
- ndfunc_arg_in_t ain[2] = { { OVERWRITE, 0 }, { Qnil, 0 } };
882
- ndfunc_t ndf = { iter_int16_store_robject, FULL_LOOP, 2, 0, ain, 0 };
883
-
884
- na_ndloop(&ndf, 2, self, obj);
885
- return self;
886
- }
887
-
888
- static void iter_int16_store_array(na_loop_t* const lp) {
889
- size_t i, n;
890
- size_t i1, n1;
891
- VALUE v1, *ptr;
892
- char* p1;
893
- size_t s1, *idx1;
894
- VALUE x;
895
- double y;
896
- dtype z;
897
- size_t len, c;
898
- double beg, step;
899
-
900
- INIT_COUNTER(lp, n);
901
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
902
- v1 = lp->args[1].value;
903
- i = 0;
904
-
905
- if (lp->args[1].ptr) {
906
- if (v1 == Qtrue) {
907
- iter_int16_store_int16(lp);
908
- i = lp->args[1].shape[0];
909
- if (idx1) {
910
- idx1 += i;
911
- } else {
912
- p1 += s1 * i;
913
- }
914
- }
915
- goto loop_end;
916
- }
917
-
918
- ptr = &v1;
919
-
920
- switch (TYPE(v1)) {
921
- case T_ARRAY:
922
- n1 = RARRAY_LEN(v1);
923
- ptr = RARRAY_PTR(v1);
924
- break;
925
- case T_NIL:
926
- n1 = 0;
927
- break;
928
- default:
929
- n1 = 1;
930
- }
931
-
932
- if (idx1) {
933
- for (i = i1 = 0; i1 < n1 && i < n; i++, i1++) {
934
- x = ptr[i1];
935
- if (rb_obj_is_kind_of(x, rb_cRange)
936
- #ifdef HAVE_RB_ARITHMETIC_SEQUENCE_EXTRACT
937
- || rb_obj_is_kind_of(x, rb_cArithSeq)
938
- #else
939
- || rb_obj_is_kind_of(x, rb_cEnumerator)
940
- #endif
941
- ) {
942
- nary_step_sequence(x, &len, &beg, &step);
943
- for (c = 0; c < len && i < n; c++, i++) {
944
- y = beg + step * c;
945
- z = m_from_double(y);
946
- SET_DATA_INDEX(p1, idx1, dtype, z);
947
- }
948
- } else if (TYPE(x) != T_ARRAY) {
949
- z = m_num_to_data(x);
950
- SET_DATA_INDEX(p1, idx1, dtype, z);
951
- }
952
- }
953
- } else {
954
- for (i = i1 = 0; i1 < n1 && i < n; i++, i1++) {
955
- x = ptr[i1];
956
- if (rb_obj_is_kind_of(x, rb_cRange)
957
- #ifdef HAVE_RB_ARITHMETIC_SEQUENCE_EXTRACT
958
- || rb_obj_is_kind_of(x, rb_cArithSeq)
959
- #else
960
- || rb_obj_is_kind_of(x, rb_cEnumerator)
961
- #endif
962
- ) {
963
- nary_step_sequence(x, &len, &beg, &step);
964
- for (c = 0; c < len && i < n; c++, i++) {
965
- y = beg + step * c;
966
- z = m_from_double(y);
967
- SET_DATA_STRIDE(p1, s1, dtype, z);
968
- }
969
- } else if (TYPE(x) != T_ARRAY) {
970
- z = m_num_to_data(x);
971
- SET_DATA_STRIDE(p1, s1, dtype, z);
972
- }
973
- }
974
- }
975
-
976
- loop_end:
977
- z = m_zero;
978
- if (idx1) {
979
- for (; i < n; i++) {
980
- SET_DATA_INDEX(p1, idx1, dtype, z);
981
- }
982
- } else {
983
- for (; i < n; i++) {
984
- SET_DATA_STRIDE(p1, s1, dtype, z);
985
- }
986
- }
987
- }
988
-
989
- static VALUE int16_store_array(VALUE self, VALUE rary) {
990
- ndfunc_arg_in_t ain[2] = { { OVERWRITE, 0 }, { rb_cArray, 0 } };
991
- ndfunc_t ndf = { iter_int16_store_array, FULL_LOOP, 2, 0, ain, 0 };
992
-
993
- na_ndloop_store_rarray(&ndf, self, rary);
994
- return self;
995
- }
996
-
997
- /*
998
- Store elements to Numo::Int16 from other.
999
- @overload store(other)
1000
- @param [Object] other
1001
- @return [Numo::Int16] self
1002
- */
1003
- static VALUE int16_store(VALUE self, VALUE obj) {
1004
- VALUE r, klass;
1005
-
1006
- klass = rb_obj_class(obj);
1007
-
1008
- if (klass == numo_cInt16) {
1009
- int16_store_int16(self, obj);
1010
- return self;
1011
- }
1012
-
1013
- if (IS_INTEGER_CLASS(klass) || klass == rb_cFloat || klass == rb_cComplex) {
1014
- int16_store_numeric(self, obj);
1015
- return self;
1016
- }
1017
-
1018
- if (klass == numo_cBit) {
1019
- int16_store_bit(self, obj);
1020
- return self;
1021
- }
1022
-
1023
- if (klass == numo_cDFloat) {
1024
- int16_store_dfloat(self, obj);
1025
- return self;
1026
- }
1027
-
1028
- if (klass == numo_cSFloat) {
1029
- int16_store_sfloat(self, obj);
1030
- return self;
1031
- }
1032
-
1033
- if (klass == numo_cInt64) {
1034
- int16_store_int64(self, obj);
1035
- return self;
1036
- }
1037
-
1038
- if (klass == numo_cInt32) {
1039
- int16_store_int32(self, obj);
1040
- return self;
1041
- }
1042
-
1043
- if (klass == numo_cInt8) {
1044
- int16_store_int8(self, obj);
1045
- return self;
1046
- }
1047
-
1048
- if (klass == numo_cUInt64) {
1049
- int16_store_uint64(self, obj);
1050
- return self;
1051
- }
1052
-
1053
- if (klass == numo_cUInt32) {
1054
- int16_store_uint32(self, obj);
1055
- return self;
1056
- }
1057
-
1058
- if (klass == numo_cUInt16) {
1059
- int16_store_uint16(self, obj);
1060
- return self;
1061
- }
1062
-
1063
- if (klass == numo_cUInt8) {
1064
- int16_store_uint8(self, obj);
1065
- return self;
1066
- }
1067
-
1068
- if (klass == numo_cRObject) {
1069
- int16_store_robject(self, obj);
1070
- return self;
1071
- }
1072
-
1073
- if (klass == rb_cArray) {
1074
- int16_store_array(self, obj);
1075
- return self;
1076
- }
1077
-
1078
- if (IsNArray(obj)) {
1079
- r = rb_funcall(obj, rb_intern("coerce_cast"), 1, cT);
1080
- if (rb_obj_class(r) == cT) {
1081
- int16_store(self, r);
1082
- return self;
1083
- }
1084
- }
1085
-
1086
- rb_raise(
1087
- nary_eCastError, "unknown conversion from %s to %s", rb_class2name(rb_obj_class(obj)),
1088
- rb_class2name(rb_obj_class(self))
1089
- );
1090
-
1091
- return self;
1092
- }
1093
-
1094
- /*
1095
- Convert a data value of obj (with a single element) to dtype.
1096
- */
1097
- static dtype int16_extract_data(VALUE obj) {
1098
- narray_t* na;
1099
- dtype x;
1100
- char* ptr;
1101
- size_t pos;
1102
- VALUE r, klass;
1103
-
1104
- if (IsNArray(obj)) {
1105
- GetNArray(obj, na);
1106
- if (na->size != 1) {
1107
- rb_raise(nary_eShapeError, "narray size should be 1");
1108
- }
1109
- klass = rb_obj_class(obj);
1110
- ptr = na_get_pointer_for_read(obj);
1111
- pos = na_get_offset(obj);
1112
-
1113
- if (klass == numo_cInt16) {
1114
- x = m_from_sint(*(int16_t*)(ptr + pos));
1115
- return x;
1116
- }
1117
-
1118
- if (klass == numo_cBit) {
1119
- {
1120
- BIT_DIGIT b;
1121
- LOAD_BIT(ptr, pos, b);
1122
- x = m_from_sint(b);
1123
- };
1124
- return x;
1125
- }
1126
-
1127
- if (klass == numo_cDFloat) {
1128
- x = m_from_real(*(double*)(ptr + pos));
1129
- return x;
1130
- }
1131
-
1132
- if (klass == numo_cSFloat) {
1133
- x = m_from_real(*(float*)(ptr + pos));
1134
- return x;
1135
- }
1136
-
1137
- if (klass == numo_cInt64) {
1138
- x = m_from_int64(*(int64_t*)(ptr + pos));
1139
- return x;
1140
- }
1141
-
1142
- if (klass == numo_cInt32) {
1143
- x = m_from_int32(*(int32_t*)(ptr + pos));
1144
- return x;
1145
- }
1146
-
1147
- if (klass == numo_cInt8) {
1148
- x = m_from_sint(*(int8_t*)(ptr + pos));
1149
- return x;
1150
- }
1151
-
1152
- if (klass == numo_cUInt64) {
1153
- x = m_from_uint64(*(u_int64_t*)(ptr + pos));
1154
- return x;
1155
- }
1156
-
1157
- if (klass == numo_cUInt32) {
1158
- x = m_from_uint32(*(u_int32_t*)(ptr + pos));
1159
- return x;
1160
- }
1161
-
1162
- if (klass == numo_cUInt16) {
1163
- x = m_from_sint(*(u_int16_t*)(ptr + pos));
1164
- return x;
1165
- }
1166
-
1167
- if (klass == numo_cUInt8) {
1168
- x = m_from_sint(*(u_int8_t*)(ptr + pos));
1169
- return x;
1170
- }
1171
-
1172
- if (klass == numo_cRObject) {
1173
- x = m_num_to_data(*(VALUE*)(ptr + pos));
1174
- return x;
1175
- }
1176
-
1177
- // coerce
1178
- r = rb_funcall(obj, rb_intern("coerce_cast"), 1, cT);
1179
- if (rb_obj_class(r) == cT) {
1180
- return int16_extract_data(r);
1181
- }
1182
-
1183
- rb_raise(
1184
- nary_eCastError, "unknown conversion from %s to %s", rb_class2name(rb_obj_class(obj)),
1185
- rb_class2name(cT)
1186
- );
1187
- }
1188
- if (TYPE(obj) == T_ARRAY) {
1189
- if (RARRAY_LEN(obj) != 1) {
1190
- rb_raise(nary_eShapeError, "array size should be 1");
1191
- }
1192
- return m_num_to_data(RARRAY_AREF(obj, 0));
1193
- }
1194
- return m_num_to_data(obj);
1195
- }
1196
-
1197
- static VALUE int16_cast_array(VALUE rary) {
1198
- VALUE nary;
1199
- narray_t* na;
1200
-
1201
- nary = na_s_new_like(cT, rary);
1202
- GetNArray(nary, na);
1203
- if (na->size > 0) {
1204
- int16_store_array(nary, rary);
1205
- }
1206
- return nary;
1207
- }
1208
-
1209
- /*
1210
- Cast object to Numo::Int16.
1211
- @overload [](elements)
1212
- @overload cast(array)
1213
- @param [Numeric,Array] elements
1214
- @param [Array] array
1215
- @return [Numo::Int16]
1216
- */
1217
- static VALUE int16_s_cast(VALUE type, VALUE obj) {
1218
- VALUE v;
1219
- narray_t* na;
1220
- dtype x;
1221
-
1222
- if (rb_obj_class(obj) == cT) {
1223
- return obj;
1224
- }
1225
- if (RTEST(rb_obj_is_kind_of(obj, rb_cNumeric))) {
1226
- x = m_num_to_data(obj);
1227
- return int16_new_dim0(x);
1228
- }
1229
- if (RTEST(rb_obj_is_kind_of(obj, rb_cArray))) {
1230
- return int16_cast_array(obj);
1231
- }
1232
- if (IsNArray(obj)) {
1233
- GetNArray(obj, na);
1234
- v = nary_new(cT, NA_NDIM(na), NA_SHAPE(na));
1235
- if (NA_SIZE(na) > 0) {
1236
- int16_store(v, obj);
1237
- }
1238
- return v;
1239
- }
1240
- if (rb_respond_to(obj, id_to_a)) {
1241
- obj = rb_funcall(obj, id_to_a, 0);
1242
- if (TYPE(obj) != T_ARRAY) {
1243
- rb_raise(rb_eTypeError, "`to_a' did not return Array");
1244
- }
1245
- return int16_cast_array(obj);
1246
- }
1247
-
1248
- rb_raise(nary_eCastError, "cannot cast to %s", rb_class2name(type));
1249
- return Qnil;
1250
- }
1251
-
1252
- /*
1253
- Multi-dimensional element assignment.
1254
- @overload []=(dim0,...,dimL,val)
1255
- @param [Numeric,Range,Array,Numo::Int32,Numo::Int64,Numo::Bit,TrueClass,FalseClass,Symbol]
1256
- dim0,...,dimL multi-dimensional indices.
1257
- @param [Numeric,Numo::NArray,Array] val Value(s) to be set to self.
1258
- @return [Numeric,Numo::NArray,Array] returns `val` (last argument).
1259
- @see Numo::NArray#[]=
1260
- @see #[]
1261
- */
1262
- static VALUE int16_aset(int argc, VALUE* argv, VALUE self) {
1263
- int nd;
1264
- size_t pos;
1265
- char* ptr;
1266
- VALUE a;
1267
- dtype x;
1268
-
1269
- argc--;
1270
- if (argc == 0) {
1271
- int16_store(self, argv[argc]);
1272
- } else {
1273
- nd = na_get_result_dimension(self, argc, argv, sizeof(dtype), &pos);
1274
- if (nd) {
1275
- a = na_aref_main(argc, argv, self, 0, nd);
1276
- int16_store(a, argv[argc]);
1277
- } else {
1278
- x = int16_extract_data(argv[argc]);
1279
- ptr = na_get_pointer_for_read_write(self) + pos;
1280
- *(dtype*)ptr = x;
1281
- }
1282
- }
1283
- return argv[argc];
1284
- }
1285
-
1286
- /*
1287
- qsort.c
1288
- Ruby/Numo::NArray - Numerical Array class for Ruby
1289
- modified by Masahiro TANAKA
1290
- */
1291
-
1292
- /*
1293
- * qsort.c: standard quicksort algorithm
1294
- *
1295
- * Modifications from vanilla NetBSD source:
1296
- * Add do ... while() macro fix
1297
- * Remove __inline, _DIAGASSERTs, __P
1298
- * Remove ill-considered "swap_cnt" switch to insertion sort,
1299
- * in favor of a simple check for presorted input.
1300
- *
1301
- * CAUTION: if you change this file, see also qsort_arg.c
1302
- *
1303
- * $PostgreSQL: pgsql/src/port/qsort.c,v 1.12 2006/10/19 20:56:22 tgl Exp $
1304
- */
1305
-
1306
- /* $NetBSD: qsort.c,v 1.13 2003/08/07 16:43:42 agc Exp $ */
1307
-
1308
- /*-
1309
- * Copyright (c) 1992, 1993
1310
- * The Regents of the University of California. All rights reserved.
1311
- *
1312
- * Redistribution and use in source and binary forms, with or without
1313
- * modification, are permitted provided that the following conditions
1314
- * are met:
1315
- * 1. Redistributions of source code must retain the above copyright
1316
- * notice, this list of conditions and the following disclaimer.
1317
- * 2. Redistributions in binary form must reproduce the above copyright
1318
- * notice, this list of conditions and the following disclaimer in the
1319
- * documentation and/or other materials provided with the distribution.
1320
- * 3. Neither the name of the University nor the names of its contributors
1321
- * may be used to endorse or promote products derived from this software
1322
- * without specific prior written permission.
1323
- *
1324
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1325
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1326
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1327
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1328
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1329
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1330
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1331
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1332
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1333
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1334
- * SUCH DAMAGE.
1335
- */
1336
-
1337
- #ifndef QSORT_INCL
1338
- #define QSORT_INCL
1339
- #define Min(x, y) ((x) < (y) ? (x) : (y))
1340
-
1341
- /*
1342
- * Qsort routine based on J. L. Bentley and M. D. McIlroy,
1343
- * "Engineering a sort function",
1344
- * Software--Practice and Experience 23 (1993) 1249-1265.
1345
- * We have modified their original by adding a check for already-sorted input,
1346
- * which seems to be a win per discussions on pgsql-hackers around 2006-03-21.
1347
- */
1348
- #define swapcode(TYPE, parmi, parmj, n) \
1349
- do { \
1350
- size_t i = (n) / sizeof(TYPE); \
1351
- TYPE* pi = (TYPE*)(void*)(parmi); \
1352
- TYPE* pj = (TYPE*)(void*)(parmj); \
1353
- do { \
1354
- TYPE t = *pi; \
1355
- *pi++ = *pj; \
1356
- *pj++ = t; \
1357
- } while (--i > 0); \
1358
- } while (0)
1359
-
1360
- #ifdef HAVE_STDINT_H
1361
- #define SWAPINIT(a, es) \
1362
- swaptype = (uintptr_t)(a) % sizeof(long) || (es) % sizeof(long) ? 2 \
1363
- : (es) == sizeof(long) ? 0 \
1364
- : 1;
1365
- #else
1366
- #define SWAPINIT(a, es) \
1367
- swaptype = ((char*)(a) - (char*)0) % sizeof(long) || (es) % sizeof(long) ? 2 \
1368
- : (es) == sizeof(long) ? 0 \
1369
- : 1;
1370
- #endif
1371
-
1372
- static inline void swapfunc(char* a, char* b, size_t n, int swaptype) {
1373
- if (swaptype <= 1)
1374
- swapcode(long, a, b, n);
1375
- else
1376
- swapcode(char, a, b, n);
1377
- }
1378
-
1379
- #define swap(a, b) \
1380
- if (swaptype == 0) { \
1381
- long t = *(long*)(void*)(a); \
1382
- *(long*)(void*)(a) = *(long*)(void*)(b); \
1383
- *(long*)(void*)(b) = t; \
1384
- } else \
1385
- swapfunc(a, b, es, swaptype)
1386
-
1387
- #define vecswap(a, b, n) \
1388
- if ((n) > 0) swapfunc((a), (b), (size_t)(n), swaptype)
1389
-
1390
- #define med3(a, b, c, _cmp) \
1391
- (cmpgt(b, a) ? (cmpgt(c, b) ? b : (cmpgt(c, a) ? c : a)) \
1392
- : (cmpgt(b, c) ? b : (cmpgt(c, a) ? a : c)))
1393
- #endif
1394
-
1395
- #undef qsort_dtype
1396
- #define qsort_dtype dtype
1397
- #undef qsort_cast
1398
- #define qsort_cast *(dtype*)
1399
-
1400
- static void int16_qsort(void* a, size_t n, ssize_t es) {
1401
- char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
1402
- int d, r, swaptype, presorted;
1403
-
1404
- loop:
1405
- SWAPINIT(a, es);
1406
- if (n < 7) {
1407
- for (pm = (char*)a + es; pm < (char*)a + n * es; pm += es)
1408
- for (pl = pm; pl > (char*)a && cmpgt(pl - es, pl); pl -= es) swap(pl, pl - es);
1409
- return;
1410
- }
1411
- presorted = 1;
1412
- for (pm = (char*)a + es; pm < (char*)a + n * es; pm += es) {
1413
- if (cmpgt(pm - es, pm)) {
1414
- presorted = 0;
1415
- break;
1416
- }
1417
- }
1418
- if (presorted) return;
1419
- pm = (char*)a + (n / 2) * es;
1420
- if (n > 7) {
1421
- pl = (char*)a;
1422
- pn = (char*)a + (n - 1) * es;
1423
- if (n > 40) {
1424
- d = (int)((n / 8) * es);
1425
- pl = med3(pl, pl + d, pl + 2 * d, cmp);
1426
- pm = med3(pm - d, pm, pm + d, cmp);
1427
- pn = med3(pn - 2 * d, pn - d, pn, cmp);
1428
- }
1429
- pm = med3(pl, pm, pn, cmp);
1430
- }
1431
- swap(a, pm);
1432
- pa = pb = (char*)a + es;
1433
- pc = pd = (char*)a + (n - 1) * es;
1434
- for (;;) {
1435
- while (pb <= pc && (r = cmp(pb, a)) <= 0) {
1436
- if (r == 0) {
1437
- swap(pa, pb);
1438
- pa += es;
1439
- }
1440
- pb += es;
1441
- }
1442
- while (pb <= pc && (r = cmp(pc, a)) >= 0) {
1443
- if (r == 0) {
1444
- swap(pc, pd);
1445
- pd -= es;
1446
- }
1447
- pc -= es;
1448
- }
1449
- if (pb > pc) break;
1450
- swap(pb, pc);
1451
- pb += es;
1452
- pc -= es;
1453
- }
1454
- pn = (char*)a + n * es;
1455
- r = (int)Min(pa - (char*)a, pb - pa);
1456
- vecswap(a, pb - r, r);
1457
- r = (int)Min(pd - pc, pn - pd - es);
1458
- vecswap(pb, pn - r, r);
1459
- if ((r = (int)(pb - pa)) > es) int16_qsort(a, r / es, es);
1460
- if ((r = (int)(pd - pc)) > es) {
1461
- /* Iterate rather than recurse to save stack space */
1462
- a = pn - r;
1463
- n = r / es;
1464
- goto loop;
1465
- }
1466
- /* qsort(pn - r, r / es, es, cmp);*/
1467
- }
1468
-
1469
- static void iter_int16_sort(na_loop_t* const lp) {
1470
- size_t n;
1471
- char* ptr;
1472
- ssize_t step;
1473
-
1474
- INIT_COUNTER(lp, n);
1475
- INIT_PTR(lp, 0, ptr, step);
1476
- int16_qsort(ptr, n, step);
1477
- }
1478
-
1479
- /*
1480
- sort of self.
1481
- @overload sort(axis:nil)
1482
- @param [Numeric,Array,Range] axis Performs sort along the axis.
1483
- @return [Numo::Int16] returns result of sort.
1484
- @example
1485
- Numo::DFloat[3,4,1,2].sort #=> Numo::DFloat[1,2,3,4]
1486
- */
1487
- static VALUE int16_sort(int argc, VALUE* argv, VALUE self) {
1488
- VALUE reduce;
1489
- ndfunc_arg_in_t ain[2] = { { OVERWRITE, 0 }, { sym_reduce, 0 } };
1490
- ndfunc_t ndf = { 0, NDF_HAS_LOOP | NDF_FLAT_REDUCE, 2, 0, ain, 0 };
1491
-
1492
- if (!TEST_INPLACE(self)) {
1493
- self = na_copy(self);
1494
- }
1495
-
1496
- ndf.func = iter_int16_sort;
1497
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
1498
-
1499
- na_ndloop(&ndf, 2, self, reduce);
1500
- return self;
1501
- }
1502
-
1503
- /*
1504
- qsort.c
1505
- Ruby/Numo::NArray - Numerical Array class for Ruby
1506
- modified by Masahiro TANAKA
1507
- */
1508
-
1509
- /*
1510
- * qsort.c: standard quicksort algorithm
1511
- *
1512
- * Modifications from vanilla NetBSD source:
1513
- * Add do ... while() macro fix
1514
- * Remove __inline, _DIAGASSERTs, __P
1515
- * Remove ill-considered "swap_cnt" switch to insertion sort,
1516
- * in favor of a simple check for presorted input.
1517
- *
1518
- * CAUTION: if you change this file, see also qsort_arg.c
1519
- *
1520
- * $PostgreSQL: pgsql/src/port/qsort.c,v 1.12 2006/10/19 20:56:22 tgl Exp $
1521
- */
1522
-
1523
- /* $NetBSD: qsort.c,v 1.13 2003/08/07 16:43:42 agc Exp $ */
1524
-
1525
- /*-
1526
- * Copyright (c) 1992, 1993
1527
- * The Regents of the University of California. All rights reserved.
1528
- *
1529
- * Redistribution and use in source and binary forms, with or without
1530
- * modification, are permitted provided that the following conditions
1531
- * are met:
1532
- * 1. Redistributions of source code must retain the above copyright
1533
- * notice, this list of conditions and the following disclaimer.
1534
- * 2. Redistributions in binary form must reproduce the above copyright
1535
- * notice, this list of conditions and the following disclaimer in the
1536
- * documentation and/or other materials provided with the distribution.
1537
- * 3. Neither the name of the University nor the names of its contributors
1538
- * may be used to endorse or promote products derived from this software
1539
- * without specific prior written permission.
1540
- *
1541
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1542
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1543
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1544
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1545
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1546
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1547
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1548
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1549
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1550
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1551
- * SUCH DAMAGE.
1552
- */
1553
-
1554
- #undef qsort_dtype
1555
- #define qsort_dtype dtype*
1556
- #undef qsort_cast
1557
- #define qsort_cast **(dtype**)
1558
-
1559
- static void int16_index_qsort(void* a, size_t n, ssize_t es) {
1560
- char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
1561
- int d, r, swaptype, presorted;
1562
-
1563
- loop:
1564
- SWAPINIT(a, es);
1565
- if (n < 7) {
1566
- for (pm = (char*)a + es; pm < (char*)a + n * es; pm += es)
1567
- for (pl = pm; pl > (char*)a && cmpgt(pl - es, pl); pl -= es) swap(pl, pl - es);
1568
- return;
1569
- }
1570
- presorted = 1;
1571
- for (pm = (char*)a + es; pm < (char*)a + n * es; pm += es) {
1572
- if (cmpgt(pm - es, pm)) {
1573
- presorted = 0;
1574
- break;
1575
- }
1576
- }
1577
- if (presorted) return;
1578
- pm = (char*)a + (n / 2) * es;
1579
- if (n > 7) {
1580
- pl = (char*)a;
1581
- pn = (char*)a + (n - 1) * es;
1582
- if (n > 40) {
1583
- d = (int)((n / 8) * es);
1584
- pl = med3(pl, pl + d, pl + 2 * d, cmp);
1585
- pm = med3(pm - d, pm, pm + d, cmp);
1586
- pn = med3(pn - 2 * d, pn - d, pn, cmp);
1587
- }
1588
- pm = med3(pl, pm, pn, cmp);
1589
- }
1590
- swap(a, pm);
1591
- for (pa = pb = (char*)a + es, pc = pd = (char*)a + (n - 1) * es; pb <= pc;
1592
- pb += es, pc -= es) {
1593
- while (pb <= pc && (r = cmp(pb, a)) <= 0) {
1594
- if (r == 0) {
1595
- swap(pa, pb);
1596
- pa += es;
1597
- }
1598
- pb += es;
1599
- }
1600
- while (pb <= pc && (r = cmp(pc, a)) >= 0) {
1601
- if (r == 0) {
1602
- swap(pc, pd);
1603
- pd -= es;
1604
- }
1605
- pc -= es;
1606
- }
1607
- if (pb > pc) break;
1608
- swap(pb, pc);
1609
- }
1610
- pn = (char*)a + n * es;
1611
- r = (int)Min(pa - (char*)a, pb - pa);
1612
- vecswap(a, pb - r, r);
1613
- r = (int)Min(pd - pc, pn - pd - es);
1614
- vecswap(pb, pn - r, r);
1615
- if ((r = (int)(pb - pa)) > es) int16_index_qsort(a, r / es, es);
1616
- if ((r = (int)(pd - pc)) > es) {
1617
- /* Iterate rather than recurse to save stack space */
1618
- a = pn - r;
1619
- n = r / es;
1620
- goto loop;
1621
- }
1622
- /* qsort(pn - r, r / es, es, cmp);*/
1623
- }
1624
-
1625
- #define idx_t int64_t
1626
- static void int16_index64_qsort(na_loop_t* const lp) {
1627
- size_t i, n, idx;
1628
- char *d_ptr, *i_ptr, *o_ptr;
1629
- ssize_t d_step, i_step, o_step;
1630
- char** ptr;
1631
-
1632
- INIT_COUNTER(lp, n);
1633
- INIT_PTR(lp, 0, d_ptr, d_step);
1634
- INIT_PTR(lp, 1, i_ptr, i_step);
1635
- INIT_PTR(lp, 2, o_ptr, o_step);
1636
-
1637
- ptr = (char**)(lp->opt_ptr);
1638
-
1639
- // o_ptr=%lx,o_step=%ld)\n",(size_t)ptr,(size_t)d_ptr,(ssize_t)d_step,(size_t)i_ptr,(ssize_t)i_step,(size_t)o_ptr,(ssize_t)o_step);
1640
-
1641
- if (n == 1) {
1642
- *(idx_t*)o_ptr = *(idx_t*)(i_ptr);
1643
- return;
1644
- }
1645
-
1646
- for (i = 0; i < n; i++) {
1647
- ptr[i] = d_ptr + d_step * i;
1648
- }
1649
-
1650
- int16_index_qsort(ptr, n, sizeof(dtype*));
1651
-
1652
- // d_ptr = lp->args[0].ptr;
1653
-
1654
- for (i = 0; i < n; i++) {
1655
- idx = (ptr[i] - d_ptr) / d_step;
1656
- *(idx_t*)o_ptr = *(idx_t*)(i_ptr + i_step * idx);
1657
- o_ptr += o_step;
1658
- }
1659
- }
1660
- #undef idx_t
1661
-
1662
- #define idx_t int32_t
1663
- static void int16_index32_qsort(na_loop_t* const lp) {
1664
- size_t i, n, idx;
1665
- char *d_ptr, *i_ptr, *o_ptr;
1666
- ssize_t d_step, i_step, o_step;
1667
- char** ptr;
1668
-
1669
- INIT_COUNTER(lp, n);
1670
- INIT_PTR(lp, 0, d_ptr, d_step);
1671
- INIT_PTR(lp, 1, i_ptr, i_step);
1672
- INIT_PTR(lp, 2, o_ptr, o_step);
1673
-
1674
- ptr = (char**)(lp->opt_ptr);
1675
-
1676
- // o_ptr=%lx,o_step=%ld)\n",(size_t)ptr,(size_t)d_ptr,(ssize_t)d_step,(size_t)i_ptr,(ssize_t)i_step,(size_t)o_ptr,(ssize_t)o_step);
1677
-
1678
- if (n == 1) {
1679
- *(idx_t*)o_ptr = *(idx_t*)(i_ptr);
1680
- return;
1681
- }
1682
-
1683
- for (i = 0; i < n; i++) {
1684
- ptr[i] = d_ptr + d_step * i;
1685
- }
1686
-
1687
- int16_index_qsort(ptr, n, sizeof(dtype*));
1688
-
1689
- // d_ptr = lp->args[0].ptr;
1690
-
1691
- for (i = 0; i < n; i++) {
1692
- idx = (ptr[i] - d_ptr) / d_step;
1693
- *(idx_t*)o_ptr = *(idx_t*)(i_ptr + i_step * idx);
1694
- o_ptr += o_step;
1695
- }
1696
- }
1697
- #undef idx_t
1698
-
1699
- /*
1700
- sort_index. Returns an index array of sort result.
1701
- @overload sort_index(axis:nil)
1702
- @param [Numeric,Array,Range] axis Performs sort_index along the axis.
1703
- @return [Integer,Numo::Int] returns result index of sort_index.
1704
- @example
1705
- Numo::NArray[3,4,1,2].sort_index #=> Numo::Int32[2,3,0,1]
1706
- */
1707
- static VALUE int16_sort_index(int argc, VALUE* argv, VALUE self) {
1708
- size_t size;
1709
- narray_t* na;
1710
- VALUE idx, tmp, reduce, res;
1711
- char* buf;
1712
- ndfunc_arg_in_t ain[3] = { { cT, 0 }, { 0, 0 }, { sym_reduce, 0 } };
1713
- ndfunc_arg_out_t aout[1] = { { 0, 0, 0 } };
1714
- ndfunc_t ndf = { 0, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE | NDF_CUM, 3, 1, ain, aout };
1715
-
1716
- GetNArray(self, na);
1717
- if (na->ndim == 0) {
1718
- return INT2FIX(0);
1719
- }
1720
- if (na->size > (~(u_int32_t)0)) {
1721
- ain[1].type = aout[0].type = numo_cInt64;
1722
- idx = nary_new(numo_cInt64, na->ndim, na->shape);
1723
-
1724
- ndf.func = int16_index64_qsort;
1725
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
1726
-
1727
- } else {
1728
- ain[1].type = aout[0].type = numo_cInt32;
1729
- idx = nary_new(numo_cInt32, na->ndim, na->shape);
1730
-
1731
- ndf.func = int16_index32_qsort;
1732
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
1733
- }
1734
- rb_funcall(idx, rb_intern("seq"), 0);
1735
-
1736
- size = na->size * sizeof(void*); // max capa
1737
- buf = rb_alloc_tmp_buffer(&tmp, size);
1738
- res = na_ndloop3(&ndf, buf, 3, self, idx, reduce);
1739
- rb_free_tmp_buffer(&tmp);
1740
- return res;
1741
- }
1742
-
1743
- static void iter_int16_median(na_loop_t* const lp) {
1744
- size_t n;
1745
- char *p1, *p2;
1746
- dtype* buf;
1747
-
1748
- INIT_COUNTER(lp, n);
1749
- p1 = (lp->args[0]).ptr + (lp->args[0].iter[0]).pos;
1750
- p2 = (lp->args[1]).ptr + (lp->args[1].iter[0]).pos;
1751
- buf = (dtype*)p1;
1752
-
1753
- int16_qsort(buf, n, sizeof(dtype));
1754
-
1755
- if (n == 0) {
1756
- *(dtype*)p2 = buf[0];
1757
- } else if (n % 2 == 0) {
1758
- *(dtype*)p2 = (buf[n / 2 - 1] + buf[n / 2]) / 2;
1759
- } else {
1760
- *(dtype*)p2 = buf[(n - 1) / 2];
1761
- }
1762
- }
1763
-
1764
- /*
1765
- median of self.
1766
- @overload median(axis:nil, keepdims:false)
1767
- @param [Numeric,Array,Range] axis Finds median along the axis.
1768
- @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
1769
- dimensions with size one.
1770
- @return [Numo::Int16] returns median of self.
1771
- */
1772
-
1773
- static VALUE int16_median(int argc, VALUE* argv, VALUE self) {
1774
- VALUE v, reduce;
1775
- ndfunc_arg_in_t ain[2] = { { OVERWRITE, 0 }, { sym_reduce, 0 } };
1776
- ndfunc_arg_out_t aout[1] = { { INT2FIX(0), 0 } };
1777
- ndfunc_t ndf = { 0, NDF_HAS_LOOP | NDF_FLAT_REDUCE, 2, 1, ain, aout };
1778
-
1779
- self = na_copy(self); // as temporary buffer
1780
-
1781
- ndf.func = iter_int16_median;
1782
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
1783
-
1784
- v = na_ndloop(&ndf, 2, self, reduce);
1785
- return int16_extract(v);
1786
- }
1787
-
1788
291
  void Init_numo_int16(void) {
1789
292
  VALUE hCast, mNumo;
1790
293
 
@@ -1818,12 +321,7 @@ void Init_numo_int16(void) {
1818
321
  rb_define_const(cT, "UPCAST", hCast);
1819
322
  rb_hash_aset(hCast, rb_cArray, cT);
1820
323
 
1821
- #ifdef RUBY_INTEGER_UNIFICATION
1822
324
  rb_hash_aset(hCast, rb_cInteger, cT);
1823
- #else
1824
- rb_hash_aset(hCast, rb_cFixnum, cT);
1825
- rb_hash_aset(hCast, rb_cBignum, cT);
1826
- #endif
1827
325
  rb_hash_aset(hCast, rb_cFloat, numo_cDFloat);
1828
326
  rb_hash_aset(hCast, rb_cComplex, numo_cDComplex);
1829
327
  rb_hash_aset(hCast, numo_cRObject, numo_cRObject);
@@ -1861,9 +359,21 @@ void Init_numo_int16(void) {
1861
359
  * otherwise returns self.
1862
360
  */
1863
361
  rb_define_method(cT, "extract", int16_extract, 0);
1864
-
362
+ /**
363
+ * Store elements to Numo::Int16 from other.
364
+ * @overload store(other)
365
+ * @param [Object] other
366
+ * @return [Numo::Int16] self
367
+ */
1865
368
  rb_define_method(cT, "store", int16_store, 1);
1866
-
369
+ /**
370
+ * Cast object to Numo::Int16.
371
+ * @overload [](elements)
372
+ * @overload cast(array)
373
+ * @param [Numeric,Array] elements
374
+ * @param [Array] array
375
+ * @return [Numo::Int16]
376
+ */
1867
377
  rb_define_singleton_method(cT, "cast", int16_s_cast, 1);
1868
378
  /**
1869
379
  * Multi-dimensional element reference.
@@ -1875,6 +385,16 @@ void Init_numo_int16(void) {
1875
385
  * @see #[]=
1876
386
  */
1877
387
  rb_define_method(cT, "[]", int16_aref, -1);
388
+ /**
389
+ * Multi-dimensional element assignment.
390
+ * @overload []=(dim0,...,dimL,val)
391
+ * @param [Numeric,Range,Array,Numo::Int32,Numo::Int64,Numo::Bit,TrueClass,FalseClass,
392
+ * Symbol] dim0,...,dimL multi-dimensional indices.
393
+ * @param [Numeric,Numo::NArray,Array] val Value(s) to be set to self.
394
+ * @return [Numeric,Numo::NArray,Array] returns `val` (last argument).
395
+ * @see Numo::NArray#[]=
396
+ * @see #[]
397
+ */
1878
398
  rb_define_method(cT, "[]=", int16_aset, -1);
1879
399
  /**
1880
400
  * return NArray with cast to the type of self.
@@ -2431,10 +951,32 @@ void Init_numo_int16(void) {
2431
951
  * @return [Numo::Int16]
2432
952
  */
2433
953
  rb_define_method(cT, "poly", int16_poly, -2);
2434
-
954
+ /**
955
+ * sort of self.
956
+ * @overload sort(axis:nil)
957
+ * @param [Numeric,Array,Range] axis Performs sort along the axis.
958
+ * @return [Numo::Int16] returns result of sort.
959
+ * @example
960
+ * Numo::DFloat[3,4,1,2].sort #=> Numo::DFloat[1,2,3,4]
961
+ */
2435
962
  rb_define_method(cT, "sort", int16_sort, -1);
2436
-
963
+ /**
964
+ * sort_index. Returns an index array of sort result.
965
+ * @overload sort_index(axis:nil)
966
+ * @param [Numeric,Array,Range] axis Performs sort_index along the axis.
967
+ * @return [Integer,Numo::Int] returns result index of sort_index.
968
+ * @example
969
+ * Numo::NArray[3,4,1,2].sort_index #=> Numo::Int32[2,3,0,1]
970
+ */
2437
971
  rb_define_method(cT, "sort_index", int16_sort_index, -1);
972
+ /**
973
+ * median of self.
974
+ * @overload median(axis:nil, keepdims:false)
975
+ * @param [Numeric,Array,Range] axis Finds median along the axis.
976
+ * @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
977
+ * dimensions with size one.
978
+ * @return [Numo::Int16] returns median of self.
979
+ */
2438
980
  rb_define_method(cT, "median", int16_median, -1);
2439
981
  rb_define_singleton_method(cT, "[]", int16_s_cast, -2);
2440
982
  /**