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.
@@ -60,8 +60,11 @@ static ID id_truncate;
60
60
  VALUE cT;
61
61
  extern VALUE cRT;
62
62
 
63
+ #include "mh/store.h"
64
+ #include "mh/s_cast.h"
63
65
  #include "mh/extract.h"
64
66
  #include "mh/aref.h"
67
+ #include "mh/aset.h"
65
68
  #include "mh/coerce_cast.h"
66
69
  #include "mh/to_a.h"
67
70
  #include "mh/fill.h"
@@ -134,8 +137,12 @@ extern VALUE cRT;
134
137
 
135
138
  typedef VALUE robject; // Type aliases for shorter notation
136
139
  // following the codebase naming convention.
140
+ DEF_NARRAY_ROBJ_STORE_METHOD_FUNC()
141
+ DEF_NARRAY_ROBJ_S_CAST_METHOD_FUNC()
137
142
  DEF_NARRAY_EXTRACT_METHOD_FUNC(robject)
138
143
  DEF_NARRAY_AREF_METHOD_FUNC(robject)
144
+ DEF_EXTRACT_DATA_FUNC(robject, numo_cRObject)
145
+ DEF_NARRAY_ASET_METHOD_FUNC(robject)
139
146
  DEF_NARRAY_COERCE_CAST_METHOD_FUNC(robject)
140
147
  DEF_NARRAY_TO_A_METHOD_FUNC(robject)
141
148
  DEF_NARRAY_FILL_METHOD_FUNC(robject)
@@ -206,8 +213,6 @@ DEF_NARRAY_EYE_METHOD_FUNC(robject)
206
213
  DEF_NARRAY_FLT_RAND_METHOD_FUNC(robject)
207
214
  DEF_NARRAY_POLY_METHOD_FUNC(robject, numo_cRObject)
208
215
 
