2000-05-01 13:42:38 +04:00
|
|
|
/**********************************************************************
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
array.c -
|
|
|
|
|
|
|
|
$Author$
|
|
|
|
$Date$
|
|
|
|
created at: Fri Aug 6 09:46:12 JST 1993
|
|
|
|
|
2003-01-16 10:34:03 +03:00
|
|
|
Copyright (C) 1993-2003 Yukihiro Matsumoto
|
2000-05-01 13:42:38 +04:00
|
|
|
Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
|
2000-05-09 08:53:16 +04:00
|
|
|
Copyright (C) 2000 Information-technology Promotion Agency, Japan
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-05-01 13:42:38 +04:00
|
|
|
**********************************************************************/
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
#include "ruby.h"
|
1999-01-20 07:59:39 +03:00
|
|
|
#include "util.h"
|
2000-12-05 12:36:54 +03:00
|
|
|
#include "st.h"
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE rb_cArray;
|
2001-12-10 10:18:16 +03:00
|
|
|
static ID id_cmp;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
#define ARY_DEFAULT_SIZE 16
|
|
|
|
|
|
|
|
void
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_mem_clear(mem, size)
|
1998-01-16 15:13:05 +03:00
|
|
|
register VALUE *mem;
|
2000-11-20 10:31:55 +03:00
|
|
|
register long size;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
while (size--) {
|
|
|
|
*mem++ = Qnil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static void
|
|
|
|
memfill(mem, size, val)
|
|
|
|
register VALUE *mem;
|
2000-11-20 10:31:55 +03:00
|
|
|
register long size;
|
1999-01-20 07:59:39 +03:00
|
|
|
register VALUE val;
|
|
|
|
{
|
|
|
|
while (size--) {
|
|
|
|
*mem++ = val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-02-01 06:12:21 +03:00
|
|
|
#define ARY_TMPLOCK FL_USER1
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2003-01-08 09:05:08 +03:00
|
|
|
static inline void
|
2001-12-10 10:18:16 +03:00
|
|
|
rb_ary_modify_check(ary)
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE ary;
|
|
|
|
{
|
2000-02-01 06:12:21 +03:00
|
|
|
if (OBJ_FROZEN(ary)) rb_error_frozen("array");
|
1999-01-20 07:59:39 +03:00
|
|
|
if (FL_TEST(ary, ARY_TMPLOCK))
|
2003-08-01 06:52:21 +04:00
|
|
|
rb_raise(rb_eTypeError, "can't modify array during iteration");
|
1999-12-14 09:50:43 +03:00
|
|
|
if (!OBJ_TAINTED(ary) && rb_safe_level() >= 4)
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_raise(rb_eSecurityError, "Insecure: can't modify array");
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2001-12-10 10:18:16 +03:00
|
|
|
static void
|
|
|
|
rb_ary_modify(ary)
|
|
|
|
VALUE ary;
|
|
|
|
{
|
|
|
|
VALUE *ptr;
|
|
|
|
|
|
|
|
rb_ary_modify_check(ary);
|
2002-05-21 09:39:19 +04:00
|
|
|
if (FL_TEST(ary, ELTS_SHARED)) {
|
|
|
|
ptr = ALLOC_N(VALUE, RARRAY(ary)->len);
|
|
|
|
FL_UNSET(ary, ELTS_SHARED);
|
|
|
|
RARRAY(ary)->aux.capa = RARRAY(ary)->len;
|
|
|
|
MEMCPY(ptr, RARRAY(ary)->ptr, VALUE, RARRAY(ary)->len);
|
|
|
|
RARRAY(ary)->ptr = ptr;
|
|
|
|
}
|
2001-12-10 10:18:16 +03:00
|
|
|
}
|
2000-02-17 10:11:22 +03:00
|
|
|
VALUE
|
|
|
|
rb_ary_freeze(ary)
|
|
|
|
VALUE ary;
|
|
|
|
{
|
|
|
|
return rb_obj_freeze(ary);
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_frozen_p(ary)
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE ary;
|
|
|
|
{
|
2000-02-01 06:12:21 +03:00
|
|
|
if (FL_TEST(ary, FL_FREEZE|ARY_TMPLOCK))
|
1999-01-20 07:59:39 +03:00
|
|
|
return Qtrue;
|
|
|
|
return Qfalse;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
* array.c (ary_alloc), dir.c (dir_s_alloc), eval.c (thgroup_s_alloc),
file.c (rb_stat_s_alloc), hash.c (hash_alloc), io.c (io_alloc),
object.c (rb_module_s_alloc, rb_class_allocate_instance),
re.c (match_alloc, rb_reg_s_alloc), string.c (str_alloc),
time.c (time_s_alloc), ext/digest/digest.c (rb_digest_base_alloc),
ext/tcltklib/tcltklib.c (ip_alloc),
ext/win32ole/win32ole.c (fole_s_allocate, fev_s_allocate)
: add prototype to get rid of VC++ warnings.
* ext/sdbm/init.c (fsdbm_alloc): allocator takes only one argument.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3197 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-12-21 21:02:01 +03:00
|
|
|
static VALUE ary_alloc _((VALUE));
|
2001-10-03 11:19:19 +04:00
|
|
|
static VALUE
|
2002-12-20 11:33:17 +03:00
|
|
|
ary_alloc(klass)
|
2001-10-03 11:19:19 +04:00
|
|
|
VALUE klass;
|
|
|
|
{
|
|
|
|
NEWOBJ(ary, struct RArray);
|
2001-10-05 10:30:42 +04:00
|
|
|
OBJSETUP(ary, klass, T_ARRAY);
|
2001-10-03 11:19:19 +04:00
|
|
|
|
|
|
|
ary->len = 0;
|
|
|
|
ary->ptr = 0;
|
2001-12-10 10:18:16 +03:00
|
|
|
ary->aux.capa = 0;
|
2001-10-03 11:19:19 +04:00
|
|
|
|
|
|
|
return (VALUE)ary;
|
|
|
|
}
|
|
|
|
|
2001-12-10 10:18:16 +03:00
|
|
|
static VALUE
|
|
|
|
ary_new(klass, len)
|
2001-10-16 07:27:23 +04:00
|
|
|
VALUE klass;
|
1999-08-13 09:45:20 +04:00
|
|
|
long len;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2002-12-20 11:33:17 +03:00
|
|
|
VALUE ary = ary_alloc(klass);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
if (len < 0) {
|
|
|
|
rb_raise(rb_eArgError, "negative array size (or size too big)");
|
|
|
|
}
|
2002-08-19 09:56:09 +04:00
|
|
|
if (len > 0 && len * sizeof(VALUE) <= len) {
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_raise(rb_eArgError, "array size too big");
|
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
if (len == 0) len++;
|
2001-10-03 11:19:19 +04:00
|
|
|
RARRAY(ary)->ptr = ALLOC_N(VALUE, len);
|
2001-12-10 10:18:16 +03:00
|
|
|
RARRAY(ary)->aux.capa = len;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2001-10-03 11:19:19 +04:00
|
|
|
return ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2001-10-16 07:27:23 +04:00
|
|
|
VALUE
|
|
|
|
rb_ary_new2(len)
|
|
|
|
long len;
|
|
|
|
{
|
2001-12-10 10:18:16 +03:00
|
|
|
return ary_new(rb_cArray, len);
|
2001-10-16 07:27:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_new()
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_ary_new2(ARY_DEFAULT_SIZE);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
#ifdef HAVE_STDARG_PROTOTYPES
|
|
|
|
#include <stdarg.h>
|
|
|
|
#define va_init_list(a,b) va_start(a,b)
|
|
|
|
#else
|
1998-01-16 15:13:05 +03:00
|
|
|
#include <varargs.h>
|
1999-01-20 07:59:39 +03:00
|
|
|
#define va_init_list(a,b) va_start(a)
|
|
|
|
#endif
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
#ifdef HAVE_STDARG_PROTOTYPES
|
1999-08-13 09:45:20 +04:00
|
|
|
rb_ary_new3(long n, ...)
|
1999-01-20 07:59:39 +03:00
|
|
|
#else
|
|
|
|
rb_ary_new3(n, va_alist)
|
1999-08-13 09:45:20 +04:00
|
|
|
long n;
|
1998-01-16 15:13:05 +03:00
|
|
|
va_dcl
|
1999-01-20 07:59:39 +03:00
|
|
|
#endif
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
va_list ar;
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE ary;
|
1999-08-13 09:45:20 +04:00
|
|
|
long i;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2002-05-21 09:39:19 +04:00
|
|
|
ary = rb_ary_new2(n);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
va_init_list(ar, n);
|
1998-01-16 15:13:05 +03:00
|
|
|
for (i=0; i<n; i++) {
|
1998-01-16 15:19:22 +03:00
|
|
|
RARRAY(ary)->ptr[i] = va_arg(ar, VALUE);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
va_end(ar);
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
RARRAY(ary)->len = n;
|
|
|
|
return ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_new4(n, elts)
|
1999-08-13 09:45:20 +04:00
|
|
|
long n;
|
2002-04-18 12:46:18 +04:00
|
|
|
const VALUE *elts;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
ary = rb_ary_new2(n);
|
2001-10-03 11:19:19 +04:00
|
|
|
if (n > 0 && elts) {
|
1999-01-20 07:59:39 +03:00
|
|
|
MEMCPY(RARRAY(ary)->ptr, elts, VALUE, n);
|
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
RARRAY(ary)->len = n;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
return ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_assoc_new(car, cdr)
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE car, cdr;
|
|
|
|
{
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
ary = rb_ary_new2(2);
|
1998-01-16 15:19:22 +03:00
|
|
|
RARRAY(ary)->ptr[0] = car;
|
|
|
|
RARRAY(ary)->ptr[1] = cdr;
|
|
|
|
RARRAY(ary)->len = 2;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
return ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2001-10-02 08:31:23 +04:00
|
|
|
static VALUE
|
|
|
|
to_ary(ary)
|
|
|
|
VALUE ary;
|
|
|
|
{
|
|
|
|
return rb_convert_type(ary, T_ARRAY, "Array", "to_ary");
|
|
|
|
}
|
|
|
|
|
2002-12-19 12:20:20 +03:00
|
|
|
VALUE
|
|
|
|
rb_check_array_type(ary)
|
|
|
|
VALUE ary;
|
|
|
|
{
|
|
|
|
return rb_check_convert_type(ary, T_ARRAY, "Array", "to_ary");
|
|
|
|
}
|
|
|
|
|
2001-10-02 08:31:23 +04:00
|
|
|
static VALUE rb_ary_replace _((VALUE, VALUE));
|
|
|
|
|
2000-02-29 11:05:32 +03:00
|
|
|
static VALUE
|
|
|
|
rb_ary_initialize(argc, argv, ary)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE ary;
|
|
|
|
{
|
|
|
|
long len;
|
|
|
|
VALUE size, val;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2001-10-02 08:31:23 +04:00
|
|
|
rb_ary_modify(ary);
|
1999-01-20 07:59:39 +03:00
|
|
|
if (rb_scan_args(argc, argv, "02", &size, &val) == 0) {
|
2001-10-02 08:31:23 +04:00
|
|
|
RARRAY(ary)->len = 0;
|
2002-01-28 11:44:45 +03:00
|
|
|
if (rb_block_given_p()) {
|
|
|
|
rb_warning("given block not used");
|
|
|
|
}
|
2001-10-02 08:31:23 +04:00
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
2002-01-28 11:44:45 +03:00
|
|
|
if (argc == 1 && !FIXNUM_P(size)) {
|
2002-12-19 12:20:20 +03:00
|
|
|
val = rb_check_array_type(size);
|
2002-01-28 11:44:45 +03:00
|
|
|
if (!NIL_P(val)) {
|
|
|
|
rb_ary_replace(ary, val);
|
|
|
|
return ary;
|
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
|
2000-02-29 11:05:32 +03:00
|
|
|
len = NUM2LONG(size);
|
|
|
|
if (len < 0) {
|
|
|
|
rb_raise(rb_eArgError, "negative array size");
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
2002-08-19 09:56:09 +04:00
|
|
|
if (len > 0 && len * (long)sizeof(VALUE) <= len) {
|
2000-02-29 11:05:32 +03:00
|
|
|
rb_raise(rb_eArgError, "array size too big");
|
|
|
|
}
|
2001-12-10 10:18:16 +03:00
|
|
|
if (len > RARRAY(ary)->aux.capa) {
|
2002-05-21 09:39:19 +04:00
|
|
|
REALLOC_N(RARRAY(ary)->ptr, VALUE, len);
|
2002-08-27 12:05:06 +04:00
|
|
|
RARRAY(ary)->aux.capa = len;
|
2000-02-29 11:05:32 +03:00
|
|
|
}
|
2002-01-28 11:44:45 +03:00
|
|
|
if (rb_block_given_p()) {
|
|
|
|
long i;
|
|
|
|
|
|
|
|
if (argc > 1) {
|
|
|
|
rb_raise(rb_eArgError, "wrong number of arguments");
|
|
|
|
}
|
|
|
|
for (i=0; i<len; i++) {
|
2002-08-21 19:47:54 +04:00
|
|
|
RARRAY(ary)->ptr[i] = rb_yield(LONG2NUM(i));
|
2002-05-21 09:39:19 +04:00
|
|
|
RARRAY(ary)->len = i + 1;
|
2002-01-28 11:44:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
memfill(RARRAY(ary)->ptr, len, val);
|
|
|
|
RARRAY(ary)->len = len;
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-02-29 11:05:32 +03:00
|
|
|
return ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_s_create(argc, argv, klass)
|
1998-01-16 15:13:05 +03:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE klass;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2002-12-20 11:33:17 +03:00
|
|
|
VALUE ary = ary_alloc(klass);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2002-05-21 09:39:19 +04:00
|
|
|
if (argc < 0) {
|
|
|
|
rb_raise(rb_eArgError, "negative number of arguments");
|
|
|
|
}
|
|
|
|
if (argc > 0) {
|
2001-10-03 11:19:19 +04:00
|
|
|
RARRAY(ary)->ptr = ALLOC_N(VALUE, argc);
|
|
|
|
MEMCPY(RARRAY(ary)->ptr, argv, VALUE, argc);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2001-12-10 10:18:16 +03:00
|
|
|
RARRAY(ary)->len = RARRAY(ary)->aux.capa = argc;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2001-10-03 11:19:19 +04:00
|
|
|
return ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_store(ary, idx, val)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE ary;
|
1999-08-13 09:45:20 +04:00
|
|
|
long idx;
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE val;
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_modify(ary);
|
1998-01-16 15:13:05 +03:00
|
|
|
if (idx < 0) {
|
1999-08-13 09:45:20 +04:00
|
|
|
idx += RARRAY(ary)->len;
|
1999-01-20 07:59:39 +03:00
|
|
|
if (idx < 0) {
|
2002-05-28 22:11:07 +04:00
|
|
|
rb_raise(rb_eIndexError, "index %ld out of array",
|
2002-05-21 09:39:19 +04:00
|
|
|
idx - RARRAY(ary)->len);
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2001-12-10 10:18:16 +03:00
|
|
|
if (idx >= RARRAY(ary)->aux.capa) {
|
2002-08-13 13:21:18 +04:00
|
|
|
long new_capa = RARRAY(ary)->aux.capa / 2;
|
|
|
|
|
|
|
|
if (new_capa < ARY_DEFAULT_SIZE) {
|
|
|
|
new_capa = ARY_DEFAULT_SIZE;
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
2002-08-13 13:21:18 +04:00
|
|
|
new_capa += idx;
|
2002-08-19 09:56:09 +04:00
|
|
|
if (new_capa * (long)sizeof(VALUE) <= new_capa) {
|
2002-08-13 13:21:18 +04:00
|
|
|
rb_raise(rb_eArgError, "index too big");
|
|
|
|
}
|
2002-08-27 12:05:06 +04:00
|
|
|
REALLOC_N(RARRAY(ary)->ptr, VALUE, new_capa);
|
2002-08-13 13:21:18 +04:00
|
|
|
RARRAY(ary)->aux.capa = new_capa;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
if (idx > RARRAY(ary)->len) {
|
2002-08-27 12:05:06 +04:00
|
|
|
rb_mem_clear(RARRAY(ary)->ptr + RARRAY(ary)->len,
|
2002-08-13 13:21:18 +04:00
|
|
|
idx-RARRAY(ary)->len + 1);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
if (idx >= RARRAY(ary)->len) {
|
|
|
|
RARRAY(ary)->len = idx + 1;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
RARRAY(ary)->ptr[idx] = val;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_push(ary, item)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE item;
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_store(ary, RARRAY(ary)->len, item);
|
1998-01-16 15:19:22 +03:00
|
|
|
return ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2000-01-05 07:41:21 +03:00
|
|
|
rb_ary_push_m(argc, argv, ary)
|
1998-01-16 15:13:05 +03:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2002-05-21 09:39:19 +04:00
|
|
|
while (argc--) {
|
|
|
|
rb_ary_push(ary, *argv++);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
return ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_pop(ary)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2001-12-10 10:18:16 +03:00
|
|
|
rb_ary_modify_check(ary);
|
1998-01-16 15:19:22 +03:00
|
|
|
if (RARRAY(ary)->len == 0) return Qnil;
|
2001-12-13 11:19:09 +03:00
|
|
|
if (!FL_TEST(ary, ELTS_SHARED) &&
|
2002-05-21 09:39:19 +04:00
|
|
|
RARRAY(ary)->len * 2 < RARRAY(ary)->aux.capa &&
|
|
|
|
RARRAY(ary)->aux.capa > ARY_DEFAULT_SIZE) {
|
2001-12-10 10:18:16 +03:00
|
|
|
RARRAY(ary)->aux.capa = RARRAY(ary)->len * 2;
|
|
|
|
REALLOC_N(RARRAY(ary)->ptr, VALUE, RARRAY(ary)->aux.capa);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
return RARRAY(ary)->ptr[--RARRAY(ary)->len];
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2001-12-10 10:18:16 +03:00
|
|
|
static void
|
|
|
|
ary_make_shared(ary)
|
|
|
|
VALUE ary;
|
|
|
|
{
|
2002-05-21 09:39:19 +04:00
|
|
|
if (!FL_TEST(ary, ELTS_SHARED)) {
|
2001-12-10 10:18:16 +03:00
|
|
|
NEWOBJ(shared, struct RArray);
|
|
|
|
OBJSETUP(shared, rb_cArray, T_ARRAY);
|
|
|
|
|
|
|
|
shared->len = RARRAY(ary)->len;
|
|
|
|
shared->ptr = RARRAY(ary)->ptr;
|
|
|
|
shared->aux.capa = RARRAY(ary)->aux.capa;
|
|
|
|
RARRAY(ary)->aux.shared = (VALUE)shared;
|
|
|
|
FL_SET(ary, ELTS_SHARED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_shift(ary)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
VALUE top;
|
|
|
|
|
2001-12-10 10:18:16 +03:00
|
|
|
rb_ary_modify_check(ary);
|
1998-01-16 15:19:22 +03:00
|
|
|
if (RARRAY(ary)->len == 0) return Qnil;
|
|
|
|
top = RARRAY(ary)->ptr[0];
|
2001-12-10 10:18:16 +03:00
|
|
|
ary_make_shared(ary);
|
|
|
|
RARRAY(ary)->ptr++; /* shift ptr */
|
1998-01-16 15:19:22 +03:00
|
|
|
RARRAY(ary)->len--;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
return top;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_unshift(ary, item)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE ary, item;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_modify(ary);
|
2002-03-08 10:03:09 +03:00
|
|
|
if (RARRAY(ary)->len == RARRAY(ary)->aux.capa) {
|
2001-12-10 10:18:16 +03:00
|
|
|
long capa_inc = RARRAY(ary)->aux.capa / 2;
|
1999-08-13 09:45:20 +04:00
|
|
|
if (capa_inc < ARY_DEFAULT_SIZE) {
|
|
|
|
capa_inc = ARY_DEFAULT_SIZE;
|
|
|
|
}
|
2002-05-21 09:39:19 +04:00
|
|
|
RARRAY(ary)->aux.capa += capa_inc;
|
2001-12-10 10:18:16 +03:00
|
|
|
REALLOC_N(RARRAY(ary)->ptr, VALUE, RARRAY(ary)->aux.capa);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* sliding items */
|
2002-05-21 09:39:19 +04:00
|
|
|
MEMMOVE(RARRAY(ary)->ptr + 1, RARRAY(ary)->ptr, VALUE, RARRAY(ary)->len);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
RARRAY(ary)->len++;
|
1999-01-20 07:59:39 +03:00
|
|
|
RARRAY(ary)->ptr[0] = item;
|
|
|
|
|
|
|
|
return ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2000-05-01 13:42:38 +04:00
|
|
|
static VALUE
|
|
|
|
rb_ary_unshift_m(argc, argv, ary)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE ary;
|
|
|
|
{
|
2002-05-21 09:39:19 +04:00
|
|
|
long len = RARRAY(ary)->len;
|
2003-06-23 21:40:14 +04:00
|
|
|
|
|
|
|
if (argc < 0) {
|
|
|
|
rb_raise(rb_eArgError, "negative number of arguments");
|
|
|
|
}
|
|
|
|
if (argc == 0) return ary;
|
|
|
|
|
2002-05-21 09:39:19 +04:00
|
|
|
/* make rooms by setting the last item */
|
|
|
|
rb_ary_store(ary, len + argc - 1, Qnil);
|
2000-05-01 13:42:38 +04:00
|
|
|
|
2002-05-21 09:39:19 +04:00
|
|
|
/* sliding items */
|
|
|
|
MEMMOVE(RARRAY(ary)->ptr + argc, RARRAY(ary)->ptr, VALUE, len);
|
|
|
|
MEMCPY(RARRAY(ary)->ptr, argv, VALUE, argc);
|
|
|
|
|
2000-05-01 13:42:38 +04:00
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_entry(ary, offset)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE ary;
|
1999-08-13 09:45:20 +04:00
|
|
|
long offset;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1998-01-16 15:19:22 +03:00
|
|
|
if (RARRAY(ary)->len == 0) return Qnil;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
if (offset < 0) {
|
2001-02-02 14:38:20 +03:00
|
|
|
offset += RARRAY(ary)->len;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
if (offset < 0 || RARRAY(ary)->len <= offset) {
|
1998-01-16 15:13:05 +03:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
return RARRAY(ary)->ptr[offset];
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2000-02-01 06:12:21 +03:00
|
|
|
rb_ary_subseq(ary, beg, len)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE ary;
|
1999-08-13 09:45:20 +04:00
|
|
|
long beg, len;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2001-10-16 07:27:23 +04:00
|
|
|
VALUE klass, ary2;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2001-02-19 12:15:27 +03:00
|
|
|
if (beg > RARRAY(ary)->len) return Qnil;
|
2000-11-20 10:31:55 +03:00
|
|
|
if (beg < 0 || len < 0) return Qnil;
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
if (beg + len > RARRAY(ary)->len) {
|
|
|
|
len = RARRAY(ary)->len - beg;
|
2002-05-21 09:39:19 +04:00
|
|
|
if (len < 0)
|
|
|
|
len = 0;
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
2001-10-16 07:27:23 +04:00
|
|
|
klass = rb_obj_class(ary);
|
2002-05-21 09:39:19 +04:00
|
|
|
if (len == 0) return ary_new(klass, 0);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2001-12-10 10:18:16 +03:00
|
|
|
ary_make_shared(ary);
|
2002-12-20 11:33:17 +03:00
|
|
|
ary2 = ary_alloc(klass);
|
2002-05-21 09:39:19 +04:00
|
|
|
RARRAY(ary2)->ptr = RARRAY(ary)->ptr + beg;
|
1998-01-16 15:19:22 +03:00
|
|
|
RARRAY(ary2)->len = len;
|
2001-12-10 10:18:16 +03:00
|
|
|
RARRAY(ary2)->aux.shared = RARRAY(ary)->aux.shared;
|
|
|
|
FL_SET(ary2, ELTS_SHARED);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
return ary2;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_aref(argc, argv, ary)
|
1998-01-16 15:13:05 +03:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2001-07-02 12:46:28 +04:00
|
|
|
VALUE arg;
|
1999-08-13 09:45:20 +04:00
|
|
|
long beg, len;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2001-07-02 12:46:28 +04:00
|
|
|
if (argc == 2) {
|
2003-03-04 12:31:02 +03:00
|
|
|
if (SYMBOL_P(argv[0])) {
|
|
|
|
rb_raise(rb_eTypeError, "Symbol as array index");
|
|
|
|
}
|
2001-07-02 12:46:28 +04:00
|
|
|
beg = NUM2LONG(argv[0]);
|
|
|
|
len = NUM2LONG(argv[1]);
|
1999-01-20 07:59:39 +03:00
|
|
|
if (beg < 0) {
|
2001-02-02 14:38:20 +03:00
|
|
|
beg += RARRAY(ary)->len;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2000-02-01 06:12:21 +03:00
|
|
|
return rb_ary_subseq(ary, beg, len);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2001-07-02 12:46:28 +04:00
|
|
|
if (argc != 1) {
|
|
|
|
rb_scan_args(argc, argv, "11", 0, 0);
|
|
|
|
}
|
|
|
|
arg = argv[0];
|
1998-01-16 15:13:05 +03:00
|
|
|
/* special case - speeding up */
|
2001-07-02 12:46:28 +04:00
|
|
|
if (FIXNUM_P(arg)) {
|
|
|
|
return rb_ary_entry(ary, FIX2LONG(arg));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2003-03-04 12:31:02 +03:00
|
|
|
if (SYMBOL_P(arg)) {
|
|
|
|
rb_raise(rb_eTypeError, "Symbol as array index");
|
|
|
|
}
|
2002-05-21 09:39:19 +04:00
|
|
|
/* check if idx is Range */
|
|
|
|
switch (rb_range_beg_len(arg, &beg, &len, RARRAY(ary)->len, 0)) {
|
|
|
|
case Qfalse:
|
|
|
|
break;
|
|
|
|
case Qnil:
|
|
|
|
return Qnil;
|
|
|
|
default:
|
|
|
|
return rb_ary_subseq(ary, beg, len);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2001-07-02 12:46:28 +04:00
|
|
|
return rb_ary_entry(ary, NUM2LONG(arg));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
1999-10-13 10:44:42 +04:00
|
|
|
static VALUE
|
|
|
|
rb_ary_at(ary, pos)
|
|
|
|
VALUE ary, pos;
|
|
|
|
{
|
|
|
|
return rb_ary_entry(ary, NUM2LONG(pos));
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2003-06-06 13:24:59 +04:00
|
|
|
rb_ary_first(argc, argv, ary)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
1999-10-13 10:44:42 +04:00
|
|
|
VALUE ary;
|
|
|
|
{
|
2003-06-06 13:24:59 +04:00
|
|
|
if (argc == 0) {
|
|
|
|
if (RARRAY(ary)->len == 0) return Qnil;
|
|
|
|
return RARRAY(ary)->ptr[0];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
VALUE nv, result;
|
|
|
|
long n, i;
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "01", &nv);
|
|
|
|
n = NUM2LONG(nv);
|
|
|
|
if (n > RARRAY(ary)->len) n = RARRAY(ary)->len;
|
|
|
|
result = rb_ary_new2(n);
|
|
|
|
for (i=0; i<n; i++) {
|
|
|
|
rb_ary_push(result, RARRAY(ary)->ptr[i]);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
1999-10-13 10:44:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2003-06-06 13:24:59 +04:00
|
|
|
rb_ary_last(argc, argv, ary)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
1999-10-13 10:44:42 +04:00
|
|
|
VALUE ary;
|
|
|
|
{
|
2003-06-06 13:24:59 +04:00
|
|
|
if (argc == 0) {
|
|
|
|
if (RARRAY(ary)->len == 0) return Qnil;
|
|
|
|
return RARRAY(ary)->ptr[RARRAY(ary)->len-1];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
VALUE nv, result;
|
|
|
|
long n, i;
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "01", &nv);
|
|
|
|
n = NUM2LONG(nv);
|
|
|
|
if (n > RARRAY(ary)->len) n = RARRAY(ary)->len;
|
|
|
|
result = rb_ary_new2(n);
|
|
|
|
for (i=RARRAY(ary)->len-n; n--; i++) {
|
|
|
|
rb_ary_push(result, RARRAY(ary)->ptr[i]);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
1999-10-13 10:44:42 +04:00
|
|
|
}
|
|
|
|
|
2001-02-02 14:38:20 +03:00
|
|
|
static VALUE
|
|
|
|
rb_ary_fetch(argc, argv, ary)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE ary;
|
|
|
|
{
|
|
|
|
VALUE pos, ifnone;
|
|
|
|
long idx;
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "11", &pos, &ifnone);
|
|
|
|
idx = NUM2LONG(pos);
|
|
|
|
|
|
|
|
if (idx < 0) {
|
|
|
|
idx += RARRAY(ary)->len;
|
|
|
|
}
|
|
|
|
if (idx < 0 || RARRAY(ary)->len <= idx) {
|
2002-01-25 11:22:11 +03:00
|
|
|
if (rb_block_given_p()) {
|
|
|
|
if (argc > 1) {
|
|
|
|
rb_raise(rb_eArgError, "wrong number of arguments");
|
|
|
|
}
|
|
|
|
return rb_yield(pos);
|
|
|
|
}
|
|
|
|
if (argc == 1) {
|
2002-05-28 22:11:07 +04:00
|
|
|
rb_raise(rb_eIndexError, "index %ld out of array", idx);
|
2002-01-25 11:22:11 +03:00
|
|
|
}
|
|
|
|
return ifnone;
|
2001-02-02 14:38:20 +03:00
|
|
|
}
|
|
|
|
return RARRAY(ary)->ptr[idx];
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_index(ary, val)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE val;
|
|
|
|
{
|
1999-08-13 09:45:20 +04:00
|
|
|
long i;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
for (i=0; i<RARRAY(ary)->len; i++) {
|
|
|
|
if (rb_equal(RARRAY(ary)->ptr[i], val))
|
2002-05-21 09:39:19 +04:00
|
|
|
return LONG2NUM(i);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_rindex(ary, val)
|
|
|
|
VALUE ary;
|
|
|
|
VALUE val;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-08-13 09:45:20 +04:00
|
|
|
long i = RARRAY(ary)->len;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
while (i--) {
|
|
|
|
if (rb_equal(RARRAY(ary)->ptr[i], val))
|
2002-08-21 19:47:54 +04:00
|
|
|
return LONG2NUM(i);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
return Qnil;
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static VALUE
|
|
|
|
rb_ary_indexes(argc, argv, ary)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE ary;
|
|
|
|
{
|
|
|
|
VALUE new_ary;
|
1999-08-13 09:45:20 +04:00
|
|
|
long i;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2003-05-04 20:03:24 +04:00
|
|
|
rb_warn("Array#%s is deprecated; use Array#values_at",
|
2001-12-11 06:48:08 +03:00
|
|
|
rb_id2name(rb_frame_last_func()));
|
1999-01-20 07:59:39 +03:00
|
|
|
new_ary = rb_ary_new2(argc);
|
|
|
|
for (i=0; i<argc; i++) {
|
1999-08-13 09:45:20 +04:00
|
|
|
rb_ary_push(new_ary, rb_ary_aref(1, argv+i, ary));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
return new_ary;
|
|
|
|
}
|
|
|
|
|
2001-10-02 08:31:23 +04:00
|
|
|
VALUE
|
|
|
|
rb_ary_to_ary(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
|
|
|
if (TYPE(obj) == T_ARRAY) {
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
if (rb_respond_to(obj, rb_intern("to_ary"))) {
|
|
|
|
return rb_convert_type(obj, T_ARRAY, "Array", "to_ary");
|
|
|
|
}
|
|
|
|
return rb_ary_new3(1, obj);
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static void
|
2001-05-06 19:06:00 +04:00
|
|
|
rb_ary_update(ary, beg, len, rpl)
|
2001-05-02 08:22:21 +04:00
|
|
|
VALUE ary;
|
1999-08-13 09:45:20 +04:00
|
|
|
long beg, len;
|
2001-05-06 19:06:00 +04:00
|
|
|
VALUE rpl;
|
2001-05-02 08:22:21 +04:00
|
|
|
{
|
2001-05-06 19:06:00 +04:00
|
|
|
long rlen;
|
|
|
|
|
2002-05-28 22:11:07 +04:00
|
|
|
if (len < 0) rb_raise(rb_eIndexError, "negative length (%ld)", len);
|
1999-08-13 09:45:20 +04:00
|
|
|
if (beg < 0) {
|
|
|
|
beg += RARRAY(ary)->len;
|
2002-05-21 09:39:19 +04:00
|
|
|
if (beg < 0) {
|
|
|
|
beg -= RARRAY(ary)->len;
|
2002-05-28 22:11:07 +04:00
|
|
|
rb_raise(rb_eIndexError, "index %ld out of array", beg);
|
2002-05-21 09:39:19 +04:00
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
|
|
|
if (beg + len > RARRAY(ary)->len) {
|
|
|
|
len = RARRAY(ary)->len - beg;
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
rb_ary_modify(ary);
|
2003-01-07 10:36:40 +03:00
|
|
|
if (NIL_P(rpl)) {
|
|
|
|
rlen = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rpl = rb_ary_to_ary(rpl);
|
|
|
|
rlen = RARRAY(rpl)->len;
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
if (beg >= RARRAY(ary)->len) {
|
2000-10-25 08:48:41 +04:00
|
|
|
len = beg + rlen;
|
2001-12-10 10:18:16 +03:00
|
|
|
if (len >= RARRAY(ary)->aux.capa) {
|
2002-05-21 09:39:19 +04:00
|
|
|
REALLOC_N(RARRAY(ary)->ptr, VALUE, len);
|
2002-08-27 12:05:06 +04:00
|
|
|
RARRAY(ary)->aux.capa = len;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2002-05-21 09:39:19 +04:00
|
|
|
rb_mem_clear(RARRAY(ary)->ptr + RARRAY(ary)->len, beg - RARRAY(ary)->len);
|
2003-01-07 10:36:40 +03:00
|
|
|
if (rlen > 0) {
|
|
|
|
MEMCPY(RARRAY(ary)->ptr + beg, RARRAY(rpl)->ptr, VALUE, rlen);
|
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
RARRAY(ary)->len = len;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
else {
|
1999-08-13 09:45:20 +04:00
|
|
|
long alen;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
if (beg + len > RARRAY(ary)->len) {
|
|
|
|
len = RARRAY(ary)->len - beg;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2000-10-25 08:48:41 +04:00
|
|
|
alen = RARRAY(ary)->len + rlen - len;
|
2001-12-10 10:18:16 +03:00
|
|
|
if (alen >= RARRAY(ary)->aux.capa) {
|
2002-05-21 09:39:19 +04:00
|
|
|
REALLOC_N(RARRAY(ary)->ptr, VALUE, alen);
|
2002-08-27 12:05:06 +04:00
|
|
|
RARRAY(ary)->aux.capa = alen;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2001-05-02 08:22:21 +04:00
|
|
|
if (len != rlen) {
|
2002-05-21 09:39:19 +04:00
|
|
|
MEMMOVE(RARRAY(ary)->ptr + beg + rlen, RARRAY(ary)->ptr + beg + len,
|
|
|
|
VALUE, RARRAY(ary)->len - (beg + len));
|
1998-01-16 15:19:22 +03:00
|
|
|
RARRAY(ary)->len = alen;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2003-01-07 10:36:40 +03:00
|
|
|
if (rlen > 0) {
|
|
|
|
MEMMOVE(RARRAY(ary)->ptr + beg, RARRAY(rpl)->ptr, VALUE, rlen);
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_aset(argc, argv, ary)
|
1998-01-16 15:13:05 +03:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-08-13 09:45:20 +04:00
|
|
|
long offset, beg, len;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-05-24 08:34:26 +04:00
|
|
|
if (argc == 3) {
|
2003-03-04 12:31:02 +03:00
|
|
|
if (SYMBOL_P(argv[0])) {
|
|
|
|
rb_raise(rb_eTypeError, "Symbol as array index");
|
|
|
|
}
|
|
|
|
if (SYMBOL_P(argv[1])) {
|
|
|
|
rb_raise(rb_eTypeError, "Symbol as subarray length");
|
|
|
|
}
|
2001-05-06 19:06:00 +04:00
|
|
|
rb_ary_update(ary, NUM2LONG(argv[0]), NUM2LONG(argv[1]), argv[2]);
|
2000-05-24 08:34:26 +04:00
|
|
|
return argv[2];
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2000-05-24 08:34:26 +04:00
|
|
|
if (argc != 2) {
|
2002-05-21 09:39:19 +04:00
|
|
|
rb_raise(rb_eArgError, "wrong number of arguments (%d for 2)", argc);
|
2000-05-24 08:34:26 +04:00
|
|
|
}
|
|
|
|
if (FIXNUM_P(argv[0])) {
|
|
|
|
offset = FIX2LONG(argv[0]);
|
1998-01-16 15:13:05 +03:00
|
|
|
goto fixnum;
|
|
|
|
}
|
2003-03-04 12:31:02 +03:00
|
|
|
if (SYMBOL_P(argv[0])) {
|
|
|
|
rb_raise(rb_eTypeError, "Symbol as array index");
|
|
|
|
}
|
2002-05-21 09:39:19 +04:00
|
|
|
if (rb_range_beg_len(argv[0], &beg, &len, RARRAY(ary)->len, 1)) {
|
1998-01-16 15:13:05 +03:00
|
|
|
/* check if idx is Range */
|
2001-05-06 19:06:00 +04:00
|
|
|
rb_ary_update(ary, beg, len, argv[1]);
|
2000-05-24 08:34:26 +04:00
|
|
|
return argv[1];
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2000-05-24 08:34:26 +04:00
|
|
|
offset = NUM2LONG(argv[0]);
|
2002-05-21 09:39:19 +04:00
|
|
|
fixnum:
|
2000-05-24 08:34:26 +04:00
|
|
|
rb_ary_store(ary, offset, argv[1]);
|
|
|
|
return argv[1];
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2001-05-02 08:22:21 +04:00
|
|
|
static VALUE
|
|
|
|
rb_ary_insert(argc, argv, ary)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE ary;
|
|
|
|
{
|
2001-05-06 19:06:00 +04:00
|
|
|
long pos;
|
|
|
|
|
2003-05-02 10:41:33 +04:00
|
|
|
if (argc < 1) {
|
|
|
|
rb_raise(rb_eArgError, "wrong number of arguments (at least 1)");
|
2001-05-02 08:22:21 +04:00
|
|
|
}
|
2001-05-06 19:06:00 +04:00
|
|
|
pos = NUM2LONG(argv[0]);
|
|
|
|
if (pos == -1) {
|
2002-07-11 12:22:18 +04:00
|
|
|
pos = RARRAY(ary)->len;
|
2001-05-06 19:06:00 +04:00
|
|
|
}
|
|
|
|
else if (pos < 0) {
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
|
2003-05-02 10:41:33 +04:00
|
|
|
if (argc == 1) return ary;
|
2002-05-21 09:39:19 +04:00
|
|
|
rb_ary_update(ary, pos, 0, rb_ary_new4(argc - 1, argv + 1));
|
2001-05-02 08:22:21 +04:00
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_each(ary)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-08-13 09:45:20 +04:00
|
|
|
long i;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
for (i=0; i<RARRAY(ary)->len; i++) {
|
|
|
|
rb_yield(RARRAY(ary)->ptr[i]);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
return ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_each_index(ary)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-08-13 09:45:20 +04:00
|
|
|
long i;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
for (i=0; i<RARRAY(ary)->len; i++) {
|
2002-08-21 19:47:54 +04:00
|
|
|
rb_yield(LONG2NUM(i));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
return ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_reverse_each(ary)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-08-13 09:45:20 +04:00
|
|
|
long len = RARRAY(ary)->len;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
while (len--) {
|
1998-01-16 15:19:22 +03:00
|
|
|
rb_yield(RARRAY(ary)->ptr[len]);
|
2003-08-01 06:52:21 +04:00
|
|
|
if (RARRAY(ary)->len < len) {
|
|
|
|
len = RARRAY(ary)->len;
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
return ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_length(ary)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2002-08-21 19:47:54 +04:00
|
|
|
return LONG2NUM(RARRAY(ary)->len);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_empty_p(ary)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1998-01-16 15:19:22 +03:00
|
|
|
if (RARRAY(ary)->len == 0)
|
1999-01-20 07:59:39 +03:00
|
|
|
return Qtrue;
|
|
|
|
return Qfalse;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2001-07-14 19:17:19 +04:00
|
|
|
VALUE
|
|
|
|
rb_ary_dup(ary)
|
|
|
|
VALUE ary;
|
|
|
|
{
|
|
|
|
VALUE dup = rb_ary_new2(RARRAY(ary)->len);
|
|
|
|
|
2001-12-10 10:18:16 +03:00
|
|
|
DUPSETUP(dup, ary);
|
2001-07-14 19:17:19 +04:00
|
|
|
MEMCPY(RARRAY(dup)->ptr, RARRAY(ary)->ptr, VALUE, RARRAY(ary)->len);
|
|
|
|
RARRAY(dup)->len = RARRAY(ary)->len;
|
|
|
|
return dup;
|
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
extern VALUE rb_output_fs;
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
inspect_join(ary, arg)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE ary;
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE *arg;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_ary_join(arg[0], arg[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_ary_join(ary, sep)
|
|
|
|
VALUE ary, sep;
|
|
|
|
{
|
2002-05-21 09:39:19 +04:00
|
|
|
long len = 1, i;
|
|
|
|
int taint = Qfalse;
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE result, tmp;
|
2000-01-05 07:41:21 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
if (RARRAY(ary)->len == 0) return rb_str_new(0, 0);
|
2002-05-21 09:39:19 +04:00
|
|
|
if (OBJ_TAINTED(ary) || OBJ_TAINTED(sep)) taint = Qtrue;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2001-05-16 13:05:54 +04:00
|
|
|
for (i=0; i<RARRAY(ary)->len; i++) {
|
2003-01-07 10:36:40 +03:00
|
|
|
tmp = rb_check_string_type(RARRAY(ary)->ptr[i]);
|
|
|
|
len += NIL_P(tmp) ? 10 : RSTRING(tmp)->len;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2001-11-15 18:56:08 +03:00
|
|
|
if (!NIL_P(sep)) {
|
|
|
|
StringValue(sep);
|
|
|
|
len += RSTRING(sep)->len * (RARRAY(ary)->len - 1);
|
2001-05-16 13:05:54 +04:00
|
|
|
}
|
2001-05-30 13:12:34 +04:00
|
|
|
result = rb_str_buf_new(len);
|
|
|
|
for (i=0; i<RARRAY(ary)->len; i++) {
|
1998-01-16 15:19:22 +03:00
|
|
|
tmp = RARRAY(ary)->ptr[i];
|
1998-01-16 15:13:05 +03:00
|
|
|
switch (TYPE(tmp)) {
|
|
|
|
case T_STRING:
|
|
|
|
break;
|
|
|
|
case T_ARRAY:
|
1999-01-20 07:59:39 +03: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 15:13:05 +03:00
|
|
|
break;
|
|
|
|
default:
|
1999-01-20 07:59:39 +03:00
|
|
|
tmp = rb_obj_as_string(tmp);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2001-05-30 13:12:34 +04:00
|
|
|
if (i > 0 && !NIL_P(sep))
|
|
|
|
rb_str_buf_append(result, sep);
|
|
|
|
rb_str_buf_append(result, tmp);
|
2002-05-21 09:39:19 +04:00
|
|
|
if (OBJ_TAINTED(tmp)) taint = Qtrue;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2000-01-05 07:41:21 +03:00
|
|
|
if (taint) OBJ_TAINT(result);
|
1998-01-16 15:13:05 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2000-01-05 07:41:21 +03:00
|
|
|
rb_ary_join_m(argc, argv, ary)
|
1998-01-16 15:13:05 +03:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
VALUE sep;
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "01", &sep);
|
1999-01-20 07:59:39 +03:00
|
|
|
if (NIL_P(sep)) sep = rb_output_fs;
|
2002-05-21 09:39:19 +04:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_ary_join(ary, sep);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_to_s(ary)
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE ary;
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
if (RARRAY(ary)->len == 0) return rb_str_new(0, 0);
|
2002-05-21 09:39:19 +04:00
|
|
|
|
|
|
|
return rb_ary_join(ary, rb_output_fs);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static ID inspect_key;
|
|
|
|
|
|
|
|
struct inspect_arg {
|
|
|
|
VALUE (*func)();
|
|
|
|
VALUE arg1, arg2;
|
|
|
|
};
|
|
|
|
|
* intern.h: add prototypes.
rb_gc_enable(), rb_gc_disable(), rb_gc_start(), rb_str_new5()
rb_str_buf_append(), rb_str_buf_cat(), rb_str_buf_cat2(),
rb_str_dup_frozen()
* ruby.h: added declaration.
rb_defout, rb_stdin, rb_stdout, rb_stderr, ruby_errinfo
* rubyio.h: changed double include guard macro to RUBYIO_H.
* array.c (inspect_call): make static.
* eval.c (dvar_asgn): ditto.
* io.c (rb_io_close_read): ditto.
* lex.c (rb_reserved_word): ditto.
* ruby.c: (req_list_head, req_list_last): ditto.
* ruby.c (require_libraries): ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@1915 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2001-12-17 10:52:35 +03:00
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
inspect_call(arg)
|
|
|
|
struct inspect_arg *arg;
|
|
|
|
{
|
|
|
|
return (*arg->func)(arg->arg1, arg->arg2);
|
|
|
|
}
|
|
|
|
|
2003-05-19 08:52:55 +04:00
|
|
|
static VALUE
|
|
|
|
get_inspect_tbl(create)
|
|
|
|
int create;
|
|
|
|
{
|
|
|
|
VALUE inspect_tbl = rb_thread_local_aref(rb_thread_current(), inspect_key);
|
|
|
|
|
2003-05-19 19:45:46 +04:00
|
|
|
if (NIL_P(inspect_tbl)) {
|
|
|
|
if (create) {
|
|
|
|
tbl_init:
|
|
|
|
inspect_tbl = rb_ary_new();
|
|
|
|
rb_thread_local_aset(rb_thread_current(), inspect_key, inspect_tbl);
|
|
|
|
}
|
2003-05-19 08:52:55 +04:00
|
|
|
}
|
|
|
|
else if (TYPE(inspect_tbl) != T_ARRAY) {
|
|
|
|
rb_warn("invalid inspect_tbl value");
|
|
|
|
if (create) goto tbl_init;
|
|
|
|
rb_thread_local_aset(rb_thread_current(), inspect_key, Qnil);
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
return inspect_tbl;
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
inspect_ensure(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
|
|
|
VALUE inspect_tbl;
|
|
|
|
|
2003-05-19 08:52:55 +04:00
|
|
|
inspect_tbl = get_inspect_tbl(Qfalse);
|
|
|
|
if (!NIL_P(inspect_tbl)) {
|
|
|
|
rb_ary_pop(inspect_tbl);
|
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_protect_inspect(func, obj, arg)
|
2001-10-29 08:07:26 +03:00
|
|
|
VALUE (*func)(ANYARGS);
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE obj, arg;
|
|
|
|
{
|
|
|
|
struct inspect_arg iarg;
|
|
|
|
VALUE inspect_tbl;
|
2000-02-01 06:12:21 +03:00
|
|
|
VALUE id;
|
1999-01-20 07:59:39 +03:00
|
|
|
|
2003-05-19 08:52:55 +04:00
|
|
|
inspect_tbl = get_inspect_tbl(Qtrue);
|
2000-02-01 06:12:21 +03:00
|
|
|
id = rb_obj_id(obj);
|
|
|
|
if (rb_ary_includes(inspect_tbl, id)) {
|
2000-01-05 07:41:21 +03:00
|
|
|
return (*func)(obj, arg);
|
|
|
|
}
|
2000-02-01 06:12:21 +03:00
|
|
|
rb_ary_push(inspect_tbl, id);
|
1999-01-20 07:59:39 +03:00
|
|
|
iarg.func = func;
|
|
|
|
iarg.arg1 = obj;
|
|
|
|
iarg.arg2 = arg;
|
2000-01-05 07:41:21 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_ensure(inspect_call, (VALUE)&iarg, inspect_ensure, obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_inspecting_p(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
|
|
|
VALUE inspect_tbl;
|
|
|
|
|
2003-05-19 08:52:55 +04:00
|
|
|
inspect_tbl = get_inspect_tbl(Qfalse);
|
1999-01-20 07:59:39 +03:00
|
|
|
if (NIL_P(inspect_tbl)) return Qfalse;
|
2000-02-01 06:12:21 +03:00
|
|
|
return rb_ary_includes(inspect_tbl, rb_obj_id(obj));
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
inspect_ary(ary)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2000-02-23 08:23:12 +03:00
|
|
|
int tainted = OBJ_TAINTED(ary);
|
2002-05-21 09:39:19 +04:00
|
|
|
long i;
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE s, str;
|
|
|
|
|
2001-05-30 13:12:34 +04:00
|
|
|
str = rb_str_buf_new2("[");
|
1998-01-16 15:19:22 +03:00
|
|
|
for (i=0; i<RARRAY(ary)->len; i++) {
|
|
|
|
s = rb_inspect(RARRAY(ary)->ptr[i]);
|
2002-05-21 09:39:19 +04:00
|
|
|
if (OBJ_TAINTED(s)) tainted = Qtrue;
|
2001-05-30 13:12:34 +04:00
|
|
|
if (i > 0) rb_str_buf_cat2(str, ", ");
|
|
|
|
rb_str_buf_append(str, s);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat2(str, "]");
|
2000-02-23 08:23:12 +03:00
|
|
|
if (tainted) OBJ_TAINT(str);
|
1998-01-16 15:13:05 +03:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03: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 15:13:05 +03:00
|
|
|
VALUE ary;
|
2003-02-10 12:40:13 +03:00
|
|
|
{
|
|
|
|
if (rb_obj_class(ary) != rb_cArray) {
|
|
|
|
VALUE dup = rb_ary_new2(RARRAY(ary)->len);
|
|
|
|
rb_ary_replace(dup, ary);
|
|
|
|
return dup;
|
|
|
|
}
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_ary_to_ary_m(ary)
|
|
|
|
VALUE ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_reverse(ary)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
VALUE *p1, *p2;
|
|
|
|
VALUE tmp;
|
|
|
|
|
1999-10-18 13:03:16 +04:00
|
|
|
rb_ary_modify(ary);
|
2003-09-05 09:07:54 +04:00
|
|
|
if (RARRAY(ary)->len > 1) {
|
|
|
|
p1 = RARRAY(ary)->ptr;
|
|
|
|
p2 = p1 + RARRAY(ary)->len - 1; /* points last item */
|
|
|
|
|
|
|
|
while (p1 < p2) {
|
|
|
|
tmp = *p1;
|
|
|
|
*p1++ = *p2;
|
|
|
|
*p2-- = tmp;
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
return ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2000-08-24 10:21:43 +04:00
|
|
|
static VALUE
|
|
|
|
rb_ary_reverse_bang(ary)
|
|
|
|
VALUE ary;
|
|
|
|
{
|
|
|
|
return rb_ary_reverse(ary);
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
2000-01-05 07:41:21 +03:00
|
|
|
rb_ary_reverse_m(ary)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2002-08-27 12:31:08 +04:00
|
|
|
return rb_ary_reverse(rb_ary_dup(ary));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
sort_1(a, b)
|
|
|
|
VALUE *a, *b;
|
|
|
|
{
|
2003-05-20 10:29:23 +04:00
|
|
|
VALUE retval = rb_yield_values(2, *a, *b);
|
2003-05-19 19:45:46 +04:00
|
|
|
return rb_cmpint(retval, *a, *b);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2002-08-19 09:56:09 +04:00
|
|
|
sort_2(ap, bp)
|
|
|
|
VALUE *ap, *bp;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
VALUE retval;
|
2002-08-19 10:44:09 +04:00
|
|
|
long a = (long)*ap, b = (long)*bp;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2002-08-19 09:56:09 +04:00
|
|
|
if (FIXNUM_P(a) && FIXNUM_P(b)) {
|
2002-08-19 10:41:02 +04:00
|
|
|
if (a > b) return 1;
|
|
|
|
if (a < b) return -1;
|
|
|
|
return 0;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2002-08-19 09:56:09 +04:00
|
|
|
if (TYPE(a) == T_STRING && TYPE(b) == T_STRING) {
|
|
|
|
return rb_str_cmp(a, b);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2002-08-19 09:56:09 +04:00
|
|
|
retval = rb_funcall(a, id_cmp, 1, b);
|
2003-05-19 19:45:46 +04:00
|
|
|
return rb_cmpint(retval, a, b);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static VALUE
|
|
|
|
sort_internal(ary)
|
|
|
|
VALUE ary;
|
|
|
|
{
|
|
|
|
qsort(RARRAY(ary)->ptr, RARRAY(ary)->len, sizeof(VALUE),
|
2000-05-24 08:34:26 +04:00
|
|
|
rb_block_given_p()?sort_1:sort_2);
|
1999-01-20 07:59:39 +03:00
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
sort_unlock(ary)
|
|
|
|
VALUE ary;
|
|
|
|
{
|
|
|
|
FL_UNSET(ary, ARY_TMPLOCK);
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_sort_bang(ary)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-10-16 14:33:06 +04:00
|
|
|
rb_ary_modify(ary);
|
2003-09-05 09:07:54 +04:00
|
|
|
if (RARRAY(ary)->len > 1) {
|
|
|
|
FL_SET(ary, ARY_TMPLOCK); /* prohibit modification during sort */
|
|
|
|
rb_ensure(sort_internal, ary, sort_unlock, ary);
|
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
return ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_sort(ary)
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE ary;
|
|
|
|
{
|
2002-08-27 12:31:08 +04:00
|
|
|
ary = rb_ary_dup(ary);
|
1999-10-20 11:10:23 +04:00
|
|
|
rb_ary_sort_bang(ary);
|
|
|
|
return ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
1999-10-29 13:25:48 +04:00
|
|
|
static VALUE
|
|
|
|
rb_ary_collect(ary)
|
|
|
|
VALUE ary;
|
|
|
|
{
|
2003-07-31 10:30:35 +04:00
|
|
|
long i;
|
1999-10-29 13:25:48 +04:00
|
|
|
VALUE collect;
|
|
|
|
|
2000-05-24 08:34:26 +04:00
|
|
|
if (!rb_block_given_p()) {
|
2001-07-14 19:17:19 +04:00
|
|
|
return rb_ary_new4(RARRAY(ary)->len, RARRAY(ary)->ptr);
|
1999-10-29 13:25:48 +04:00
|
|
|
}
|
|
|
|
|
2003-07-31 10:30:35 +04:00
|
|
|
collect = rb_ary_new2(RARRAY(ary)->len);
|
|
|
|
for (i = 0; i < RARRAY(ary)->len; i++) {
|
1999-10-29 13:25:48 +04:00
|
|
|
rb_ary_push(collect, rb_yield(RARRAY(ary)->ptr[i]));
|
|
|
|
}
|
|
|
|
return collect;
|
|
|
|
}
|
|
|
|
|
2000-05-01 13:42:38 +04:00
|
|
|
static VALUE
|
|
|
|
rb_ary_collect_bang(ary)
|
|
|
|
VALUE ary;
|
|
|
|
{
|
|
|
|
long i;
|
|
|
|
|
|
|
|
rb_ary_modify(ary);
|
|
|
|
for (i = 0; i < RARRAY(ary)->len; i++) {
|
|
|
|
RARRAY(ary)->ptr[i] = rb_yield(RARRAY(ary)->ptr[i]);
|
|
|
|
}
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
2003-07-03 15:02:53 +04:00
|
|
|
VALUE
|
|
|
|
rb_values_at(obj, olen, argc, argv, func)
|
|
|
|
VALUE obj;
|
|
|
|
long olen;
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE (*func) _((VALUE,long));
|
2003-06-02 08:49:46 +04:00
|
|
|
{
|
2003-07-03 15:02:53 +04:00
|
|
|
VALUE result = rb_ary_new2(argc);
|
|
|
|
long beg, len, i, j;
|
2003-06-02 08:49:46 +04:00
|
|
|
|
2003-07-03 15:02:53 +04:00
|
|
|
for (i=0; i<argc; i++) {
|
|
|
|
if (FIXNUM_P(argv[i])) {
|
|
|
|
rb_ary_push(result, (*func)(obj, FIX2LONG(argv[i])));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* check if idx is Range */
|
|
|
|
switch (rb_range_beg_len(argv[i], &beg, &len, olen, 0)) {
|
|
|
|
case Qfalse:
|
|
|
|
break;
|
|
|
|
case Qnil:
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
for (j=0; j<len; j++) {
|
|
|
|
rb_ary_push(result, (*func)(obj, j+beg));
|
|
|
|
}
|
|
|
|
continue;
|
2003-06-02 08:49:46 +04:00
|
|
|
}
|
2003-07-03 15:02:53 +04:00
|
|
|
rb_ary_push(result, (*func)(obj, NUM2LONG(argv[i])));
|
2003-06-02 08:49:46 +04:00
|
|
|
}
|
2003-07-03 15:02:53 +04:00
|
|
|
return result;
|
2003-06-02 08:49:46 +04:00
|
|
|
}
|
|
|
|
|
2003-05-04 20:03:24 +04:00
|
|
|
static VALUE
|
|
|
|
rb_ary_values_at(argc, argv, ary)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE ary;
|
|
|
|
{
|
2003-07-03 15:02:53 +04:00
|
|
|
return rb_values_at(ary, RARRAY(ary)->len, argc, argv, rb_ary_entry);
|
2003-05-04 20:03:24 +04:00
|
|
|
}
|
|
|
|
|
2001-12-11 06:48:08 +03:00
|
|
|
static VALUE
|
|
|
|
rb_ary_select(argc, argv, ary)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE ary;
|
|
|
|
{
|
2002-08-23 19:04:27 +04:00
|
|
|
VALUE result;
|
2001-12-11 06:48:08 +03:00
|
|
|
long i;
|
|
|
|
|
2003-05-04 20:03:24 +04:00
|
|
|
if (!rb_block_given_p()) {
|
|
|
|
rb_warn("Array#select(index..) is deprecated; use Array#values_at");
|
|
|
|
return rb_ary_values_at(argc, argv, ary);
|
2001-12-11 06:48:08 +03:00
|
|
|
}
|
2003-05-04 20:03:24 +04:00
|
|
|
if (argc > 0) {
|
|
|
|
rb_raise(rb_eArgError, "wrong number arguments (%d for 0)", argc);
|
|
|
|
}
|
|
|
|
result = rb_ary_new2(RARRAY(ary)->len);
|
|
|
|
for (i = 0; i < RARRAY(ary)->len; i++) {
|
|
|
|
if (RTEST(rb_yield(RARRAY(ary)->ptr[i]))) {
|
|
|
|
rb_ary_push(result, RARRAY(ary)->ptr[i]);
|
2001-12-11 06:48:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_delete(ary, item)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE item;
|
|
|
|
{
|
1999-08-13 09:45:20 +04:00
|
|
|
long i1, i2;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_modify(ary);
|
1998-01-16 15:19:22 +03:00
|
|
|
for (i1 = i2 = 0; i1 < RARRAY(ary)->len; i1++) {
|
|
|
|
if (rb_equal(RARRAY(ary)->ptr[i1], item)) continue;
|
1998-01-16 15:13:05 +03:00
|
|
|
if (i1 != i2) {
|
1998-01-16 15:19:22 +03:00
|
|
|
RARRAY(ary)->ptr[i2] = RARRAY(ary)->ptr[i1];
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
i2++;
|
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
if (RARRAY(ary)->len == i2) {
|
2000-05-24 08:34:26 +04:00
|
|
|
if (rb_block_given_p()) {
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_yield(item);
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
return Qnil;
|
|
|
|
}
|
2002-05-21 09:39:19 +04:00
|
|
|
|
|
|
|
RARRAY(ary)->len = i2;
|
|
|
|
if (i2 * 2 < RARRAY(ary)->aux.capa &&
|
|
|
|
RARRAY(ary)->aux.capa > ARY_DEFAULT_SIZE) {
|
2002-08-27 12:05:06 +04:00
|
|
|
REALLOC_N(RARRAY(ary)->ptr, VALUE, i2 * 2);
|
2002-05-21 09:39:19 +04:00
|
|
|
RARRAY(ary)->aux.capa = i2 * 2;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
2000-01-05 07:41:21 +03:00
|
|
|
rb_ary_delete_at(ary, pos)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE ary;
|
2000-01-05 07:41:21 +03:00
|
|
|
long pos;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2000-01-05 07:41:21 +03:00
|
|
|
long i, len = RARRAY(ary)->len;
|
2002-05-21 09:39:19 +04:00
|
|
|
VALUE del;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-10-16 14:33:06 +04:00
|
|
|
rb_ary_modify(ary);
|
1999-10-13 10:44:42 +04:00
|
|
|
if (pos >= len) return Qnil;
|
2002-05-21 09:39:19 +04:00
|
|
|
if (pos < 0) {
|
|
|
|
pos += len;
|
|
|
|
if (pos < 0) return Qnil;
|
|
|
|
}
|
1999-10-13 10:44:42 +04:00
|
|
|
|
|
|
|
del = RARRAY(ary)->ptr[pos];
|
|
|
|
for (i = pos + 1; i < len; i++, pos++) {
|
|
|
|
RARRAY(ary)->ptr[pos] = RARRAY(ary)->ptr[i];
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-10-13 10:44:42 +04:00
|
|
|
RARRAY(ary)->len = pos;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
return del;
|
|
|
|
}
|
|
|
|
|
2001-03-13 08:45:13 +03:00
|
|
|
static VALUE
|
2000-05-30 08:24:17 +04:00
|
|
|
rb_ary_delete_at_m(ary, pos)
|
|
|
|
VALUE ary, pos;
|
|
|
|
{
|
|
|
|
return rb_ary_delete_at(ary, NUM2LONG(pos));
|
|
|
|
}
|
|
|
|
|
2000-01-05 07:41:21 +03:00
|
|
|
static VALUE
|
2000-05-24 08:34:26 +04:00
|
|
|
rb_ary_slice_bang(argc, argv, ary)
|
2000-01-05 07:41:21 +03:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE ary;
|
|
|
|
{
|
|
|
|
VALUE arg1, arg2;
|
2002-05-21 09:39:19 +04:00
|
|
|
long pos, len;
|
2000-01-05 07:41:21 +03:00
|
|
|
|
|
|
|
rb_ary_modify(ary);
|
|
|
|
if (rb_scan_args(argc, argv, "11", &arg1, &arg2) == 2) {
|
|
|
|
pos = NUM2LONG(arg1);
|
|
|
|
len = NUM2LONG(arg2);
|
2002-05-22 09:57:08 +04:00
|
|
|
delete_pos_len:
|
2000-01-05 07:41:21 +03:00
|
|
|
if (pos < 0) {
|
|
|
|
pos = RARRAY(ary)->len + pos;
|
|
|
|
}
|
2000-02-01 06:12:21 +03:00
|
|
|
arg2 = rb_ary_subseq(ary, pos, len);
|
2001-05-06 19:06:00 +04:00
|
|
|
rb_ary_update(ary, pos, len, Qnil); /* Qnil/rb_ary_new2(0) */
|
2000-01-05 07:41:21 +03:00
|
|
|
return arg2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!FIXNUM_P(arg1) && rb_range_beg_len(arg1, &pos, &len, RARRAY(ary)->len, 1)) {
|
|
|
|
goto delete_pos_len;
|
|
|
|
}
|
|
|
|
|
2002-05-22 09:57:08 +04:00
|
|
|
return rb_ary_delete_at(ary, NUM2LONG(arg1));
|
2000-01-05 07:41:21 +03:00
|
|
|
}
|
2000-02-01 06:12:21 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
2000-08-07 09:05:04 +04:00
|
|
|
rb_ary_reject_bang(ary)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-08-13 09:45:20 +04:00
|
|
|
long i1, i2;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_modify(ary);
|
1998-01-16 15:19:22 +03:00
|
|
|
for (i1 = i2 = 0; i1 < RARRAY(ary)->len; i1++) {
|
1999-08-13 09:45:20 +04:00
|
|
|
if (RTEST(rb_yield(RARRAY(ary)->ptr[i1]))) continue;
|
1998-01-16 15:13:05 +03:00
|
|
|
if (i1 != i2) {
|
1998-01-16 15:19:22 +03:00
|
|
|
RARRAY(ary)->ptr[i2] = RARRAY(ary)->ptr[i1];
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
i2++;
|
|
|
|
}
|
2000-08-07 09:05:04 +04:00
|
|
|
if (RARRAY(ary)->len == i2) return Qnil;
|
1998-01-16 15:19:22 +03:00
|
|
|
RARRAY(ary)->len = i2;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
return ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2001-10-16 07:27:23 +04:00
|
|
|
static VALUE
|
|
|
|
rb_ary_reject(ary)
|
|
|
|
VALUE ary;
|
|
|
|
{
|
2002-08-27 12:31:08 +04:00
|
|
|
ary = rb_ary_dup(ary);
|
2001-10-16 07:27:23 +04:00
|
|
|
rb_ary_reject_bang(ary);
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
2000-08-07 09:05:04 +04:00
|
|
|
static VALUE
|
|
|
|
rb_ary_delete_if(ary)
|
|
|
|
VALUE ary;
|
|
|
|
{
|
|
|
|
rb_ary_reject_bang(ary);
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
2002-11-03 14:04:35 +03:00
|
|
|
static VALUE
|
|
|
|
rb_ary_zip(argc, argv, ary)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE ary;
|
|
|
|
{
|
2002-11-19 11:07:51 +03:00
|
|
|
int i, j;
|
|
|
|
long len;
|
2002-11-03 14:04:35 +03:00
|
|
|
VALUE result;
|
|
|
|
|
|
|
|
for (i=0; i<argc; i++) {
|
|
|
|
argv[i] = to_ary(argv[i]);
|
2002-11-19 11:07:51 +03:00
|
|
|
}
|
|
|
|
if (rb_block_given_p()) {
|
2003-08-01 06:52:21 +04:00
|
|
|
for (i=0; i<RARRAY(ary)->len; i++) {
|
2002-11-19 11:07:51 +03:00
|
|
|
VALUE tmp = rb_ary_new2(argc+1);
|
|
|
|
|
|
|
|
rb_ary_push(tmp, rb_ary_entry(ary, i));
|
|
|
|
for (j=0; j<argc; j++) {
|
|
|
|
rb_ary_push(tmp, rb_ary_entry(argv[j], i));
|
|
|
|
}
|
|
|
|
rb_yield(tmp);
|
|
|
|
}
|
|
|
|
return Qnil;
|
2002-11-03 14:04:35 +03:00
|
|
|
}
|
2003-08-01 06:52:21 +04:00
|
|
|
len = RARRAY(ary)->len;
|
2002-11-03 14:04:35 +03:00
|
|
|
result = rb_ary_new2(len);
|
|
|
|
for (i=0; i<len; i++) {
|
|
|
|
VALUE tmp = rb_ary_new2(argc+1);
|
|
|
|
|
|
|
|
rb_ary_push(tmp, rb_ary_entry(ary, i));
|
|
|
|
for (j=0; j<argc; j++) {
|
|
|
|
rb_ary_push(tmp, rb_ary_entry(argv[j], i));
|
|
|
|
}
|
|
|
|
rb_ary_push(result, tmp);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2002-11-19 11:07:51 +03:00
|
|
|
static VALUE
|
|
|
|
rb_ary_transpose(ary)
|
2002-12-31 13:28:52 +03:00
|
|
|
VALUE ary;
|
2002-11-19 11:07:51 +03:00
|
|
|
{
|
|
|
|
long elen = -1, alen, i, j;
|
2003-07-24 22:33:50 +04:00
|
|
|
VALUE tmp, result = 0;
|
2002-11-19 11:07:51 +03:00
|
|
|
|
|
|
|
alen = RARRAY(ary)->len;
|
|
|
|
if (alen == 0) return rb_ary_dup(ary);
|
|
|
|
for (i=0; i<alen; i++) {
|
|
|
|
tmp = to_ary(RARRAY(ary)->ptr[i]);
|
|
|
|
if (elen < 0) { /* first element */
|
|
|
|
elen = RARRAY(tmp)->len;
|
|
|
|
result = rb_ary_new2(elen);
|
|
|
|
for (j=0; j<elen; j++) {
|
|
|
|
rb_ary_store(result, j, rb_ary_new2(alen));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (elen != RARRAY(tmp)->len) {
|
|
|
|
rb_raise(rb_eIndexError, "element size differ (%d should be %d)",
|
|
|
|
RARRAY(tmp)->len, elen);
|
|
|
|
}
|
|
|
|
for (j=0; j<elen; j++) {
|
|
|
|
rb_ary_store(RARRAY(result)->ptr[j], i, RARRAY(tmp)->ptr[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static VALUE
|
2002-09-03 09:20:14 +04:00
|
|
|
rb_ary_replace(copy, orig)
|
|
|
|
VALUE copy, orig;
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
2002-09-03 09:20:14 +04:00
|
|
|
rb_ary_modify(copy);
|
|
|
|
orig = to_ary(orig);
|
|
|
|
if (copy == orig) return copy;
|
|
|
|
ary_make_shared(orig);
|
|
|
|
if (RARRAY(copy)->ptr && !FL_TEST(copy, ELTS_SHARED))
|
|
|
|
free(RARRAY(copy)->ptr);
|
|
|
|
RARRAY(copy)->ptr = RARRAY(orig)->ptr;
|
|
|
|
RARRAY(copy)->len = RARRAY(orig)->len;
|
|
|
|
RARRAY(copy)->aux.shared = RARRAY(orig)->aux.shared;
|
|
|
|
FL_SET(copy, ELTS_SHARED);
|
|
|
|
|
|
|
|
return copy;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2001-03-13 08:45:13 +03:00
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_clear(ary)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-10-18 13:03:16 +04:00
|
|
|
rb_ary_modify(ary);
|
1998-01-16 15:19:22 +03:00
|
|
|
RARRAY(ary)->len = 0;
|
2002-05-21 09:39:19 +04:00
|
|
|
if (ARY_DEFAULT_SIZE * 2 < RARRAY(ary)->aux.capa) {
|
2002-08-27 12:05:06 +04:00
|
|
|
REALLOC_N(RARRAY(ary)->ptr, VALUE, ARY_DEFAULT_SIZE * 2);
|
2001-12-10 10:18:16 +03:00
|
|
|
RARRAY(ary)->aux.capa = ARY_DEFAULT_SIZE * 2;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
return ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_fill(argc, argv, ary)
|
1998-01-16 15:13:05 +03:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
VALUE item, arg1, arg2;
|
1999-08-13 09:45:20 +04:00
|
|
|
long beg, end, len;
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE *p, *pend;
|
2002-01-28 13:18:14 +03:00
|
|
|
int block_p = Qfalse;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2002-01-28 11:44:45 +03:00
|
|
|
if (rb_block_given_p()) {
|
|
|
|
block_p = Qtrue;
|
|
|
|
rb_scan_args(argc, argv, "02", &arg1, &arg2);
|
|
|
|
argc += 1; /* hackish */
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_scan_args(argc, argv, "12", &item, &arg1, &arg2);
|
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
switch (argc) {
|
|
|
|
case 1:
|
|
|
|
beg = 0;
|
2002-01-28 11:44:45 +03:00
|
|
|
len = RARRAY(ary)->len;
|
1999-08-13 09:45:20 +04:00
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
if (rb_range_beg_len(arg1, &beg, &len, RARRAY(ary)->len, 1)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* fall through */
|
|
|
|
case 3:
|
2002-05-21 09:39:19 +04:00
|
|
|
beg = NIL_P(arg1) ? 0 : NUM2LONG(arg1);
|
1998-01-16 15:13:05 +03:00
|
|
|
if (beg < 0) {
|
1998-01-16 15:19:22 +03:00
|
|
|
beg = RARRAY(ary)->len + beg;
|
1998-01-16 15:13:05 +03:00
|
|
|
if (beg < 0) beg = 0;
|
|
|
|
}
|
2002-05-21 09:39:19 +04:00
|
|
|
len = NIL_P(arg2) ? RARRAY(ary)->len - beg : NUM2LONG(arg2);
|
1999-08-13 09:45:20 +04:00
|
|
|
break;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_modify(ary);
|
1998-01-16 15:13:05 +03:00
|
|
|
end = beg + len;
|
1998-01-16 15:19:22 +03:00
|
|
|
if (end > RARRAY(ary)->len) {
|
2001-12-10 10:18:16 +03:00
|
|
|
if (end >= RARRAY(ary)->aux.capa) {
|
2002-05-21 09:39:19 +04:00
|
|
|
REALLOC_N(RARRAY(ary)->ptr, VALUE, end);
|
2002-08-27 12:05:06 +04:00
|
|
|
RARRAY(ary)->aux.capa = end;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
if (beg > RARRAY(ary)->len) {
|
2002-05-21 09:39:19 +04:00
|
|
|
rb_mem_clear(RARRAY(ary)->ptr + RARRAY(ary)->len, end - RARRAY(ary)->len);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
RARRAY(ary)->len = end;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2002-01-28 11:44:45 +03:00
|
|
|
if (block_p) {
|
2003-08-01 06:52:21 +04:00
|
|
|
VALUE v;
|
|
|
|
long i;
|
|
|
|
|
2003-08-07 01:50:06 +04:00
|
|
|
for (i=beg; i<RARRAY(ary)->len; i++) {
|
|
|
|
v = rb_yield(LONG2NUM(i));
|
2003-08-01 06:52:21 +04:00
|
|
|
if (i>=RARRAY(ary)->len) break;
|
|
|
|
RARRAY(ary)->ptr[i] = v;
|
2002-01-28 11:44:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2003-08-01 06:52:21 +04:00
|
|
|
p = RARRAY(ary)->ptr + beg;
|
|
|
|
pend = p + len;
|
2002-01-28 11:44:45 +03:00
|
|
|
while (p < pend) {
|
|
|
|
*p++ = item;
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
return ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_plus(x, y)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE x, y;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE z;
|
2002-05-21 09:39:19 +04:00
|
|
|
long len;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-10-10 11:03:36 +04:00
|
|
|
y = to_ary(y);
|
2002-05-21 09:39:19 +04:00
|
|
|
len = RARRAY(x)->len + RARRAY(y)->len;
|
|
|
|
z = rb_ary_new2(len);
|
1998-01-16 15:19:22 +03:00
|
|
|
MEMCPY(RARRAY(z)->ptr, RARRAY(x)->ptr, VALUE, RARRAY(x)->len);
|
2002-05-21 09:39:19 +04:00
|
|
|
MEMCPY(RARRAY(z)->ptr + RARRAY(x)->len, RARRAY(y)->ptr, VALUE, RARRAY(y)->len);
|
|
|
|
RARRAY(z)->len = len;
|
1998-01-16 15:19:22 +03:00
|
|
|
return z;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_concat(x, y)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE x, y;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2000-10-10 11:03:36 +04:00
|
|
|
y = to_ary(y);
|
2001-05-02 08:22:21 +04:00
|
|
|
if (RARRAY(y)->len > 0) {
|
2001-05-06 19:06:00 +04:00
|
|
|
rb_ary_update(x, RARRAY(x)->len, 0, y);
|
2000-10-10 11:03:36 +04:00
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
return x;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_times(ary, times)
|
2002-08-23 11:48:48 +04:00
|
|
|
VALUE ary, times;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2003-01-07 10:36:40 +03:00
|
|
|
VALUE ary2, tmp;
|
1999-08-13 09:45:20 +04:00
|
|
|
long i, len;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2003-01-07 10:36:40 +03:00
|
|
|
tmp = rb_check_string_type(times);
|
|
|
|
if (!NIL_P(tmp)) {
|
|
|
|
return rb_ary_join(ary, tmp);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
len = NUM2LONG(times);
|
2003-04-10 13:48:15 +04:00
|
|
|
if (len == 0) return rb_ary_new2(0);
|
1998-01-16 15:13:05 +03:00
|
|
|
if (len < 0) {
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_raise(rb_eArgError, "negative argument");
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2003-04-10 13:48:15 +04:00
|
|
|
if (LONG_MAX/len < RARRAY(ary)->len) {
|
|
|
|
rb_raise(rb_eArgError, "argument too big");
|
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
len *= RARRAY(ary)->len;
|
|
|
|
|
2001-12-10 10:18:16 +03:00
|
|
|
ary2 = ary_new(rb_obj_class(ary), len);
|
1999-01-20 07:59:39 +03:00
|
|
|
RARRAY(ary2)->len = len;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1998-01-16 15:19:22 +03: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 15:13:05 +03:00
|
|
|
}
|
2001-10-02 08:31:23 +04:00
|
|
|
OBJ_INFECT(ary2, ary);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
return ary2;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_assoc(ary, key)
|
2002-05-21 09:39:19 +04:00
|
|
|
VALUE ary, key;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
VALUE *p, *pend;
|
|
|
|
|
2002-05-21 09:39:19 +04:00
|
|
|
p = RARRAY(ary)->ptr;
|
|
|
|
pend = p + RARRAY(ary)->len;
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
while (p < pend) {
|
2002-05-21 09:39:19 +04:00
|
|
|
if (TYPE(*p) == T_ARRAY &&
|
|
|
|
RARRAY(*p)->len > 0 &&
|
|
|
|
rb_equal(RARRAY(*p)->ptr[0], key))
|
1998-01-16 15:13:05 +03:00
|
|
|
return *p;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_rassoc(ary, value)
|
2002-08-23 11:48:48 +04:00
|
|
|
VALUE ary, value;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
VALUE *p, *pend;
|
|
|
|
|
2002-05-21 09:39:19 +04:00
|
|
|
p = RARRAY(ary)->ptr;
|
|
|
|
pend = p + RARRAY(ary)->len;
|
|
|
|
|
1998-01-16 15:13:05 +03: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-20 07:59:39 +03:00
|
|
|
rb_ary_equal(ary1, ary2)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE ary1, ary2;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-08-13 09:45:20 +04:00
|
|
|
long i;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2001-08-29 10:28:51 +04:00
|
|
|
if (ary1 == ary2) return Qtrue;
|
2003-02-04 10:27:43 +03:00
|
|
|
if (TYPE(ary2) != T_ARRAY) {
|
2003-02-04 10:40:52 +03:00
|
|
|
if (!rb_respond_to(ary2, rb_intern("to_ary"))) {
|
2003-02-04 10:27:43 +03:00
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
return rb_equal(ary2, ary1);
|
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
if (RARRAY(ary1)->len != RARRAY(ary2)->len) return Qfalse;
|
1998-01-16 15:19:22 +03:00
|
|
|
for (i=0; i<RARRAY(ary1)->len; i++) {
|
|
|
|
if (!rb_equal(RARRAY(ary1)->ptr[i], RARRAY(ary2)->ptr[i]))
|
1999-01-20 07:59:39 +03:00
|
|
|
return Qfalse;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
return Qtrue;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_eql(ary1, ary2)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE ary1, ary2;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-08-13 09:45:20 +04:00
|
|
|
long i;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2003-01-07 10:36:40 +03:00
|
|
|
if (ary1 == ary2) return Qtrue;
|
2003-02-03 11:45:26 +03:00
|
|
|
if (TYPE(ary2) != T_ARRAY) return Qfalse;
|
2000-12-29 05:46:12 +03:00
|
|
|
if (RARRAY(ary1)->len != RARRAY(ary2)->len) return Qfalse;
|
1998-01-16 15:19:22 +03:00
|
|
|
for (i=0; i<RARRAY(ary1)->len; i++) {
|
|
|
|
if (!rb_eql(RARRAY(ary1)->ptr[i], RARRAY(ary2)->ptr[i]))
|
1999-01-20 07:59:39 +03:00
|
|
|
return Qfalse;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
return Qtrue;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_hash(ary)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2002-08-23 11:48:48 +04:00
|
|
|
long i, h;
|
2000-11-20 10:31:55 +03:00
|
|
|
VALUE n;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
h = RARRAY(ary)->len;
|
|
|
|
for (i=0; i<RARRAY(ary)->len; i++) {
|
2002-05-21 09:39:19 +04:00
|
|
|
h = (h << 1) | (h<0 ? 1 : 0);
|
2000-01-18 09:09:05 +03:00
|
|
|
n = rb_hash(RARRAY(ary)->ptr[i]);
|
1999-01-20 07:59:39 +03:00
|
|
|
h ^= NUM2LONG(n);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2002-05-21 09:39:19 +04:00
|
|
|
return LONG2FIX(h);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_includes(ary, item)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE item;
|
|
|
|
{
|
1999-08-13 09:45:20 +04:00
|
|
|
long i;
|
2002-08-23 11:48:48 +04:00
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
for (i=0; i<RARRAY(ary)->len; i++) {
|
|
|
|
if (rb_equal(RARRAY(ary)->ptr[i], item)) {
|
1999-01-20 07:59:39 +03:00
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
2001-08-23 10:02:15 +04:00
|
|
|
VALUE
|
2002-08-23 11:48:48 +04:00
|
|
|
rb_ary_cmp(ary1, ary2)
|
|
|
|
VALUE ary1, ary2;
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
1999-08-13 09:45:20 +04:00
|
|
|
long i, len;
|
1999-01-20 07:59:39 +03:00
|
|
|
|
2002-08-23 11:48:48 +04:00
|
|
|
ary2 = to_ary(ary2);
|
|
|
|
len = RARRAY(ary1)->len;
|
1999-01-20 07:59:39 +03:00
|
|
|
if (len > RARRAY(ary2)->len) {
|
|
|
|
len = RARRAY(ary2)->len;
|
|
|
|
}
|
|
|
|
for (i=0; i<len; i++) {
|
2002-08-23 11:48:48 +04:00
|
|
|
VALUE v = rb_funcall(RARRAY(ary1)->ptr[i], id_cmp, 1, RARRAY(ary2)->ptr[i]);
|
1999-01-20 07:59:39 +03:00
|
|
|
if (v != INT2FIX(0)) {
|
|
|
|
return v;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
2002-08-23 11:48:48 +04:00
|
|
|
len = RARRAY(ary1)->len - RARRAY(ary2)->len;
|
1999-01-20 07:59:39 +03:00
|
|
|
if (len == 0) return INT2FIX(0);
|
|
|
|
if (len > 0) return INT2FIX(1);
|
|
|
|
return INT2FIX(-1);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2000-12-08 10:10:38 +03:00
|
|
|
static VALUE
|
2000-12-12 10:42:35 +03:00
|
|
|
ary_make_hash(ary1, ary2)
|
2000-12-05 12:36:54 +03:00
|
|
|
VALUE ary1, ary2;
|
|
|
|
{
|
2000-12-08 10:10:38 +03:00
|
|
|
VALUE hash = rb_hash_new();
|
2002-08-23 11:48:48 +04:00
|
|
|
long i;
|
2000-12-05 12:36:54 +03:00
|
|
|
|
|
|
|
for (i=0; i<RARRAY(ary1)->len; i++) {
|
2000-12-08 10:10:38 +03:00
|
|
|
rb_hash_aset(hash, RARRAY(ary1)->ptr[i], Qtrue);
|
2000-12-05 12:36:54 +03:00
|
|
|
}
|
|
|
|
if (ary2) {
|
|
|
|
for (i=0; i<RARRAY(ary2)->len; i++) {
|
2000-12-08 10:10:38 +03:00
|
|
|
rb_hash_aset(hash, RARRAY(ary2)->ptr[i], Qtrue);
|
2000-12-05 12:36:54 +03:00
|
|
|
}
|
|
|
|
}
|
2000-12-08 10:10:38 +03:00
|
|
|
return hash;
|
2000-12-05 12:36:54 +03:00
|
|
|
}
|
|
|
|
|
2003-02-03 08:34:16 +03:00
|
|
|
static VALUE
|
|
|
|
rb_ary_diff(ary1, ary2)
|
|
|
|
VALUE ary1, ary2;
|
|
|
|
{
|
|
|
|
VALUE ary3, hash;
|
|
|
|
long i;
|
|
|
|
|
|
|
|
hash = ary_make_hash(to_ary(ary2), 0);
|
|
|
|
ary3 = rb_ary_new();
|
|
|
|
|
|
|
|
for (i=0; i<RARRAY(ary1)->len; i++) {
|
|
|
|
if (st_lookup(RHASH(hash)->tbl, RARRAY(ary1)->ptr[i], 0)) continue;
|
|
|
|
rb_ary_push(ary3, RARRAY(ary1)->ptr[i]);
|
|
|
|
}
|
|
|
|
return ary3;
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_and(ary1, ary2)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE ary1, ary2;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2002-08-23 11:48:48 +04:00
|
|
|
VALUE hash, ary3;
|
1999-08-13 09:45:20 +04:00
|
|
|
long i;
|
2000-12-08 10:10:38 +03:00
|
|
|
|
|
|
|
ary2 = to_ary(ary2);
|
2002-08-23 11:48:48 +04:00
|
|
|
ary3 = rb_ary_new2(RARRAY(ary1)->len < RARRAY(ary2)->len ?
|
|
|
|
RARRAY(ary1)->len : RARRAY(ary2)->len);
|
2000-12-08 10:10:38 +03:00
|
|
|
hash = ary_make_hash(ary2, 0);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
for (i=0; i<RARRAY(ary1)->len; i++) {
|
2000-12-08 10:10:38 +03:00
|
|
|
VALUE v = RARRAY(ary1)->ptr[i];
|
2003-07-25 09:36:55 +04:00
|
|
|
if (st_delete(RHASH(hash)->tbl, (st_data_t*)&v, 0)) {
|
2001-05-16 13:05:54 +04:00
|
|
|
rb_ary_push(ary3, RARRAY(ary1)->ptr[i]);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
2000-12-05 12:36:54 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
return ary3;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_or(ary1, ary2)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE ary1, ary2;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2002-08-23 11:48:48 +04:00
|
|
|
VALUE hash, ary3;
|
2000-12-05 12:36:54 +03:00
|
|
|
VALUE v;
|
1999-08-13 09:45:20 +04:00
|
|
|
long i;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-12-05 12:36:54 +03:00
|
|
|
ary2 = to_ary(ary2);
|
2002-08-23 11:48:48 +04:00
|
|
|
ary3 = rb_ary_new2(RARRAY(ary1)->len+RARRAY(ary2)->len);
|
2000-12-08 10:10:38 +03:00
|
|
|
hash = ary_make_hash(ary1, ary2);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
for (i=0; i<RARRAY(ary1)->len; i++) {
|
2000-12-05 12:36:54 +03:00
|
|
|
v = RARRAY(ary1)->ptr[i];
|
2003-07-25 09:36:55 +04:00
|
|
|
if (st_delete(RHASH(hash)->tbl, (st_data_t*)&v, 0)) {
|
2001-05-16 13:05:54 +04:00
|
|
|
rb_ary_push(ary3, RARRAY(ary1)->ptr[i]);
|
2000-12-05 12:36:54 +03:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
for (i=0; i<RARRAY(ary2)->len; i++) {
|
2000-12-05 12:36:54 +03:00
|
|
|
v = RARRAY(ary2)->ptr[i];
|
2003-07-25 09:36:55 +04:00
|
|
|
if (st_delete(RHASH(hash)->tbl, (st_data_t*)&v, 0)) {
|
2001-05-16 13:05:54 +04:00
|
|
|
rb_ary_push(ary3, RARRAY(ary2)->ptr[i]);
|
2000-12-05 12:36:54 +03:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
return ary3;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_uniq_bang(ary)
|
|
|
|
VALUE ary;
|
|
|
|
{
|
2002-08-23 11:48:48 +04:00
|
|
|
VALUE hash;
|
2000-12-05 12:36:54 +03:00
|
|
|
VALUE *p, *q, *end;
|
1999-01-20 07:59:39 +03:00
|
|
|
|
2002-08-23 11:48:48 +04:00
|
|
|
rb_ary_modify(ary);
|
|
|
|
|
|
|
|
hash = ary_make_hash(ary, 0);
|
|
|
|
|
2000-12-08 10:10:38 +03:00
|
|
|
if (RARRAY(ary)->len == RHASH(hash)->tbl->num_entries) {
|
2000-12-05 12:36:54 +03:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
p = q = RARRAY(ary)->ptr;
|
|
|
|
end = p + RARRAY(ary)->len;
|
1999-01-20 07:59:39 +03:00
|
|
|
while (p < end) {
|
2001-05-16 13:05:54 +04:00
|
|
|
VALUE v = *p;
|
2003-07-25 09:36:55 +04:00
|
|
|
if (st_delete(RHASH(hash)->tbl, (st_data_t*)&v, 0)) {
|
2001-05-16 13:05:54 +04:00
|
|
|
*q++ = *p;
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
2001-05-16 13:05:54 +04:00
|
|
|
p++;
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
2000-12-05 12:36:54 +03:00
|
|
|
RARRAY(ary)->len = (q - RARRAY(ary)->ptr);
|
1999-01-20 07:59:39 +03:00
|
|
|
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_ary_uniq(ary)
|
|
|
|
VALUE ary;
|
|
|
|
{
|
2002-08-27 12:31:08 +04:00
|
|
|
ary = rb_ary_dup(ary);
|
1999-10-20 11:10:23 +04:00
|
|
|
rb_ary_uniq_bang(ary);
|
|
|
|
return ary;
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_ary_compact_bang(ary)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
VALUE *p, *t, *end;
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_modify(ary);
|
1998-01-16 15:19:22 +03:00
|
|
|
p = t = RARRAY(ary)->ptr;
|
|
|
|
end = p + RARRAY(ary)->len;
|
2002-05-21 09:39:19 +04:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
while (t < end) {
|
|
|
|
if (NIL_P(*t)) t++;
|
|
|
|
else *p++ = *t++;
|
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
if (RARRAY(ary)->len == (p - RARRAY(ary)->ptr)) {
|
|
|
|
return Qnil;
|
|
|
|
}
|
2001-12-10 10:18:16 +03:00
|
|
|
RARRAY(ary)->len = RARRAY(ary)->aux.capa = (p - RARRAY(ary)->ptr);
|
1998-01-16 15:19:22 +03:00
|
|
|
REALLOC_N(RARRAY(ary)->ptr, VALUE, RARRAY(ary)->len);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
return ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_compact(ary)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2002-08-27 12:31:08 +04:00
|
|
|
ary = rb_ary_dup(ary);
|
1999-10-20 11:10:23 +04:00
|
|
|
rb_ary_compact_bang(ary);
|
|
|
|
return ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_nitems(ary)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-08-13 09:45:20 +04:00
|
|
|
long n = 0;
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE *p, *pend;
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
p = RARRAY(ary)->ptr;
|
|
|
|
pend = p + RARRAY(ary)->len;
|
2002-05-21 09:39:19 +04:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
while (p < pend) {
|
|
|
|
if (!NIL_P(*p)) n++;
|
|
|
|
p++;
|
|
|
|
}
|
2002-08-21 19:47:54 +04:00
|
|
|
return LONG2NUM(n);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2002-05-21 09:39:19 +04:00
|
|
|
static long
|
2001-05-02 08:22:21 +04:00
|
|
|
flatten(ary, idx, ary2, memo)
|
|
|
|
VALUE ary;
|
|
|
|
long idx;
|
|
|
|
VALUE ary2, memo;
|
|
|
|
{
|
|
|
|
VALUE id;
|
|
|
|
long i = idx;
|
|
|
|
long n, lim = idx + RARRAY(ary2)->len;
|
|
|
|
|
|
|
|
id = rb_obj_id(ary2);
|
|
|
|
if (rb_ary_includes(memo, id)) {
|
|
|
|
rb_raise(rb_eArgError, "tried to flatten recursive array");
|
|
|
|
}
|
|
|
|
rb_ary_push(memo, id);
|
2001-05-06 19:06:00 +04:00
|
|
|
rb_ary_update(ary, idx, 1, ary2);
|
2001-05-02 08:22:21 +04:00
|
|
|
while (i < lim) {
|
|
|
|
if (TYPE(RARRAY(ary)->ptr[i]) == T_ARRAY) {
|
|
|
|
n = flatten(ary, i, RARRAY(ary)->ptr[i], memo);
|
|
|
|
i += n; lim += n;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
rb_ary_pop(memo);
|
|
|
|
|
|
|
|
return lim - idx - 1; /* returns number of increased items */
|
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static VALUE
|
|
|
|
rb_ary_flatten_bang(ary)
|
|
|
|
VALUE ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2001-05-02 08:22:21 +04:00
|
|
|
long i = 0;
|
1999-01-20 07:59:39 +03:00
|
|
|
int mod = 0;
|
2001-05-02 08:22:21 +04:00
|
|
|
VALUE memo = Qnil;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_modify(ary);
|
2001-05-02 08:22:21 +04:00
|
|
|
while (i<RARRAY(ary)->len) {
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE ary2 = RARRAY(ary)->ptr[i];
|
2001-05-02 08:22:21 +04:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
if (TYPE(ary2) == T_ARRAY) {
|
2001-05-02 08:22:21 +04:00
|
|
|
if (NIL_P(memo)) {
|
|
|
|
memo = rb_ary_new();
|
2000-02-01 06:12:21 +03:00
|
|
|
}
|
2001-05-02 08:22:21 +04:00
|
|
|
i += flatten(ary, i, ary2, memo);
|
1999-01-20 07:59:39 +03:00
|
|
|
mod = 1;
|
|
|
|
}
|
2001-05-02 08:22:21 +04:00
|
|
|
i++;
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
if (mod == 0) return Qnil;
|
|
|
|
return ary;
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static VALUE
|
|
|
|
rb_ary_flatten(ary)
|
|
|
|
VALUE ary;
|
|
|
|
{
|
2002-08-27 12:31:08 +04:00
|
|
|
ary = rb_ary_dup(ary);
|
1999-10-20 11:10:23 +04:00
|
|
|
rb_ary_flatten_bang(ary);
|
|
|
|
return ary;
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
void
|
|
|
|
Init_Array()
|
|
|
|
{
|
|
|
|
rb_cArray = rb_define_class("Array", rb_cObject);
|
|
|
|
rb_include_module(rb_cArray, rb_mEnumerable);
|
|
|
|
|
2002-12-20 11:33:17 +03:00
|
|
|
rb_define_alloc_func(rb_cArray, ary_alloc);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_singleton_method(rb_cArray, "[]", rb_ary_s_create, -1);
|
2000-02-29 11:05:32 +03:00
|
|
|
rb_define_method(rb_cArray, "initialize", rb_ary_initialize, -1);
|
2003-05-19 09:41:08 +04:00
|
|
|
rb_define_method(rb_cArray, "initialize_copy", rb_ary_replace, 1);
|
2003-04-03 09:25:00 +04:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
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);
|
2003-02-10 12:40:13 +03:00
|
|
|
rb_define_method(rb_cArray, "to_ary", rb_ary_to_ary_m, 0);
|
1999-01-20 07:59:39 +03:00
|
|
|
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);
|
1999-10-13 10:44:42 +04:00
|
|
|
rb_define_method(rb_cArray, "at", rb_ary_at, 1);
|
2001-02-02 14:38:20 +03:00
|
|
|
rb_define_method(rb_cArray, "fetch", rb_ary_fetch, -1);
|
2003-06-06 13:24:59 +04:00
|
|
|
rb_define_method(rb_cArray, "first", rb_ary_first, -1);
|
|
|
|
rb_define_method(rb_cArray, "last", rb_ary_last, -1);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_method(rb_cArray, "concat", rb_ary_concat, 1);
|
|
|
|
rb_define_method(rb_cArray, "<<", rb_ary_push, 1);
|
2000-01-05 07:41:21 +03:00
|
|
|
rb_define_method(rb_cArray, "push", rb_ary_push_m, -1);
|
2000-01-08 08:00:25 +03:00
|
|
|
rb_define_method(rb_cArray, "pop", rb_ary_pop, 0);
|
|
|
|
rb_define_method(rb_cArray, "shift", rb_ary_shift, 0);
|
2000-05-01 13:42:38 +04:00
|
|
|
rb_define_method(rb_cArray, "unshift", rb_ary_unshift_m, -1);
|
2001-05-02 08:22:21 +04:00
|
|
|
rb_define_method(rb_cArray, "insert", rb_ary_insert, -1);
|
1999-01-20 07:59:39 +03:00
|
|
|
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);
|
2000-01-05 07:41:21 +03:00
|
|
|
rb_define_method(rb_cArray, "join", rb_ary_join_m, -1);
|
|
|
|
rb_define_method(rb_cArray, "reverse", rb_ary_reverse_m, 0);
|
2000-08-24 10:21:43 +04:00
|
|
|
rb_define_method(rb_cArray, "reverse!", rb_ary_reverse_bang, 0);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_method(rb_cArray, "sort", rb_ary_sort, 0);
|
|
|
|
rb_define_method(rb_cArray, "sort!", rb_ary_sort_bang, 0);
|
1999-10-29 13:25:48 +04:00
|
|
|
rb_define_method(rb_cArray, "collect", rb_ary_collect, 0);
|
2000-05-01 13:42:38 +04:00
|
|
|
rb_define_method(rb_cArray, "collect!", rb_ary_collect_bang, 0);
|
2001-07-14 19:17:19 +04:00
|
|
|
rb_define_method(rb_cArray, "map", rb_ary_collect, 0);
|
2000-05-01 13:42:38 +04:00
|
|
|
rb_define_method(rb_cArray, "map!", rb_ary_collect_bang, 0);
|
2003-05-04 20:03:24 +04:00
|
|
|
rb_define_method(rb_cArray, "select", rb_ary_select, -1);
|
|
|
|
rb_define_method(rb_cArray, "values_at", rb_ary_values_at, -1);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_method(rb_cArray, "delete", rb_ary_delete, 1);
|
2000-05-30 08:24:17 +04:00
|
|
|
rb_define_method(rb_cArray, "delete_at", rb_ary_delete_at_m, 1);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_method(rb_cArray, "delete_if", rb_ary_delete_if, 0);
|
2001-10-16 07:27:23 +04:00
|
|
|
rb_define_method(rb_cArray, "reject", rb_ary_reject, 0);
|
2000-08-07 09:05:04 +04:00
|
|
|
rb_define_method(rb_cArray, "reject!", rb_ary_reject_bang, 0);
|
2002-11-03 14:04:35 +03:00
|
|
|
rb_define_method(rb_cArray, "zip", rb_ary_zip, -1);
|
2002-11-19 11:07:51 +03:00
|
|
|
rb_define_method(rb_cArray, "transpose", rb_ary_transpose, 0);
|
2001-05-06 19:06:00 +04:00
|
|
|
rb_define_method(rb_cArray, "replace", rb_ary_replace, 1);
|
1999-01-20 07:59:39 +03: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-02-01 06:12:21 +03:00
|
|
|
rb_define_method(rb_cArray, "slice", rb_ary_aref, -1);
|
2000-05-24 08:34:26 +04:00
|
|
|
rb_define_method(rb_cArray, "slice!", rb_ary_slice_bang, -1);
|
2000-02-01 06:12:21 +03:00
|
|
|
|
1999-01-20 07:59:39 +03: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 15:13:05 +03:00
|
|
|
|
2001-12-10 10:18:16 +03:00
|
|
|
id_cmp = rb_intern("<=>");
|
2002-05-21 09:39:19 +04:00
|
|
|
inspect_key = rb_intern("__inspect_key__");
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|