Ruby 1.9.2p136(2010-12-25revision30365)

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

# array.c

Go to the documentation of this file.

    00001 /**********************************************************************
    00002
    00003   array.c -
    00004
    00005   $Author: yugui $
    00006   created at: Fri Aug  6 09:46:12 JST 1993
    00007
    00008   Copyright (C) 1993-2007 Yukihiro Matsumoto
    00009   Copyright (C) 2000  Network Applied Communication Laboratory, Inc.
    00010   Copyright (C) 2000  Information-technology Promotion Agency, Japan
    00011
    00012 **********************************************************************/
    00013
    00014 #include "ruby/ruby.h"
    00015 #include "ruby/util.h"
    00016 #include "ruby/st.h"
    00017
    00018 #ifndef ARRAY_DEBUG
    00019 # define NDEBUG
    00020 #endif
    00021 #include <assert.h>
    00022
    00023 VALUE rb_cArray;
    00024
    00025 static ID id_cmp;
    00026
    00027 #define ARY_DEFAULT_SIZE 16
    00028 #define ARY_MAX_SIZE (LONG_MAX / (int)sizeof(VALUE))
    00029
    00030 void
    00031 rb_mem_clear(register VALUE *mem, register long size)
    00032 {
    00033     while (size--) {
    00034         *mem++ = Qnil;
    00035     }
    00036 }
    00037
    00038 static inline void
    00039 memfill(register VALUE *mem, register long size, register VALUE val)
    00040 {
    00041     while (size--) {
    00042         *mem++ = val;
    00043     }
    00044 }
    00045
    00046 # define ARY_SHARED_P(ary) \
    00047     (assert(!FL_TEST(ary, ELTS_SHARED) || !FL_TEST(ary, RARRAY_EMBED_FLAG)), \
    00048      FL_TEST(ary,ELTS_SHARED)!=0)
    00049 # define ARY_EMBED_P(ary) \
    00050     (assert(!FL_TEST(ary, ELTS_SHARED) || !FL_TEST(ary, RARRAY_EMBED_FLAG)), \
    00051      FL_TEST(ary, RARRAY_EMBED_FLAG)!=0)
    00052
    00053 #define ARY_HEAP_PTR(a) (assert(!ARY_EMBED_P(a)), RARRAY(a)->as.heap.ptr)
    00054 #define ARY_HEAP_LEN(a) (assert(!ARY_EMBED_P(a)), RARRAY(a)->as.heap.len)
    00055 #define ARY_EMBED_PTR(a) (assert(ARY_EMBED_P(a)), RARRAY(a)->as.ary)
    00056 #define ARY_EMBED_LEN(a) \
    00057     (assert(ARY_EMBED_P(a)), \
    00058      (long)((RBASIC(a)->flags >> RARRAY_EMBED_LEN_SHIFT) & \
    00059          (RARRAY_EMBED_LEN_MASK >> RARRAY_EMBED_LEN_SHIFT)))
    00060
    00061 #define ARY_OWNS_HEAP_P(a) (!FL_TEST(a, ELTS_SHARED|RARRAY_EMBED_FLAG))
    00062 #define FL_SET_EMBED(a) do { \
    00063     assert(!ARY_SHARED_P(a)); \
    00064     assert(!OBJ_FROZEN(a)); \
    00065     FL_SET(a, RARRAY_EMBED_FLAG); \
    00066 } while (0)
    00067 #define FL_UNSET_EMBED(ary) FL_UNSET(ary, RARRAY_EMBED_FLAG|RARRAY_EMBED_LEN_MASK)
    00068 #define FL_SET_SHARED(ary) do { \
    00069     assert(!ARY_EMBED_P(ary)); \
    00070     FL_SET(ary, ELTS_SHARED); \
    00071 } while (0)
    00072 #define FL_UNSET_SHARED(ary) FL_UNSET(ary, ELTS_SHARED)
    00073
    00074 #define ARY_SET_PTR(ary, p) do { \
    00075     assert(!ARY_EMBED_P(ary)); \
    00076     assert(!OBJ_FROZEN(ary)); \
    00077     RARRAY(ary)->as.heap.ptr = (p); \
    00078 } while (0)
    00079 #define ARY_SET_EMBED_LEN(ary, n) do { \
    00080     long tmp_n = n; \
    00081     assert(ARY_EMBED_P(ary)); \
    00082     assert(!OBJ_FROZEN(ary)); \
    00083     RBASIC(ary)->flags &= ~RARRAY_EMBED_LEN_MASK; \
    00084     RBASIC(ary)->flags |= (tmp_n) << RARRAY_EMBED_LEN_SHIFT; \
    00085 } while (0)
    00086 #define ARY_SET_HEAP_LEN(ary, n) do { \
    00087     assert(!ARY_EMBED_P(ary)); \
    00088     RARRAY(ary)->as.heap.len = n; \
    00089 } while (0)
    00090 #define ARY_SET_LEN(ary, n) do { \
    00091     if (ARY_EMBED_P(ary)) { \
    00092         ARY_SET_EMBED_LEN(ary, n); \
    00093     } \
    00094     else { \
    00095         ARY_SET_HEAP_LEN(ary, n); \
    00096     } \
    00097     assert(RARRAY_LEN(ary) == n); \
    00098 } while (0)
    00099 #define ARY_INCREASE_PTR(ary, n) do  { \
    00100     assert(!ARY_EMBED_P(ary)); \
    00101     assert(!OBJ_FROZEN(ary)); \
    00102     RARRAY(ary)->as.heap.ptr += n; \
    00103 } while (0)
    00104 #define ARY_INCREASE_LEN(ary, n) do  { \
    00105     assert(!OBJ_FROZEN(ary)); \
    00106     if (ARY_EMBED_P(ary)) { \
    00107         ARY_SET_EMBED_LEN(ary, RARRAY_LEN(ary)+n); \
    00108     } \
    00109     else { \
    00110         RARRAY(ary)->as.heap.len += n; \
    00111     } \
    00112 } while (0)
    00113
    00114 #define ARY_CAPA(ary) (ARY_EMBED_P(ary) ? RARRAY_EMBED_LEN_MAX : \
    00115                        ARY_SHARED_ROOT_P(ary) ? RARRAY_LEN(ary) : RARRAY(ary)->as.heap.aux.capa)
    00116 #define ARY_SET_CAPA(ary, n) do { \
    00117     assert(!ARY_EMBED_P(ary)); \
    00118     assert(!ARY_SHARED_P(ary)); \
    00119     assert(!OBJ_FROZEN(ary)); \
    00120     RARRAY(ary)->as.heap.aux.capa = (n); \
    00121 } while (0)
    00122
    00123 #define ARY_SHARED(ary) (assert(ARY_SHARED_P(ary)), RARRAY(ary)->as.heap.aux.shared)
    00124 #define ARY_SET_SHARED(ary, value) do { \
    00125     assert(!ARY_EMBED_P(ary)); \
    00126     assert(ARY_SHARED_P(ary)); \
    00127     assert(ARY_SHARED_ROOT_P(value)); \
    00128     RARRAY(ary)->as.heap.aux.shared = (value); \
    00129 } while (0)
    00130 #define RARRAY_SHARED_ROOT_FLAG FL_USER5
    00131 #define ARY_SHARED_ROOT_P(ary) (FL_TEST(ary, RARRAY_SHARED_ROOT_FLAG))
    00132 #define ARY_SHARED_NUM(ary) \
    00133     (assert(ARY_SHARED_ROOT_P(ary)), RARRAY(ary)->as.heap.aux.capa)
    00134 #define ARY_SET_SHARED_NUM(ary, value) do { \
    00135     assert(ARY_SHARED_ROOT_P(ary)); \
    00136     RARRAY(ary)->as.heap.aux.capa = (value); \
    00137 } while (0)
    00138 #define FL_SET_SHARED_ROOT(ary) do { \
    00139     assert(!ARY_EMBED_P(ary)); \
    00140     FL_SET(ary, RARRAY_SHARED_ROOT_FLAG); \
    00141 } while (0)
    00142
    00143 static void
    00144 ary_resize_capa(VALUE ary, long capacity)
    00145 {
    00146     assert(RARRAY_LEN(ary) <= capacity);
    00147     assert(!OBJ_FROZEN(ary));
    00148     assert(!ARY_SHARED_P(ary));
    00149     if (capacity > RARRAY_EMBED_LEN_MAX) {
    00150         if (ARY_EMBED_P(ary)) {
    00151             long len = ARY_EMBED_LEN(ary);
    00152             VALUE *ptr = ALLOC_N(VALUE, (capacity));
    00153             MEMCPY(ptr, ARY_EMBED_PTR(ary), VALUE, len);
    00154             FL_UNSET_EMBED(ary);
    00155             ARY_SET_PTR(ary, ptr);
    00156             ARY_SET_HEAP_LEN(ary, len);
    00157         }
    00158         else {
    00159             REALLOC_N(RARRAY(ary)->as.heap.ptr, VALUE, (capacity));
    00160         }
    00161         ARY_SET_CAPA(ary, (capacity));
    00162     }
    00163     else {
    00164         if (!ARY_EMBED_P(ary)) {
    00165             long len = RARRAY_LEN(ary);
    00166             VALUE *ptr = RARRAY_PTR(ary);
    00167             if (len > capacity) len = capacity;
    00168             MEMCPY(RARRAY(ary)->as.ary, ptr, VALUE, len);
    00169             FL_SET_EMBED(ary);
    00170             ARY_SET_LEN(ary, len);
    00171             xfree(ptr);
    00172         }
    00173     }
    00174 }
    00175
    00176 static void
    00177 ary_double_capa(VALUE ary, long min)
    00178 {
    00179     long new_capa = ARY_CAPA(ary) / 2;
    00180
    00181     if (new_capa < ARY_DEFAULT_SIZE) {
    00182         new_capa = ARY_DEFAULT_SIZE;
    00183     }
    00184     if (new_capa >= ARY_MAX_SIZE - min) {
    00185         new_capa = (ARY_MAX_SIZE - min) / 2;
    00186     }
    00187     new_capa += min;
    00188     ary_resize_capa(ary, new_capa);
    00189 }
    00190
    00191 static void
    00192 rb_ary_decrement_share(VALUE shared)
    00193 {
    00194     if (shared) {
    00195         long num = ARY_SHARED_NUM(shared) - 1;
    00196         if (num == 0) {
    00197             rb_ary_free(shared);
    00198             rb_gc_force_recycle(shared);
    00199         }
    00200         else if (num > 0) {
    00201             ARY_SET_SHARED_NUM(shared, num);
    00202         }
    00203     }
    00204 }
    00205
    00206 static void
    00207 rb_ary_unshare(VALUE ary)
    00208 {
    00209     VALUE shared = RARRAY(ary)->as.heap.aux.shared;
    00210     rb_ary_decrement_share(shared);
    00211     FL_UNSET_SHARED(ary);
    00212 }
    00213
    00214 static inline void
    00215 rb_ary_unshare_safe(VALUE ary)
    00216 {
    00217     if (ARY_SHARED_P(ary) && !ARY_EMBED_P(ary)) {
    00218         rb_ary_unshare(ary);
    00219     }
    00220 }
    00221
    00222 static VALUE
    00223 rb_ary_increment_share(VALUE shared)
    00224 {
    00225     long num = ARY_SHARED_NUM(shared);
    00226     if (num >= 0) {
    00227         ARY_SET_SHARED_NUM(shared, num + 1);
    00228     }
    00229     return shared;
    00230 }
    00231
    00232 static void
    00233 rb_ary_set_shared(VALUE ary, VALUE shared)
    00234 {
    00235     rb_ary_increment_share(shared);
    00236     FL_SET_SHARED(ary);
    00237     ARY_SET_SHARED(ary, shared);
    00238 }
    00239
    00240 static inline void
    00241 rb_ary_modify_check(VALUE ary)
    00242 {
    00243     if (OBJ_FROZEN(ary)) rb_error_frozen("array");
    00244     if (!OBJ_UNTRUSTED(ary) && rb_safe_level() >= 4)
    00245         rb_raise(rb_eSecurityError, "Insecure: can't modify array");
    00246 }
    00247
    00248 static void
    00249 rb_ary_modify(VALUE ary)
    00250 {
    00251     rb_ary_modify_check(ary);
    00252     if (ARY_SHARED_P(ary)) {
    00253         long len = RARRAY_LEN(ary);
    00254         if (len <= RARRAY_EMBED_LEN_MAX) {
    00255             VALUE *ptr = ARY_HEAP_PTR(ary);
    00256             VALUE shared = ARY_SHARED(ary);
    00257             FL_UNSET_SHARED(ary);
    00258             FL_SET_EMBED(ary);
    00259             MEMCPY(ARY_EMBED_PTR(ary), ptr, VALUE, len);
    00260             rb_ary_decrement_share(shared);
    00261             ARY_SET_EMBED_LEN(ary, len);
    00262         }
    00263         else {
    00264             VALUE *ptr = ALLOC_N(VALUE, len);
    00265             MEMCPY(ptr, RARRAY_PTR(ary), VALUE, len);
    00266             rb_ary_unshare(ary);
    00267             ARY_SET_CAPA(ary, len);
    00268             ARY_SET_PTR(ary, ptr);
    00269         }
    00270     }
    00271 }
    00272
    00273 VALUE
    00274 rb_ary_freeze(VALUE ary)
    00275 {
    00276     return rb_obj_freeze(ary);
    00277 }
    00278
    00279 /*
    00280  *  call-seq:
    00281  *     ary.frozen?  -> true or false
    00282  *
    00283  *  Return <code>true</code> if this array is frozen (or temporarily frozen
    00284  *  while being sorted).
    00285  */
    00286
    00287 static VALUE
    00288 rb_ary_frozen_p(VALUE ary)
    00289 {
    00290     if (OBJ_FROZEN(ary)) return Qtrue;
    00291     return Qfalse;
    00292 }
    00293
    00294 static VALUE
    00295 ary_alloc(VALUE klass)
    00296 {
    00297     NEWOBJ(ary, struct RArray);
    00298     OBJSETUP(ary, klass, T_ARRAY);
    00299     FL_SET_EMBED((VALUE)ary);
    00300     ARY_SET_EMBED_LEN((VALUE)ary, 0);
    00301
    00302     return (VALUE)ary;
    00303 }
    00304
    00305 static VALUE
    00306 ary_new(VALUE klass, long capa)
    00307 {
    00308     VALUE ary;
    00309
    00310     if (capa < 0) {
    00311         rb_raise(rb_eArgError, "negative array size (or size too big)");
    00312     }
    00313     if (capa > ARY_MAX_SIZE) {
    00314         rb_raise(rb_eArgError, "array size too big");
    00315     }
    00316     ary = ary_alloc(klass);
    00317     if (capa > RARRAY_EMBED_LEN_MAX) {
    00318         FL_UNSET_EMBED(ary);
    00319         ARY_SET_PTR(ary, ALLOC_N(VALUE, capa));
    00320         ARY_SET_CAPA(ary, capa);
    00321         ARY_SET_HEAP_LEN(ary, 0);
    00322     }
    00323
    00324     return ary;
    00325 }
    00326
    00327 VALUE
    00328 rb_ary_new2(long capa)
    00329 {
    00330     return ary_new(rb_cArray, capa);
    00331 }
    00332
    00333
    00334 VALUE
    00335 rb_ary_new(void)
    00336 {
    00337     return rb_ary_new2(RARRAY_EMBED_LEN_MAX);
    00338 }
    00339
    00340 #include <stdarg.h>
    00341
    00342 VALUE
    00343 rb_ary_new3(long n, ...)
    00344 {
    00345     va_list ar;
    00346     VALUE ary;
    00347     long i;
    00348
    00349     ary = rb_ary_new2(n);
    00350
    00351     va_start(ar, n);
    00352     for (i=0; i<n; i++) {
    00353         RARRAY_PTR(ary)[i] = va_arg(ar, VALUE);
    00354     }
    00355     va_end(ar);
    00356
    00357     ARY_SET_LEN(ary, n);
    00358     return ary;
    00359 }
    00360
    00361 VALUE
    00362 rb_ary_new4(long n, const VALUE *elts)
    00363 {
    00364     VALUE ary;
    00365
    00366     ary = rb_ary_new2(n);
    00367     if (n > 0 && elts) {
    00368         MEMCPY(RARRAY_PTR(ary), elts, VALUE, n);
    00369         ARY_SET_LEN(ary, n);
    00370     }
    00371
    00372     return ary;
    00373 }
    00374
    00375 VALUE
    00376 rb_ary_tmp_new(long capa)
    00377 {
    00378     return ary_new(0, capa);
    00379 }
    00380
    00381 void
    00382 rb_ary_free(VALUE ary)
    00383 {
    00384     if (ARY_OWNS_HEAP_P(ary)) {
    00385         xfree(ARY_HEAP_PTR(ary));
    00386     }
    00387 }
    00388
    00389 size_t
    00390 rb_ary_memsize(VALUE ary)
    00391 {
    00392     if (ARY_OWNS_HEAP_P(ary)) {
    00393         return RARRAY(ary)->as.heap.aux.capa * sizeof(VALUE);
    00394     }
    00395     else {
    00396         return 0;
    00397     }
    00398 }
    00399
    00400 static inline void
    00401 ary_discard(VALUE ary)
    00402 {
    00403     rb_ary_free(ary);
    00404     RBASIC(ary)->flags |= RARRAY_EMBED_FLAG;
    00405     RBASIC(ary)->flags &= ~RARRAY_EMBED_LEN_MASK;
    00406 }
    00407
    00408 static VALUE
    00409 ary_make_shared(VALUE ary)
    00410 {
    00411     assert(!ARY_EMBED_P(ary));
    00412     if (ARY_SHARED_P(ary)) {
    00413         return ARY_SHARED(ary);
    00414     }
    00415     else if (ARY_SHARED_ROOT_P(ary)) {
    00416         return ary;
    00417     }
    00418     else if (OBJ_FROZEN(ary)) {
    00419         ary_resize_capa(ary, ARY_HEAP_LEN(ary));
    00420         FL_SET_SHARED_ROOT(ary);
    00421         ARY_SET_SHARED_NUM(ary, 1);
    00422         return ary;
    00423     }
    00424     else {
    00425         NEWOBJ(shared, struct RArray);
    00426         OBJSETUP(shared, 0, T_ARRAY);
    00427         FL_UNSET_EMBED(shared);
    00428
    00429         ARY_SET_LEN((VALUE)shared, RARRAY_LEN(ary));
    00430         ARY_SET_PTR((VALUE)shared, RARRAY_PTR(ary));
    00431         FL_SET_SHARED_ROOT(shared);
    00432         ARY_SET_SHARED_NUM((VALUE)shared, 1);
    00433         FL_SET_SHARED(ary);
    00434         ARY_SET_SHARED(ary, (VALUE)shared);
    00435         OBJ_FREEZE(shared);
    00436         return (VALUE)shared;
    00437     }
    00438 }
    00439
    00440
    00441 static VALUE
    00442 ary_make_substitution(VALUE ary)
    00443 {
    00444     if (RARRAY_LEN(ary) <= RARRAY_EMBED_LEN_MAX) {
    00445         VALUE subst = rb_ary_new2(RARRAY_LEN(ary));
    00446         MEMCPY(ARY_EMBED_PTR(subst), RARRAY_PTR(ary), VALUE, RARRAY_LEN(ary));
    00447         ARY_SET_EMBED_LEN(subst, RARRAY_LEN(ary));
    00448         return subst;
    00449     }
    00450     else {
    00451         return rb_ary_increment_share(ary_make_shared(ary));
    00452     }
    00453 }
    00454
    00455 VALUE
    00456 rb_assoc_new(VALUE car, VALUE cdr)
    00457 {
    00458     return rb_ary_new3(2, car, cdr);
    00459 }
    00460
    00461 static VALUE
    00462 to_ary(VALUE ary)
    00463 {
    00464     return rb_convert_type(ary, T_ARRAY, "Array", "to_ary");
    00465 }
    00466
    00467 VALUE
    00468 rb_check_array_type(VALUE ary)
    00469 {
    00470     return rb_check_convert_type(ary, T_ARRAY, "Array", "to_ary");
    00471 }
    00472
    00473 /*
    00474  *  call-seq:
    00475  *     Array.try_convert(obj) -> array or nil
    00476  *
    00477  *  Try to convert <i>obj</i> into an array, using +to_ary+ method.
    00478  *  Returns converted array or +nil+ if <i>obj</i> cannot be converted
    00479  *  for any reason. This method can be used to check if an argument is an
    00480  *  array.
    00481  *
    00482  *     Array.try_convert([1])   #=> [1]
    00483  *     Array.try_convert("1")   #=> nil
    00484  *
    00485  *     if tmp = Array.try_convert(arg)
    00486  *       # the argument is an array
    00487  *     elsif tmp = String.try_convert(arg)
    00488  *       # the argument is a string
    00489  *     end
    00490  *
    00491  */
    00492
    00493 static VALUE
    00494 rb_ary_s_try_convert(VALUE dummy, VALUE ary)
    00495 {
    00496     return rb_check_array_type(ary);
    00497 }
    00498
    00499 /*
    00500  *  call-seq:
    00501  *     Array.new(size=0, obj=nil)
    00502  *     Array.new(array)
    00503  *     Array.new(size) {|index| block }
    00504  *
    00505  *  Returns a new array. In the first form, the new array is
    00506  *  empty. In the second it is created with _size_ copies of _obj_
    00507  *  (that is, _size_ references to the same
    00508  *  _obj_). The third form creates a copy of the array
    00509  *  passed as a parameter (the array is generated by calling
    00510  *  to_ary  on the parameter). In the last form, an array
    00511  *  of the given size is created. Each element in this array is
    00512  *  calculated by passing the element's index to the given block and
    00513  *  storing the return value.
    00514  *
    00515  *     Array.new
    00516  *     Array.new(2)
    00517  *     Array.new(5, "A")
    00518  *
    00519  *     # only one copy of the object is created
    00520  *     a = Array.new(2, Hash.new)
    00521  *     a[0]['cat'] = 'feline'
    00522  *     a
    00523  *     a[1]['cat'] = 'Felix'
    00524  *     a
    00525  *
    00526  *     # here multiple copies are created
    00527  *     a = Array.new(2) { Hash.new }
    00528  *     a[0]['cat'] = 'feline'
    00529  *     a
    00530  *
    00531  *     squares = Array.new(5) {|i| i*i}
    00532  *     squares
    00533  *
    00534  *     copy = Array.new(squares)
    00535  */
    00536
    00537 static VALUE
    00538 rb_ary_initialize(int argc, VALUE *argv, VALUE ary)
    00539 {
    00540     long len;
    00541     VALUE size, val;
    00542
    00543     rb_ary_modify(ary);
    00544     if (argc == 0) {
    00545         if (ARY_OWNS_HEAP_P(ary) && RARRAY_PTR(ary)) {
    00546             xfree(RARRAY_PTR(ary));
    00547         }
    00548         rb_ary_unshare_safe(ary);
    00549         FL_SET_EMBED(ary);
    00550         ARY_SET_EMBED_LEN(ary, 0);
    00551         if (rb_block_given_p()) {
    00552             rb_warning("given block not used");
    00553         }
    00554         return ary;
    00555     }
    00556     rb_scan_args(argc, argv, "02", &size, &val);
    00557     if (argc == 1 && !FIXNUM_P(size)) {
    00558         val = rb_check_array_type(size);
    00559         if (!NIL_P(val)) {
    00560             rb_ary_replace(ary, val);
    00561             return ary;
    00562         }
    00563     }
    00564
    00565     len = NUM2LONG(size);
    00566     if (len < 0) {
    00567         rb_raise(rb_eArgError, "negative array size");
    00568     }
    00569     if (len > ARY_MAX_SIZE) {
    00570         rb_raise(rb_eArgError, "array size too big");
    00571     }
    00572     rb_ary_modify(ary);
    00573     ary_resize_capa(ary, len);
    00574     if (rb_block_given_p()) {
    00575         long i;
    00576
    00577         if (argc == 2) {
    00578             rb_warn("block supersedes default value argument");
    00579         }
    00580         for (i=0; i<len; i++) {
    00581             rb_ary_store(ary, i, rb_yield(LONG2NUM(i)));
    00582             ARY_SET_LEN(ary, i + 1);
    00583         }
    00584     }
    00585     else {
    00586         memfill(RARRAY_PTR(ary), len, val);
    00587         ARY_SET_LEN(ary, len);
    00588     }
    00589     return ary;
    00590 }
    00591
    00592
    00593 /*
    00594 * Returns a new array populated with the given objects.
    00595 *
    00596 *   Array.[]( 1, 'a', /^A/ )
    00597 *   Array[ 1, 'a', /^A/ ]
    00598 *   [ 1, 'a', /^A/ ]
    00599 */
    00600
    00601 static VALUE
    00602 rb_ary_s_create(int argc, VALUE *argv, VALUE klass)
    00603 {
    00604     VALUE ary = ary_new(klass, argc);
    00605     if (argc > 0 && argv) {
    00606         MEMCPY(RARRAY_PTR(ary), argv, VALUE, argc);
    00607         ARY_SET_LEN(ary, argc);
    00608     }
    00609
    00610     return ary;
    00611 }
    00612
    00613 void
    00614 rb_ary_store(VALUE ary, long idx, VALUE val)
    00615 {
    00616     if (idx < 0) {
    00617         idx += RARRAY_LEN(ary);
    00618         if (idx < 0) {
    00619             rb_raise(rb_eIndexError, "index %ld too small for array; minimum: %ld",
    00620                      idx - RARRAY_LEN(ary), -RARRAY_LEN(ary));
    00621         }
    00622     }
    00623     else if (idx >= ARY_MAX_SIZE) {
    00624         rb_raise(rb_eIndexError, "index %ld too big", idx);
    00625     }
    00626
    00627     rb_ary_modify(ary);
    00628     if (idx >= ARY_CAPA(ary)) {
    00629         ary_double_capa(ary, idx);
    00630     }
    00631     if (idx > RARRAY_LEN(ary)) {
    00632         rb_mem_clear(RARRAY_PTR(ary) + RARRAY_LEN(ary),
    00633                      idx-RARRAY_LEN(ary) + 1);
    00634     }
    00635
    00636     if (idx >= RARRAY_LEN(ary)) {
    00637         ARY_SET_LEN(ary, idx + 1);
    00638     }
    00639     RARRAY_PTR(ary)[idx] = val;
    00640 }
    00641
    00642 static VALUE
    00643 ary_make_partial(VALUE ary, VALUE klass, long offset, long len)
    00644 {
    00645     assert(offset >= 0);
    00646     assert(len >= 0);
    00647     assert(offset+len <= RARRAY_LEN(ary));
    00648
    00649     if (len <= RARRAY_EMBED_LEN_MAX) {
    00650         VALUE result = ary_alloc(klass);
    00651         MEMCPY(ARY_EMBED_PTR(result), RARRAY_PTR(ary) + offset, VALUE, len);
    00652         ARY_SET_EMBED_LEN(result, len);
    00653         return result;
    00654     }
    00655     else {
    00656         VALUE shared, result = ary_alloc(klass);
    00657         FL_UNSET_EMBED(result);
    00658
    00659         shared = ary_make_shared(ary);
    00660         ARY_SET_PTR(result, RARRAY_PTR(ary));
    00661         ARY_SET_LEN(result, RARRAY_LEN(ary));
    00662         rb_ary_set_shared(result, shared);
    00663
    00664         ARY_INCREASE_PTR(result, offset);
    00665         ARY_SET_LEN(result, len);
    00666         return result;
    00667     }
    00668 }
    00669
    00670 static VALUE
    00671 ary_make_shared_copy(VALUE ary)
    00672 {
    00673     return ary_make_partial(ary, rb_obj_class(ary), 0, RARRAY_LEN(ary));
    00674 }
    00675
    00676 enum ary_take_pos_flags
    00677 {
    00678     ARY_TAKE_FIRST = 0,
    00679     ARY_TAKE_LAST = 1
    00680 };
    00681
    00682 static VALUE
    00683 ary_take_first_or_last(int argc, VALUE *argv, VALUE ary, enum ary_take_pos_flags last)
    00684 {
    00685     VALUE nv;
    00686     long n;
    00687     long offset = 0;
    00688
    00689     rb_scan_args(argc, argv, "1", &nv);
    00690     n = NUM2LONG(nv);
    00691     if (n > RARRAY_LEN(ary)) {
    00692         n = RARRAY_LEN(ary);
    00693     }
    00694     else if (n < 0) {
    00695         rb_raise(rb_eArgError, "negative array size");
    00696     }
    00697     if (last) {
    00698         offset = RARRAY_LEN(ary) - n;
    00699     }
    00700     return ary_make_partial(ary, rb_cArray, offset, n);
    00701 }
    00702
    00703 static VALUE rb_ary_push_1(VALUE ary, VALUE item);
    00704
    00705 /*
    00706  *  call-seq:
    00707  *     ary << obj            -> ary
    00708  *
    00709  *  Append---Pushes the given object on to the end of this array. This
    00710  *  expression returns the array itself, so several appends
    00711  *  may be chained together.
    00712  *
    00713  *     [ 1, 2 ] << "c" << "d" << [ 3, 4 ]
    00714  *             #=>  [ 1, 2, "c", "d", [ 3, 4 ] ]
    00715  *
    00716  */
    00717
    00718 VALUE
    00719 rb_ary_push(VALUE ary, VALUE item)
    00720 {
    00721     rb_ary_modify(ary);
    00722     return rb_ary_push_1(ary, item);
    00723 }
    00724
    00725 static VALUE
    00726 rb_ary_push_1(VALUE ary, VALUE item)
    00727 {
    00728     long idx = RARRAY_LEN(ary);
    00729
    00730     if (idx >= ARY_CAPA(ary)) {
    00731         ary_double_capa(ary, idx);
    00732     }
    00733     RARRAY_PTR(ary)[idx] = item;
    00734     ARY_SET_LEN(ary, idx + 1);
    00735     return ary;
    00736 }
    00737
    00738 /*
    00739  *  call-seq:
    00740  *     ary.push(obj, ... )   -> ary
    00741  *
    00742  *  Append---Pushes the given object(s) on to the end of this array. This
    00743  *  expression returns the array itself, so several appends
    00744  *  may be chained together.
    00745  *
    00746  *     a = [ "a", "b", "c" ]
    00747  *     a.push("d", "e", "f")
    00748  *             #=> ["a", "b", "c", "d", "e", "f"]
    00749  */
    00750
    00751 static VALUE
    00752 rb_ary_push_m(int argc, VALUE *argv, VALUE ary)
    00753 {
    00754     rb_ary_modify(ary);
    00755     while (argc--) {
    00756         rb_ary_push_1(ary, *argv++);
    00757     }
    00758     return ary;
    00759 }
    00760
    00761 VALUE
    00762 rb_ary_pop(VALUE ary)
    00763 {
    00764     long n;
    00765     rb_ary_modify_check(ary);
    00766     if (RARRAY_LEN(ary) == 0) return Qnil;
    00767     if (ARY_OWNS_HEAP_P(ary) &&
    00768         RARRAY_LEN(ary) * 3 < ARY_CAPA(ary) &&
    00769         ARY_CAPA(ary) > ARY_DEFAULT_SIZE)
    00770     {
    00771         ary_resize_capa(ary, RARRAY_LEN(ary) * 2);
    00772     }
    00773     n = RARRAY_LEN(ary)-1;
    00774     ARY_SET_LEN(ary, n);
    00775     return RARRAY_PTR(ary)[n];
    00776 }
    00777
    00778 /*
    00779  *  call-seq:
    00780  *     ary.pop    -> obj or nil
    00781  *     ary.pop(n) -> new_ary
    00782  *
    00783  *  Removes the last element from +self+ and returns it, or
    00784  *  <code>nil</code> if the array is empty.
    00785  *
    00786  *  If a number _n_ is given, returns an array of the last n elements
    00787  *  (or less) just like <code>array.slice!(-n, n)</code> does.
    00788  *
    00789  *     a = [ "a", "b", "c", "d" ]
    00790  *     a.pop     #=> "d"
    00791  *     a.pop(2)  #=> ["b", "c"]
    00792  *     a         #=> ["a"]
    00793  */
    00794
    00795 static VALUE
    00796 rb_ary_pop_m(int argc, VALUE *argv, VALUE ary)
    00797 {
    00798     VALUE result;
    00799
    00800     if (argc == 0) {
    00801         return rb_ary_pop(ary);
    00802     }
    00803
    00804     rb_ary_modify_check(ary);
    00805     result = ary_take_first_or_last(argc, argv, ary, ARY_TAKE_LAST);
    00806     ARY_INCREASE_LEN(ary, -RARRAY_LEN(result));
    00807     return result;
    00808 }
    00809
    00810 VALUE
    00811 rb_ary_shift(VALUE ary)
    00812 {
    00813     VALUE top;
    00814
    00815     rb_ary_modify_check(ary);
    00816     if (RARRAY_LEN(ary) == 0) return Qnil;
    00817     top = RARRAY_PTR(ary)[0];
    00818     if (!ARY_SHARED_P(ary)) {
    00819         if (RARRAY_LEN(ary) < ARY_DEFAULT_SIZE) {
    00820             MEMMOVE(RARRAY_PTR(ary), RARRAY_PTR(ary)+1, VALUE, RARRAY_LEN(ary)-1);
    00821             ARY_INCREASE_LEN(ary, -1);
    00822             return top;
    00823         }
    00824         assert(!ARY_EMBED_P(ary)); /* ARY_EMBED_LEN_MAX < ARY_DEFAULT_SIZE */
    00825
    00826         RARRAY_PTR(ary)[0] = Qnil;
    00827         ary_make_shared(ary);
    00828     }
    00829     else if (ARY_SHARED_NUM(ARY_SHARED(ary)) == 1) {
    00830         RARRAY_PTR(ary)[0] = Qnil;
    00831     }
    00832     ARY_INCREASE_PTR(ary, 1);           /* shift ptr */
    00833     ARY_INCREASE_LEN(ary, -1);
    00834
    00835     return top;
    00836 }
    00837
    00838 /*
    00839  *  call-seq:
    00840  *     ary.shift    -> obj or nil
    00841  *     ary.shift(n) -> new_ary
    00842  *
    00843  *  Returns the first element of +self+ and removes it (shifting all
    00844  *  other elements down by one). Returns <code>nil</code> if the array
    00845  *  is empty.
    00846  *
    00847  *  If a number _n_ is given, returns an array of the first n elements
    00848  *  (or less) just like <code>array.slice!(0, n)</code> does.
    00849  *
    00850  *     args = [ "-m", "-q", "filename" ]
    00851  *     args.shift     #=> "-m"
    00852  *     args           #=> ["-q", "filename"]
    00853  *
    00854  *     args = [ "-m", "-q", "filename" ]
    00855  *     args.shift(2)  #=> ["-m", "-q"]
    00856  *     args           #=> ["filename"]
    00857  */
    00858
    00859 static VALUE
    00860 rb_ary_shift_m(int argc, VALUE *argv, VALUE ary)
    00861 {
    00862     VALUE result;
    00863     long n;
    00864
    00865     if (argc == 0) {
    00866         return rb_ary_shift(ary);
    00867     }
    00868
    00869     rb_ary_modify_check(ary);
    00870     result = ary_take_first_or_last(argc, argv, ary, ARY_TAKE_FIRST);
    00871     n = RARRAY_LEN(result);
    00872     if (ARY_SHARED_P(ary)) {
    00873         if (ARY_SHARED_NUM(ARY_SHARED(ary)) == 1) {
    00874             rb_mem_clear(RARRAY_PTR(ary), n);
    00875         }
    00876         ARY_INCREASE_PTR(ary, n);
    00877     }
    00878     else {
    00879         MEMMOVE(RARRAY_PTR(ary), RARRAY_PTR(ary)+n, VALUE, RARRAY_LEN(ary)-n);
    00880     }
    00881     ARY_INCREASE_LEN(ary, -n);
    00882
    00883     return result;
    00884 }
    00885
    00886 /*
    00887  *  call-seq:
    00888  *     ary.unshift(obj, ...)  -> ary
    00889  *
    00890  *  Prepends objects to the front of +self+,
    00891  *  moving other elements upwards.
    00892  *
    00893  *     a = [ "b", "c", "d" ]
    00894  *     a.unshift("a")   #=> ["a", "b", "c", "d"]
    00895  *     a.unshift(1, 2)  #=> [ 1, 2, "a", "b", "c", "d"]
    00896  */
    00897
    00898 static VALUE
    00899 rb_ary_unshift_m(int argc, VALUE *argv, VALUE ary)
    00900 {
    00901     long len;
    00902
    00903     rb_ary_modify(ary);
    00904     if (argc == 0) return ary;
    00905     if (ARY_CAPA(ary) <= (len = RARRAY_LEN(ary)) + argc) {
    00906         ary_double_capa(ary, len + argc);
    00907     }
    00908
    00909     /* sliding items */
    00910     MEMMOVE(RARRAY_PTR(ary) + argc, RARRAY_PTR(ary), VALUE, len);
    00911     MEMCPY(RARRAY_PTR(ary), argv, VALUE, argc);
    00912     ARY_INCREASE_LEN(ary, argc);
    00913
    00914     return ary;
    00915 }
    00916
    00917 VALUE
    00918 rb_ary_unshift(VALUE ary, VALUE item)
    00919 {
    00920     return rb_ary_unshift_m(1,&item,ary);
    00921 }
    00922
    00923 /* faster version - use this if you don't need to treat negative offset */
    00924 static inline VALUE
    00925 rb_ary_elt(VALUE ary, long offset)
    00926 {
    00927     if (RARRAY_LEN(ary) == 0) return Qnil;
    00928     if (offset < 0 || RARRAY_LEN(ary) <= offset) {
    00929         return Qnil;
    00930     }
    00931     return RARRAY_PTR(ary)[offset];
    00932 }
    00933
    00934 VALUE
    00935 rb_ary_entry(VALUE ary, long offset)
    00936 {
    00937     if (offset < 0) {
    00938         offset += RARRAY_LEN(ary);
    00939     }
    00940     return rb_ary_elt(ary, offset);
    00941 }
    00942
    00943 VALUE
    00944 rb_ary_subseq(VALUE ary, long beg, long len)
    00945 {
    00946     VALUE klass;
    00947
    00948     if (beg > RARRAY_LEN(ary)) return Qnil;
    00949     if (beg < 0 || len < 0) return Qnil;
    00950
    00951     if (RARRAY_LEN(ary) < len || RARRAY_LEN(ary) < beg + len) {
    00952         len = RARRAY_LEN(ary) - beg;
    00953     }
    00954     klass = rb_obj_class(ary);
    00955     if (len == 0) return ary_new(klass, 0);
    00956
    00957     return ary_make_partial(ary, klass, beg, len);
    00958 }
    00959
    00960 /*
    00961  *  call-seq:
    00962  *     ary[index]                -> obj     or nil
    00963  *     ary[start, length]        -> new_ary or nil
    00964  *     ary[range]                -> new_ary or nil
    00965  *     ary.slice(index)          -> obj     or nil
    00966  *     ary.slice(start, length)  -> new_ary or nil
    00967  *     ary.slice(range)          -> new_ary or nil
    00968  *
    00969  *  Element Reference---Returns the element at _index_,
    00970  *  or returns a subarray starting at _start_ and
    00971  *  continuing for _length_ elements, or returns a subarray
    00972  *  specified by _range_.
    00973  *  Negative indices count backward from the end of the
    00974  *  array (-1 is the last element). Returns +nil+ if the index
    00975  *  (or starting index) are out of range.
    00976  *
    00977  *     a = [ "a", "b", "c", "d", "e" ]
    00978  *     a[2] +  a[0] + a[1]    #=> "cab"
    00979  *     a[6]                   #=> nil
    00980  *     a[1, 2]                #=> [ "b", "c" ]
    00981  *     a[1..3]                #=> [ "b", "c", "d" ]
    00982  *     a[4..7]                #=> [ "e" ]
    00983  *     a[6..10]               #=> nil
    00984  *     a[-3, 3]               #=> [ "c", "d", "e" ]
    00985  *     # special cases
    00986  *     a[5]                   #=> nil
    00987  *     a[5, 1]                #=> []
    00988  *     a[5..10]               #=> []
    00989  *
    00990  */
    00991
    00992 VALUE
    00993 rb_ary_aref(int argc, VALUE *argv, VALUE ary)
    00994 {
    00995     VALUE arg;
    00996     long beg, len;
    00997
    00998     if (argc == 2) {
    00999         beg = NUM2LONG(argv[0]);
    01000         len = NUM2LONG(argv[1]);
    01001         if (beg < 0) {
    01002             beg += RARRAY_LEN(ary);
    01003         }
    01004         return rb_ary_subseq(ary, beg, len);
    01005     }
    01006     if (argc != 1) {
    01007         rb_scan_args(argc, argv, "11", 0, 0);
    01008     }
    01009     arg = argv[0];
    01010     /* special case - speeding up */
    01011     if (FIXNUM_P(arg)) {
    01012         return rb_ary_entry(ary, FIX2LONG(arg));
    01013     }
    01014     /* check if idx is Range */
    01015     switch (rb_range_beg_len(arg, &beg, &len, RARRAY_LEN(ary), 0)) {
    01016       case Qfalse:
    01017         break;
    01018       case Qnil:
    01019         return Qnil;
    01020       default:
    01021         return rb_ary_subseq(ary, beg, len);
    01022     }
    01023     return rb_ary_entry(ary, NUM2LONG(arg));
    01024 }
    01025
    01026 /*
    01027  *  call-seq:
    01028  *     ary.at(index)   ->   obj  or nil
    01029  *
    01030  *  Returns the element at _index_. A
    01031  *  negative index counts from the end of +self+.  Returns +nil+
    01032  *  if the index is out of range. See also <code>Array#[]</code>.
    01033  *
    01034  *     a = [ "a", "b", "c", "d", "e" ]
    01035  *     a.at(0)     #=> "a"
    01036  *     a.at(-1)    #=> "e"
    01037  */
    01038
    01039 static VALUE
    01040 rb_ary_at(VALUE ary, VALUE pos)
    01041 {
    01042     return rb_ary_entry(ary, NUM2LONG(pos));
    01043 }
    01044
    01045 /*
    01046  *  call-seq:
    01047  *     ary.first     ->   obj or nil
    01048  *     ary.first(n)  ->   new_ary
    01049  *
    01050  *  Returns the first element, or the first +n+ elements, of the array.
    01051  *  If the array is empty, the first form returns <code>nil</code>, and the
    01052  *  second form returns an empty array.
    01053  *
    01054  *     a = [ "q", "r", "s", "t" ]
    01055  *     a.first     #=> "q"
    01056  *     a.first(2)  #=> ["q", "r"]
    01057  */
    01058
    01059 static VALUE
    01060 rb_ary_first(int argc, VALUE *argv, VALUE ary)
    01061 {
    01062     if (argc == 0) {
    01063         if (RARRAY_LEN(ary) == 0) return Qnil;
    01064         return RARRAY_PTR(ary)[0];
    01065     }
    01066     else {
    01067         return ary_take_first_or_last(argc, argv, ary, ARY_TAKE_FIRST);
    01068     }
    01069 }
    01070
    01071 /*
    01072  *  call-seq:
    01073  *     ary.last     ->  obj or nil
    01074  *     ary.last(n)  ->  new_ary
    01075  *
    01076  *  Returns the last element(s) of +self+. If the array is empty,
    01077  *  the first form returns <code>nil</code>.
    01078  *
    01079  *     a = [ "w", "x", "y", "z" ]
    01080  *     a.last     #=> "z"
    01081  *     a.last(2)  #=> ["y", "z"]
    01082  */
    01083
    01084 VALUE
    01085 rb_ary_last(int argc, VALUE *argv, VALUE ary)
    01086 {
    01087     if (argc == 0) {
    01088         if (RARRAY_LEN(ary) == 0) return Qnil;
    01089         return RARRAY_PTR(ary)[RARRAY_LEN(ary)-1];
    01090     }
    01091     else {
    01092         return ary_take_first_or_last(argc, argv, ary, ARY_TAKE_LAST);
    01093     }
    01094 }
    01095
    01096 /*
    01097  *  call-seq:
    01098  *     ary.fetch(index)                    -> obj
    01099  *     ary.fetch(index, default )          -> obj
    01100  *     ary.fetch(index) {|index| block }   -> obj
    01101  *
    01102  *  Tries to return the element at position <i>index</i>. If the index
    01103  *  lies outside the array, the first form throws an
    01104  *  <code>IndexError</code> exception, the second form returns
    01105  *  <i>default</i>, and the third form returns the value of invoking
    01106  *  the block, passing in the index. Negative values of <i>index</i>
    01107  *  count from the end of the array.
    01108  *
    01109  *     a = [ 11, 22, 33, 44 ]
    01110  *     a.fetch(1)               #=> 22
    01111  *     a.fetch(-1)              #=> 44
    01112  *     a.fetch(4, 'cat')        #=> "cat"
    01113  *     a.fetch(4) { |i| i*i }   #=> 16
    01114  */
    01115
    01116 static VALUE
    01117 rb_ary_fetch(int argc, VALUE *argv, VALUE ary)
    01118 {
    01119     VALUE pos, ifnone;
    01120     long block_given;
    01121     long idx;
    01122
    01123     rb_scan_args(argc, argv, "11", &pos, &ifnone);
    01124     block_given = rb_block_given_p();
    01125     if (block_given && argc == 2) {
    01126         rb_warn("block supersedes default value argument");
    01127     }
    01128     idx = NUM2LONG(pos);
    01129
    01130     if (idx < 0) {
    01131         idx +=  RARRAY_LEN(ary);
    01132     }
    01133     if (idx < 0 || RARRAY_LEN(ary) <= idx) {
    01134         if (block_given) return rb_yield(pos);
    01135         if (argc == 1) {
    01136             rb_raise(rb_eIndexError, "index %ld outside of array bounds: %ld...%ld",
    01137                         idx - (idx < 0 ? RARRAY_LEN(ary) : 0), -RARRAY_LEN(ary), RARRAY_LEN(ary));
    01138         }
    01139         return ifnone;
    01140     }
    01141     return RARRAY_PTR(ary)[idx];
    01142 }
    01143
    01144 /*
    01145  *  call-seq:
    01146  *     ary.index(obj)           ->  int or nil
    01147  *     ary.index {|item| block} ->  int or nil
    01148  *     ary.index                ->  an_enumerator
    01149  *
    01150  *  Returns the index of the first object in +self+ such that is
    01151  *  <code>==</code> to <i>obj</i>. If a block is given instead of an
    01152  *  argument, returns first object for which <em>block</em> is true.
    01153  *  Returns <code>nil</code> if no match is found.
    01154  *  See also <code>Array#rindex</code>.
    01155  *
    01156  *  If neither block nor argument is given, an enumerator is returned instead.
    01157  *
    01158  *     a = [ "a", "b", "c" ]
    01159  *     a.index("b")        #=> 1
    01160  *     a.index("z")        #=> nil
    01161  *     a.index{|x|x=="b"}  #=> 1
    01162  *
    01163  *  This is an alias of <code>#find_index</code>.
    01164  */
    01165
    01166 static VALUE
    01167 rb_ary_index(int argc, VALUE *argv, VALUE ary)
    01168 {
    01169     VALUE val;
    01170     long i;
    01171
    01172     if (argc == 0) {
    01173         RETURN_ENUMERATOR(ary, 0, 0);
    01174         for (i=0; i<RARRAY_LEN(ary); i++) {
    01175             if (RTEST(rb_yield(RARRAY_PTR(ary)[i]))) {
    01176                 return LONG2NUM(i);
    01177             }
    01178         }
    01179         return Qnil;
    01180     }
    01181     rb_scan_args(argc, argv, "1", &val);
    01182     if (rb_block_given_p())
    01183         rb_warn("given block not used");
    01184     for (i=0; i<RARRAY_LEN(ary); i++) {
    01185         if (rb_equal(RARRAY_PTR(ary)[i], val))
    01186             return LONG2NUM(i);
    01187     }
    01188     return Qnil;
    01189 }
    01190
    01191 /*
    01192  *  call-seq:
    01193  *     ary.rindex(obj)           ->  int or nil
    01194  *     ary.rindex {|item| block} ->  int or nil
    01195  *     ary.rindex                ->  an_enumerator
    01196  *
    01197  *  Returns the index of the last object in +self+
    01198  *  <code>==</code> to <i>obj</i>. If a block is given instead of an
    01199  *  argument, returns first object for which <em>block</em> is
    01200  *  true, starting from the last object.
    01201  *  Returns <code>nil</code> if no match is found.
    01202  *  See also <code>Array#index</code>.
    01203  *
    01204  *  If neither block nor argument is given, an enumerator is returned instead.
    01205  *
    01206  *     a = [ "a", "b", "b", "b", "c" ]
    01207  *     a.rindex("b")        #=> 3
    01208  *     a.rindex("z")        #=> nil
    01209  *     a.rindex{|x|x=="b"}  #=> 3
    01210  */
    01211
    01212 static VALUE
    01213 rb_ary_rindex(int argc, VALUE *argv, VALUE ary)
    01214 {
    01215     VALUE val;
    01216     long i = RARRAY_LEN(ary);
    01217
    01218     if (argc == 0) {
    01219         RETURN_ENUMERATOR(ary, 0, 0);
    01220         while (i--) {
    01221             if (RTEST(rb_yield(RARRAY_PTR(ary)[i])))
    01222                 return LONG2NUM(i);
    01223             if (i > RARRAY_LEN(ary)) {
    01224                 i = RARRAY_LEN(ary);
    01225             }
    01226         }
    01227         return Qnil;
    01228     }
    01229     rb_scan_args(argc, argv, "1", &val);
    01230     if (rb_block_given_p())
    01231         rb_warn("given block not used");
    01232     while (i--) {
    01233         if (rb_equal(RARRAY_PTR(ary)[i], val))
    01234             return LONG2NUM(i);
    01235         if (i > RARRAY_LEN(ary)) {
    01236             i = RARRAY_LEN(ary);
    01237         }
    01238     }
    01239     return Qnil;
    01240 }
    01241
    01242 VALUE
    01243 rb_ary_to_ary(VALUE obj)
    01244 {
    01245     VALUE tmp = rb_check_array_type(obj);
    01246
    01247     if (!NIL_P(tmp)) return tmp;
    01248     return rb_ary_new3(1, obj);
    01249 }
    01250
    01251 static void
    01252 rb_ary_splice(VALUE ary, long beg, long len, VALUE rpl)
    01253 {
    01254     long rlen;
    01255
    01256     if (len < 0) rb_raise(rb_eIndexError, "negative length (%ld)", len);
    01257     if (beg < 0) {
    01258         beg += RARRAY_LEN(ary);
    01259         if (beg < 0) {
    01260             rb_raise(rb_eIndexError, "index %ld too small for array; minimum: %ld",
    01261                      beg - RARRAY_LEN(ary), -RARRAY_LEN(ary));
    01262         }
    01263     }
    01264     if (RARRAY_LEN(ary) < len || RARRAY_LEN(ary) < beg + len) {
    01265         len = RARRAY_LEN(ary) - beg;
    01266     }
    01267
    01268     if (rpl == Qundef) {
    01269         rlen = 0;
    01270     }
    01271     else {
    01272         rpl = rb_ary_to_ary(rpl);
    01273         rlen = RARRAY_LEN(rpl);
    01274     }
    01275     rb_ary_modify(ary);
    01276     if (beg >= RARRAY_LEN(ary)) {
    01277         if (beg > ARY_MAX_SIZE - rlen) {
    01278             rb_raise(rb_eIndexError, "index %ld too big", beg);
    01279         }
    01280         len = beg + rlen;
    01281         if (len >= ARY_CAPA(ary)) {
    01282             ary_double_capa(ary, len);
    01283         }
    01284         rb_mem_clear(RARRAY_PTR(ary) + RARRAY_LEN(ary), beg - RARRAY_LEN(ary));
    01285         if (rlen > 0) {
    01286             MEMCPY(RARRAY_PTR(ary) + beg, RARRAY_PTR(rpl), VALUE, rlen);
    01287         }
    01288         ARY_SET_LEN(ary, len);
    01289     }
    01290     else {
    01291         long alen;
    01292
    01293         alen = RARRAY_LEN(ary) + rlen - len;
    01294         if (alen >= ARY_CAPA(ary)) {
    01295             ary_double_capa(ary, alen);
    01296         }
    01297
    01298         if (len != rlen) {
    01299             MEMMOVE(RARRAY_PTR(ary) + beg + rlen, RARRAY_PTR(ary) + beg + len,
    01300                     VALUE, RARRAY_LEN(ary) - (beg + len));
    01301             ARY_SET_LEN(ary, alen);
    01302         }
    01303         if (rlen > 0) {
    01304             MEMMOVE(RARRAY_PTR(ary) + beg, RARRAY_PTR(rpl), VALUE, rlen);
    01305         }
    01306     }
    01307 }
    01308
    01309 /*
    01310  *  call-seq:
    01311  *     ary[index]         = obj                      ->  obj
    01312  *     ary[start, length] = obj or other_ary or nil  ->  obj or other_ary or nil
    01313  *     ary[range]         = obj or other_ary or nil  ->  obj or other_ary or nil
    01314  *
    01315  *  Element Assignment---Sets the element at _index_,
    01316  *  or replaces a subarray starting at _start_ and
    01317  *  continuing for _length_ elements, or replaces a subarray
    01318  *  specified by _range_.  If indices are greater than
    01319  *  the current capacity of the array, the array grows
    01320  *  automatically. A negative indices will count backward
    01321  *  from the end of the array. Inserts elements if _length_ is
    01322  *  zero. An +IndexError+ is raised if a negative index points
    01323  *  past the beginning of the array. See also
    01324  *  <code>Array#push</code>, and <code>Array#unshift</code>.
    01325  *
    01326  *     a = Array.new
    01327  *     a[4] = "4";                 #=> [nil, nil, nil, nil, "4"]
    01328  *     a[0, 3] = [ 'a', 'b', 'c' ] #=> ["a", "b", "c", nil, "4"]
    01329  *     a[1..2] = [ 1, 2 ]          #=> ["a", 1, 2, nil, "4"]
    01330  *     a[0, 2] = "?"               #=> ["?", 2, nil, "4"]
    01331  *     a[0..2] = "A"               #=> ["A", "4"]
    01332  *     a[-1]   = "Z"               #=> ["A", "Z"]
    01333  *     a[1..-1] = nil              #=> ["A", nil]
    01334  *     a[1..-1] = []               #=> ["A"]
    01335  */
    01336
    01337 static VALUE
    01338 rb_ary_aset(int argc, VALUE *argv, VALUE ary)
    01339 {
    01340     long offset, beg, len;
    01341
    01342     if (argc == 3) {
    01343         rb_ary_modify_check(ary);
    01344         beg = NUM2LONG(argv[0]);
    01345         len = NUM2LONG(argv[1]);
    01346         rb_ary_splice(ary, beg, len, argv[2]);
    01347         return argv[2];
    01348     }
    01349     if (argc != 2) {
    01350         rb_raise(rb_eArgError, "wrong number of arguments (%d for 2)", argc);
    01351     }
    01352     rb_ary_modify_check(ary);
    01353     if (FIXNUM_P(argv[0])) {
    01354         offset = FIX2LONG(argv[0]);
    01355         goto fixnum;
    01356     }
    01357     if (rb_range_beg_len(argv[0], &beg, &len, RARRAY_LEN(ary), 1)) {
    01358         /* check if idx is Range */
    01359         rb_ary_splice(ary, beg, len, argv[1]);
    01360         return argv[1];
    01361     }
    01362
    01363     offset = NUM2LONG(argv[0]);
    01364 fixnum:
    01365     rb_ary_store(ary, offset, argv[1]);
    01366     return argv[1];
    01367 }
    01368
    01369 /*
    01370  *  call-seq:
    01371  *     ary.insert(index, obj...)  -> ary
    01372  *
    01373  *  Inserts the given values before the element with the given index
    01374  *  (which may be negative).
    01375  *
    01376  *     a = %w{ a b c d }
    01377  *     a.insert(2, 99)         #=> ["a", "b", 99, "c", "d"]
    01378  *     a.insert(-2, 1, 2, 3)   #=> ["a", "b", 99, "c", 1, 2, 3, "d"]
    01379  */
    01380
    01381 static VALUE
    01382 rb_ary_insert(int argc, VALUE *argv, VALUE ary)
    01383 {
    01384     long pos;
    01385
    01386     if (argc < 1) {
    01387         rb_raise(rb_eArgError, "wrong number of arguments (at least 1)");
    01388     }
    01389     rb_ary_modify_check(ary);
    01390     if (argc == 1) return ary;
    01391     pos = NUM2LONG(argv[0]);
    01392     if (pos == -1) {
    01393         pos = RARRAY_LEN(ary);
    01394     }
    01395     if (pos < 0) {
    01396         pos++;
    01397     }
    01398     rb_ary_splice(ary, pos, 0, rb_ary_new4(argc - 1, argv + 1));
    01399     return ary;
    01400 }
    01401
    01402 /*
    01403  *  call-seq:
    01404  *     ary.each {|item| block }   -> ary
    01405  *     ary.each                   -> an_enumerator
    01406  *
    01407  *  Calls <i>block</i> once for each element in +self+, passing that
    01408  *  element as a parameter.
    01409  *
    01410  *  If no block is given, an enumerator is returned instead.
    01411  *
    01412  *     a = [ "a", "b", "c" ]
    01413  *     a.each {|x| print x, " -- " }
    01414  *
    01415  *  produces:
    01416  *
    01417  *     a -- b -- c --
    01418  */
    01419
    01420 VALUE
    01421 rb_ary_each(VALUE ary)
    01422 {
    01423     long i;
    01424
    01425     RETURN_ENUMERATOR(ary, 0, 0);
    01426     for (i=0; i<RARRAY_LEN(ary); i++) {
    01427         rb_yield(RARRAY_PTR(ary)[i]);
    01428     }
    01429     return ary;
    01430 }
    01431
    01432 /*
    01433  *  call-seq:
    01434  *     ary.each_index {|index| block }  -> ary
    01435  *     ary.each_index                   -> an_enumerator
    01436  *
    01437  *  Same as <code>Array#each</code>, but passes the index of the element
    01438  *  instead of the element itself.
    01439  *
    01440  *  If no block is given, an enumerator is returned instead.
    01441  *
    01442  *
    01443  *     a = [ "a", "b", "c" ]
    01444  *     a.each_index {|x| print x, " -- " }
    01445  *
    01446  *  produces:
    01447  *
    01448  *     0 -- 1 -- 2 --
    01449  */
    01450
    01451 static VALUE
    01452 rb_ary_each_index(VALUE ary)
    01453 {
    01454     long i;
    01455     RETURN_ENUMERATOR(ary, 0, 0);
    01456
    01457     for (i=0; i<RARRAY_LEN(ary); i++) {
    01458         rb_yield(LONG2NUM(i));
    01459     }
    01460     return ary;
    01461 }
    01462
    01463 /*
    01464  *  call-seq:
    01465  *     ary.reverse_each {|item| block }   -> ary
    01466  *     ary.reverse_each                   -> an_enumerator
    01467  *
    01468  *  Same as <code>Array#each</code>, but traverses +self+ in reverse
    01469  *  order.
    01470  *
    01471  *     a = [ "a", "b", "c" ]
    01472  *     a.reverse_each {|x| print x, " " }
    01473  *
    01474  *  produces:
    01475  *
    01476  *     c b a
    01477  */
    01478
    01479 static VALUE
    01480 rb_ary_reverse_each(VALUE ary)
    01481 {
    01482     long len;
    01483
    01484     RETURN_ENUMERATOR(ary, 0, 0);
    01485     len = RARRAY_LEN(ary);
    01486     while (len--) {
    01487         rb_yield(RARRAY_PTR(ary)[len]);
    01488         if (RARRAY_LEN(ary) < len) {
    01489             len = RARRAY_LEN(ary);
    01490         }
    01491     }
    01492     return ary;
    01493 }
    01494
    01495 /*
    01496  *  call-seq:
    01497  *     ary.length -> int
    01498  *
    01499  *  Returns the number of elements in +self+. May be zero.
    01500  *
    01501  *     [ 1, 2, 3, 4, 5 ].length   #=> 5
    01502  */
    01503
    01504 static VALUE
    01505 rb_ary_length(VALUE ary)
    01506 {
    01507     long len = RARRAY_LEN(ary);
    01508     return LONG2NUM(len);
    01509 }
    01510
    01511 /*
    01512  *  call-seq:
    01513  *     ary.empty?   -> true or false
    01514  *
    01515  *  Returns <code>true</code> if +self+ contains no elements.
    01516  *
    01517  *     [].empty?   #=> true
    01518  */
    01519
    01520 static VALUE
    01521 rb_ary_empty_p(VALUE ary)
    01522 {
    01523     if (RARRAY_LEN(ary) == 0)
    01524         return Qtrue;
    01525     return Qfalse;
    01526 }
    01527
    01528 static VALUE
    01529 rb_ary_dup_setup(VALUE ary)
    01530 {
    01531     VALUE dup = rb_ary_new2(RARRAY_LEN(ary));
    01532     int is_embed = ARY_EMBED_P(dup);
    01533     DUPSETUP(dup, ary);
    01534     if (is_embed) FL_SET_EMBED(dup);
    01535     ARY_SET_LEN(dup, RARRAY_LEN(ary));
    01536     return dup;
    01537 }
    01538
    01539 VALUE
    01540 rb_ary_dup(VALUE ary)
    01541 {
    01542     VALUE dup = rb_ary_dup_setup(ary);
    01543     MEMCPY(RARRAY_PTR(dup), RARRAY_PTR(ary), VALUE, RARRAY_LEN(ary));
    01544     return dup;
    01545 }
    01546
    01547 VALUE
    01548 rb_ary_resurrect(VALUE ary)
    01549 {
    01550     return rb_ary_new4(RARRAY_LEN(ary), RARRAY_PTR(ary));
    01551 }
    01552
    01553 extern VALUE rb_output_fs;
    01554
    01555 static void ary_join_1(VALUE obj, VALUE ary, VALUE sep, long i, VALUE result);
    01556
    01557 static VALUE
    01558 recursive_join(VALUE obj, VALUE argp, int recur)
    01559 {
    01560     VALUE *arg = (VALUE *)argp;
    01561     VALUE ary = arg[0];
    01562     VALUE sep = arg[1];
    01563     VALUE result = arg[2];
    01564
    01565     if (recur) {
    01566         rb_raise(rb_eArgError, "recursive array join");
    01567     }
    01568     else {
    01569         ary_join_1(obj, ary, sep, 0, result);
    01570     }
    01571     return Qnil;
    01572 }
    01573
    01574 static void
    01575 ary_join_0(VALUE ary, VALUE sep, long max, VALUE result)
    01576 {
    01577     long i;
    01578     VALUE val;
    01579
    01580     for (i=0; i<max; i++) {
    01581         val = RARRAY_PTR(ary)[i];
    01582         if (i > 0 && !NIL_P(sep))
    01583             rb_str_buf_append(result, sep);
    01584         rb_str_buf_append(result, val);
    01585         if (OBJ_TAINTED(val)) OBJ_TAINT(result);
    01586         if (OBJ_UNTRUSTED(val)) OBJ_TAINT(result);
    01587     }
    01588 }
    01589
    01590 static void
    01591 ary_join_1(VALUE obj, VALUE ary, VALUE sep, long i, VALUE result)
    01592 {
    01593     VALUE val, tmp;
    01594
    01595     for (; i<RARRAY_LEN(ary); i++) {
    01596         if (i > 0 && !NIL_P(sep))
    01597             rb_str_buf_append(result, sep);
    01598
    01599         val = RARRAY_PTR(ary)[i];
    01600         switch (TYPE(val)) {
    01601           case T_STRING:
    01602           str_join:
    01603             rb_str_buf_append(result, val);
    01604             break;
    01605           case T_ARRAY:
    01606             obj = val;
    01607           ary_join:
    01608             if (val == ary) {
    01609                 rb_raise(rb_eArgError, "recursive array join");
    01610             }
    01611             else {
    01612                 VALUE args[3];
    01613
    01614                 args[0] = val;
    01615                 args[1] = sep;
    01616                 args[2] = result;
    01617                 rb_exec_recursive(recursive_join, obj, (VALUE)args);
    01618             }
    01619             break;
    01620           default:
    01621             tmp = rb_check_string_type(val);
    01622             if (!NIL_P(tmp)) {
    01623                 val = tmp;
    01624                 goto str_join;
    01625             }
    01626             tmp = rb_check_convert_type(val, T_ARRAY, "Array", "to_ary");
    01627             if (!NIL_P(tmp)) {
    01628                 obj = val;
    01629                 val = tmp;
    01630                 goto ary_join;
    01631             }
    01632             val = rb_obj_as_string(val);
    01633             goto str_join;
    01634         }
    01635     }
    01636 }
    01637
    01638 VALUE
    01639 rb_ary_join(VALUE ary, VALUE sep)
    01640 {
    01641     long len = 1, i;
    01642     int taint = FALSE;
    01643     int untrust = FALSE;
    01644     VALUE val, tmp, result;
    01645
    01646     if (RARRAY_LEN(ary) == 0) return rb_str_new(0, 0);
    01647     if (OBJ_TAINTED(ary) || OBJ_TAINTED(sep)) taint = TRUE;
    01648     if (OBJ_UNTRUSTED(ary) || OBJ_UNTRUSTED(sep)) untrust = TRUE;
    01649
    01650     if (!NIL_P(sep)) {
    01651         StringValue(sep);
    01652         len += RSTRING_LEN(sep) * (RARRAY_LEN(ary) - 1);
    01653     }
    01654     for (i=0; i<RARRAY_LEN(ary); i++) {
    01655         val = RARRAY_PTR(ary)[i];
    01656         tmp = rb_check_string_type(val);
    01657
    01658         if (NIL_P(tmp) || tmp != val) {
    01659             result = rb_str_buf_new(len + (RARRAY_LEN(ary)-i)*10);
    01660             if (taint) OBJ_TAINT(result);
    01661             if (untrust) OBJ_UNTRUST(result);
    01662             ary_join_0(ary, sep, i, result);
    01663             ary_join_1(ary, ary, sep, i, result);
    01664             return result;
    01665         }
    01666
    01667         len += RSTRING_LEN(tmp);
    01668     }
    01669
    01670     result = rb_str_buf_new(len);
    01671     if (taint) OBJ_TAINT(result);
    01672     if (untrust) OBJ_UNTRUST(result);
    01673     ary_join_0(ary, sep, RARRAY_LEN(ary), result);
    01674
    01675     return result;
    01676 }
    01677
    01678 /*
    01679  *  call-seq:
    01680  *     ary.join(sep=$,)    -> str
    01681  *
    01682  *  Returns a string created by converting each element of the array to
    01683  *  a string, separated by <i>sep</i>.
    01684  *
    01685  *     [ "a", "b", "c" ].join        #=> "abc"
    01686  *     [ "a", "b", "c" ].join("-")   #=> "a-b-c"
    01687  */
    01688
    01689 static VALUE
    01690 rb_ary_join_m(int argc, VALUE *argv, VALUE ary)
    01691 {
    01692     VALUE sep;
    01693
    01694     rb_scan_args(argc, argv, "01", &sep);
    01695     if (NIL_P(sep)) sep = rb_output_fs;
    01696
    01697     return rb_ary_join(ary, sep);
    01698 }
    01699
    01700 static VALUE
    01701 inspect_ary(VALUE ary, VALUE dummy, int recur)
    01702 {
    01703     int tainted = OBJ_TAINTED(ary);
    01704     int untrust = OBJ_UNTRUSTED(ary);
    01705     long i;
    01706     VALUE s, str;
    01707
    01708     if (recur) return rb_tainted_str_new2("[...]");
    01709     str = rb_str_buf_new2("[");
    01710     for (i=0; i<RARRAY_LEN(ary); i++) {
    01711         s = rb_inspect(RARRAY_PTR(ary)[i]);
    01712         if (OBJ_TAINTED(s)) tainted = TRUE;
    01713         if (OBJ_UNTRUSTED(s)) untrust = TRUE;
    01714         if (i > 0) rb_str_buf_cat2(str, ", ");
    01715         rb_str_buf_append(str, s);
    01716     }
    01717     rb_str_buf_cat2(str, "]");
    01718     if (tainted) OBJ_TAINT(str);
    01719     if (untrust) OBJ_UNTRUST(str);
    01720     return str;
    01721 }
    01722
    01723 /*
    01724  *  call-seq:
    01725  *     ary.to_s -> string
    01726  *     ary.inspect  -> string
    01727  *
    01728  *  Creates a string representation of +self+.
    01729  */
    01730
    01731 static VALUE
    01732 rb_ary_inspect(VALUE ary)
    01733 {
    01734     if (RARRAY_LEN(ary) == 0) return rb_usascii_str_new2("[]");
    01735     return rb_exec_recursive(inspect_ary, ary, 0);
    01736 }
    01737
    01738 VALUE
    01739 rb_ary_to_s(VALUE ary)
    01740 {
    01741     return rb_ary_inspect(ary);
    01742 }
    01743
    01744 /*
    01745  *  call-seq:
    01746  *     ary.to_a     -> ary
    01747  *
    01748  *  Returns +self+. If called on a subclass of Array, converts
    01749  *  the receiver to an Array object.
    01750  */
    01751
    01752 static VALUE
    01753 rb_ary_to_a(VALUE ary)
    01754 {
    01755     if (rb_obj_class(ary) != rb_cArray) {
    01756         VALUE dup = rb_ary_new2(RARRAY_LEN(ary));
    01757         rb_ary_replace(dup, ary);
    01758         return dup;
    01759     }
    01760     return ary;
    01761 }
    01762
    01763 /*
    01764  *  call-seq:
    01765  *     ary.to_ary -> ary
    01766  *
    01767  *  Returns +self+.
    01768  */
    01769
    01770 static VALUE
    01771 rb_ary_to_ary_m(VALUE ary)
    01772 {
    01773     return ary;
    01774 }
    01775
    01776 static void
    01777 ary_reverse(p1, p2)
    01778     VALUE *p1, *p2;
    01779 {
    01780     while (p1 < p2) {
    01781         VALUE tmp = *p1;
    01782         *p1++ = *p2;
    01783         *p2-- = tmp;
    01784     }
    01785 }
    01786
    01787 VALUE
    01788 rb_ary_reverse(VALUE ary)
    01789 {
    01790     VALUE *p1, *p2;
    01791
    01792     rb_ary_modify(ary);
    01793     if (RARRAY_LEN(ary) > 1) {
    01794         p1 = RARRAY_PTR(ary);
    01795         p2 = p1 + RARRAY_LEN(ary) - 1;  /* points last item */
    01796         ary_reverse(p1, p2);
    01797     }
    01798     return ary;
    01799 }
    01800
    01801 /*
    01802  *  call-seq:
    01803  *     ary.reverse!   -> ary
    01804  *
    01805  *  Reverses +self+ in place.
    01806  *
    01807  *     a = [ "a", "b", "c" ]
    01808  *     a.reverse!       #=> ["c", "b", "a"]
    01809  *     a                #=> ["c", "b", "a"]
    01810  */
    01811
    01812 static VALUE
    01813 rb_ary_reverse_bang(VALUE ary)
    01814 {
    01815     return rb_ary_reverse(ary);
    01816 }
    01817
    01818 /*
    01819  *  call-seq:
    01820  *     ary.reverse -> new_ary
    01821  *
    01822  *  Returns a new array containing +self+'s elements in reverse order.
    01823  *
    01824  *     [ "a", "b", "c" ].reverse   #=> ["c", "b", "a"]
    01825  *     [ 1 ].reverse               #=> [1]
    01826  */
    01827
    01828 static VALUE
    01829 rb_ary_reverse_m(VALUE ary)
    01830 {
    01831     VALUE dup = rb_ary_dup_setup(ary);
    01832     long len = RARRAY_LEN(ary);
    01833
    01834     if (len > 0) {
    01835         VALUE *p1 = RARRAY_PTR(ary);
    01836         VALUE *p2 = RARRAY_PTR(dup) + len - 1;
    01837         do *p2-- = *p1++; while (--len > 0);
    01838     }
    01839     return dup;
    01840 }
    01841
    01842 static inline long
    01843 rotate_count(long cnt, long len)
    01844 {
    01845     return (cnt < 0) ? (len - (~cnt % len) - 1) : (cnt % len);
    01846 }
    01847
    01848 VALUE
    01849 rb_ary_rotate(VALUE ary, long cnt)
    01850 {
    01851     rb_ary_modify(ary);
    01852
    01853     if (cnt != 0) {
    01854         VALUE *ptr = RARRAY_PTR(ary);
    01855         long len = RARRAY_LEN(ary);
    01856
    01857         if (len > 0 && (cnt = rotate_count(cnt, len)) > 0) {
    01858             --len;
    01859             if (cnt < len) ary_reverse(ptr + cnt, ptr + len);
    01860             if (--cnt > 0) ary_reverse(ptr, ptr + cnt);
    01861             if (len > 0) ary_reverse(ptr, ptr + len);
    01862             return ary;
    01863         }
    01864     }
    01865
    01866     return Qnil;
    01867 }
    01868
    01869 /*
    01870  *  call-seq:
    01871  *     ary.rotate!(cnt=1) -> ary
    01872  *
    01873  *  Rotates +self+ in place so that the element at +cnt+ comes first,
    01874  *  and returns +self+.  If +cnt+ is negative then it rotates in
    01875  *  counter direction.
    01876  *
    01877  *     a = [ "a", "b", "c", "d" ]
    01878  *     a.rotate!        #=> ["b", "c", "d", "a"]
    01879  *     a                #=> ["b", "c", "d", "a"]
    01880  *     a.rotate!(2)     #=> ["d", "a", "b", "c"]
    01881  *     a.rotate!(-3)    #=> ["a", "b", "c", "d"]
    01882  */
    01883
    01884 static VALUE
    01885 rb_ary_rotate_bang(int argc, VALUE *argv, VALUE ary)
    01886 {
    01887     long n = 1;
    01888
    01889     switch (argc) {
    01890       case 1: n = NUM2LONG(argv[0]);
    01891       case 0: break;
    01892       default: rb_scan_args(argc, argv, "01", NULL);
    01893     }
    01894     rb_ary_rotate(ary, n);
    01895     return ary;
    01896 }
    01897
    01898 /*
    01899  *  call-seq:
    01900  *     ary.rotate([n = 1]) -> new_ary
    01901  *
    01902  *  Returns new array by rotating +self+, whose first element is the
    01903  *  element at +cnt+ in +self+.  If +cnt+ is negative then it rotates
    01904  *  in counter direction.
    01905  *
    01906  *     a = [ "a", "b", "c", "d" ]
    01907  *     a.rotate         #=> ["b", "c", "d", "a"]
    01908  *     a                #=> ["a", "b", "c", "d"]
    01909  *     a.rotate(2)      #=> ["c", "d", "a", "b"]
    01910  *     a.rotate(-3)     #=> ["b", "c", "d", "a"]
    01911  */
    01912
    01913 static VALUE
    01914 rb_ary_rotate_m(int argc, VALUE *argv, VALUE ary)
    01915 {
    01916     VALUE rotated, *ptr, *ptr2;
    01917     long len, cnt = 1;
    01918
    01919     switch (argc) {
    01920       case 1: cnt = NUM2LONG(argv[0]);
    01921       case 0: break;
    01922       default: rb_scan_args(argc, argv, "01", NULL);
    01923     }
    01924
    01925     len = RARRAY_LEN(ary);
    01926     rotated = rb_ary_dup_setup(ary);
    01927     if (len > 0) {
    01928         cnt = rotate_count(cnt, len);
    01929         ptr = RARRAY_PTR(ary);
    01930         ptr2 = RARRAY_PTR(rotated);
    01931         len -= cnt;
    01932         MEMCPY(ptr2, ptr + cnt, VALUE, len);
    01933         MEMCPY(ptr2 + len, ptr, VALUE, cnt);
    01934     }
    01935     return rotated;
    01936 }
    01937
    01938 struct ary_sort_data {
    01939     VALUE ary;
    01940     int opt_methods;
    01941     int opt_inited;
    01942 };
    01943
    01944 enum {
    01945     sort_opt_Fixnum,
    01946     sort_opt_String,
    01947     sort_optimizable_count
    01948 };
    01949
    01950 #define STRING_P(s) (TYPE(s) == T_STRING && CLASS_OF(s) == rb_cString)
    01951
    01952 #define SORT_OPTIMIZABLE_BIT(type) (1U << TOKEN_PASTE(sort_opt_,type))
    01953 #define SORT_OPTIMIZABLE(data, type) \
    01954     ((data->opt_inited & SORT_OPTIMIZABLE_BIT(type)) ? \
    01955      (data->opt_methods & SORT_OPTIMIZABLE_BIT(type)) : \
    01956      ((data->opt_inited |= SORT_OPTIMIZABLE_BIT(type)), \
    01957       rb_method_basic_definition_p(TOKEN_PASTE(rb_c,type), id_cmp) && \
    01958       (data->opt_methods |= SORT_OPTIMIZABLE_BIT(type))))
    01959
    01960 static VALUE
    01961 sort_reentered(VALUE ary)
    01962 {
    01963     if (RBASIC(ary)->klass) {
    01964         rb_raise(rb_eRuntimeError, "sort reentered");
    01965     }
    01966     return Qnil;
    01967 }
    01968
    01969 static int
    01970 sort_1(const void *ap, const void *bp, void *dummy)
    01971 {
    01972     struct ary_sort_data *data = dummy;
    01973     VALUE retval = sort_reentered(data->ary);
    01974     VALUE a = *(const VALUE *)ap, b = *(const VALUE *)bp;
    01975     int n;
    01976
    01977     retval = rb_yield_values(2, a, b);
    01978     n = rb_cmpint(retval, a, b);
    01979     sort_reentered(data->ary);
    01980     return n;
    01981 }
    01982
    01983 static int
    01984 sort_2(const void *ap, const void *bp, void *dummy)
    01985 {
    01986     struct ary_sort_data *data = dummy;
    01987     VALUE retval = sort_reentered(data->ary);
    01988     VALUE a = *(const VALUE *)ap, b = *(const VALUE *)bp;
    01989     int n;
    01990
    01991     if (FIXNUM_P(a) && FIXNUM_P(b) && SORT_OPTIMIZABLE(data, Fixnum)) {
    01992         if ((long)a > (long)b) return 1;
    01993         if ((long)a < (long)b) return -1;
    01994         return 0;
    01995     }
    01996     if (STRING_P(a) && STRING_P(b) && SORT_OPTIMIZABLE(data, String)) {
    01997         return rb_str_cmp(a, b);
    01998     }
    01999
    02000     retval = rb_funcall(a, id_cmp, 1, b);
    02001     n = rb_cmpint(retval, a, b);
    02002     sort_reentered(data->ary);
    02003
    02004     return n;
    02005 }
    02006
    02007 /*
    02008  *  call-seq:
    02009  *     ary.sort!                   -> ary
    02010  *     ary.sort! {| a,b | block }  -> ary
    02011  *
    02012  *  Sorts +self+. Comparisons for
    02013  *  the sort will be done using the <code><=></code> operator or using
    02014  *  an optional code block. The block implements a comparison between
    02015  *  <i>a</i> and <i>b</i>, returning -1, 0, or +1. See also
    02016  *  <code>Enumerable#sort_by</code>.
    02017  *
    02018  *     a = [ "d", "a", "e", "c", "b" ]
    02019  *     a.sort                    #=> ["a", "b", "c", "d", "e"]
    02020  *     a.sort {|x,y| y <=> x }   #=> ["e", "d", "c", "b", "a"]
    02021  */
    02022
    02023 VALUE
    02024 rb_ary_sort_bang(VALUE ary)
    02025 {
    02026     rb_ary_modify(ary);
    02027     assert(!ARY_SHARED_P(ary));
    02028     if (RARRAY_LEN(ary) > 1) {
    02029         VALUE tmp = ary_make_substitution(ary); /* only ary refers tmp */
    02030         struct ary_sort_data data;
    02031
    02032         RBASIC(tmp)->klass = 0;
    02033         data.ary = tmp;
    02034         data.opt_methods = 0;
    02035         data.opt_inited = 0;
    02036         ruby_qsort(RARRAY_PTR(tmp), RARRAY_LEN(tmp), sizeof(VALUE),
    02037                    rb_block_given_p()?sort_1:sort_2, &data);
    02038
    02039         if (ARY_EMBED_P(tmp)) {
    02040             assert(ARY_EMBED_P(tmp));
    02041             if (ARY_SHARED_P(ary)) { /* ary might be destructively operated in the given block */
    02042                 rb_ary_unshare(ary);
    02043             }
    02044             FL_SET_EMBED(ary);
    02045             MEMCPY(RARRAY_PTR(ary), ARY_EMBED_PTR(tmp), VALUE, ARY_EMBED_LEN(tmp));
    02046             ARY_SET_LEN(ary, ARY_EMBED_LEN(tmp));
    02047         }
    02048         else {
    02049             assert(!ARY_EMBED_P(tmp));
    02050             if (ARY_HEAP_PTR(ary) == ARY_HEAP_PTR(tmp)) {
    02051                 assert(!ARY_EMBED_P(ary));
    02052                 FL_UNSET_SHARED(ary);
    02053                 ARY_SET_CAPA(ary, ARY_CAPA(tmp));
    02054             }
    02055             else {
    02056                 assert(!ARY_SHARED_P(tmp));
    02057                 if (ARY_EMBED_P(ary)) {
    02058                     FL_UNSET_EMBED(ary);
    02059                 }
    02060                 else if (ARY_SHARED_P(ary)) {
    02061                     /* ary might be destructively operated in the given block */
    02062                     rb_ary_unshare(ary);
    02063                 }
    02064                 else {
    02065                     xfree(ARY_HEAP_PTR(ary));
    02066                 }
    02067                 ARY_SET_PTR(ary, RARRAY_PTR(tmp));
    02068                 ARY_SET_HEAP_LEN(ary, RARRAY_LEN(tmp));
    02069                 ARY_SET_CAPA(ary, ARY_CAPA(tmp));
    02070             }
    02071             /* tmp was lost ownership for the ptr */
    02072             FL_UNSET(tmp, FL_FREEZE);
    02073             FL_SET_EMBED(tmp);
    02074             ARY_SET_EMBED_LEN(tmp, 0);
    02075             FL_SET(tmp, FL_FREEZE);
    02076         }
    02077         /* tmp will be GC'ed. */
    02078         RBASIC(tmp)->klass = rb_cArray;
    02079     }
    02080     return ary;
    02081 }
    02082
    02083 /*
    02084  *  call-seq:
    02085  *     ary.sort                   -> new_ary
    02086  *     ary.sort {| a,b | block }  -> new_ary
    02087  *
    02088  *  Returns a new array created by sorting +self+. Comparisons for
    02089  *  the sort will be done using the <code><=></code> operator or using
    02090  *  an optional code block. The block implements a comparison between
    02091  *  <i>a</i> and <i>b</i>, returning -1, 0, or +1. See also
    02092  *  <code>Enumerable#sort_by</code>.
    02093  *
    02094  *     a = [ "d", "a", "e", "c", "b" ]
    02095  *     a.sort                    #=> ["a", "b", "c", "d", "e"]
    02096  *     a.sort {|x,y| y <=> x }   #=> ["e", "d", "c", "b", "a"]
    02097  */
    02098
    02099 VALUE
    02100 rb_ary_sort(VALUE ary)
    02101 {
    02102     ary = rb_ary_dup(ary);
    02103     rb_ary_sort_bang(ary);
    02104     return ary;
    02105 }
    02106
    02107
    02108 static VALUE
    02109 sort_by_i(VALUE i)
    02110 {
    02111     return rb_yield(i);
    02112 }
    02113
    02114 /*
    02115  *  call-seq:
    02116  *     ary.sort_by! {| obj | block }    -> ary
    02117  *     ary.sort_by!                     -> an_enumerator
    02118  *
    02119  *  Sorts +self+ in place using a set of keys generated by mapping the
    02120  *  values in +self+ through the given block.
    02121  *
    02122  *  If no block is given, an enumerator is returned instead.
    02123  *
    02124  */
    02125
    02126 static VALUE
    02127 rb_ary_sort_by_bang(VALUE ary)
    02128 {
    02129     VALUE sorted;
    02130
    02131     RETURN_ENUMERATOR(ary, 0, 0);
    02132     rb_ary_modify(ary);
    02133     sorted = rb_block_call(ary, rb_intern("sort_by"), 0, 0, sort_by_i, 0);
    02134     rb_ary_replace(ary, sorted);
    02135     return ary;
    02136 }
    02137
    02138
    02139 /*
    02140  *  call-seq:
    02141  *     ary.collect {|item| block }  -> new_ary
    02142  *     ary.map     {|item| block }  -> new_ary
    02143  *     ary.collect                  -> an_enumerator
    02144  *     ary.map                      -> an_enumerator
    02145  *
    02146  *  Invokes <i>block</i> once for each element of +self+. Creates a
    02147  *  new array containing the values returned by the block.
    02148  *  See also <code>Enumerable#collect</code>.
    02149  *
    02150  *  If no block is given, an enumerator is returned instead.
    02151  *
    02152  *     a = [ "a", "b", "c", "d" ]
    02153  *     a.collect {|x| x + "!" }   #=> ["a!", "b!", "c!", "d!"]
    02154  *     a                          #=> ["a", "b", "c", "d"]
    02155  */
    02156
    02157 static VALUE
    02158 rb_ary_collect(VALUE ary)
    02159 {
    02160     long i;
    02161     VALUE collect;
    02162
    02163     RETURN_ENUMERATOR(ary, 0, 0);
    02164     collect = rb_ary_new2(RARRAY_LEN(ary));
    02165     for (i = 0; i < RARRAY_LEN(ary); i++) {
    02166         rb_ary_push(collect, rb_yield(RARRAY_PTR(ary)[i]));
    02167     }
    02168     return collect;
    02169 }
    02170
    02171
    02172 /*
    02173  *  call-seq:
    02174  *     ary.collect! {|item| block }   -> ary
    02175  *     ary.map!     {|item| block }   -> ary
    02176  *     ary.collect                    -> an_enumerator
    02177  *     ary.map                        -> an_enumerator
    02178  *
    02179  *  Invokes the block once for each element of +self+, replacing the
    02180  *  element with the value returned by _block_.
    02181  *  See also <code>Enumerable#collect</code>.
    02182  *
    02183  *  If no block is given, an enumerator is returned instead.
    02184  *
    02185  *     a = [ "a", "b", "c", "d" ]
    02186  *     a.collect! {|x| x + "!" }
    02187  *     a             #=>  [ "a!", "b!", "c!", "d!" ]
    02188  */
    02189
    02190 static VALUE
    02191 rb_ary_collect_bang(VALUE ary)
    02192 {
    02193     long i;
    02194
    02195     RETURN_ENUMERATOR(ary, 0, 0);
    02196     rb_ary_modify(ary);
    02197     for (i = 0; i < RARRAY_LEN(ary); i++) {
    02198         rb_ary_store(ary, i, rb_yield(RARRAY_PTR(ary)[i]));
    02199     }
    02200     return ary;
    02201 }
    02202
    02203 VALUE
    02204 rb_get_values_at(VALUE obj, long olen, int argc, VALUE *argv, VALUE (*func) (VALUE, long))
    02205 {
    02206     VALUE result = rb_ary_new2(argc);
    02207     long beg, len, i, j;
    02208
    02209     for (i=0; i<argc; i++) {
    02210         if (FIXNUM_P(argv[i])) {
    02211             rb_ary_push(result, (*func)(obj, FIX2LONG(argv[i])));
    02212             continue;
    02213         }
    02214         /* check if idx is Range */
    02215         switch (rb_range_beg_len(argv[i], &beg, &len, olen, 0)) {
    02216           case Qfalse:
    02217             break;
    02218           case Qnil:
    02219             continue;
    02220           default:
    02221             for (j=0; j<len; j++) {
    02222                 rb_ary_push(result, (*func)(obj, j+beg));
    02223             }
    02224             continue;
    02225         }
    02226         rb_ary_push(result, (*func)(obj, NUM2LONG(argv[i])));
    02227     }
    02228     return result;
    02229 }
    02230
    02231 /*
    02232  *  call-seq:
    02233  *     ary.values_at(selector,... )  -> new_ary
    02234  *
    02235  *  Returns an array containing the elements in
    02236  *  +self+ corresponding to the given selector(s). The selectors
    02237  *  may be either integer indices or ranges.
    02238  *  See also <code>Array#select</code>.
    02239  *
    02240  *     a = %w{ a b c d e f }
    02241  *     a.values_at(1, 3, 5)
    02242  *     a.values_at(1, 3, 5, 7)
    02243  *     a.values_at(-1, -3, -5, -7)
    02244  *     a.values_at(1..3, 2...5)
    02245  */
    02246
    02247 static VALUE
    02248 rb_ary_values_at(int argc, VALUE *argv, VALUE ary)
    02249 {
    02250     return rb_get_values_at(ary, RARRAY_LEN(ary), argc, argv, rb_ary_entry);
    02251 }
    02252
    02253
    02254 /*
    02255  *  call-seq:
    02256  *     ary.select {|item| block } -> new_ary
    02257  *     ary.select                 -> an_enumerator
    02258  *
    02259  *  Invokes the block passing in successive elements from +self+,
    02260  *  returning an array containing those elements for which the block
    02261  *  returns a true value (equivalent to <code>Enumerable#select</code>).
    02262  *
    02263  *  If no block is given, an enumerator is returned instead.
    02264  *
    02265  *     a = %w{ a b c d e f }
    02266  *     a.select {|v| v =~ /[aeiou]/}   #=> ["a", "e"]
    02267  */
    02268
    02269 static VALUE
    02270 rb_ary_select(VALUE ary)
    02271 {
    02272     VALUE result;
    02273     long i;
    02274
    02275     RETURN_ENUMERATOR(ary, 0, 0);
    02276     result = rb_ary_new2(RARRAY_LEN(ary));
    02277     for (i = 0; i < RARRAY_LEN(ary); i++) {
    02278         if (RTEST(rb_yield(RARRAY_PTR(ary)[i]))) {
    02279             rb_ary_push(result, rb_ary_elt(ary, i));
    02280         }
    02281     }
    02282     return result;
    02283 }
    02284
    02285 /*
    02286  *  call-seq:
    02287  *     ary.select! {|item| block } -> new_ary or nil
    02288  *     ary.select!                 -> an_enumerator
    02289  *
    02290  *  Invokes the block passing in successive elements from
    02291  *  +self+, deleting elements for which the block returns a
    02292  *  false value. It returns +self+ if changes were made,
    02293  *  otherwise it returns <code>nil</code>.
    02294  *  See also <code>Array#keep_if</code>
    02295  *
    02296  *  If no block is given, an enumerator is returned instead.
    02297  *
    02298  */
    02299
    02300 static VALUE
    02301 rb_ary_select_bang(VALUE ary)
    02302 {
    02303     long i1, i2;
    02304
    02305     RETURN_ENUMERATOR(ary, 0, 0);
    02306     rb_ary_modify(ary);
    02307     for (i1 = i2 = 0; i1 < RARRAY_LEN(ary); i1++) {
    02308         VALUE v = RARRAY_PTR(ary)[i1];
    02309         if (!RTEST(rb_yield(v))) continue;
    02310         if (i1 != i2) {
    02311             rb_ary_store(ary, i2, v);
    02312         }
    02313         i2++;
    02314     }
    02315
    02316     if (RARRAY_LEN(ary) == i2) return Qnil;
    02317     if (i2 < RARRAY_LEN(ary))
    02318         ARY_SET_LEN(ary, i2);
    02319     return ary;
    02320 }
    02321
    02322 /*
    02323  *  call-seq:
    02324  *     ary.keep_if {|item| block } -> ary
    02325  *     ary.keep_if                 -> an_enumerator
    02326  *
    02327  *  Deletes every element of +self+ for which <i>block</i> evaluates
    02328  *  to false.
    02329  *  See also <code>Array#select!</code>
    02330  *
    02331  *  If no block is given, an enumerator is returned instead.
    02332  *
    02333  *     a = %w{ a b c d e f }
    02334  *     a.keep_if {|v| v =~ /[aeiou]/}   #=> ["a", "e"]
    02335  */
    02336
    02337 static VALUE
    02338 rb_ary_keep_if(VALUE ary)
    02339 {
    02340     RETURN_ENUMERATOR(ary, 0, 0);
    02341     rb_ary_select_bang(ary);
    02342     return ary;
    02343 }
    02344
    02345 /*
    02346  *  call-seq:
    02347  *     ary.delete(obj)            -> obj or nil
    02348  *     ary.delete(obj) { block }  -> obj or nil
    02349  *
    02350  *  Deletes items from +self+ that are equal to <i>obj</i>.
    02351  *  If any items are found, returns <i>obj</i>.   If
    02352  *  the item is not found, returns <code>nil</code>. If the optional
    02353  *  code block is given, returns the result of <i>block</i> if the item
    02354  *  is not found.  (To remove <code>nil</code> elements and
    02355  *  get an informative return value, use #compact!)
    02356  *
    02357  *     a = [ "a", "b", "b", "b", "c" ]
    02358  *     a.delete("b")                   #=> "b"
    02359  *     a                               #=> ["a", "c"]
    02360  *     a.delete("z")                   #=> nil
    02361  *     a.delete("z") { "not found" }   #=> "not found"
    02362  */
    02363
    02364 VALUE
    02365 rb_ary_delete(VALUE ary, VALUE item)
    02366 {
    02367     VALUE v = item;
    02368     long i1, i2;
    02369
    02370     for (i1 = i2 = 0; i1 < RARRAY_LEN(ary); i1++) {
    02371         VALUE e = RARRAY_PTR(ary)[i1];
    02372
    02373         if (rb_equal(e, item)) {
    02374             v = e;
    02375             continue;
    02376         }
    02377         if (i1 != i2) {
    02378             rb_ary_store(ary, i2, e);
    02379         }
    02380         i2++;
    02381     }
    02382     if (RARRAY_LEN(ary) == i2) {
    02383         if (rb_block_given_p()) {
    02384             return rb_yield(item);
    02385         }
    02386         return Qnil;
    02387     }
    02388
    02389     rb_ary_modify(ary);
    02390     if (RARRAY_LEN(ary) > i2) {
    02391         ARY_SET_LEN(ary, i2);
    02392         if (i2 * 2 < ARY_CAPA(ary) &&
    02393             ARY_CAPA(ary) > ARY_DEFAULT_SIZE) {
    02394             ary_resize_capa(ary, i2*2);
    02395         }
    02396     }
    02397
    02398     return v;
    02399 }
    02400
    02401 VALUE
    02402 rb_ary_delete_at(VALUE ary, long pos)
    02403 {
    02404     long len = RARRAY_LEN(ary);
    02405     VALUE del;
    02406
    02407     if (pos >= len) return Qnil;
    02408     if (pos < 0) {
    02409         pos += len;
    02410         if (pos < 0) return Qnil;
    02411     }
    02412
    02413     rb_ary_modify(ary);
    02414     del = RARRAY_PTR(ary)[pos];
    02415     MEMMOVE(RARRAY_PTR(ary)+pos, RARRAY_PTR(ary)+pos+1, VALUE,
    02416             RARRAY_LEN(ary)-pos-1);
    02417     ARY_INCREASE_LEN(ary, -1);
    02418
    02419     return del;
    02420 }
    02421
    02422 /*
    02423  *  call-seq:
    02424  *     ary.delete_at(index)  -> obj or nil
    02425  *
    02426  *  Deletes the element at the specified index, returning that element,
    02427  *  or <code>nil</code> if the index is out of range. See also
    02428  *  <code>Array#slice!</code>.
    02429  *
    02430  *     a = %w( ant bat cat dog )
    02431  *     a.delete_at(2)    #=> "cat"
    02432  *     a                 #=> ["ant", "bat", "dog"]
    02433  *     a.delete_at(99)   #=> nil
    02434  */
    02435
    02436 static VALUE
    02437 rb_ary_delete_at_m(VALUE ary, VALUE pos)
    02438 {
    02439     return rb_ary_delete_at(ary, NUM2LONG(pos));
    02440 }
    02441
    02442 /*
    02443  *  call-seq:
    02444  *     ary.slice!(index)         -> obj or nil
    02445  *     ary.slice!(start, length) -> new_ary or nil
    02446  *     ary.slice!(range)         -> new_ary or nil
    02447  *
    02448  *  Deletes the element(s) given by an index (optionally with a length)
    02449  *  or by a range. Returns the deleted object (or objects), or
    02450  *  <code>nil</code> if the index is out of range.
    02451  *
    02452  *     a = [ "a", "b", "c" ]
    02453  *     a.slice!(1)     #=> "b"
    02454  *     a               #=> ["a", "c"]
    02455  *     a.slice!(-1)    #=> "c"
    02456  *     a               #=> ["a"]
    02457  *     a.slice!(100)   #=> nil
    02458  *     a               #=> ["a"]
    02459  */
    02460
    02461 static VALUE
    02462 rb_ary_slice_bang(int argc, VALUE *argv, VALUE ary)
    02463 {
    02464     VALUE arg1, arg2;
    02465     long pos, len, orig_len;
    02466
    02467     rb_ary_modify_check(ary);
    02468     if (argc == 2) {
    02469         pos = NUM2LONG(argv[0]);
    02470         len = NUM2LONG(argv[1]);
    02471       delete_pos_len:
    02472         if (len < 0) return Qnil;
    02473         orig_len = RARRAY_LEN(ary);
    02474         if (pos < 0) {
    02475             pos += orig_len;
    02476             if (pos < 0) return Qnil;
    02477         }
    02478         else if (orig_len < pos) return Qnil;
    02479         if (orig_len < pos + len) {
    02480             len = orig_len - pos;
    02481         }
    02482         if (len == 0) return rb_ary_new2(0);
    02483         arg2 = rb_ary_new4(len, RARRAY_PTR(ary)+pos);
    02484         RBASIC(arg2)->klass = rb_obj_class(ary);
    02485         rb_ary_splice(ary, pos, len, Qundef);
    02486         return arg2;
    02487     }
    02488
    02489     if (argc != 1) {
    02490         /* error report */
    02491         rb_scan_args(argc, argv, "11", NULL, NULL);
    02492     }
    02493     arg1 = argv[0];
    02494
    02495     if (!FIXNUM_P(arg1)) {
    02496         switch (rb_range_beg_len(arg1, &pos, &len, RARRAY_LEN(ary), 0)) {
    02497           case Qtrue:
    02498             /* valid range */
    02499             goto delete_pos_len;
    02500           case Qnil:
    02501             /* invalid range */
    02502             return Qnil;
    02503           default:
    02504             /* not a range */
    02505             break;
    02506         }
    02507     }
    02508
    02509     return rb_ary_delete_at(ary, NUM2LONG(arg1));
    02510 }
    02511
    02512 /*
    02513  *  call-seq:
    02514  *     ary.reject! {|item| block }  -> ary or nil
    02515  *     ary.reject!                  -> an_enumerator
    02516  *
    02517  *  Equivalent to <code>Array#delete_if</code>, deleting elements from
    02518  *  +self+ for which the block evaluates to true, but returns
    02519  *  <code>nil</code> if no changes were made.
    02520  *  See also <code>Enumerable#reject</code> and <code>Array#delete_if</code>.
    02521  *
    02522  *  If no block is given, an enumerator is returned instead.
    02523  *
    02524  */
    02525
    02526 static VALUE
    02527 rb_ary_reject_bang(VALUE ary)
    02528 {
    02529     long i1, i2;
    02530
    02531     RETURN_ENUMERATOR(ary, 0, 0);
    02532     rb_ary_modify(ary);
    02533     for (i1 = i2 = 0; i1 < RARRAY_LEN(ary); i1++) {
    02534         VALUE v = RARRAY_PTR(ary)[i1];
    02535         if (RTEST(rb_yield(v))) continue;
    02536         if (i1 != i2) {
    02537             rb_ary_store(ary, i2, v);
    02538         }
    02539         i2++;
    02540     }
    02541
    02542     if (RARRAY_LEN(ary) == i2) return Qnil;
    02543     if (i2 < RARRAY_LEN(ary))
    02544         ARY_SET_LEN(ary, i2);
    02545     return ary;
    02546 }
    02547
    02548 /*
    02549  *  call-seq:
    02550  *     ary.reject {|item| block }  -> new_ary
    02551  *     ary.reject                  -> an_enumerator
    02552  *
    02553  *  Returns a new array containing the items in +self+
    02554  *  for which the block is not true.
    02555  *  See also <code>Array#delete_if</code>
    02556  *
    02557  *  If no block is given, an enumerator is returned instead.
    02558  *
    02559  */
    02560
    02561 static VALUE
    02562 rb_ary_reject(VALUE ary)
    02563 {
    02564     RETURN_ENUMERATOR(ary, 0, 0);
    02565     ary = rb_ary_dup(ary);
    02566     rb_ary_reject_bang(ary);
    02567     return ary;
    02568 }
    02569
    02570 /*
    02571  *  call-seq:
    02572  *     ary.delete_if {|item| block }  -> ary
    02573  *     ary.delete_if                  -> an_enumerator
    02574  *
    02575  *  Deletes every element of +self+ for which <i>block</i> evaluates
    02576  *  to true.
    02577  *  See also <code>Array#reject!</code>
    02578  *
    02579  *  If no block is given, an enumerator is returned instead.
    02580  *
    02581  *     a = [ "a", "b", "c" ]
    02582  *     a.delete_if {|x| x >= "b" }   #=> ["a"]
    02583  */
    02584
    02585 static VALUE
    02586 rb_ary_delete_if(VALUE ary)
    02587 {
    02588     RETURN_ENUMERATOR(ary, 0, 0);
    02589     rb_ary_reject_bang(ary);
    02590     return ary;
    02591 }
    02592
    02593 static VALUE
    02594 take_i(VALUE val, VALUE *args, int argc, VALUE *argv)
    02595 {
    02596     if (args[1]-- == 0) rb_iter_break();
    02597     if (argc > 1) val = rb_ary_new4(argc, argv);
    02598     rb_ary_push(args[0], val);
    02599     return Qnil;
    02600 }
    02601
    02602 static VALUE
    02603 take_items(VALUE obj, long n)
    02604 {
    02605     VALUE result = rb_check_array_type(obj);
    02606     VALUE args[2];
    02607
    02608     if (!NIL_P(result)) return rb_ary_subseq(result, 0, n);
    02609     result = rb_ary_new2(n);
    02610     args[0] = result; args[1] = (VALUE)n;
    02611     rb_block_call(obj, rb_intern("each"), 0, 0, take_i, (VALUE)args);
    02612     return result;
    02613 }
    02614
    02615
    02616 /*
    02617  *  call-seq:
    02618  *     ary.zip(arg, ...)                   -> new_ary
    02619  *     ary.zip(arg, ...) {| arr | block }  -> nil
    02620  *
    02621  *  Converts any arguments to arrays, then merges elements of
    02622  *  +self+ with corresponding elements from each argument. This
    02623  *  generates a sequence of <code>self.size</code> <em>n</em>-element
    02624  *  arrays, where <em>n</em> is one more that the count of arguments. If
    02625  *  the size of any argument is less than <code>enumObj.size</code>,
    02626  *  <code>nil</code> values are supplied. If a block is given, it is
    02627  *  invoked for each output array, otherwise an array of arrays is
    02628  *  returned.
    02629  *
    02630  *     a = [ 4, 5, 6 ]
    02631  *     b = [ 7, 8, 9 ]
    02632  *     [1,2,3].zip(a, b)      #=> [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
    02633  *     [1,2].zip(a,b)         #=> [[1, 4, 7], [2, 5, 8]]
    02634  *     a.zip([1,2],[8])       #=> [[4,1,8], [5,2,nil], [6,nil,nil]]
    02635  */
    02636
    02637 static VALUE
    02638 rb_ary_zip(int argc, VALUE *argv, VALUE ary)
    02639 {
    02640     int i, j;
    02641     long len;
    02642     VALUE result = Qnil;
    02643
    02644     len = RARRAY_LEN(ary);
    02645     for (i=0; i<argc; i++) {
    02646         argv[i] = take_items(argv[i], len);
    02647     }
    02648     if (!rb_block_given_p()) {
    02649         result = rb_ary_new2(len);
    02650     }
    02651
    02652     for (i=0; i<RARRAY_LEN(ary); i++) {
    02653         VALUE tmp = rb_ary_new2(argc+1);
    02654
    02655         rb_ary_push(tmp, rb_ary_elt(ary, i));
    02656         for (j=0; j<argc; j++) {
    02657             rb_ary_push(tmp, rb_ary_elt(argv[j], i));
    02658         }
    02659         if (NIL_P(result)) {
    02660             rb_yield(tmp);
    02661         }
    02662         else {
    02663             rb_ary_push(result, tmp);
    02664         }
    02665     }
    02666     return result;
    02667 }
    02668
    02669 /*
    02670  *  call-seq:
    02671  *     ary.transpose -> new_ary
    02672  *
    02673  *  Assumes that +self+ is an array of arrays and transposes the
    02674  *  rows and columns.
    02675  *
    02676  *     a = [[1,2], [3,4], [5,6]]
    02677  *     a.transpose   #=> [[1, 3, 5], [2, 4, 6]]
    02678  */
    02679
    02680 static VALUE
    02681 rb_ary_transpose(VALUE ary)
    02682 {
    02683     long elen = -1, alen, i, j;
    02684     VALUE tmp, result = 0;
    02685
    02686     alen = RARRAY_LEN(ary);
    02687     if (alen == 0) return rb_ary_dup(ary);
    02688     for (i=0; i<alen; i++) {
    02689         tmp = to_ary(rb_ary_elt(ary, i));
    02690         if (elen < 0) {         /* first element */
    02691             elen = RARRAY_LEN(tmp);
    02692             result = rb_ary_new2(elen);
    02693             for (j=0; j<elen; j++) {
    02694                 rb_ary_store(result, j, rb_ary_new2(alen));
    02695             }
    02696         }
    02697         else if (elen != RARRAY_LEN(tmp)) {
    02698             rb_raise(rb_eIndexError, "element size differs (%ld should be %ld)",
    02699                      RARRAY_LEN(tmp), elen);
    02700         }
    02701         for (j=0; j<elen; j++) {
    02702             rb_ary_store(rb_ary_elt(result, j), i, rb_ary_elt(tmp, j));
    02703         }
    02704     }
    02705     return result;
    02706 }
    02707
    02708 /*
    02709  *  call-seq:
    02710  *     ary.replace(other_ary)  -> ary
    02711  *
    02712  *  Replaces the contents of +self+ with the contents of
    02713  *  <i>other_ary</i>, truncating or expanding if necessary.
    02714  *
    02715  *     a = [ "a", "b", "c", "d", "e" ]
    02716  *     a.replace([ "x", "y", "z" ])   #=> ["x", "y", "z"]
    02717  *     a                              #=> ["x", "y", "z"]
    02718  */
    02719
    02720 VALUE
    02721 rb_ary_replace(VALUE copy, VALUE orig)
    02722 {
    02723     rb_ary_modify_check(copy);
    02724     orig = to_ary(orig);
    02725     if (copy == orig) return copy;
    02726
    02727     if (RARRAY_LEN(orig) <= RARRAY_EMBED_LEN_MAX) {
    02728         VALUE *ptr;
    02729         VALUE shared = 0;
    02730
    02731         if (ARY_OWNS_HEAP_P(copy)) {
    02732             xfree(RARRAY_PTR(copy));
    02733         }
    02734         else if (ARY_SHARED_P(copy)) {
    02735             shared = ARY_SHARED(copy);
    02736             FL_UNSET_SHARED(copy);
    02737         }
    02738         FL_SET_EMBED(copy);
    02739         ptr = RARRAY_PTR(orig);
    02740         MEMCPY(RARRAY_PTR(copy), ptr, VALUE, RARRAY_LEN(orig));
    02741         if (shared) {
    02742             rb_ary_decrement_share(shared);
    02743         }
    02744         ARY_SET_LEN(copy, RARRAY_LEN(orig));
    02745     }
    02746     else {
    02747         VALUE shared = ary_make_shared(orig);
    02748         if (ARY_OWNS_HEAP_P(copy)) {
    02749             xfree(RARRAY_PTR(copy));
    02750         }
    02751         else {
    02752             rb_ary_unshare_safe(copy);
    02753         }
    02754         FL_UNSET_EMBED(copy);
    02755         ARY_SET_PTR(copy, RARRAY_PTR(orig));
    02756         ARY_SET_LEN(copy, RARRAY_LEN(orig));
    02757         rb_ary_set_shared(copy, shared);
    02758     }
    02759     return copy;
    02760 }
    02761
    02762 /*
    02763  *  call-seq:
    02764  *     ary.clear    -> ary
    02765  *
    02766  *  Removes all elements from +self+.
    02767  *
    02768  *     a = [ "a", "b", "c", "d", "e" ]
    02769  *     a.clear    #=> [ ]
    02770  */
    02771
    02772 VALUE
    02773 rb_ary_clear(VALUE ary)
    02774 {
    02775     rb_ary_modify(ary);
    02776     ARY_SET_LEN(ary, 0);
    02777     if (ARY_DEFAULT_SIZE * 2 < ARY_CAPA(ary)) {
    02778         ary_resize_capa(ary, ARY_DEFAULT_SIZE * 2);
    02779     }
    02780     return ary;
    02781 }
    02782
    02783 /*
    02784  *  call-seq:
    02785  *     ary.fill(obj)                                -> ary
    02786  *     ary.fill(obj, start [, length])              -> ary
    02787  *     ary.fill(obj, range )                        -> ary
    02788  *     ary.fill {|index| block }                    -> ary
    02789  *     ary.fill(start [, length] ) {|index| block } -> ary
    02790  *     ary.fill(range) {|index| block }             -> ary
    02791  *
    02792  *  The first three forms set the selected elements of +self+ (which
    02793  *  may be the entire array) to <i>obj</i>. A <i>start</i> of
    02794  *  <code>nil</code> is equivalent to zero. A <i>length</i> of
    02795  *  <code>nil</code> is equivalent to <i>self.length</i>. The last three
    02796  *  forms fill the array with the value of the block. The block is
    02797  *  passed the absolute index of each element to be filled.
    02798  *  Negative values of <i>start</i> count from the end of the array.
    02799  *
    02800  *     a = [ "a", "b", "c", "d" ]
    02801  *     a.fill("x")              #=> ["x", "x", "x", "x"]
    02802  *     a.fill("z", 2, 2)        #=> ["x", "x", "z", "z"]
    02803  *     a.fill("y", 0..1)        #=> ["y", "y", "z", "z"]
    02804  *     a.fill {|i| i*i}         #=> [0, 1, 4, 9]
    02805  *     a.fill(-2) {|i| i*i*i}   #=> [0, 1, 8, 27]
    02806  */
    02807
    02808 static VALUE
    02809 rb_ary_fill(int argc, VALUE *argv, VALUE ary)
    02810 {
    02811     VALUE item, arg1, arg2;
    02812     long beg = 0, end = 0, len = 0;
    02813     VALUE *p, *pend;
    02814     int block_p = FALSE;
    02815
    02816     if (rb_block_given_p()) {
    02817         block_p = TRUE;
    02818         rb_scan_args(argc, argv, "02", &arg1, &arg2);
    02819         argc += 1;              /* hackish */
    02820     }
    02821     else {
    02822         rb_scan_args(argc, argv, "12", &item, &arg1, &arg2);
    02823     }
    02824     switch (argc) {
    02825       case 1:
    02826         beg = 0;
    02827         len = RARRAY_LEN(ary);
    02828         break;
    02829       case 2:
    02830         if (rb_range_beg_len(arg1, &beg, &len, RARRAY_LEN(ary), 1)) {
    02831             break;
    02832         }
    02833         /* fall through */
    02834       case 3:
    02835         beg = NIL_P(arg1) ? 0 : NUM2LONG(arg1);
    02836         if (beg < 0) {
    02837             beg = RARRAY_LEN(ary) + beg;
    02838             if (beg < 0) beg = 0;
    02839         }
    02840         len = NIL_P(arg2) ? RARRAY_LEN(ary) - beg : NUM2LONG(arg2);
    02841         break;
    02842     }
    02843     rb_ary_modify(ary);
    02844     if (len < 0) {
    02845         return ary;
    02846     }
    02847     if (beg >= ARY_MAX_SIZE || len > ARY_MAX_SIZE - beg) {
    02848         rb_raise(rb_eArgError, "argument too big");
    02849     }
    02850     end = beg + len;
    02851     if (RARRAY_LEN(ary) < end) {
    02852         if (end >= ARY_CAPA(ary)) {
    02853             ary_resize_capa(ary, end);
    02854         }
    02855         rb_mem_clear(RARRAY_PTR(ary) + RARRAY_LEN(ary), end - RARRAY_LEN(ary));
    02856         ARY_SET_LEN(ary, end);
    02857     }
    02858
    02859     if (block_p) {
    02860         VALUE v;
    02861         long i;
    02862
    02863         for (i=beg; i<end; i++) {
    02864             v = rb_yield(LONG2NUM(i));
    02865             if (i>=RARRAY_LEN(ary)) break;
    02866             RARRAY_PTR(ary)[i] = v;
    02867         }
    02868     }
    02869     else {
    02870         p = RARRAY_PTR(ary) + beg;
    02871         pend = p + len;
    02872         while (p < pend) {
    02873             *p++ = item;
    02874         }
    02875     }
    02876     return ary;
    02877 }
    02878
    02879 /*
    02880  *  call-seq:
    02881  *     ary + other_ary   -> new_ary
    02882  *
    02883  *  Concatenation---Returns a new array built by concatenating the
    02884  *  two arrays together to produce a third array.
    02885  *
    02886  *     [ 1, 2, 3 ] + [ 4, 5 ]    #=> [ 1, 2, 3, 4, 5 ]
    02887  */
    02888
    02889 VALUE
    02890 rb_ary_plus(VALUE x, VALUE y)
    02891 {
    02892     VALUE z;
    02893     long len;
    02894
    02895     y = to_ary(y);
    02896     len = RARRAY_LEN(x) + RARRAY_LEN(y);
    02897     z = rb_ary_new2(len);
    02898     MEMCPY(RARRAY_PTR(z), RARRAY_PTR(x), VALUE, RARRAY_LEN(x));
    02899     MEMCPY(RARRAY_PTR(z) + RARRAY_LEN(x), RARRAY_PTR(y), VALUE, RARRAY_LEN(y));
    02900     ARY_SET_LEN(z, len);
    02901     return z;
    02902 }
    02903
    02904 /*
    02905  *  call-seq:
    02906  *     ary.concat(other_ary)   -> ary
    02907  *
    02908  *  Appends the elements of <i>other_ary</i> to +self+.
    02909  *
    02910  *     [ "a", "b" ].concat( ["c", "d"] ) #=> [ "a", "b", "c", "d" ]
    02911  */
    02912
    02913
    02914 VALUE
    02915 rb_ary_concat(VALUE x, VALUE y)
    02916 {
    02917     rb_ary_modify_check(x);
    02918     y = to_ary(y);
    02919     if (RARRAY_LEN(y) > 0) {
    02920         rb_ary_splice(x, RARRAY_LEN(x), 0, y);
    02921     }
    02922     return x;
    02923 }
    02924
    02925
    02926 /*
    02927  *  call-seq:
    02928  *     ary * int     -> new_ary
    02929  *     ary * str     -> new_string
    02930  *
    02931  *  Repetition---With a String argument, equivalent to
    02932  *  self.join(str). Otherwise, returns a new array
    02933  *  built by concatenating the _int_ copies of +self+.
    02934  *
    02935  *
    02936  *     [ 1, 2, 3 ] * 3    #=> [ 1, 2, 3, 1, 2, 3, 1, 2, 3 ]
    02937  *     [ 1, 2, 3 ] * ","  #=> "1,2,3"
    02938  *
    02939  */
    02940
    02941 static VALUE
    02942 rb_ary_times(VALUE ary, VALUE times)
    02943 {
    02944     VALUE ary2, tmp, *ptr, *ptr2;
    02945     long i, t, len;
    02946
    02947     tmp = rb_check_string_type(times);
    02948     if (!NIL_P(tmp)) {
    02949         return rb_ary_join(ary, tmp);
    02950     }
    02951
    02952     len = NUM2LONG(times);
    02953     if (len == 0) {
    02954         ary2 = ary_new(rb_obj_class(ary), 0);
    02955         goto out;
    02956     }
    02957     if (len < 0) {
    02958         rb_raise(rb_eArgError, "negative argument");
    02959     }
    02960     if (ARY_MAX_SIZE/len < RARRAY_LEN(ary)) {
    02961         rb_raise(rb_eArgError, "argument too big");
    02962     }
    02963     len *= RARRAY_LEN(ary);
    02964
    02965     ary2 = ary_new(rb_obj_class(ary), len);
    02966     ARY_SET_LEN(ary2, len);
    02967
    02968     ptr = RARRAY_PTR(ary);
    02969     ptr2 = RARRAY_PTR(ary2);
    02970     t = RARRAY_LEN(ary);
    02971     for (i=0; i<len; i+=t) {
    02972         MEMCPY(ptr2+i, ptr, VALUE, t);
    02973     }
    02974   out:
    02975     OBJ_INFECT(ary2, ary);
    02976
    02977     return ary2;
    02978 }
    02979
    02980 /*
    02981  *  call-seq:
    02982  *     ary.assoc(obj)   -> new_ary  or  nil
    02983  *
    02984  *  Searches through an array whose elements are also arrays
    02985  *  comparing _obj_ with the first element of each contained array
    02986  *  using obj.==.
    02987  *  Returns the first contained array that matches (that
    02988  *  is, the first associated array),
    02989  *  or +nil+ if no match is found.
    02990  *  See also <code>Array#rassoc</code>.
    02991  *
    02992  *     s1 = [ "colors", "red", "blue", "green" ]
    02993  *     s2 = [ "letters", "a", "b", "c" ]
    02994  *     s3 = "foo"
    02995  *     a  = [ s1, s2, s3 ]
    02996  *     a.assoc("letters")  #=> [ "letters", "a", "b", "c" ]
    02997  *     a.assoc("foo")      #=> nil
    02998  */
    02999
    03000 VALUE
    03001 rb_ary_assoc(VALUE ary, VALUE key)
    03002 {
    03003     long i;
    03004     VALUE v;
    03005
    03006     for (i = 0; i < RARRAY_LEN(ary); ++i) {
    03007         v = rb_check_array_type(RARRAY_PTR(ary)[i]);
    03008         if (!NIL_P(v) && RARRAY_LEN(v) > 0 &&
    03009             rb_equal(RARRAY_PTR(v)[0], key))
    03010             return v;
    03011     }
    03012     return Qnil;
    03013 }
    03014
    03015 /*
    03016  *  call-seq:
    03017  *     ary.rassoc(obj) -> new_ary or nil
    03018  *
    03019  *  Searches through the array whose elements are also arrays. Compares
    03020  *  _obj_ with the second element of each contained array using
    03021  *  <code>==</code>. Returns the first contained array that matches. See
    03022  *  also <code>Array#assoc</code>.
    03023  *
    03024  *     a = [ [ 1, "one"], [2, "two"], [3, "three"], ["ii", "two"] ]
    03025  *     a.rassoc("two")    #=> [2, "two"]
    03026  *     a.rassoc("four")   #=> nil
    03027  */
    03028
    03029 VALUE
    03030 rb_ary_rassoc(VALUE ary, VALUE value)
    03031 {
    03032     long i;
    03033     VALUE v;
    03034
    03035     for (i = 0; i < RARRAY_LEN(ary); ++i) {
    03036         v = RARRAY_PTR(ary)[i];
    03037         if (TYPE(v) == T_ARRAY &&
    03038             RARRAY_LEN(v) > 1 &&
    03039             rb_equal(RARRAY_PTR(v)[1], value))
    03040             return v;
    03041     }
    03042     return Qnil;
    03043 }
    03044
    03045 static VALUE
    03046 recursive_equal(VALUE ary1, VALUE ary2, int recur)
    03047 {
    03048     long i;
    03049
    03050     if (recur) return Qtrue; /* Subtle! */
    03051     for (i=0; i<RARRAY_LEN(ary1); i++) {
    03052         if (!rb_equal(rb_ary_elt(ary1, i), rb_ary_elt(ary2, i)))
    03053             return Qfalse;
    03054     }
    03055     return Qtrue;
    03056 }
    03057
    03058 /*
    03059  *  call-seq:
    03060  *     ary == other_ary   ->   bool
    03061  *
    03062  *  Equality---Two arrays are equal if they contain the same number
    03063  *  of elements and if each element is equal to (according to
    03064  *  Object.==) the corresponding element in the other array.
    03065  *
    03066  *     [ "a", "c" ]    == [ "a", "c", 7 ]     #=> false
    03067  *     [ "a", "c", 7 ] == [ "a", "c", 7 ]     #=> true
    03068  *     [ "a", "c", 7 ] == [ "a", "d", "f" ]   #=> false
    03069  *
    03070  */
    03071
    03072 static VALUE
    03073 rb_ary_equal(VALUE ary1, VALUE ary2)
    03074 {
    03075     if (ary1 == ary2) return Qtrue;
    03076     if (TYPE(ary2) != T_ARRAY) {
    03077         if (!rb_respond_to(ary2, rb_intern("to_ary"))) {
    03078             return Qfalse;
    03079         }
    03080         return rb_equal(ary2, ary1);
    03081     }
    03082     if (RARRAY_LEN(ary1) != RARRAY_LEN(ary2)) return Qfalse;
    03083     return rb_exec_recursive_paired(recursive_equal, ary1, ary2, ary2);
    03084 }
    03085
    03086 static VALUE
    03087 recursive_eql(VALUE ary1, VALUE ary2, int recur)
    03088 {
    03089     long i;
    03090
    03091     if (recur) return Qtrue; /* Subtle! */
    03092     for (i=0; i<RARRAY_LEN(ary1); i++) {
    03093         if (!rb_eql(rb_ary_elt(ary1, i), rb_ary_elt(ary2, i)))
    03094             return Qfalse;
    03095     }
    03096     return Qtrue;
    03097 }
    03098
    03099 /*
    03100  *  call-seq:
    03101  *     ary.eql?(other)  -> true or false
    03102  *
    03103  *  Returns <code>true</code> if +self+ and _other_ are the same object,
    03104  *  or are both arrays with the same content.
    03105  */
    03106
    03107 static VALUE
    03108 rb_ary_eql(VALUE ary1, VALUE ary2)
    03109 {
    03110     if (ary1 == ary2) return Qtrue;
    03111     if (TYPE(ary2) != T_ARRAY) return Qfalse;
    03112     if (RARRAY_LEN(ary1) != RARRAY_LEN(ary2)) return Qfalse;
    03113     return rb_exec_recursive_paired(recursive_eql, ary1, ary2, ary2);
    03114 }
    03115
    03116 static VALUE
    03117 recursive_hash(VALUE ary, VALUE dummy, int recur)
    03118 {
    03119     long i;
    03120     st_index_t h;
    03121     VALUE n;
    03122
    03123     h = rb_hash_start(RARRAY_LEN(ary));
    03124     if (recur) {
    03125         h = rb_hash_uint(h, NUM2LONG(rb_hash(rb_cArray)));
    03126     }
    03127     else {
    03128         for (i=0; i<RARRAY_LEN(ary); i++) {
    03129             n = rb_hash(RARRAY_PTR(ary)[i]);
    03130             h = rb_hash_uint(h, NUM2LONG(n));
    03131         }
    03132     }
    03133     h = rb_hash_end(h);
    03134     return LONG2FIX(h);
    03135 }
    03136
    03137 /*
    03138  *  call-seq:
    03139  *     ary.hash   -> fixnum
    03140  *
    03141  *  Compute a hash-code for this array. Two arrays with the same content
    03142  *  will have the same hash code (and will compare using <code>eql?</code>).
    03143  */
    03144
    03145 static VALUE
    03146 rb_ary_hash(VALUE ary)
    03147 {
    03148     return rb_exec_recursive_outer(recursive_hash, ary, 0);
    03149 }
    03150
    03151 /*
    03152  *  call-seq:
    03153  *     ary.include?(obj)   -> true or false
    03154  *
    03155  *  Returns <code>true</code> if the given object is present in
    03156  *  +self+ (that is, if any object <code>==</code> <i>anObject</i>),
    03157  *  <code>false</code> otherwise.
    03158  *
    03159  *     a = [ "a", "b", "c" ]
    03160  *     a.include?("b")   #=> true
    03161  *     a.include?("z")   #=> false
    03162  */
    03163
    03164 VALUE
    03165 rb_ary_includes(VALUE ary, VALUE item)
    03166 {
    03167     long i;
    03168
    03169     for (i=0; i<RARRAY_LEN(ary); i++) {
    03170         if (rb_equal(RARRAY_PTR(ary)[i], item)) {
    03171             return Qtrue;
    03172         }
    03173     }
    03174     return Qfalse;
    03175 }
    03176
    03177
    03178 static VALUE
    03179 recursive_cmp(VALUE ary1, VALUE ary2, int recur)
    03180 {
    03181     long i, len;
    03182
    03183     if (recur) return Qundef;   /* Subtle! */
    03184     len = RARRAY_LEN(ary1);
    03185     if (len > RARRAY_LEN(ary2)) {
    03186         len = RARRAY_LEN(ary2);
    03187     }
    03188     for (i=0; i<len; i++) {
    03189         VALUE v = rb_funcall(rb_ary_elt(ary1, i), id_cmp, 1, rb_ary_elt(ary2, i));
    03190         if (v != INT2FIX(0)) {
    03191             return v;
    03192         }
    03193     }
    03194     return Qundef;
    03195 }
    03196
    03197 /*
    03198  *  call-seq:
    03199  *     ary <=> other_ary   ->  -1, 0, +1 or nil
    03200  *
    03201  *  Comparison---Returns an integer (-1, 0,
    03202  *  or +1) if this array is less than, equal to, or greater than
    03203  *  <i>other_ary</i>.  Each object in each array is compared
    03204  *  (using <=>). If any value isn't
    03205  *  equal, then that inequality is the return value. If all the
    03206  *  values found are equal, then the return is based on a
    03207  *  comparison of the array lengths.  Thus, two arrays are
    03208  *  ``equal'' according to <code>Array#<=></code> if and only if they have
    03209  *  the same length and the value of each element is equal to the
    03210  *  value of the corresponding element in the other array.
    03211  *
    03212  *     [ "a", "a", "c" ]    <=> [ "a", "b", "c" ]   #=> -1
    03213  *     [ 1, 2, 3, 4, 5, 6 ] <=> [ 1, 2 ]            #=> +1
    03214  *
    03215  */
    03216
    03217 VALUE
    03218 rb_ary_cmp(VALUE ary1, VALUE ary2)
    03219 {
    03220     long len;
    03221     VALUE v;
    03222
    03223     ary2 = rb_check_array_type(ary2);
    03224     if (NIL_P(ary2)) return Qnil;
    03225     if (ary1 == ary2) return INT2FIX(0);
    03226     v = rb_exec_recursive_paired(recursive_cmp, ary1, ary2, ary2);
    03227     if (v != Qundef) return v;
    03228     len = RARRAY_LEN(ary1) - RARRAY_LEN(ary2);
    03229     if (len == 0) return INT2FIX(0);
    03230     if (len > 0) return INT2FIX(1);
    03231     return INT2FIX(-1);
    03232 }
    03233
    03234 static VALUE
    03235 ary_add_hash(VALUE hash, VALUE ary)
    03236 {
    03237     long i;
    03238
    03239     for (i=0; i<RARRAY_LEN(ary); i++) {
    03240         rb_hash_aset(hash, RARRAY_PTR(ary)[i], Qtrue);
    03241     }
    03242     return hash;
    03243 }
    03244
    03245 static inline VALUE
    03246 ary_tmp_hash_new(void)
    03247 {
    03248     VALUE hash = rb_hash_new();
    03249
    03250     RBASIC(hash)->klass = 0;
    03251     return hash;
    03252 }
    03253
    03254 static VALUE
    03255 ary_make_hash(VALUE ary)
    03256 {
    03257     VALUE hash = ary_tmp_hash_new();
    03258     return ary_add_hash(hash, ary);
    03259 }
    03260
    03261 static VALUE
    03262 ary_add_hash_by(VALUE hash, VALUE ary)
    03263 {
    03264     long i;
    03265
    03266     for (i = 0; i < RARRAY_LEN(ary); ++i) {
    03267         VALUE v = rb_ary_elt(ary, i), k = rb_yield(v);
    03268         if (rb_hash_lookup2(hash, k, Qundef) == Qundef) {
    03269             rb_hash_aset(hash, k, v);
    03270         }
    03271     }
    03272     return hash;
    03273 }
    03274
    03275 static VALUE
    03276 ary_make_hash_by(VALUE ary)
    03277 {
    03278     VALUE hash = ary_tmp_hash_new();
    03279     return ary_add_hash_by(hash, ary);
    03280 }
    03281
    03282 static inline void
    03283 ary_recycle_hash(VALUE hash)
    03284 {
    03285     if (RHASH(hash)->ntbl) {
    03286         st_table *tbl = RHASH(hash)->ntbl;
    03287         RHASH(hash)->ntbl = 0;
    03288         st_free_table(tbl);
    03289     }
    03290 }
    03291
    03292 /*
    03293  *  call-seq:
    03294  *     ary - other_ary    -> new_ary
    03295  *
    03296  *  Array Difference---Returns a new array that is a copy of
    03297  *  the original array, removing any items that also appear in
    03298  *  <i>other_ary</i>. (If you need set-like behavior, see the
    03299  *  library class Set.)
    03300  *
    03301  *     [ 1, 1, 2, 2, 3, 3, 4, 5 ] - [ 1, 2, 4 ]  #=>  [ 3, 3, 5 ]
    03302  */
    03303
    03304 static VALUE
    03305 rb_ary_diff(VALUE ary1, VALUE ary2)
    03306 {
    03307     VALUE ary3;
    03308     volatile VALUE hash;
    03309     long i;
    03310
    03311     hash = ary_make_hash(to_ary(ary2));
    03312     ary3 = rb_ary_new();
    03313
    03314     for (i=0; i<RARRAY_LEN(ary1); i++) {
    03315         if (st_lookup(RHASH_TBL(hash), RARRAY_PTR(ary1)[i], 0)) continue;
    03316         rb_ary_push(ary3, rb_ary_elt(ary1, i));
    03317     }
    03318     ary_recycle_hash(hash);
    03319     return ary3;
    03320 }
    03321
    03322 /*
    03323  *  call-seq:
    03324  *     ary & other_ary      -> new_ary
    03325  *
    03326  *  Set Intersection---Returns a new array
    03327  *  containing elements common to the two arrays, with no duplicates.
    03328  *
    03329  *     [ 1, 1, 3, 5 ] & [ 1, 2, 3 ]   #=> [ 1, 3 ]
    03330  */
    03331
    03332
    03333 static VALUE
    03334 rb_ary_and(VALUE ary1, VALUE ary2)
    03335 {
    03336     VALUE hash, ary3, v, vv;
    03337     long i;
    03338
    03339     ary2 = to_ary(ary2);
    03340     ary3 = rb_ary_new2(RARRAY_LEN(ary1) < RARRAY_LEN(ary2) ?
    03341             RARRAY_LEN(ary1) : RARRAY_LEN(ary2));
    03342     hash = ary_make_hash(ary2);
    03343
    03344     if (RHASH_EMPTY_P(hash))
    03345         return ary3;
    03346
    03347     for (i=0; i<RARRAY_LEN(ary1); i++) {
    03348         v = vv = rb_ary_elt(ary1, i);
    03349         if (st_delete(RHASH_TBL(hash), (st_data_t*)&vv, 0)) {
    03350             rb_ary_push(ary3, v);
    03351         }
    03352     }
    03353     ary_recycle_hash(hash);
    03354
    03355     return ary3;
    03356 }
    03357
    03358 /*
    03359  *  call-seq:
    03360  *     ary | other_ary     -> new_ary
    03361  *
    03362  *  Set Union---Returns a new array by joining this array with
    03363  *  <i>other_ary</i>, removing duplicates.
    03364  *
    03365  *     [ "a", "b", "c" ] | [ "c", "d", "a" ]
    03366  *            #=> [ "a", "b", "c", "d" ]
    03367  */
    03368
    03369 static VALUE
    03370 rb_ary_or(VALUE ary1, VALUE ary2)
    03371 {
    03372     VALUE hash, ary3;
    03373     VALUE v, vv;
    03374     long i;
    03375
    03376     ary2 = to_ary(ary2);
    03377     ary3 = rb_ary_new2(RARRAY_LEN(ary1)+RARRAY_LEN(ary2));
    03378     hash = ary_add_hash(ary_make_hash(ary1), ary2);
    03379
    03380     for (i=0; i<RARRAY_LEN(ary1); i++) {
    03381         v = vv = rb_ary_elt(ary1, i);
    03382         if (st_delete(RHASH_TBL(hash), (st_data_t*)&vv, 0)) {
    03383             rb_ary_push(ary3, v);
    03384         }
    03385     }
    03386     for (i=0; i<RARRAY_LEN(ary2); i++) {
    03387         v = vv = rb_ary_elt(ary2, i);
    03388         if (st_delete(RHASH_TBL(hash), (st_data_t*)&vv, 0)) {
    03389             rb_ary_push(ary3, v);
    03390         }
    03391     }
    03392     ary_recycle_hash(hash);
    03393     return ary3;
    03394 }
    03395
    03396 static int
    03397 push_value(st_data_t key, st_data_t val, st_data_t ary)
    03398 {
    03399     rb_ary_push((VALUE)ary, (VALUE)val);
    03400     return ST_CONTINUE;
    03401 }
    03402
    03403 /*
    03404  *  call-seq:
    03405  *     ary.uniq! -> ary or nil
    03406  *
    03407  *  Removes duplicate elements from +self+.
    03408  *  Returns <code>nil</code> if no changes are made (that is, no
    03409  *  duplicates are found).
    03410  *
    03411  *     a = [ "a", "a", "b", "b", "c" ]
    03412  *     a.uniq!   #=> ["a", "b", "c"]
    03413  *     b = [ "a", "b", "c" ]
    03414  *     b.uniq!   #=> nil
    03415  *     c = [ "a:def", "a:xyz", "b:abc", "b:xyz", "c:jkl" ]
    03416  *     c.uniq! {|s| s[/^\w+/]}  #=> [ "a:def", "b:abc", "c:jkl" ]
    03417  */
    03418
    03419 static VALUE
    03420 rb_ary_uniq_bang(VALUE ary)
    03421 {
    03422     VALUE hash, v;
    03423     long i, j;
    03424
    03425     rb_ary_modify_check(ary);
    03426     if (RARRAY_LEN(ary) <= 1)
    03427         return Qnil;
    03428     if (rb_block_given_p()) {
    03429         hash = ary_make_hash_by(ary);
    03430         if (RARRAY_LEN(ary) == (i = RHASH_SIZE(hash))) {
    03431             return Qnil;
    03432         }
    03433         ary_resize_capa(ary, i);
    03434         ARY_SET_LEN(ary, 0);
    03435         st_foreach(RHASH_TBL(hash), push_value, ary);
    03436     }
    03437     else {
    03438         hash = ary_make_hash(ary);
    03439         if (RARRAY_LEN(ary) == (long)RHASH_SIZE(hash)) {
    03440             return Qnil;
    03441         }
    03442         for (i=j=0; i<RARRAY_LEN(ary); i++) {
    03443             st_data_t vv = (st_data_t)(v = rb_ary_elt(ary, i));
    03444             if (st_delete(RHASH_TBL(hash), &vv, 0)) {
    03445                 rb_ary_store(ary, j++, v);
    03446             }
    03447         }
    03448         ARY_SET_LEN(ary, j);
    03449     }
    03450     ary_recycle_hash(hash);
    03451
    03452     return ary;
    03453 }
    03454
    03455 /*
    03456  *  call-seq:
    03457  *     ary.uniq   -> new_ary
    03458  *
    03459  *  Returns a new array by removing duplicate values in +self+.
    03460  *
    03461  *     a = [ "a", "a", "b", "b", "c" ]
    03462  *     a.uniq   #=> ["a", "b", "c"]
    03463  *     c = [ "a:def", "a:xyz", "b:abc", "b:xyz", "c:jkl" ]
    03464  *     c.uniq {|s| s[/^\w+/]}  #=> [ "a:def", "b:abc", "c:jkl" ]
    03465  */
    03466
    03467 static VALUE
    03468 rb_ary_uniq(VALUE ary)
    03469 {
    03470     VALUE hash, uniq, v;
    03471     long i;
    03472
    03473     if (RARRAY_LEN(ary) <= 1)
    03474         return rb_ary_dup(ary);
    03475     if (rb_block_given_p()) {
    03476         hash = ary_make_hash_by(ary);
    03477         uniq = ary_new(rb_obj_class(ary), RHASH_SIZE(hash));
    03478         st_foreach(RHASH_TBL(hash), push_value, uniq);
    03479     }
    03480     else {
    03481         hash = ary_make_hash(ary);
    03482         uniq = ary_new(rb_obj_class(ary), RHASH_SIZE(hash));
    03483         for (i=0; i<RARRAY_LEN(ary); i++) {
    03484             st_data_t vv = (st_data_t)(v = rb_ary_elt(ary, i));
    03485             if (st_delete(RHASH_TBL(hash), &vv, 0)) {
    03486                 rb_ary_push(uniq, v);
    03487             }
    03488         }
    03489     }
    03490     ary_recycle_hash(hash);
    03491
    03492     return uniq;
    03493 }
    03494
    03495 /*
    03496  *  call-seq:
    03497  *     ary.compact!    -> ary  or  nil
    03498  *
    03499  *  Removes +nil+ elements from the array.
    03500  *  Returns +nil+ if no changes were made, otherwise returns
    03501  *  </i>ary</i>.
    03502  *
    03503  *     [ "a", nil, "b", nil, "c" ].compact! #=> [ "a", "b", "c" ]
    03504  *     [ "a", "b", "c" ].compact!           #=> nil
    03505  */
    03506
    03507 static VALUE
    03508 rb_ary_compact_bang(VALUE ary)
    03509 {
    03510     VALUE *p, *t, *end;
    03511     long n;
    03512
    03513     rb_ary_modify(ary);
    03514     p = t = RARRAY_PTR(ary);
    03515     end = p + RARRAY_LEN(ary);
    03516
    03517     while (t < end) {
    03518         if (NIL_P(*t)) t++;
    03519         else *p++ = *t++;
    03520     }
    03521     n = p - RARRAY_PTR(ary);
    03522     if (RARRAY_LEN(ary) == n) {
    03523         return Qnil;
    03524     }
    03525     ARY_SET_LEN(ary, n);
    03526     if (n * 2 < ARY_CAPA(ary) && ARY_DEFAULT_SIZE * 2 < ARY_CAPA(ary)) {
    03527         ary_resize_capa(ary, n * 2);
    03528     }
    03529
    03530     return ary;
    03531 }
    03532
    03533 /*
    03534  *  call-seq:
    03535  *     ary.compact     -> new_ary
    03536  *
    03537  *  Returns a copy of +self+ with all +nil+ elements removed.
    03538  *
    03539  *     [ "a", nil, "b", nil, "c", nil ].compact
    03540  *                       #=> [ "a", "b", "c" ]
    03541  */
    03542
    03543 static VALUE
    03544 rb_ary_compact(VALUE ary)
    03545 {
    03546     ary = rb_ary_dup(ary);
    03547     rb_ary_compact_bang(ary);
    03548     return ary;
    03549 }
    03550
    03551 /*
    03552  *  call-seq:
    03553  *     ary.count      -> int
    03554  *     ary.count(obj) -> int
    03555  *     ary.count { |item| block }  -> int
    03556  *
    03557  *  Returns the number of elements.  If an argument is given, counts
    03558  *  the number of elements which equals to <i>obj</i>.  If a block is
    03559  *  given, counts the number of elements yielding a true value.
    03560  *
    03561  *     ary = [1, 2, 4, 2]
    03562  *     ary.count             #=> 4
    03563  *     ary.count(2)          #=> 2
    03564  *     ary.count{|x|x%2==0}  #=> 3
    03565  *
    03566  */
    03567
    03568 static VALUE
    03569 rb_ary_count(int argc, VALUE *argv, VALUE ary)
    03570 {
    03571     long n = 0;
    03572
    03573     if (argc == 0) {
    03574         VALUE *p, *pend;
    03575
    03576         if (!rb_block_given_p())
    03577             return LONG2NUM(RARRAY_LEN(ary));
    03578
    03579         for (p = RARRAY_PTR(ary), pend = p + RARRAY_LEN(ary); p < pend; p++) {
    03580             if (RTEST(rb_yield(*p))) n++;
    03581         }
    03582     }
    03583     else {
    03584         VALUE obj, *p, *pend;
    03585
    03586         rb_scan_args(argc, argv, "1", &obj);
    03587         if (rb_block_given_p()) {
    03588             rb_warn("given block not used");
    03589         }
    03590         for (p = RARRAY_PTR(ary), pend = p + RARRAY_LEN(ary); p < pend; p++) {
    03591             if (rb_equal(*p, obj)) n++;
    03592         }
    03593     }
    03594
    03595     return LONG2NUM(n);
    03596 }
    03597
    03598 static VALUE
    03599 flatten(VALUE ary, int level, int *modified)
    03600 {
    03601     long i = 0;
    03602     VALUE stack, result, tmp, elt;
    03603     st_table *memo;
    03604     st_data_t id;
    03605
    03606     stack = ary_new(0, ARY_DEFAULT_SIZE);
    03607     result = ary_new(0, RARRAY_LEN(ary));
    03608     memo = st_init_numtable();
    03609     st_insert(memo, (st_data_t)ary, (st_data_t)Qtrue);
    03610     *modified = 0;
    03611
    03612     while (1) {
    03613         while (i < RARRAY_LEN(ary)) {
    03614             elt = RARRAY_PTR(ary)[i++];
    03615             tmp = rb_check_array_type(elt);
    03616             if (RBASIC(result)->klass) {
    03617                 rb_raise(rb_eRuntimeError, "flatten reentered");
    03618             }
    03619             if (NIL_P(tmp) || (level >= 0 && RARRAY_LEN(stack) / 2 >= level)) {
    03620                 rb_ary_push(result, elt);
    03621             }
    03622             else {
    03623                 *modified = 1;
    03624                 id = (st_data_t)tmp;
    03625                 if (st_lookup(memo, id, 0)) {
    03626                     st_free_table(memo);
    03627                     rb_raise(rb_eArgError, "tried to flatten recursive array");
    03628                 }
    03629                 st_insert(memo, id, (st_data_t)Qtrue);
    03630                 rb_ary_push(stack, ary);
    03631                 rb_ary_push(stack, LONG2NUM(i));
    03632                 ary = tmp;
    03633                 i = 0;
    03634             }
    03635         }
    03636         if (RARRAY_LEN(stack) == 0) {
    03637             break;
    03638         }
    03639         id = (st_data_t)ary;
    03640         st_delete(memo, &id, 0);
    03641         tmp = rb_ary_pop(stack);
    03642         i = NUM2LONG(tmp);
    03643         ary = rb_ary_pop(stack);
    03644     }
    03645
    03646     st_free_table(memo);
    03647
    03648     RBASIC(result)->klass = rb_class_of(ary);
    03649     return result;
    03650 }
    03651
    03652 /*
    03653  *  call-seq:
    03654  *     ary.flatten!        -> ary or nil
    03655  *     ary.flatten!(level) -> array or nil
    03656  *
    03657  *  Flattens +self+ in place.
    03658  *  Returns <code>nil</code> if no modifications were made (i.e.,
    03659  *  <i>ary</i> contains no subarrays.)  If the optional <i>level</i>
    03660  *  argument determines the level of recursion to flatten.
    03661  *
    03662  *     a = [ 1, 2, [3, [4, 5] ] ]
    03663  *     a.flatten!   #=> [1, 2, 3, 4, 5]
    03664  *     a.flatten!   #=> nil
    03665  *     a            #=> [1, 2, 3, 4, 5]
    03666  *     a = [ 1, 2, [3, [4, 5] ] ]
    03667  *     a.flatten!(1) #=> [1, 2, 3, [4, 5]]
    03668  */
    03669
    03670 static VALUE
    03671 rb_ary_flatten_bang(int argc, VALUE *argv, VALUE ary)
    03672 {
    03673     int mod = 0, level = -1;
    03674     VALUE result, lv;
    03675
    03676     rb_scan_args(argc, argv, "01", &lv);
    03677     rb_ary_modify_check(ary);
    03678     if (!NIL_P(lv)) level = NUM2INT(lv);
    03679     if (level == 0) return Qnil;
    03680
    03681     result = flatten(ary, level, &mod);
    03682     if (mod == 0) {
    03683         ary_discard(result);
    03684         return Qnil;
    03685     }
    03686     if (!(mod = ARY_EMBED_P(result))) rb_obj_freeze(result);
    03687     rb_ary_replace(ary, result);
    03688     if (mod) ARY_SET_EMBED_LEN(result, 0);
    03689
    03690     return ary;
    03691 }
    03692
    03693 /*
    03694  *  call-seq:
    03695  *     ary.flatten -> new_ary
    03696  *     ary.flatten(level) -> new_ary
    03697  *
    03698  *  Returns a new array that is a one-dimensional flattening of this
    03699  *  array (recursively). That is, for every element that is an array,
    03700  *  extract its elements into the new array.  If the optional
    03701  *  <i>level</i> argument determines the level of recursion to flatten.
    03702  *
    03703  *     s = [ 1, 2, 3 ]           #=> [1, 2, 3]
    03704  *     t = [ 4, 5, 6, [7, 8] ]   #=> [4, 5, 6, [7, 8]]
    03705  *     a = [ s, t, 9, 10 ]       #=> [[1, 2, 3], [4, 5, 6, [7, 8]], 9, 10]
    03706  *     a.flatten                 #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    03707  *     a = [ 1, 2, [3, [4, 5] ] ]
    03708  *     a.flatten(1)              #=> [1, 2, 3, [4, 5]]
    03709  */
    03710
    03711 static VALUE
    03712 rb_ary_flatten(int argc, VALUE *argv, VALUE ary)
    03713 {
    03714     int mod = 0, level = -1;
    03715     VALUE result, lv;
    03716
    03717     rb_scan_args(argc, argv, "01", &lv);
    03718     if (!NIL_P(lv)) level = NUM2INT(lv);
    03719     if (level == 0) return ary_make_shared_copy(ary);
    03720
    03721     result = flatten(ary, level, &mod);
    03722     OBJ_INFECT(result, ary);
    03723
    03724     return result;
    03725 }
    03726
    03727 /*
    03728  *  call-seq:
    03729  *     ary.shuffle!        -> ary
    03730  *
    03731  *  Shuffles elements in +self+ in place.
    03732  */
    03733
    03734
    03735 static VALUE
    03736 rb_ary_shuffle_bang(VALUE ary)
    03737 {
    03738     VALUE *ptr;
    03739     long i = RARRAY_LEN(ary);
    03740
    03741     rb_ary_modify(ary);
    03742     ptr = RARRAY_PTR(ary);
    03743     while (i) {
    03744         long j = (long)(rb_genrand_real()*i);
    03745         VALUE tmp = ptr[--i];
    03746         ptr[i] = ptr[j];
    03747         ptr[j] = tmp;
    03748     }
    03749     return ary;
    03750 }
    03751
    03752
    03753 /*
    03754  *  call-seq:
    03755  *     ary.shuffle -> new_ary
    03756  *
    03757  *  Returns a new array with elements of this array shuffled.
    03758  *
    03759  *     a = [ 1, 2, 3 ]           #=> [1, 2, 3]
    03760  *     a.shuffle                 #=> [2, 3, 1]
    03761  */
    03762
    03763 static VALUE
    03764 rb_ary_shuffle(VALUE ary)
    03765 {
    03766     ary = rb_ary_dup(ary);
    03767     rb_ary_shuffle_bang(ary);
    03768     return ary;
    03769 }
    03770
    03771
    03772 /*
    03773  *  call-seq:
    03774  *     ary.sample        -> obj
    03775  *     ary.sample(n)     -> new_ary
    03776  *
    03777  *  Choose a random element or +n+ random elements from the array. The elements
    03778  *  are chosen by using random and unique indices into the array in order to
    03779  *  ensure that an element doesn't repeat itself unless the array already
    03780  *  contained duplicate elements. If the array is empty the first form returns
    03781  *  <code>nil</code> and the second form returns an empty array.
    03782  *
    03783  */
    03784
    03785
    03786 static VALUE
    03787 rb_ary_sample(int argc, VALUE *argv, VALUE ary)
    03788 {
    03789     VALUE nv, result, *ptr;
    03790     long n, len, i, j, k, idx[10];
    03791
    03792     len = RARRAY_LEN(ary);
    03793     if (argc == 0) {
    03794         if (len == 0) return Qnil;
    03795         i = len == 1 ? 0 : (long)(rb_genrand_real()*len);
    03796         return RARRAY_PTR(ary)[i];
    03797     }
    03798     rb_scan_args(argc, argv, "1", &nv);
    03799     n = NUM2LONG(nv);
    03800     if (n < 0) rb_raise(rb_eArgError, "negative sample number");
    03801     ptr = RARRAY_PTR(ary);
    03802     len = RARRAY_LEN(ary);
    03803     if (n > len) n = len;
    03804     switch (n) {
    03805       case 0: return rb_ary_new2(0);
    03806       case 1:
    03807         return rb_ary_new4(1, &ptr[(long)(rb_genrand_real()*len)]);
    03808       case 2:
    03809         i = (long)(rb_genrand_real()*len);
    03810         j = (long)(rb_genrand_real()*(len-1));
    03811         if (j >= i) j++;
    03812         return rb_ary_new3(2, ptr[i], ptr[j]);
    03813       case 3:
    03814         i = (long)(rb_genrand_real()*len);
    03815         j = (long)(rb_genrand_real()*(len-1));
    03816         k = (long)(rb_genrand_real()*(len-2));
    03817         {
    03818             long l = j, g = i;
    03819             if (j >= i) l = i, g = ++j;
    03820             if (k >= l && (++k >= g)) ++k;
    03821         }
    03822         return rb_ary_new3(3, ptr[i], ptr[j], ptr[k]);
    03823     }
    03824     if ((size_t)n < sizeof(idx)/sizeof(idx[0])) {
    03825         VALUE *ptr_result;
    03826         long sorted[sizeof(idx)/sizeof(idx[0])];
    03827         sorted[0] = idx[0] = (long)(rb_genrand_real()*len);
    03828         for (i=1; i<n; i++) {
    03829             k = (long)(rb_genrand_real()*--len);
    03830             for (j = 0; j < i; ++j) {
    03831                 if (k < sorted[j]) break;
    03832                 ++k;
    03833             }
    03834             memmove(&sorted[j+1], &sorted[j], sizeof(sorted[0])*(i-j));
    03835             sorted[j] = idx[i] = k;
    03836         }
    03837         result = rb_ary_new2(n);
    03838         ptr_result = RARRAY_PTR(result);
    03839         for (i=0; i<n; i++) {
    03840             ptr_result[i] = ptr[idx[i]];
    03841         }
    03842     }
    03843     else {
    03844         VALUE *ptr_result;
    03845         result = rb_ary_new4(len, ptr);
    03846         ptr_result = RARRAY_PTR(result);
    03847         RB_GC_GUARD(ary);
    03848         for (i=0; i<n; i++) {
    03849             j = (long)(rb_genrand_real()*(len-i)) + i;
    03850             nv = ptr_result[j];
    03851             ptr_result[j] = ptr_result[i];
    03852             ptr_result[i] = nv;
    03853         }
    03854     }
    03855     ARY_SET_LEN(result, n);
    03856
    03857     return result;
    03858 }
    03859
    03860
    03861 /*
    03862  *  call-seq:
    03863  *     ary.cycle(n=nil) {|obj| block }  -> nil
    03864  *     ary.cycle(n=nil)                 -> an_enumerator
    03865  *
    03866  *  Calls <i>block</i> for each element repeatedly _n_ times or
    03867  *  forever if none or +nil+ is given.  If a non-positive number is
    03868  *  given or the array is empty, does nothing.  Returns +nil+ if the
    03869  *  loop has finished without getting interrupted.
    03870  *
    03871  *  If no block is given, an enumerator is returned instead.
    03872  *
    03873  *
    03874  *     a = ["a", "b", "c"]
    03875  *     a.cycle {|x| puts x }  # print, a, b, c, a, b, c,.. forever.
    03876  *     a.cycle(2) {|x| puts x }  # print, a, b, c, a, b, c.
    03877  *
    03878  */
    03879
    03880 static VALUE
    03881 rb_ary_cycle(int argc, VALUE *argv, VALUE ary)
    03882 {
    03883     long n, i;
    03884     VALUE nv = Qnil;
    03885
    03886     rb_scan_args(argc, argv, "01", &nv);
    03887
    03888     RETURN_ENUMERATOR(ary, argc, argv);
    03889     if (NIL_P(nv)) {
    03890         n = -1;
    03891     }
    03892     else {
    03893         n = NUM2LONG(nv);
    03894         if (n <= 0) return Qnil;
    03895     }
    03896
    03897     while (RARRAY_LEN(ary) > 0 && (n < 0 || 0 < n--)) {
    03898         for (i=0; i<RARRAY_LEN(ary); i++) {
    03899             rb_yield(RARRAY_PTR(ary)[i]);
    03900         }
    03901     }
    03902     return Qnil;
    03903 }
    03904
    03905 #define tmpbuf(n, size) rb_str_tmp_new((n)*(size))
    03906 #define tmpbuf_discard(s) (rb_str_resize((s), 0L), RBASIC(s)->klass = rb_cString)
    03907 #define tmpary(n) rb_ary_tmp_new(n)
    03908 #define tmpary_discard(a) (ary_discard(a), RBASIC(a)->klass = rb_cArray)
    03909
    03910 /*
    03911  * Recursively compute permutations of r elements of the set [0..n-1].
    03912  * When we have a complete permutation of array indexes, copy the values
    03913  * at those indexes into a new array and yield that array.
    03914  *
    03915  * n: the size of the set
    03916  * r: the number of elements in each permutation
    03917  * p: the array (of size r) that we're filling in
    03918  * index: what index we're filling in now
    03919  * used: an array of booleans: whether a given index is already used
    03920  * values: the Ruby array that holds the actual values to permute
    03921  */
    03922 static void
    03923 permute0(long n, long r, long *p, long index, char *used, VALUE values)
    03924 {
    03925     long i,j;
    03926     for (i = 0; i < n; i++) {
    03927         if (used[i] == 0) {
    03928             p[index] = i;
    03929             if (index < r-1) {             /* if not done yet */
    03930                 used[i] = 1;               /* mark index used */
    03931                 permute0(n, r, p, index+1, /* recurse */
    03932                          used, values);
    03933                 used[i] = 0;               /* index unused */
    03934             }
    03935             else {
    03936                 /* We have a complete permutation of array indexes */
    03937                 /* Build a ruby array of the corresponding values */
    03938                 /* And yield it to the associated block */
    03939                 VALUE result = rb_ary_new2(r);
    03940                 VALUE *result_array = RARRAY_PTR(result);
    03941                 const VALUE *values_array = RARRAY_PTR(values);
    03942
    03943                 for (j = 0; j < r; j++) result_array[j] = values_array[p[j]];
    03944                 ARY_SET_LEN(result, r);
    03945                 rb_yield(result);
    03946                 if (RBASIC(values)->klass) {
    03947                     rb_raise(rb_eRuntimeError, "permute reentered");
    03948                 }
    03949             }
    03950         }
    03951     }
    03952 }
    03953
    03954 /*
    03955  *  call-seq:
    03956  *     ary.permutation { |p| block }          -> ary
    03957  *     ary.permutation                        -> an_enumerator
    03958  *     ary.permutation(n) { |p| block }       -> ary
    03959  *     ary.permutation(n)                     -> an_enumerator
    03960  *
    03961  * When invoked with a block, yield all permutations of length <i>n</i>
    03962  * of the elements of <i>ary</i>, then return the array itself.
    03963  * If <i>n</i> is not specified, yield all permutations of all elements.
    03964  * The implementation makes no guarantees about the order in which
    03965  * the permutations are yielded.
    03966  *
    03967  * If no block is given, an enumerator is returned instead.
    03968  *
    03969  * Examples:
    03970  *
    03971  *     a = [1, 2, 3]
    03972  *     a.permutation.to_a     #=> [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
    03973  *     a.permutation(1).to_a  #=> [[1],[2],[3]]
    03974  *     a.permutation(2).to_a  #=> [[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]]
    03975  *     a.permutation(3).to_a  #=> [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
    03976  *     a.permutation(0).to_a  #=> [[]] # one permutation of length 0
    03977  *     a.permutation(4).to_a  #=> []   # no permutations of length 4
    03978  */
    03979
    03980 static VALUE
    03981 rb_ary_permutation(int argc, VALUE *argv, VALUE ary)
    03982 {
    03983     VALUE num;
    03984     long r, n, i;
    03985
    03986     n = RARRAY_LEN(ary);                  /* Array length */
    03987     RETURN_ENUMERATOR(ary, argc, argv);   /* Return enumerator if no block */
    03988     rb_scan_args(argc, argv, "01", &num);
    03989     r = NIL_P(num) ? n : NUM2LONG(num);   /* Permutation size from argument */
    03990
    03991     if (r < 0 || n < r) {
    03992         /* no permutations: yield nothing */
    03993     }
    03994     else if (r == 0) { /* exactly one permutation: the zero-length array */
    03995         rb_yield(rb_ary_new2(0));
    03996     }
    03997     else if (r == 1) { /* this is a special, easy case */
    03998         for (i = 0; i < RARRAY_LEN(ary); i++) {
    03999             rb_yield(rb_ary_new3(1, RARRAY_PTR(ary)[i]));
    04000         }
    04001     }
    04002     else {             /* this is the general case */
    04003         volatile VALUE t0 = tmpbuf(n,sizeof(long));
    04004         long *p = (long*)RSTRING_PTR(t0);
    04005         volatile VALUE t1 = tmpbuf(n,sizeof(char));
    04006         char *used = (char*)RSTRING_PTR(t1);
    04007         VALUE ary0 = ary_make_shared_copy(ary); /* private defensive copy of ary */
    04008         RBASIC(ary0)->klass = 0;
    04009
    04010         MEMZERO(used, char, n); /* initialize array */
    04011
    04012         permute0(n, r, p, 0, used, ary0); /* compute and yield permutations */
    04013         tmpbuf_discard(t0);
    04014         tmpbuf_discard(t1);
    04015         RBASIC(ary0)->klass = rb_cArray;
    04016     }
    04017     return ary;
    04018 }
    04019
    04020 /*
    04021  *  call-seq:
    04022  *     ary.combination(n) { |c| block }    -> ary
    04023  *     ary.combination(n)                  -> an_enumerator
    04024  *
    04025  * When invoked with a block, yields all combinations of length <i>n</i>
    04026  * of elements from <i>ary</i> and then returns <i>ary</i> itself.
    04027  * The implementation makes no guarantees about the order in which
    04028  * the combinations are yielded.
    04029  *
    04030  * If no block is given, an enumerator is returned instead.
    04031  *
    04032  * Examples:
    04033  *
    04034  *     a = [1, 2, 3, 4]
    04035  *     a.combination(1).to_a  #=> [[1],[2],[3],[4]]
    04036  *     a.combination(2).to_a  #=> [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]
    04037  *     a.combination(3).to_a  #=> [[1,2,3],[1,2,4],[1,3,4],[2,3,4]]
    04038  *     a.combination(4).to_a  #=> [[1,2,3,4]]
    04039  *     a.combination(0).to_a  #=> [[]] # one combination of length 0
    04040  *     a.combination(5).to_a  #=> []   # no combinations of length 5
    04041  *
    04042  */
    04043
    04044 static VALUE
    04045 rb_ary_combination(VALUE ary, VALUE num)
    04046 {
    04047     long n, i, len;
    04048
    04049     n = NUM2LONG(num);
    04050     RETURN_ENUMERATOR(ary, 1, &num);
    04051     len = RARRAY_LEN(ary);
    04052     if (n < 0 || len < n) {
    04053         /* yield nothing */
    04054     }
    04055     else if (n == 0) {
    04056         rb_yield(rb_ary_new2(0));
    04057     }
    04058     else if (n == 1) {
    04059         for (i = 0; i < len; i++) {
    04060             rb_yield(rb_ary_new3(1, RARRAY_PTR(ary)[i]));
    04061         }
    04062     }
    04063     else {
    04064         volatile VALUE t0 = tmpbuf(n+1, sizeof(long));
    04065         long *stack = (long*)RSTRING_PTR(t0);
    04066         volatile VALUE cc = tmpary(n);
    04067         VALUE *chosen = RARRAY_PTR(cc);
    04068         long lev = 0;
    04069
    04070         MEMZERO(stack, long, n);
    04071         stack[0] = -1;
    04072         for (;;) {
    04073             chosen[lev] = RARRAY_PTR(ary)[stack[lev+1]];
    04074             for (lev++; lev < n; lev++) {
    04075                 chosen[lev] = RARRAY_PTR(ary)[stack[lev+1] = stack[lev]+1];
    04076             }
    04077             rb_yield(rb_ary_new4(n, chosen));
    04078             if (RBASIC(t0)->klass) {
    04079                 rb_raise(rb_eRuntimeError, "combination reentered");
    04080             }
    04081             do {
    04082                 if (lev == 0) goto done;
    04083                 stack[lev--]++;
    04084             } while (stack[lev+1]+n == len+lev+1);
    04085         }
    04086     done:
    04087         tmpbuf_discard(t0);
    04088         tmpary_discard(cc);
    04089     }
    04090     return ary;
    04091 }
    04092
    04093 /*
    04094  * Recursively compute repeated permutations of r elements of the set
    04095  * [0..n-1].
    04096  * When we have a complete repeated permutation of array indexes, copy the
    04097  * values at those indexes into a new array and yield that array.
    04098  *
    04099  * n: the size of the set
    04100  * r: the number of elements in each permutation
    04101  * p: the array (of size r) that we're filling in
    04102  * index: what index we're filling in now
    04103  * values: the Ruby array that holds the actual values to permute
    04104  */
    04105 static void
    04106 rpermute0(long n, long r, long *p, long index, VALUE values)
    04107 {
    04108     long i, j;
    04109     for (i = 0; i < n; i++) {
    04110         p[index] = i;
    04111         if (index < r-1) {              /* if not done yet */
    04112             rpermute0(n, r, p, index+1, values); /* recurse */
    04113         }
    04114         else {
    04115             /* We have a complete permutation of array indexes */
    04116             /* Build a ruby array of the corresponding values */
    04117             /* And yield it to the associated block */
    04118             VALUE result = rb_ary_new2(r);
    04119             VALUE *result_array = RARRAY_PTR(result);
    04120             const VALUE *values_array = RARRAY_PTR(values);
    04121
    04122             for (j = 0; j < r; j++) result_array[j] = values_array[p[j]];
    04123             ARY_SET_LEN(result, r);
    04124             rb_yield(result);
    04125             if (RBASIC(values)->klass) {
    04126                 rb_raise(rb_eRuntimeError, "repeated permute reentered");
    04127             }
    04128         }
    04129     }
    04130 }
    04131
    04132 /*
    04133  *  call-seq:
    04134  *     ary.repeated_permutation(n) { |p| block } -> ary
    04135  *     ary.repeated_permutation(n)               -> an_enumerator
    04136  *
    04137  * When invoked with a block, yield all repeated permutations of length
    04138  * <i>n</i> of the elements of <i>ary</i>, then return the array itself.
    04139  * The implementation makes no guarantees about the order in which
    04140  * the repeated permutations are yielded.
    04141  *
    04142  * If no block is given, an enumerator is returned instead.
    04143  *
    04144  * Examples:
    04145  *
    04146  *     a = [1, 2]
    04147  *     a.repeated_permutation(1).to_a  #=> [[1], [2]]
    04148  *     a.repeated_permutation(2).to_a  #=> [[1,1],[1,2],[2,1],[2,2]]
    04149  *     a.repeated_permutation(3).to_a  #=> [[1,1,1],[1,1,2],[1,2,1],[1,2,2],
    04150  *                                     #    [2,1,1],[2,1,2],[2,2,1],[2,2,2]]
    04151  *     a.repeated_permutation(0).to_a  #=> [[]] # one permutation of length 0
    04152  */
    04153
    04154 static VALUE
    04155 rb_ary_repeated_permutation(VALUE ary, VALUE num)
    04156 {
    04157     long r, n, i;
    04158
    04159     n = RARRAY_LEN(ary);                  /* Array length */
    04160     RETURN_ENUMERATOR(ary, 1, &num);      /* Return enumerator if no block */
    04161     r = NUM2LONG(num);                    /* Permutation size from argument */
    04162
    04163     if (r < 0) {
    04164         /* no permutations: yield nothing */
    04165     }
    04166     else if (r == 0) { /* exactly one permutation: the zero-length array */
    04167         rb_yield(rb_ary_new2(0));
    04168     }
    04169     else if (r == 1) { /* this is a special, easy case */
    04170         for (i = 0; i < RARRAY_LEN(ary); i++) {
    04171             rb_yield(rb_ary_new3(1, RARRAY_PTR(ary)[i]));
    04172         }
    04173     }
    04174     else {             /* this is the general case */
    04175         volatile VALUE t0 = tmpbuf(r, sizeof(long));
    04176         long *p = (long*)RSTRING_PTR(t0);
    04177         VALUE ary0 = ary_make_shared_copy(ary); /* private defensive copy of ary */
    04178         RBASIC(ary0)->klass = 0;
    04179
    04180         rpermute0(n, r, p, 0, ary0); /* compute and yield repeated permutations */
    04181         tmpbuf_discard(t0);
    04182         RBASIC(ary0)->klass = rb_cArray;
    04183     }
    04184     return ary;
    04185 }
    04186
    04187 static void
    04188 rcombinate0(long n, long r, long *p, long index, long rest, VALUE values)
    04189 {
    04190     long j;
    04191     if (rest > 0) {
    04192         for (; index < n; ++index) {
    04193             p[r-rest] = index;
    04194             rcombinate0(n, r, p, index, rest-1, values);
    04195         }
    04196     }
    04197     else {
    04198         VALUE result = rb_ary_new2(r);
    04199         VALUE *result_array = RARRAY_PTR(result);
    04200         const VALUE *values_array = RARRAY_PTR(values);
    04201
    04202         for (j = 0; j < r; ++j) result_array[j] = values_array[p[j]];
    04203         ARY_SET_LEN(result, r);
    04204         rb_yield(result);
    04205         if (RBASIC(values)->klass) {
    04206             rb_raise(rb_eRuntimeError, "repeated combination reentered");
    04207         }
    04208     }
    04209 }
    04210
    04211 /*
    04212  *  call-seq:
    04213  *     ary.repeated_combination(n) { |c| block } -> ary
    04214  *     ary.repeated_combination(n)               -> an_enumerator
    04215  *
    04216  * When invoked with a block, yields all repeated combinations of
    04217  * length <i>n</i> of elements from <i>ary</i> and then returns
    04218  * <i>ary</i> itself.
    04219  * The implementation makes no guarantees about the order in which
    04220  * the repeated combinations are yielded.
    04221  *
    04222  * If no block is given, an enumerator is returned instead.
    04223  *
    04224  * Examples:
    04225  *
    04226  *     a = [1, 2, 3]
    04227  *     a.repeated_combination(1).to_a  #=> [[1], [2], [3]]
    04228  *     a.repeated_combination(2).to_a  #=> [[1,1],[1,2],[1,3],[2,2],[2,3],[3,3]]
    04229  *     a.repeated_combination(3).to_a  #=> [[1,1,1],[1,1,2],[1,1,3],[1,2,2],[1,2,3],
    04230  *                                     #    [1,3,3],[2,2,2],[2,2,3],[2,3,3],[3,3,3]]
    04231  *     a.repeated_combination(4).to_a  #=> [[1,1,1,1],[1,1,1,2],[1,1,1,3],[1,1,2,2],[1,1,2,3],
    04232  *                                     #    [1,1,3,3],[1,2,2,2],[1,2,2,3],[1,2,3,3],[1,3,3,3],
    04233  *                                     #    [2,2,2,2],[2,2,2,3],[2,2,3,3],[2,3,3,3],[3,3,3,3]]
    04234  *     a.repeated_combination(0).to_a  #=> [[]] # one combination of length 0
    04235  *
    04236  */
    04237
    04238 static VALUE
    04239 rb_ary_repeated_combination(VALUE ary, VALUE num)
    04240 {
    04241     long n, i, len;
    04242
    04243     n = NUM2LONG(num);                 /* Combination size from argument */
    04244     RETURN_ENUMERATOR(ary, 1, &num);   /* Return enumerator if no block */
    04245     len = RARRAY_LEN(ary);
    04246     if (n < 0) {
    04247         /* yield nothing */
    04248     }
    04249     else if (n == 0) {
    04250         rb_yield(rb_ary_new2(0));
    04251     }
    04252     else if (n == 1) {
    04253         for (i = 0; i < len; i++) {
    04254             rb_yield(rb_ary_new3(1, RARRAY_PTR(ary)[i]));
    04255         }
    04256     }
    04257     else if (len == 0) {
    04258         /* yield nothing */
    04259     }
    04260     else {
    04261         volatile VALUE t0 = tmpbuf(n, sizeof(long));
    04262         long *p = (long*)RSTRING_PTR(t0);
    04263         VALUE ary0 = ary_make_shared_copy(ary); /* private defensive copy of ary */
    04264         RBASIC(ary0)->klass = 0;
    04265
    04266         rcombinate0(len, n, p, 0, n, ary0); /* compute and yield repeated combinations */
    04267         tmpbuf_discard(t0);
    04268         RBASIC(ary0)->klass = rb_cArray;
    04269     }
    04270     return ary;
    04271 }
    04272
    04273 /*
    04274  *  call-seq:
    04275  *     ary.product(other_ary, ...)                -> new_ary
    04276  *     ary.product(other_ary, ...) { |p| block }  -> ary
    04277  *
    04278  *  Returns an array of all combinations of elements from all arrays,
    04279  *  The length of the returned array is the product of the length
    04280  *  of +self+ and the argument arrays.
    04281  *  If given a block, <i>product</i> will yield all combinations
    04282  *  and return +self+ instead.
    04283  *
    04284  *
    04285  *     [1,2,3].product([4,5])     #=> [[1,4],[1,5],[2,4],[2,5],[3,4],[3,5]]
    04286  *     [1,2].product([1,2])       #=> [[1,1],[1,2],[2,1],[2,2]]
    04287  *     [1,2].product([3,4],[5,6]) #=> [[1,3,5],[1,3,6],[1,4,5],[1,4,6],
    04288  *                                #     [2,3,5],[2,3,6],[2,4,5],[2,4,6]]
    04289  *     [1,2].product()            #=> [[1],[2]]
    04290  *     [1,2].product([])          #=> []
    04291  */
    04292
    04293 static VALUE
    04294 rb_ary_product(int argc, VALUE *argv, VALUE ary)
    04295 {
    04296     int n = argc+1;    /* How many arrays we're operating on */
    04297     volatile VALUE t0 = tmpary(n);
    04298     volatile VALUE t1 = tmpbuf(n, sizeof(int));
    04299     VALUE *arrays = RARRAY_PTR(t0); /* The arrays we're computing the product of */
    04300     int *counters = (int*)RSTRING_PTR(t1); /* The current position in each one */
    04301     VALUE result = Qnil;      /* The array we'll be returning, when no block given */
    04302     long i,j;
    04303     long resultlen = 1;
    04304
    04305     RBASIC(t0)->klass = 0;
    04306     RBASIC(t1)->klass = 0;
    04307
    04308     /* initialize the arrays of arrays */
    04309     ARY_SET_LEN(t0, n);
    04310     arrays[0] = ary;
    04311     for (i = 1; i < n; i++) arrays[i] = Qnil;
    04312     for (i = 1; i < n; i++) arrays[i] = to_ary(argv[i-1]);
    04313
    04314     /* initialize the counters for the arrays */
    04315     for (i = 0; i < n; i++) counters[i] = 0;
    04316
    04317     /* Otherwise, allocate and fill in an array of results */
    04318     if (rb_block_given_p()) {
    04319         /* Make defensive copies of arrays; exit if any is empty */
    04320         for (i = 0; i < n; i++) {
    04321             if (RARRAY_LEN(arrays[i]) == 0) goto done;
    04322             arrays[i] = ary_make_shared_copy(arrays[i]);
    04323         }
    04324     }
    04325     else {
    04326         /* Compute the length of the result array; return [] if any is empty */
    04327         for (i = 0; i < n; i++) {
    04328             long k = RARRAY_LEN(arrays[i]), l = resultlen;
    04329             if (k == 0) {
    04330                 result = rb_ary_new2(0);
    04331                 goto done;
    04332             }
    04333             resultlen *= k;
    04334             if (resultlen < k || resultlen < l || resultlen / k != l) {
    04335                 rb_raise(rb_eRangeError, "too big to product");
    04336             }
    04337         }
    04338         result = rb_ary_new2(resultlen);
    04339     }
    04340     for (;;) {
    04341         int m;
    04342         /* fill in one subarray */
    04343         VALUE subarray = rb_ary_new2(n);
    04344         for (j = 0; j < n; j++) {
    04345             rb_ary_push(subarray, rb_ary_entry(arrays[j], counters[j]));
    04346         }
    04347
    04348         /* put it on the result array */
    04349         if(NIL_P(result)) {
    04350             FL_SET(t0, FL_USER5);
    04351             rb_yield(subarray);
    04352             if (! FL_TEST(t0, FL_USER5)) {
    04353                 rb_raise(rb_eRuntimeError, "product reentered");
    04354             }
    04355             else {
    04356                 FL_UNSET(t0, FL_USER5);
    04357             }
    04358         }
    04359         else {
    04360             rb_ary_push(result, subarray);
    04361         }
    04362
    04363         /*
    04364          * Increment the last counter.  If it overflows, reset to 0
    04365          * and increment the one before it.
    04366          */
    04367         m = n-1;
    04368         counters[m]++;
    04369         while (counters[m] == RARRAY_LEN(arrays[m])) {
    04370             counters[m] = 0;
    04371             /* If the first counter overlows, we are done */
    04372             if (--m < 0) goto done;
    04373             counters[m]++;
    04374         }
    04375     }
    04376 done:
    04377     tmpary_discard(t0);
    04378     tmpbuf_discard(t1);
    04379
    04380     return NIL_P(result) ? ary : result;
    04381 }
    04382
    04383 /*
    04384  *  call-seq:
    04385  *     ary.take(n)               -> new_ary
    04386  *
    04387  *  Returns first n elements from <i>ary</i>.
    04388  *
    04389  *     a = [1, 2, 3, 4, 5, 0]
    04390  *     a.take(3)             #=> [1, 2, 3]
    04391  *
    04392  */
    04393
    04394 static VALUE
    04395 rb_ary_take(VALUE obj, VALUE n)
    04396 {
    04397     long len = NUM2LONG(n);
    04398     if (len < 0) {
    04399         rb_raise(rb_eArgError, "attempt to take negative size");
    04400     }
    04401     return rb_ary_subseq(obj, 0, len);
    04402 }
    04403
    04404 /*
    04405  *  call-seq:
    04406  *     ary.take_while {|arr| block }   -> new_ary
    04407  *     ary.take_while                  -> an_enumerator
    04408  *
    04409  *  Passes elements to the block until the block returns +nil+ or +false+,
    04410  *  then stops iterating and returns an array of all prior elements.
    04411  *
    04412  *  If no block is given, an enumerator is returned instead.
    04413  *
    04414  *     a = [1, 2, 3, 4, 5, 0]
    04415  *     a.take_while {|i| i < 3 }   #=> [1, 2]
    04416  *
    04417  */
    04418
    04419 static VALUE
    04420 rb_ary_take_while(VALUE ary)
    04421 {
    04422     long i;
    04423
    04424     RETURN_ENUMERATOR(ary, 0, 0);
    04425     for (i = 0; i < RARRAY_LEN(ary); i++) {
    04426         if (!RTEST(rb_yield(RARRAY_PTR(ary)[i]))) break;
    04427     }
    04428     return rb_ary_take(ary, LONG2FIX(i));
    04429 }
    04430
    04431 /*
    04432  *  call-seq:
    04433  *     ary.drop(n)               -> new_ary
    04434  *
    04435  *  Drops first n elements from <i>ary</i>, and returns rest elements
    04436  *  in an array.
    04437  *
    04438  *     a = [1, 2, 3, 4, 5, 0]
    04439  *     a.drop(3)             #=> [4, 5, 0]
    04440  *
    04441  */
    04442
    04443 static VALUE
    04444 rb_ary_drop(VALUE ary, VALUE n)
    04445 {
    04446     VALUE result;
    04447     long pos = NUM2LONG(n);
    04448     if (pos < 0) {
    04449         rb_raise(rb_eArgError, "attempt to drop negative size");
    04450     }
    04451
    04452     result = rb_ary_subseq(ary, pos, RARRAY_LEN(ary));
    04453     if (result == Qnil) result = rb_ary_new();
    04454     return result;
    04455 }
    04456
    04457 /*
    04458  *  call-seq:
    04459  *     ary.drop_while {|arr| block }   -> new_ary
    04460  *     ary.drop_while                  -> an_enumerator
    04461  *
    04462  *  Drops elements up to, but not including, the first element for
    04463  *  which the block returns +nil+ or +false+ and returns an array
    04464  *  containing the remaining elements.
    04465  *
    04466  *  If no block is given, an enumerator is returned instead.
    04467  *
    04468  *     a = [1, 2, 3, 4, 5, 0]
    04469  *     a.drop_while {|i| i < 3 }   #=> [3, 4, 5, 0]
    04470  *
    04471  */
    04472
    04473 static VALUE
    04474 rb_ary_drop_while(VALUE ary)
    04475 {
    04476     long i;
    04477
    04478     RETURN_ENUMERATOR(ary, 0, 0);
    04479     for (i = 0; i < RARRAY_LEN(ary); i++) {
    04480         if (!RTEST(rb_yield(RARRAY_PTR(ary)[i]))) break;
    04481     }
    04482     return rb_ary_drop(ary, LONG2FIX(i));
    04483 }
    04484
    04485
    04486
    04487 /* Arrays are ordered, integer-indexed collections of any object.
    04488  * Array indexing starts at 0, as in C or Java.  A negative index is
    04489  * assumed to be relative to the end of the array---that is, an index of -1
    04490  * indicates the last element of the array, -2 is the next to last
    04491  * element in the array, and so on.
    04492  */
    04493
    04494 void
    04495 Init_Array(void)
    04496 {
    04497 #undef rb_intern
    04498 #define rb_intern(str) rb_intern_const(str)
    04499
    04500     rb_cArray  = rb_define_class("Array", rb_cObject);
    04501     rb_include_module(rb_cArray, rb_mEnumerable);
    04502
    04503     rb_define_alloc_func(rb_cArray, ary_alloc);
    04504     rb_define_singleton_method(rb_cArray, "[]", rb_ary_s_create, -1);
    04505     rb_define_singleton_method(rb_cArray, "try_convert", rb_ary_s_try_convert, 1);
    04506     rb_define_method(rb_cArray, "initialize", rb_ary_initialize, -1);
    04507     rb_define_method(rb_cArray, "initialize_copy", rb_ary_replace, 1);
    04508
    04509     rb_define_method(rb_cArray, "inspect", rb_ary_inspect, 0);
    04510     rb_define_alias(rb_cArray,  "to_s", "inspect");
    04511     rb_define_method(rb_cArray, "to_a", rb_ary_to_a, 0);
    04512     rb_define_method(rb_cArray, "to_ary", rb_ary_to_ary_m, 0);
    04513     rb_define_method(rb_cArray, "frozen?",  rb_ary_frozen_p, 0);
    04514
    04515     rb_define_method(rb_cArray, "==", rb_ary_equal, 1);
    04516     rb_define_method(rb_cArray, "eql?", rb_ary_eql, 1);
    04517     rb_define_method(rb_cArray, "hash", rb_ary_hash, 0);
    04518
    04519     rb_define_method(rb_cArray, "[]", rb_ary_aref, -1);
    04520     rb_define_method(rb_cArray, "[]=", rb_ary_aset, -1);
    04521     rb_define_method(rb_cArray, "at", rb_ary_at, 1);
    04522     rb_define_method(rb_cArray, "fetch", rb_ary_fetch, -1);
    04523     rb_define_method(rb_cArray, "first", rb_ary_first, -1);
    04524     rb_define_method(rb_cArray, "last", rb_ary_last, -1);
    04525     rb_define_method(rb_cArray, "concat", rb_ary_concat, 1);
    04526     rb_define_method(rb_cArray, "<<", rb_ary_push, 1);
    04527     rb_define_method(rb_cArray, "push", rb_ary_push_m, -1);
    04528     rb_define_method(rb_cArray, "pop", rb_ary_pop_m, -1);
    04529     rb_define_method(rb_cArray, "shift", rb_ary_shift_m, -1);
    04530     rb_define_method(rb_cArray, "unshift", rb_ary_unshift_m, -1);
    04531     rb_define_method(rb_cArray, "insert", rb_ary_insert, -1);
    04532     rb_define_method(rb_cArray, "each", rb_ary_each, 0);
    04533     rb_define_method(rb_cArray, "each_index", rb_ary_each_index, 0);
    04534     rb_define_method(rb_cArray, "reverse_each", rb_ary_reverse_each, 0);
    04535     rb_define_method(rb_cArray, "length", rb_ary_length, 0);
    04536     rb_define_alias(rb_cArray,  "size", "length");
    04537     rb_define_method(rb_cArray, "empty?", rb_ary_empty_p, 0);
    04538     rb_define_method(rb_cArray, "find_index", rb_ary_index, -1);
    04539     rb_define_method(rb_cArray, "index", rb_ary_index, -1);
    04540     rb_define_method(rb_cArray, "rindex", rb_ary_rindex, -1);
    04541     rb_define_method(rb_cArray, "join", rb_ary_join_m, -1);
    04542     rb_define_method(rb_cArray, "reverse", rb_ary_reverse_m, 0);
    04543     rb_define_method(rb_cArray, "reverse!", rb_ary_reverse_bang, 0);
    04544     rb_define_method(rb_cArray, "rotate", rb_ary_rotate_m, -1);
    04545     rb_define_method(rb_cArray, "rotate!", rb_ary_rotate_bang, -1);
    04546     rb_define_method(rb_cArray, "sort", rb_ary_sort, 0);
    04547     rb_define_method(rb_cArray, "sort!", rb_ary_sort_bang, 0);
    04548     rb_define_method(rb_cArray, "sort_by!", rb_ary_sort_by_bang, 0);
    04549     rb_define_method(rb_cArray, "collect", rb_ary_collect, 0);
    04550     rb_define_method(rb_cArray, "collect!", rb_ary_collect_bang, 0);
    04551     rb_define_method(rb_cArray, "map", rb_ary_collect, 0);
    04552     rb_define_method(rb_cArray, "map!", rb_ary_collect_bang, 0);
    04553     rb_define_method(rb_cArray, "select", rb_ary_select, 0);
    04554     rb_define_method(rb_cArray, "select!", rb_ary_select_bang, 0);
    04555     rb_define_method(rb_cArray, "keep_if", rb_ary_keep_if, 0);
    04556     rb_define_method(rb_cArray, "values_at", rb_ary_values_at, -1);
    04557     rb_define_method(rb_cArray, "delete", rb_ary_delete, 1);
    04558     rb_define_method(rb_cArray, "delete_at", rb_ary_delete_at_m, 1);
    04559     rb_define_method(rb_cArray, "delete_if", rb_ary_delete_if, 0);
    04560     rb_define_method(rb_cArray, "reject", rb_ary_reject, 0);
    04561     rb_define_method(rb_cArray, "reject!", rb_ary_reject_bang, 0);
    04562     rb_define_method(rb_cArray, "zip", rb_ary_zip, -1);
    04563     rb_define_method(rb_cArray, "transpose", rb_ary_transpose, 0);
    04564     rb_define_method(rb_cArray, "replace", rb_ary_replace, 1);
    04565     rb_define_method(rb_cArray, "clear", rb_ary_clear, 0);
    04566     rb_define_method(rb_cArray, "fill", rb_ary_fill, -1);
    04567     rb_define_method(rb_cArray, "include?", rb_ary_includes, 1);
    04568     rb_define_method(rb_cArray, "<=>", rb_ary_cmp, 1);
    04569
    04570     rb_define_method(rb_cArray, "slice", rb_ary_aref, -1);
    04571     rb_define_method(rb_cArray, "slice!", rb_ary_slice_bang, -1);
    04572
    04573     rb_define_method(rb_cArray, "assoc", rb_ary_assoc, 1);
    04574     rb_define_method(rb_cArray, "rassoc", rb_ary_rassoc, 1);
    04575
    04576     rb_define_method(rb_cArray, "+", rb_ary_plus, 1);
    04577     rb_define_method(rb_cArray, "*", rb_ary_times, 1);
    04578
    04579     rb_define_method(rb_cArray, "-", rb_ary_diff, 1);
    04580     rb_define_method(rb_cArray, "&", rb_ary_and, 1);
    04581     rb_define_method(rb_cArray, "|", rb_ary_or, 1);
    04582
    04583     rb_define_method(rb_cArray, "uniq", rb_ary_uniq, 0);
    04584     rb_define_method(rb_cArray, "uniq!", rb_ary_uniq_bang, 0);
    04585     rb_define_method(rb_cArray, "compact", rb_ary_compact, 0);
    04586     rb_define_method(rb_cArray, "compact!", rb_ary_compact_bang, 0);
    04587     rb_define_method(rb_cArray, "flatten", rb_ary_flatten, -1);
    04588     rb_define_method(rb_cArray, "flatten!", rb_ary_flatten_bang, -1);
    04589     rb_define_method(rb_cArray, "count", rb_ary_count, -1);
    04590     rb_define_method(rb_cArray, "shuffle!", rb_ary_shuffle_bang, 0);
    04591     rb_define_method(rb_cArray, "shuffle", rb_ary_shuffle, 0);
    04592     rb_define_method(rb_cArray, "sample", rb_ary_sample, -1);
    04593     rb_define_method(rb_cArray, "cycle", rb_ary_cycle, -1);
    04594     rb_define_method(rb_cArray, "permutation", rb_ary_permutation, -1);
    04595     rb_define_method(rb_cArray, "combination", rb_ary_combination, 1);
    04596     rb_define_method(rb_cArray, "repeated_permutation", rb_ary_repeated_permutation, 1);
    04597     rb_define_method(rb_cArray, "repeated_combination", rb_ary_repeated_combination, 1);
    04598     rb_define_method(rb_cArray, "product", rb_ary_product, -1);
    04599
    04600     rb_define_method(rb_cArray, "take", rb_ary_take, 1);
    04601     rb_define_method(rb_cArray, "take_while", rb_ary_take_while, 0);
    04602     rb_define_method(rb_cArray, "drop", rb_ary_drop, 1);
    04603     rb_define_method(rb_cArray, "drop_while", rb_ary_drop_while, 0);
    04604
    04605     id_cmp = rb_intern("<=>");
    04606 }
    04607