1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986
|
/*
na_index.c
Numerical Array Extention for Ruby
(C) Copyright 1999-2008 by Masahiro TANAKA
This program is free software.
You can distribute/modify this program
under the same terms as Ruby itself.
NO WARRANTY.
*/
#include <ruby.h>
#include "narray.h"
#include "narray_local.h"
#define EXCL(r) (RTEST(rb_funcall((r),na_id_exclude_end,0)))
static int
na_index_range(VALUE obj, int size, struct slice *sl)
{
int beg,end,len,step;
VALUE vbeg, vend;
sl->idx = NULL;
/* Beginning */
vbeg = rb_funcall(obj, na_id_beg, 0);
if (vbeg==Qnil) /* First is nil */
beg = 0;
else
beg = NUM2INT(vbeg);
if (beg<0) beg += size;
/* End */
vend = rb_funcall(obj, na_id_end, 0);
if (vend==Qnil) { /* Last is nil */
sl->beg = beg;
sl->step = 1;
return sl->n = 0;
}
else
end = NUM2INT(vend);
if (end<0) end += size;
/* length */
len = end-beg;
/* direction */
if (len>0) {
step = 1;
if (EXCL(obj)) --end; else ++len;
}
else if (len<0) {
len = -len;
step = -1;
if (EXCL(obj)) ++end; else ++len;
}
else /*if(len==0)*/ {
if (EXCL(obj))
rb_raise(rb_eIndexError, "empty range");
else {
++len;
step = 1; /* or 0 ? depend on whether removing rank */
}
}
if ( beg<0 || beg>=size || end<0 || end>=size )
rb_raise(rb_eIndexError, "index out of range");
sl->n = len;
sl->beg = beg;
sl->step = step;
return len;
}
static int
na_index_scalar(int idx, int size, struct slice *sl)
{
if (idx<0) idx+=size;
if (idx<0 || idx>=size)
rb_raise(rb_eIndexError, "index out of range");
sl->n = 1;
sl->beg = idx;
sl->step = 0;
sl->idx = NULL;
return 1;
}
static int
na_ary_to_index(struct NARRAY *a1, int size, struct slice *s)
{
int i;
na_index_t idx, *p;
/* Empty Array */
if (a1->total==0) {
s->n = 0;
s->beg = 0;
s->step = 1;
s->idx = NULL;
}
else
/* single element */
if (a1->total==1) {
SetFuncs[NA_LINT][a1->type](1, &idx, 0, a1->ptr, 0);
if ( idx<0 ) idx += size;
if ( idx<0 || idx>=size )
rb_raise(rb_eIndexError, "index %i out of range %i", idx, size);
s->n = 1;
s->beg = idx;
s->step = 1;
s->idx = NULL;
}
else {
/* Copy index array */
s->n = a1->total;
s->step = 1;
s->idx = p = ALLOC_N(na_index_t, a1->total);
SetFuncs[NA_LINT][a1->type]( s->n,
s->idx, na_sizeof[NA_LINT],
a1->ptr, na_sizeof[a1->type] );
for ( i=a1->total; i>0; --i ) {
if ( *p<0 ) *p += size;
if ( *p<0 || *p>=size )
rb_raise(rb_eIndexError, "index %i out of range %i", *p, size);
++p;
}
s->beg = s->idx[0];
}
return s->n;
}
static struct NARRAY *
na_flatten_temporarily(struct NARRAY *dst, struct NARRAY *src)
{
/* Not normal construction !! Do not wrap as object ! */
dst->shape = &(dst->total);
dst->rank = 1;
dst->total = src->total;
dst->type = src->type;
dst->ptr = src->ptr;
dst->ref = src->ref;
return dst;
}
#define na_flatten_temp(ary) \
{ary = na_flatten_temporarily(ALLOCA_N(struct NARRAY,1), ary);}
/* free index memory */
static void na_free_slice_index(struct slice *s, int n)
{
while (n-->0)
if (s[n].idx != NULL) xfree(s[n].idx);
}
static int na_index_test(volatile VALUE idx, int shape, struct slice *sl)
{
int size;
struct NARRAY *na;
switch(TYPE(idx)) {
case T_FIXNUM:
/* scalar slice */
na_index_scalar(FIX2LONG(idx),shape,sl);
return 1;
case T_FLOAT:
/* scalar slice */
na_index_scalar(NUM2LONG(idx),shape,sl);
return 1;
case T_NIL:
case T_TRUE:
/* entire slice */
sl->n = shape;
sl->beg = 0;
sl->step = 1;
sl->idx = NULL;
return shape;
case T_ARRAY:
/* Array Index */
idx = na_cast_object(idx,NA_LINT);
GetNArray(idx,na);
size = na_ary_to_index(na,shape,sl);
return size;
default:
/* Range object */
if (rb_obj_is_kind_of(idx, rb_cRange)) {
size = na_index_range(idx,shape,sl);
}
else
/* NArray index */
if (NA_IsNArray(idx)) {
GetNArray(idx,na);
size = na_ary_to_index(na,shape,sl);
}
else
/* NO ALLOWED */
if (TYPE(idx)==T_BIGNUM) {
rb_raise(rb_eIndexError, "BigNum is not allowed");
}
else
rb_raise(rb_eIndexError, "not allowed type");
}
return size;
}
static int
na_index_analysis(int nidx, VALUE *idx, struct NARRAY *ary, struct slice *sl)
{
int i, j, k, total=1, size;
int multi_ellip=0;
for (i=j=0; i<nidx; ++i) {
if (TYPE(idx[i])==T_FALSE) {
if (multi_ellip!=0)
rb_raise(rb_eIndexError, "multiple ellipsis-dimension is not allowd");
for (k=ary->rank-nidx+1; k>0; --k,++j) {
size = na_index_test( Qtrue, ary->shape[j], &sl[j] );
if (size != 1)
total *= size;
}
multi_ellip = 1;
} else {
if (j < ary->rank) {
size = na_index_test( idx[i], ary->shape[j], &sl[j] );
if (size != 1)
total *= size;
}
++j;
}
}
if (j != ary->rank)
rb_raise(rb_eIndexError, "# of index=%i != ary.dim=%i", j, ary->rank);
return total;
}
/* -------------------- Class Dimension -------------------- */
int
na_shrink_class(int class_dim, int *shrink)
{
int i;
for (i=0; i<class_dim; ++i) {
if (shrink[i]==0) /* non-trim dimention */
return 0;
}
return 1; /* all trim */
}
/* remove single-element rank */
VALUE
na_shrink_rank(VALUE obj, int class_dim, int *shrink)
{
int i, j;
struct NARRAY *ary;
GetNArray(obj,ary);
if (ary->rank < class_dim)
return obj;
for (j=i=0; i<class_dim; ++i) {
if (ary->shape[i]!=1 || shrink[i]==0) /* not trim */
++j;
}
if (j>0) /* if non-trim dimensions exist, */
j = class_dim; /* then do not trim class_dimension. */
/* if (j==0) then all trim. */
for (i=class_dim; i<ary->rank; ++i) {
if (ary->shape[i]!=1 || shrink[i]==0) { /* not trim */
if (i>j) ary->shape[j] = ary->shape[i];
++j;
}
}
ary->rank = j;
if (j==0 && ary->total==1) {
SetFuncs[NA_ROBJ][ary->type](1, &obj, 0, ary->ptr, 0);
}
return obj;
}
/* ------------------- bracket methods ------------------ */
/*
[] -- Reference method
*/
static VALUE
na_aref_slice(struct NARRAY *a2, struct slice *s2, VALUE klass, int flag)
{
int i, ndim, class_dim, *shape, *shrink;
VALUE extr;
struct NARRAY *a1;
struct slice *s1;
ndim = a2->rank;
shape = ALLOCA_N(int,ndim);
shrink = ALLOCA_N(int,ndim);
for (i=0; i<ndim; ++i) {
shape[i] = s2[i].n;
if (shape[i]==1 && s2[i].step==0) /* shrink? */
shrink[i] = 1;
else
shrink[i] = 0;
}
class_dim = na_class_dim(klass);
if (ndim < class_dim)
rb_raise(rb_eRuntimeError,
"dimension(%i) is smaller than CLASS_DIMENSION(%i)",
ndim, class_dim);
if ((!flag) && class_dim>0 && na_shrink_class(class_dim,shrink))
klass = cNArray;
extr = na_make_object( a2->type, ndim, shape, klass );
GetNArray(extr,a1);
s1 = ALLOC_N(struct slice, ndim+1);
na_set_slice_1obj(ndim,s1,a1->shape);
na_init_slice( s1, ndim, shape, na_sizeof[a2->type] );
na_init_slice( s2, ndim, a2->shape, na_sizeof[a2->type] );
na_loop_index_ref( a1, a2, s1, s2, SetFuncs[a2->type][a2->type] );
xfree(s1);
if (!flag)
extr = na_shrink_rank(extr,class_dim,shrink);
return extr;
}
static VALUE
na_aref_single_dim_array(VALUE self, volatile VALUE vidx)
{
int total;
struct NARRAY *a1, *a2, *aidx;
struct slice *s1, *s2;
VALUE v;
GetNArray( self, a1 );
vidx = na_cast_object( vidx, NA_LINT );
GetNArray(vidx,aidx);
/* make Slice from index */
s1 = ALLOCA_N(struct slice, 2);
total = na_ary_to_index( aidx, a1->total, s1 );
if (total==0) {
return na_make_empty(a1->type,cNArray);
}
else {
/* create New NArray & 1-dimentionize */
v = na_make_object( a1->type, aidx->rank, aidx->shape, CLASS_OF(vidx) );
GetNArray(v,a2);
if (a2->rank>1) na_flatten_temp(a2);
if (a1->rank>1) na_flatten_temp(a1);
/* Slice for Destination array */
s2 = ALLOCA_N(struct slice, 2);
na_set_slice_1obj(1,s2,a2->shape);
/* Iteration */
na_init_slice( s2, 1, a2->shape, na_sizeof[a1->type] );
na_init_slice( s1, 1, a1->shape, na_sizeof[a1->type] );
na_loop_index_ref( a2, a1, s2, s1, SetFuncs[a1->type][a1->type] );
}
na_free_slice_index(s1,1);
return v;
}
static VALUE
na_aref_single_dim(VALUE self, VALUE idx, int flag)
{
int size;
VALUE v;
struct NARRAY *ary, *arynew;
struct slice *sl;
GetNArray(self,ary);
sl = ALLOCA_N(struct slice, 2);
size = na_index_test(idx, ary->total, sl);
if ( size == 1 ) {
if (flag || sl->step!=0) {
/* single-element NArray */
v = na_make_object(ary->type,1,&size,cNArray);
GetNArray(v,arynew);
SetFuncs[ary->type][ary->type](1, arynew->ptr,0, NA_PTR(ary,sl->beg),0);
} else {
SetFuncs[NA_ROBJ][ary->type](1, &v,0, NA_PTR(ary,sl->beg),0);
}
}
else
if ( size > 1 ) {
if ( ary->rank > 1 ) /* 1-dimensional serial index */
na_flatten_temp(ary);
v = na_aref_slice(ary, sl, CLASS_OF(self), flag);
}
else /* size < 1 */ {
v = na_make_empty(ary->type,cNArray);
}
/* na_free_slice_index(sl,1); free index memory */
return v;
}
static VALUE
na_aref_multi_dim_single_elm(VALUE self, struct slice *sl, int flag)
{
int i, rank, pos, *shape;
struct NARRAY *ary, *arynew;
VALUE v;
ary = (struct NARRAY *)DATA_PTR(self); /* type is already checked */
/* check rank-shrink; whether return NArray or Element */
if (flag==0) {
rank = 0; /* [] */
for ( i=ary->rank; (i--)>0; ) {
if (sl[i].step!=0) ++rank;
}
}
else {
rank = ary->rank; /* slice() */
}
/* get position */
pos = 0;
for ( i=ary->rank; i-->0; ) {
pos = pos * ary->shape[i] + sl[i].beg;
}
if (rank==0) {
SetFuncs[NA_ROBJ][ary->type](1, &v, 0, NA_PTR(ary,pos), 0);
} else {
VALUE klass;
int class_dim;
klass = CLASS_OF(self);
class_dim = na_class_dim(klass);
if (rank < class_dim) rank = class_dim;
shape = ALLOCA_N(int, rank);
for (i=0;i<rank;++i) shape[i]=1;
v = na_make_object(ary->type,rank,shape,klass);
GetNArray(v,arynew);
SetFuncs[ary->type][ary->type](1, arynew->ptr, 0, NA_PTR(ary,pos), 0);
}
return v;
}
static VALUE
na_aref_multi_dim(VALUE self, int nidx, VALUE *idx, int flag)
{
VALUE v;
int size;
struct NARRAY *ary;
struct slice *sl;
GetNArray(self,ary);
if (ary->rank==0)
rb_raise(rb_eIndexError, "Cannot extract from Empty NArray");
/* make Slice */
sl = ALLOC_N(struct slice, ary->rank+1);
size = na_index_analysis(nidx, idx, ary, sl);
if ( size == 1 ) { /* return Single Element */
v = na_aref_multi_dim_single_elm(self, sl, flag);
}
else
if ( size > 1 ) {
v = na_aref_slice(ary, sl, CLASS_OF(self), flag);
}
else /* size < 1 */ {
v = na_make_empty(ary->type,cNArray);
}
na_free_slice_index(sl,ary->rank); /* free index memory */
xfree(sl);
return v;
}
/* vvv mask vvv */
static int
na_count_true_body(VALUE self)
{
struct NARRAY *ary;
int n, count=0;
u_int8_t *ptr;
GetNArray(self,ary);
if ( ary->type == NA_BYTE ) {
ptr = (u_int8_t *)ary->ptr;
n = ary->total;
for (; n; --n) {
if (*ptr++) ++count;
}
} else
rb_raise(rb_eTypeError,"cannot count_true NArray except BYTE type");
return count;
}
VALUE
na_count_true(VALUE self)
{
return( INT2NUM(na_count_true_body(self)) );
}
static int
na_count_false_body(VALUE self)
{
struct NARRAY *ary;
int n, count=0;
u_int8_t *ptr;
GetNArray(self,ary);
if ( ary->type == NA_BYTE ) {
ptr = (u_int8_t *)ary->ptr;
n = ary->total;
for (; n; --n) {
if (!*ptr++) ++count;
}
} else
rb_raise(rb_eTypeError,"cannot count_false NArray except BYTE type");
return count;
}
VALUE
na_count_false(VALUE self)
{
return( INT2NUM(na_count_false_body(self)) );
}
VALUE
na_aref_mask(VALUE self, VALUE mask)
{
int total, i;
struct NARRAY *a1, *am, *a2;
VALUE v;
GetNArray( self, a1 );
GetNArray( mask, am );
if (a1->total != am->total)
rb_raise(rb_eTypeError,"self.size(=%i) != mask.size(=%i)",
a1->total, am->total);
if (a1->rank != am->rank)
rb_raise(rb_eTypeError,"self.rank(=%i) != mask.rank(=%i)",
a1->rank, am->rank);
for (i=0; i<a1->rank; ++i)
if (a1->shape[i] != am->shape[i])
rb_raise(rb_eTypeError,"self.shape[%i](=%i) != mask.shape[%i](=%i)",
i, a1->shape[i], i, am->shape[i]);
total = na_count_true_body(mask);
v = na_make_object( a1->type, 1, &total, CLASS_OF(self) );
GetNArray(v,a2);
RefMaskFuncs[a1->type]
( a1->total, a2->ptr, na_sizeof[a2->type], a1->ptr,
na_sizeof[a1->type], am->ptr, 1 );
return(v);
}
/* ^^^ mask ^^^ */
/* method: [](idx1,idx2,...,idxN) */
static VALUE
na_aref_body(int nidx, VALUE *idx, VALUE self, int flag)
{
if (nidx==0) {
return na_clone(self);
}
if (nidx==1) {
if ( NA_IsNArray(idx[0]) ) {
if( NA_TYPE(idx[0]) == NA_BYTE ) /* then supposed to be a mask */
return na_aref_mask(self, idx[0]);
}
if ( na_class_dim(CLASS_OF(self)) != 1 ) {
if ( NA_IsArray(idx[0]) ) /* Array Index ? */
return na_aref_single_dim_array( self, idx[0] );
else
return na_aref_single_dim( self, idx[0], flag );
}
}
/* if (nidx>1) */
return na_aref_multi_dim( self, nidx, idx, flag );
}
/* method: [](idx1,idx2,...,idxN) */
VALUE na_aref(int argc, VALUE *argv, VALUE self)
{ return na_aref_body(argc, argv, self, 0); }
/* method: slice(idx1,idx2,...,idxN) */
VALUE na_slice(int argc, VALUE *argv, VALUE self)
{ return na_aref_body(argc, argv, self, 1); }
/*
[]= -- Set elements to specified indices
*/
/* make slice for array-set: a[0..-1,1..2] = 1 */
static void
na_make_slice_aset_fill(int rank, struct NARRAY *src_ary,
struct slice *src_slc, int *src_shape,
struct slice *dst_slc)
{
int i;
for (i=0; i<rank; ++i) {
src_shape[i] = 1; /* all 1 */
if ( (src_slc[i].n = dst_slc[i].n) < 1 )
rb_raise(rb_eIndexError, "dst_slice[%i].n=%i ???", i, dst_slc[i].n);
src_slc[i].beg = 0;
src_slc[i].idx = NULL;
src_slc[i].step = 0;
}
}
/* make slice for array-set */
static void
na_make_slice_aset(struct NARRAY *dst, struct NARRAY *src,
struct slice *s1, struct slice *s2, int *src_shape)
{
int i, j, idx_end;
/* count range index */
for (j=i=0; i<dst->rank; ++i) {
if ( s1[i].step !=0 ) { /* Range index */
/* rank check */
if ( j >= src->rank )
rb_raise(rb_eIndexError, "dst.range-dim=%i > src.dim=%i",
j+1, src->rank);
if ( s1[i].n == 0 ) {
/* Size is NOT specified:
a[0..nil] = other_array
a[0] = other_array
*/
s1[i].n = src->shape[j];
idx_end = s1[i].beg + (s1[i].n-1) * s1[i].step;
if ( idx_end < 0 || idx_end >= dst->shape[i] )
rb_raise(rb_eIndexError, "end-index=%i is out of dst.shape[%i]=%i",
idx_end, i, dst->shape[i]);
} else
/* Size is specified:
a[0..10] = other
*/
if ( src->shape[j] >1 && s1[i].n != src->shape[j] ) {
rb_raise(rb_eIndexError, "dst.shape[%i]=%i != src.shape[%i]=%i",
i, s1[i].n, j, src->shape[j]);
}
/* copy source shape */
src_shape[i] = src->shape[j++];
}
else /* if ( s1[i].n==1 )
Scalar index:
a[0, 0..-1] = other --- first rank is skipped.
*/
src_shape[i] = 1; /* insert dummy rank */
s2[i].beg = 0;
s2[i].idx = NULL;
s2[i].n = s1[i].n; /* repeate number is same as a1 index */
if ( s1[i].n >1 && src_shape[i]==1 ) /* Extensible index */
s2[i].step = 0;
else
s2[i].step = 1;
}
/* rank check */
if ( j != src->rank )
rb_raise(rb_eIndexError, "dst.range-dim=%i < src.dim=%i", j, src->rank);
}
/* Iterate with bifinc, src has extensible index */
void
na_aset_slice(struct NARRAY *dst, struct NARRAY *src, struct slice *dst_slc)
{
int rank = dst->rank;
int *src_shape;
struct slice *src_slc;
/* rank check */
if (rank < src->rank)
rb_raise(rb_eIndexError, "%i dst.ranks < %i src.ranks", rank, src->rank);
if (src->rank == 0)
rb_raise(rb_eIndexError, "cannot store empty array");
/* extend rank */
src_shape = ALLOCA_N(int, rank);
src_slc = ALLOC_N(struct slice, rank+1);
if (src->total==1)
na_make_slice_aset_fill( rank, src, src_slc, src_shape, dst_slc );
else
na_make_slice_aset( dst, src, dst_slc, src_slc, src_shape );
/* Iteration */
na_init_slice( dst_slc, rank, dst->shape, na_sizeof[dst->type] );
na_init_slice( src_slc, rank, src_shape, na_sizeof[src->type] );
na_loop_general( dst,src, dst_slc,src_slc, SetFuncs[dst->type][src->type] );
xfree(src_slc);
}
static void
na_aset_array_index( VALUE self, volatile VALUE idx, volatile VALUE val )
{
int i, total;
struct NARRAY *aidx, *src, *dst;
struct slice *sl;
GetNArray(self,dst);
idx = na_cast_object(idx,NA_LINT);
GetNArray(idx,aidx);
val = na_cast_unless_narray(val,dst->type);
GetNArray(val,src);
/* empty index -- do nothing */
if (aidx->total==0 && (src->total==0 || src->total==1))
return;
/* check rank */
if (aidx->rank != src->rank)
rb_raise( rb_eIndexError, "idx.rank=%i != src.rank=%i",
aidx->rank, src->rank );
/* check shape */
for (i=0;i<src->rank;++i)
if (aidx->shape[i] != src->shape[i] && src->shape[i] != 1)
rb_raise( rb_eIndexError, "idx.shape[%i]=%i != src.shape[%i]=%i",
i, aidx->shape[i], i, src->shape[i] );
/* make Slice from index */
sl = ALLOCA_N(struct slice,2);
total = na_ary_to_index( NA_STRUCT(idx), dst->total, sl );
/* 1-dimensionize */
if (dst->rank > 1) {
na_flatten_temp(dst);
}
if (src->rank > 1) {
na_flatten_temp(src);
}
na_aset_slice( dst, src, sl );
na_free_slice_index( sl, 1 ); /* free index memory */
}
static void
na_aset_single_dim(VALUE self, VALUE idx, volatile VALUE val)
{
int size;
struct NARRAY *src, *dst;
struct slice *sl;
GetNArray(self,dst);
if (dst->total==0)
rb_raise(rb_eRuntimeError, "cannot set value to empty array");
sl = ALLOCA_N(struct slice, 2);
size = na_index_test(idx, dst->total, sl);
if ( size == 1 ) {
if (NA_IsNArray(val)) {
GetNArray(val,src);
if ( src->total == 1 ) {
SetFuncs[dst->type][src->type](1, NA_PTR(dst,sl->beg),0, src->ptr,0);
return;
}
}
else if (TYPE(val)!=T_ARRAY) {
/* Storing single element:
a[1] = 1
*/
SetFuncs[dst->type][NA_ROBJ](1, NA_PTR(dst,sl->beg),0, &val,0);
return;
}
/* Beginning index:
a[1] = [1,2,3]
*/
sl[0].n = 0;
sl[0].step = 1;
}
else if ( size == 0 ) return; /* Empty index */
if ( dst->rank > 1 ) { /* 1-dimensionize */
na_flatten_temp(dst);
}
val = na_cast_unless_narray(val,dst->type);
GetNArray(val,src);
na_aset_slice( dst, src, sl );
na_free_slice_index(sl,1); /* free index memory */
}
static void
na_aset_multi_dim(VALUE self, int nidx, VALUE *idx, volatile VALUE val)
{
int i, pos, size;
struct NARRAY *dst, *src;
struct slice *sl;
GetNArray(self,dst);
if (dst->total==0)
rb_raise(rb_eRuntimeError, "cannot set value to empty array");
/* make Slice from index-argv */
sl = ALLOC_N(struct slice, dst->rank+1);
size = na_index_analysis( nidx, idx, dst, sl );
if ( size == 0 ) { xfree(sl); return; } /* Empty index */
if ( size == 1 ) {
if (NA_IsArray(val)) {
/* Beginning index:
a[2,3,4] = other
*/
val = na_cast_unless_narray(val,dst->type);
GetNArray(val,src);
if (src->total > 1)
for( i=0; i<src->rank; ++i ) {
sl[i].n = 0;
sl[i].step = 1;
}
}
else {
/* Single Element:
a[2,3,4] = 5
*/
for ( pos=0, i=dst->rank; i-->0; )
pos = pos * dst->shape[i] + sl[i].beg;
SetFuncs[dst->type][NA_ROBJ](1, NA_PTR(dst,pos), 0, &val, 0 );
xfree(sl);
return;
}
}
else
val = na_cast_unless_narray(val,dst->type);
GetNArray(val,src);
/* if ( size>1 ) */
/* Range index:
a[0..9,0] = other
*/
na_aset_slice( dst, src, sl );
na_free_slice_index(sl,nidx); /* free index memory */
xfree(sl);
}
static void
na_aset_fill(VALUE self, volatile VALUE val)
{
struct NARRAY *dst, *src;
struct slice *sl;
GetNArray(self,dst);
if (dst->total==0)
rb_raise(rb_eRuntimeError, "cannot set value to empty array");
if ( NA_IsArray(val) ) { /* store Array? */
sl = ALLOC_N(struct slice, dst->rank+1);
na_set_slice_1obj(dst->rank,sl,dst->shape);
val = na_cast_unless_narray(val,dst->type);
GetNArray(val,src);
na_aset_slice( dst, src, sl );
xfree(sl);
}
else {
na_fill( self, val ); /* Simple filling */
}
}
/* --- mask --- */
void
na_aset_mask(VALUE self, VALUE mask, VALUE val)
{
int size, step, i;
struct NARRAY *a1, *am, *a2;
GetNArray( self, a1 );
GetNArray( mask, am );
if (a1->total != am->total)
rb_raise(rb_eTypeError,"self.size(=%i) != mask.size(=%i)",
a1->total, am->total);
if (a1->rank != am->rank)
rb_raise(rb_eTypeError,"self.rank(=%i) != mask.rank(=%i)",
a1->rank, am->rank);
for (i=0; i<a1->rank; ++i)
if (a1->shape[i] != am->shape[i])
rb_raise(rb_eTypeError,"self.shape[%i](=%i) != mask.shape[%i](=%i)",
i, a1->shape[i], i, am->shape[i]);
size = na_count_true_body(mask);
val = na_cast_object(val,a1->type);
GetNArray( val, a2 );
if (a2->total == 1) {
step = 0;
} else if (a2->total == size) {
step = na_sizeof[a2->type];
} else {
rb_raise(rb_eTypeError,"val.length != mask.count_true");
}
SetMaskFuncs[a1->type]
( a1->total, a1->ptr, na_sizeof[a1->type],
a2->ptr, step, am->ptr, 1 );
}
/* method: []=(idx1,idx2,...,idxN,val) */
VALUE
na_aset(int nidx, VALUE *idx, VALUE self)
{
--nidx;
if (nidx==0) {
na_aset_fill( self, idx[0] );
}
else
if (nidx==1) {
if ( NA_IsNArray(idx[0]) ) {
if( NA_TYPE(idx[0]) == NA_BYTE ) { /* then supposed to be a mask */
na_aset_mask(self, idx[0], idx[1]);
return(idx[1]);
}
}
if ( NA_IsArray(idx[0]) ) /* Array Index ? */
na_aset_array_index( self, idx[0], idx[1] );
else
na_aset_single_dim( self, idx[0], idx[1] );
}
else
if (nidx>1) {
na_aset_multi_dim( self, nidx, idx, idx[nidx] );
}
else /* if (nidx<0) */
rb_raise( rb_eArgError, "No value specified" );
return idx[nidx];
}
|