ext/cumo/narray/array.c in cumo-0.1.0 vs ext/cumo/narray/array.c in cumo-0.1.1
- old
+ new
@@ -3,180 +3,180 @@
// mdai: Multi-Dimensional Array Investigation
typedef struct {
size_t shape;
VALUE val;
-} na_mdai_item_t;
+} cumo_na_mdai_item_t;
typedef struct {
int capa;
- na_mdai_item_t *item;
+ cumo_na_mdai_item_t *item;
int type; // Ruby numeric type - investigated separately
VALUE na_type; // NArray type
VALUE int_max;
-} na_mdai_t;
+} cumo_na_mdai_t;
// Order of Ruby object.
-enum { NA_NONE, NA_BIT, NA_INT32, NA_INT64, NA_RATIONAL,
- NA_DFLOAT, NA_DCOMPLEX, NA_ROBJ, NA_NTYPES };
+enum { CUMO_NA_NONE, CUMO_NA_BIT, CUMO_NA_INT32, CUMO_NA_INT64, CUMO_NA_RATIONAL,
+ CUMO_NA_DFLOAT, CUMO_NA_DCOMPLEX, CUMO_NA_ROBJ, CUMO_NA_NTYPES };
-static ID id_begin;
-static ID id_end;
-static ID id_step;
-static ID id_abs;
-static ID id_cast;
-static ID id_le;
-static ID id_Complex;
+static ID cumo_id_begin;
+static ID cumo_id_end;
+static ID cumo_id_step;
+static ID cumo_id_abs;
+static ID cumo_id_cast;
+static ID cumo_id_le;
+static ID cumo_id_Complex;
static VALUE
- na_object_type(int type, VALUE v)
+ cumo_na_object_type(int type, VALUE v)
{
static VALUE int32_max = Qnil;
if (NIL_P(int32_max))
int32_max = ULONG2NUM(2147483647);
switch(TYPE(v)) {
case T_TRUE:
case T_FALSE:
- if (type<NA_BIT)
- return NA_BIT;
+ if (type<CUMO_NA_BIT)
+ return CUMO_NA_BIT;
return type;
#if SIZEOF_LONG == 4
case T_FIXNUM:
- if (type<NA_INT32)
- return NA_INT32;
+ if (type<CUMO_NA_INT32)
+ return CUMO_NA_INT32;
return type;
case T_BIGNUM:
- if (type<NA_INT64) {
- v = rb_funcall(v,id_abs,0);
- if (RTEST(rb_funcall(v,id_le,1,int32_max))) {
- if (type<NA_INT32)
- return NA_INT32;
+ if (type<CUMO_NA_INT64) {
+ v = rb_funcall(v,cumo_id_abs,0);
+ if (RTEST(rb_funcall(v,cumo_id_le,1,int32_max))) {
+ if (type<CUMO_NA_INT32)
+ return CUMO_NA_INT32;
} else {
- return NA_INT64;
+ return CUMO_NA_INT64;
}
}
return type;
#elif SIZEOF_LONG == 8
case T_FIXNUM:
- if (type<NA_INT64) {
+ if (type<CUMO_NA_INT64) {
long x = NUM2LONG(v);
if (x<0) x=-x;
if (x<=2147483647) {
- if (type<NA_INT32)
- return NA_INT32;
+ if (type<CUMO_NA_INT32)
+ return CUMO_NA_INT32;
} else {
- return NA_INT64;
+ return CUMO_NA_INT64;
}
}
return type;
case T_BIGNUM:
- if (type<NA_INT64)
- return NA_INT64;
+ if (type<CUMO_NA_INT64)
+ return CUMO_NA_INT64;
return type;
#else
case T_FIXNUM:
case T_BIGNUM:
- if (type<NA_INT64) {
- v = rb_funcall(v,id_abs,0);
- if (RTEST(rb_funcall(v,id_le,1,int32_max))) {
- if (type<NA_INT32)
- return NA_INT32;
+ if (type<CUMO_NA_INT64) {
+ v = rb_funcall(v,cumo_id_abs,0);
+ if (RTEST(rb_funcall(v,cumo_id_le,1,int32_max))) {
+ if (type<CUMO_NA_INT32)
+ return CUMO_NA_INT32;
} else {
- return NA_INT64;
+ return CUMO_NA_INT64;
}
}
return type;
#endif
case T_FLOAT:
- if (type<NA_DFLOAT)
- return NA_DFLOAT;
+ if (type<CUMO_NA_DFLOAT)
+ return CUMO_NA_DFLOAT;
return type;
case T_NIL:
return type;
default:
- if (CLASS_OF(v) == rb_const_get( rb_cObject, id_Complex )) {
- return NA_DCOMPLEX;
+ if (rb_obj_class(v) == rb_const_get( rb_cObject, cumo_id_Complex )) {
+ return CUMO_NA_DCOMPLEX;
}
}
- return NA_ROBJ;
+ return CUMO_NA_ROBJ;
}
#define MDAI_ATTR_TYPE(tp,v,attr) \
- {tp = na_object_type(tp,rb_funcall(v,id_##attr,0));}
+ {tp = cumo_na_object_type(tp,rb_funcall(v,cumo_id_##attr,0));}
-static int na_mdai_object_type(int type, VALUE v)
+static int cumo_na_mdai_object_type(int type, VALUE v)
{
if (rb_obj_is_kind_of(v, rb_cRange)) {
MDAI_ATTR_TYPE(type,v,begin);
MDAI_ATTR_TYPE(type,v,end);
- } else if (rb_obj_is_kind_of(v, na_cStep)) {
+ } else if (rb_obj_is_kind_of(v, cumo_na_cStep)) {
MDAI_ATTR_TYPE(type,v,begin);
MDAI_ATTR_TYPE(type,v,end);
MDAI_ATTR_TYPE(type,v,step);
} else {
- type = na_object_type(type,v);
+ type = cumo_na_object_type(type,v);
}
return type;
}
-static na_mdai_t *
-na_mdai_alloc(VALUE ary)
+static cumo_na_mdai_t *
+cumo_na_mdai_alloc(VALUE ary)
{
int i, n=4;
- na_mdai_t *mdai;
+ cumo_na_mdai_t *mdai;
- mdai = ALLOC(na_mdai_t);
+ mdai = ALLOC(cumo_na_mdai_t);
mdai->capa = n;
- mdai->item = ALLOC_N( na_mdai_item_t, n );
+ mdai->item = ALLOC_N( cumo_na_mdai_item_t, n );
for (i=0; i<n; i++) {
mdai->item[i].shape = 0;
mdai->item[i].val = Qnil;
}
mdai->item[0].val = ary;
- mdai->type = NA_NONE;
+ mdai->type = CUMO_NA_NONE;
mdai->na_type = Qnil;
return mdai;
}
static void
-na_mdai_realloc(na_mdai_t *mdai, int n_extra)
+cumo_na_mdai_realloc(cumo_na_mdai_t *mdai, int n_extra)
{
int i, n;
i = mdai->capa;
mdai->capa += n_extra;
n = mdai->capa;
- REALLOC_N( mdai->item, na_mdai_item_t, n );
+ REALLOC_N( mdai->item, cumo_na_mdai_item_t, n );
for (; i<n; i++) {
mdai->item[i].shape = 0;
mdai->item[i].val = Qnil;
}
}
static void
-na_mdai_free(void *ptr)
+cumo_na_mdai_free(void *ptr)
{
- na_mdai_t *mdai = (na_mdai_t*)ptr;
+ cumo_na_mdai_t *mdai = (cumo_na_mdai_t*)ptr;
xfree(mdai->item);
xfree(mdai);
}
/* investigate ndim, shape, type of Array */
static int
-na_mdai_investigate(na_mdai_t *mdai, int ndim)
+cumo_na_mdai_investigate(cumo_na_mdai_t *mdai, int ndim)
{
ssize_t i;
int j;
size_t len, length;
double dbeg, dstep;
@@ -195,46 +195,46 @@
if (mdai->item[j].val == v)
rb_raise(rb_eStandardError,
"cannot convert from a recursive Array to NArray");
}
if ( ndim >= mdai->capa ) {
- na_mdai_realloc(mdai,4);
+ cumo_na_mdai_realloc(mdai,4);
}
mdai->item[ndim].val = v;
- if ( na_mdai_investigate(mdai,ndim+1) ) {
+ if ( cumo_na_mdai_investigate(mdai,ndim+1) ) {
len--; /* Array is empty */
}
}
else
- if (rb_obj_is_kind_of(v, rb_cRange) || rb_obj_is_kind_of(v, na_cStep)) {
- nary_step_sequence(v,&length,&dbeg,&dstep);
+ if (rb_obj_is_kind_of(v, rb_cRange) || rb_obj_is_kind_of(v, cumo_na_cStep)) {
+ cumo_na_step_sequence(v,&length,&dbeg,&dstep);
len += length-1;
- mdai->type = na_mdai_object_type(mdai->type, v);
+ mdai->type = cumo_na_mdai_object_type(mdai->type, v);
}
- else if (IsNArray(v)) {
+ else if (CumoIsNArray(v)) {
int r;
- narray_t *na;
- GetNArray(v,na);
+ cumo_narray_t *na;
+ CumoGetNArray(v,na);
if ( na->ndim == 0 ) {
len--; /* NArray is empty */
} else {
if ( ndim+na->ndim > mdai->capa ) {
- na_mdai_realloc(mdai,((na->ndim-1)/4+1)*4);
+ cumo_na_mdai_realloc(mdai,((na->ndim-1)/4+1)*4);
}
for ( j=0,r=ndim; j < na->ndim ; j++,r++ ) {
if ( mdai->item[r].shape < na->shape[j] )
mdai->item[r].shape = na->shape[j];
}
}
// type
if (NIL_P(mdai->na_type)) {
- mdai->na_type = CLASS_OF(v);
+ mdai->na_type = rb_obj_class(v);
} else {
- mdai->na_type = na_upcast(CLASS_OF(v), mdai->na_type);
+ mdai->na_type = cumo_na_upcast(rb_obj_class(v), mdai->na_type);
}
} else {
- mdai->type = na_mdai_object_type(mdai->type, v);
+ mdai->type = cumo_na_mdai_object_type(mdai->type, v);
}
}
if (len==0) return 1; /* this array is empty */
if (mdai->item[ndim-1].shape < len) {
@@ -243,69 +243,69 @@
return 0;
}
static inline int
-na_mdai_ndim(na_mdai_t *mdai)
+cumo_na_mdai_ndim(cumo_na_mdai_t *mdai)
{
int i;
// Dimension
for (i=0; i < mdai->capa && mdai->item[i].shape > 0; i++) ;
return i;
}
static inline void
-na_mdai_shape(na_mdai_t *mdai, int ndim, size_t *shape)
+cumo_na_mdai_shape(cumo_na_mdai_t *mdai, int ndim, size_t *shape)
{
int i;
for (i=0; i<ndim; i++) {
shape[i] = mdai->item[i].shape;
}
}
static VALUE
-na_mdai_dtype_numeric(int type)
+cumo_na_mdai_dtype_numeric(int type)
{
VALUE tp;
// DataType
switch(type) {
- case NA_BIT:
+ case CUMO_NA_BIT:
tp = cumo_cBit;
break;
- case NA_INT32:
+ case CUMO_NA_INT32:
tp = cumo_cInt32;
break;
- case NA_INT64:
+ case CUMO_NA_INT64:
tp = cumo_cInt64;
break;
- case NA_DFLOAT:
+ case CUMO_NA_DFLOAT:
tp = cumo_cDFloat;
break;
- case NA_DCOMPLEX:
+ case CUMO_NA_DCOMPLEX:
tp = cumo_cDComplex;
break;
- case NA_ROBJ:
+ case CUMO_NA_ROBJ:
tp = cumo_cRObject;
break;
default:
tp = Qnil;
}
return tp;
}
static VALUE
-na_mdai_dtype(na_mdai_t *mdai)
+cumo_na_mdai_dtype(cumo_na_mdai_t *mdai)
{
VALUE tp;
- tp = na_mdai_dtype_numeric(mdai->type);
+ tp = cumo_na_mdai_dtype_numeric(mdai->type);
if (!NIL_P(mdai->na_type)) {
if (NIL_P(tp)) {
tp = mdai->na_type;
} else {
- tp = na_upcast(mdai->na_type,tp);
+ tp = cumo_na_upcast(mdai->na_type,tp);
}
}
return tp;
}
@@ -325,129 +325,129 @@
static inline void
check_subclass_of_narray(VALUE dtype)
{
if (RTEST(rb_obj_is_kind_of(dtype, rb_cClass))) {
- if (RTEST(rb_funcall(dtype, id_le, 1, cNArray))) {
+ if (RTEST(rb_funcall(dtype, cumo_id_le, 1, cNArray))) {
return;
}
}
- rb_raise(nary_eCastError, "cannot convert to NArray");
+ rb_raise(cumo_na_eCastError, "cannot convert to NArray");
}
static size_t
-na_mdai_memsize(const void *ptr)
+cumo_na_mdai_memsize(const void *ptr)
{
- const na_mdai_t *mdai = (const na_mdai_t*)ptr;
+ const cumo_na_mdai_t *mdai = (const cumo_na_mdai_t*)ptr;
- return sizeof(na_mdai_t) + mdai->capa * sizeof(na_mdai_item_t);
+ return sizeof(cumo_na_mdai_t) + mdai->capa * sizeof(cumo_na_mdai_item_t);
}
static const rb_data_type_t mdai_data_type = {
"Cumo::NArray/mdai",
- {NULL, na_mdai_free, na_mdai_memsize,},
+ {NULL, cumo_na_mdai_free, cumo_na_mdai_memsize,},
0, 0, RUBY_TYPED_FREE_IMMEDIATELY|RUBY_TYPED_WB_PROTECTED
};
static void
-na_composition3_ary(VALUE ary, VALUE *ptype, VALUE *pshape, VALUE *pnary)
+cumo_na_composition3_ary(VALUE ary, VALUE *ptype, VALUE *pshape, VALUE *pnary)
{
VALUE vmdai;
- na_mdai_t *mdai;
+ cumo_na_mdai_t *mdai;
int i, ndim;
size_t *shape;
VALUE dtype, dshape;
- mdai = na_mdai_alloc(ary);
+ mdai = cumo_na_mdai_alloc(ary);
vmdai = TypedData_Wrap_Struct(rb_cData, &mdai_data_type, (void*)mdai);
- if ( na_mdai_investigate(mdai, 1) ) {
+ if ( cumo_na_mdai_investigate(mdai, 1) ) {
// empty
dtype = update_type(ptype, cumo_cInt32);
if (pshape) {
*pshape = rb_ary_new3(1, INT2FIX(0));
}
if (pnary) {
check_subclass_of_narray(dtype);
shape = ALLOCA_N(size_t, 1);
shape[0] = 0;
- *pnary = nary_new(dtype, 1, shape);
+ *pnary = cumo_na_new(dtype, 1, shape);
}
} else {
- ndim = na_mdai_ndim(mdai);
+ ndim = cumo_na_mdai_ndim(mdai);
shape = ALLOCA_N(size_t, ndim);
- na_mdai_shape(mdai, ndim, shape);
- dtype = update_type(ptype, na_mdai_dtype(mdai));
+ cumo_na_mdai_shape(mdai, ndim, shape);
+ dtype = update_type(ptype, cumo_na_mdai_dtype(mdai));
if (pshape) {
dshape = rb_ary_new2(ndim);
for (i=0; i<ndim; i++) {
rb_ary_push(dshape, SIZET2NUM(shape[i]));
}
*pshape = dshape;
}
if (pnary) {
check_subclass_of_narray(dtype);
- *pnary = nary_new(dtype, ndim, shape);
+ *pnary = cumo_na_new(dtype, ndim, shape);
}
}
RB_GC_GUARD(vmdai);
}
static void
-na_composition3(VALUE obj, VALUE *ptype, VALUE *pshape, VALUE *pnary)
+cumo_na_composition3(VALUE obj, VALUE *ptype, VALUE *pshape, VALUE *pnary)
{
VALUE dtype, dshape;
if (TYPE(obj) == T_ARRAY) {
- na_composition3_ary(obj, ptype, pshape, pnary);
+ cumo_na_composition3_ary(obj, ptype, pshape, pnary);
}
else if (RTEST(rb_obj_is_kind_of(obj,rb_cNumeric))) {
- dtype = na_mdai_dtype_numeric(na_mdai_object_type(NA_NONE, obj));
+ dtype = cumo_na_mdai_dtype_numeric(cumo_na_mdai_object_type(CUMO_NA_NONE, obj));
dtype = update_type(ptype, dtype);
if (pshape) {
*pshape = rb_ary_new();
}
if (pnary) {
check_subclass_of_narray(dtype);
- *pnary = nary_new(dtype, 0, 0);
+ *pnary = cumo_na_new(dtype, 0, 0);
}
}
- else if (IsNArray(obj)) {
+ else if (CumoIsNArray(obj)) {
int i, ndim;
- narray_t *na;
- GetNArray(obj,na);
+ cumo_narray_t *na;
+ CumoGetNArray(obj,na);
ndim = na->ndim;
- dtype = update_type(ptype, CLASS_OF(obj));
+ dtype = update_type(ptype, rb_obj_class(obj));
if (pshape) {
dshape = rb_ary_new2(ndim);
for (i=0; i<ndim; i++) {
rb_ary_push(dshape, SIZET2NUM(na->shape[i]));
}
*pshape = dshape;
}
if (pnary) {
- *pnary = nary_new(dtype, ndim, na->shape);
+ *pnary = cumo_na_new(dtype, ndim, na->shape);
}
} else {
rb_raise(rb_eTypeError,"invalid type for NArray: %s",
- rb_class2name(CLASS_OF(obj)));
+ rb_class2name(rb_obj_class(obj)));
}
}
static VALUE
-na_s_array_shape(VALUE mod, VALUE ary)
+cumo_na_s_array_shape(VALUE mod, VALUE ary)
{
VALUE shape;
if (TYPE(ary) != T_ARRAY) {
// 0-dimension
return rb_ary_new();
}
- na_composition3(ary, 0, &shape, 0);
+ cumo_na_composition3(ary, 0, &shape, 0);
return shape;
}
/*
@@ -465,82 +465,82 @@
=> Cumo::DFloat#shape=[2,2](empty)
Cumo::NArray.new_like([1,2i,3])
=> Cumo::DComplex#shape=[3](empty)
*/
VALUE
-na_s_new_like(VALUE type, VALUE obj)
+cumo_na_s_new_like(VALUE type, VALUE obj)
{
VALUE newary;
- na_composition3(obj, &type, 0, &newary);
+ cumo_na_composition3(obj, &type, 0, &newary);
return newary;
}
VALUE
-na_ary_composition_dtype(VALUE ary)
+cumo_na_ary_composition_dtype(VALUE ary)
{
VALUE type = Qnil;
- na_composition3(ary, &type, 0, 0);
+ cumo_na_composition3(ary, &type, 0, 0);
return type;
}
static VALUE
-na_s_array_type(VALUE mod, VALUE ary)
+cumo_na_s_array_type(VALUE mod, VALUE ary)
{
- return na_ary_composition_dtype(ary);
+ return cumo_na_ary_composition_dtype(ary);
}
/*
Generate NArray object. NArray datatype is automatically selected.
@overload [](elements)
@param [Numeric,Array] elements
@return [NArray]
*/
static VALUE
-nary_s_bracket(VALUE klass, VALUE ary)
+cumo_na_s_bracket(VALUE klass, VALUE ary)
{
VALUE dtype=Qnil;
if (TYPE(ary)!=T_ARRAY) {
rb_bug("Argument is not array");
}
- dtype = na_ary_composition_dtype(ary);
+ dtype = cumo_na_ary_composition_dtype(ary);
check_subclass_of_narray(dtype);
- return rb_funcall(dtype, id_cast, 1, ary);
+ return rb_funcall(dtype, cumo_id_cast, 1, ary);
}
//VALUE
//nst_check_compatibility(VALUE self, VALUE ary);
/* investigate ndim, shape, type of Array */
/*
static int
-na_mdai_for_struct(na_mdai_t *mdai, int ndim)
+cumo_na_mdai_for_struct(cumo_na_mdai_t *mdai, int ndim)
{
size_t i;
int j, r;
size_t len;
VALUE v;
VALUE val;
- narray_t *na;
+ cumo_narray_t *na;
//fprintf(stderr,"ndim=%d\n",ndim); rb_p(mdai->na_type);
if (ndim>4) { abort(); }
val = mdai->item[ndim].val;
//fpintf(stderr,"val = "); rb_p(val);
- if (CLASS_OF(val) == mdai->na_type) {
- GetNArray(val,na);
+ if (rb_obj_class(val) == mdai->na_type) {
+ CumoGetNArray(val,na);
if ( ndim+na->ndim > mdai->capa ) {
abort();
- na_mdai_realloc(mdai,((na->ndim-1)/4+1)*4);
+ cumo_na_mdai_realloc(mdai,((na->ndim-1)/4+1)*4);
}
for ( j=0,r=ndim; j < na->ndim; j++,r++ ) {
if ( mdai->item[r].shape < na->shape[j] )
mdai->item[r].shape = na->shape[j];
}
@@ -561,11 +561,11 @@
return 1;
}
// otherwise, multi-dimention
if (ndim >= mdai->capa) {
//fprintf(stderr,"exeed capa\n"); abort();
- na_mdai_realloc(mdai,4);
+ cumo_na_mdai_realloc(mdai,4);
}
// finally, multidimension-check
len = RARRAY_LEN(val);
for (i=0; i < len; i++) {
v = RARRAY_AREF(val,i);
@@ -576,11 +576,11 @@
}
for (i=0; i < len; i++) {
v = RARRAY_AREF(val,i);
//fprintf(stderr,"check:"); rb_p(v);
mdai->item[ndim+1].val = v;
- if ( na_mdai_for_struct( mdai, ndim+1 ) == 0 ) {
+ if ( cumo_na_mdai_for_struct( mdai, ndim+1 ) == 0 ) {
//fprintf(stderr,"not struct:"); rb_p(v);
//abort();
return 0;
}
}
@@ -596,43 +596,43 @@
*/
/*
VALUE
-na_ary_composition_for_struct(VALUE nstruct, VALUE ary)
+cumo_na_ary_composition_for_struct(VALUE nstruct, VALUE ary)
{
volatile VALUE vmdai, vnc;
- na_mdai_t *mdai;
- na_compose_t *nc;
+ cumo_na_mdai_t *mdai;
+ cumo_na_compose_t *nc;
- mdai = na_mdai_alloc(ary);
+ mdai = cumo_na_mdai_alloc(ary);
mdai->na_type = nstruct;
vmdai = TypedData_Wrap_Struct(rb_cData, &mdai_data_type, (void*)mdai);
- na_mdai_for_struct(mdai, 0);
- nc = na_compose_alloc();
+ cumo_na_mdai_for_struct(mdai, 0);
+ nc = cumo_na_compose_alloc();
vnc = WrapCompose(nc);
- na_mdai_result(mdai, nc);
+ cumo_na_mdai_result(mdai, nc);
//fprintf(stderr,"nc->ndim=%d\n",nc->ndim);
rb_gc_force_recycle(vmdai);
return vnc;
}
*/
void
-Init_cumo_nary_array()
+Init_cumo_na_array()
{
- rb_define_singleton_method(cNArray, "array_shape", na_s_array_shape, 1);
- rb_define_singleton_method(cNArray, "array_type", na_s_array_type, 1);
- rb_define_singleton_method(cNArray, "new_like", na_s_new_like, 1);
+ rb_define_singleton_method(cNArray, "array_shape", cumo_na_s_array_shape, 1);
+ rb_define_singleton_method(cNArray, "array_type", cumo_na_s_array_type, 1);
+ rb_define_singleton_method(cNArray, "new_like", cumo_na_s_new_like, 1);
- rb_define_singleton_method(cNArray, "[]", nary_s_bracket, -2);
+ rb_define_singleton_method(cNArray, "[]", cumo_na_s_bracket, -2);
- id_begin = rb_intern("begin");
- id_end = rb_intern("end");
- id_step = rb_intern("step");
- id_cast = rb_intern("cast");
- id_abs = rb_intern("abs");
- id_le = rb_intern("<=");
- id_Complex = rb_intern("Complex");
+ cumo_id_begin = rb_intern("begin");
+ cumo_id_end = rb_intern("end");
+ cumo_id_step = rb_intern("step");
+ cumo_id_cast = rb_intern("cast");
+ cumo_id_abs = rb_intern("abs");
+ cumo_id_le = rb_intern("<=");
+ cumo_id_Complex = rb_intern("Complex");
}