Class: Numo::Struct
- Inherits:
-
NArray
- Object
- NArray
- Numo::Struct
- Defined in:
- ext/numo/narray/struct.c,
ext/numo/narray/struct.c
Overview
Structured array class.
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
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'ext/numo/narray/struct.c', line 200
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
739 740 741 742 743 |
# File 'ext/numo/narray/struct.c', line 739
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
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 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 |
# File 'ext/numo/narray/struct.c', line 231
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__");
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
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'ext/numo/narray/struct.c', line 46
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
355 356 357 |
# File 'ext/numo/narray/struct.c', line 355 static VALUE nst_extract(VALUE self) { return self; } |
#field(idx) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 |
# File 'ext/numo/narray/struct.c', line 180
static 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
192 193 194 195 196 197 198 |
# File 'ext/numo/narray/struct.c', line 192
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.
733 734 735 736 737 |
# File 'ext/numo/narray/struct.c', line 733 static 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.
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 |
# File 'ext/numo/narray/struct.c', line 676
static VALUE nary_struct_store(VALUE self, VALUE obj) {
if (TYPE(obj) == T_ARRAY) {
nary_struct_store_array(self, obj);
return self;
}
if (rb_obj_class(self) == rb_obj_class(obj)) {
nary_struct_store_struct(self, obj);
return self;
}
rb_raise(
nary_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”, nary_nstruct_debug_print, 0);
426 427 428 429 430 431 432 433 434 |
# File 'ext/numo/narray/struct.c', line 426
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);
}
|