Class: Cumo::Struct

Inherits:
NArray
  • Object
show all
Defined in:
ext/cumo/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



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'ext/cumo/narray/struct.c', line 209

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



794
795
796
797
798
799
800
801
802
# File 'ext/cumo/narray/struct.c', line 794

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

.bfloatObject

.castObject

.dcomplexObject

.dfloatObject

.hfloatObject

.int16Object

.int32Object

.int64Object

.int8Object

.new(*args) ⇒ Object

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



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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'ext/cumo/narray/struct.c', line 243

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



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

#definitionsObject

#extractObject



374
375
376
377
378
# File 'ext/cumo/narray/struct.c', line 374

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

#field(idx) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'ext/cumo/narray/struct.c', line 184

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



198
199
200
201
202
203
204
205
206
# File 'ext/cumo/narray/struct.c', line 198

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)


785
786
787
788
789
790
791
# File 'ext/cumo/narray/struct.c', line 785

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.

Parameters:

  • other (Object)

Returns:



724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
# File 'ext/cumo/narray/struct.c', line 724

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_aObject

rb_define_method(cT, "debug_print", cumo_na_nstruct_debug_print, 0);



454
455
456
457
458
459
460
461
462
463
464
# File 'ext/cumo/narray/struct.c', line 454

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