Esempio n. 1
0
VALUE
rb_ca_count_not_masked (VALUE self)
{
  CArray *ca;
  Data_Get_Struct(self, CArray, ca);
  return SIZE2NUM(ca_count_not_masked(ca));
}
Esempio n. 2
0
/* method: size() -- returns the total number of typeents */
static VALUE
na_size(VALUE self)
{
    narray_t *na;
    GetNArray(self,na);
    return SIZE2NUM(na->size);
}
Esempio n. 3
0
static VALUE
rb_ca_s_sizeof (VALUE klass, VALUE rtype)
{
  int8_t data_type;
  ca_size_t bytes;
  rb_ca_guess_type_and_bytes(rtype, INT2NUM(0), &data_type, &bytes);
  return SIZE2NUM(bytes);
}
Esempio n. 4
0
/*
  Returns total byte size of NArray.
  @return [Integer] byte size.
 */
static VALUE
nary_byte_size(VALUE self)
{
    VALUE velmsz; //klass,
    narray_t *na;
    size_t sz;

    GetNArray(self,na);
    velmsz = rb_const_get(CLASS_OF(self), rb_intern(ELEMENT_BYTE_SIZE));
    sz = SIZE2NUM(NUM2SIZE(velmsz) * na->size);
    return sz;
}
Esempio n. 5
0
static inline dtype
yield_map_with_index(dtype x, size_t *c, VALUE *a, int nd, int md)
{
    int j;
    VALUE y;

    a[0] = m_data_to_num(x);
    for (j=0; j<=nd; j++) {
        a[j+1] = SIZE2NUM(c[j]);
    }
    y = rb_yield(rb_ary_new4(md,a));
    return m_num_to_data(y);
}
Esempio n. 6
0
static VALUE
rb_ca_ubrep_spec (VALUE self)
{
  volatile VALUE spec;
  CAUnboundRepeat *ca;
  int i;

  Data_Get_Struct(self, CAUnboundRepeat, ca);

  spec = rb_ary_new2(ca->rep_rank);
  for (i=0; i<ca->rep_rank; i++) {
    if ( ca->rep_dim[i] ) {
      rb_ary_store(spec, i, SIZE2NUM(ca->rep_dim[i]));
    }
    else {
      rb_ary_store(spec, i, ID2SYM(rb_intern("*")));
    }
  }

  return spec;
}
Esempio n. 7
0
/* method: shape() -- returns shape, array of the size of dimensions */
static VALUE
 na_shape(VALUE self)
{
    volatile VALUE v;
    narray_t *na;
    size_t i, n, c, s;

    GetNArray(self,na);
    n = NA_NDIM(na);
    if (TEST_COLUMN_MAJOR(na)) {
        c = n-1;
        s = -1;
    } else {
        c = 0;
        s = 1;
    }
    v = rb_ary_new2(n);
    for (i=0; i<n; i++) {
        rb_ary_push(v, SIZE2NUM(na->shape[c]));
        c += s;
    }
    return v;
}
Esempio n. 8
0
static int
carray_setup_i (CArray *ca,
                int8_t data_type, int8_t rank, ca_size_t *dim, ca_size_t bytes,
                CArray *mask, int allocate, int use_calloc)
{
  ca_size_t elements;
  double  length;
  int8_t i;
  ca_size_t k;

  /* check arguments */
  CA_CHECK_DATA_TYPE(data_type);
  CA_CHECK_RANK(rank);
  CA_CHECK_DIM(rank, dim);
  CA_CHECK_BYTES(data_type, bytes);

  /* calculate total number of elements */
  elements = 1;
  length = bytes;
  for (i=0; i<rank; i++) {
    elements *= dim[i];
    length *= dim[i];
  }

  if ( length > CA_LENGTH_MAX ) {
    rb_raise(rb_eRuntimeError, "too large byte length");
  }

  /* set values to the struct members */
  if ( allocate ) {
    ca->obj_type = CA_OBJ_ARRAY;
  }
  else {
    ca->obj_type = CA_OBJ_ARRAY_WRAP;
  }

  ca->data_type = data_type;
  ca->flags     = 0;
  ca->rank      = rank;
  ca->bytes     = bytes;
  ca->elements  = elements;
  ca->dim       = ALLOC_N(ca_size_t, rank);
  memcpy(ca->dim, dim, rank*sizeof(ca_size_t));

  if ( allocate ) {                                      /* allocate == true */

    /* allocate memory for entity */
    if ( use_calloc ) {
      /* ca->ptr = ALLOC_N(char, elements * bytes); */
      ca->ptr = malloc_with_check(elements * bytes);
      MEMZERO(ca->ptr, char, elements * bytes);
    }
    else {
      /* ca->ptr = ALLOC_N(char, elements * bytes); */
      ca->ptr = malloc_with_check(elements * bytes);
    }

    ca_mem_count += (double)(ca_length(ca));
    ca_mem_usage += (double)(ca_length(ca));

    /* initialize elements with Qnil for CA_OBJECT data_type */
    if ( allocate && data_type == CA_OBJECT ) {
      volatile VALUE zero = SIZE2NUM(0);
      VALUE *p = (VALUE *) ca->ptr;
      for (k=0; k<elements; k++) {
        *p++ = zero;
      }
    }

  }
Esempio n. 9
0
VALUE
na_reduce_dimension(int argc, VALUE *argv, int naryc, VALUE *naryv)
{
    int ndim, ndim0;
    int row_major;
    int i, r;
    size_t j;
    size_t len;
    ssize_t beg, step;
    VALUE v;
    narray_t *na;
    size_t m;
    VALUE reduce;

    if (naryc<1) {
        rb_raise(rb_eRuntimeError,"must be positive: naryc=%d", naryc);
    }
    GetNArray(naryv[0],na);
    reduce = na->reduce;
    if (argc==0) {
        //printf("pass argc=0 reduce=%d\n",NUM2INT(reduce));
        return reduce;
    }
    ndim = ndim0 = na->ndim;
    row_major = TEST_COLUMN_MAJOR(naryv[0]);
    for (i=1; i<naryc; i++) {
        GetNArray(naryv[i],na);
        if (TEST_COLUMN_MAJOR(naryv[i]) != row_major) {
            rb_raise(nary_eDimensionError,"dimension order is different");
        }
        if (na->ndim > ndim) {
            ndim = na->ndim;
        }
    }
    if (ndim != ndim0) {
        j = FIX2ULONG(reduce) << (ndim-ndim0);
        reduce = ULONG2NUM(j);
        if (!FIXNUM_P(reduce)) {
            rb_raise(nary_eDimensionError,"reduce has too many bits");
        }
    }
    //printf("argc=%d\n",argc);

    m = 0;
    reduce = Qnil;
    for (i=0; i<argc; i++) {
        v = argv[i];
        //printf("argv[%d]=",i);rb_p(v);
        if (TYPE(v)==T_FIXNUM) {
            beg = FIX2INT(v);
            if (beg<0) beg+=ndim;
            if (beg>=ndim || beg<0) {
                rb_raise(nary_eDimensionError,"dimension is out of range");
            }
            len = 1;
            step = 0;
            //printf("beg=%d step=%d len=%d\n",beg,step,len);
        } else if (rb_obj_is_kind_of(v,rb_cRange) ||
                   rb_obj_is_kind_of(v,na_cStep)) {
            nary_step_array_index( v, ndim, &len, &beg, &step );
        } else {
            rb_raise(nary_eDimensionError, "invalid dimension argument %s",
                     rb_obj_classname(v));
        }
        for (j=0; j<len; j++) {
            r = beg + step*j;
            if (row_major)
                r = ndim-1-r;
            if (reduce==Qnil) {
              if ( r < (ssize_t)sizeof(size_t) ) {
                    m |= ((size_t)1) << r;
                    continue;
                } else {
                    reduce = SIZE2NUM(m);
                }
            }
            v = rb_funcall( INT2FIX(1), rb_intern("<<"), 1, INT2FIX(r) );
            reduce = rb_funcall( reduce, rb_intern("|"), 1, v );
        }
    }
    if (reduce==Qnil) reduce = SIZE2NUM(m);
    return reduce;
}