summaryrefslogtreecommitdiff
path: root/array.c
diff options
context:
space:
mode:
Diffstat (limited to 'array.c')
-rw-r--r--array.c547
1 files changed, 283 insertions, 264 deletions
diff --git a/array.c b/array.c
index 6150d862dd..ce4553bfff 100644
--- a/array.c
+++ b/array.c
@@ -12,13 +12,12 @@
#include "ruby.h"
-
-VALUE cArray;
+VALUE rb_cArray;
#define ARY_DEFAULT_SIZE 16
void
-memclear(mem, size)
+rb_mem_clear(mem, size)
register VALUE *mem;
register int size;
{
@@ -39,19 +38,20 @@ memfill(mem, size, val)
}
#define ARY_FREEZE FL_USER1
+#define ARY_TMPLOCK FL_USER2
static void
-ary_modify(ary)
+rb_ary_modify(ary)
VALUE ary;
{
rb_secure(5);
- if (FL_TEST(ary, ARY_FREEZE)) {
- TypeError("can't modify frozen array");
+ if (FL_TEST(ary, ARY_FREEZE|ARY_TMPLOCK)) {
+ rb_raise(rb_eTypeError, "can't modify frozen array");
}
}
VALUE
-ary_freeze(ary)
+rb_ary_freeze(ary)
VALUE ary;
{
FL_SET(ary, ARY_FREEZE);
@@ -59,40 +59,39 @@ ary_freeze(ary)
}
static VALUE
-ary_frozen_p(ary)
+rb_ary_frozen_p(ary)
VALUE ary;
{
- if (FL_TEST(ary, ARY_FREEZE))
- return TRUE;
- return FALSE;
+ if (FL_TEST(ary, ARY_FREEZE|ARY_TMPLOCK))
+ return Qtrue;
+ return Qfalse;
}
VALUE
-ary_new2(len)
+rb_ary_new2(len)
int len;
{
NEWOBJ(ary, struct RArray);
- OBJSETUP(ary, cArray, T_ARRAY);
+ OBJSETUP(ary, rb_cArray, T_ARRAY);
if (len < 0) {
- ArgError("negative array size (or size too big)");
+ rb_raise(rb_eArgError, "negative array size (or size too big)");
}
if (len > 0 && len*sizeof(VALUE) <= 0) {
- ArgError("array size too big");
+ rb_raise(rb_eArgError, "array size too big");
}
ary->len = 0;
ary->capa = len;
ary->ptr = 0;
ary->ptr = ALLOC_N(VALUE, len);
- memclear(ary->ptr, len);
return (VALUE)ary;
}
VALUE
-ary_new()
+rb_ary_new()
{
- return ary_new2(ARY_DEFAULT_SIZE);
+ return rb_ary_new2(ARY_DEFAULT_SIZE);
}
#ifdef HAVE_STDARG_PROTOTYPES
@@ -105,9 +104,9 @@ ary_new()
VALUE
#ifdef HAVE_STDARG_PROTOTYPES
-ary_new3(int n, ...)
+rb_ary_new3(int n, ...)
#else
-ary_new3(n, va_alist)
+rb_ary_new3(n, va_alist)
int n;
va_dcl
#endif
@@ -117,9 +116,9 @@ ary_new3(n, va_alist)
int i;
if (n < 0) {
- IndexError("Negative number of items(%d)", n);
+ rb_raise(rb_eIndexError, "Negative number of items(%d)", n);
}
- ary = ary_new2(n<ARY_DEFAULT_SIZE?ARY_DEFAULT_SIZE:n);
+ ary = rb_ary_new2(n<ARY_DEFAULT_SIZE?ARY_DEFAULT_SIZE:n);
va_init_list(ar, n);
for (i=0; i<n; i++) {
@@ -132,13 +131,13 @@ ary_new3(n, va_alist)
}
VALUE
-ary_new4(n, elts)
+rb_ary_new4(n, elts)
int n;
VALUE *elts;
{
VALUE ary;
- ary = ary_new2(n);
+ ary = rb_ary_new2(n);
if (elts) {
MEMCPY(RARRAY(ary)->ptr, elts, VALUE, n);
}
@@ -148,12 +147,12 @@ ary_new4(n, elts)
}
VALUE
-assoc_new(car, cdr)
+rb_assoc_new(car, cdr)
VALUE car, cdr;
{
VALUE ary;
- ary = ary_new2(2);
+ ary = rb_ary_new2(2);
RARRAY(ary)->ptr[0] = car;
RARRAY(ary)->ptr[1] = cdr;
RARRAY(ary)->len = 2;
@@ -162,7 +161,7 @@ assoc_new(car, cdr)
}
static VALUE
-ary_s_new(argc, argv, klass)
+rb_ary_s_new(argc, argv, klass)
int argc;
VALUE *argv;
VALUE klass;
@@ -181,10 +180,10 @@ ary_s_new(argc, argv, klass)
int capa = NUM2INT(size);
if (capa < 0) {
- ArgError("negative array size");
+ rb_raise(rb_eArgError, "negative array size");
}
if (capa > 0 && capa*sizeof(VALUE) <= 0) {
- ArgError("array size too big");
+ rb_raise(rb_eArgError, "array size too big");
}
ary->capa = capa;
len = capa;
@@ -192,13 +191,13 @@ ary_s_new(argc, argv, klass)
ary->ptr = ALLOC_N(VALUE, ary->capa);
memfill(ary->ptr, len, val);
ary->len = len;
- obj_call_init((VALUE)ary);
+ rb_obj_call_init((VALUE)ary);
return (VALUE)ary;
}
static VALUE
-ary_s_create(argc, argv, klass)
+rb_ary_s_create(argc, argv, klass)
int argc;
VALUE *argv;
VALUE klass;
@@ -220,14 +219,14 @@ ary_s_create(argc, argv, klass)
}
void
-ary_store(ary, idx, val)
+rb_ary_store(ary, idx, val)
VALUE ary;
int idx;
VALUE val;
{
- ary_modify(ary);
+ rb_ary_modify(ary);
if (idx < 0) {
- IndexError("negative index for array");
+ rb_raise(rb_eIndexError, "negative index for array");
}
if (idx >= RARRAY(ary)->capa) {
@@ -235,7 +234,8 @@ ary_store(ary, idx, val)
REALLOC_N(RARRAY(ary)->ptr, VALUE, RARRAY(ary)->capa);
}
if (idx > RARRAY(ary)->len) {
- memclear(RARRAY(ary)->ptr+RARRAY(ary)->len, idx-RARRAY(ary)->len+1);
+ rb_mem_clear(RARRAY(ary)->ptr+RARRAY(ary)->len,
+ idx-RARRAY(ary)->len+1);
}
if (idx >= RARRAY(ary)->len) {
@@ -245,28 +245,28 @@ ary_store(ary, idx, val)
}
VALUE
-ary_push(ary, item)
+rb_ary_push(ary, item)
VALUE ary;
VALUE item;
{
- ary_store(ary, RARRAY(ary)->len, item);
+ rb_ary_store(ary, RARRAY(ary)->len, item);
return ary;
}
static VALUE
-ary_push_method(argc, argv, ary)
+rb_ary_push_method(argc, argv, ary)
int argc;
VALUE *argv;
VALUE ary;
{
while (argc--) {
- ary_store(ary, RARRAY(ary)->len, *argv++);
+ rb_ary_store(ary, RARRAY(ary)->len, *argv++);
}
return ary;
}
VALUE
-ary_pop(ary)
+rb_ary_pop(ary)
VALUE ary;
{
if (RARRAY(ary)->len == 0) return Qnil;
@@ -278,7 +278,7 @@ ary_pop(ary)
}
VALUE
-ary_shift(ary)
+rb_ary_shift(ary)
VALUE ary;
{
VALUE top;
@@ -299,10 +299,10 @@ ary_shift(ary)
}
VALUE
-ary_unshift(ary, item)
+rb_ary_unshift(ary, item)
VALUE ary, item;
{
- ary_modify(ary);
+ rb_ary_modify(ary);
if (RARRAY(ary)->len >= RARRAY(ary)->capa) {
RARRAY(ary)->capa+=ARY_DEFAULT_SIZE;
REALLOC_N(RARRAY(ary)->ptr, VALUE, RARRAY(ary)->capa);
@@ -316,7 +316,7 @@ ary_unshift(ary, item)
}
VALUE
-ary_entry(ary, offset)
+rb_ary_entry(ary, offset)
VALUE ary;
int offset;
{
@@ -333,7 +333,7 @@ ary_entry(ary, offset)
}
static VALUE
-ary_subseq(ary, beg, len)
+rb_ary_subseq(ary, beg, len)
VALUE ary;
int beg, len;
{
@@ -344,10 +344,10 @@ ary_subseq(ary, beg, len)
if (beg < 0) beg = 0;
}
if (len < 0) {
- IndexError("negative length %d", RARRAY(ary)->len);
+ rb_raise(rb_eIndexError, "negative length %d", RARRAY(ary)->len);
}
if (len == 0) {
- return ary_new2(0);
+ return rb_ary_new2(0);
}
if (beg + len > RARRAY(ary)->len) {
len = RARRAY(ary)->len - beg;
@@ -356,7 +356,7 @@ ary_subseq(ary, beg, len)
len = 0;
}
- ary2 = ary_new2(len);
+ ary2 = rb_ary_new2(len);
MEMCPY(RARRAY(ary2)->ptr, RARRAY(ary)->ptr+beg, VALUE, len);
RARRAY(ary2)->len = len;
@@ -371,10 +371,10 @@ beg_len(range, begp, lenp, len)
{
int beg, end;
- if (!range_beg_end(range, &beg, &end)) return FALSE;
+ if (!rb_range_beg_end(range, &beg, &end)) return Qfalse;
if ((beg > 0 && end > 0 || beg < 0 && end < 0) && beg > end) {
- IndexError("end smaller than beg [%d..%d]", beg, end);
+ rb_raise(rb_eIndexError, "end smaller than beg [%d..%d]", beg, end);
}
if (beg < 0) {
@@ -397,11 +397,11 @@ beg_len(range, begp, lenp, len)
*lenp = end - beg +1;
}
}
- return TRUE;
+ return Qtrue;
}
VALUE
-ary_aref(argc, argv, ary)
+rb_ary_aref(argc, argv, ary)
int argc;
VALUE *argv;
VALUE ary;
@@ -413,27 +413,27 @@ ary_aref(argc, argv, ary)
beg = NUM2INT(arg1);
len = NUM2INT(arg2);
if (len <= 0) {
- return ary_new();
+ return rb_ary_new();
}
- return ary_subseq(ary, beg, len);
+ return rb_ary_subseq(ary, beg, len);
}
/* special case - speeding up */
if (FIXNUM_P(arg1)) {
- return ary_entry(ary, FIX2INT(arg1));
+ return rb_ary_entry(ary, FIX2INT(arg1));
}
else if (TYPE(arg1) == T_BIGNUM) {
- IndexError("index too big");
+ rb_raise(rb_eIndexError, "index too big");
}
else if (beg_len(arg1, &beg, &len, RARRAY(ary)->len)) {
/* check if idx is Range */
- return ary_subseq(ary, beg, len);
+ return rb_ary_subseq(ary, beg, len);
}
- return ary_entry(ary, NUM2INT(arg1));
+ return rb_ary_entry(ary, NUM2INT(arg1));
}
static VALUE
-ary_index(ary, val)
+rb_ary_index(ary, val)
VALUE ary;
VALUE val;
{
@@ -447,7 +447,7 @@ ary_index(ary, val)
}
static VALUE
-ary_rindex(ary, val)
+rb_ary_rindex(ary, val)
VALUE ary;
VALUE val;
{
@@ -461,7 +461,7 @@ ary_rindex(ary, val)
}
static VALUE
-ary_indexes(argc, argv, ary)
+rb_ary_indexes(argc, argv, ary)
int argc;
VALUE *argv;
VALUE ary;
@@ -469,20 +469,20 @@ ary_indexes(argc, argv, ary)
VALUE new_ary;
int i;
- new_ary = ary_new2(argc);
+ new_ary = rb_ary_new2(argc);
for (i=0; i<argc; i++) {
- ary_store(new_ary, i, ary_entry(ary, NUM2INT(argv[i])));
+ rb_ary_store(new_ary, i, rb_ary_entry(ary, NUM2INT(argv[i])));
}
return new_ary;
}
static void
-ary_replace(ary, beg, len, rpl)
+rb_ary_replace(ary, beg, len, rpl)
VALUE ary, rpl;
int beg, len;
{
- ary_modify(ary);
+ rb_ary_modify(ary);
if (TYPE(rpl) != T_ARRAY) {
rpl = rb_Array(rpl);
}
@@ -496,7 +496,7 @@ ary_replace(ary, beg, len, rpl)
RARRAY(ary)->capa=len;
REALLOC_N(RARRAY(ary)->ptr, VALUE, RARRAY(ary)->capa);
}
- memclear(RARRAY(ary)->ptr+RARRAY(ary)->len, beg-RARRAY(ary)->len);
+ rb_mem_clear(RARRAY(ary)->ptr+RARRAY(ary)->len, beg-RARRAY(ary)->len);
MEMCPY(RARRAY(ary)->ptr+beg, RARRAY(rpl)->ptr, VALUE, RARRAY(rpl)->len);
RARRAY(ary)->len = len;
}
@@ -507,7 +507,7 @@ ary_replace(ary, beg, len, rpl)
len = RARRAY(ary)->len - beg;
}
if (len < 0) {
- IndexError("negative length %d", RARRAY(ary)->len);
+ rb_raise(rb_eIndexError, "negative length %d", RARRAY(ary)->len);
}
alen = RARRAY(ary)->len + RARRAY(rpl)->len - len;
@@ -526,7 +526,7 @@ ary_replace(ary, beg, len, rpl)
}
static VALUE
-ary_aset(argc, argv, ary)
+rb_ary_aset(argc, argv, ary)
int argc;
VALUE *argv;
VALUE ary;
@@ -538,7 +538,7 @@ ary_aset(argc, argv, ary)
if (rb_scan_args(argc, argv, "21", &arg1, &arg2, &arg3) == 3) {
beg = NUM2INT(arg1);
len = NUM2INT(arg2);
- ary_replace(ary, beg, len, arg3);
+ rb_ary_replace(ary, beg, len, arg3);
return arg3;
}
else if (FIXNUM_P(arg1)) {
@@ -547,11 +547,11 @@ ary_aset(argc, argv, ary)
}
else if (beg_len(arg1, &beg, &len, RARRAY(ary)->len)) {
/* check if idx is Range */
- ary_replace(ary, beg, len, arg2);
+ rb_ary_replace(ary, beg, len, arg2);
return arg2;
}
if (TYPE(arg1) == T_BIGNUM) {
- IndexError("index too big");
+ rb_raise(rb_eIndexError, "index too big");
}
offset = NUM2INT(arg1);
@@ -559,12 +559,12 @@ ary_aset(argc, argv, ary)
if (offset < 0) {
offset = RARRAY(ary)->len + offset;
}
- ary_store(ary, offset, arg2);
+ rb_ary_store(ary, offset, arg2);
return arg2;
}
VALUE
-ary_each(ary)
+rb_ary_each(ary)
VALUE ary;
{
int i;
@@ -576,7 +576,7 @@ ary_each(ary)
}
static VALUE
-ary_each_index(ary)
+rb_ary_each_index(ary)
VALUE ary;
{
int i;
@@ -588,7 +588,7 @@ ary_each_index(ary)
}
static VALUE
-ary_reverse_each(ary)
+rb_ary_reverse_each(ary)
VALUE ary;
{
int len = RARRAY(ary)->len;
@@ -600,26 +600,26 @@ ary_reverse_each(ary)
}
static VALUE
-ary_length(ary)
+rb_ary_length(ary)
VALUE ary;
{
return INT2FIX(RARRAY(ary)->len);
}
static VALUE
-ary_empty_p(ary)
+rb_ary_empty_p(ary)
VALUE ary;
{
if (RARRAY(ary)->len == 0)
- return TRUE;
- return FALSE;
+ return Qtrue;
+ return Qfalse;
}
static VALUE
-ary_clone(ary)
+rb_ary_clone(ary)
VALUE ary;
{
- VALUE ary2 = ary_new2(RARRAY(ary)->len);
+ VALUE ary2 = rb_ary_new2(RARRAY(ary)->len);
CLONESETUP(ary2, ary);
MEMCPY(RARRAY(ary2)->ptr, RARRAY(ary)->ptr, VALUE, RARRAY(ary)->len);
@@ -628,10 +628,10 @@ ary_clone(ary)
}
static VALUE
-ary_dup(ary)
+rb_ary_dup(ary)
VALUE ary;
{
- return ary_new4(RARRAY(ary)->len, RARRAY(ary)->ptr);
+ return rb_ary_new4(RARRAY(ary)->len, RARRAY(ary)->ptr);
}
static VALUE
@@ -641,26 +641,26 @@ to_ary(ary)
return rb_convert_type(ary, T_ARRAY, "Array", "to_ary");
}
-extern VALUE OFS;
+extern VALUE rb_output_fs;
VALUE
-ary_join(ary, sep)
+rb_ary_join(ary, sep)
VALUE ary;
VALUE sep;
{
int i;
VALUE result, tmp;
- if (RARRAY(ary)->len == 0) return str_new(0, 0);
+ if (RARRAY(ary)->len == 0) return rb_str_new(0, 0);
switch (TYPE(RARRAY(ary)->ptr[0])) {
case T_STRING:
- result = str_dup(RARRAY(ary)->ptr[0]);
+ result = rb_str_dup(RARRAY(ary)->ptr[0]);
break;
case T_ARRAY:
- result = ary_join(RARRAY(ary)->ptr[0], sep);
+ result = rb_ary_join(RARRAY(ary)->ptr[0], sep);
break;
default:
- result = obj_as_string(RARRAY(ary)->ptr[0]);
+ result = rb_obj_as_string(RARRAY(ary)->ptr[0]);
break;
}
@@ -670,21 +670,21 @@ ary_join(ary, sep)
case T_STRING:
break;
case T_ARRAY:
- tmp = ary_join(tmp, sep);
+ tmp = rb_ary_join(tmp, sep);
break;
default:
- tmp = obj_as_string(tmp);
+ tmp = rb_obj_as_string(tmp);
}
- if (!NIL_P(sep)) str_concat(result, sep);
- str_cat(result, RSTRING(tmp)->ptr, RSTRING(tmp)->len);
- if (str_tainted(tmp)) str_taint(result);
+ if (!NIL_P(sep)) rb_str_concat(result, sep);
+ rb_str_cat(result, RSTRING(tmp)->ptr, RSTRING(tmp)->len);
+ if (rb_str_tainted(tmp)) rb_str_taint(result);
}
return result;
}
static VALUE
-ary_join_method(argc, argv, ary)
+rb_ary_join_method(argc, argv, ary)
int argc;
VALUE *argv;
VALUE ary;
@@ -692,51 +692,51 @@ ary_join_method(argc, argv, ary)
VALUE sep;
rb_scan_args(argc, argv, "01", &sep);
- if (NIL_P(sep)) sep = OFS;
+ if (NIL_P(sep)) sep = rb_output_fs;
- return ary_join(ary, sep);
+ return rb_ary_join(ary, sep);
}
VALUE
-ary_to_s(ary)
+rb_ary_to_s(ary)
VALUE ary;
{
- VALUE str = ary_join(ary, OFS);
- if (NIL_P(str)) return str_new(0, 0);
+ VALUE str = rb_ary_join(ary, rb_output_fs);
+ if (NIL_P(str)) return rb_str_new(0, 0);
return str;
}
static VALUE
-ary_inspect(ary)
+rb_ary_inspect(ary)
VALUE ary;
{
int i, len;
VALUE s, str;
- if (RARRAY(ary)->len == 0) return str_new2("[]");
- str = str_new2("[");
+ if (RARRAY(ary)->len == 0) return rb_str_new2("[]");
+ str = rb_str_new2("[");
len = 1;
for (i=0; i<RARRAY(ary)->len; i++) {
s = rb_inspect(RARRAY(ary)->ptr[i]);
- if (i > 0) str_cat(str, ", ", 2);
- str_cat(str, RSTRING(s)->ptr, RSTRING(s)->len);
+ if (i > 0) rb_str_cat(str, ", ", 2);
+ rb_str_cat(str, RSTRING(s)->ptr, RSTRING(s)->len);
len += RSTRING(s)->len + 2;
}
- str_cat(str, "]", 1);
+ rb_str_cat(str, "]", 1);
return str;
}
static VALUE
-ary_to_a(ary)
+rb_ary_to_a(ary)
VALUE ary;
{
return ary;
}
VALUE
-ary_reverse(ary)
+rb_ary_reverse(ary)
VALUE ary;
{
VALUE *p1, *p2;
@@ -758,10 +758,10 @@ ary_reverse(ary)
}
static VALUE
-ary_reverse_method(ary)
+rb_ary_reverse_method(ary)
VALUE ary;
{
- return ary_reverse(ary_dup(ary));
+ return rb_ary_reverse(rb_ary_dup(ary));
}
static ID cmp;
@@ -770,7 +770,7 @@ static int
sort_1(a, b)
VALUE *a, *b;
{
- VALUE retval = rb_yield(assoc_new(*a, *b));
+ VALUE retval = rb_yield(rb_assoc_new(*a, *b));
return NUM2INT(retval);
}
@@ -783,42 +783,59 @@ sort_2(a, b)
if (FIXNUM_P(*a)) {
if (FIXNUM_P(*b)) return *a - *b;
}
- else if (TYPE(*a) == T_STRING) {
- if (TYPE(*b) == T_STRING) return str_cmp(*a, *b);
+ else if (TYPE(*a) == T_STRING && TYPE(*b) == T_STRING) {
+ return rb_str_cmp(*a, *b);
}
retval = rb_funcall(*a, cmp, 1, *b);
return NUM2INT(retval);
}
+static VALUE
+sort_internal(ary)
+ VALUE ary;
+{
+ qsort(RARRAY(ary)->ptr, RARRAY(ary)->len, sizeof(VALUE),
+ rb_iterator_p()?sort_1:sort_2);
+ return ary;
+}
+
+static VALUE
+sort_unlock(ary)
+ VALUE ary;
+{
+ FL_UNSET(ary, ARY_TMPLOCK);
+ return ary;
+}
+
VALUE
-ary_sort_bang(ary)
+rb_ary_sort_bang(ary)
VALUE ary;
{
if (RARRAY(ary)->len == 0) return ary;
- ary_modify(ary);
- qsort(RARRAY(ary)->ptr, RARRAY(ary)->len, sizeof(VALUE),
- iterator_p()?sort_1:sort_2);
+ rb_ary_modify(ary);
+ FL_SET(ary, ARY_TMPLOCK); /* prohibit modification during sort */
+ rb_ensure(sort_internal, ary, sort_unlock, ary);
return ary;
}
VALUE
-ary_sort(ary)
+rb_ary_sort(ary)
VALUE ary;
{
if (RARRAY(ary)->len == 0) return ary;
- return ary_sort_bang(ary_dup(ary));
+ return rb_ary_sort_bang(rb_ary_dup(ary));
}
VALUE
-ary_delete(ary, item)
+rb_ary_delete(ary, item)
VALUE ary;
VALUE item;
{
int i1, i2;
- ary_modify(ary);
+ rb_ary_modify(ary);
for (i1 = i2 = 0; i1 < RARRAY(ary)->len; i1++) {
if (rb_equal(RARRAY(ary)->ptr[i1], item)) continue;
if (i1 != i2) {
@@ -827,7 +844,7 @@ ary_delete(ary, item)
i2++;
}
if (RARRAY(ary)->len == i2) {
- if (iterator_p()) {
+ if (rb_iterator_p()) {
return rb_yield(item);
}
return Qnil;
@@ -840,14 +857,14 @@ ary_delete(ary, item)
}
VALUE
-ary_delete_at(ary, at)
+rb_ary_delete_at(ary, at)
VALUE ary;
VALUE at;
{
int i1, i2, pos;
VALUE del = Qnil;
- ary_modify(ary);
+ rb_ary_modify(ary);
pos = NUM2INT(at);
for (i1 = i2 = 0; i1 < RARRAY(ary)->len; i1++) {
if (i1 == pos) {
@@ -865,12 +882,12 @@ ary_delete_at(ary, at)
}
static VALUE
-ary_delete_if(ary)
+rb_ary_delete_if(ary)
VALUE ary;
{
int i1, i2;
- ary_modify(ary);
+ rb_ary_modify(ary);
for (i1 = i2 = 0; i1 < RARRAY(ary)->len; i1++) {
if (rb_yield(RARRAY(ary)->ptr[i1])) continue;
if (i1 != i2) {
@@ -884,12 +901,12 @@ ary_delete_if(ary)
}
static VALUE
-ary_filter(ary)
+rb_ary_filter(ary)
VALUE ary;
{
int i;
- ary_modify(ary);
+ rb_ary_modify(ary);
for (i = 0; i < RARRAY(ary)->len; i++) {
RARRAY(ary)->ptr[i] = rb_yield(RARRAY(ary)->ptr[i]);
}
@@ -897,16 +914,16 @@ ary_filter(ary)
}
static VALUE
-ary_replace_method(ary, ary2)
+rb_ary_replace_method(ary, ary2)
VALUE ary, ary2;
{
ary2 = to_ary(ary2);
- ary_replace(ary, 0, RARRAY(ary2)->len, ary2);
+ rb_ary_replace(ary, 0, RARRAY(ary2)->len, ary2);
return ary;
}
static VALUE
-ary_clear(ary)
+rb_ary_clear(ary)
VALUE ary;
{
RARRAY(ary)->len = 0;
@@ -918,7 +935,7 @@ ary_clear(ary)
}
static VALUE
-ary_fill(argc, argv, ary)
+rb_ary_fill(argc, argv, ary)
int argc;
VALUE *argv;
VALUE ary;
@@ -939,6 +956,7 @@ ary_fill(argc, argv, ary)
}
len = NIL_P(arg2)?RARRAY(ary)->len - beg:NUM2INT(arg2);
}
+ rb_ary_modify(ary);
end = beg + len;
if (end > RARRAY(ary)->len) {
if (end >= RARRAY(ary)->capa) {
@@ -946,7 +964,7 @@ ary_fill(argc, argv, ary)
REALLOC_N(RARRAY(ary)->ptr, VALUE, RARRAY(ary)->capa);
}
if (beg > RARRAY(ary)->len) {
- memclear(RARRAY(ary)->ptr+RARRAY(ary)->len,end-RARRAY(ary)->len);
+ rb_mem_clear(RARRAY(ary)->ptr+RARRAY(ary)->len,end-RARRAY(ary)->len);
}
RARRAY(ary)->len = end;
}
@@ -959,16 +977,16 @@ ary_fill(argc, argv, ary)
}
VALUE
-ary_plus(x, y)
+rb_ary_plus(x, y)
VALUE x, y;
{
VALUE z;
if (TYPE(y) != T_ARRAY) {
- return ary_plus(x, rb_Array(y));
+ return rb_ary_plus(x, rb_Array(y));
}
- z = ary_new2(RARRAY(x)->len + RARRAY(y)->len);
+ z = rb_ary_new2(RARRAY(x)->len + RARRAY(y)->len);
MEMCPY(RARRAY(z)->ptr, RARRAY(x)->ptr, VALUE, RARRAY(x)->len);
MEMCPY(RARRAY(z)->ptr+RARRAY(x)->len, RARRAY(y)->ptr, VALUE, RARRAY(y)->len);
RARRAY(z)->len = RARRAY(x)->len + RARRAY(y)->len;
@@ -976,26 +994,26 @@ ary_plus(x, y)
}
VALUE
-ary_concat(x, y)
+rb_ary_concat(x, y)
VALUE x, y;
{
VALUE *p, *pend;
if (TYPE(y) != T_ARRAY) {
- return ary_concat(x, rb_Array(y));
+ return rb_ary_concat(x, rb_Array(y));
}
p = RARRAY(y)->ptr;
pend = p + RARRAY(y)->len;
while (p < pend) {
- ary_store(x, RARRAY(x)->len, *p);
+ rb_ary_store(x, RARRAY(x)->len, *p);
p++;
}
return x;
}
static VALUE
-ary_times(ary, times)
+rb_ary_times(ary, times)
VALUE ary;
VALUE times;
{
@@ -1003,16 +1021,16 @@ ary_times(ary, times)
int i, len;
if (TYPE(times) == T_STRING) {
- return ary_join(ary, times);
+ return rb_ary_join(ary, times);
}
len = NUM2INT(times);
if (len < 0) {
- ArgError("negative argument");
+ rb_raise(rb_eArgError, "negative argument");
}
len *= RARRAY(ary)->len;
- ary2 = ary_new2(len);
+ ary2 = rb_ary_new2(len);
RARRAY(ary2)->len = len;
for (i=0; i<len; i+=RARRAY(ary)->len) {
@@ -1023,7 +1041,7 @@ ary_times(ary, times)
}
VALUE
-ary_assoc(ary, key)
+rb_ary_assoc(ary, key)
VALUE ary;
VALUE key;
{
@@ -1041,7 +1059,7 @@ ary_assoc(ary, key)
}
VALUE
-ary_rassoc(ary, value)
+rb_ary_rassoc(ary, value)
VALUE ary;
VALUE value;
{
@@ -1059,65 +1077,66 @@ ary_rassoc(ary, value)
}
static VALUE
-ary_equal(ary1, ary2)
+rb_ary_equal(ary1, ary2)
VALUE ary1, ary2;
{
int i;
- if (TYPE(ary2) != T_ARRAY) return FALSE;
- if (RARRAY(ary1)->len != RARRAY(ary2)->len) return FALSE;
+ if (TYPE(ary2) != T_ARRAY) return Qfalse;
+ if (RARRAY(ary1)->len != RARRAY(ary2)->len) return Qfalse;
for (i=0; i<RARRAY(ary1)->len; i++) {
if (!rb_equal(RARRAY(ary1)->ptr[i], RARRAY(ary2)->ptr[i]))
- return FALSE;
+ return Qfalse;
}
- return TRUE;
+ return Qtrue;
}
static VALUE
-ary_eql(ary1, ary2)
+rb_ary_eql(ary1, ary2)
VALUE ary1, ary2;
{
int i;
- if (TYPE(ary2) != T_ARRAY) return FALSE;
+ if (TYPE(ary2) != T_ARRAY) return Qfalse;
if (RARRAY(ary1)->len != RARRAY(ary2)->len)
- return FALSE;
+ return Qfalse;
for (i=0; i<RARRAY(ary1)->len; i++) {
if (!rb_eql(RARRAY(ary1)->ptr[i], RARRAY(ary2)->ptr[i]))
- return FALSE;
+ return Qfalse;
}
- return TRUE;
+ return Qtrue;
}
static VALUE
-ary_hash(ary)
+rb_ary_hash(ary)
VALUE ary;
{
int h, i;
h = RARRAY(ary)->len;
for (i=0; i<RARRAY(ary)->len; i++) {
- h ^= rb_hash(RARRAY(ary)->ptr[i]);
+ int n = rb_hash(RARRAY(ary)->ptr[i]);
+ h ^= NUM2LONG(n);
}
return INT2FIX(h);
}
VALUE
-ary_includes(ary, item)
+rb_ary_includes(ary, item)
VALUE ary;
VALUE item;
{
int i;
for (i=0; i<RARRAY(ary)->len; i++) {
if (rb_equal(RARRAY(ary)->ptr[i], item)) {
- return TRUE;
+ return Qtrue;
}
}
- return FALSE;
+ return Qfalse;
}
-VALUE
-ary_cmp(ary, ary2)
+static VALUE
+rb_ary_cmp(ary, ary2)
VALUE ary;
VALUE ary2;
{
@@ -1141,72 +1160,72 @@ ary_cmp(ary, ary2)
}
static VALUE
-ary_diff(ary1, ary2)
+rb_ary_diff(ary1, ary2)
VALUE ary1, ary2;
{
VALUE ary3;
int i;
ary2 = to_ary(ary2);
- ary3 = ary_new();
+ ary3 = rb_ary_new();
for (i=0; i<RARRAY(ary1)->len; i++) {
- if (ary_includes(ary2, RARRAY(ary1)->ptr[i])) continue;
- if (ary_includes(ary3, RARRAY(ary1)->ptr[i])) continue;
- ary_push(ary3, RARRAY(ary1)->ptr[i]);
+ if (rb_ary_includes(ary2, RARRAY(ary1)->ptr[i])) continue;
+ if (rb_ary_includes(ary3, RARRAY(ary1)->ptr[i])) continue;
+ rb_ary_push(ary3, RARRAY(ary1)->ptr[i]);
}
return ary3;
}
static VALUE
-ary_and(ary1, ary2)
+rb_ary_and(ary1, ary2)
VALUE ary1, ary2;
{
VALUE ary3;
int i;
ary2 = to_ary(ary2);
- ary3 = ary_new();
+ ary3 = rb_ary_new();
for (i=0; i<RARRAY(ary1)->len; i++) {
- if (ary_includes(ary2, RARRAY(ary1)->ptr[i])
- && !ary_includes(ary3, RARRAY(ary1)->ptr[i])) {
- ary_push(ary3, RARRAY(ary1)->ptr[i]);
+ if (rb_ary_includes(ary2, RARRAY(ary1)->ptr[i])
+ && !rb_ary_includes(ary3, RARRAY(ary1)->ptr[i])) {
+ rb_ary_push(ary3, RARRAY(ary1)->ptr[i]);
}
}
return ary3;
}
static VALUE
-ary_or(ary1, ary2)
+rb_ary_or(ary1, ary2)
VALUE ary1, ary2;
{
VALUE ary3;
int i;
if (TYPE(ary2) != T_ARRAY) {
- if (ary_includes(ary1, ary2)) return ary1;
- else return ary_plus(ary1, ary2);
+ if (rb_ary_includes(ary1, ary2)) return ary1;
+ else return rb_ary_plus(ary1, ary2);
}
- ary3 = ary_new();
+ ary3 = rb_ary_new();
for (i=0; i<RARRAY(ary1)->len; i++) {
- if (!ary_includes(ary3, RARRAY(ary1)->ptr[i]))
- ary_push(ary3, RARRAY(ary1)->ptr[i]);
+ if (!rb_ary_includes(ary3, RARRAY(ary1)->ptr[i]))
+ rb_ary_push(ary3, RARRAY(ary1)->ptr[i]);
}
for (i=0; i<RARRAY(ary2)->len; i++) {
- if (!ary_includes(ary3, RARRAY(ary2)->ptr[i]))
- ary_push(ary3, RARRAY(ary2)->ptr[i]);
+ if (!rb_ary_includes(ary3, RARRAY(ary2)->ptr[i]))
+ rb_ary_push(ary3, RARRAY(ary2)->ptr[i]);
}
return ary3;
}
static VALUE
-ary_uniq_bang(ary)
+rb_ary_uniq_bang(ary)
VALUE ary;
{
VALUE *p, *q, *t, *end;
VALUE v;
- ary_modify(ary);
+ rb_ary_modify(ary);
p = RARRAY(ary)->ptr;
end = p + RARRAY(ary)->len;
@@ -1229,22 +1248,22 @@ ary_uniq_bang(ary)
}
static VALUE
-ary_uniq(ary)
+rb_ary_uniq(ary)
VALUE ary;
{
- VALUE v = ary_uniq_bang(ary_dup(ary));
+ VALUE v = rb_ary_uniq_bang(rb_ary_dup(ary));
if (NIL_P(v)) return ary;
return v;
}
static VALUE
-ary_compact_bang(ary)
+rb_ary_compact_bang(ary)
VALUE ary;
{
VALUE *p, *t, *end;
- ary_modify(ary);
+ rb_ary_modify(ary);
p = t = RARRAY(ary)->ptr;
end = p + RARRAY(ary)->len;
while (t < end) {
@@ -1261,17 +1280,17 @@ ary_compact_bang(ary)
}
static VALUE
-ary_compact(ary)
+rb_ary_compact(ary)
VALUE ary;
{
- VALUE v = ary_compact_bang(ary_dup(ary));
+ VALUE v = rb_ary_compact_bang(rb_ary_dup(ary));
if (NIL_P(v)) return ary;
return v;
}
static VALUE
-ary_nitems(ary)
+rb_ary_nitems(ary)
VALUE ary;
{
int n = 0;
@@ -1287,17 +1306,17 @@ ary_nitems(ary)
}
static VALUE
-ary_flatten_bang(ary)
+rb_ary_flatten_bang(ary)
VALUE ary;
{
int i;
int mod = 0;
- ary_modify(ary);
+ rb_ary_modify(ary);
for (i=0; i<RARRAY(ary)->len; i++) {
VALUE ary2 = RARRAY(ary)->ptr[i];
if (TYPE(ary2) == T_ARRAY) {
- ary_replace(ary, i--, 1, ary2);
+ rb_ary_replace(ary, i--, 1, ary2);
mod = 1;
}
}
@@ -1306,10 +1325,10 @@ ary_flatten_bang(ary)
}
static VALUE
-ary_flatten(ary)
+rb_ary_flatten(ary)
VALUE ary;
{
- VALUE v = ary_flatten_bang(ary_dup(ary));
+ VALUE v = rb_ary_flatten_bang(rb_ary_dup(ary));
if (NIL_P(v)) return ary;
return v;
@@ -1318,76 +1337,76 @@ ary_flatten(ary)
void
Init_Array()
{
- cArray = rb_define_class("Array", cObject);
- rb_include_module(cArray, mEnumerable);
-
- rb_define_singleton_method(cArray, "new", ary_s_new, -1);
- rb_define_singleton_method(cArray, "[]", ary_s_create, -1);
- rb_define_method(cArray, "to_s", ary_to_s, 0);
- rb_define_method(cArray, "inspect", ary_inspect, 0);
- rb_define_method(cArray, "to_a", ary_to_a, 0);
- rb_define_method(cArray, "to_ary", ary_to_a, 0);
-
- rb_define_method(cArray, "freeze", ary_freeze, 0);
- rb_define_method(cArray, "frozen?", ary_frozen_p, 0);
-
- rb_define_method(cArray, "==", ary_equal, 1);
- rb_define_method(cArray, "eql?", ary_eql, 1);
- rb_define_method(cArray, "hash", ary_hash, 0);
-
- rb_define_method(cArray, "[]", ary_aref, -1);
- rb_define_method(cArray, "[]=", ary_aset, -1);
- rb_define_method(cArray, "concat", ary_concat, 1);
- rb_define_method(cArray, "<<", ary_push, 1);
- rb_define_method(cArray, "push", ary_push_method, -1);
- rb_define_method(cArray, "pop", ary_pop, 0);
- rb_define_method(cArray, "shift", ary_shift, 0);
- rb_define_method(cArray, "unshift", ary_unshift, 1);
- rb_define_method(cArray, "each", ary_each, 0);
- rb_define_method(cArray, "each_index", ary_each_index, 0);
- rb_define_method(cArray, "reverse_each", ary_reverse_each, 0);
- rb_define_method(cArray, "length", ary_length, 0);
- rb_define_alias(cArray, "size", "length");
- rb_define_method(cArray, "empty?", ary_empty_p, 0);
- rb_define_method(cArray, "index", ary_index, 1);
- rb_define_method(cArray, "rindex", ary_rindex, 1);
- rb_define_method(cArray, "indexes", ary_indexes, -1);
- rb_define_method(cArray, "indices", ary_indexes, -1);
- rb_define_method(cArray, "clone", ary_clone, 0);
- rb_define_method(cArray, "dup", ary_dup, 0);
- rb_define_method(cArray, "join", ary_join_method, -1);
- rb_define_method(cArray, "reverse", ary_reverse_method, 0);
- rb_define_method(cArray, "reverse!", ary_reverse, 0);
- rb_define_method(cArray, "sort", ary_sort, 0);
- rb_define_method(cArray, "sort!", ary_sort_bang, 0);
- rb_define_method(cArray, "delete", ary_delete, 1);
- rb_define_method(cArray, "delete_at", ary_delete_at, 1);
- rb_define_method(cArray, "delete_if", ary_delete_if, 0);
- rb_define_method(cArray, "filter", ary_filter, 0);
- rb_define_method(cArray, "replace", ary_replace_method, 1);
- rb_define_method(cArray, "clear", ary_clear, 0);
- rb_define_method(cArray, "fill", ary_fill, -1);
- rb_define_method(cArray, "include?", ary_includes, 1);
- rb_define_method(cArray, "===", ary_includes, 1);
- rb_define_method(cArray, "<=>", ary_cmp, 1);
-
- rb_define_method(cArray, "assoc", ary_assoc, 1);
- rb_define_method(cArray, "rassoc", ary_rassoc, 1);
-
- rb_define_method(cArray, "+", ary_plus, 1);
- rb_define_method(cArray, "*", ary_times, 1);
-
- rb_define_method(cArray, "-", ary_diff, 1);
- rb_define_method(cArray, "&", ary_and, 1);
- rb_define_method(cArray, "|", ary_or, 1);
-
- rb_define_method(cArray, "uniq", ary_uniq, 0);
- rb_define_method(cArray, "uniq!", ary_uniq_bang, 0);
- rb_define_method(cArray, "compact", ary_compact, 0);
- rb_define_method(cArray, "compact!", ary_compact_bang, 0);
- rb_define_method(cArray, "flatten", ary_flatten, 0);
- rb_define_method(cArray, "flatten!", ary_flatten_bang, 0);
- rb_define_method(cArray, "nitems", ary_nitems, 0);
+ rb_cArray = rb_define_class("Array", rb_cObject);
+ rb_include_module(rb_cArray, rb_mEnumerable);
+
+ rb_define_singleton_method(rb_cArray, "new", rb_ary_s_new, -1);
+ rb_define_singleton_method(rb_cArray, "[]", rb_ary_s_create, -1);
+ rb_define_method(rb_cArray, "to_s", rb_ary_to_s, 0);
+ rb_define_method(rb_cArray, "inspect", rb_ary_inspect, 0);
+ rb_define_method(rb_cArray, "to_a", rb_ary_to_a, 0);
+ rb_define_method(rb_cArray, "to_ary", rb_ary_to_a, 0);
+
+ rb_define_method(rb_cArray, "freeze", rb_ary_freeze, 0);
+ rb_define_method(rb_cArray, "frozen?", rb_ary_frozen_p, 0);
+
+ rb_define_method(rb_cArray, "==", rb_ary_equal, 1);
+ rb_define_method(rb_cArray, "eql?", rb_ary_eql, 1);
+ rb_define_method(rb_cArray, "hash", rb_ary_hash, 0);
+
+ rb_define_method(rb_cArray, "[]", rb_ary_aref, -1);
+ rb_define_method(rb_cArray, "[]=", rb_ary_aset, -1);
+ rb_define_method(rb_cArray, "concat", rb_ary_concat, 1);
+ rb_define_method(rb_cArray, "<<", rb_ary_push, 1);
+ rb_define_method(rb_cArray, "push", rb_ary_push_method, -1);
+ rb_define_method(rb_cArray, "pop", rb_ary_pop, 0);
+ rb_define_method(rb_cArray, "shift", rb_ary_shift, 0);
+ rb_define_method(rb_cArray, "unshift", rb_ary_unshift, 1);
+ rb_define_method(rb_cArray, "each", rb_ary_each, 0);
+ rb_define_method(rb_cArray, "each_index", rb_ary_each_index, 0);
+ rb_define_method(rb_cArray, "reverse_each", rb_ary_reverse_each, 0);
+ rb_define_method(rb_cArray, "length", rb_ary_length, 0);
+ rb_define_alias(rb_cArray, "size", "length");
+ rb_define_method(rb_cArray, "empty?", rb_ary_empty_p, 0);
+ rb_define_method(rb_cArray, "index", rb_ary_index, 1);
+ rb_define_method(rb_cArray, "rindex", rb_ary_rindex, 1);
+ rb_define_method(rb_cArray, "indexes", rb_ary_indexes, -1);
+ rb_define_method(rb_cArray, "indices", rb_ary_indexes, -1);
+ rb_define_method(rb_cArray, "clone", rb_ary_clone, 0);
+ rb_define_method(rb_cArray, "dup", rb_ary_dup, 0);
+ rb_define_method(rb_cArray, "join", rb_ary_join_method, -1);
+ rb_define_method(rb_cArray, "reverse", rb_ary_reverse_method, 0);
+ rb_define_method(rb_cArray, "reverse!", rb_ary_reverse, 0);
+ rb_define_method(rb_cArray, "sort", rb_ary_sort, 0);
+ rb_define_method(rb_cArray, "sort!", rb_ary_sort_bang, 0);
+ rb_define_method(rb_cArray, "delete", rb_ary_delete, 1);
+ rb_define_method(rb_cArray, "delete_at", rb_ary_delete_at, 1);
+ rb_define_method(rb_cArray, "delete_if", rb_ary_delete_if, 0);
+ rb_define_method(rb_cArray, "filter", rb_ary_filter, 0);
+ rb_define_method(rb_cArray, "replace", rb_ary_replace_method, 1);
+ rb_define_method(rb_cArray, "clear", rb_ary_clear, 0);
+ rb_define_method(rb_cArray, "fill", rb_ary_fill, -1);
+ rb_define_method(rb_cArray, "include?", rb_ary_includes, 1);
+ rb_define_method(rb_cArray, "===", rb_ary_includes, 1);
+ rb_define_method(rb_cArray, "<=>", rb_ary_cmp, 1);
+
+ rb_define_method(rb_cArray, "assoc", rb_ary_assoc, 1);
+ rb_define_method(rb_cArray, "rassoc", rb_ary_rassoc, 1);
+
+ rb_define_method(rb_cArray, "+", rb_ary_plus, 1);
+ rb_define_method(rb_cArray, "*", rb_ary_times, 1);
+
+ rb_define_method(rb_cArray, "-", rb_ary_diff, 1);
+ rb_define_method(rb_cArray, "&", rb_ary_and, 1);
+ rb_define_method(rb_cArray, "|", rb_ary_or, 1);
+
+ rb_define_method(rb_cArray, "uniq", rb_ary_uniq, 0);
+ rb_define_method(rb_cArray, "uniq!", rb_ary_uniq_bang, 0);
+ rb_define_method(rb_cArray, "compact", rb_ary_compact, 0);
+ rb_define_method(rb_cArray, "compact!", rb_ary_compact_bang, 0);
+ rb_define_method(rb_cArray, "flatten", rb_ary_flatten, 0);
+ rb_define_method(rb_cArray, "flatten!", rb_ary_flatten_bang, 0);
+ rb_define_method(rb_cArray, "nitems", rb_ary_nitems, 0);
cmp = rb_intern("<=>");
}