1998-01-16 07:13:05 -05:00
|
|
|
/************************************************
|
|
|
|
|
|
|
|
array.c -
|
|
|
|
|
|
|
|
$Author$
|
|
|
|
$Date$
|
|
|
|
created at: Fri Aug 6 09:46:12 JST 1993
|
|
|
|
|
2000-01-04 23:41:21 -05:00
|
|
|
Copyright (C) 1993-2000 Yukihiro Matsumoto
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
************************************************/
|
|
|
|
|
|
|
|
#include "ruby.h"
|
1999-01-19 23:59:39 -05:00
|
|
|
#include "util.h"
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE rb_cArray;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
#define ARY_DEFAULT_SIZE 16
|
|
|
|
|
|
|
|
void
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_mem_clear(mem, size)
|
1998-01-16 07:13:05 -05:00
|
|
|
register VALUE *mem;
|
1999-01-19 23:59:39 -05:00
|
|
|
register size_t size;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
while (size--) {
|
|
|
|
*mem++ = Qnil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
static void
|
|
|
|
memfill(mem, size, val)
|
|
|
|
register VALUE *mem;
|
|
|
|
register size_t size;
|
|
|
|
register VALUE val;
|
|
|
|
{
|
|
|
|
while (size--) {
|
|
|
|
*mem++ = val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-01-31 22:12:21 -05:00
|
|
|
#define ARY_TMPLOCK FL_USER1
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
static void
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_modify(ary)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE ary;
|
|
|
|
{
|
2000-01-31 22:12:21 -05:00
|
|
|
if (OBJ_FROZEN(ary)) rb_error_frozen("array");
|
1999-01-19 23:59:39 -05:00
|
|
|
if (FL_TEST(ary, ARY_TMPLOCK))
|
|
|
|
rb_raise(rb_eTypeError, "can't modify array during sort");
|
1999-12-14 01:50:43 -05:00
|
|
|
if (!OBJ_TAINTED(ary) && rb_safe_level() >= 4)
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_raise(rb_eSecurityError, "Insecure: can't modify array");
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_frozen_p(ary)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE ary;
|
|
|
|
{
|
2000-01-31 22:12:21 -05:00
|
|
|
if (FL_TEST(ary, FL_FREEZE|ARY_TMPLOCK))
|
1999-01-19 23:59:39 -05:00
|
|
|
return Qtrue;
|
|
|
|
return Qfalse;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_new2(len)
|
1999-08-13 01:45:20 -04:00
|
|
|
long len;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
NEWOBJ(ary, struct RArray);
|
1999-01-19 23:59:39 -05:00
|
|
|
OBJSETUP(ary, rb_cArray, T_ARRAY);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
if (len < 0) {
|
|
|
|
rb_raise(rb_eArgError, "negative array size (or size too big)");
|
|
|
|
}
|
|
|
|
if (len > 0 && len*sizeof(VALUE) <= 0) {
|
|
|
|
rb_raise(rb_eArgError, "array size too big");
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
ary->len = 0;
|
|
|
|
ary->capa = len;
|
1999-01-19 23:59:39 -05:00
|
|
|
ary->ptr = 0;
|
1999-08-13 01:45:20 -04:00
|
|
|
if (len == 0) len++;
|
1999-01-19 23:59:39 -05:00
|
|
|
ary->ptr = ALLOC_N(VALUE, len);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
return (VALUE)ary;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_new()
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_ary_new2(ARY_DEFAULT_SIZE);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
#ifdef HAVE_STDARG_PROTOTYPES
|
|
|
|
#include <stdarg.h>
|
|
|
|
#define va_init_list(a,b) va_start(a,b)
|
|
|
|
#else
|
1998-01-16 07:13:05 -05:00
|
|
|
#include <varargs.h>
|
1999-01-19 23:59:39 -05:00
|
|
|
#define va_init_list(a,b) va_start(a)
|
|
|
|
#endif
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
#ifdef HAVE_STDARG_PROTOTYPES
|
1999-08-13 01:45:20 -04:00
|
|
|
rb_ary_new3(long n, ...)
|
1999-01-19 23:59:39 -05:00
|
|
|
#else
|
|
|
|
rb_ary_new3(n, va_alist)
|
1999-08-13 01:45:20 -04:00
|
|
|
long n;
|
1998-01-16 07:13:05 -05:00
|
|
|
va_dcl
|
1999-01-19 23:59:39 -05:00
|
|
|
#endif
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
va_list ar;
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE ary;
|
1999-08-13 01:45:20 -04:00
|
|
|
long i;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
if (n < 0) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_raise(rb_eIndexError, "negative number of items(%d)", n);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
ary = rb_ary_new2(n<ARY_DEFAULT_SIZE?ARY_DEFAULT_SIZE:n);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
va_init_list(ar, n);
|
1998-01-16 07:13:05 -05:00
|
|
|
for (i=0; i<n; i++) {
|
1998-01-16 07:19:22 -05:00
|
|
|
RARRAY(ary)->ptr[i] = va_arg(ar, VALUE);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
va_end(ar);
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
RARRAY(ary)->len = n;
|
|
|
|
return ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_new4(n, elts)
|
1999-08-13 01:45:20 -04:00
|
|
|
long n;
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE *elts;
|
|
|
|
{
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
ary = rb_ary_new2(n);
|
|
|
|
if (elts) {
|
|
|
|
MEMCPY(RARRAY(ary)->ptr, elts, VALUE, n);
|
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
RARRAY(ary)->len = n;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
return ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_assoc_new(car, cdr)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE car, cdr;
|
|
|
|
{
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
ary = rb_ary_new2(2);
|
1998-01-16 07:19:22 -05:00
|
|
|
RARRAY(ary)->ptr[0] = car;
|
|
|
|
RARRAY(ary)->ptr[1] = cdr;
|
|
|
|
RARRAY(ary)->len = 2;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
return ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_s_new(argc, argv, klass)
|
1998-01-16 07:13:05 -05:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE klass;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
long len = 0;
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE size, val;
|
1998-01-16 07:13:05 -05:00
|
|
|
NEWOBJ(ary, struct RArray);
|
1999-01-19 23:59:39 -05:00
|
|
|
OBJSETUP(ary, klass, T_ARRAY);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
ary->len = 0;
|
1999-01-19 23:59:39 -05:00
|
|
|
ary->ptr = 0;
|
|
|
|
if (rb_scan_args(argc, argv, "02", &size, &val) == 0) {
|
|
|
|
ary->capa = ARY_DEFAULT_SIZE;
|
|
|
|
}
|
|
|
|
else {
|
1999-08-13 01:45:20 -04:00
|
|
|
long capa = NUM2LONG(size);
|
1999-01-19 23:59:39 -05:00
|
|
|
|
|
|
|
if (capa < 0) {
|
|
|
|
rb_raise(rb_eArgError, "negative array size");
|
|
|
|
}
|
|
|
|
if (capa > 0 && capa*sizeof(VALUE) <= 0) {
|
|
|
|
rb_raise(rb_eArgError, "array size too big");
|
|
|
|
}
|
|
|
|
ary->capa = capa;
|
|
|
|
len = capa;
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
ary->ptr = ALLOC_N(VALUE, ary->capa);
|
1999-01-19 23:59:39 -05:00
|
|
|
memfill(ary->ptr, len, val);
|
|
|
|
ary->len = len;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
return (VALUE)ary;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_s_create(argc, argv, klass)
|
1998-01-16 07:13:05 -05:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE klass;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
NEWOBJ(ary, struct RArray);
|
1999-01-19 23:59:39 -05:00
|
|
|
OBJSETUP(ary, klass, T_ARRAY);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
ary->len = ary->capa = 0;
|
1998-01-16 07:13:05 -05:00
|
|
|
if (argc == 0) {
|
|
|
|
ary->ptr = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ary->ptr = ALLOC_N(VALUE, argc);
|
|
|
|
MEMCPY(ary->ptr, argv, VALUE, argc);
|
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
ary->len = ary->capa = argc;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
return (VALUE)ary;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_store(ary, idx, val)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE ary;
|
1999-08-13 01:45:20 -04:00
|
|
|
long idx;
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE val;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_modify(ary);
|
1998-01-16 07:13:05 -05:00
|
|
|
if (idx < 0) {
|
1999-08-13 01:45:20 -04:00
|
|
|
idx += RARRAY(ary)->len;
|
1999-01-19 23:59:39 -05:00
|
|
|
if (idx < 0) {
|
1999-08-13 01:45:20 -04:00
|
|
|
rb_raise(rb_eIndexError, "index %d out of array",
|
|
|
|
idx - RARRAY(ary)->len);
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
if (idx >= RARRAY(ary)->capa) {
|
1999-08-13 01:45:20 -04:00
|
|
|
long capa_inc = RARRAY(ary)->capa / 2;
|
|
|
|
if (capa_inc < ARY_DEFAULT_SIZE) {
|
|
|
|
capa_inc = ARY_DEFAULT_SIZE;
|
|
|
|
}
|
|
|
|
RARRAY(ary)->capa = idx + capa_inc;
|
1998-01-16 07:19:22 -05:00
|
|
|
REALLOC_N(RARRAY(ary)->ptr, VALUE, RARRAY(ary)->capa);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
if (idx > RARRAY(ary)->len) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_mem_clear(RARRAY(ary)->ptr+RARRAY(ary)->len,
|
|
|
|
idx-RARRAY(ary)->len+1);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
if (idx >= RARRAY(ary)->len) {
|
|
|
|
RARRAY(ary)->len = idx + 1;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
RARRAY(ary)->ptr[idx] = val;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_push(ary, item)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE item;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_store(ary, RARRAY(ary)->len, item);
|
1998-01-16 07:19:22 -05:00
|
|
|
return ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2000-01-04 23:41:21 -05:00
|
|
|
rb_ary_push_m(argc, argv, ary)
|
1998-01-16 07:13:05 -05:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
while (argc--) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_store(ary, RARRAY(ary)->len, *argv++);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
return ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_pop(ary)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-10-18 05:03:16 -04:00
|
|
|
rb_ary_modify(ary);
|
1998-01-16 07:19:22 -05:00
|
|
|
if (RARRAY(ary)->len == 0) return Qnil;
|
|
|
|
if (RARRAY(ary)->len * 10 < RARRAY(ary)->capa && RARRAY(ary)->capa > ARY_DEFAULT_SIZE) {
|
|
|
|
RARRAY(ary)->capa = RARRAY(ary)->len * 2;
|
|
|
|
REALLOC_N(RARRAY(ary)->ptr, VALUE, RARRAY(ary)->capa);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
return RARRAY(ary)->ptr[--RARRAY(ary)->len];
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_shift(ary)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
VALUE top;
|
|
|
|
|
1999-10-18 05:03:16 -04:00
|
|
|
rb_ary_modify(ary);
|
1998-01-16 07:19:22 -05:00
|
|
|
if (RARRAY(ary)->len == 0) return Qnil;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
top = RARRAY(ary)->ptr[0];
|
|
|
|
RARRAY(ary)->len--;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
/* sliding items */
|
1998-01-16 07:19:22 -05:00
|
|
|
MEMMOVE(RARRAY(ary)->ptr, RARRAY(ary)->ptr+1, VALUE, RARRAY(ary)->len);
|
|
|
|
if (RARRAY(ary)->len * 10 < RARRAY(ary)->capa && RARRAY(ary)->capa > ARY_DEFAULT_SIZE) {
|
|
|
|
RARRAY(ary)->capa = RARRAY(ary)->len * 2;
|
|
|
|
REALLOC_N(RARRAY(ary)->ptr, VALUE, RARRAY(ary)->capa);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return top;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_unshift(ary, item)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE ary, item;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_modify(ary);
|
1998-01-16 07:19:22 -05:00
|
|
|
if (RARRAY(ary)->len >= RARRAY(ary)->capa) {
|
1999-08-13 01:45:20 -04:00
|
|
|
long capa_inc = RARRAY(ary)->capa / 2;
|
|
|
|
if (capa_inc < ARY_DEFAULT_SIZE) {
|
|
|
|
capa_inc = ARY_DEFAULT_SIZE;
|
|
|
|
}
|
|
|
|
RARRAY(ary)->capa+=capa_inc;
|
1998-01-16 07:19:22 -05:00
|
|
|
REALLOC_N(RARRAY(ary)->ptr, VALUE, RARRAY(ary)->capa);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* sliding items */
|
1998-01-16 07:19:22 -05:00
|
|
|
MEMMOVE(RARRAY(ary)->ptr+1, RARRAY(ary)->ptr, VALUE, RARRAY(ary)->len);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
RARRAY(ary)->len++;
|
1999-01-19 23:59:39 -05:00
|
|
|
RARRAY(ary)->ptr[0] = item;
|
|
|
|
|
|
|
|
return ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_entry(ary, offset)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE ary;
|
1999-08-13 01:45:20 -04:00
|
|
|
long offset;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1998-01-16 07:19:22 -05:00
|
|
|
if (RARRAY(ary)->len == 0) return Qnil;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
if (offset < 0) {
|
1998-01-16 07:19:22 -05:00
|
|
|
offset = RARRAY(ary)->len + offset;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
if (offset < 0 || RARRAY(ary)->len <= offset) {
|
1998-01-16 07:13:05 -05:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
return RARRAY(ary)->ptr[offset];
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2000-01-31 22:12:21 -05:00
|
|
|
rb_ary_subseq(ary, beg, len)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE ary;
|
1999-08-13 01:45:20 -04:00
|
|
|
long beg, len;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE ary2;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
if (beg > RARRAY(ary)->len) return Qnil;
|
1999-01-19 23:59:39 -05:00
|
|
|
if (beg < 0) {
|
|
|
|
len += beg;
|
|
|
|
beg = 0;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
if (len < 0) return Qnil;
|
1998-01-16 07:19:22 -05:00
|
|
|
if (beg + len > RARRAY(ary)->len) {
|
|
|
|
len = RARRAY(ary)->len - beg;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
if (len < 0) {
|
|
|
|
len = 0;
|
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
if (len == 0) return rb_ary_new2(0);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
ary2 = rb_ary_new2(len);
|
1998-01-16 07:19:22 -05:00
|
|
|
MEMCPY(RARRAY(ary2)->ptr, RARRAY(ary)->ptr+beg, VALUE, len);
|
|
|
|
RARRAY(ary2)->len = len;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
return ary2;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_aref(argc, argv, ary)
|
1998-01-16 07:13:05 -05:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
VALUE arg1, arg2;
|
1999-08-13 01:45:20 -04:00
|
|
|
long beg, len;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
if (rb_scan_args(argc, argv, "11", &arg1, &arg2) == 2) {
|
1999-08-13 01:45:20 -04:00
|
|
|
beg = NUM2LONG(arg1);
|
|
|
|
len = NUM2LONG(arg2);
|
1999-01-19 23:59:39 -05:00
|
|
|
if (beg < 0) {
|
|
|
|
beg = RARRAY(ary)->len + beg;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2000-01-31 22:12:21 -05:00
|
|
|
return rb_ary_subseq(ary, beg, len);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* special case - speeding up */
|
|
|
|
if (FIXNUM_P(arg1)) {
|
1999-08-13 01:45:20 -04:00
|
|
|
return rb_ary_entry(ary, FIX2LONG(arg1));
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
else if (TYPE(arg1) == T_BIGNUM) {
|
|
|
|
rb_raise(rb_eIndexError, "index too big");
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
else {
|
1999-01-19 23:59:39 -05:00
|
|
|
/* check if idx is Range */
|
1999-08-13 01:45:20 -04:00
|
|
|
switch (rb_range_beg_len(arg1, &beg, &len, RARRAY(ary)->len, 0)) {
|
|
|
|
case Qfalse:
|
|
|
|
break;
|
|
|
|
case Qnil:
|
|
|
|
return Qnil;
|
|
|
|
default:
|
2000-01-31 22:12:21 -05:00
|
|
|
return rb_ary_subseq(ary, beg, len);
|
1999-08-13 01:45:20 -04:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
return rb_ary_entry(ary, NUM2LONG(arg1));
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1999-10-13 02:44:42 -04:00
|
|
|
static VALUE
|
|
|
|
rb_ary_at(ary, pos)
|
|
|
|
VALUE ary, pos;
|
|
|
|
{
|
|
|
|
return rb_ary_entry(ary, NUM2LONG(pos));
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_ary_first(ary)
|
|
|
|
VALUE ary;
|
|
|
|
{
|
|
|
|
if (RARRAY(ary)->len == 0) return Qnil;
|
|
|
|
return RARRAY(ary)->ptr[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_ary_last(ary)
|
|
|
|
VALUE ary;
|
|
|
|
{
|
|
|
|
if (RARRAY(ary)->len == 0) return Qnil;
|
|
|
|
return RARRAY(ary)->ptr[RARRAY(ary)->len-1];
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_index(ary, val)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE val;
|
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
long i;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
for (i=0; i<RARRAY(ary)->len; i++) {
|
|
|
|
if (rb_equal(RARRAY(ary)->ptr[i], val))
|
1999-08-13 01:45:20 -04:00
|
|
|
return INT2NUM(i);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_rindex(ary, val)
|
|
|
|
VALUE ary;
|
|
|
|
VALUE val;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
long i = RARRAY(ary)->len;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
while (i--) {
|
|
|
|
if (rb_equal(RARRAY(ary)->ptr[i], val))
|
1999-08-13 01:45:20 -04:00
|
|
|
return INT2NUM(i);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
return Qnil;
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
static VALUE
|
|
|
|
rb_ary_indexes(argc, argv, ary)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE ary;
|
|
|
|
{
|
|
|
|
VALUE new_ary;
|
1999-08-13 01:45:20 -04:00
|
|
|
long i;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
new_ary = rb_ary_new2(argc);
|
|
|
|
for (i=0; i<argc; i++) {
|
1999-08-13 01:45:20 -04:00
|
|
|
rb_ary_push(new_ary, rb_ary_aref(1, argv+i, ary));
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
return new_ary;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_replace(ary, beg, len, rpl)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE ary, rpl;
|
1999-08-13 01:45:20 -04:00
|
|
|
long beg, len;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
if (len < 0) rb_raise(rb_eIndexError, "negative length %d", len);
|
|
|
|
if (beg < 0) {
|
|
|
|
beg += RARRAY(ary)->len;
|
|
|
|
}
|
|
|
|
if (beg < 0) {
|
|
|
|
beg -= RARRAY(ary)->len;
|
|
|
|
rb_raise(rb_eIndexError, "index %d out of array", beg);
|
|
|
|
}
|
|
|
|
if (beg + len > RARRAY(ary)->len) {
|
|
|
|
len = RARRAY(ary)->len - beg;
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
|
|
|
|
2000-01-04 23:41:21 -05:00
|
|
|
if (NIL_P(rpl)) {
|
|
|
|
rpl = rb_ary_new2(0);
|
|
|
|
}
|
|
|
|
else if (TYPE(rpl) != T_ARRAY) {
|
|
|
|
rpl = rb_ary_new3(1, rpl);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
|
|
|
|
rb_ary_modify(ary);
|
1998-01-16 07:19:22 -05:00
|
|
|
if (beg >= RARRAY(ary)->len) {
|
|
|
|
len = beg + RARRAY(rpl)->len;
|
|
|
|
if (len >= RARRAY(ary)->capa) {
|
|
|
|
RARRAY(ary)->capa=len;
|
|
|
|
REALLOC_N(RARRAY(ary)->ptr, VALUE, RARRAY(ary)->capa);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_mem_clear(RARRAY(ary)->ptr+RARRAY(ary)->len, beg-RARRAY(ary)->len);
|
1998-01-16 07:19:22 -05:00
|
|
|
MEMCPY(RARRAY(ary)->ptr+beg, RARRAY(rpl)->ptr, VALUE, RARRAY(rpl)->len);
|
|
|
|
RARRAY(ary)->len = len;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else {
|
1999-08-13 01:45:20 -04:00
|
|
|
long alen;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
if (beg + len > RARRAY(ary)->len) {
|
|
|
|
len = RARRAY(ary)->len - beg;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
alen = RARRAY(ary)->len + RARRAY(rpl)->len - len;
|
|
|
|
if (alen >= RARRAY(ary)->capa) {
|
|
|
|
RARRAY(ary)->capa=alen;
|
|
|
|
REALLOC_N(RARRAY(ary)->ptr, VALUE, RARRAY(ary)->capa);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (len != RARRAY(rpl)->len) {
|
1998-01-16 07:19:22 -05:00
|
|
|
MEMMOVE(RARRAY(ary)->ptr+beg+RARRAY(rpl)->len, RARRAY(ary)->ptr+beg+len,
|
|
|
|
VALUE, RARRAY(ary)->len-(beg+len));
|
|
|
|
RARRAY(ary)->len = alen;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
MEMCPY(RARRAY(ary)->ptr+beg, RARRAY(rpl)->ptr, VALUE, RARRAY(rpl)->len);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_aset(argc, argv, ary)
|
1998-01-16 07:13:05 -05:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE arg1, arg2, arg3;
|
1999-08-13 01:45:20 -04:00
|
|
|
long offset, beg, len;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
if (rb_scan_args(argc, argv, "21", &arg1, &arg2, &arg3) == 3) {
|
1999-08-13 01:45:20 -04:00
|
|
|
rb_ary_replace(ary, NUM2LONG(arg1), NUM2LONG(arg2), arg3);
|
1998-01-16 07:19:22 -05:00
|
|
|
return arg3;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else if (FIXNUM_P(arg1)) {
|
1999-08-13 01:45:20 -04:00
|
|
|
offset = FIX2LONG(arg1);
|
1998-01-16 07:13:05 -05:00
|
|
|
goto fixnum;
|
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
else if (rb_range_beg_len(arg1, &beg, &len, RARRAY(ary)->len, 1)) {
|
1998-01-16 07:13:05 -05:00
|
|
|
/* check if idx is Range */
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_replace(ary, beg, len, arg2);
|
1998-01-16 07:13:05 -05:00
|
|
|
return arg2;
|
|
|
|
}
|
|
|
|
if (TYPE(arg1) == T_BIGNUM) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_raise(rb_eIndexError, "index too big");
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
offset = NUM2LONG(arg1);
|
1998-01-16 07:13:05 -05:00
|
|
|
fixnum:
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_store(ary, offset, arg2);
|
1998-01-16 07:13:05 -05:00
|
|
|
return arg2;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_each(ary)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
long i;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
for (i=0; i<RARRAY(ary)->len; i++) {
|
|
|
|
rb_yield(RARRAY(ary)->ptr[i]);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
return ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_each_index(ary)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
long i;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
for (i=0; i<RARRAY(ary)->len; i++) {
|
1999-08-13 01:45:20 -04:00
|
|
|
rb_yield(INT2NUM(i));
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
return ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_reverse_each(ary)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
long len = RARRAY(ary)->len;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
while (len--) {
|
1998-01-16 07:19:22 -05:00
|
|
|
rb_yield(RARRAY(ary)->ptr[len]);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
return ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_length(ary)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
return INT2NUM(RARRAY(ary)->len);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_empty_p(ary)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1998-01-16 07:19:22 -05:00
|
|
|
if (RARRAY(ary)->len == 0)
|
1999-01-19 23:59:39 -05:00
|
|
|
return Qtrue;
|
|
|
|
return Qfalse;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_clone(ary)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2000-01-04 23:41:21 -05:00
|
|
|
VALUE clone = rb_ary_new2(RARRAY(ary)->len);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-01-04 23:41:21 -05:00
|
|
|
CLONESETUP(clone, ary);
|
|
|
|
MEMCPY(RARRAY(clone)->ptr, RARRAY(ary)->ptr, VALUE, RARRAY(ary)->len);
|
|
|
|
RARRAY(clone)->len = RARRAY(ary)->len;
|
|
|
|
return clone;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_dup(ary)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE ary;
|
|
|
|
{
|
2000-01-04 23:41:21 -05:00
|
|
|
VALUE dup;
|
|
|
|
|
|
|
|
dup = rb_ary_s_create(RARRAY(ary)->len, RARRAY(ary)->ptr, CLASS_OF(ary));
|
|
|
|
if (OBJ_TAINTED(ary)) OBJ_TAINT(dup);
|
|
|
|
return dup;
|
1998-01-16 07:19:22 -05:00
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
static VALUE
|
|
|
|
to_ary(ary)
|
|
|
|
VALUE ary;
|
|
|
|
{
|
|
|
|
return rb_convert_type(ary, T_ARRAY, "Array", "to_ary");
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
extern VALUE rb_output_fs;
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
inspect_join(ary, arg)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE ary;
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE *arg;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_ary_join(arg[0], arg[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_ary_join(ary, sep)
|
|
|
|
VALUE ary, sep;
|
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
long i;
|
2000-01-04 23:41:21 -05:00
|
|
|
int taint;
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE result, tmp;
|
2000-01-04 23:41:21 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
if (RARRAY(ary)->len == 0) return rb_str_new(0, 0);
|
2000-01-04 23:41:21 -05:00
|
|
|
if (OBJ_TAINTED(ary)) taint = 1;
|
|
|
|
if (OBJ_TAINTED(sep)) taint = 1;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
tmp = RARRAY(ary)->ptr[0];
|
2000-01-04 23:41:21 -05:00
|
|
|
if (OBJ_TAINTED(tmp)) taint = 1;
|
1999-01-19 23:59:39 -05:00
|
|
|
switch (TYPE(tmp)) {
|
1998-01-16 07:13:05 -05:00
|
|
|
case T_STRING:
|
1999-01-19 23:59:39 -05:00
|
|
|
result = rb_str_dup(tmp);
|
1998-01-16 07:13:05 -05:00
|
|
|
break;
|
|
|
|
case T_ARRAY:
|
1999-01-19 23:59:39 -05:00
|
|
|
if (rb_inspecting_p(tmp)) {
|
|
|
|
result = rb_str_new2("[...]");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
VALUE args[2];
|
|
|
|
|
|
|
|
args[0] = tmp;
|
|
|
|
args[1] = sep;
|
|
|
|
result = rb_protect_inspect(inspect_join, ary, (VALUE)args);
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
break;
|
|
|
|
default:
|
1999-01-19 23:59:39 -05:00
|
|
|
result = rb_obj_as_string(tmp);
|
1998-01-16 07:13:05 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
for (i=1; i<RARRAY(ary)->len; i++) {
|
|
|
|
tmp = RARRAY(ary)->ptr[i];
|
1998-01-16 07:13:05 -05:00
|
|
|
switch (TYPE(tmp)) {
|
|
|
|
case T_STRING:
|
|
|
|
break;
|
|
|
|
case T_ARRAY:
|
1999-01-19 23:59:39 -05:00
|
|
|
if (rb_inspecting_p(tmp)) {
|
|
|
|
tmp = rb_str_new2("[...]");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
VALUE args[2];
|
|
|
|
|
|
|
|
args[0] = tmp;
|
|
|
|
args[1] = sep;
|
|
|
|
tmp = rb_protect_inspect(inspect_join, ary, (VALUE)args);
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
break;
|
|
|
|
default:
|
1999-01-19 23:59:39 -05:00
|
|
|
tmp = rb_obj_as_string(tmp);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
if (!NIL_P(sep)) rb_str_concat(result, sep);
|
|
|
|
rb_str_cat(result, RSTRING(tmp)->ptr, RSTRING(tmp)->len);
|
2000-01-04 23:41:21 -05:00
|
|
|
if (OBJ_TAINTED(tmp)) taint = 1;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2000-01-04 23:41:21 -05:00
|
|
|
if (taint) OBJ_TAINT(result);
|
1998-01-16 07:13:05 -05:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2000-01-04 23:41:21 -05:00
|
|
|
rb_ary_join_m(argc, argv, ary)
|
1998-01-16 07:13:05 -05:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
VALUE sep;
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "01", &sep);
|
1999-01-19 23:59:39 -05:00
|
|
|
if (NIL_P(sep)) sep = rb_output_fs;
|
|
|
|
return rb_ary_join(ary, sep);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_to_s(ary)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE ary;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE str;
|
|
|
|
|
|
|
|
if (RARRAY(ary)->len == 0) return rb_str_new(0, 0);
|
|
|
|
str = rb_ary_join(ary, rb_output_fs);
|
|
|
|
if (NIL_P(str)) return rb_str_new(0, 0);
|
1998-01-16 07:13:05 -05:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
static ID inspect_key;
|
|
|
|
|
|
|
|
struct inspect_arg {
|
|
|
|
VALUE (*func)();
|
|
|
|
VALUE arg1, arg2;
|
|
|
|
};
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
inspect_call(arg)
|
|
|
|
struct inspect_arg *arg;
|
|
|
|
{
|
|
|
|
return (*arg->func)(arg->arg1, arg->arg2);
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
inspect_ensure(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
|
|
|
VALUE inspect_tbl;
|
|
|
|
|
|
|
|
inspect_tbl = rb_thread_local_aref(rb_thread_current(), inspect_key);
|
|
|
|
rb_ary_pop(inspect_tbl);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_protect_inspect(func, obj, arg)
|
|
|
|
VALUE (*func)();
|
|
|
|
VALUE obj, arg;
|
|
|
|
{
|
|
|
|
struct inspect_arg iarg;
|
|
|
|
VALUE inspect_tbl;
|
2000-01-31 22:12:21 -05:00
|
|
|
VALUE id;
|
1999-01-19 23:59:39 -05:00
|
|
|
|
|
|
|
if (!inspect_key) {
|
|
|
|
inspect_key = rb_intern("__inspect_key__");
|
|
|
|
}
|
|
|
|
inspect_tbl = rb_thread_local_aref(rb_thread_current(), inspect_key);
|
|
|
|
if (NIL_P(inspect_tbl)) {
|
|
|
|
inspect_tbl = rb_ary_new();
|
|
|
|
rb_thread_local_aset(rb_thread_current(), inspect_key, inspect_tbl);
|
|
|
|
}
|
2000-01-31 22:12:21 -05:00
|
|
|
id = rb_obj_id(obj);
|
|
|
|
if (rb_ary_includes(inspect_tbl, id)) {
|
2000-01-04 23:41:21 -05:00
|
|
|
return (*func)(obj, arg);
|
|
|
|
}
|
2000-01-31 22:12:21 -05:00
|
|
|
rb_ary_push(inspect_tbl, id);
|
1999-01-19 23:59:39 -05:00
|
|
|
iarg.func = func;
|
|
|
|
iarg.arg1 = obj;
|
|
|
|
iarg.arg2 = arg;
|
2000-01-04 23:41:21 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_ensure(inspect_call, (VALUE)&iarg, inspect_ensure, obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_inspecting_p(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
|
|
|
VALUE inspect_tbl;
|
|
|
|
|
|
|
|
if (!inspect_key) return Qfalse;
|
|
|
|
inspect_tbl = rb_thread_local_aref(rb_thread_current(), inspect_key);
|
|
|
|
if (NIL_P(inspect_tbl)) return Qfalse;
|
2000-01-31 22:12:21 -05:00
|
|
|
return rb_ary_includes(inspect_tbl, rb_obj_id(obj));
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
inspect_ary(ary)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
long i = 0;
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE s, str;
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
str = rb_str_new2("[");
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
for (i=0; i<RARRAY(ary)->len; i++) {
|
|
|
|
s = rb_inspect(RARRAY(ary)->ptr[i]);
|
1999-01-19 23:59:39 -05:00
|
|
|
if (i > 0) rb_str_cat(str, ", ", 2);
|
|
|
|
rb_str_cat(str, RSTRING(s)->ptr, RSTRING(s)->len);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_str_cat(str, "]", 1);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_inspect(ary)
|
|
|
|
VALUE ary;
|
|
|
|
{
|
|
|
|
if (RARRAY(ary)->len == 0) return rb_str_new2("[]");
|
|
|
|
if (rb_inspecting_p(ary)) return rb_str_new2("[...]");
|
|
|
|
return rb_protect_inspect(inspect_ary, ary, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_ary_to_a(ary)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE ary;
|
|
|
|
{
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_reverse(ary)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
VALUE *p1, *p2;
|
|
|
|
VALUE tmp;
|
|
|
|
|
1999-10-18 05:03:16 -04:00
|
|
|
rb_ary_modify(ary);
|
1999-01-19 23:59:39 -05:00
|
|
|
if (RARRAY(ary)->len == 0) return ary;
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
p1 = RARRAY(ary)->ptr;
|
|
|
|
p2 = p1 + RARRAY(ary)->len - 1; /* points last item */
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
while (p1 < p2) {
|
|
|
|
tmp = *p1;
|
|
|
|
*p1 = *p2;
|
|
|
|
*p2 = tmp;
|
|
|
|
p1++; p2--;
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
return ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2000-01-04 23:41:21 -05:00
|
|
|
rb_ary_reverse_m(ary)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_ary_reverse(rb_ary_dup(ary));
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static ID cmp;
|
|
|
|
|
|
|
|
static int
|
|
|
|
sort_1(a, b)
|
|
|
|
VALUE *a, *b;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE retval = rb_yield(rb_assoc_new(*a, *b));
|
1998-01-16 07:13:05 -05:00
|
|
|
return NUM2INT(retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
sort_2(a, b)
|
|
|
|
VALUE *a, *b;
|
|
|
|
{
|
|
|
|
VALUE retval;
|
|
|
|
|
|
|
|
if (FIXNUM_P(*a)) {
|
|
|
|
if (FIXNUM_P(*b)) return *a - *b;
|
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
else if (TYPE(*a) == T_STRING && TYPE(*b) == T_STRING) {
|
|
|
|
return rb_str_cmp(*a, *b);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
retval = rb_funcall(*a, cmp, 1, *b);
|
|
|
|
return NUM2INT(retval);
|
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_sort_bang(ary)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-10-16 06:33:06 -04:00
|
|
|
rb_ary_modify(ary);
|
1999-01-19 23:59:39 -05:00
|
|
|
if (RARRAY(ary)->len <= 1) return ary;
|
|
|
|
|
|
|
|
FL_SET(ary, ARY_TMPLOCK); /* prohibit modification during sort */
|
|
|
|
rb_ensure(sort_internal, ary, sort_unlock, ary);
|
1998-01-16 07:19:22 -05:00
|
|
|
return ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_sort(ary)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE ary;
|
|
|
|
{
|
1999-10-16 06:33:06 -04:00
|
|
|
ary = rb_ary_dup(ary);
|
1999-10-20 03:10:23 -04:00
|
|
|
rb_ary_sort_bang(ary);
|
|
|
|
return ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1999-10-29 05:25:48 -04:00
|
|
|
static VALUE
|
|
|
|
rb_ary_collect(ary)
|
|
|
|
VALUE ary;
|
|
|
|
{
|
|
|
|
long len, i;
|
|
|
|
VALUE collect;
|
|
|
|
|
|
|
|
if (!rb_iterator_p()) {
|
|
|
|
return rb_ary_dup(ary);
|
|
|
|
}
|
|
|
|
|
|
|
|
len = RARRAY(ary)->len;
|
|
|
|
collect = rb_ary_new2(len);
|
|
|
|
for (i=0; i<len; i++) {
|
|
|
|
rb_ary_push(collect, rb_yield(RARRAY(ary)->ptr[i]));
|
|
|
|
}
|
|
|
|
return collect;
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_delete(ary, item)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE item;
|
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
long i1, i2;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_modify(ary);
|
1998-01-16 07:19:22 -05:00
|
|
|
for (i1 = i2 = 0; i1 < RARRAY(ary)->len; i1++) {
|
|
|
|
if (rb_equal(RARRAY(ary)->ptr[i1], item)) continue;
|
1998-01-16 07:13:05 -05:00
|
|
|
if (i1 != i2) {
|
1998-01-16 07:19:22 -05:00
|
|
|
RARRAY(ary)->ptr[i2] = RARRAY(ary)->ptr[i1];
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
i2++;
|
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
if (RARRAY(ary)->len == i2) {
|
1999-01-19 23:59:39 -05:00
|
|
|
if (rb_iterator_p()) {
|
|
|
|
return rb_yield(item);
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
else {
|
1998-01-16 07:19:22 -05:00
|
|
|
RARRAY(ary)->len = i2;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
2000-01-04 23:41:21 -05:00
|
|
|
rb_ary_delete_at(ary, pos)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE ary;
|
2000-01-04 23:41:21 -05:00
|
|
|
long pos;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2000-01-04 23:41:21 -05:00
|
|
|
long i, len = RARRAY(ary)->len;
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE del = Qnil;
|
|
|
|
|
1999-10-16 06:33:06 -04:00
|
|
|
rb_ary_modify(ary);
|
1999-10-13 02:44:42 -04:00
|
|
|
if (pos >= len) return Qnil;
|
|
|
|
if (pos < 0) pos += len;
|
|
|
|
if (pos < 0) return Qnil;
|
|
|
|
|
|
|
|
del = RARRAY(ary)->ptr[pos];
|
|
|
|
for (i = pos + 1; i < len; i++, pos++) {
|
|
|
|
RARRAY(ary)->ptr[pos] = RARRAY(ary)->ptr[i];
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-10-13 02:44:42 -04:00
|
|
|
RARRAY(ary)->len = pos;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
return del;
|
|
|
|
}
|
|
|
|
|
2000-01-04 23:41:21 -05:00
|
|
|
static VALUE
|
|
|
|
rb_ary_delete_at_m(argc, argv, ary)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE ary;
|
|
|
|
{
|
|
|
|
VALUE arg1, arg2;
|
|
|
|
long pos, len, i;
|
|
|
|
|
|
|
|
rb_ary_modify(ary);
|
|
|
|
if (rb_scan_args(argc, argv, "11", &arg1, &arg2) == 2) {
|
|
|
|
pos = NUM2LONG(arg1);
|
|
|
|
len = NUM2LONG(arg2);
|
|
|
|
delete_pos_len:
|
|
|
|
if (pos < 0) {
|
|
|
|
pos = RARRAY(ary)->len + pos;
|
|
|
|
}
|
2000-01-31 22:12:21 -05:00
|
|
|
arg2 = rb_ary_subseq(ary, pos, len);
|
2000-01-04 23:41:21 -05:00
|
|
|
rb_ary_replace(ary, pos, len, Qnil); /* Qnil/rb_ary_new2(0) */
|
|
|
|
return arg2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!FIXNUM_P(arg1) && rb_range_beg_len(arg1, &pos, &len, RARRAY(ary)->len, 1)) {
|
|
|
|
goto delete_pos_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = NUM2LONG(arg1);
|
|
|
|
len = RARRAY(ary)->len;
|
|
|
|
|
|
|
|
if (pos >= len) return Qnil;
|
|
|
|
if (pos < 0) pos += len;
|
|
|
|
if (pos < 0) return Qnil;
|
|
|
|
|
|
|
|
arg2 = RARRAY(ary)->ptr[pos];
|
|
|
|
for (i = pos + 1; i < len; i++, pos++) {
|
|
|
|
RARRAY(ary)->ptr[pos] = RARRAY(ary)->ptr[i];
|
|
|
|
}
|
|
|
|
RARRAY(ary)->len = pos;
|
|
|
|
|
|
|
|
return arg2;
|
|
|
|
}
|
2000-01-31 22:12:21 -05:00
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_delete_if(ary)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
long i1, i2;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_modify(ary);
|
1998-01-16 07:19:22 -05:00
|
|
|
for (i1 = i2 = 0; i1 < RARRAY(ary)->len; i1++) {
|
1999-08-13 01:45:20 -04:00
|
|
|
if (RTEST(rb_yield(RARRAY(ary)->ptr[i1]))) continue;
|
1998-01-16 07:13:05 -05:00
|
|
|
if (i1 != i2) {
|
1998-01-16 07:19:22 -05:00
|
|
|
RARRAY(ary)->ptr[i2] = RARRAY(ary)->ptr[i1];
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
i2++;
|
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
RARRAY(ary)->len = i2;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
return ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_filter(ary)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
long i;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_modify(ary);
|
1998-01-16 07:19:22 -05:00
|
|
|
for (i = 0; i < RARRAY(ary)->len; i++) {
|
|
|
|
RARRAY(ary)->ptr[i] = rb_yield(RARRAY(ary)->ptr[i]);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
return ary;
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
static VALUE
|
2000-01-04 23:41:21 -05:00
|
|
|
rb_ary_replace_m(ary, ary2)
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE ary, ary2;
|
|
|
|
{
|
|
|
|
ary2 = to_ary(ary2);
|
1999-08-13 01:45:20 -04:00
|
|
|
rb_ary_replace(ary, 0, RARRAY(ary)->len, ary2);
|
1998-01-16 07:19:22 -05:00
|
|
|
return ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_clear(ary)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-10-18 05:03:16 -04:00
|
|
|
rb_ary_modify(ary);
|
1998-01-16 07:19:22 -05:00
|
|
|
RARRAY(ary)->len = 0;
|
|
|
|
if (ARY_DEFAULT_SIZE*3 < RARRAY(ary)->capa) {
|
|
|
|
RARRAY(ary)->capa = ARY_DEFAULT_SIZE * 2;
|
|
|
|
REALLOC_N(RARRAY(ary)->ptr, VALUE, RARRAY(ary)->capa);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
return ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_fill(argc, argv, ary)
|
1998-01-16 07:13:05 -05:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
VALUE item, arg1, arg2;
|
1999-08-13 01:45:20 -04:00
|
|
|
long beg, end, len;
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE *p, *pend;
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
rb_scan_args(argc, argv, "12", &item, &arg1, &arg2);
|
|
|
|
switch (argc) {
|
|
|
|
case 1:
|
|
|
|
beg = 0;
|
|
|
|
len = RARRAY(ary)->len - beg;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
if (rb_range_beg_len(arg1, &beg, &len, RARRAY(ary)->len, 1)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* fall through */
|
|
|
|
case 3:
|
|
|
|
beg = NIL_P(arg1)?0:NUM2LONG(arg1);
|
1998-01-16 07:13:05 -05:00
|
|
|
if (beg < 0) {
|
1998-01-16 07:19:22 -05:00
|
|
|
beg = RARRAY(ary)->len + beg;
|
1998-01-16 07:13:05 -05:00
|
|
|
if (beg < 0) beg = 0;
|
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
len = NIL_P(arg2)?RARRAY(ary)->len - beg:NUM2LONG(arg2);
|
|
|
|
break;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_modify(ary);
|
1998-01-16 07:13:05 -05:00
|
|
|
end = beg + len;
|
1998-01-16 07:19:22 -05:00
|
|
|
if (end > RARRAY(ary)->len) {
|
|
|
|
if (end >= RARRAY(ary)->capa) {
|
|
|
|
RARRAY(ary)->capa=end;
|
|
|
|
REALLOC_N(RARRAY(ary)->ptr, VALUE, RARRAY(ary)->capa);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
if (beg > RARRAY(ary)->len) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_mem_clear(RARRAY(ary)->ptr+RARRAY(ary)->len,end-RARRAY(ary)->len);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
RARRAY(ary)->len = end;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
p = RARRAY(ary)->ptr + beg; pend = p + len;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
while (p < pend) {
|
|
|
|
*p++ = item;
|
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
return ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_plus(x, y)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE x, y;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE z;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-01-04 23:41:21 -05:00
|
|
|
Check_Type(y, T_ARRAY);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
z = rb_ary_new2(RARRAY(x)->len + RARRAY(y)->len);
|
1998-01-16 07:19:22 -05:00
|
|
|
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;
|
|
|
|
return z;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_concat(x, y)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE x, y;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
VALUE *p, *pend;
|
|
|
|
|
1999-10-16 06:33:06 -04:00
|
|
|
rb_ary_modify(x);
|
2000-01-04 23:41:21 -05:00
|
|
|
Check_Type(y, T_ARRAY);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
p = RARRAY(y)->ptr;
|
|
|
|
pend = p + RARRAY(y)->len;
|
1998-01-16 07:13:05 -05:00
|
|
|
while (p < pend) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_store(x, RARRAY(x)->len, *p);
|
1998-01-16 07:13:05 -05:00
|
|
|
p++;
|
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
return x;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_times(ary, times)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE times;
|
|
|
|
{
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE ary2;
|
1999-08-13 01:45:20 -04:00
|
|
|
long i, len;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
if (TYPE(times) == T_STRING) {
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_ary_join(ary, times);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
len = NUM2LONG(times);
|
1998-01-16 07:13:05 -05:00
|
|
|
if (len < 0) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_raise(rb_eArgError, "negative argument");
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
len *= RARRAY(ary)->len;
|
|
|
|
|
|
|
|
ary2 = rb_ary_new2(len);
|
|
|
|
RARRAY(ary2)->len = len;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
for (i=0; i<len; i+=RARRAY(ary)->len) {
|
|
|
|
MEMCPY(RARRAY(ary2)->ptr+i, RARRAY(ary)->ptr, VALUE, RARRAY(ary)->len);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
return ary2;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_assoc(ary, key)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE key;
|
|
|
|
{
|
|
|
|
VALUE *p, *pend;
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
p = RARRAY(ary)->ptr; pend = p + RARRAY(ary)->len;
|
1998-01-16 07:13:05 -05:00
|
|
|
while (p < pend) {
|
|
|
|
if (TYPE(*p) == T_ARRAY
|
|
|
|
&& RARRAY(*p)->len > 1
|
|
|
|
&& rb_equal(RARRAY(*p)->ptr[0], key))
|
|
|
|
return *p;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_rassoc(ary, value)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE value;
|
|
|
|
{
|
|
|
|
VALUE *p, *pend;
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
p = RARRAY(ary)->ptr; pend = p + RARRAY(ary)->len;
|
1998-01-16 07:13:05 -05:00
|
|
|
while (p < pend) {
|
|
|
|
if (TYPE(*p) == T_ARRAY
|
|
|
|
&& RARRAY(*p)->len > 1
|
|
|
|
&& rb_equal(RARRAY(*p)->ptr[1], value))
|
|
|
|
return *p;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_equal(ary1, ary2)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE ary1, ary2;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
long i;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
if (TYPE(ary2) != T_ARRAY) return Qfalse;
|
|
|
|
if (RARRAY(ary1)->len != RARRAY(ary2)->len) return Qfalse;
|
1998-01-16 07:19:22 -05:00
|
|
|
for (i=0; i<RARRAY(ary1)->len; i++) {
|
|
|
|
if (!rb_equal(RARRAY(ary1)->ptr[i], RARRAY(ary2)->ptr[i]))
|
1999-01-19 23:59:39 -05:00
|
|
|
return Qfalse;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
return Qtrue;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_eql(ary1, ary2)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE ary1, ary2;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
long i;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
if (TYPE(ary2) != T_ARRAY) return Qfalse;
|
1998-01-16 07:19:22 -05:00
|
|
|
if (RARRAY(ary1)->len != RARRAY(ary2)->len)
|
1999-01-19 23:59:39 -05:00
|
|
|
return Qfalse;
|
1998-01-16 07:19:22 -05:00
|
|
|
for (i=0; i<RARRAY(ary1)->len; i++) {
|
|
|
|
if (!rb_eql(RARRAY(ary1)->ptr[i], RARRAY(ary2)->ptr[i]))
|
1999-01-19 23:59:39 -05:00
|
|
|
return Qfalse;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
return Qtrue;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_hash(ary)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
long i;
|
2000-01-18 01:09:05 -05:00
|
|
|
int n, h;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
h = RARRAY(ary)->len;
|
|
|
|
for (i=0; i<RARRAY(ary)->len; i++) {
|
2000-01-18 01:09:05 -05:00
|
|
|
h = (h<<1) | (h<0 ? 1 : 0);
|
|
|
|
n = rb_hash(RARRAY(ary)->ptr[i]);
|
1999-01-19 23:59:39 -05:00
|
|
|
h ^= NUM2LONG(n);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
return INT2FIX(h);
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_includes(ary, item)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE item;
|
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
long i;
|
1998-01-16 07:19:22 -05:00
|
|
|
for (i=0; i<RARRAY(ary)->len; i++) {
|
|
|
|
if (rb_equal(RARRAY(ary)->ptr[i], item)) {
|
1999-01-19 23:59:39 -05:00
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_ary_cmp(ary, ary2)
|
|
|
|
VALUE ary;
|
|
|
|
VALUE ary2;
|
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
long i, len;
|
1999-01-19 23:59:39 -05:00
|
|
|
|
|
|
|
ary2 = to_ary(ary2);
|
|
|
|
len = RARRAY(ary)->len;
|
|
|
|
if (len > RARRAY(ary2)->len) {
|
|
|
|
len = RARRAY(ary2)->len;
|
|
|
|
}
|
|
|
|
for (i=0; i<len; i++) {
|
|
|
|
VALUE v = rb_funcall(RARRAY(ary)->ptr[i],cmp,1,RARRAY(ary2)->ptr[i]);
|
|
|
|
if (v != INT2FIX(0)) {
|
|
|
|
return v;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
len = RARRAY(ary)->len - RARRAY(ary2)->len;
|
|
|
|
if (len == 0) return INT2FIX(0);
|
|
|
|
if (len > 0) return INT2FIX(1);
|
|
|
|
return INT2FIX(-1);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_diff(ary1, ary2)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE ary1, ary2;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
VALUE ary3;
|
1999-08-13 01:45:20 -04:00
|
|
|
long i;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
ary2 = to_ary(ary2);
|
|
|
|
ary3 = rb_ary_new();
|
1998-01-16 07:19:22 -05:00
|
|
|
for (i=0; i<RARRAY(ary1)->len; i++) {
|
1999-01-19 23:59:39 -05:00
|
|
|
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]);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
return ary3;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_and(ary1, ary2)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE ary1, ary2;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
VALUE ary3;
|
1999-08-13 01:45:20 -04:00
|
|
|
long i;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
ary2 = to_ary(ary2);
|
|
|
|
ary3 = rb_ary_new();
|
1998-01-16 07:19:22 -05:00
|
|
|
for (i=0; i<RARRAY(ary1)->len; i++) {
|
1999-01-19 23:59:39 -05:00
|
|
|
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]);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ary3;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_or(ary1, ary2)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE ary1, ary2;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
VALUE ary3;
|
1999-08-13 01:45:20 -04:00
|
|
|
long i;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
if (TYPE(ary2) != T_ARRAY) {
|
1999-01-19 23:59:39 -05:00
|
|
|
if (rb_ary_includes(ary1, ary2)) return ary1;
|
2000-01-04 23:41:21 -05:00
|
|
|
else return rb_ary_push(ary1, ary2);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
ary3 = rb_ary_new();
|
1998-01-16 07:19:22 -05:00
|
|
|
for (i=0; i<RARRAY(ary1)->len; i++) {
|
1999-01-19 23:59:39 -05:00
|
|
|
if (!rb_ary_includes(ary3, RARRAY(ary1)->ptr[i]))
|
|
|
|
rb_ary_push(ary3, RARRAY(ary1)->ptr[i]);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
for (i=0; i<RARRAY(ary2)->len; i++) {
|
1999-01-19 23:59:39 -05:00
|
|
|
if (!rb_ary_includes(ary3, RARRAY(ary2)->ptr[i]))
|
|
|
|
rb_ary_push(ary3, RARRAY(ary2)->ptr[i]);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
return ary3;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_uniq_bang(ary)
|
|
|
|
VALUE ary;
|
|
|
|
{
|
|
|
|
VALUE *p, *q, *t, *end;
|
|
|
|
VALUE v;
|
|
|
|
|
|
|
|
rb_ary_modify(ary);
|
|
|
|
p = RARRAY(ary)->ptr;
|
|
|
|
end = p + RARRAY(ary)->len;
|
|
|
|
|
|
|
|
while (p < end) {
|
|
|
|
v = *p++;
|
|
|
|
q = t = p;
|
|
|
|
while (q < end) {
|
|
|
|
if (rb_equal(*q, v)) q++;
|
|
|
|
else *t++ = *q++;
|
|
|
|
}
|
|
|
|
end = t;
|
|
|
|
}
|
|
|
|
if (RARRAY(ary)->len == (end - RARRAY(ary)->ptr)) {
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
RARRAY(ary)->len = (end - RARRAY(ary)->ptr);
|
|
|
|
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_ary_uniq(ary)
|
|
|
|
VALUE ary;
|
|
|
|
{
|
1999-10-20 03:10:23 -04:00
|
|
|
ary = rb_ary_dup(ary);
|
|
|
|
rb_ary_uniq_bang(ary);
|
|
|
|
return ary;
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_ary_compact_bang(ary)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
VALUE *p, *t, *end;
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_modify(ary);
|
1998-01-16 07:19:22 -05:00
|
|
|
p = t = RARRAY(ary)->ptr;
|
|
|
|
end = p + RARRAY(ary)->len;
|
1998-01-16 07:13:05 -05:00
|
|
|
while (t < end) {
|
|
|
|
if (NIL_P(*t)) t++;
|
|
|
|
else *p++ = *t++;
|
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
if (RARRAY(ary)->len == (p - RARRAY(ary)->ptr)) {
|
|
|
|
return Qnil;
|
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
RARRAY(ary)->len = RARRAY(ary)->capa = (p - RARRAY(ary)->ptr);
|
|
|
|
REALLOC_N(RARRAY(ary)->ptr, VALUE, RARRAY(ary)->len);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
return ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_compact(ary)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-10-20 03:10:23 -04:00
|
|
|
ary = rb_ary_dup(ary);
|
|
|
|
rb_ary_compact_bang(ary);
|
|
|
|
return ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_nitems(ary)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
long n = 0;
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE *p, *pend;
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
p = RARRAY(ary)->ptr;
|
|
|
|
pend = p + RARRAY(ary)->len;
|
1998-01-16 07:13:05 -05:00
|
|
|
while (p < pend) {
|
|
|
|
if (!NIL_P(*p)) n++;
|
|
|
|
p++;
|
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
return INT2NUM(n);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
static VALUE
|
|
|
|
rb_ary_flatten_bang(ary)
|
|
|
|
VALUE ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
long i;
|
1999-01-19 23:59:39 -05:00
|
|
|
int mod = 0;
|
2000-01-31 22:12:21 -05:00
|
|
|
VALUE flattening = Qnil;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_modify(ary);
|
|
|
|
for (i=0; i<RARRAY(ary)->len; i++) {
|
|
|
|
VALUE ary2 = RARRAY(ary)->ptr[i];
|
|
|
|
if (TYPE(ary2) == T_ARRAY) {
|
2000-01-31 22:12:21 -05:00
|
|
|
if (ary == ary2) {
|
|
|
|
ary2 = Qnil;
|
|
|
|
} else {
|
|
|
|
VALUE id;
|
|
|
|
|
|
|
|
if (NIL_P(flattening)) {
|
|
|
|
flattening = rb_ary_new();
|
|
|
|
}
|
|
|
|
id = rb_obj_id(ary2);
|
|
|
|
if (rb_ary_includes(flattening, id)) {
|
|
|
|
rb_raise(rb_eArgError, "tryed to flatten recursive array");
|
|
|
|
}
|
|
|
|
rb_ary_push(flattening, id);
|
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_replace(ary, i--, 1, ary2);
|
|
|
|
mod = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (mod == 0) return Qnil;
|
|
|
|
return ary;
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
static VALUE
|
|
|
|
rb_ary_flatten(ary)
|
|
|
|
VALUE ary;
|
|
|
|
{
|
1999-10-20 03:10:23 -04:00
|
|
|
ary = rb_ary_dup(ary);
|
|
|
|
rb_ary_flatten_bang(ary);
|
|
|
|
return ary;
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
void
|
|
|
|
Init_Array()
|
|
|
|
{
|
|
|
|
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, "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_equal, 1);
|
|
|
|
|
|
|
|
rb_define_method(rb_cArray, "[]", rb_ary_aref, -1);
|
|
|
|
rb_define_method(rb_cArray, "[]=", rb_ary_aset, -1);
|
1999-10-13 02:44:42 -04:00
|
|
|
rb_define_method(rb_cArray, "at", rb_ary_at, 1);
|
|
|
|
rb_define_method(rb_cArray, "first", rb_ary_first, 0);
|
|
|
|
rb_define_method(rb_cArray, "last", rb_ary_last, 0);
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_define_method(rb_cArray, "concat", rb_ary_concat, 1);
|
|
|
|
rb_define_method(rb_cArray, "<<", rb_ary_push, 1);
|
2000-01-04 23:41:21 -05:00
|
|
|
rb_define_method(rb_cArray, "push", rb_ary_push_m, -1);
|
2000-01-08 00:00:25 -05:00
|
|
|
rb_define_method(rb_cArray, "pop", rb_ary_pop, 0);
|
|
|
|
rb_define_method(rb_cArray, "shift", rb_ary_shift, 0);
|
1999-01-19 23:59:39 -05:00
|
|
|
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);
|
2000-01-04 23:41:21 -05:00
|
|
|
rb_define_method(rb_cArray, "join", rb_ary_join_m, -1);
|
|
|
|
rb_define_method(rb_cArray, "reverse", rb_ary_reverse_m, 0);
|
1999-01-19 23:59:39 -05:00
|
|
|
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);
|
1999-10-29 05:25:48 -04:00
|
|
|
rb_define_method(rb_cArray, "collect", rb_ary_collect, 0);
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_define_method(rb_cArray, "delete", rb_ary_delete, 1);
|
2000-01-04 23:41:21 -05:00
|
|
|
rb_define_method(rb_cArray, "delete_at", rb_ary_delete_at_m, -1);
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_define_method(rb_cArray, "delete_if", rb_ary_delete_if, 0);
|
1999-08-13 01:45:20 -04:00
|
|
|
rb_define_method(rb_cArray, "reject!", rb_ary_delete_if, 0);
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_define_method(rb_cArray, "filter", rb_ary_filter, 0);
|
2000-01-04 23:41:21 -05:00
|
|
|
rb_define_method(rb_cArray, "replace", rb_ary_replace_m, 1);
|
1999-01-19 23:59:39 -05:00
|
|
|
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_cmp, 1);
|
|
|
|
|
2000-01-31 22:12:21 -05:00
|
|
|
rb_define_method(rb_cArray, "slice", rb_ary_aref, -1);
|
|
|
|
rb_define_method(rb_cArray, "slice!", rb_ary_delete_at_m, -1);
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
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);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
cmp = rb_intern("<=>");
|
|
|
|
}
|