Class: RGame::Util::Tensor

Inherits:
Object
  • Object
show all
Defined in:
ext/rgame_util/tensor.c

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'ext/rgame_util/tensor.c', line 98

static VALUE tensor_initialize(int argc, VALUE *argv, VALUE self) {
    VALUE width, height, depth, opts;
    rb_scan_args(argc, argv, "30:", &width, &height, &depth, &opts);

    long w = NUM2LONG(width);
    long h = NUM2LONG(height);
    long d = NUM2LONG(depth);
    if (w < 0 || h < 0 || d < 0) {
        rb_raise(rb_eArgError, "tensor dimensions must be non-negative");
    }

    VALUE initial = Qnil;
    if (!NIL_P(opts)) {
        initial = rb_hash_aref(opts, ID2SYM(rb_intern("initial")));
    }

    rgame_tensor *t = tensor_unwrap(self);
    t->width = w;
    t->height = h;
    t->depth = d;
    t->plane = w * h;
    t->size = t->plane * d;
    t->data = ALLOC_N(VALUE, t->size); /* ALLOC_N(_, 0) returns a valid pointer */
    for (long i = 0; i < t->size; i++) {
        t->data[i] = initial;
    }

    return self;
}

Instance Method Details

#[](x, y, z) ⇒ Object



149
150
151
152
# File 'ext/rgame_util/tensor.c', line 149

static VALUE tensor_aref(VALUE self, VALUE x, VALUE y, VALUE z) {
    rgame_tensor *t = tensor_unwrap(self);
    return t->data[tensor_offset(t, x, y, z)];
}

#[]=(x, y, z, value) ⇒ Object



154
155
156
157
158
# File 'ext/rgame_util/tensor.c', line 154

static VALUE tensor_aset(VALUE self, VALUE x, VALUE y, VALUE z, VALUE value) {
    rgame_tensor *t = tensor_unwrap(self);
    t->data[tensor_offset(t, x, y, z)] = value;
    return value; /* Ruby returns the assigned value from []= regardless */
}

#depthObject



168
169
170
# File 'ext/rgame_util/tensor.c', line 168

static VALUE tensor_depth(VALUE self) {
    return LONG2NUM(tensor_unwrap(self)->depth);
}

#heightObject



164
165
166
# File 'ext/rgame_util/tensor.c', line 164

static VALUE tensor_height(VALUE self) {
    return LONG2NUM(tensor_unwrap(self)->height);
}

#widthObject



160
161
162
# File 'ext/rgame_util/tensor.c', line 160

static VALUE tensor_width(VALUE self) {
    return LONG2NUM(tensor_unwrap(self)->width);
}