Class: Numo::Struct

Inherits:
NArray
  • Object
show all
Defined in:
ext/numo/narray/struct.c

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'ext/numo/narray/struct.c', line 216

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



801
802
803
804
805
806
807
808
809
# File 'ext/numo/narray/struct.c', line 801

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;
}

.castObject

.dcomplexObject

.dfloatObject

.int16Object

.int32Object

.int64Object

.int8Object

.new(*args) ⇒ Object

Foo = Numo::Struct.new { int8 :byte float64 :float, [2,2] dcomplex :compl }



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
309
# File 'ext/numo/narray/struct.c', line 250

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;
}

.scomplexObject

.sfloatObject

.uint16Object

.uint32Object

.uint64Object

.uint8Object

Instance Method Details

#definition(idx) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'ext/numo/narray/struct.c', line 50

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;
}

#definitionsObject

#extractObject



381
382
383
384
385
# File 'ext/numo/narray/struct.c', line 381

static VALUE
nst_extract(VALUE self)
{
    return self;
}

#field(idx) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'ext/numo/narray/struct.c', line 191

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



205
206
207
208
209
210
211
212
213
# File 'ext/numo/narray/struct.c', line 205

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;
}

#inspectString

Returns a string containing a human-readable representation of NArray.

Returns:

  • (String)


792
793
794
795
796
797
798
# File 'ext/numo/narray/struct.c', line 792

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.

Parameters:

  • other (Object)

Returns:



731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
# File 'ext/numo/narray/struct.c', line 731

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_aObject

rb_define_method(cT, “debug_print”, nary_nstruct_debug_print, 0);



461
462
463
464
465
466
467
468
469
470
471
# File 'ext/numo/narray/struct.c', line 461

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);
}