summaryrefslogtreecommitdiffhomepage
path: root/src/array.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/array.c')
-rw-r--r--src/array.c129
1 files changed, 65 insertions, 64 deletions
diff --git a/src/array.c b/src/array.c
index 4e55e9ac1..ed207a2a2 100644
--- a/src/array.c
+++ b/src/array.c
@@ -10,7 +10,6 @@
#include "mruby/string.h"
#include "mruby/class.h"
-//#define ARY_DEFAULT_LEN 16
#define ARY_DEFAULT_LEN 4
#define ARY_SHRINK_RATIO 5 /* must be larger than 2 */
#ifdef LONG_MAX
@@ -56,7 +55,7 @@ ary_new_capa(mrb_state *mrb, size_t capa)
}
mrb_value
-mrb_ary_new_capa(mrb_state *mrb, size_t capa)
+mrb_ary_new_capa(mrb_state *mrb, int capa)
{
struct RArray *a = ary_new_capa(mrb, capa);
return mrb_obj_value(a);
@@ -69,7 +68,7 @@ mrb_ary_new(mrb_state *mrb)
}
mrb_value
-mrb_ary_new_from_values(mrb_state *mrb, size_t size, mrb_value *vals)
+mrb_ary_new_from_values(mrb_state *mrb, int size, mrb_value *vals)
{
mrb_value ary;
struct RArray *a;
@@ -92,7 +91,7 @@ mrb_assoc_new(mrb_state *mrb, mrb_value car, mrb_value cdr)
}
void
-ary_fill_with_nil(mrb_value *buf, size_t size)
+ary_fill_with_nil(mrb_value *buf, int size)
{
mrb_value nil = mrb_nil_value();
@@ -102,9 +101,9 @@ ary_fill_with_nil(mrb_value *buf, size_t size)
}
void
-mrb_ary_expand_capa(mrb_state *mrb, struct RArray *a, size_t len)
+mrb_ary_expand_capa(mrb_state *mrb, struct RArray *a, int len)
{
- size_t capa = a->capa;
+ int capa = a->capa;
#ifdef LONG_MAX
if (len > ARY_MAX_SIZE) {
@@ -134,7 +133,7 @@ mrb_ary_expand_capa(mrb_state *mrb, struct RArray *a, size_t len)
void
mrb_ary_shrink_capa(mrb_state *mrb, struct RArray *a)
{
- size_t capa = a->capa;
+ int capa = a->capa;
if (capa < ARY_DEFAULT_LEN * 2) return;
if (capa <= a->len * ARY_SHRINK_RATIO) return;
@@ -160,29 +159,36 @@ mrb_ary_s_create(mrb_state *mrb, mrb_value self)
int len;
mrb_get_args(mrb, "*", &vals, &len);
- return mrb_ary_new_from_values(mrb, (size_t)len, vals);
+ return mrb_ary_new_from_values(mrb, (int)len, vals);
+}
+
+static void
+ary_concat(mrb_state *mrb, struct RArray *a, mrb_value *buf, int blen)
+{
+ int len = a->len + blen;
+
+ if (a->capa < len) mrb_ary_expand_capa(mrb, a, len);
+ memcpy(a->buf+a->len, buf, sizeof(mrb_value)*blen);
+ mrb_write_barrier(mrb, (struct RBasic*)a);
+ a->len = len;
}
void
mrb_ary_concat(mrb_state *mrb, mrb_value self, mrb_value other)
{
- struct RArray *a1 = mrb_ary_ptr(self);
struct RArray *a2 = mrb_ary_ptr(other);
- size_t len = a1->len + a2->len;
- if (a1->capa < len) mrb_ary_expand_capa(mrb, a1, len);
- memcpy(a1->buf+a1->len, a2->buf, sizeof(mrb_value)*a2->len);
- mrb_write_barrier(mrb, (struct RBasic*)a1);
- a1->len = len;
+ ary_concat(mrb, mrb_ary_ptr(self), a2->buf, a2->len);
}
mrb_value
mrb_ary_concat_m(mrb_state *mrb, mrb_value self)
{
- mrb_value other;
+ mrb_value *buf;
+ int blen;
- mrb_get_args(mrb, "A", &other);
- mrb_ary_concat(mrb, self, other);
+ mrb_get_args(mrb, "a", &buf, &blen);
+ ary_concat(mrb, mrb_ary_ptr(self), buf, blen);
return self;
}
@@ -191,15 +197,16 @@ mrb_ary_plus(mrb_state *mrb, mrb_value self)
{
struct RArray *a1 = mrb_ary_ptr(self);
struct RArray *a2;
- mrb_value other;
mrb_value ary;
+ mrb_value *buf;
+ int blen;
- mrb_get_args(mrb, "A", &other);
- ary = mrb_ary_new_capa(mrb, a1->len + RARRAY_LEN(other));
+ mrb_get_args(mrb, "a", &buf, &blen);
+ ary = mrb_ary_new_capa(mrb, a1->len + blen);
a2 = mrb_ary_ptr(ary);
memcpy(a2->buf, a1->buf, sizeof(mrb_value)*a1->len);
- memcpy(a2->buf + a1->len, RARRAY_PTR(other), sizeof(mrb_value)*RARRAY_LEN(other));
- a2->len = a1->len + RARRAY_LEN(other);
+ memcpy(a2->buf + a1->len, buf, sizeof(mrb_value)*blen);
+ a2->len = a1->len + blen;
return ary;
}
@@ -248,8 +255,8 @@ mrb_ary_cmp(mrb_state *mrb, mrb_value ary1)
return mrb_fixnum_value((len == 0)? 0: (len > 0)? 1: -1);
}
-void
-mrb_ary_replace(mrb_state *mrb, struct RArray *a, mrb_value *argv, size_t len)
+static void
+ary_replace(mrb_state *mrb, struct RArray *a, mrb_value *argv, int len)
{
if (a->capa < len) mrb_ary_expand_capa(mrb, a, len);
memcpy(a->buf, argv, sizeof(mrb_value)*len);
@@ -257,13 +264,21 @@ mrb_ary_replace(mrb_state *mrb, struct RArray *a, mrb_value *argv, size_t len)
a->len = len;
}
+void
+mrb_ary_replace(mrb_state *mrb, mrb_value self, mrb_value other)
+{
+ struct RArray *a2 = mrb_ary_ptr(other);
+
+ ary_replace(mrb, mrb_ary_ptr(self), a2->buf, a2->len);
+}
+
mrb_value
mrb_ary_replace_m(mrb_state *mrb, mrb_value self)
{
mrb_value other;
- mrb_get_args(mrb, "o", &other);
- mrb_ary_replace(mrb, mrb_ary_ptr(self), RARRAY_PTR(other), RARRAY_LEN(other));
+ mrb_get_args(mrb, "A", &other);
+ mrb_ary_replace(mrb, self, other);
return self;
}
@@ -276,7 +291,6 @@ mrb_ary_times(mrb_state *mrb, mrb_value self)
mrb_value ary;
mrb_value *buf;
mrb_int times;
- //size_t len;
mrb_get_args(mrb, "i", &times);
if (times < 0) {
@@ -339,21 +353,21 @@ mrb_ary_reverse(mrb_state *mrb, mrb_value self)
}
mrb_value
-mrb_ary_new4(mrb_state *mrb, long n, const mrb_value *elts)
+mrb_ary_new4(mrb_state *mrb, int n, const mrb_value *elts)
{
mrb_value ary;
- ary = mrb_ary_new_capa(mrb, n);//mrb_ary_new2(n);
+ ary = mrb_ary_new_capa(mrb, n);
if (n > 0 && elts) {
memcpy(RARRAY_PTR(ary), elts, sizeof(mrb_value)*n);
- RARRAY_LEN(ary) = n; //ARY_SET_LEN(ary, n);
+ RARRAY_LEN(ary) = n;
}
return ary;
}
mrb_value
-mrb_ary_new_elts(mrb_state *mrb, long n, const mrb_value *elts)
+mrb_ary_new_elts(mrb_state *mrb, int n, const mrb_value *elts)
{
return mrb_ary_new4(mrb, n, elts);
}
@@ -405,7 +419,7 @@ mrb_ary_shift(mrb_state *mrb, mrb_value self)
{
struct RArray *a = mrb_ary_ptr(self);
mrb_value *buf = a->buf;
- size_t size = a->len;
+ int size = a->len;
mrb_value val;
if (size == 0) return mrb_nil_value();
@@ -463,7 +477,7 @@ 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 <= (size_t)n) return mrb_nil_value();
+ if (n < 0 || a->len <= (int)n) return mrb_nil_value();
return a->buf[n];
}
@@ -478,8 +492,8 @@ mrb_ary_set(mrb_state *mrb, mrb_value ary, mrb_int n, mrb_value val) /* rb_ary_s
if (n < 0) {
mrb_raise(mrb, E_INDEX_ERROR, "index %ld out of array", n - a->len);
}
- if (a->len <= (size_t)n) {
- if (a->capa <= (size_t)n) mrb_ary_expand_capa(mrb, a, n + 1);
+ if (a->len <= (int)n) {
+ if (a->capa <= (int)n) mrb_ary_expand_capa(mrb, a, n + 1);
ary_fill_with_nil(a->buf + a->len, n + 1 - a->len);
a->len = n + 1;
}
@@ -493,7 +507,7 @@ mrb_ary_splice(mrb_state *mrb, mrb_value ary, mrb_int head, mrb_int len, mrb_val
{
struct RArray *a = mrb_ary_ptr(ary);
mrb_int tail;
- size_t size;
+ int size;
mrb_value *argv;
int i, argc;
@@ -520,7 +534,7 @@ mrb_ary_splice(mrb_state *mrb, mrb_value ary, mrb_int head, mrb_int len, mrb_val
if (size > a->capa) mrb_ary_expand_capa(mrb, a, size);
if (head > a->len) {
- ary_fill_with_nil(a->buf + a->len, (size_t)(head - a->len));
+ ary_fill_with_nil(a->buf + a->len, (int)(head - a->len));
}
else if (head < a->len) {
memmove(a->buf + head + argc, a->buf + tail, sizeof(mrb_value)*(a->len - tail));
@@ -560,10 +574,10 @@ mrb_ary_aget(mrb_state *mrb, mrb_value self)
}
len = mrb_fixnum(argv[0]);
if (index < 0) index += a->len;
- if (index < 0 || a->len < (size_t)index) return mrb_nil_value();
+ if (index < 0 || a->len < (int)index) return mrb_nil_value();
if ((len = mrb_fixnum(argv[0])) < 0) return mrb_nil_value();
- if (a->len == (size_t)index) return mrb_ary_new(mrb);
- if ((size_t)len > a->len - index) len = a->len - index;
+ if (a->len == (int)index) return mrb_ary_new(mrb);
+ if ((int)len > a->len - index) len = a->len - index;
return mrb_ary_new_from_values(mrb, len, a->buf + index);
default:
@@ -609,11 +623,11 @@ mrb_ary_delete_at(mrb_state *mrb, mrb_value self)
mrb_int index;
mrb_value val;
mrb_value *buf;
- size_t len;
+ int len;
mrb_get_args(mrb, "i", &index);
if (index < 0) index += a->len;
- if (index < 0 || a->len <= (size_t)index) return mrb_nil_value();
+ if (index < 0 || a->len <= (int)index) return mrb_nil_value();
val = a->buf[index];
@@ -634,8 +648,7 @@ mrb_value
mrb_ary_first(mrb_state *mrb, mrb_value self)
{
struct RArray *a = mrb_ary_ptr(self);
- //mrb_value ary;
- size_t size;
+ int size;
mrb_value *vals;
int len;
@@ -656,8 +669,7 @@ mrb_value
mrb_ary_last(mrb_state *mrb, mrb_value self)
{
struct RArray *a = mrb_ary_ptr(self);
- //mrb_value ary;
- size_t size;
+ int size;
mrb_value *vals;
int len;
@@ -749,7 +761,7 @@ mrb_check_array_type(mrb_state *mrb, mrb_value ary)
}
mrb_value
-mrb_ary_entry(mrb_value ary, long offset)
+mrb_ary_entry(mrb_value ary, int offset)
{
if (offset < 0) {
offset += RARRAY_LEN(ary);
@@ -757,24 +769,16 @@ mrb_ary_entry(mrb_value ary, long offset)
return ary_elt(ary, offset);
}
-void
-mrb_mem_clear(mrb_value *mem, long size)
-{
- while (size--) {
- *mem++ = mrb_nil_value();
- }
-}
-
mrb_value
-mrb_ary_tmp_new(mrb_state *mrb, long capa)
+mrb_ary_tmp_new(mrb_state *mrb, int capa)
{
- return mrb_ary_new_capa(mrb, capa);//ary_new(0, capa);
+ return mrb_ary_new_capa(mrb, capa);
}
static mrb_value
inspect_ary(mrb_state *mrb, mrb_value ary, mrb_value list)
{
- long i;
+ int i;
mrb_value s, arystr;
char *head = "[";
char *sep = ", ";
@@ -803,7 +807,6 @@ inspect_ary(mrb_state *mrb, mrb_value ary, mrb_value list)
} else {
s = mrb_inspect(mrb, RARRAY_PTR(ary)[i]);
}
- //mrb_str_buf_append(mrb, arystr, s);
mrb_str_buf_cat(mrb, arystr, RSTRING_PTR(s), RSTRING_LEN(s));
mrb_gc_arena_restore(mrb, ai);
}
@@ -837,7 +840,7 @@ mrb_ary_inspect(mrb_state *mrb, mrb_value ary)
static mrb_value
join_ary(mrb_state *mrb, mrb_value ary, mrb_value sep, mrb_value list)
{
- long i;
+ int i;
mrb_value result, val, tmp;
/* check recursive */
@@ -853,7 +856,6 @@ join_ary(mrb_state *mrb, mrb_value ary, mrb_value sep, mrb_value list)
for(i=0; i<RARRAY_LEN(ary); i++) {
if (i > 0 && !mrb_nil_p(sep)) {
- //mrb_str_buf_append(mrb, result, sep); // segv (encoding error?)
mrb_str_buf_cat(mrb, result, RSTRING_PTR(sep), RSTRING_LEN(sep));
}
@@ -866,7 +868,6 @@ join_ary(mrb_state *mrb, mrb_value ary, mrb_value sep, mrb_value list)
case MRB_TT_STRING:
str_join:
- //mrb_str_buf_append(mrb, result, val);
mrb_str_buf_cat(mrb, result, RSTRING_PTR(val), RSTRING_LEN(val));
break;
@@ -953,7 +954,7 @@ mrb_ary_equal(mrb_state *mrb, mrb_value ary1)
}
if (RARRAY_LEN(ary1) != RARRAY_LEN(ary2)) return mrb_false_value();
else {
- long i;
+ int i;
for (i=0; i<RARRAY_LEN(ary1); i++) {
if (!mrb_equal(mrb, ary_elt(ary1, i), ary_elt(ary2, i)))
@@ -982,7 +983,7 @@ mrb_ary_eql(mrb_state *mrb, mrb_value ary1)
if (mrb_type(ary2) != MRB_TT_ARRAY) return mrb_false_value();
if (RARRAY_LEN(ary1) != RARRAY_LEN(ary2)) return mrb_false_value();
else {
- long i;
+ int i;
for (i=0; i<RARRAY_LEN(ary1); i++) {
if (!mrb_eql(mrb, ary_elt(ary1, i), ary_elt(ary2, i)))