ext/numo/narray/index.c in numo-narray-0.9.0.4 vs ext/numo/narray/index.c in numo-narray-0.9.0.5

- old
+ new

@@ -1,9 +1,9 @@ /* index.c Numerical Array Extension for Ruby - (C) Copyright 1999-2016 by Masahiro TANAKA + (C) Copyright 1999-2017 by Masahiro TANAKA */ //#define NARRAY_C #include <string.h> #include <ruby.h> @@ -72,11 +72,16 @@ static VALUE sym_tilde; static VALUE sym_rest; static ID id_beg; static ID id_end; static ID id_exclude_end; -static ID id_each, id_step; +static ID id_each; +static ID id_step; +static ID id_dup; +static ID id_bracket; +static ID id_shift_left; +static ID id_mask; void na_index_set_step(na_index_arg_t *q, int i, size_t n, size_t beg, ssize_t step) { @@ -146,11 +151,11 @@ GetNArray(a,na); if (NA_NDIM(na) != 1) { rb_raise(rb_eIndexError, "should be 1-d NArray"); } n = NA_SIZE(na); - idx = rb_narray_new(cIndex,1,&n); + idx = nary_new(cIndex,1,&n); na_store(idx,a); GetNArrayData(idx,nidx); nidxp = (ssize_t*)nidx->ptr; q->idx = ALLOC_N(size_t, n); @@ -380,12 +385,12 @@ } na2->base.shape[j] = size = q[i].n; if (q[i].reduce != 0) { - m = rb_funcall(INT2FIX(1),rb_intern("<<"),1,INT2FIX(j)); - na2->base.reduce = rb_funcall(m,rb_intern("|"),1,na2->base.reduce); + m = rb_funcall(INT2FIX(1),id_shift_left,1,INT2FIX(j)); + na2->base.reduce = rb_funcall(m,'|',1,na2->base.reduce); } // array index if (q[i].idx != NULL) { index = q[i].idx; @@ -407,11 +412,11 @@ } static void na_index_aref_naview(narray_view_t *na1, narray_view_t *na2, - na_index_arg_t *q, int ndim, int keep_dim) + na_index_arg_t *q, ssize_t elmsz, int ndim, int keep_dim) { int i, j; ssize_t total=1; for (i=j=0; i<ndim; i++) { @@ -429,15 +434,19 @@ } na2->base.shape[j] = size = q[i].n; if (q[i].reduce != 0) { - VALUE m = rb_funcall(INT2FIX(1),rb_intern("<<"),1,INT2FIX(j)); - na2->base.reduce = rb_funcall(m,rb_intern("|"),1,na2->base.reduce); + VALUE m = rb_funcall(INT2FIX(1),id_shift_left,1,INT2FIX(j)); + na2->base.reduce = rb_funcall(m,'|',1,na2->base.reduce); } - if (q[i].idx != NULL && SDX_IS_INDEX(sdx1)) { + if (q[i].orig_dim >= na1->base.ndim) { + // new dimension + SDX_SET_STRIDE(na2->stridx[j], elmsz); + } + else if (q[i].idx != NULL && SDX_IS_INDEX(sdx1)) { // index <- index int k; size_t *index = q[i].idx; SDX_SET_INDEX(na2->stridx[j], index); q[i].idx = NULL; @@ -544,10 +553,11 @@ int keep_dim = data->keep_dim; int ndim_new; VALUE view; narray_view_t *na2; + ssize_t elmsz; na_index_parse_args(args, na1, q, ndim); if (na_debug_flag) print_index_arg(q,ndim); @@ -563,20 +573,22 @@ na_alloc_shape((narray_t*)na2, ndim_new); na2->stridx = ALLOC_N(stridx_t,ndim_new); + elmsz = nary_element_stride(self); + switch(na1->type) { case NARRAY_DATA_T: case NARRAY_FILEMAP_T: - na_index_aref_nadata((narray_data_t *)na1,na2,q,na_get_elmsz(self),ndim,keep_dim); + na_index_aref_nadata((narray_data_t *)na1,na2,q,elmsz,ndim,keep_dim); na2->data = self; break; case NARRAY_VIEW_T: na2->offset = ((narray_view_t *)na1)->offset; na2->data = ((narray_view_t *)na1)->data; - na_index_aref_naview((narray_view_t *)na1,na2,q,ndim,keep_dim); + na_index_aref_naview((narray_view_t *)na1,na2,q,elmsz,ndim,keep_dim); break; } if (store) { na_get_pointer_for_write(store); // allocate memory na_store(na_flatten_dim(store,0),view); @@ -612,16 +624,16 @@ args = rb_ary_new4(argc,argv); if (argc == 1 && result_nd == 1) { idx = argv[0]; if (rb_obj_is_kind_of(idx, rb_cArray)) { - idx = rb_apply(numo_cNArray,rb_intern("[]"),idx); + idx = rb_apply(numo_cNArray,id_bracket,idx); } if (rb_obj_is_kind_of(idx, numo_cNArray)) { GetNArray(idx,nidx); if (NA_NDIM(nidx)>1) { - store = rb_narray_new(CLASS_OF(self),NA_NDIM(nidx),NA_SHAPE(nidx)); + store = nary_new(CLASS_OF(self),NA_NDIM(nidx),NA_SHAPE(nidx)); idx = na_flatten(idx); RARRAY_ASET(args,0,idx); } } // flatten should be done only for narray-view with non-uniform stride. @@ -648,15 +660,15 @@ na_aref_main(int nidx, VALUE *idx, VALUE self, int keep_dim, int nd) { na_index_arg_to_internal_order(nidx, idx, self); if (nidx==0) { - return rb_funcall(self,rb_intern("copy"),0); + return rb_funcall(self,id_dup,0); } if (nidx==1) { if (CLASS_OF(*idx)==numo_cBit) { - return rb_funcall(*idx,rb_intern("mask"),1,self); + return rb_funcall(*idx,id_mask,1,self); } } return na_aref_md(nidx, idx, self, keep_dim, nd); } @@ -779,11 +791,11 @@ return check_index_count(argc, na->ndim, count_new, count_rest); } break; default: if (!stride) { - stride = na_get_elmsz(self); + stride = nary_element_stride(self); } if (argc==1 && j==1) { x = na_range_check(idx[0], na->size, 0); *pos_idx = stride * x; } @@ -806,21 +818,25 @@ void Init_nary_index() { rb_define_method(cNArray, "slice", na_slice, -1); - sym_ast = ID2SYM(rb_intern("*")); - sym_all = ID2SYM(rb_intern("all")); - sym_minus = ID2SYM(rb_intern("-")); - sym_new = ID2SYM(rb_intern("new")); - sym_reverse = ID2SYM(rb_intern("reverse")); - sym_plus = ID2SYM(rb_intern("+")); - //sym_reduce = ID2SYM(rb_intern("reduce")); - sym_sum = ID2SYM(rb_intern("sum")); - sym_tilde = ID2SYM(rb_intern("~")); - sym_rest = ID2SYM(rb_intern("rest")); - id_beg = rb_intern("begin"); - id_end = rb_intern("end"); + sym_ast = ID2SYM(rb_intern("*")); + sym_all = ID2SYM(rb_intern("all")); + sym_minus = ID2SYM(rb_intern("-")); + sym_new = ID2SYM(rb_intern("new")); + sym_reverse = ID2SYM(rb_intern("reverse")); + sym_plus = ID2SYM(rb_intern("+")); + //sym_reduce = ID2SYM(rb_intern("reduce")); + sym_sum = ID2SYM(rb_intern("sum")); + sym_tilde = ID2SYM(rb_intern("~")); + sym_rest = ID2SYM(rb_intern("rest")); + id_beg = rb_intern("begin"); + id_end = rb_intern("end"); id_exclude_end = rb_intern("exclude_end?"); - id_each = rb_intern("each"); - id_step = rb_intern("step"); + id_each = rb_intern("each"); + id_step = rb_intern("step"); + id_dup = rb_intern("dup"); + id_bracket = rb_intern("[]"); + id_shift_left = rb_intern("<<"); + id_mask = rb_intern("mask"); }