static mrb_value mrb_ary_times(mrb_state *mrb, mrb_value self) { struct RArray *a1 = mrb_ary_ptr(self); struct RArray *a2; mrb_value *ptr; mrb_int times; mrb_get_args(mrb, "i", ×); if (times < 0) { mrb_raise(mrb, E_ARGUMENT_ERROR, "negative argument"); } if (times == 0) return mrb_ary_new(mrb); if (ARY_MAX_SIZE / times < a1->len) { mrb_raise(mrb, E_ARGUMENT_ERROR, "array size too big"); } a2 = ary_new_capa(mrb, a1->len * times); ptr = a2->ptr; while (times--) { array_copy(ptr, a1->ptr, a1->len); ptr += a1->len; a2->len += a1->len; } return mrb_obj_value(a2); }
static mrb_value mrb_ary_shuffle_bang(mrb_state *mrb, mrb_value ary) { mrb_int i; mt_state *random = NULL; if (RARRAY_LEN(ary) > 1) { mrb_get_args(mrb, "|d", &random, &mt_state_type); if (random == NULL) { mrb_value random_val = mrb_const_get(mrb, mrb_obj_value(mrb_class_get(mrb, "Random")), mrb_intern_lit(mrb, "DEFAULT")); random = (mt_state *)DATA_PTR(random_val); } mrb_random_rand_seed(mrb, random); mrb_ary_modify(mrb, mrb_ary_ptr(ary)); for (i = RARRAY_LEN(ary) - 1; i > 0; i--) { mrb_int j; mrb_value tmp; j = mrb_fixnum(mrb_random_mt_rand(mrb, random, mrb_fixnum_value(RARRAY_LEN(ary)))); tmp = RARRAY_PTR(ary)[i]; RARRAY_PTR(ary)[i] = RARRAY_PTR(ary)[j]; RARRAY_PTR(ary)[j] = tmp; } } return ary; }
static mrb_value mrb_ary_shuffle_bang(mrb_state *mrb, mrb_value ary) { mrb_int i; mrb_value random = mrb_nil_value(); if (RARRAY_LEN(ary) > 1) { mrb_get_args(mrb, "|o", &random); if( mrb_nil_p(random) ) { mrb_random_g_rand_seed(mrb); } else { mrb_data_check_type(mrb, random, &mt_state_type); mrb_random_rand_seed(mrb, random); } mrb_ary_modify(mrb, mrb_ary_ptr(ary)); for (i = RARRAY_LEN(ary) - 1; i > 0; i--) { mrb_int j; if( mrb_nil_p(random) ) { j = mrb_fixnum(mrb_random_mt_g_rand(mrb, mrb_fixnum_value(RARRAY_LEN(ary)))); } else { j = mrb_fixnum(mrb_random_mt_rand(mrb, DATA_PTR(random), mrb_fixnum_value(RARRAY_LEN(ary)))); } mrb_value t = RARRAY_PTR(ary)[i]; RARRAY_PTR(ary)[i] = RARRAY_PTR(ary)[j]; RARRAY_PTR(ary)[j] = t; } } return ary; }
/* mrb_gc_unregister() removes the object from GC root. */ MRB_API void mrb_gc_unregister(mrb_state *mrb, mrb_value obj) { mrb_sym root = mrb_intern_lit(mrb, GC_ROOT_NAME); mrb_value table = mrb_gv_get(mrb, root); struct RArray *a; mrb_int i; if (mrb_nil_p(table)) return; if (mrb_type(table) != MRB_TT_ARRAY) { mrb_gv_set(mrb, root, mrb_nil_value()); return; } a = mrb_ary_ptr(table); mrb_ary_modify(mrb, a); for (i = 0; i < ARY_LEN(a); i++) { if (mrb_obj_eq(mrb, ARY_PTR(a)[i], obj)) { mrb_int len = ARY_LEN(a)-1; mrb_value *ptr = ARY_PTR(a); ARY_SET_LEN(a, len); memmove(&ptr[i], &ptr[i + 1], (len - i) * sizeof(mrb_value)); break; } } }
static mrb_value mrb_ary_unshift_m(mrb_state *mrb, mrb_value self) { struct RArray *a = mrb_ary_ptr(self); mrb_value *vals, *ptr; mrb_int alen, len; mrb_get_args(mrb, "*!", &vals, &alen); len = ARY_LEN(a); if (alen > ARY_MAX_SIZE - len) { mrb_raise(mrb, E_ARGUMENT_ERROR, "array size too big"); } if (ARY_SHARED_P(a) && a->as.heap.aux.shared->refcnt == 1 /* shared only referenced from this array */ && a->as.heap.ptr - a->as.heap.aux.shared->ptr >= alen) /* there's room for unshifted item */ { ary_modify_check(mrb, a); a->as.heap.ptr -= len; ptr = a->as.heap.ptr; } else { ary_modify(mrb, a); if (alen == 0) return self; if (ARY_CAPA(a) < len + alen) ary_expand_capa(mrb, a, len + alen); ptr = ARY_PTR(a); value_move(ptr + alen, ptr, len); } array_copy(ptr, vals, alen); ARY_SET_LEN(a, len+alen); while (alen--) { mrb_field_write_barrier_value(mrb, (struct RBasic*)a, vals[alen]); } return self; }
static mrb_value mrb_ary_aset(mrb_state *mrb, mrb_value self) { mrb_value v1, v2, v3; mrb_int i, len; mrb_ary_modify(mrb, mrb_ary_ptr(self)); if (mrb_get_args(mrb, "oo|o", &v1, &v2, &v3) == 2) { /* a[n..m] = v */ switch (mrb_range_beg_len(mrb, v1, &i, &len, RARRAY_LEN(self), FALSE)) { case 0: /* not range */ mrb_ary_set(mrb, self, aget_index(mrb, v1), v2); break; case 1: /* range */ mrb_ary_splice(mrb, self, i, len, v2); break; case 2: /* out of range */ mrb_raisef(mrb, E_RANGE_ERROR, "%S out of range", v1); break; } return v2; } /* a[n,m] = v */ mrb_ary_splice(mrb, self, aget_index(mrb, v1), aget_index(mrb, v2), v3); return v3; }
/* self = [1,2,3] item = 0 self.unshift item p self #=> [0, 1, 2, 3] */ MRB_API mrb_value mrb_ary_unshift(mrb_state *mrb, mrb_value self, mrb_value item) { struct RArray *a = mrb_ary_ptr(self); mrb_int len = ARY_LEN(a); if (ARY_SHARED_P(a) && a->as.heap.aux.shared->refcnt == 1 /* shared only referenced from this array */ && a->as.heap.ptr - a->as.heap.aux.shared->ptr >= 1) /* there's room for unshifted item */ { a->as.heap.ptr--; a->as.heap.ptr[0] = item; } else { mrb_value *ptr; ary_modify(mrb, a); if (ARY_CAPA(a) < len + 1) ary_expand_capa(mrb, a, len + 1); ptr = ARY_PTR(a); value_move(ptr + 1, ptr, len); ptr[0] = item; } ARY_SET_LEN(a, len+1); mrb_field_write_barrier_value(mrb, (struct RBasic*)a, item); return self; }
MRB_API mrb_value mrb_ary_shift(mrb_state *mrb, mrb_value self) { struct RArray *a = mrb_ary_ptr(self); mrb_int len = ARY_LEN(a); mrb_value val; ary_modify_check(mrb, a); if (len == 0) return mrb_nil_value(); if (ARY_SHARED_P(a)) { L_SHIFT: val = a->as.heap.ptr[0]; a->as.heap.ptr++; a->as.heap.len--; return val; } if (len > ARY_SHIFT_SHARED_MIN) { ary_make_shared(mrb, a); goto L_SHIFT; } else { mrb_value *ptr = ARY_PTR(a); mrb_int size = len; val = *ptr; while (--size) { *ptr = *(ptr+1); ++ptr; } ARY_SET_LEN(a, len-1); } return val; }
static mrb_value mrb_ary_times(mrb_state *mrb, mrb_value self) { struct RArray *a1 = mrb_ary_ptr(self); struct RArray *a2; mrb_value *ptr; mrb_int times, len1; mrb_get_args(mrb, "i", ×); if (times < 0) { mrb_raise(mrb, E_ARGUMENT_ERROR, "negative argument"); } if (times == 0) return mrb_ary_new(mrb); if (ARY_MAX_SIZE / times < ARY_LEN(a1)) { mrb_raise(mrb, E_ARGUMENT_ERROR, "array size too big"); } len1 = ARY_LEN(a1); a2 = ary_new_capa(mrb, len1 * times); ARY_SET_LEN(a2, len1 * times); ptr = ARY_PTR(a2); while (times--) { array_copy(ptr, ARY_PTR(a1), len1); ptr += len1; } return mrb_obj_value(a2); }
mrb_value mrb_ary_unshift_m(mrb_state *mrb, mrb_value self) { struct RArray *a = mrb_ary_ptr(self); mrb_value *vals; int len; mrb_get_args(mrb, "*", &vals, &len); if ((a->flags & MRB_ARY_SHARED) && a->aux.shared->refcnt == 1 /* shared only referenced from this array */ && a->ptr - a->aux.shared->ptr >= len) /* there's room for unshifted item */ { a->ptr -= len; } else { ary_modify(mrb, a); if (len == 0) return self; if (a->aux.capa < a->len + len) ary_expand_capa(mrb, a, a->len + len); memmove(a->ptr + len, a->ptr, sizeof(mrb_value)*a->len); } array_copy(a->ptr, vals, len); a->len += len; mrb_write_barrier(mrb, (struct RBasic*)a); return self; }
mrb_value mrb_ary_aget(mrb_state *mrb, mrb_value self) { struct RArray *a = mrb_ary_ptr(self); mrb_int index, len; mrb_value *argv; int size; mrb_get_args(mrb, "i*", &index, &argv, &size); switch(size) { case 0: return mrb_ary_ref(mrb, self, index); case 1: if (mrb_type(argv[0]) != MRB_TT_FIXNUM) { mrb_raise(mrb, E_TYPE_ERROR, "expected Fixnum"); } if (index < 0) index += a->len; if (index < 0 || a->len < (int)index) return mrb_nil_value(); len = mrb_fixnum(argv[0]); if (len < 0) return mrb_nil_value(); if (a->len == (int)index) return mrb_ary_new(mrb); if (len > a->len - index) len = a->len - index; return ary_subseq(mrb, a, index, len); default: mrb_raise(mrb, E_ARGUMENT_ERROR, "wrong number of arguments"); break; } return mrb_nil_value(); /* dummy to avoid warning : not reach here */ }
MRB_API mrb_value mrb_ary_shift(mrb_state *mrb, mrb_value self) { struct RArray *a = mrb_ary_ptr(self); mrb_value val; if (a->len == 0) return mrb_nil_value(); if (ARY_SHARED_P(a)) { L_SHIFT: val = a->ptr[0]; a->ptr++; a->len--; return val; } if (a->len > ARY_SHIFT_SHARED_MIN) { ary_make_shared(mrb, a); goto L_SHIFT; } else { mrb_value *ptr = a->ptr; mrb_int size = a->len; val = *ptr; while (--size) { *ptr = *(ptr+1); ++ptr; } --a->len; } return val; }
static mrb_value mrb_ary_unshift_m(mrb_state *mrb, mrb_value self) { struct RArray *a = mrb_ary_ptr(self); mrb_value *vals; mrb_int len; mrb_get_args(mrb, "*", &vals, &len); if (ARY_SHARED_P(a) && a->aux.shared->refcnt == 1 /* shared only referenced from this array */ && a->ptr - a->aux.shared->ptr >= len) /* there's room for unshifted item */ { a->ptr -= len; } else { ary_modify(mrb, a); if (len == 0) return self; if (a->aux.capa < a->len + len) ary_expand_capa(mrb, a, a->len + len); value_move(a->ptr + len, a->ptr, a->len); } array_copy(a->ptr, vals, len); a->len += len; while (len--) { mrb_field_write_barrier_value(mrb, (struct RBasic*)a, vals[len]); } return self; }
static mrb_value mrb_ary_size(mrb_state *mrb, mrb_value self) { struct RArray *a = mrb_ary_ptr(self); return mrb_fixnum_value(a->len); }
static mrb_value mrb_ary_empty_p(mrb_state *mrb, mrb_value self) { struct RArray *a = mrb_ary_ptr(self); return mrb_bool_value(a->len == 0); }
static mrb_value mrb_ary_aget(mrb_state *mrb, mrb_value self) { struct RArray *a = mrb_ary_ptr(self); mrb_int i, len; mrb_value index; if (mrb_get_args(mrb, "o|i", &index, &len) == 1) { switch (mrb_type(index)) { /* a[n..m] */ case MRB_TT_RANGE: if (mrb_range_beg_len(mrb, index, &i, &len, a->len)) { return ary_subseq(mrb, a, i, len); } else { return mrb_nil_value(); } case MRB_TT_FIXNUM: return mrb_ary_ref(mrb, self, mrb_fixnum(index)); default: return mrb_ary_ref(mrb, self, aget_index(mrb, index)); } } i = aget_index(mrb, index); if (i < 0) i += a->len; if (i < 0 || a->len < i) return mrb_nil_value(); if (len < 0) return mrb_nil_value(); if (a->len == i) return mrb_ary_new(mrb); if (len > a->len - i) len = a->len - i; return ary_subseq(mrb, a, i, len); }
static mrb_value mrb_ary_delete_at(mrb_state *mrb, mrb_value self) { struct RArray *a = mrb_ary_ptr(self); mrb_int index; mrb_value val; mrb_value *ptr; mrb_int len; mrb_get_args(mrb, "i", &index); if (index < 0) index += a->len; if (index < 0 || a->len <= index) return mrb_nil_value(); ary_modify(mrb, a); val = a->ptr[index]; ptr = a->ptr + index; len = a->len - index; while (--len) { *ptr = *(ptr+1); ++ptr; } --a->len; ary_shrink_capa(mrb, a); return val; }
mrb_value cfunc_struct_define_struct(mrb_state *mrb, mrb_value klass) { mrb_value elements_mrb; mrb_get_args(mrb, "A", &elements_mrb); struct RArray *elements = mrb_ary_ptr(elements_mrb); ffi_type *tm_type = mrb_malloc(mrb, sizeof(ffi_type)); tm_type->type = FFI_TYPE_STRUCT; tm_type->size = tm_type->alignment = 0; ffi_type **tm_type_elements = mrb_malloc(mrb, sizeof(ffi_type*) * (elements->len + 1)); int i; for(i = 0; i < elements->len; ++i) { tm_type_elements[i] = rclass_to_mrb_ffi_type(mrb, mrb_class_ptr(elements->ptr[i]))->ffi_type_value; } tm_type_elements[i] = NULL; tm_type->elements = tm_type_elements; struct mrb_ffi_type *mft = mrb_malloc(mrb, sizeof(struct mrb_ffi_type)); mft->name = mrb_class_name(mrb, mrb_class_ptr(klass)); mft->ffi_type_value = tm_type; mft->mrb_to_c = &cfunc_type_ffi_struct_mrb_to_c; mft->c_to_mrb = &cfunc_type_ffi_struct_c_to_mrb; mrb_value __ffi_type = mrb_obj_value(Data_Wrap_Struct(mrb, mrb->object_class, &cfunc_struct_data_type, mft)); mrb_obj_iv_set(mrb, (struct RObject*)(mrb_class_ptr(klass)), mrb_intern_cstr(mrb, "@ffi_type"), __ffi_type); return mrb_nil_value(); }
static mrb_value mrb_ary_shuffle_bang(mrb_state *mrb, mrb_value ary) { mrb_int i; mt_state *random = NULL; if (RARRAY_LEN(ary) > 1) { mrb_get_args(mrb, "|d", &random, &mt_state_type); if (random == NULL) { random = get_random_state(mrb); } mrb_random_rand_seed(mrb, random); mrb_ary_modify(mrb, mrb_ary_ptr(ary)); for (i = RARRAY_LEN(ary) - 1; i > 0; i--) { mrb_int j; mrb_value tmp; j = mrb_fixnum(mrb_random_mt_rand(mrb, random, mrb_fixnum_value(RARRAY_LEN(ary)))); tmp = RARRAY_PTR(ary)[i]; RARRAY_PTR(ary)[i] = RARRAY_PTR(ary)[j]; RARRAY_PTR(ary)[j] = tmp; } } return ary; }
mrb_value mrb_ary_empty_p(mrb_state *mrb, mrb_value self) { struct RArray *a = mrb_ary_ptr(self); return ((a->len == 0)? mrb_true_value(): mrb_false_value()); }
mrb_value mrb_ary_last(mrb_state *mrb, mrb_value self) { struct RArray *a = mrb_ary_ptr(self); mrb_int size; mrb_value *vals; int len; mrb_get_args(mrb, "*", &vals, &len); if (len > 1) { mrb_raise(mrb, E_ARGUMENT_ERROR, "wrong number of arguments"); } if (len == 0) return (a->len > 0)? a->ptr[a->len - 1]: mrb_nil_value(); /* len == 1 */ size = mrb_fixnum(*vals); if (size < 0) { mrb_raise(mrb, E_ARGUMENT_ERROR, "negative array size"); } if (size > a->len) size = a->len; if ((a->flags & MRB_ARY_SHARED) || size > ARY_DEFAULT_LEN) { return ary_subseq(mrb, a, a->len - size, size); } return mrb_ary_new_from_values(mrb, size, a->ptr + a->len - size); }
mrb_value mrb_ary_pop(mrb_state *mrb, mrb_value ary) { struct RArray *a = mrb_ary_ptr(ary); if (a->len == 0) return mrb_nil_value(); return a->ptr[--a->len]; }
struct task_arg* mrb_value_to_task_arg(mrb_state *mrb, mrb_value v) { struct task_arg *arg = mrb_malloc(mrb, sizeof(struct task_arg)); arg->tt = mrb_type(v); switch (mrb_type(v)) { case MRB_TT_FALSE: case MRB_TT_TRUE: case MRB_TT_FIXNUM: arg->value.i = v.value.i; break; case MRB_TT_FLOAT: arg->value.f = v.value.f; break; case MRB_TT_SYMBOL: { size_t len; const char* name = mrb_sym2name_len(mrb, v.value.sym, &len); arg->value.string.len = len; arg->value.string.ptr = mrb_malloc(mrb, len + 1); memcpy(arg->value.string.ptr, name, len + 1); } break; case MRB_TT_STRING: { struct RString *str = mrb_str_ptr(v); arg->value.string.len = str->len; arg->value.string.ptr = mrb_malloc(mrb, arg->value.string.len+1); memcpy(arg->value.string.ptr, str->ptr, arg->value.string.len+1); } break; case MRB_TT_ARRAY: { struct RArray *ary = mrb_ary_ptr(v); arg->value.array.len = ary->len; arg->value.array.ptr = mrb_malloc(mrb, ary->len * sizeof(struct task_arg)); int i; for(i=0; i<ary->len; i++) { arg->value.array.ptr[i] = mrb_value_to_task_arg(mrb, ary->ptr[i]); } } break; default: mrb_free(mrb, arg); mrb_raise(mrb, E_TYPE_ERROR, "cannot pass to other RubyVM"); break; } return arg; }
mrb_value mrb_ary_plus(mrb_state *mrb, mrb_value self) { struct RArray *a1 = mrb_ary_ptr(self); struct RArray *a2; mrb_value ary; mrb_value *ptr; int blen; mrb_get_args(mrb, "a", &ptr, &blen); ary = mrb_ary_new_capa(mrb, a1->len + blen); a2 = mrb_ary_ptr(ary); memcpy(a2->ptr, a1->ptr, sizeof(mrb_value)*a1->len); memcpy(a2->ptr + a1->len, ptr, sizeof(mrb_value)*blen); a2->len = a1->len + blen; return ary; }
MRB_API mrb_value mrb_ary_splice(mrb_state *mrb, mrb_value ary, mrb_int head, mrb_int len, mrb_value rpl) { struct RArray *a = mrb_ary_ptr(ary); mrb_int tail, size; mrb_value *argv; mrb_int i, argc; ary_modify(mrb, a); /* len check */ if (len < 0) mrb_raisef(mrb, E_INDEX_ERROR, "negative length (%S)", mrb_fixnum_value(len)); /* range check */ if (head < 0) { head += a->len; if (head < 0) { mrb_raise(mrb, E_INDEX_ERROR, "index is out of array"); } } if (a->len < len || a->len < head + len) { len = a->len - head; } tail = head + len; /* size check */ if (mrb_array_p(rpl)) { argc = RARRAY_LEN(rpl); argv = RARRAY_PTR(rpl); } else { argc = 1; argv = &rpl; } size = head + argc; if (tail < a->len) size += a->len - tail; if (size > a->aux.capa) ary_expand_capa(mrb, a, size); if (head > a->len) { ary_fill_with_nil(a->ptr + a->len, head - a->len); } else if (head < a->len) { value_move(a->ptr + head + argc, a->ptr + tail, a->len - tail); } for (i = 0; i < argc; i++) { *(a->ptr + head + i) = *(argv + i); mrb_field_write_barrier_value(mrb, (struct RBasic*)a, argv[i]); } a->len = size; return ary; }
mrb_value mrb_ary_concat_m(mrb_state *mrb, mrb_value self) { mrb_value *ptr; int blen; mrb_get_args(mrb, "a", &ptr, &blen); ary_concat(mrb, mrb_ary_ptr(self), ptr, blen); return self; }
mrb_value mrb_exc_backtrace(mrb_state *mrb, mrb_value self) { mrb_value ary; ary = mrb_ary_new(mrb); exc_output_backtrace(mrb, mrb_obj_ptr(self), get_backtrace_i, (void*)mrb_ary_ptr(ary)); return ary; }
mrb_value mrb_ary_clear(mrb_state *mrb, mrb_value self) { struct RArray *a = mrb_ary_ptr(self); a->len = 0; ary_modify(mrb, a); ary_shrink_capa(mrb, a); return self; }
mrb_value mrb_ary_ref(mrb_state *mrb, mrb_value ary, mrb_int n) { struct RArray *a = mrb_ary_ptr(ary); /* range check */ if (n < 0) n += a->len; if (n < 0 || a->len <= (int)n) return mrb_nil_value(); return a->ptr[n]; }
void mrb_ary_push(mrb_state *mrb, mrb_value ary, mrb_value elem) /* mrb_ary_push */ { struct RArray *a = mrb_ary_ptr(ary); ary_modify(mrb, a); if (a->len == a->aux.capa) ary_expand_capa(mrb, a, a->len + 1); a->ptr[a->len++] = elem; mrb_write_barrier(mrb, (struct RBasic*)a); }