mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
array implementation: several small changes
src/array.c:
- make various functions without declaration in the mruby headers static
- removed all uncessary int casts and two useless comments
mrb_ary_new_from_values: move to similar functions
mrb_check_array_type: fix indentation
include/mruby/array.h:
mrb_shared_array: padding (default "mrbconf.h" on an usual 64-bit system)
sizeof(mrb_int)==sizeof(int)==4 && sizeof(mrb_value*)==8
both:
mrb_ary_aget: remove declaration in header and make static
nobody uses it which is not surprising since it has 1 req arg!
mrb_assoc_new: small optimization and move to similar functions
mrb_ary_len: re-implement as static inline function (it is used by mrbgems)
programmers should directly use RARRAY_LEN instead
This commit is contained in:
+10
-4
@@ -13,8 +13,8 @@ extern "C" {
|
||||
|
||||
typedef struct mrb_shared_array {
|
||||
int refcnt;
|
||||
mrb_value *ptr;
|
||||
mrb_int len;
|
||||
mrb_value *ptr;
|
||||
} mrb_shared_array;
|
||||
|
||||
struct RArray {
|
||||
@@ -40,23 +40,29 @@ void mrb_ary_decref(mrb_state*, mrb_shared_array*);
|
||||
mrb_value mrb_ary_new_capa(mrb_state*, mrb_int);
|
||||
mrb_value mrb_ary_new(mrb_state *mrb);
|
||||
mrb_value mrb_ary_new_from_values(mrb_state *mrb, mrb_int size, const mrb_value *vals);
|
||||
mrb_value mrb_assoc_new(mrb_state *mrb, mrb_value car, mrb_value cdr);
|
||||
void mrb_ary_concat(mrb_state*, mrb_value, mrb_value);
|
||||
mrb_value mrb_ary_splat(mrb_state*, mrb_value);
|
||||
void mrb_ary_push(mrb_state*, mrb_value, mrb_value);
|
||||
mrb_value mrb_ary_pop(mrb_state *mrb, mrb_value ary);
|
||||
mrb_value mrb_ary_aget(mrb_state *mrb, mrb_value self);
|
||||
mrb_value mrb_ary_ref(mrb_state *mrb, mrb_value ary, mrb_int n);
|
||||
void mrb_ary_set(mrb_state *mrb, mrb_value ary, mrb_int n, mrb_value val);
|
||||
mrb_int mrb_ary_len(mrb_state *mrb, mrb_value ary);
|
||||
void mrb_ary_replace(mrb_state *mrb, mrb_value a, mrb_value b);
|
||||
mrb_value mrb_check_array_type(mrb_state *mrb, mrb_value self);
|
||||
mrb_value mrb_ary_unshift(mrb_state *mrb, mrb_value self, mrb_value item);
|
||||
mrb_value mrb_assoc_new(mrb_state *mrb, mrb_value car, mrb_value cdr);
|
||||
mrb_value mrb_ary_entry(mrb_value ary, mrb_int offset);
|
||||
mrb_value mrb_ary_shift(mrb_state *mrb, mrb_value self);
|
||||
mrb_value mrb_ary_clear(mrb_state *mrb, mrb_value self);
|
||||
mrb_value mrb_ary_join(mrb_state *mrb, mrb_value ary, mrb_value sep);
|
||||
|
||||
static inline mrb_int
|
||||
mrb_ary_len(mrb_state *mrb, mrb_value ary)
|
||||
{
|
||||
(void)mrb;
|
||||
mrb_assert(mrb_array_p(ary));
|
||||
return RARRAY_LEN(ary);
|
||||
}
|
||||
|
||||
#if defined(__cplusplus)
|
||||
} /* extern "C" { */
|
||||
#endif
|
||||
|
||||
+52
-55
@@ -85,13 +85,30 @@ array_copy(mrb_value *dst, const mrb_value *src, size_t size)
|
||||
}
|
||||
}
|
||||
|
||||
mrb_value
|
||||
mrb_ary_new_from_values(mrb_state *mrb, mrb_int size, const mrb_value *vals)
|
||||
{
|
||||
mrb_value ary;
|
||||
struct RArray *a;
|
||||
|
||||
ary = mrb_ary_new_capa(mrb, size);
|
||||
a = mrb_ary_ptr(ary);
|
||||
array_copy(a->ptr, vals, size);
|
||||
a->len = size;
|
||||
|
||||
return ary;
|
||||
}
|
||||
|
||||
mrb_value
|
||||
mrb_assoc_new(mrb_state *mrb, mrb_value car, mrb_value cdr)
|
||||
{
|
||||
mrb_value arv[2];
|
||||
arv[0] = car;
|
||||
arv[1] = cdr;
|
||||
return mrb_ary_new_from_values(mrb, 2, arv);
|
||||
struct RArray *a;
|
||||
|
||||
a = ary_new_capa(mrb, 2);
|
||||
a->ptr[0] = car;
|
||||
a->ptr[1] = cdr;
|
||||
a->len = 2;
|
||||
return mrb_obj_value(a);
|
||||
}
|
||||
|
||||
static void
|
||||
@@ -99,7 +116,7 @@ ary_fill_with_nil(mrb_value *ptr, mrb_int size)
|
||||
{
|
||||
mrb_value nil = mrb_nil_value();
|
||||
|
||||
while ((int)(size--)) {
|
||||
while (size--) {
|
||||
*ptr++ = nil;
|
||||
}
|
||||
}
|
||||
@@ -213,7 +230,7 @@ ary_shrink_capa(mrb_state *mrb, struct RArray *a)
|
||||
}
|
||||
}
|
||||
|
||||
mrb_value
|
||||
static mrb_value
|
||||
mrb_ary_s_create(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
mrb_value *vals;
|
||||
@@ -243,7 +260,7 @@ mrb_ary_concat(mrb_state *mrb, mrb_value self, mrb_value other)
|
||||
ary_concat(mrb, mrb_ary_ptr(self), a2->ptr, a2->len);
|
||||
}
|
||||
|
||||
mrb_value
|
||||
static mrb_value
|
||||
mrb_ary_concat_m(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
mrb_value *ptr;
|
||||
@@ -254,7 +271,7 @@ mrb_ary_concat_m(mrb_state *mrb, mrb_value self)
|
||||
return self;
|
||||
}
|
||||
|
||||
mrb_value
|
||||
static mrb_value
|
||||
mrb_ary_plus(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
struct RArray *a1 = mrb_ary_ptr(self);
|
||||
@@ -291,7 +308,7 @@ mrb_ary_plus(mrb_state *mrb, mrb_value self)
|
||||
* [ 1, 2, 3, 4, 5, 6 ] <=> [ 1, 2 ] #=> +1
|
||||
*
|
||||
*/
|
||||
mrb_value
|
||||
static mrb_value
|
||||
mrb_ary_cmp(mrb_state *mrb, mrb_value ary1)
|
||||
{
|
||||
mrb_value ary2;
|
||||
@@ -339,7 +356,7 @@ mrb_ary_replace(mrb_state *mrb, mrb_value self, mrb_value other)
|
||||
ary_replace(mrb, mrb_ary_ptr(self), a2->ptr, a2->len);
|
||||
}
|
||||
|
||||
mrb_value
|
||||
static mrb_value
|
||||
mrb_ary_replace_m(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
mrb_value other;
|
||||
@@ -350,7 +367,7 @@ mrb_ary_replace_m(mrb_state *mrb, mrb_value self)
|
||||
return self;
|
||||
}
|
||||
|
||||
mrb_value
|
||||
static mrb_value
|
||||
mrb_ary_times(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
struct RArray *a1 = mrb_ary_ptr(self);
|
||||
@@ -377,7 +394,7 @@ mrb_ary_times(mrb_state *mrb, mrb_value self)
|
||||
return ary;
|
||||
}
|
||||
|
||||
mrb_value
|
||||
static mrb_value
|
||||
mrb_ary_reverse_bang(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
struct RArray *a = mrb_ary_ptr(self);
|
||||
@@ -398,7 +415,7 @@ mrb_ary_reverse_bang(mrb_state *mrb, mrb_value self)
|
||||
return self;
|
||||
}
|
||||
|
||||
mrb_value
|
||||
static mrb_value
|
||||
mrb_ary_reverse(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
struct RArray *a = mrb_ary_ptr(self), *b;
|
||||
@@ -420,22 +437,8 @@ mrb_ary_reverse(mrb_state *mrb, mrb_value self)
|
||||
return ary;
|
||||
}
|
||||
|
||||
mrb_value
|
||||
mrb_ary_new_from_values(mrb_state *mrb, mrb_int size, const mrb_value *vals)
|
||||
{
|
||||
mrb_value ary;
|
||||
struct RArray *a;
|
||||
|
||||
ary = mrb_ary_new_capa(mrb, size);
|
||||
a = mrb_ary_ptr(ary);
|
||||
array_copy(a->ptr, vals, size);
|
||||
a->len = size;
|
||||
|
||||
return ary;
|
||||
}
|
||||
|
||||
void
|
||||
mrb_ary_push(mrb_state *mrb, mrb_value ary, mrb_value elem) /* mrb_ary_push */
|
||||
mrb_ary_push(mrb_state *mrb, mrb_value ary, mrb_value elem)
|
||||
{
|
||||
struct RArray *a = mrb_ary_ptr(ary);
|
||||
|
||||
@@ -446,7 +449,7 @@ mrb_ary_push(mrb_state *mrb, mrb_value ary, mrb_value elem) /* mrb_ary_push */
|
||||
mrb_write_barrier(mrb, (struct RBasic*)a);
|
||||
}
|
||||
|
||||
mrb_value
|
||||
static mrb_value
|
||||
mrb_ary_push_m(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
mrb_value *argv;
|
||||
@@ -494,7 +497,7 @@ mrb_ary_shift(mrb_state *mrb, mrb_value self)
|
||||
mrb_int size = a->len;
|
||||
|
||||
val = *ptr;
|
||||
while ((int)(--size)) {
|
||||
while (--size) {
|
||||
*ptr = *(ptr+1);
|
||||
++ptr;
|
||||
}
|
||||
@@ -531,7 +534,7 @@ mrb_ary_unshift(mrb_state *mrb, mrb_value self, mrb_value item)
|
||||
return self;
|
||||
}
|
||||
|
||||
mrb_value
|
||||
static mrb_value
|
||||
mrb_ary_unshift_m(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
struct RArray *a = mrb_ary_ptr(self);
|
||||
@@ -565,13 +568,13 @@ mrb_ary_ref(mrb_state *mrb, mrb_value ary, mrb_int n)
|
||||
|
||||
/* range check */
|
||||
if (n < 0) n += a->len;
|
||||
if (n < 0 || a->len <= (int)n) return mrb_nil_value();
|
||||
if (n < 0 || a->len <= n) return mrb_nil_value();
|
||||
|
||||
return a->ptr[n];
|
||||
}
|
||||
|
||||
void
|
||||
mrb_ary_set(mrb_state *mrb, mrb_value ary, mrb_int n, mrb_value val) /* rb_ary_store */
|
||||
mrb_ary_set(mrb_state *mrb, mrb_value ary, mrb_int n, mrb_value val)
|
||||
{
|
||||
struct RArray *a = mrb_ary_ptr(ary);
|
||||
|
||||
@@ -583,8 +586,8 @@ mrb_ary_set(mrb_state *mrb, mrb_value ary, mrb_int n, mrb_value val) /* rb_ary_s
|
||||
mrb_raisef(mrb, E_INDEX_ERROR, "index %S out of array", mrb_fixnum_value(n - a->len));
|
||||
}
|
||||
}
|
||||
if (a->len <= (int)n) {
|
||||
if (a->aux.capa <= (int)n)
|
||||
if (a->len <= n) {
|
||||
if (a->aux.capa <= n)
|
||||
ary_expand_capa(mrb, a, n + 1);
|
||||
ary_fill_with_nil(a->ptr + a->len, n + 1 - a->len);
|
||||
a->len = n + 1;
|
||||
@@ -631,7 +634,7 @@ mrb_ary_splice(mrb_state *mrb, mrb_value ary, mrb_int head, mrb_int len, mrb_val
|
||||
ary_expand_capa(mrb, a, size);
|
||||
|
||||
if (head > a->len) {
|
||||
ary_fill_with_nil(a->ptr + a->len, (int)(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);
|
||||
@@ -646,12 +649,6 @@ mrb_ary_splice(mrb_state *mrb, mrb_value ary, mrb_int head, mrb_int len, mrb_val
|
||||
return ary;
|
||||
}
|
||||
|
||||
mrb_int
|
||||
mrb_ary_len(mrb_state *mrb, mrb_value ary)
|
||||
{
|
||||
return RARRAY_LEN(ary);
|
||||
}
|
||||
|
||||
void
|
||||
mrb_ary_decref(mrb_state *mrb, mrb_shared_array *shared)
|
||||
{
|
||||
@@ -719,7 +716,7 @@ aget_index(mrb_state *mrb, mrb_value index)
|
||||
*
|
||||
*/
|
||||
|
||||
mrb_value
|
||||
static mrb_value
|
||||
mrb_ary_aget(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
struct RArray *a = mrb_ary_ptr(self);
|
||||
@@ -745,9 +742,9 @@ mrb_ary_aget(mrb_state *mrb, mrb_value self)
|
||||
|
||||
i = aget_index(mrb, index);
|
||||
if (i < 0) i += a->len;
|
||||
if (i < 0 || a->len < (int)i) return mrb_nil_value();
|
||||
if (i < 0 || a->len < i) return mrb_nil_value();
|
||||
if (len < 0) return mrb_nil_value();
|
||||
if (a->len == (int)i) return mrb_ary_new(mrb);
|
||||
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);
|
||||
@@ -788,7 +785,7 @@ mrb_ary_aget(mrb_state *mrb, mrb_value self)
|
||||
* a[3, 0] = "B" #=> [1, 2, "A", "B"]
|
||||
*/
|
||||
|
||||
mrb_value
|
||||
static mrb_value
|
||||
mrb_ary_aset(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
mrb_value v1, v2, v3;
|
||||
@@ -818,7 +815,7 @@ mrb_ary_aset(mrb_state *mrb, mrb_value self)
|
||||
return v3;
|
||||
}
|
||||
|
||||
mrb_value
|
||||
static mrb_value
|
||||
mrb_ary_delete_at(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
struct RArray *a = mrb_ary_ptr(self);
|
||||
@@ -829,14 +826,14 @@ mrb_ary_delete_at(mrb_state *mrb, mrb_value self)
|
||||
|
||||
mrb_get_args(mrb, "i", &index);
|
||||
if (index < 0) index += a->len;
|
||||
if (index < 0 || a->len <= (int)index) return mrb_nil_value();
|
||||
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 ((int)(--len)) {
|
||||
while (--len) {
|
||||
*ptr = *(ptr+1);
|
||||
++ptr;
|
||||
}
|
||||
@@ -847,7 +844,7 @@ mrb_ary_delete_at(mrb_state *mrb, mrb_value self)
|
||||
return val;
|
||||
}
|
||||
|
||||
mrb_value
|
||||
static mrb_value
|
||||
mrb_ary_first(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
struct RArray *a = mrb_ary_ptr(self);
|
||||
@@ -867,7 +864,7 @@ mrb_ary_first(mrb_state *mrb, mrb_value self)
|
||||
return mrb_ary_new_from_values(mrb, size, a->ptr);
|
||||
}
|
||||
|
||||
mrb_value
|
||||
static mrb_value
|
||||
mrb_ary_last(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
struct RArray *a = mrb_ary_ptr(self);
|
||||
@@ -894,7 +891,7 @@ mrb_ary_last(mrb_state *mrb, mrb_value self)
|
||||
return mrb_ary_new_from_values(mrb, size, a->ptr + a->len - size);
|
||||
}
|
||||
|
||||
mrb_value
|
||||
static mrb_value
|
||||
mrb_ary_index_m(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
mrb_value obj;
|
||||
@@ -909,7 +906,7 @@ mrb_ary_index_m(mrb_state *mrb, mrb_value self)
|
||||
return mrb_nil_value();
|
||||
}
|
||||
|
||||
mrb_value
|
||||
static mrb_value
|
||||
mrb_ary_rindex_m(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
mrb_value obj;
|
||||
@@ -960,7 +957,7 @@ mrb_ary_clear(mrb_state *mrb, mrb_value self)
|
||||
return self;
|
||||
}
|
||||
|
||||
mrb_value
|
||||
static mrb_value
|
||||
mrb_ary_empty_p(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
struct RArray *a = mrb_ary_ptr(self);
|
||||
@@ -971,7 +968,7 @@ mrb_ary_empty_p(mrb_state *mrb, mrb_value self)
|
||||
mrb_value
|
||||
mrb_check_array_type(mrb_state *mrb, mrb_value ary)
|
||||
{
|
||||
return mrb_check_convert_type(mrb, ary, MRB_TT_ARRAY, "Array", "to_ary");
|
||||
return mrb_check_convert_type(mrb, ary, MRB_TT_ARRAY, "Array", "to_ary");
|
||||
}
|
||||
|
||||
mrb_value
|
||||
|
||||
Reference in New Issue
Block a user