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



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

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

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

  return self;
}

Instance Method Details

#<<(val) ⇒ self

Parameters:

  • val (Integer)

    Element to push

Returns:

  • (self)


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

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

  say_array *ary = ray_rb2int_array(self);
  int e = NUM2INT(val);

  say_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



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

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

  int *elem = say_array_get(ary, idx);

  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



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

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

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

  if (say_array_get_size(ary) <= idx)
    say_array_resize(ary, idx + 1);

  *(int*)say_array_get(ary, idx) = NUM2INT(val);

  return val;
}

#clearObject

Removes all the elements from the array



113
114
115
116
117
# File 'ext/gl_int_array.c', line 113

static
VALUE ray_int_array_clear(VALUE self) {
  say_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



44
45
46
47
48
# File 'ext/gl_int_array.c', line 44

static
VALUE ray_int_array_init_copy(VALUE self, VALUE orig) {
  say_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



107
108
109
110
# File 'ext/gl_int_array.c', line 107

static
VALUE ray_int_array_size(VALUE self) {
  return INT2FIX(say_array_get_size(ray_rb2int_array(self)));
}