209
- static VALUE robject_store(VALUE, VALUE);
210
-
211
216
  static size_t robject_memsize(const void* ptr) {
212
217
  size_t size = sizeof(narray_data_t);
213
218
  const narray_data_t* na = (const narray_data_t*)ptr;
@@ -331,1010 +336,6 @@ static VALUE robject_allocate(VALUE self) {
331
336
  return self;
332
337
  }
333
338
 
334
- static VALUE robject_new_dim0(dtype x) {
335
- VALUE v;
336
- dtype* ptr;
337
-
338
- v = nary_new(cT, 0, NULL);
339
- ptr = (dtype*)(char*)na_get_pointer_for_write(v);
340
- *ptr = x;
341
- na_release_lock(v);
342
- return v;
343
- }
344
-
345
- static VALUE robject_store_numeric(VALUE self, VALUE obj) {
346
- dtype x;
347
- x = m_num_to_data(obj);
348
- obj = robject_new_dim0(x);
349
- robject_store(self, obj);
350
- return self;
351
- }
352
-
353
- static void iter_robject_store_bit(na_loop_t* const lp) {
354
- size_t i;
355
- char* p1;
356
- size_t p2;
357
- ssize_t s1, s2;
358
- size_t *idx1, *idx2;
359
- BIT_DIGIT *a2, x;
360
- dtype y;
361
-
362
- INIT_COUNTER(lp, i);
363
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
364
- INIT_PTR_BIT_IDX(lp, 1, a2, p2, s2, idx2);
365
- if (idx2) {
366
- if (idx1) {
367
- for (; i--;) {
368
- LOAD_BIT(a2, p2 + *idx2, x);
369
- idx2++;
370
- y = m_from_sint(x);
371
- SET_DATA_INDEX(p1, idx1, dtype, y);
372
- }
373
- } else {
374
- for (; i--;) {
375
- LOAD_BIT(a2, p2 + *idx2, x);
376
- idx2++;
377
- y = m_from_sint(x);
378
- SET_DATA_STRIDE(p1, s1, dtype, y);
379
- }
380
- }
381
- } else {
382
- if (idx1) {
383
- for (; i--;) {
384
- LOAD_BIT(a2, p2, x);
385
- p2 += s2;
386
- y = m_from_sint(x);
387
- SET_DATA_INDEX(p1, idx1, dtype, y);
388
- }
389
- } else {
390
- for (; i--;) {
391
- LOAD_BIT(a2, p2, x);
392
- p2 += s2;
393
- y = m_from_sint(x);
394
- SET_DATA_STRIDE(p1, s1, dtype, y);
395
- }
396
- }
397
- }
398
- }
399
-
400
- static VALUE robject_store_bit(VALUE self, VALUE obj) {
401
- ndfunc_arg_in_t ain[2] = { { OVERWRITE, 0 }, { Qnil, 0 } };
402
- ndfunc_t ndf = { iter_robject_store_bit, FULL_LOOP, 2, 0, ain, 0 };
403
-
404
- na_ndloop(&ndf, 2, self, obj);
405
- return self;
406
- }
407
-
408
- static void iter_robject_store_dfloat(na_loop_t* const lp) {
409
- size_t i, s1, s2;
410
- char *p1, *p2;
411
- size_t *idx1, *idx2;
412
- double x;
413
- dtype y;
414
-
415
- INIT_COUNTER(lp, i);
416
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
417
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
418
- if (idx2) {
419
- if (idx1) {
420
- for (; i--;) {
421
- GET_DATA_INDEX(p2, idx2, double, x);
422
- y = m_from_real(x);
423
- SET_DATA_INDEX(p1, idx1, dtype, y);
424
- }
425
- } else {
426
- for (; i--;) {
427
- GET_DATA_INDEX(p2, idx2, double, x);
428
- y = m_from_real(x);
429
- SET_DATA_STRIDE(p1, s1, dtype, y);
430
- }
431
- }
432
- } else {
433
- if (idx1) {
434
- for (; i--;) {
435
- GET_DATA_STRIDE(p2, s2, double, x);
436
- y = m_from_real(x);
437
- SET_DATA_INDEX(p1, idx1, dtype, y);
438
- }
439
- } else {
440
- for (; i--;) {
441
- GET_DATA_STRIDE(p2, s2, double, x);
442
- y = m_from_real(x);
443
- SET_DATA_STRIDE(p1, s1, dtype, y);
444
- }
445
- }
446
- }
447
- }
448
-
449
- static VALUE robject_store_dfloat(VALUE self, VALUE obj) {
450
- ndfunc_arg_in_t ain[2] = { { OVERWRITE, 0 }, { Qnil, 0 } };
451
- ndfunc_t ndf = { iter_robject_store_dfloat, FULL_LOOP, 2, 0, ain, 0 };
452
-
453
- na_ndloop(&ndf, 2, self, obj);
454
- return self;
455
- }
456
-
457
- static void iter_robject_store_sfloat(na_loop_t* const lp) {
458
- size_t i, s1, s2;
459
- char *p1, *p2;
460
- size_t *idx1, *idx2;
461
- float x;
462
- dtype y;
463
-
464
- INIT_COUNTER(lp, i);
465
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
466
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
467
- if (idx2) {
468
- if (idx1) {
469
- for (; i--;) {
470
- GET_DATA_INDEX(p2, idx2, float, x);
471
- y = m_from_real(x);
472
- SET_DATA_INDEX(p1, idx1, dtype, y);
473
- }
474
- } else {
475
- for (; i--;) {
476
- GET_DATA_INDEX(p2, idx2, float, x);
477
- y = m_from_real(x);
478
- SET_DATA_STRIDE(p1, s1, dtype, y);
479
- }
480
- }
481
- } else {
482
- if (idx1) {
483
- for (; i--;) {
484
- GET_DATA_STRIDE(p2, s2, float, x);
485
- y = m_from_real(x);
486
- SET_DATA_INDEX(p1, idx1, dtype, y);
487
- }
488
- } else {
489
- for (; i--;) {
490
- GET_DATA_STRIDE(p2, s2, float, x);
491
- y = m_from_real(x);
492
- SET_DATA_STRIDE(p1, s1, dtype, y);
493
- }
494
- }
495
- }
496
- }
497
-
498
- static VALUE robject_store_sfloat(VALUE self, VALUE obj) {
499
- ndfunc_arg_in_t ain[2] = { { OVERWRITE, 0 }, { Qnil, 0 } };
500
- ndfunc_t ndf = { iter_robject_store_sfloat, FULL_LOOP, 2, 0, ain, 0 };
501
-
502
- na_ndloop(&ndf, 2, self, obj);
503
- return self;
504
- }
505
-
506
- static void iter_robject_store_int64(na_loop_t* const lp) {
507
- size_t i, s1, s2;
508
- char *p1, *p2;
509
- size_t *idx1, *idx2;
510
- int64_t x;
511
- dtype y;
512
-
513
- INIT_COUNTER(lp, i);
514
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
515
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
516
- if (idx2) {
517
- if (idx1) {
518
- for (; i--;) {
519
- GET_DATA_INDEX(p2, idx2, int64_t, x);
520
- y = m_from_int64(x);
521
- SET_DATA_INDEX(p1, idx1, dtype, y);
522
- }
523
- } else {
524
- for (; i--;) {
525
- GET_DATA_INDEX(p2, idx2, int64_t, x);
526
- y = m_from_int64(x);
527
- SET_DATA_STRIDE(p1, s1, dtype, y);
528
- }
529
- }
530
- } else {
531
- if (idx1) {
532
- for (; i--;) {
533
- GET_DATA_STRIDE(p2, s2, int64_t, x);
534
- y = m_from_int64(x);
535
- SET_DATA_INDEX(p1, idx1, dtype, y);
536
- }
537
- } else {
538
- for (; i--;) {
539
- GET_DATA_STRIDE(p2, s2, int64_t, x);
540
- y = m_from_int64(x);
541
- SET_DATA_STRIDE(p1, s1, dtype, y);
542
- }
543
- }
544
- }
545
- }
546
-
547
- static VALUE robject_store_int64(VALUE self, VALUE obj) {
548
- ndfunc_arg_in_t ain[2] = { { OVERWRITE, 0 }, { Qnil, 0 } };
549
- ndfunc_t ndf = { iter_robject_store_int64, FULL_LOOP, 2, 0, ain, 0 };
550
-
551
- na_ndloop(&ndf, 2, self, obj);
552
- return self;
553
- }
554
-
555
- static void iter_robject_store_int32(na_loop_t* const lp) {
556
- size_t i, s1, s2;
557
- char *p1, *p2;
558
- size_t *idx1, *idx2;
559
- int32_t x;
560
- dtype y;
561
-
562
- INIT_COUNTER(lp, i);
563
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
564
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
565
- if (idx2) {
566
- if (idx1) {
567
- for (; i--;) {
568
- GET_DATA_INDEX(p2, idx2, int32_t, x);
569
- y = m_from_int32(x);
570
- SET_DATA_INDEX(p1, idx1, dtype, y);
571
- }
572
- } else {
573
- for (; i--;) {
574
- GET_DATA_INDEX(p2, idx2, int32_t, x);
575
- y = m_from_int32(x);
576
- SET_DATA_STRIDE(p1, s1, dtype, y);
577
- }
578
- }
579
- } else {
580
- if (idx1) {
581
- for (; i--;) {
582
- GET_DATA_STRIDE(p2, s2, int32_t, x);
583
- y = m_from_int32(x);
584
- SET_DATA_INDEX(p1, idx1, dtype, y);
585
- }
586
- } else {
587
- for (; i--;) {
588
- GET_DATA_STRIDE(p2, s2, int32_t, x);
589
- y = m_from_int32(x);
590
- SET_DATA_STRIDE(p1, s1, dtype, y);
591
- }
592
- }
593
- }
594
- }
595
-
596
- static VALUE robject_store_int32(VALUE self, VALUE obj) {
597
- ndfunc_arg_in_t ain[2] = { { OVERWRITE, 0 }, { Qnil, 0 } };
598
- ndfunc_t ndf = { iter_robject_store_int32, FULL_LOOP, 2, 0, ain, 0 };
599
-
600
- na_ndloop(&ndf, 2, self, obj);
601
- return self;
602
- }
603
-
604
- static void iter_robject_store_int16(na_loop_t* const lp) {
605
- size_t i, s1, s2;
606
- char *p1, *p2;
607
- size_t *idx1, *idx2;
608
- int16_t x;
609
- dtype y;
610
-
611
- INIT_COUNTER(lp, i);
612
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
613
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
614
- if (idx2) {
615
- if (idx1) {
616
- for (; i--;) {
617
- GET_DATA_INDEX(p2, idx2, int16_t, x);
618
- y = m_from_sint(x);
619
- SET_DATA_INDEX(p1, idx1, dtype, y);
620
- }
621
- } else {
622
- for (; i--;) {
623
- GET_DATA_INDEX(p2, idx2, int16_t, x);
624
- y = m_from_sint(x);
625
- SET_DATA_STRIDE(p1, s1, dtype, y);
626
- }
627
- }
628
- } else {
629
- if (idx1) {
630
- for (; i--;) {
631
- GET_DATA_STRIDE(p2, s2, int16_t, x);
632
- y = m_from_sint(x);
633
- SET_DATA_INDEX(p1, idx1, dtype, y);
634
- }
635
- } else {
636
- for (; i--;) {
637
- GET_DATA_STRIDE(p2, s2, int16_t, x);
638
- y = m_from_sint(x);
639
- SET_DATA_STRIDE(p1, s1, dtype, y);
640
- }
641
- }
642
- }
643
- }
644
-
645
- static VALUE robject_store_int16(VALUE self, VALUE obj) {
646
- ndfunc_arg_in_t ain[2] = { { OVERWRITE, 0 }, { Qnil, 0 } };
647
- ndfunc_t ndf = { iter_robject_store_int16, FULL_LOOP, 2, 0, ain, 0 };
648
-
649
- na_ndloop(&ndf, 2, self, obj);
650
- return self;
651
- }
652
-
653
- static void iter_robject_store_int8(na_loop_t* const lp) {
654
- size_t i, s1, s2;
655
- char *p1, *p2;
656
- size_t *idx1, *idx2;
657
- int8_t x;
658
- dtype y;
659
-
660
- INIT_COUNTER(lp, i);
661
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
662
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
663
- if (idx2) {
664
- if (idx1) {
665
- for (; i--;) {
666
- GET_DATA_INDEX(p2, idx2, int8_t, x);
667
- y = m_from_sint(x);
668
- SET_DATA_INDEX(p1, idx1, dtype, y);
669
- }
670
- } else {
671
- for (; i--;) {
672
- GET_DATA_INDEX(p2, idx2, int8_t, x);
673
- y = m_from_sint(x);
674
- SET_DATA_STRIDE(p1, s1, dtype, y);
675
- }
676
- }
677
- } else {
678
- if (idx1) {
679
- for (; i--;) {
680
- GET_DATA_STRIDE(p2, s2, int8_t, x);
681
- y = m_from_sint(x);
682
- SET_DATA_INDEX(p1, idx1, dtype, y);
683
- }
684
- } else {
685
- for (; i--;) {
686
- GET_DATA_STRIDE(p2, s2, int8_t, x);
687
- y = m_from_sint(x);
688
- SET_DATA_STRIDE(p1, s1, dtype, y);
689
- }
690
- }
691
- }
692
- }
693
-
694
- static VALUE robject_store_int8(VALUE self, VALUE obj) {
695
- ndfunc_arg_in_t ain[2] = { { OVERWRITE, 0 }, { Qnil, 0 } };
696
- ndfunc_t ndf = { iter_robject_store_int8, FULL_LOOP, 2, 0, ain, 0 };
697
-
698
- na_ndloop(&ndf, 2, self, obj);
699
- return self;
700
- }
701
-
702
- static void iter_robject_store_uint64(na_loop_t* const lp) {
703
- size_t i, s1, s2;
704
- char *p1, *p2;
705
- size_t *idx1, *idx2;
706
- u_int64_t x;
707
- dtype y;
708
-
709
- INIT_COUNTER(lp, i);
710
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
711
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
712
- if (idx2) {
713
- if (idx1) {
714
- for (; i--;) {
715
- GET_DATA_INDEX(p2, idx2, u_int64_t, x);
716
- y = m_from_uint64(x);
717
- SET_DATA_INDEX(p1, idx1, dtype, y);
718
- }
719
- } else {
720
- for (; i--;) {
721
- GET_DATA_INDEX(p2, idx2, u_int64_t, x);
722
- y = m_from_uint64(x);
723
- SET_DATA_STRIDE(p1, s1, dtype, y);
724
- }
725
- }
726
- } else {
727
- if (idx1) {
728
- for (; i--;) {
729
- GET_DATA_STRIDE(p2, s2, u_int64_t, x);
730
- y = m_from_uint64(x);
731
- SET_DATA_INDEX(p1, idx1, dtype, y);
732
- }
733
- } else {
734
- for (; i--;) {
735
- GET_DATA_STRIDE(p2, s2, u_int64_t, x);
736
- y = m_from_uint64(x);
737
- SET_DATA_STRIDE(p1, s1, dtype, y);
738
- }
739
- }
740
- }
741
- }
742
-
743
- static VALUE robject_store_uint64(VALUE self, VALUE obj) {
744
- ndfunc_arg_in_t ain[2] = { { OVERWRITE, 0 }, { Qnil, 0 } };
745
- ndfunc_t ndf = { iter_robject_store_uint64, FULL_LOOP, 2, 0, ain, 0 };
746
-
747
- na_ndloop(&ndf, 2, self, obj);
748
- return self;
749
- }
750
-
751
- static void iter_robject_store_uint32(na_loop_t* const lp) {
752
- size_t i, s1, s2;
753
- char *p1, *p2;
754
- size_t *idx1, *idx2;
755
- u_int32_t x;
756
- dtype y;
757
-
758
- INIT_COUNTER(lp, i);
759
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
760
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
761
- if (idx2) {
762
- if (idx1) {
763
- for (; i--;) {
764
- GET_DATA_INDEX(p2, idx2, u_int32_t, x);
765
- y = m_from_uint32(x);
766
- SET_DATA_INDEX(p1, idx1, dtype, y);
767
- }
768
- } else {
769
- for (; i--;) {
770
- GET_DATA_INDEX(p2, idx2, u_int32_t, x);
771
- y = m_from_uint32(x);
772
- SET_DATA_STRIDE(p1, s1, dtype, y);
773
- }
774
- }
775
- } else {
776
- if (idx1) {
777
- for (; i--;) {
778
- GET_DATA_STRIDE(p2, s2, u_int32_t, x);
779
- y = m_from_uint32(x);
780
- SET_DATA_INDEX(p1, idx1, dtype, y);
781
- }
782
- } else {
783
- for (; i--;) {
784
- GET_DATA_STRIDE(p2, s2, u_int32_t, x);
785
- y = m_from_uint32(x);
786
- SET_DATA_STRIDE(p1, s1, dtype, y);
787
- }
788
- }
789
- }
790
- }
791
-
792
- static VALUE robject_store_uint32(VALUE self, VALUE obj) {
793
- ndfunc_arg_in_t ain[2] = { { OVERWRITE, 0 }, { Qnil, 0 } };
794
- ndfunc_t ndf = { iter_robject_store_uint32, FULL_LOOP, 2, 0, ain, 0 };
795
-
796
- na_ndloop(&ndf, 2, self, obj);
797
- return self;
798
- }
799
-
800
- static void iter_robject_store_uint16(na_loop_t* const lp) {
801
- size_t i, s1, s2;
802
- char *p1, *p2;
803
- size_t *idx1, *idx2;
804
- u_int16_t x;
805
- dtype y;
806
-
807
- INIT_COUNTER(lp, i);
808
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
809
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
810
- if (idx2) {
811
- if (idx1) {
812
- for (; i--;) {
813
- GET_DATA_INDEX(p2, idx2, u_int16_t, x);
814
- y = m_from_sint(x);
815
- SET_DATA_INDEX(p1, idx1, dtype, y);
816
- }
817
- } else {
818
- for (; i--;) {
819
- GET_DATA_INDEX(p2, idx2, u_int16_t, x);
820
- y = m_from_sint(x);
821
- SET_DATA_STRIDE(p1, s1, dtype, y);
822
- }
823
- }
824
- } else {
825
- if (idx1) {
826
- for (; i--;) {
827
- GET_DATA_STRIDE(p2, s2, u_int16_t, x);
828
- y = m_from_sint(x);
829
- SET_DATA_INDEX(p1, idx1, dtype, y);
830
- }
831
- } else {
832
- for (; i--;) {
833
- GET_DATA_STRIDE(p2, s2, u_int16_t, x);
834
- y = m_from_sint(x);
835
- SET_DATA_STRIDE(p1, s1, dtype, y);
836
- }
837
- }
838
- }
839
- }
840
-
841
- static VALUE robject_store_uint16(VALUE self, VALUE obj) {
842
- ndfunc_arg_in_t ain[2] = { { OVERWRITE, 0 }, { Qnil, 0 } };
843
- ndfunc_t ndf = { iter_robject_store_uint16, FULL_LOOP, 2, 0, ain, 0 };
844
-
845
- na_ndloop(&ndf, 2, self, obj);
846
- return self;
847
- }
848
-
849
- static void iter_robject_store_uint8(na_loop_t* const lp) {
850
- size_t i, s1, s2;
851
- char *p1, *p2;
852
- size_t *idx1, *idx2;
853
- u_int8_t x;
854
- dtype y;
855
-
856
- INIT_COUNTER(lp, i);
857
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
858
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
859
- if (idx2) {
860
- if (idx1) {
861
- for (; i--;) {
862
- GET_DATA_INDEX(p2, idx2, u_int8_t, x);
863
- y = m_from_sint(x);
864
- SET_DATA_INDEX(p1, idx1, dtype, y);
865
- }
866
- } else {
867
- for (; i--;) {
868
- GET_DATA_INDEX(p2, idx2, u_int8_t, x);
869
- y = m_from_sint(x);
870
- SET_DATA_STRIDE(p1, s1, dtype, y);
871
- }
872
- }
873
- } else {
874
- if (idx1) {
875
- for (; i--;) {
876
- GET_DATA_STRIDE(p2, s2, u_int8_t, x);
877
- y = m_from_sint(x);
878
- SET_DATA_INDEX(p1, idx1, dtype, y);
879
- }
880
- } else {
881
- for (; i--;) {
882
- GET_DATA_STRIDE(p2, s2, u_int8_t, x);
883
- y = m_from_sint(x);
884
- SET_DATA_STRIDE(p1, s1, dtype, y);
885
- }
886
- }
887
- }
888
- }
889
-
890
- static VALUE robject_store_uint8(VALUE self, VALUE obj) {
891
- ndfunc_arg_in_t ain[2] = { { OVERWRITE, 0 }, { Qnil, 0 } };
892
- ndfunc_t ndf = { iter_robject_store_uint8, FULL_LOOP, 2, 0, ain, 0 };
893
-
894
- na_ndloop(&ndf, 2, self, obj);
895
- return self;
896
- }
897
-
898
- static void iter_robject_store_robject(na_loop_t* const lp) {
899
- size_t i, s1, s2;
900
- char *p1, *p2;
901
- size_t *idx1, *idx2;
902
- VALUE x;
903
- dtype y;
904
-
905
- INIT_COUNTER(lp, i);
906
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
907
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
908
- if (idx2) {
909
- if (idx1) {
910
- for (; i--;) {
911
- GET_DATA_INDEX(p2, idx2, VALUE, x);
912
- y = m_num_to_data(x);
913
- SET_DATA_INDEX(p1, idx1, dtype, y);
914
- }
915
- } else {
916
- for (; i--;) {
917
- GET_DATA_INDEX(p2, idx2, VALUE, x);
918
- y = m_num_to_data(x);
919
- SET_DATA_STRIDE(p1, s1, dtype, y);
920
- }
921
- }
922
- } else {
923
- if (idx1) {
924
- for (; i--;) {
925
- GET_DATA_STRIDE(p2, s2, VALUE, x);
926
- y = m_num_to_data(x);
927
- SET_DATA_INDEX(p1, idx1, dtype, y);
928
- }
929
- } else {
930
- for (; i--;) {
931
- GET_DATA_STRIDE(p2, s2, VALUE, x);
932
- y = m_num_to_data(x);
933
- SET_DATA_STRIDE(p1, s1, dtype, y);
934
- }
935
- }
936
- }
937
- }
938
-
939
- static VALUE robject_store_robject(VALUE self, VALUE obj) {
940
- ndfunc_arg_in_t ain[2] = { { OVERWRITE, 0 }, { Qnil, 0 } };
941
- ndfunc_t ndf = { iter_robject_store_robject, FULL_LOOP, 2, 0, ain, 0 };
942
-
943
- na_ndloop(&ndf, 2, self, obj);
944
- return self;
945
- }
946
-
947
- static void iter_robject_store_array(na_loop_t* const lp) {
948
- size_t i, n;
949
- size_t i1, n1;
950
- VALUE v1, *ptr;
951
- char* p1;
952
- size_t s1, *idx1;
953
- VALUE x;
954
- double y;
955
- dtype z;
956
- size_t len, c;
957
- double beg, step;
958
-
959
- INIT_COUNTER(lp, n);
960
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
961
- v1 = lp->args[1].value;
962
- i = 0;
963
-
964
- if (lp->args[1].ptr) {
965
- if (v1 == Qtrue) {
966
- iter_robject_store_robject(lp);
967
- i = lp->args[1].shape[0];
968
- if (idx1) {
969
- idx1 += i;
970
- } else {
971
- p1 += s1 * i;
972
- }
973
- }
974
- goto loop_end;
975
- }
976
-
977
- ptr = &v1;
978
-
979
- switch (TYPE(v1)) {
980
- case T_ARRAY:
981
- n1 = RARRAY_LEN(v1);
982
- ptr = RARRAY_PTR(v1);
983
- break;
984
- case T_NIL:
985
- n1 = 0;
986
- break;
987
- default:
988
- n1 = 1;
989
- }
990
-
991
- if (idx1) {
992
- for (i = i1 = 0; i1 < n1 && i < n; i++, i1++) {
993
- x = ptr[i1];
994
- if (rb_obj_is_kind_of(x, rb_cRange)
995
- #ifdef HAVE_RB_ARITHMETIC_SEQUENCE_EXTRACT
996
- || rb_obj_is_kind_of(x, rb_cArithSeq)
997
- #else
998
- || rb_obj_is_kind_of(x, rb_cEnumerator)
999
- #endif
1000
- ) {
1001
- nary_step_sequence(x, &len, &beg, &step);
1002
- for (c = 0; c < len && i < n; c++, i++) {
1003
- y = beg + step * c;
1004
- z = m_from_double(y);
1005
- SET_DATA_INDEX(p1, idx1, dtype, z);
1006
- }
1007
- } else if (TYPE(x) != T_ARRAY) {
1008
- z = m_num_to_data(x);
1009
- SET_DATA_INDEX(p1, idx1, dtype, z);
1010
- }
1011
- }
1012
- } else {
1013
- for (i = i1 = 0; i1 < n1 && i < n; i++, i1++) {
1014
- x = ptr[i1];
1015
- if (rb_obj_is_kind_of(x, rb_cRange)
1016
- #ifdef HAVE_RB_ARITHMETIC_SEQUENCE_EXTRACT
1017
- || rb_obj_is_kind_of(x, rb_cArithSeq)
1018
- #else
1019
- || rb_obj_is_kind_of(x, rb_cEnumerator)
1020
- #endif
1021
- ) {
1022
- nary_step_sequence(x, &len, &beg, &step);
1023
- for (c = 0; c < len && i < n; c++, i++) {
1024
- y = beg + step * c;
1025
- z = m_from_double(y);
1026
- SET_DATA_STRIDE(p1, s1, dtype, z);
1027
- }
1028
- } else if (TYPE(x) != T_ARRAY) {
1029
- z = m_num_to_data(x);
1030
- SET_DATA_STRIDE(p1, s1, dtype, z);
1031
- }
1032
- }
1033
- }
1034
-
1035
- loop_end:
1036
- z = m_zero;
1037
- if (idx1) {
1038
- for (; i < n; i++) {
1039
- SET_DATA_INDEX(p1, idx1, dtype, z);
1040
- }
1041
- } else {
1042
- for (; i < n; i++) {
1043
- SET_DATA_STRIDE(p1, s1, dtype, z);
1044
- }
1045
- }
1046
- }
1047
-
1048
- static VALUE robject_store_array(VALUE self, VALUE rary) {
1049
- ndfunc_arg_in_t ain[2] = { { OVERWRITE, 0 }, { rb_cArray, 0 } };
1050
- ndfunc_t ndf = { iter_robject_store_array, FULL_LOOP, 2, 0, ain, 0 };
1051
-
1052
- na_ndloop_store_rarray(&ndf, self, rary);
1053
- return self;
1054
- }
1055
-
1056
- /*
1057
- Store elements to Numo::RObject from other.
1058
- @overload store(other)
1059
- @param [Object] other
1060
- @return [Numo::RObject] self
1061
- */
1062
- static VALUE robject_store(VALUE self, VALUE obj) {
1063
- VALUE r, klass;
1064
-
1065
- klass = rb_obj_class(obj);
1066
-
1067
- if (klass == numo_cRObject) {
1068
- robject_store_robject(self, obj);
1069
- return self;
1070
- }
1071
-
1072
- if (IS_INTEGER_CLASS(klass) || klass == rb_cFloat || klass == rb_cComplex) {
1073
- robject_store_numeric(self, obj);
1074
- return self;
1075
- }
1076
-
1077
- if (klass == numo_cBit) {
1078
- robject_store_bit(self, obj);
1079
- return self;
1080
- }
1081
-
1082
- if (klass == numo_cDFloat) {
1083
- robject_store_dfloat(self, obj);
1084
- return self;
1085
- }
1086
-
1087
- if (klass == numo_cSFloat) {
1088
- robject_store_sfloat(self, obj);
1089
- return self;
1090
- }
1091
-
1092
- if (klass == numo_cInt64) {
1093
- robject_store_int64(self, obj);
1094
- return self;
1095
- }
1096
-
1097
- if (klass == numo_cInt32) {
1098
- robject_store_int32(self, obj);
1099
- return self;
1100
- }
1101
-
1102
- if (klass == numo_cInt16) {
1103
- robject_store_int16(self, obj);
1104
- return self;
1105
- }
1106
-
1107
- if (klass == numo_cInt8) {
1108
- robject_store_int8(self, obj);
1109
- return self;
1110
- }
1111
-
1112
- if (klass == numo_cUInt64) {
1113
- robject_store_uint64(self, obj);
1114
- return self;
1115
- }
1116
-
1117
- if (klass == numo_cUInt32) {
1118
- robject_store_uint32(self, obj);
1119
- return self;
1120
- }
1121
-
1122
- if (klass == numo_cUInt16) {
1123
- robject_store_uint16(self, obj);
1124
- return self;
1125
- }
1126
-
1127
- if (klass == numo_cUInt8) {
1128
- robject_store_uint8(self, obj);
1129
- return self;
1130
- }
1131
-
1132
- if (klass == rb_cArray) {
1133
- robject_store_array(self, obj);
1134
- return self;
1135
- }
1136
-
1137
- if (IsNArray(obj)) {
1138
- r = rb_funcall(obj, rb_intern("coerce_cast"), 1, cT);
1139
- if (rb_obj_class(r) == cT) {
1140
- robject_store(self, r);
1141
- return self;
1142
- }
1143
- }
1144
-
1145
- robject_store_numeric(self, obj);
1146
-
1147
- return self;
1148
- }
1149
-
1150
- /*
1151
- Convert a data value of obj (with a single element) to dtype.
1152
- */
1153
- static dtype robject_extract_data(VALUE obj) {
1154
- narray_t* na;
1155
- dtype x;
1156
- char* ptr;
1157
- size_t pos;
1158
- VALUE r, klass;
1159
-
1160
- if (IsNArray(obj)) {
1161
- GetNArray(obj, na);
1162
- if (na->size != 1) {
1163
- rb_raise(nary_eShapeError, "narray size should be 1");
1164
- }
1165
- klass = rb_obj_class(obj);
1166
- ptr = na_get_pointer_for_read(obj);
1167
- pos = na_get_offset(obj);
1168
-
1169
- if (klass == numo_cRObject) {
1170
- x = m_num_to_data(*(VALUE*)(ptr + pos));
1171
- return x;
1172
- }
1173
-
1174
- if (klass == numo_cBit) {
1175
- {
1176
- BIT_DIGIT b;
1177
- LOAD_BIT(ptr, pos, b);
1178
- x = m_from_sint(b);
1179
- };
1180
- return x;
1181
- }
1182
-
1183
- if (klass == numo_cDFloat) {
1184
- x = m_from_real(*(double*)(ptr + pos));
1185
- return x;
1186
- }
1187
-
1188
- if (klass == numo_cSFloat) {
1189
- x = m_from_real(*(float*)(ptr + pos));
1190
- return x;
1191
- }
1192
-
1193
- if (klass == numo_cInt64) {
1194
- x = m_from_int64(*(int64_t*)(ptr + pos));
1195
- return x;
1196
- }
1197
-
1198
- if (klass == numo_cInt32) {
1199
- x = m_from_int32(*(int32_t*)(ptr + pos));
1200
- return x;
1201
- }
1202
-
1203
- if (klass == numo_cInt16) {
1204
- x = m_from_sint(*(int16_t*)(ptr + pos));
1205
- return x;
1206
- }
1207
-
1208
- if (klass == numo_cInt8) {
1209
- x = m_from_sint(*(int8_t*)(ptr + pos));
1210
- return x;
1211
- }
1212
-
1213
- if (klass == numo_cUInt64) {
1214
- x = m_from_uint64(*(u_int64_t*)(ptr + pos));
1215
- return x;
1216
- }
1217
-
1218
- if (klass == numo_cUInt32) {
1219
- x = m_from_uint32(*(u_int32_t*)(ptr + pos));
1220
- return x;
1221
- }
1222
-
1223
- if (klass == numo_cUInt16) {
1224
- x = m_from_sint(*(u_int16_t*)(ptr + pos));
1225
- return x;
1226
- }
1227
-
1228
- if (klass == numo_cUInt8) {
1229
- x = m_from_sint(*(u_int8_t*)(ptr + pos));
1230
- return x;
1231
- }
1232
-
1233
- // coerce
1234
- r = rb_funcall(obj, rb_intern("coerce_cast"), 1, cT);
1235
- if (rb_obj_class(r) == cT) {
1236
- return robject_extract_data(r);
1237
- }
1238
-
1239
- return obj;
1240
- }
1241
- if (TYPE(obj) == T_ARRAY) {
1242
- if (RARRAY_LEN(obj) != 1) {
1243
- rb_raise(nary_eShapeError, "array size should be 1");
1244
- }
1245
- return m_num_to_data(RARRAY_AREF(obj, 0));
1246
- }
1247
- return m_num_to_data(obj);
1248
- }
1249
-
1250
- static VALUE robject_cast_array(VALUE rary) {
1251
- VALUE nary;
1252
- narray_t* na;
1253
-
1254
- nary = na_s_new_like(cT, rary);
1255
- GetNArray(nary, na);
1256
- if (na->size > 0) {
1257
- robject_store_array(nary, rary);
1258
- }
1259
- return nary;
1260
- }
1261
-
1262
- /*
1263
- Cast object to Numo::RObject.
1264
- @overload [](elements)
1265
- @overload cast(array)
1266
- @param [Numeric,Array] elements
1267
- @param [Array] array
1268
- @return [Numo::RObject]
1269
- */
1270
- static VALUE robject_s_cast(VALUE type, VALUE obj) {
1271
- VALUE v;
1272
- narray_t* na;
1273
- dtype x;
1274
-
1275
- if (rb_obj_class(obj) == cT) {
1276
- return obj;
1277
- }
1278
- if (RTEST(rb_obj_is_kind_of(obj, rb_cNumeric))) {
1279
- x = m_num_to_data(obj);
1280
- return robject_new_dim0(x);
1281
- }
1282
- if (RTEST(rb_obj_is_kind_of(obj, rb_cArray))) {
1283
- return robject_cast_array(obj);
1284
- }
1285
- if (IsNArray(obj)) {
1286
- GetNArray(obj, na);
1287
- v = nary_new(cT, NA_NDIM(na), NA_SHAPE(na));
1288
- if (NA_SIZE(na) > 0) {
1289
- robject_store(v, obj);
1290
- }
1291
- return v;
1292
- }
1293
- if (rb_respond_to(obj, id_to_a)) {
1294
- obj = rb_funcall(obj, id_to_a, 0);
1295
- if (TYPE(obj) != T_ARRAY) {
1296
- rb_raise(rb_eTypeError, "`to_a' did not return Array");
1297
- }
1298
- return robject_cast_array(obj);
1299
- }
1300
-
1301
- return robject_new_dim0(obj);
1302
- }
1303
-
1304
- /*
1305
- Multi-dimensional element assignment.
1306
- @overload []=(dim0,...,dimL,val)
1307
- @param [Numeric,Range,Array,Numo::Int32,Numo::Int64,Numo::Bit,TrueClass,FalseClass,Symbol]
1308
- dim0,...,dimL multi-dimensional indices.
1309
- @param [Numeric,Numo::NArray,Array] val Value(s) to be set to self.
1310
- @return [Numeric,Numo::NArray,Array] returns `val` (last argument).
1311
- @see Numo::NArray#[]=
1312
- @see #[]
1313
- */
1314
- static VALUE robject_aset(int argc, VALUE* argv, VALUE self) {
1315
- int nd;
1316
- size_t pos;
1317
- char* ptr;
1318
- VALUE a;
1319
- dtype x;
1320
-
1321
- argc--;
1322
- if (argc == 0) {
1323
- robject_store(self, argv[argc]);
1324
- } else {
1325
- nd = na_get_result_dimension(self, argc, argv, sizeof(dtype), &pos);
1326
- if (nd) {
1327
- a = na_aref_main(argc, argv, self, 0, nd);
1328
- robject_store(a, argv[argc]);
1329
- } else {
1330
- x = robject_extract_data(argv[argc]);
1331
- ptr = na_get_pointer_for_read_write(self) + pos;
1332
- *(dtype*)ptr = x;
1333
- }
1334
- }
1335
- return argv[argc];
1336
- }
1337
-
1338
339
  void Init_numo_robject(void) {
1339
340
  VALUE hCast, mNumo;
1340
341
 
@@ -1385,12 +386,7 @@ void Init_numo_robject(void) {
1385
386
  rb_define_const(cT, "UPCAST", hCast);
1386
387
  rb_hash_aset(hCast, rb_cArray, cT);
1387
388
 
1388
- #ifdef RUBY_INTEGER_UNIFICATION
1389
389
  rb_hash_aset(hCast, rb_cInteger, cT);
1390
- #else
1391
- rb_hash_aset(hCast, rb_cFixnum, cT);
1392
- rb_hash_aset(hCast, rb_cBignum, cT);
1393
- #endif
1394
390
  rb_hash_aset(hCast, rb_cFloat, cT);
1395
391
  rb_hash_aset(hCast, rb_cComplex, cT);
1396
392
  rb_hash_aset(hCast, numo_cDComplex, numo_cRObject);
@@ -1430,9 +426,21 @@ void Init_numo_robject(void) {
1430
426
  * otherwise returns self.
1431
427
  */
1432
428
  rb_define_method(cT, "extract", robject_extract, 0);
1433
-
429
+ /**
430
+ * Store elements to Numo::RObject from other.
431
+ * @overload store(other)
432
+ * @param [Object] other
433
+ * @return [Numo::RObject] self
434
+ */
1434
435
  rb_define_method(cT, "store", robject_store, 1);
1435
-
436
+ /**
437
+ * Cast object to Numo::RObject.
438
+ * @overload [](elements)
439
+ * @overload cast(array)
440
+ * @param [Numeric,Array] elements
441
+ * @param [Array] array
442
+ * @return [Numo::RObject]
443
+ */
1436
444
  rb_define_singleton_method(cT, "cast", robject_s_cast, 1);
1437
445
  /**
1438
446
  * Multi-dimensional element reference.
@@ -1444,6 +452,16 @@ void Init_numo_robject(void) {
1444
452
  * @see #[]=
1445
453
  */
1446
454
  rb_define_method(cT, "[]", robject_aref, -1);
455
+ /**
456
+ * Multi-dimensional element assignment.
457
+ * @overload []=(dim0,...,dimL,val)
458
+ * @param [Numeric,Range,Array,Numo::Int32,Numo::Int64,Numo::Bit,TrueClass,FalseClass,
459
+ * Symbol] dim0,...,dimL multi-dimensional indices.
460
+ * @param [Numeric,Numo::NArray,Array] val Value(s) to be set to self.
461
+ * @return [Numeric,Numo::NArray,Array] returns `val` (last argument).
462
+ * @see Numo::NArray#[]=
463
+ * @see #[]
464
+ */
1447
465
  rb_define_method(cT, "[]=", robject_aset, -1);
1448
466
  /**
1449
467
  * return NArray with cast to the type of self.