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