Class: Ray::GL::IntArray

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ray/gl/int_array.rb,
ext/gl_int_array.c

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object

Parameters:

  • args (Array<Integer>)

    Inital content of the array



34
35
36
37
38
39
40
41
42
43
44
# File 'ext/gl_int_array.c', line 34

static
VALUE ray_int_array_init(int argc, VALUE *argv, VALUE self) {
  mo_array *ary = ray_rb2int_array(self);

  for (int i = 0; i < argc; i++) {
    GLint val = NUM2INT(argv[i]);
    mo_array_push(ary, &val);
  }

  return self;
}

Instance Method Details

#<<(val) ⇒ self

Parameters:

  • val (Integer)

    Element to push

Returns:

  • (self)


57
58
59
60
61
62
63
64
65
66
67
# File 'ext/gl_int_array.c', line 57

static
VALUE ray_int_array_push(VALUE self, VALUE val) {
  rb_check_frozen(self);

  mo_array *ary = ray_rb2int_array(self);
  GLint     e   = NUM2INT(val);

  mo_array_push(ary, &e);

  return self;
}

#[](i) ⇒ Integer?

Returns Value at that index.

Parameters:

  • i (Integer)

    Index to read from

Returns:

  • (Integer, nil)

    Value at that index



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'ext/gl_int_array.c', line 74

static
VALUE ray_int_array_get(VALUE self, VALUE i) {
  mo_array *ary = ray_rb2int_array(self);
  size_t idx = NUM2ULONG(i);

  GLint *elem = mo_array_get_ptr(ary, idx, GLint);

  if (elem) {
    return INT2FIX(*elem);
  }
  else
    return Qnil;
}

#[]=(i, val) ⇒ Object

Parameters:

  • i (Integer)

    Index of the value to change

  • val (Integer)

    Value to assign



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'ext/gl_int_array.c', line 93

static
VALUE ray_int_array_set(VALUE self, VALUE i, VALUE val) {
  rb_check_frozen(self);

  mo_array *ary  = ray_rb2int_array(self);
  size_t     idx = NUM2ULONG(i);

  if (ary->size <= idx)
    mo_array_resize(ary, idx + 1);

  mo_array_get_as(ary, idx, GLint) = NUM2INT(val);

  return val;
}

#clearObject

Removes all the elements from the array



115
116
117
118
119
# File 'ext/gl_int_array.c', line 115

static
VALUE ray_int_array_clear(VALUE self) {
  mo_array_resize(ray_rb2int_array(self), 0);
  return self;
}

#eachObject



6
7
8
# File 'lib/ray/gl/int_array.rb', line 6

def each
  (0...size).each { |i| yield self[i] }
end

#each_index {|i| ... } ⇒ Object

Yields:

  • Each index of the array

Yield Parameters:

  • i (Integer)

    Index of an element



19
20
21
# File 'lib/ray/gl/int_array.rb', line 19

def each_index(&block)
  (0...size).each(&block)
end

#initialize_copy(orig) ⇒ Object



46
47
48
49
50
# File 'ext/gl_int_array.c', line 46

static
VALUE ray_int_array_init_copy(VALUE self, VALUE orig) {
  mo_array_copy(ray_rb2int_array(self), ray_rb2int_array(orig));
  return self;
}

#map! {|val| ... } ⇒ Object

Yields:

  • A block changing each value of the array

Yield Parameters:

  • val (Integer)

    Old value

Yield Returns:

  • (Integer)

    New value



13
14
15
# File 'lib/ray/gl/int_array.rb', line 13

def map!
  (0...size).each { |i| self[i] = yield self[i] }
end

#sizeInteger

Returns size of the array.

Returns:

  • (Integer)

    size of the array



109
110
111
112
# File 'ext/gl_int_array.c', line 109

static
VALUE ray_int_array_size(VALUE self) {
  return INT2FIX(ray_rb2int_array(self)->size);
}