Method: Snow::Vec3Array#fetch

Defined in:
ext/snow-math/snow-math.c

#fetch(sm_index) ⇒ Object Also known as: []

Fetches a Vec3 from the array at the index and returns it. The returned Vec3 may be a cached object. In all cases, values returned from a typed array are associated with the memory of the array and not given their own memory. So, modifying a Vec3 fetched from an array modifies the array’s data.

As a result, objects returned by a Vec3Array should not be considered thread-safe, nor should manipulating a Vec3Array be considered thread-safe either. If you want to work with data returned from an array without altering the array data, you should call Vec3#dup or Vec3#copy to get a new Vec3 with a copy of the array object’s data.

call-seq: fetch(index) -> vec3



481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
# File 'ext/snow-math/snow-math.c', line 481

static VALUE sm_vec3_array_fetch(VALUE sm_self, VALUE sm_index)
{
  vec3_t *arr;
  size_t length = NUM2SIZET(sm_mathtype_array_length(sm_self));
  size_t index = NUM2SIZET(sm_index);
  VALUE sm_inner;
  VALUE sm_cache;
  if (index >= length) {
    rb_raise(rb_eRangeError,
      "Index %zu out of bounds for array with length %zu",
      index, length);
  }

  sm_cache = rb_ivar_get(sm_self, kRB_IVAR_MATHARRAY_CACHE);
  if (!RTEST(sm_cache)) {
    rb_raise(rb_eRuntimeError, "No cache available");
  }
  sm_inner = rb_ary_entry(sm_cache, (long)index);

  if (!RTEST(sm_inner)) {
    /* No cached value, create one. */
    Data_Get_Struct(sm_self, vec3_t, arr);
    sm_inner = Data_Wrap_Struct(s_sm_vec3_klass, 0, 0, arr[index]);
    rb_ivar_set(sm_inner, kRB_IVAR_MATHARRAY_SOURCE, sm_self);
    /* Store the Vec3 in the cache */
    rb_ary_store(sm_cache, (long)index, sm_inner);
  }

  if (OBJ_FROZEN(sm_self)) {
    rb_funcall2(sm_inner, kRB_NAME_FREEZE, 0, 0);
  }

  return sm_inner;
}