Class: Cumo::Struct
- Inherits:
-
NArray
- Object
- NArray
- Cumo::Struct
- Defined in:
- ext/cumo/narray/struct.c
Class Method Summary collapse
- .[] ⇒ Object
- .add_type(*args) ⇒ Object
- .cast ⇒ Object
- .dcomplex ⇒ Object
- .dfloat ⇒ Object
- .int16 ⇒ Object
- .int32 ⇒ Object
- .int64 ⇒ Object
- .int8 ⇒ Object
-
.new(*args) ⇒ Object
Foo = Cumo::Struct.new { int8 :byte float64 :float, [2,2] dcomplex :compl }.
- .scomplex ⇒ Object
- .sfloat ⇒ Object
- .uint16 ⇒ Object
- .uint32 ⇒ Object
- .uint64 ⇒ Object
- .uint8 ⇒ Object
Instance Method Summary collapse
- #definition(idx) ⇒ Object
- #definitions ⇒ Object
- #extract ⇒ Object
- #field(idx) ⇒ Object
- #field_set(idx, other) ⇒ Object
-
#inspect ⇒ String
Returns a string containing a human-readable representation of NArray.
- #method_missing(*args) ⇒ Object
-
#store(other) ⇒ Cumo::Struct
Store elements to Cumo::Struct from other.
-
#to_a ⇒ Object
rb_define_method(cT, “debug_print”, cumo_na_nstruct_debug_print, 0);.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(*args) ⇒ Object
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 |
# File 'ext/cumo/narray/struct.c', line 215
static VALUE
nst_method_missing(int argc, VALUE *argv, VALUE self)
{
VALUE s, tag, obj;
if (argc == 2) {
s = rb_sym_to_s(argv[0]);
if (RSTRING_PTR(s)[RSTRING_LEN(s)-1] == '=') {
tag = rb_str_intern(rb_str_new(RSTRING_PTR(s), RSTRING_LEN(s)-1));
obj = nst_field(self, tag);
if (RTEST(obj)) {
rb_funcall(obj, rb_intern("store"), 1, argv[1]);
return argv[1];
}
}
return rb_call_super(argc,argv);
}
if (argc == 1) {
obj = nst_field(self,argv[0]);
if (RTEST(obj)) {
return obj;
}
}
return rb_call_super(argc,argv);
}
|
Class Method Details
.[] ⇒ Object
.add_type(*args) ⇒ Object
800 801 802 803 804 805 806 807 808 |
# File 'ext/cumo/narray/struct.c', line 800
static VALUE
nst_s_add_type(int argc, VALUE *argv, VALUE mod)
{
if (argc==0)
rb_raise(rb_eArgError,
"wrong number of arguments (%d for 1)", argc);
nstruct_add_type(argv[0],argc-1,argv+1,mod);
return Qnil;
}
|
.cast ⇒ Object
.dcomplex ⇒ Object
.dfloat ⇒ Object
.int16 ⇒ Object
.int32 ⇒ Object
.int64 ⇒ Object
.int8 ⇒ Object
.new(*args) ⇒ Object
Foo = Cumo::Struct.new
int8 :byte
float64 :float, [2,2]
dcomplex :compl
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 |
# File 'ext/cumo/narray/struct.c', line 249
static VALUE
nst_s_new(int argc, VALUE *argv, VALUE klass)
{
VALUE name=Qnil, rest, size;
VALUE st, members;
ID id;
rb_scan_args(argc, argv, "0*", &rest);
if (RARRAY_LEN(rest)>0) {
name = RARRAY_AREF(rest,0);
if (!NIL_P(name)) {
VALUE tmp = rb_check_string_type(name);
if (!NIL_P(tmp)) {
rb_ary_shift(rest);
} else {
name = Qnil;
}
}
}
if (NIL_P(name)) {
st = rb_define_class_id(name, klass);
rb_funcall(klass, rb_intern("inherited"), 1, st);
}
else {
char *cname = StringValuePtr(name);
id = rb_intern(cname);
if (!rb_is_const_id(id)) {
rb_name_error(id, "identifier %s needs to be constant", cname);
}
if (rb_const_defined_at(klass, id)) {
rb_warn("redefining constant Struct::%s", cname);
rb_mod_remove_const(klass, ID2SYM(id));
}
st = rb_define_class_under(klass, rb_id2name(id), klass);
}
rb_iv_set(st, "__members__", rb_ary_new());
rb_iv_set(st, "__offset__", INT2FIX(0));
if (rb_block_given_p()) {
rb_mod_module_eval(0, 0, st);
}
size = rb_iv_get(st, "__offset__");
members = rb_iv_get(st, "__members__");
//printf("size=%d\n",NUM2INT(size));
rb_define_const(st, "CONTIGUOUS_STRIDE", size);
rb_define_const(st, "ELEMENT_BYTE_SIZE", size);
rb_define_const(st, "ELEMENT_BIT_SIZE", rb_funcall(size,'*',1,INT2FIX(8)));
OBJ_FREEZE(members);
rb_define_const(st, "DEFINITIONS", members);
rb_define_singleton_method(st, "new", rb_class_new_instance, -1);
//rb_define_singleton_method(st, "[]", rb_class_new_instance, -1);
rb_define_method(st, "allocate", nst_allocate, 0);
return st;
}
|
.scomplex ⇒ Object
.sfloat ⇒ Object
.uint16 ⇒ Object
.uint32 ⇒ Object
.uint64 ⇒ Object
.uint8 ⇒ Object
Instance Method Details
#definition(idx) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'ext/cumo/narray/struct.c', line 47
static VALUE
nst_definition(VALUE nst, VALUE idx)
{
long i;
VALUE def = nst_definitions(rb_obj_class(nst));
long len = RARRAY_LEN(def);
if (TYPE(idx) == T_STRING || TYPE(idx) == T_SYMBOL) {
ID id = rb_to_id(idx);
for (i=0; i<len; i++) {
VALUE key = RARRAY_AREF(RARRAY_AREF(def,i),0);
if (SYM2ID(key) == id) {
return RARRAY_AREF(def,i);
}
}
} else if (rb_obj_is_kind_of(idx,rb_cNumeric)) {
i = NUM2LONG(idx);
if (i<-len || i>=len) {
rb_raise(rb_eIndexError,"offset %ld out of range of struct(size:%ld)", i, len);
}
return RARRAY_AREF(def,i);
}
return Qnil;
}
|
#definitions ⇒ Object
#extract ⇒ Object
380 381 382 383 384 |
# File 'ext/cumo/narray/struct.c', line 380
static VALUE
nst_extract(VALUE self)
{
return self;
}
|
#field(idx) ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'ext/cumo/narray/struct.c', line 190
static VALUE
nst_field(VALUE self, VALUE idx)
{
VALUE obj;
cumo_narray_view_t *nv;
obj = nst_field_view(self,idx);
CumoGetNArrayView(obj,nv);
if (nv->base.ndim==0) {
obj = rb_funcall(obj,rb_intern("extract_cpu"),0);
}
return obj;
}
|
#field_set(idx, other) ⇒ Object
204 205 206 207 208 209 210 211 212 |
# File 'ext/cumo/narray/struct.c', line 204
static VALUE
nst_field_set(VALUE self, VALUE idx, VALUE other)
{
VALUE obj;
obj = nst_field_view(self,idx);
rb_funcall(obj,rb_intern("store"),1,other);
return other;
}
|
#inspect ⇒ String
Returns a string containing a human-readable representation of NArray.
791 792 793 794 795 796 797 |
# File 'ext/cumo/narray/struct.c', line 791
static VALUE
cumo_na_struct_inspect(VALUE ary)
{
VALUE opt;
opt = nst_create_member_views(ary);
return cumo_na_ndloop_inspect(ary, iter_struct_inspect, opt);
}
|
#store(other) ⇒ Cumo::Struct
Store elements to Cumo::Struct from other.
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 |
# File 'ext/cumo/narray/struct.c', line 730
static VALUE
cumo_na_struct_store(VALUE self, VALUE obj)
{
if (TYPE(obj)==T_ARRAY) {
cumo_na_struct_store_array(self,obj);
return self;
}
if (rb_obj_class(self) == rb_obj_class(obj)) {
cumo_na_struct_store_struct(self,obj);
return self;
}
rb_raise(cumo_na_eCastError, "unknown conversion from %s to %s",
rb_class2name(rb_obj_class(obj)),
rb_class2name(rb_obj_class(self)));
return self;
}
|
#to_a ⇒ Object
rb_define_method(cT, “debug_print”, cumo_na_nstruct_debug_print, 0);
460 461 462 463 464 465 466 467 468 469 470 |
# File 'ext/cumo/narray/struct.c', line 460
static VALUE
cumo_na_struct_to_a(VALUE self)
{
volatile VALUE opt;
cumo_ndfunc_arg_in_t ain[3] = {{Qnil,0},{cumo_sym_loop_opt},{cumo_sym_option}};
cumo_ndfunc_arg_out_t aout[1] = {{rb_cArray,0}}; // dummy?
cumo_ndfunc_t ndf = {iter_nstruct_to_a, CUMO_NO_LOOP, 3, 1, ain, aout};
opt = nst_create_member_views(self);
return cumo_na_ndloop_cast_narray_to_rarray(&ndf, self, opt);
}
|