Class: Numo::Struct
- Inherits:
-
NArray
- Object
- NArray
- Numo::Struct
- Defined in:
- ext/numo/narray/struct.c
Defined Under Namespace
Modules: Math
Constant Summary collapse
- ELEMENT_BIT_SIZE =
INT2FIX(1)
- ELEMENT_BYTE_SIZE =
rb_float_new(1.0/8)
- CONTIGUOUS_STRIDE =
INT2FIX(1)
- UPCAST =
hCast
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 = Numo::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) ⇒ Numo::Struct
Store elements to Numo::Struct from other.
-
#to_a ⇒ Object
rb_define_method(cT, “debug_print”, nary_nstruct_debug_print, 0);.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(*args) ⇒ Object
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 |
# File 'ext/numo/narray/struct.c', line 221 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
783 784 785 786 787 788 789 790 791 |
# File 'ext/numo/narray/struct.c', line 783 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 = Numo::Struct.new
int8 :byte
float64 :float, [2,2]
dcomplex :compl
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 |
# File 'ext/numo/narray/struct.c', line 255 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
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'ext/numo/narray/struct.c', line 55 static VALUE nst_definition(VALUE nst, VALUE idx) { long i; VALUE def = nst_definitions(CLASS_OF(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
386 387 388 389 390 |
# File 'ext/numo/narray/struct.c', line 386 static VALUE nst_extract(VALUE self) { return self; } |
#field(idx) ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'ext/numo/narray/struct.c', line 196 VALUE nst_field(VALUE self, VALUE idx) { VALUE obj; narray_view_t *nv; obj = nst_field_view(self,idx); GetNArrayView(obj,nv); if (nv->base.ndim==0) { obj = rb_funcall(obj,rb_intern("extract"),0); } return obj; } |
#field_set(idx, other) ⇒ Object
210 211 212 213 214 215 216 217 218 |
# File 'ext/numo/narray/struct.c', line 210 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.
774 775 776 777 778 779 780 |
# File 'ext/numo/narray/struct.c', line 774 VALUE nary_struct_inspect(VALUE ary) { VALUE opt; opt = nst_create_member_views(ary); return na_ndloop_inspect(ary, iter_struct_inspect, opt); } |
#store(other) ⇒ Numo::Struct
Store elements to Numo::Struct from other.
713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 |
# File 'ext/numo/narray/struct.c', line 713 static VALUE nary_struct_store(VALUE self, VALUE obj) { if (TYPE(obj)==T_ARRAY) { nary_struct_store_array(self,obj); return self; } if (CLASS_OF(self) == CLASS_OF(obj)) { nary_struct_store_struct(self,obj); return self; } rb_raise(nary_eCastError, "unknown conversion from %s to %s", rb_class2name(CLASS_OF(obj)), rb_class2name(CLASS_OF(self))); return self; } |
#to_a ⇒ Object
rb_define_method(cT, “debug_print”, nary_nstruct_debug_print, 0);
451 452 453 454 455 456 457 458 459 460 461 |
# File 'ext/numo/narray/struct.c', line 451 static VALUE nary_struct_to_a(VALUE self) { volatile VALUE opt; ndfunc_arg_in_t ain[3] = {{Qnil,0},{sym_loop_opt},{sym_option}}; ndfunc_arg_out_t aout[1] = {{rb_cArray,0}}; // dummy? ndfunc_t ndf = { iter_nstruct_to_a, NO_LOOP, 3, 1, ain, aout }; opt = nst_create_member_views(self); return na_ndloop_cast_narray_to_rarray(&ndf, self, opt); } |