In Files

  • fiddle/closure.c

Fiddle::MemoryView

Public Class Methods

new(p1) click to toggle source
 
               static VALUE
rb_fiddle_memview_initialize(VALUE obj, VALUE target)
{
    struct memview_data *data;
    TypedData_Get_Struct(obj, struct memview_data, &fiddle_memview_data_type, data);

    if (!rb_memory_view_get(target, &data->view, 0)) {
        rb_raise(rb_eArgError, "Unable to get a memory view from %+"PRIsVALUE, target);
    }

    return Qnil;
}
            

Public Instance Methods

[](*args) click to toggle source
 
               static VALUE
rb_fiddle_memview_aref(int argc, VALUE *argv, VALUE obj)
{
    struct memview_data *data;
    TypedData_Get_Struct(obj, struct memview_data, &fiddle_memview_data_type, data);

    if (NIL_P(data->view.obj)) return Qnil;

    const ssize_t ndim = data->view.ndim;
    if (argc != ndim) {
        rb_raise(rb_eIndexError, "wrong number of index (%d for %"PRIdSIZE")", argc, ndim);
    }

    VALUE indices_v = 0;
    ssize_t *indices = ALLOCV_N(ssize_t, indices_v, ndim);

    ssize_t i;
    for (i = 0; i < ndim; ++i) {
        ssize_t x = NUM2SSIZET(argv[i]);
        indices[i] = x;
    }

    uint8_t *ptr = rb_memory_view_get_item_pointer(&data->view, indices);
    ALLOCV_END(indices_v);

    if (data->view.format == NULL) {
        return INT2FIX(*ptr);
    }

    if (!data->members) {
        const char *err;
        if (rb_memory_view_parse_item_format(data->view.format, &data->members, &data->n_members, &err) < 0) {
            rb_raise(rb_eRuntimeError, "Unable to recognize item format at %"PRIdSIZE" in \"%s\"",
                     err - data->view.format, data->view.format);
        }
    }

    return rb_memory_view_extract_item_members(ptr, data->members, data->n_members);
}
            
byte_size() click to toggle source
 
               static VALUE
rb_fiddle_memview_get_byte_size(VALUE obj)
{
    struct memview_data *data;
    TypedData_Get_Struct(obj, struct memview_data, &fiddle_memview_data_type, data);

    if (NIL_P(data->view.obj)) return Qnil;
    return SSIZET2NUM(data->view.byte_size);
}
            
format() click to toggle source
 
               static VALUE
rb_fiddle_memview_get_format(VALUE obj)
{
    struct memview_data *data;
    TypedData_Get_Struct(obj, struct memview_data, &fiddle_memview_data_type, data);

    if (NIL_P(data->view.obj)) return Qnil;
    return data->view.format == NULL ? Qnil : rb_str_new_cstr(data->view.format);
}
            
item_size() click to toggle source
 
               static VALUE
rb_fiddle_memview_get_item_size(VALUE obj)
{
    struct memview_data *data;
    TypedData_Get_Struct(obj, struct memview_data, &fiddle_memview_data_type, data);

    if (NIL_P(data->view.obj)) return Qnil;
    return SSIZET2NUM(data->view.item_size);
}
            
ndim() click to toggle source
 
               static VALUE
rb_fiddle_memview_get_ndim(VALUE obj)
{
    struct memview_data *data;
    TypedData_Get_Struct(obj, struct memview_data, &fiddle_memview_data_type, data);

    if (NIL_P(data->view.obj)) return Qnil;
    return SSIZET2NUM(data->view.ndim);
}
            
obj() click to toggle source
 
               static VALUE
rb_fiddle_memview_get_obj(VALUE obj)
{
    struct memview_data *data;
    TypedData_Get_Struct(obj, struct memview_data, &fiddle_memview_data_type, data);

    return data->view.obj;
}
            
readonly?() click to toggle source
 
               static VALUE
rb_fiddle_memview_get_readonly(VALUE obj)
{
    struct memview_data *data;
    TypedData_Get_Struct(obj, struct memview_data, &fiddle_memview_data_type, data);

    if (NIL_P(data->view.obj)) return Qnil;
    return data->view.readonly ? Qtrue : Qfalse;
}
            
shape() click to toggle source
 
               static VALUE
rb_fiddle_memview_get_shape(VALUE obj)
{
    struct memview_data *data;
    TypedData_Get_Struct(obj, struct memview_data, &fiddle_memview_data_type, data);

    if (NIL_P(data->view.obj)) return Qnil;
    if (data->view.shape == NULL) return Qnil;

    const ssize_t ndim = data->view.ndim;
    VALUE shape = rb_ary_new_capa(ndim);
    ssize_t i;
    for (i = 0; i < ndim; ++i) {
        rb_ary_push(shape, SSIZET2NUM(data->view.shape[i]));
    }
    return shape;
}
            
strides() click to toggle source
 
               static VALUE
rb_fiddle_memview_get_strides(VALUE obj)
{
    struct memview_data *data;
    TypedData_Get_Struct(obj, struct memview_data, &fiddle_memview_data_type, data);

    if (NIL_P(data->view.obj)) return Qnil;
    if (data->view.strides == NULL) return Qnil;

    const ssize_t ndim = data->view.ndim;
    VALUE strides = rb_ary_new_capa(ndim);
    ssize_t i;
    for (i = 0; i < ndim; ++i) {
        rb_ary_push(strides, SSIZET2NUM(data->view.strides[i]));
    }
    return strides;
}
            
sub_offsets() click to toggle source
 
               static VALUE
rb_fiddle_memview_get_sub_offsets(VALUE obj)
{
    struct memview_data *data;
    TypedData_Get_Struct(obj, struct memview_data, &fiddle_memview_data_type, data);

    if (NIL_P(data->view.obj)) return Qnil;
    if (data->view.sub_offsets == NULL) return Qnil;

    const ssize_t ndim = data->view.ndim;
    VALUE sub_offsets = rb_ary_new_capa(ndim);
    ssize_t i;
    for (i = 0; i < ndim; ++i) {
        rb_ary_push(sub_offsets, SSIZET2NUM(data->view.sub_offsets[i]));
    }
    return sub_offsets;
}