summaryrefslogtreecommitdiff
path: root/enum.c
diff options
context:
space:
mode:
Diffstat (limited to 'enum.c')
-rw-r--r--enum.c2236
1 files changed, 1349 insertions, 887 deletions
diff --git a/enum.c b/enum.c
index d29e598a07..54ccf61c78 100644
--- a/enum.c
+++ b/enum.c
@@ -65,9 +65,9 @@ static VALUE
enum_yield(int argc, VALUE ary)
{
if (argc > 1)
- return rb_yield_force_blockarg(ary);
+ return rb_yield_force_blockarg(ary);
if (argc == 1)
- return rb_yield(ary);
+ return rb_yield(ary);
return rb_yield_values2(0, 0);
}
@@ -77,9 +77,9 @@ enum_yield_array(VALUE ary)
long len = RARRAY_LEN(ary);
if (len > 1)
- return rb_yield_force_blockarg(ary);
+ return rb_yield_force_blockarg(ary);
if (len == 1)
- return rb_yield(RARRAY_AREF(ary, 0));
+ return rb_yield(RARRAY_AREF(ary, 0));
return rb_yield_values2(0, 0);
}
@@ -90,7 +90,7 @@ grep_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
ENUM_WANT_SVALUE();
if (RTEST(rb_funcallv(memo->v1, id_eqq, 1, &i)) == RTEST(memo->u3.value)) {
- rb_ary_push(memo->v2, i);
+ rb_ary_push(memo->v2, i);
}
return Qnil;
}
@@ -106,7 +106,7 @@ grep_regexp_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
converted_element = SYMBOL_P(i) ? i : rb_check_string_type(i);
match = NIL_P(converted_element) ? Qfalse : rb_reg_match_p(memo->v1, i, 0);
if (match == memo->u3.value) {
- rb_ary_push(memo->v2, i);
+ rb_ary_push(memo->v2, i);
}
return Qnil;
}
@@ -118,7 +118,7 @@ grep_iter_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
ENUM_WANT_SVALUE();
if (RTEST(rb_funcallv(memo->v1, id_eqq, 1, &i)) == RTEST(memo->u3.value)) {
- rb_ary_push(memo->v2, enum_yield(argc, i));
+ rb_ary_push(memo->v2, enum_yield(argc, i));
}
return Qnil;
}
@@ -130,14 +130,14 @@ enum_grep0(VALUE obj, VALUE pat, VALUE test)
struct MEMO *memo = MEMO_NEW(pat, ary, test);
rb_block_call_func_t fn;
if (rb_block_given_p()) {
- fn = grep_iter_i;
+ fn = grep_iter_i;
}
else if (RB_TYPE_P(pat, T_REGEXP) &&
LIKELY(rb_method_basic_definition_p(CLASS_OF(pat), idEqq))) {
- fn = grep_regexp_i;
+ fn = grep_regexp_i;
}
else {
- fn = grep_i;
+ fn = grep_i;
}
rb_block_call(obj, id_each, 0, 0, fn, (VALUE)memo);
@@ -214,13 +214,13 @@ static void
imemo_count_up(struct MEMO *memo)
{
if (memo->flags & COUNT_BIGNUM) {
- MEMO_V3_SET(memo, rb_int_succ(memo->u3.value));
+ MEMO_V3_SET(memo, rb_int_succ(memo->u3.value));
}
else if (++memo->u3.cnt == 0) {
- /* overflow */
- unsigned long buf[2] = {0, 1};
- MEMO_V3_SET(memo, rb_big_unpack(buf, 2));
- memo->flags |= COUNT_BIGNUM;
+ /* overflow */
+ unsigned long buf[2] = {0, 1};
+ MEMO_V3_SET(memo, rb_big_unpack(buf, 2));
+ memo->flags |= COUNT_BIGNUM;
}
}
@@ -228,10 +228,10 @@ static VALUE
imemo_count_value(struct MEMO *memo)
{
if (memo->flags & COUNT_BIGNUM) {
- return memo->u3.value;
+ return memo->u3.value;
}
else {
- return ULONG2NUM(memo->u3.cnt);
+ return ULONG2NUM(memo->u3.cnt);
}
}
@@ -243,7 +243,7 @@ count_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, memop))
ENUM_WANT_SVALUE();
if (rb_equal(i, memo->v1)) {
- imemo_count_up(memo);
+ imemo_count_up(memo);
}
return Qnil;
}
@@ -254,7 +254,7 @@ count_iter_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, memop))
struct MEMO *memo = MEMO_CAST(memop);
if (RTEST(rb_yield_values2(argc, argv))) {
- imemo_count_up(memo);
+ imemo_count_up(memo);
}
return Qnil;
}
@@ -302,18 +302,18 @@ enum_count(int argc, VALUE *argv, VALUE obj)
rb_block_call_func *func;
if (argc == 0) {
- if (rb_block_given_p()) {
- func = count_iter_i;
- }
- else {
- func = count_all_i;
- }
+ if (rb_block_given_p()) {
+ func = count_iter_i;
+ }
+ else {
+ func = count_all_i;
+ }
}
else {
- rb_scan_args(argc, argv, "1", &item);
- if (rb_block_given_p()) {
- rb_warn("given block not used");
- }
+ rb_scan_args(argc, argv, "1", &item);
+ if (rb_block_given_p()) {
+ rb_warn("given block not used");
+ }
func = count_i;
}
@@ -328,10 +328,10 @@ find_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, memop))
ENUM_WANT_SVALUE();
if (RTEST(enum_yield(argc, i))) {
- struct MEMO *memo = MEMO_CAST(memop);
- MEMO_V1_SET(memo, i);
- memo->u3.cnt = 1;
- rb_iter_break();
+ struct MEMO *memo = MEMO_CAST(memop);
+ MEMO_V1_SET(memo, i);
+ memo->u3.cnt = 1;
+ rb_iter_break();
}
return Qnil;
}
@@ -354,7 +354,7 @@ find_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, memop))
* {foo: 0, bar: 1, baz: 2}.find {|key, value| key.start_with?('b') } # => [:bar, 1]
* {foo: 0, bar: 1, baz: 2}.find(proc {[]}) {|key, value| key.start_with?('c') } # => []
*
- * With no block given, returns an \Enumerator.
+ * With no block given, returns an Enumerator.
*
*/
static VALUE
@@ -368,10 +368,10 @@ enum_find(int argc, VALUE *argv, VALUE obj)
memo = MEMO_NEW(Qundef, 0, 0);
rb_block_call(obj, id_each, 0, 0, find_i, (VALUE)memo);
if (memo->u3.cnt) {
- return memo->v1;
+ return memo->v1;
}
if (!NIL_P(if_none)) {
- return rb_funcallv(if_none, id_call, 0, 0);
+ return rb_funcallv(if_none, id_call, 0, 0);
}
return Qnil;
}
@@ -384,8 +384,8 @@ find_index_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, memop))
ENUM_WANT_SVALUE();
if (rb_equal(i, memo->v2)) {
- MEMO_V1_SET(memo, imemo_count_value(memo));
- rb_iter_break();
+ MEMO_V1_SET(memo, imemo_count_value(memo));
+ rb_iter_break();
}
imemo_count_up(memo);
return Qnil;
@@ -397,8 +397,8 @@ find_index_iter_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, memop))
struct MEMO *memo = MEMO_CAST(memop);
if (RTEST(rb_yield_values2(argc, argv))) {
- MEMO_V1_SET(memo, imemo_count_value(memo));
- rb_iter_break();
+ MEMO_V1_SET(memo, imemo_count_value(memo));
+ rb_iter_break();
}
imemo_count_up(memo);
return Qnil;
@@ -424,7 +424,7 @@ find_index_iter_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, memop))
* ['a', 'b', 'c', 'b'].find_index {|element| element.start_with?('b') } # => 1
* {foo: 0, bar: 1, baz: 2}.find_index {|key, value| value > 1 } # => 2
*
- * With no argument and no block given, returns an \Enumerator.
+ * With no argument and no block given, returns an Enumerator.
*
*/
@@ -440,10 +440,10 @@ enum_find_index(int argc, VALUE *argv, VALUE obj)
func = find_index_iter_i;
}
else {
- rb_scan_args(argc, argv, "1", &condition_value);
- if (rb_block_given_p()) {
- rb_warn("given block not used");
- }
+ rb_scan_args(argc, argv, "1", &condition_value);
+ if (rb_block_given_p()) {
+ rb_warn("given block not used");
+ }
func = find_index_i;
}
@@ -458,7 +458,7 @@ find_all_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, ary))
ENUM_WANT_SVALUE();
if (RTEST(enum_yield(argc, i))) {
- rb_ary_push(ary, i);
+ rb_ary_push(ary, i);
}
return Qnil;
}
@@ -501,7 +501,7 @@ enum_size_over_p(VALUE obj, long n)
* a = {foo: 0, bar: 1, baz: 2}.select {|key, value| key.start_with?('b') }
* a # => {:bar=>1, :baz=>2}
*
- * With no block given, returns an \Enumerator.
+ * With no block given, returns an Enumerator.
*
* Related: #reject.
*/
@@ -543,7 +543,7 @@ filter_map_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, ary))
* (0..9).filter_map {|i| i * 2 if i.even? } # => [0, 4, 8, 12, 16]
* {foo: 0, bar: 1, baz: 2}.filter_map {|key, value| key if value.even? } # => [:foo, :baz]
*
- * When no block given, returns an \Enumerator.
+ * When no block given, returns an Enumerator.
*
*/
static VALUE
@@ -566,7 +566,7 @@ reject_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, ary))
ENUM_WANT_SVALUE();
if (!RTEST(enum_yield(argc, i))) {
- rb_ary_push(ary, i);
+ rb_ary_push(ary, i);
}
return Qnil;
}
@@ -584,7 +584,7 @@ reject_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, ary))
* (0..9).reject {|i| i * 2 if i.even? } # => [1, 3, 5, 7, 9]
* {foo: 0, bar: 1, baz: 2}.reject {|key, value| key if value.odd? } # => {:foo=>0, :baz=>2}
*
- * When no block given, returns an \Enumerator.
+ * When no block given, returns an Enumerator.
*
* Related: #select.
*/
@@ -631,7 +631,7 @@ collect_all(RB_BLOCK_CALL_FUNC_ARGLIST(i, ary))
* (0..4).map {|i| i*i } # => [0, 1, 4, 9, 16]
* {foo: 0, bar: 1, baz: 2}.map {|key, value| value*2} # => [0, 2, 4]
*
- * With no block given, returns an \Enumerator.
+ * With no block given, returns an Enumerator.
*
*/
static VALUE
@@ -658,10 +658,10 @@ flat_map_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, ary))
tmp = rb_check_array_type(i);
if (NIL_P(tmp)) {
- rb_ary_push(ary, i);
+ rb_ary_push(ary, i);
}
else {
- rb_ary_concat(ary, tmp);
+ rb_ary_concat(ary, tmp);
}
return Qnil;
}
@@ -681,7 +681,7 @@ flat_map_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, ary))
* [[0, 1], [2, 3]].flat_map {|e| e + [100] } # => [0, 1, 100, 2, 3, 100]
* {foo: 0, bar: 1, baz: 2}.flat_map {|key, value| [key, value] } # => [:foo, 0, :bar, 1, :baz, 2]
*
- * With no block given, returns an \Enumerator.
+ * With no block given, returns an Enumerator.
*
* Alias: #collect_concat.
*/
@@ -700,20 +700,19 @@ enum_flat_map(VALUE obj)
/*
* call-seq:
- * to_a -> array
+ * to_a(*args) -> array
*
* Returns an array containing the items in +self+:
*
* (0..4).to_a # => [0, 1, 2, 3, 4]
*
- * Enumerable#entries is an alias for Enumerable#to_a.
*/
static VALUE
enum_to_a(int argc, VALUE *argv, VALUE obj)
{
VALUE ary = rb_ary_new();
- rb_block_call(obj, id_each, argc, argv, collect_all, ary);
+ rb_block_call_kw(obj, id_each, argc, argv, collect_all, ary, RB_PASS_CALLED_KEYWORDS);
return ary;
}
@@ -746,8 +745,8 @@ enum_to_h_ii(RB_BLOCK_CALL_FUNC_ARGLIST(i, hash))
/*
* call-seq:
- * to_h -> hash
- * to_h {|element| ... } -> hash
+ * to_h(*args) -> hash
+ * to_h(*args) {|element| ... } -> hash
*
* When +self+ consists of 2-element arrays,
* returns a hash each of whose entries is the key-value pair
@@ -779,11 +778,11 @@ inject_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, p))
ENUM_WANT_SVALUE();
- if (memo->v1 == Qundef) {
- MEMO_V1_SET(memo, i);
+ if (UNDEF_P(memo->v1)) {
+ MEMO_V1_SET(memo, i);
}
else {
- MEMO_V1_SET(memo, rb_yield_values(2, memo->v1, i));
+ MEMO_V1_SET(memo, rb_yield_values(2, memo->v1, i));
}
return Qnil;
}
@@ -796,18 +795,18 @@ inject_op_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, p))
ENUM_WANT_SVALUE();
- if (memo->v1 == Qundef) {
- MEMO_V1_SET(memo, i);
+ if (UNDEF_P(memo->v1)) {
+ MEMO_V1_SET(memo, i);
}
else if (SYMBOL_P(name = memo->u3.value)) {
- const ID mid = SYM2ID(name);
- MEMO_V1_SET(memo, rb_funcallv_public(memo->v1, mid, 1, &i));
+ const ID mid = SYM2ID(name);
+ MEMO_V1_SET(memo, rb_funcallv_public(memo->v1, mid, 1, &i));
}
else {
- VALUE args[2];
- args[0] = name;
- args[1] = i;
- MEMO_V1_SET(memo, rb_f_send(numberof(args), args, memo->v1));
+ VALUE args[2];
+ args[0] = name;
+ args[1] = i;
+ MEMO_V1_SET(memo, rb_f_send(numberof(args), args, memo->v1));
}
return Qnil;
}
@@ -820,9 +819,9 @@ ary_inject_op(VALUE ary, VALUE init, VALUE op)
long i, n;
if (RARRAY_LEN(ary) == 0)
- return init == Qundef ? Qnil : init;
+ return UNDEF_P(init) ? Qnil : init;
- if (init == Qundef) {
+ if (UNDEF_P(init)) {
v = RARRAY_AREF(ary, 0);
i = 1;
if (RARRAY_LEN(ary) == 1)
@@ -835,9 +834,9 @@ ary_inject_op(VALUE ary, VALUE init, VALUE op)
id = SYM2ID(op);
if (id == idPLUS) {
- if (RB_INTEGER_TYPE_P(v) &&
- rb_method_basic_definition_p(rb_cInteger, idPLUS) &&
- rb_obj_respond_to(v, idPLUS, FALSE)) {
+ if (RB_INTEGER_TYPE_P(v) &&
+ rb_method_basic_definition_p(rb_cInteger, idPLUS) &&
+ rb_obj_respond_to(v, idPLUS, FALSE)) {
n = 0;
for (; i < RARRAY_LEN(ary); i++) {
e = RARRAY_AREF(ary, i);
@@ -1000,7 +999,6 @@ ary_inject_op(VALUE ary, VALUE init, VALUE op)
* "Memo: 3; element: 3"
* "Memo: 6; element: 4"
*
- * Enumerable#reduce is an alias for Enumerable#inject.
*
*/
static VALUE
@@ -1010,28 +1008,36 @@ enum_inject(int argc, VALUE *argv, VALUE obj)
VALUE init, op;
rb_block_call_func *iter = inject_i;
ID id;
+ int num_args;
- switch (rb_scan_args(argc, argv, "02", &init, &op)) {
+ if (rb_block_given_p()) {
+ num_args = rb_scan_args(argc, argv, "02", &init, &op);
+ }
+ else {
+ num_args = rb_scan_args(argc, argv, "11", &init, &op);
+ }
+
+ switch (num_args) {
case 0:
- init = Qundef;
- break;
+ init = Qundef;
+ break;
case 1:
- if (rb_block_given_p()) {
- break;
- }
- id = rb_check_id(&init);
- op = id ? ID2SYM(id) : init;
- init = Qundef;
- iter = inject_op_i;
- break;
+ if (rb_block_given_p()) {
+ break;
+ }
+ id = rb_check_id(&init);
+ op = id ? ID2SYM(id) : init;
+ init = Qundef;
+ iter = inject_op_i;
+ break;
case 2:
- if (rb_block_given_p()) {
- rb_warning("given block not used");
- }
- id = rb_check_id(&op);
- if (id) op = ID2SYM(id);
- iter = inject_op_i;
- break;
+ if (rb_block_given_p()) {
+ rb_warning("given block not used");
+ }
+ id = rb_check_id(&op);
+ if (id) op = ID2SYM(id);
+ iter = inject_op_i;
+ break;
}
if (iter == inject_op_i &&
@@ -1043,7 +1049,7 @@ enum_inject(int argc, VALUE *argv, VALUE obj)
memo = MEMO_NEW(init, Qnil, op);
rb_block_call(obj, id_each, 0, 0, iter, (VALUE)memo);
- if (memo->v1 == Qundef) return Qnil;
+ if (UNDEF_P(memo->v1)) return Qnil;
return memo->v1;
}
@@ -1055,10 +1061,10 @@ partition_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, arys))
ENUM_WANT_SVALUE();
if (RTEST(enum_yield(argc, i))) {
- ary = memo->v1;
+ ary = memo->v1;
}
else {
- ary = memo->v2;
+ ary = memo->v2;
}
rb_ary_push(ary, i);
return Qnil;
@@ -1116,11 +1122,11 @@ group_by_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, hash))
group = enum_yield(argc, i);
values = rb_hash_aref(hash, group);
if (!RB_TYPE_P(values, T_ARRAY)) {
- values = rb_ary_new3(1, i);
- rb_hash_aset(hash, group, values);
+ values = rb_ary_new3(1, i);
+ rb_hash_aset(hash, group, values);
}
else {
- rb_ary_push(values, i);
+ rb_ary_push(values, i);
}
return Qnil;
}
@@ -1224,7 +1230,7 @@ enum_tally(int argc, VALUE *argv, VALUE obj)
{
VALUE hash;
if (rb_check_arity(argc, 0, 1)) {
- hash = rb_convert_type(argv[0], T_HASH, "Hash", "to_hash");
+ hash = rb_to_hash_type(argv[0]);
rb_check_frozen(hash);
}
else {
@@ -1280,12 +1286,12 @@ enum_first(int argc, VALUE *argv, VALUE obj)
struct MEMO *memo;
rb_check_arity(argc, 0, 1);
if (argc > 0) {
- return enum_take(obj, argv[0]);
+ return enum_take(obj, argv[0]);
}
else {
- memo = MEMO_NEW(Qnil, 0, 0);
- rb_block_call(obj, id_each, 0, 0, first_i, (VALUE)memo);
- return memo->v1;
+ memo = MEMO_NEW(Qnil, 0, 0);
+ rb_block_call(obj, id_each, 0, 0, first_i, (VALUE)memo);
+ return memo->v1;
}
}
@@ -1298,7 +1304,7 @@ enum_first(int argc, VALUE *argv, VALUE obj)
* The ordering of equal elements is indeterminate and may be unstable.
*
* With no block given, the sort compares
- * using the items' own method <tt><=></tt>:
+ * using the elements' own method <tt><=></tt>:
*
* %w[b c a d].sort # => ["a", "b", "c", "d"]
* {foo: 0, bar: 1, baz: 2}.sort # => [[:bar, 1], [:baz, 2], [:foo, 0]]
@@ -1328,10 +1334,12 @@ enum_sort(VALUE obj)
}
#define SORT_BY_BUFSIZE 16
+#define SORT_BY_UNIFORMED(num, flo, fix) (((num&1)<<2)|((flo&1)<<1)|fix)
struct sort_by_data {
const VALUE ary;
const VALUE buf;
- long n;
+ uint8_t n;
+ uint8_t primitive_uniformed;
};
static VALUE
@@ -1346,18 +1354,23 @@ sort_by_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, _data))
v = enum_yield(argc, i);
if (RBASIC(ary)->klass) {
- rb_raise(rb_eRuntimeError, "sort_by reentered");
+ rb_raise(rb_eRuntimeError, "sort_by reentered");
}
if (RARRAY_LEN(data->buf) != SORT_BY_BUFSIZE*2) {
- rb_raise(rb_eRuntimeError, "sort_by reentered");
+ rb_raise(rb_eRuntimeError, "sort_by reentered");
}
+ if (data->primitive_uniformed) {
+ data->primitive_uniformed &= SORT_BY_UNIFORMED((FIXNUM_P(v)) || (RB_FLOAT_TYPE_P(v)),
+ RB_FLOAT_TYPE_P(v),
+ FIXNUM_P(v));
+ }
RARRAY_ASET(data->buf, data->n*2, v);
RARRAY_ASET(data->buf, data->n*2+1, i);
data->n++;
if (data->n == SORT_BY_BUFSIZE) {
- rb_ary_concat(ary, data->buf);
- data->n = 0;
+ rb_ary_concat(ary, data->buf);
+ data->n = 0;
}
return Qnil;
}
@@ -1365,21 +1378,193 @@ sort_by_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, _data))
static int
sort_by_cmp(const void *ap, const void *bp, void *data)
{
- struct cmp_opt_data cmp_opt = { 0, 0 };
VALUE a;
VALUE b;
VALUE ary = (VALUE)data;
if (RBASIC(ary)->klass) {
- rb_raise(rb_eRuntimeError, "sort_by reentered");
+ rb_raise(rb_eRuntimeError, "sort_by reentered");
}
a = *(VALUE *)ap;
b = *(VALUE *)bp;
- return OPTIMIZED_CMP(a, b, cmp_opt);
+ return OPTIMIZED_CMP(a, b);
}
+
+/*
+ This is parts of uniform sort
+*/
+
+#define uless rb_uniform_is_less
+#define UNIFORM_SWAP(a,b)\
+ do{struct rb_uniform_sort_data tmp = a; a = b; b = tmp;} while(0)
+
+struct rb_uniform_sort_data {
+ VALUE v;
+ VALUE i;
+};
+
+static inline bool
+rb_uniform_is_less(VALUE a, VALUE b)
+{
+
+ if (FIXNUM_P(a) && FIXNUM_P(b)) {
+ return (SIGNED_VALUE)a < (SIGNED_VALUE)b;
+ }
+ else if (FIXNUM_P(a)) {
+ RUBY_ASSERT(RB_FLOAT_TYPE_P(b));
+ return rb_float_cmp(b, a) > 0;
+ }
+ else {
+ RUBY_ASSERT(RB_FLOAT_TYPE_P(a));
+ return rb_float_cmp(a, b) < 0;
+ }
+}
+
+static inline bool
+rb_uniform_is_larger(VALUE a, VALUE b)
+{
+
+ if (FIXNUM_P(a) && FIXNUM_P(b)) {
+ return (SIGNED_VALUE)a > (SIGNED_VALUE)b;
+ }
+ else if (FIXNUM_P(a)) {
+ RUBY_ASSERT(RB_FLOAT_TYPE_P(b));
+ return rb_float_cmp(b, a) < 0;
+ }
+ else {
+ RUBY_ASSERT(RB_FLOAT_TYPE_P(a));
+ return rb_float_cmp(a, b) > 0;
+ }
+}
+
+#define med3_val(a,b,c) (uless(a,b)?(uless(b,c)?b:uless(c,a)?a:c):(uless(c,b)?b:uless(a,c)?a:c))
+
+static void
+rb_uniform_insertionsort_2(struct rb_uniform_sort_data* ptr_begin,
+ struct rb_uniform_sort_data* ptr_end)
+{
+ if ((ptr_end - ptr_begin) < 2) return;
+ struct rb_uniform_sort_data tmp, *j, *k,
+ *index = ptr_begin+1;
+ for (; index < ptr_end; index++) {
+ tmp = *index;
+ j = k = index;
+ if (uless(tmp.v, ptr_begin->v)) {
+ while (ptr_begin < j) {
+ *j = *(--k);
+ j = k;
+ }
+ }
+ else {
+ while (uless(tmp.v, (--k)->v)) {
+ *j = *k;
+ j = k;
+ }
+ }
+ *j = tmp;
+ }
+}
+
+static inline void
+rb_uniform_heap_down_2(struct rb_uniform_sort_data* ptr_begin,
+ size_t offset, size_t len)
+{
+ size_t c;
+ struct rb_uniform_sort_data tmp = ptr_begin[offset];
+ while ((c = (offset<<1)+1) <= len) {
+ if (c < len && uless(ptr_begin[c].v, ptr_begin[c+1].v)) {
+ c++;
+ }
+ if (!uless(tmp.v, ptr_begin[c].v)) break;
+ ptr_begin[offset] = ptr_begin[c];
+ offset = c;
+ }
+ ptr_begin[offset] = tmp;
+}
+
+static void
+rb_uniform_heapsort_2(struct rb_uniform_sort_data* ptr_begin,
+ struct rb_uniform_sort_data* ptr_end)
+{
+ size_t n = ptr_end - ptr_begin;
+ if (n < 2) return;
+
+ for (size_t offset = n>>1; offset > 0;) {
+ rb_uniform_heap_down_2(ptr_begin, --offset, n-1);
+ }
+ for (size_t offset = n-1; offset > 0;) {
+ UNIFORM_SWAP(*ptr_begin, ptr_begin[offset]);
+ rb_uniform_heap_down_2(ptr_begin, 0, --offset);
+ }
+}
+
+
+static void
+rb_uniform_quicksort_intro_2(struct rb_uniform_sort_data* ptr_begin,
+ struct rb_uniform_sort_data* ptr_end, size_t d)
+{
+
+ if (ptr_end - ptr_begin <= 16) {
+ rb_uniform_insertionsort_2(ptr_begin, ptr_end);
+ return;
+ }
+ if (d == 0) {
+ rb_uniform_heapsort_2(ptr_begin, ptr_end);
+ return;
+ }
+
+ VALUE x = med3_val(ptr_begin->v,
+ ptr_begin[(ptr_end - ptr_begin)>>1].v,
+ ptr_end[-1].v);
+ struct rb_uniform_sort_data *i = ptr_begin;
+ struct rb_uniform_sort_data *j = ptr_end-1;
+
+ do {
+ while (uless(i->v, x)) i++;
+ while (uless(x, j->v)) j--;
+ if (i <= j) {
+ UNIFORM_SWAP(*i, *j);
+ i++;
+ j--;
+ }
+ } while (i <= j);
+ j++;
+ if (ptr_end - j > 1) rb_uniform_quicksort_intro_2(j, ptr_end, d-1);
+ if (i - ptr_begin > 1) rb_uniform_quicksort_intro_2(ptr_begin, i, d-1);
+}
+
+/**
+ * Direct primitive data compare sort. Implement with intro sort.
+ * @param[in] ptr_begin The begin address of target rb_ary's raw pointer.
+ * @param[in] ptr_end The end address of target rb_ary's raw pointer.
+**/
+static void
+rb_uniform_intro_sort_2(struct rb_uniform_sort_data* ptr_begin,
+ struct rb_uniform_sort_data* ptr_end)
+{
+ size_t n = ptr_end - ptr_begin;
+ size_t d = CHAR_BIT * sizeof(n) - nlz_intptr(n) - 1;
+ bool sorted_flag = true;
+
+ for (struct rb_uniform_sort_data* ptr = ptr_begin+1; ptr < ptr_end; ptr++) {
+ if (rb_uniform_is_larger((ptr-1)->v, (ptr)->v)) {
+ sorted_flag = false;
+ break;
+ }
+ }
+
+ if (sorted_flag) {
+ return;
+ }
+ rb_uniform_quicksort_intro_2(ptr_begin, ptr_end, d<<1);
+}
+
+#undef uless
+
+
/*
* call-seq:
* sort_by {|element| ... } -> array
@@ -1473,36 +1658,46 @@ enum_sort_by(VALUE obj)
RETURN_SIZED_ENUMERATOR(obj, 0, 0, enum_size);
if (RB_TYPE_P(obj, T_ARRAY) && RARRAY_LEN(obj) <= LONG_MAX/2) {
- ary = rb_ary_new2(RARRAY_LEN(obj)*2);
+ ary = rb_ary_new2(RARRAY_LEN(obj)*2);
}
else {
- ary = rb_ary_new();
+ ary = rb_ary_new();
}
RBASIC_CLEAR_CLASS(ary);
- buf = rb_ary_tmp_new(SORT_BY_BUFSIZE*2);
+ buf = rb_ary_hidden_new(SORT_BY_BUFSIZE*2);
rb_ary_store(buf, SORT_BY_BUFSIZE*2-1, Qnil);
memo = MEMO_NEW(0, 0, 0);
data = (struct sort_by_data *)&memo->v1;
RB_OBJ_WRITE(memo, &data->ary, ary);
RB_OBJ_WRITE(memo, &data->buf, buf);
data->n = 0;
+ data->primitive_uniformed = SORT_BY_UNIFORMED((CMP_OPTIMIZABLE(FLOAT) && CMP_OPTIMIZABLE(INTEGER)),
+ CMP_OPTIMIZABLE(FLOAT),
+ CMP_OPTIMIZABLE(INTEGER));
rb_block_call(obj, id_each, 0, 0, sort_by_i, (VALUE)memo);
ary = data->ary;
buf = data->buf;
if (data->n) {
- rb_ary_resize(buf, data->n*2);
- rb_ary_concat(ary, buf);
+ rb_ary_resize(buf, data->n*2);
+ rb_ary_concat(ary, buf);
}
if (RARRAY_LEN(ary) > 2) {
- RARRAY_PTR_USE(ary, ptr,
- ruby_qsort(ptr, RARRAY_LEN(ary)/2, 2*sizeof(VALUE),
- sort_by_cmp, (void *)ary));
+ if (data->primitive_uniformed) {
+ RARRAY_PTR_USE(ary, ptr,
+ rb_uniform_intro_sort_2((struct rb_uniform_sort_data*)ptr,
+ (struct rb_uniform_sort_data*)(ptr + RARRAY_LEN(ary))));
+ }
+ else {
+ RARRAY_PTR_USE(ary, ptr,
+ ruby_qsort(ptr, RARRAY_LEN(ary)/2, 2*sizeof(VALUE),
+ sort_by_cmp, (void *)ary));
+ }
}
if (RBASIC(ary)->klass) {
- rb_raise(rb_eRuntimeError, "sort_by reentered");
+ rb_raise(rb_eRuntimeError, "sort_by reentered");
}
for (i=1; i<RARRAY_LEN(ary); i+=2) {
- RARRAY_ASET(ary, i/2, RARRAY_AREF(ary, i));
+ RARRAY_ASET(ary, i/2, RARRAY_AREF(ary, i));
}
rb_ary_resize(ary, RARRAY_LEN(ary)/2);
RBASIC_SET_CLASS_RAW(ary, rb_cArray);
@@ -1548,8 +1743,8 @@ enum_##name##_func(VALUE result, struct MEMO *memo)
DEFINE_ENUMFUNCS(all)
{
if (!RTEST(result)) {
- MEMO_V1_SET(memo, Qfalse);
- rb_iter_break();
+ MEMO_V1_SET(memo, Qfalse);
+ rb_iter_break();
}
return Qnil;
}
@@ -1562,6 +1757,9 @@ DEFINE_ENUMFUNCS(all)
*
* Returns whether every element meets a given criterion.
*
+ * If +self+ has no element, returns +true+ and argument or block
+ * are not used.
+ *
* With no argument and no block,
* returns whether every element is truthy:
*
@@ -1593,7 +1791,7 @@ DEFINE_ENUMFUNCS(all)
* {foo: 0, bar: 1, baz: 2}.all? {|key, value| value < 3 } # => true
* {foo: 0, bar: 1, baz: 2}.all? {|key, value| value < 2 } # => false
*
- * Related: #any?.
+ * Related: #any?, #none? #one?.
*
*/
@@ -1609,8 +1807,8 @@ enum_all(int argc, VALUE *argv, VALUE obj)
DEFINE_ENUMFUNCS(any)
{
if (RTEST(result)) {
- MEMO_V1_SET(memo, Qtrue);
- rb_iter_break();
+ MEMO_V1_SET(memo, Qtrue);
+ rb_iter_break();
}
return Qnil;
}
@@ -1623,6 +1821,9 @@ DEFINE_ENUMFUNCS(any)
*
* Returns whether any element meets a given criterion.
*
+ * If +self+ has no element, returns +false+ and argument or block
+ * are not used.
+ *
* With no argument and no block,
* returns whether any element is truthy:
*
@@ -1653,8 +1854,7 @@ DEFINE_ENUMFUNCS(any)
* {foo: 0, bar: 1, baz: 2}.any? {|key, value| value < 1 } # => true
* {foo: 0, bar: 1, baz: 2}.any? {|key, value| value < 0 } # => false
*
- *
- * Related: #all?
+ * Related: #all?, #none?, #one?.
*/
static VALUE
@@ -1669,13 +1869,13 @@ enum_any(int argc, VALUE *argv, VALUE obj)
DEFINE_ENUMFUNCS(one)
{
if (RTEST(result)) {
- if (memo->v1 == Qundef) {
- MEMO_V1_SET(memo, Qtrue);
- }
- else if (memo->v1 == Qtrue) {
- MEMO_V1_SET(memo, Qfalse);
- rb_iter_break();
- }
+ if (UNDEF_P(memo->v1)) {
+ MEMO_V1_SET(memo, Qtrue);
+ }
+ else if (memo->v1 == Qtrue) {
+ MEMO_V1_SET(memo, Qfalse);
+ rb_iter_break();
+ }
}
return Qnil;
}
@@ -1705,11 +1905,10 @@ cmpint_reenter_check(struct nmin_data *data, VALUE val)
static int
nmin_cmp(const void *ap, const void *bp, void *_data)
{
- struct cmp_opt_data cmp_opt = { 0, 0 };
struct nmin_data *data = (struct nmin_data *)_data;
VALUE a = *(const VALUE *)ap, b = *(const VALUE *)bp;
#define rb_cmpint(cmp, a, b) rb_cmpint(cmpint_reenter_check(data, (cmp)), a, b)
- return OPTIMIZED_CMP(a, b, cmp_opt);
+ return OPTIMIZED_CMP(a, b);
#undef rb_cmpint
}
@@ -1737,7 +1936,7 @@ nmin_filter(struct nmin_data *data)
long i, j;
if (data->curlen <= data->n)
- return;
+ return;
n = data->n;
beg = RARRAY_PTR(data->buf);
@@ -1757,46 +1956,46 @@ nmin_filter(struct nmin_data *data)
} while (0)
while (1) {
- long pivot_index = left + (right-left)/2;
- long num_pivots = 1;
-
- SWAP(pivot_index, right);
- pivot_index = right;
-
- store_index = left;
- i = left;
- while (i <= right-num_pivots) {
- int c = data->cmpfunc(GETPTR(i), GETPTR(pivot_index), data);
- if (data->rev)
- c = -c;
- if (c == 0) {
- SWAP(i, right-num_pivots);
- num_pivots++;
- continue;
- }
- if (c < 0) {
- SWAP(i, store_index);
- store_index++;
- }
- i++;
- }
- j = store_index;
- for (i = right; right-num_pivots < i; i--) {
- if (i <= j)
- break;
- SWAP(j, i);
- j++;
- }
-
- if (store_index <= n && n <= store_index+num_pivots)
- break;
-
- if (n < store_index) {
- right = store_index-1;
- }
- else {
- left = store_index+num_pivots;
- }
+ long pivot_index = left + (right-left)/2;
+ long num_pivots = 1;
+
+ SWAP(pivot_index, right);
+ pivot_index = right;
+
+ store_index = left;
+ i = left;
+ while (i <= right-num_pivots) {
+ int c = data->cmpfunc(GETPTR(i), GETPTR(pivot_index), data);
+ if (data->rev)
+ c = -c;
+ if (c == 0) {
+ SWAP(i, right-num_pivots);
+ num_pivots++;
+ continue;
+ }
+ if (c < 0) {
+ SWAP(i, store_index);
+ store_index++;
+ }
+ i++;
+ }
+ j = store_index;
+ for (i = right; right-num_pivots < i; i--) {
+ if (i <= j)
+ break;
+ SWAP(j, i);
+ j++;
+ }
+
+ if (store_index <= n && n <= store_index+num_pivots)
+ break;
+
+ if (n < store_index) {
+ right = store_index-1;
+ }
+ else {
+ left = store_index+num_pivots;
+ }
}
#undef GETPTR
#undef SWAP
@@ -1815,11 +2014,11 @@ nmin_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, _data))
ENUM_WANT_SVALUE();
if (data->by)
- cmpv = enum_yield(argc, i);
+ cmpv = enum_yield(argc, i);
else
- cmpv = i;
+ cmpv = i;
- if (data->limit != Qundef) {
+ if (!UNDEF_P(data->limit)) {
int c = data->cmpfunc(&cmpv, &data->limit, data);
if (data->rev)
c = -c;
@@ -1828,13 +2027,13 @@ nmin_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, _data))
}
if (data->by)
- rb_ary_push(data->buf, cmpv);
+ rb_ary_push(data->buf, cmpv);
rb_ary_push(data->buf, i);
data->curlen++;
if (data->curlen == data->bufmax) {
- nmin_filter(data);
+ nmin_filter(data);
}
return Qnil;
@@ -1855,28 +2054,28 @@ rb_nmin_run(VALUE obj, VALUE num, int by, int rev, int ary)
rb_raise(rb_eArgError, "too big size");
data.bufmax = data.n * 4;
data.curlen = 0;
- data.buf = rb_ary_tmp_new(data.bufmax * (by ? 2 : 1));
+ data.buf = rb_ary_hidden_new(data.bufmax * (by ? 2 : 1));
data.limit = Qundef;
data.cmpfunc = by ? nmin_cmp :
rb_block_given_p() ? nmin_block_cmp :
- nmin_cmp;
+ nmin_cmp;
data.rev = rev;
data.by = by;
if (ary) {
- long i;
- for (i = 0; i < RARRAY_LEN(obj); i++) {
- VALUE args[1];
- args[0] = RARRAY_AREF(obj, i);
+ long i;
+ for (i = 0; i < RARRAY_LEN(obj); i++) {
+ VALUE args[1];
+ args[0] = RARRAY_AREF(obj, i);
nmin_i(obj, (VALUE)&data, 1, args, Qundef);
- }
+ }
}
else {
- rb_block_call(obj, id_each, 0, 0, nmin_i, (VALUE)&data);
+ rb_block_call(obj, id_each, 0, 0, nmin_i, (VALUE)&data);
}
nmin_filter(&data);
result = data.buf;
if (by) {
- long i;
+ long i;
RARRAY_PTR_USE(result, ptr, {
ruby_qsort(ptr,
RARRAY_LEN(result)/2,
@@ -1886,7 +2085,7 @@ rb_nmin_run(VALUE obj, VALUE num, int by, int rev, int ary)
ptr[i/2] = ptr[i];
}
});
- rb_ary_resize(result, RARRAY_LEN(result)/2);
+ rb_ary_resize(result, RARRAY_LEN(result)/2);
}
else {
RARRAY_PTR_USE(result, ptr, {
@@ -1904,26 +2103,45 @@ rb_nmin_run(VALUE obj, VALUE num, int by, int rev, int ary)
/*
* call-seq:
- * enum.one? [{ |obj| block }] -> true or false
- * enum.one?(pattern) -> true or false
- *
- * Passes each element of the collection to the given block. The method
- * returns <code>true</code> if the block returns <code>true</code>
- * exactly once. If the block is not given, <code>one?</code> will return
- * <code>true</code> only if exactly one of the collection members is
- * true.
- *
- * If instead a pattern is supplied, the method returns whether
- * <code>pattern === element</code> for exactly one collection member.
- *
- * %w{ant bear cat}.one? { |word| word.length == 4 } #=> true
- * %w{ant bear cat}.one? { |word| word.length > 4 } #=> false
- * %w{ant bear cat}.one? { |word| word.length < 4 } #=> false
- * %w{ant bear cat}.one?(/t/) #=> false
- * [ nil, true, 99 ].one? #=> false
- * [ nil, true, false ].one? #=> true
- * [ nil, true, 99 ].one?(Integer) #=> true
- * [].one? #=> false
+ * one? -> true or false
+ * one?(pattern) -> true or false
+ * one? {|element| ... } -> true or false
+ *
+ * Returns whether exactly one element meets a given criterion.
+ *
+ * With no argument and no block,
+ * returns whether exactly one element is truthy:
+ *
+ * (1..1).one? # => true
+ * [1, nil, false].one? # => true
+ * (1..4).one? # => false
+ * {foo: 0}.one? # => true
+ * {foo: 0, bar: 1}.one? # => false
+ * [].one? # => false
+ *
+ * With argument +pattern+ and no block,
+ * returns whether for exactly one element +element+,
+ * <tt>pattern === element</tt>:
+ *
+ * [nil, false, 0].one?(Integer) # => true
+ * [nil, false, 0].one?(Numeric) # => true
+ * [nil, false, 0].one?(Float) # => false
+ * %w[bar baz bat bam].one?(/m/) # => true
+ * %w[bar baz bat bam].one?(/foo/) # => false
+ * %w[bar baz bat bam].one?('ba') # => false
+ * {foo: 0, bar: 1, baz: 2}.one?(Array) # => false
+ * {foo: 0}.one?(Array) # => true
+ * [].one?(Integer) # => false
+ *
+ * With a block given, returns whether the block returns a truthy value
+ * for exactly one element:
+ *
+ * (1..4).one? {|element| element < 2 } # => true
+ * (1..4).one? {|element| element < 1 } # => false
+ * {foo: 0, bar: 1, baz: 2}.one? {|key, value| value < 1 } # => true
+ * {foo: 0, bar: 1, baz: 2}.one? {|key, value| value < 2 } # => false
+ *
+ * Related: #none?, #all?, #any?.
*
*/
static VALUE
@@ -1935,40 +2153,58 @@ enum_one(int argc, VALUE *argv, VALUE obj)
WARN_UNUSED_BLOCK(argc);
rb_block_call(obj, id_each, 0, 0, ENUMFUNC(one), (VALUE)memo);
result = memo->v1;
- if (result == Qundef) return Qfalse;
+ if (UNDEF_P(result)) return Qfalse;
return result;
}
DEFINE_ENUMFUNCS(none)
{
if (RTEST(result)) {
- MEMO_V1_SET(memo, Qfalse);
- rb_iter_break();
+ MEMO_V1_SET(memo, Qfalse);
+ rb_iter_break();
}
return Qnil;
}
/*
* call-seq:
- * enum.none? [{ |obj| block }] -> true or false
- * enum.none?(pattern) -> true or false
- *
- * Passes each element of the collection to the given block. The method
- * returns <code>true</code> if the block never returns <code>true</code>
- * for all elements. If the block is not given, <code>none?</code> will return
- * <code>true</code> only if none of the collection members is true.
- *
- * If instead a pattern is supplied, the method returns whether
- * <code>pattern === element</code> for none of the collection members.
- *
- * %w{ant bear cat}.none? { |word| word.length == 5 } #=> true
- * %w{ant bear cat}.none? { |word| word.length >= 4 } #=> false
- * %w{ant bear cat}.none?(/d/) #=> true
- * [1, 3.14, 42].none?(Float) #=> false
- * [].none? #=> true
- * [nil].none? #=> true
- * [nil, false].none? #=> true
- * [nil, false, true].none? #=> false
+ * none? -> true or false
+ * none?(pattern) -> true or false
+ * none? {|element| ... } -> true or false
+ *
+ * Returns whether no element meets a given criterion.
+ *
+ * With no argument and no block,
+ * returns whether no element is truthy:
+ *
+ * (1..4).none? # => false
+ * [nil, false].none? # => true
+ * {foo: 0}.none? # => false
+ * {foo: 0, bar: 1}.none? # => false
+ * [].none? # => true
+ *
+ * With argument +pattern+ and no block,
+ * returns whether for no element +element+,
+ * <tt>pattern === element</tt>:
+ *
+ * [nil, false, 1.1].none?(Integer) # => true
+ * %w[bar baz bat bam].none?(/m/) # => false
+ * %w[bar baz bat bam].none?(/foo/) # => true
+ * %w[bar baz bat bam].none?('ba') # => true
+ * {foo: 0, bar: 1, baz: 2}.none?(Hash) # => true
+ * {foo: 0}.none?(Array) # => false
+ * [].none?(Integer) # => true
+ *
+ * With a block given, returns whether the block returns a truthy value
+ * for no element:
+ *
+ * (1..4).none? {|element| element < 1 } # => true
+ * (1..4).none? {|element| element < 2 } # => false
+ * {foo: 0, bar: 1, baz: 2}.none? {|key, value| value < 0 } # => true
+ * {foo: 0, bar: 1, baz: 2}.none? {|key, value| value < 1 } # => false
+ *
+ * Related: #one?, #all?, #any?.
+ *
*/
static VALUE
enum_none(int argc, VALUE *argv, VALUE obj)
@@ -1982,7 +2218,6 @@ enum_none(int argc, VALUE *argv, VALUE obj)
struct min_t {
VALUE min;
- struct cmp_opt_data cmp_opt;
};
static VALUE
@@ -1992,13 +2227,13 @@ min_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
ENUM_WANT_SVALUE();
- if (memo->min == Qundef) {
- memo->min = i;
+ if (UNDEF_P(memo->min)) {
+ memo->min = i;
}
else {
- if (OPTIMIZED_CMP(i, memo->min, memo->cmp_opt) < 0) {
- memo->min = i;
- }
+ if (OPTIMIZED_CMP(i, memo->min) < 0) {
+ memo->min = i;
+ }
}
return Qnil;
}
@@ -2011,14 +2246,14 @@ min_ii(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
ENUM_WANT_SVALUE();
- if (memo->min == Qundef) {
- memo->min = i;
+ if (UNDEF_P(memo->min)) {
+ memo->min = i;
}
else {
- cmp = rb_yield_values(2, i, memo->min);
- if (rb_cmpint(cmp, i, memo->min) < 0) {
- memo->min = i;
- }
+ cmp = rb_yield_values(2, i, memo->min);
+ if (rb_cmpint(cmp, i, memo->min) < 0) {
+ memo->min = i;
+ }
}
return Qnil;
}
@@ -2026,33 +2261,66 @@ min_ii(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
/*
* call-seq:
- * enum.min -> obj
- * enum.min { |a, b| block } -> obj
- * enum.min(n) -> array
- * enum.min(n) { |a, b| block } -> array
- *
- * Returns the object in _enum_ with the minimum value. The
- * first form assumes all objects implement <code><=></code>;
- * the second uses the block to return <em>a <=> b</em>.
- *
- * a = %w(albatross dog horse)
- * a.min #=> "albatross"
- * a.min { |a, b| a.length <=> b.length } #=> "dog"
- *
- * If the +n+ argument is given, minimum +n+ elements are returned
- * as a sorted array.
- *
- * a = %w[albatross dog horse]
- * a.min(2) #=> ["albatross", "dog"]
- * a.min(2) {|a, b| a.length <=> b.length } #=> ["dog", "horse"]
- * [5, 1, 3, 4, 2].min(3) #=> [1, 2, 3]
+ * min -> element
+ * min(n) -> array
+ * min {|a, b| ... } -> element
+ * min(n) {|a, b| ... } -> array
+ *
+ * Returns the element with the minimum element according to a given criterion.
+ * The ordering of equal elements is indeterminate and may be unstable.
+ *
+ * With no argument and no block, returns the minimum element,
+ * using the elements' own method <tt><=></tt> for comparison:
+ *
+ * (1..4).min # => 1
+ * (-4..-1).min # => -4
+ * %w[d c b a].min # => "a"
+ * {foo: 0, bar: 1, baz: 2}.min # => [:bar, 1]
+ * [].min # => nil
+ *
+ * With positive integer argument +n+ given, and no block,
+ * returns an array containing the first +n+ minimum elements that exist:
+ *
+ * (1..4).min(2) # => [1, 2]
+ * (-4..-1).min(2) # => [-4, -3]
+ * %w[d c b a].min(2) # => ["a", "b"]
+ * {foo: 0, bar: 1, baz: 2}.min(2) # => [[:bar, 1], [:baz, 2]]
+ * [].min(2) # => []
+ *
+ * With a block given, the block determines the minimum elements.
+ * The block is called with two elements +a+ and +b+, and must return:
+ *
+ * - A negative integer if <tt>a < b</tt>.
+ * - Zero if <tt>a == b</tt>.
+ * - A positive integer if <tt>a > b</tt>.
+ *
+ * With a block given and no argument,
+ * returns the minimum element as determined by the block:
+ *
+ * %w[xxx x xxxx xx].min {|a, b| a.size <=> b.size } # => "x"
+ * h = {foo: 0, bar: 1, baz: 2}
+ * h.min {|pair1, pair2| pair1[1] <=> pair2[1] } # => [:foo, 0]
+ * [].min {|a, b| a <=> b } # => nil
+ *
+ * With a block given and positive integer argument +n+ given,
+ * returns an array containing the first +n+ minimum elements that exist,
+ * as determined by the block.
+ *
+ * %w[xxx x xxxx xx].min(2) {|a, b| a.size <=> b.size } # => ["x", "xx"]
+ * h = {foo: 0, bar: 1, baz: 2}
+ * h.min(2) {|pair1, pair2| pair1[1] <=> pair2[1] }
+ * # => [[:foo, 0], [:bar, 1]]
+ * [].min(2) {|a, b| a <=> b } # => []
+ *
+ * Related: #min_by, #minmax, #max.
+ *
*/
static VALUE
enum_min(int argc, VALUE *argv, VALUE obj)
{
VALUE memo;
- struct min_t *m = NEW_CMP_OPT_MEMO(struct min_t, memo);
+ struct min_t *m = NEW_MEMO_FOR(struct min_t, memo);
VALUE result;
VALUE num;
@@ -2060,22 +2328,19 @@ enum_min(int argc, VALUE *argv, VALUE obj)
return rb_nmin_run(obj, num, 0, 0, 0);
m->min = Qundef;
- m->cmp_opt.opt_methods = 0;
- m->cmp_opt.opt_inited = 0;
if (rb_block_given_p()) {
- rb_block_call(obj, id_each, 0, 0, min_ii, memo);
+ rb_block_call(obj, id_each, 0, 0, min_ii, memo);
}
else {
- rb_block_call(obj, id_each, 0, 0, min_i, memo);
+ rb_block_call(obj, id_each, 0, 0, min_i, memo);
}
result = m->min;
- if (result == Qundef) return Qnil;
+ if (UNDEF_P(result)) return Qnil;
return result;
}
struct max_t {
VALUE max;
- struct cmp_opt_data cmp_opt;
};
static VALUE
@@ -2085,13 +2350,13 @@ max_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
ENUM_WANT_SVALUE();
- if (memo->max == Qundef) {
- memo->max = i;
+ if (UNDEF_P(memo->max)) {
+ memo->max = i;
}
else {
- if (OPTIMIZED_CMP(i, memo->max, memo->cmp_opt) > 0) {
- memo->max = i;
- }
+ if (OPTIMIZED_CMP(i, memo->max) > 0) {
+ memo->max = i;
+ }
}
return Qnil;
}
@@ -2104,47 +2369,80 @@ max_ii(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
ENUM_WANT_SVALUE();
- if (memo->max == Qundef) {
- memo->max = i;
+ if (UNDEF_P(memo->max)) {
+ memo->max = i;
}
else {
- cmp = rb_yield_values(2, i, memo->max);
- if (rb_cmpint(cmp, i, memo->max) > 0) {
- memo->max = i;
- }
+ cmp = rb_yield_values(2, i, memo->max);
+ if (rb_cmpint(cmp, i, memo->max) > 0) {
+ memo->max = i;
+ }
}
return Qnil;
}
/*
* call-seq:
- * enum.max -> obj
- * enum.max { |a, b| block } -> obj
- * enum.max(n) -> array
- * enum.max(n) { |a, b| block } -> array
- *
- * Returns the object in _enum_ with the maximum value. The
- * first form assumes all objects implement <code><=></code>;
- * the second uses the block to return <em>a <=> b</em>.
- *
- * a = %w(albatross dog horse)
- * a.max #=> "horse"
- * a.max { |a, b| a.length <=> b.length } #=> "albatross"
- *
- * If the +n+ argument is given, maximum +n+ elements are returned
- * as an array, sorted in descending order.
- *
- * a = %w[albatross dog horse]
- * a.max(2) #=> ["horse", "dog"]
- * a.max(2) {|a, b| a.length <=> b.length } #=> ["albatross", "horse"]
- * [5, 1, 3, 4, 2].max(3) #=> [5, 4, 3]
+ * max -> element
+ * max(n) -> array
+ * max {|a, b| ... } -> element
+ * max(n) {|a, b| ... } -> array
+ *
+ * Returns the element with the maximum element according to a given criterion.
+ * The ordering of equal elements is indeterminate and may be unstable.
+ *
+ * With no argument and no block, returns the maximum element,
+ * using the elements' own method <tt><=></tt> for comparison:
+ *
+ * (1..4).max # => 4
+ * (-4..-1).max # => -1
+ * %w[d c b a].max # => "d"
+ * {foo: 0, bar: 1, baz: 2}.max # => [:foo, 0]
+ * [].max # => nil
+ *
+ * With positive integer argument +n+ given, and no block,
+ * returns an array containing the first +n+ maximum elements that exist:
+ *
+ * (1..4).max(2) # => [4, 3]
+ * (-4..-1).max(2) # => [-1, -2]
+ * %w[d c b a].max(2) # => ["d", "c"]
+ * {foo: 0, bar: 1, baz: 2}.max(2) # => [[:foo, 0], [:baz, 2]]
+ * [].max(2) # => []
+ *
+ * With a block given, the block determines the maximum elements.
+ * The block is called with two elements +a+ and +b+, and must return:
+ *
+ * - A negative integer if <tt>a < b</tt>.
+ * - Zero if <tt>a == b</tt>.
+ * - A positive integer if <tt>a > b</tt>.
+ *
+ * With a block given and no argument,
+ * returns the maximum element as determined by the block:
+ *
+ * %w[xxx x xxxx xx].max {|a, b| a.size <=> b.size } # => "xxxx"
+ * h = {foo: 0, bar: 1, baz: 2}
+ * h.max {|pair1, pair2| pair1[1] <=> pair2[1] } # => [:baz, 2]
+ * [].max {|a, b| a <=> b } # => nil
+ *
+ * With a block given and positive integer argument +n+ given,
+ * returns an array containing the first +n+ maximum elements that exist,
+ * as determined by the block.
+ *
+ * %w[xxx x xxxx xx].max(2) {|a, b| a.size <=> b.size } # => ["xxxx", "xxx"]
+ * h = {foo: 0, bar: 1, baz: 2}
+ * h.max(2) {|pair1, pair2| pair1[1] <=> pair2[1] }
+ * # => [[:baz, 2], [:bar, 1]]
+ * [].max(2) {|a, b| a <=> b } # => []
+ *
+ * Related: #min, #minmax, #max_by.
+ *
*/
static VALUE
enum_max(int argc, VALUE *argv, VALUE obj)
{
VALUE memo;
- struct max_t *m = NEW_CMP_OPT_MEMO(struct max_t, memo);
+ struct max_t *m = NEW_MEMO_FOR(struct max_t, memo);
VALUE result;
VALUE num;
@@ -2152,16 +2450,14 @@ enum_max(int argc, VALUE *argv, VALUE obj)
return rb_nmin_run(obj, num, 0, 1, 0);
m->max = Qundef;
- m->cmp_opt.opt_methods = 0;
- m->cmp_opt.opt_inited = 0;
if (rb_block_given_p()) {
- rb_block_call(obj, id_each, 0, 0, max_ii, (VALUE)memo);
+ rb_block_call(obj, id_each, 0, 0, max_ii, (VALUE)memo);
}
else {
- rb_block_call(obj, id_each, 0, 0, max_i, (VALUE)memo);
+ rb_block_call(obj, id_each, 0, 0, max_i, (VALUE)memo);
}
result = m->max;
- if (result == Qundef) return Qnil;
+ if (UNDEF_P(result)) return Qnil;
return result;
}
@@ -2169,7 +2465,6 @@ struct minmax_t {
VALUE min;
VALUE max;
VALUE last;
- struct cmp_opt_data cmp_opt;
};
static void
@@ -2177,19 +2472,19 @@ minmax_i_update(VALUE i, VALUE j, struct minmax_t *memo)
{
int n;
- if (memo->min == Qundef) {
- memo->min = i;
- memo->max = j;
+ if (UNDEF_P(memo->min)) {
+ memo->min = i;
+ memo->max = j;
}
else {
- n = OPTIMIZED_CMP(i, memo->min, memo->cmp_opt);
- if (n < 0) {
- memo->min = i;
- }
- n = OPTIMIZED_CMP(j, memo->max, memo->cmp_opt);
- if (n > 0) {
- memo->max = j;
- }
+ n = OPTIMIZED_CMP(i, memo->min);
+ if (n < 0) {
+ memo->min = i;
+ }
+ n = OPTIMIZED_CMP(j, memo->max);
+ if (n > 0) {
+ memo->max = j;
+ }
}
}
@@ -2202,14 +2497,14 @@ minmax_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, _memo))
ENUM_WANT_SVALUE();
- if (memo->last == Qundef) {
+ if (UNDEF_P(memo->last)) {
memo->last = i;
return Qnil;
}
j = memo->last;
memo->last = Qundef;
- n = OPTIMIZED_CMP(j, i, memo->cmp_opt);
+ n = OPTIMIZED_CMP(j, i);
if (n == 0)
i = j;
else if (n < 0) {
@@ -2229,19 +2524,19 @@ minmax_ii_update(VALUE i, VALUE j, struct minmax_t *memo)
{
int n;
- if (memo->min == Qundef) {
- memo->min = i;
- memo->max = j;
+ if (UNDEF_P(memo->min)) {
+ memo->min = i;
+ memo->max = j;
}
else {
- n = rb_cmpint(rb_yield_values(2, i, memo->min), i, memo->min);
- if (n < 0) {
- memo->min = i;
- }
- n = rb_cmpint(rb_yield_values(2, j, memo->max), j, memo->max);
- if (n > 0) {
- memo->max = j;
- }
+ n = rb_cmpint(rb_yield_values(2, i, memo->min), i, memo->min);
+ if (n < 0) {
+ memo->min = i;
+ }
+ n = rb_cmpint(rb_yield_values(2, j, memo->max), j, memo->max);
+ if (n > 0) {
+ memo->max = j;
+ }
}
}
@@ -2254,7 +2549,7 @@ minmax_ii(RB_BLOCK_CALL_FUNC_ARGLIST(i, _memo))
ENUM_WANT_SVALUE();
- if (memo->last == Qundef) {
+ if (UNDEF_P(memo->last)) {
memo->last = i;
return Qnil;
}
@@ -2278,41 +2573,55 @@ minmax_ii(RB_BLOCK_CALL_FUNC_ARGLIST(i, _memo))
/*
* call-seq:
- * enum.minmax -> [min, max]
- * enum.minmax { |a, b| block } -> [min, max]
+ * minmax -> [minimum, maximum]
+ * minmax {|a, b| ... } -> [minimum, maximum]
*
- * Returns a two element array which contains the minimum and the
- * maximum value in the enumerable. The first form assumes all
- * objects implement <code><=></code>; the second uses the
- * block to return <em>a <=> b</em>.
+ * Returns a 2-element array containing the minimum and maximum elements
+ * according to a given criterion.
+ * The ordering of equal elements is indeterminate and may be unstable.
+ *
+ * With no argument and no block, returns the minimum and maximum elements,
+ * using the elements' own method <tt><=></tt> for comparison:
+ *
+ * (1..4).minmax # => [1, 4]
+ * (-4..-1).minmax # => [-4, -1]
+ * %w[d c b a].minmax # => ["a", "d"]
+ * {foo: 0, bar: 1, baz: 2}.minmax # => [[:bar, 1], [:foo, 0]]
+ * [].minmax # => [nil, nil]
+ *
+ * With a block given, returns the minimum and maximum elements
+ * as determined by the block:
+ *
+ * %w[xxx x xxxx xx].minmax {|a, b| a.size <=> b.size } # => ["x", "xxxx"]
+ * h = {foo: 0, bar: 1, baz: 2}
+ * h.minmax {|pair1, pair2| pair1[1] <=> pair2[1] }
+ * # => [[:foo, 0], [:baz, 2]]
+ * [].minmax {|a, b| a <=> b } # => [nil, nil]
+ *
+ * Related: #min, #max, #minmax_by.
*
- * a = %w(albatross dog horse)
- * a.minmax #=> ["albatross", "horse"]
- * a.minmax { |a, b| a.length <=> b.length } #=> ["dog", "albatross"]
*/
static VALUE
enum_minmax(VALUE obj)
{
VALUE memo;
- struct minmax_t *m = NEW_CMP_OPT_MEMO(struct minmax_t, memo);
+ struct minmax_t *m = NEW_MEMO_FOR(struct minmax_t, memo);
m->min = Qundef;
m->last = Qundef;
- m->cmp_opt.opt_methods = 0;
- m->cmp_opt.opt_inited = 0;
if (rb_block_given_p()) {
- rb_block_call(obj, id_each, 0, 0, minmax_ii, memo);
- if (m->last != Qundef)
- minmax_ii_update(m->last, m->last, m);
+ rb_block_call(obj, id_each, 0, 0, minmax_ii, memo);
+ if (!UNDEF_P(m->last))
+ minmax_ii_update(m->last, m->last, m);
}
else {
- rb_block_call(obj, id_each, 0, 0, minmax_i, memo);
- if (m->last != Qundef)
- minmax_i_update(m->last, m->last, m);
+ rb_block_call(obj, id_each, 0, 0, minmax_i, memo);
+ if (!UNDEF_P(m->last))
+ minmax_i_update(m->last, m->last, m);
}
- if (m->min != Qundef) {
- return rb_assoc_new(m->min, m->max);
+ if (!UNDEF_P(m->min)) {
+ return rb_assoc_new(m->min, m->max);
}
return rb_assoc_new(Qnil, Qnil);
}
@@ -2320,45 +2629,57 @@ enum_minmax(VALUE obj)
static VALUE
min_by_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
{
- struct cmp_opt_data cmp_opt = { 0, 0 };
struct MEMO *memo = MEMO_CAST(args);
VALUE v;
ENUM_WANT_SVALUE();
v = enum_yield(argc, i);
- if (memo->v1 == Qundef) {
- MEMO_V1_SET(memo, v);
- MEMO_V2_SET(memo, i);
+ if (UNDEF_P(memo->v1)) {
+ MEMO_V1_SET(memo, v);
+ MEMO_V2_SET(memo, i);
}
- else if (OPTIMIZED_CMP(v, memo->v1, cmp_opt) < 0) {
- MEMO_V1_SET(memo, v);
- MEMO_V2_SET(memo, i);
+ else if (OPTIMIZED_CMP(v, memo->v1) < 0) {
+ MEMO_V1_SET(memo, v);
+ MEMO_V2_SET(memo, i);
}
return Qnil;
}
/*
* call-seq:
- * enum.min_by {|obj| block } -> obj
- * enum.min_by -> an_enumerator
- * enum.min_by(n) {|obj| block } -> array
- * enum.min_by(n) -> an_enumerator
+ * min_by {|element| ... } -> element
+ * min_by(n) {|element| ... } -> array
+ * min_by -> enumerator
+ * min_by(n) -> enumerator
*
- * Returns the object in <i>enum</i> that gives the minimum
- * value from the given block.
+ * Returns the elements for which the block returns the minimum values.
*
- * If no block is given, an enumerator is returned instead.
+ * With a block given and no argument,
+ * returns the element for which the block returns the minimum value:
*
- * a = %w(albatross dog horse)
- * a.min_by { |x| x.length } #=> "dog"
+ * (1..4).min_by {|element| -element } # => 4
+ * %w[a b c d].min_by {|element| -element.ord } # => "d"
+ * {foo: 0, bar: 1, baz: 2}.min_by {|key, value| -value } # => [:baz, 2]
+ * [].min_by {|element| -element } # => nil
*
- * If the +n+ argument is given, minimum +n+ elements are returned
- * as an array. These +n+ elements are sorted by the value from the
- * given block.
+ * With a block given and positive integer argument +n+ given,
+ * returns an array containing the +n+ elements
+ * for which the block returns minimum values:
+ *
+ * (1..4).min_by(2) {|element| -element }
+ * # => [4, 3]
+ * %w[a b c d].min_by(2) {|element| -element.ord }
+ * # => ["d", "c"]
+ * {foo: 0, bar: 1, baz: 2}.min_by(2) {|key, value| -value }
+ * # => [[:baz, 2], [:bar, 1]]
+ * [].min_by(2) {|element| -element }
+ * # => []
+ *
+ * Returns an Enumerator if no block is given.
+ *
+ * Related: #min, #minmax, #max_by.
*
- * a = %w[albatross dog horse]
- * p a.min_by(2) {|x| x.length } #=> ["dog", "horse"]
*/
static VALUE
@@ -2382,89 +2703,56 @@ enum_min_by(int argc, VALUE *argv, VALUE obj)
static VALUE
max_by_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
{
- struct cmp_opt_data cmp_opt = { 0, 0 };
struct MEMO *memo = MEMO_CAST(args);
VALUE v;
ENUM_WANT_SVALUE();
v = enum_yield(argc, i);
- if (memo->v1 == Qundef) {
- MEMO_V1_SET(memo, v);
- MEMO_V2_SET(memo, i);
+ if (UNDEF_P(memo->v1)) {
+ MEMO_V1_SET(memo, v);
+ MEMO_V2_SET(memo, i);
}
- else if (OPTIMIZED_CMP(v, memo->v1, cmp_opt) > 0) {
- MEMO_V1_SET(memo, v);
- MEMO_V2_SET(memo, i);
+ else if (OPTIMIZED_CMP(v, memo->v1) > 0) {
+ MEMO_V1_SET(memo, v);
+ MEMO_V2_SET(memo, i);
}
return Qnil;
}
/*
* call-seq:
- * enum.max_by {|obj| block } -> obj
- * enum.max_by -> an_enumerator
- * enum.max_by(n) {|obj| block } -> obj
- * enum.max_by(n) -> an_enumerator
- *
- * Returns the object in <i>enum</i> that gives the maximum
- * value from the given block.
- *
- * If no block is given, an enumerator is returned instead.
- *
- * a = %w(albatross dog horse)
- * a.max_by { |x| x.length } #=> "albatross"
- *
- * If the +n+ argument is given, maximum +n+ elements are returned
- * as an array. These +n+ elements are sorted by the value from the
- * given block, in descending order.
- *
- * a = %w[albatross dog horse]
- * a.max_by(2) {|x| x.length } #=> ["albatross", "horse"]
- *
- * enum.max_by(n) can be used to implement weighted random sampling.
- * Following example implements and use Enumerable#wsample.
- *
- * module Enumerable
- * # weighted random sampling.
- * #
- * # Pavlos S. Efraimidis, Paul G. Spirakis
- * # Weighted random sampling with a reservoir
- * # Information Processing Letters
- * # Volume 97, Issue 5 (16 March 2006)
- * def wsample(n)
- * self.max_by(n) {|v| rand ** (1.0/yield(v)) }
- * end
- * end
- * e = (-20..20).to_a*10000
- * a = e.wsample(20000) {|x|
- * Math.exp(-(x/5.0)**2) # normal distribution
- * }
- * # a is 20000 samples from e.
- * p a.length #=> 20000
- * h = a.group_by {|x| x }
- * -10.upto(10) {|x| puts "*" * (h[x].length/30.0).to_i if h[x] }
- * #=> *
- * # ***
- * # ******
- * # ***********
- * # ******************
- * # *****************************
- * # *****************************************
- * # ****************************************************
- * # ***************************************************************
- * # ********************************************************************
- * # ***********************************************************************
- * # ***********************************************************************
- * # **************************************************************
- * # ****************************************************
- * # ***************************************
- * # ***************************
- * # ******************
- * # ***********
- * # *******
- * # ***
- * # *
+ * max_by {|element| ... } -> element
+ * max_by(n) {|element| ... } -> array
+ * max_by -> enumerator
+ * max_by(n) -> enumerator
+ *
+ * Returns the elements for which the block returns the maximum values.
+ *
+ * With a block given and no argument,
+ * returns the element for which the block returns the maximum value:
+ *
+ * (1..4).max_by {|element| -element } # => 1
+ * %w[a b c d].max_by {|element| -element.ord } # => "a"
+ * {foo: 0, bar: 1, baz: 2}.max_by {|key, value| -value } # => [:foo, 0]
+ * [].max_by {|element| -element } # => nil
+ *
+ * With a block given and positive integer argument +n+ given,
+ * returns an array containing the +n+ elements
+ * for which the block returns maximum values:
+ *
+ * (1..4).max_by(2) {|element| -element }
+ * # => [1, 2]
+ * %w[a b c d].max_by(2) {|element| -element.ord }
+ * # => ["a", "b"]
+ * {foo: 0, bar: 1, baz: 2}.max_by(2) {|key, value| -value }
+ * # => [[:foo, 0], [:bar, 1]]
+ * [].max_by(2) {|element| -element }
+ * # => []
+ *
+ * Returns an Enumerator if no block is given.
+ *
+ * Related: #max, #minmax, #min_by.
*
*/
@@ -2498,30 +2786,27 @@ struct minmax_by_t {
static void
minmax_by_i_update(VALUE v1, VALUE v2, VALUE i1, VALUE i2, struct minmax_by_t *memo)
{
- struct cmp_opt_data cmp_opt = { 0, 0 };
-
- if (memo->min_bv == Qundef) {
- memo->min_bv = v1;
- memo->max_bv = v2;
- memo->min = i1;
- memo->max = i2;
+ if (UNDEF_P(memo->min_bv)) {
+ memo->min_bv = v1;
+ memo->max_bv = v2;
+ memo->min = i1;
+ memo->max = i2;
}
else {
- if (OPTIMIZED_CMP(v1, memo->min_bv, cmp_opt) < 0) {
- memo->min_bv = v1;
- memo->min = i1;
- }
- if (OPTIMIZED_CMP(v2, memo->max_bv, cmp_opt) > 0) {
- memo->max_bv = v2;
- memo->max = i2;
- }
+ if (OPTIMIZED_CMP(v1, memo->min_bv) < 0) {
+ memo->min_bv = v1;
+ memo->min = i1;
+ }
+ if (OPTIMIZED_CMP(v2, memo->max_bv) > 0) {
+ memo->max_bv = v2;
+ memo->max = i2;
+ }
}
}
static VALUE
minmax_by_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, _memo))
{
- struct cmp_opt_data cmp_opt = { 0, 0 };
struct minmax_by_t *memo = MEMO_FOR(struct minmax_by_t, _memo);
VALUE vi, vj, j;
int n;
@@ -2530,7 +2815,7 @@ minmax_by_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, _memo))
vi = enum_yield(argc, i);
- if (memo->last_bv == Qundef) {
+ if (UNDEF_P(memo->last_bv)) {
memo->last_bv = vi;
memo->last = i;
return Qnil;
@@ -2539,7 +2824,7 @@ minmax_by_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, _memo))
j = memo->last;
memo->last_bv = Qundef;
- n = OPTIMIZED_CMP(vj, vi, cmp_opt);
+ n = OPTIMIZED_CMP(vj, vi);
if (n == 0) {
i = j;
vi = vj;
@@ -2561,17 +2846,25 @@ minmax_by_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, _memo))
/*
* call-seq:
- * enum.minmax_by { |obj| block } -> [min, max]
- * enum.minmax_by -> an_enumerator
+ * minmax_by {|element| ... } -> [minimum, maximum]
+ * minmax_by -> enumerator
+ *
+ * Returns a 2-element array containing the elements
+ * for which the block returns minimum and maximum values:
*
- * Returns a two element array containing the objects in
- * <i>enum</i> that correspond to the minimum and maximum values respectively
- * from the given block.
+ * (1..4).minmax_by {|element| -element }
+ * # => [4, 1]
+ * %w[a b c d].minmax_by {|element| -element.ord }
+ * # => ["d", "a"]
+ * {foo: 0, bar: 1, baz: 2}.minmax_by {|key, value| -value }
+ * # => [[:baz, 2], [:foo, 0]]
+ * [].minmax_by {|element| -element }
+ * # => [nil, nil]
*
- * If no block is given, an enumerator is returned instead.
+ * Returns an Enumerator if no block is given.
+ *
+ * Related: #max_by, #minmax, #min_by.
*
- * a = %w(albatross dog horse)
- * a.minmax_by { |x| x.length } #=> ["dog", "albatross"]
*/
static VALUE
@@ -2589,7 +2882,7 @@ enum_minmax_by(VALUE obj)
m->last_bv = Qundef;
m->last = Qundef;
rb_block_call(obj, id_each, 0, 0, minmax_by_i, memo);
- if (m->last_bv != Qundef)
+ if (!UNDEF_P(m->last_bv))
minmax_by_i_update(m->last_bv, m->last_bv, m->last, m->last, m);
m = MEMO_FOR(struct minmax_by_t, memo);
return rb_assoc_new(m->min, m->max);
@@ -2601,24 +2894,26 @@ member_i(RB_BLOCK_CALL_FUNC_ARGLIST(iter, args))
struct MEMO *memo = MEMO_CAST(args);
if (rb_equal(rb_enum_values_pack(argc, argv), memo->v1)) {
- MEMO_V2_SET(memo, Qtrue);
- rb_iter_break();
+ MEMO_V2_SET(memo, Qtrue);
+ rb_iter_break();
}
return Qnil;
}
/*
* call-seq:
- * enum.include?(obj) -> true or false
- * enum.member?(obj) -> true or false
+ * include?(object) -> true or false
*
- * Returns <code>true</code> if any member of <i>enum</i> equals
- * <i>obj</i>. Equality is tested using <code>==</code>.
+ * Returns whether for any element <tt>object == element</tt>:
*
- * (1..10).include? 5 #=> true
- * (1..10).include? 15 #=> false
- * (1..10).member? 5 #=> true
- * (1..10).member? 15 #=> false
+ * (1..4).include?(2) # => true
+ * (1..4).include?(5) # => false
+ * (1..4).include?('2') # => false
+ * %w[a b c d].include?('b') # => true
+ * %w[a b c d].include?('2') # => false
+ * {foo: 0, bar: 1, baz: 2}.include?(:foo) # => true
+ * {foo: 0, bar: 1, baz: 2}.include?('foo') # => false
+ * {foo: 0, bar: 1, baz: 2}.include?(0) # => false
*
*/
@@ -2643,20 +2938,28 @@ each_with_index_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, memo))
/*
* call-seq:
- * enum.each_with_index(*args) { |obj, i| block } -> enum
- * enum.each_with_index(*args) -> an_enumerator
+ * each_with_index(*args) {|element, i| ..... } -> self
+ * each_with_index(*args) -> enumerator
*
- * Calls <em>block</em> with two arguments, the item and its index,
- * for each item in <i>enum</i>. Given arguments are passed through
- * to #each().
+ * With a block given, calls the block with each element and its index;
+ * returns +self+:
*
- * If no block is given, an enumerator is returned instead.
+ * h = {}
+ * (1..4).each_with_index {|element, i| h[element] = i } # => 1..4
+ * h # => {1=>0, 2=>1, 3=>2, 4=>3}
*
- * hash = Hash.new
- * %w(cat dog wombat).each_with_index { |item, index|
- * hash[item] = index
- * }
- * hash #=> {"cat"=>0, "dog"=>1, "wombat"=>2}
+ * h = {}
+ * %w[a b c d].each_with_index {|element, i| h[element] = i }
+ * # => ["a", "b", "c", "d"]
+ * h # => {"a"=>0, "b"=>1, "c"=>2, "d"=>3}
+ *
+ * a = []
+ * h = {foo: 0, bar: 1, baz: 2}
+ * h.each_with_index {|element, i| a.push([i, element]) }
+ * # => {:foo=>0, :bar=>1, :baz=>2}
+ * a # => [[0, [:foo, 0]], [1, [:bar, 1]], [2, [:baz, 2]]]
+ *
+ * With no block given, returns an Enumerator.
*
*/
@@ -2675,20 +2978,28 @@ enum_each_with_index(int argc, VALUE *argv, VALUE obj)
/*
* call-seq:
- * enum.reverse_each(*args) { |item| block } -> enum
- * enum.reverse_each(*args) -> an_enumerator
+ * reverse_each(*args) {|element| ... } -> self
+ * reverse_each(*args) -> enumerator
*
- * Builds a temporary array and traverses that array in reverse order.
+ * With a block given, calls the block with each element,
+ * but in reverse order; returns +self+:
*
- * If no block is given, an enumerator is returned instead.
+ * a = []
+ * (1..4).reverse_each {|element| a.push(-element) } # => 1..4
+ * a # => [-4, -3, -2, -1]
*
- * (1..3).reverse_each { |v| p v }
+ * a = []
+ * %w[a b c d].reverse_each {|element| a.push(element) }
+ * # => ["a", "b", "c", "d"]
+ * a # => ["d", "c", "b", "a"]
*
- * produces:
+ * a = []
+ * h.reverse_each {|element| a.push(element) }
+ * # => {:foo=>0, :bar=>1, :baz=>2}
+ * a # => [[:baz, 2], [:bar, 1], [:foo, 0]]
+ *
+ * With no block given, returns an Enumerator.
*
- * 3
- * 2
- * 1
*/
static VALUE
@@ -2725,30 +3036,39 @@ each_val_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, p))
/*
* call-seq:
- * enum.each_entry { |obj| block } -> enum
- * enum.each_entry -> an_enumerator
- *
- * Calls <i>block</i> once for each element in +self+, passing that
- * element as a parameter, converting multiple values from yield to an
- * array.
- *
- * If no block is given, an enumerator is returned instead.
- *
- * class Foo
- * include Enumerable
- * def each
- * yield 1
- * yield 1, 2
- * yield
- * end
- * end
- * Foo.new.each_entry{ |o| p o }
+ * each_entry(*args) {|element| ... } -> self
+ * each_entry(*args) -> enumerator
*
- * produces:
+ * Calls the given block with each element,
+ * converting multiple values from yield to an array; returns +self+:
*
- * 1
- * [1, 2]
- * nil
+ * a = []
+ * (1..4).each_entry {|element| a.push(element) } # => 1..4
+ * a # => [1, 2, 3, 4]
+ *
+ * a = []
+ * h = {foo: 0, bar: 1, baz:2}
+ * h.each_entry {|element| a.push(element) }
+ * # => {:foo=>0, :bar=>1, :baz=>2}
+ * a # => [[:foo, 0], [:bar, 1], [:baz, 2]]
+ *
+ * class Foo
+ * include Enumerable
+ * def each
+ * yield 1
+ * yield 1, 2
+ * yield
+ * end
+ * end
+ * Foo.new.each_entry {|yielded| p yielded }
+ *
+ * Output:
+ *
+ * 1
+ * [1, 2]
+ * nil
+ *
+ * With no block given, returns an Enumerator.
*
*/
@@ -2790,14 +3110,14 @@ each_slice_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, m))
rb_ary_push(ary, i);
if (RARRAY_LEN(ary) == size) {
- v = rb_yield(ary);
+ v = rb_yield(ary);
- if (memo->v2) {
- MEMO_V1_SET(memo, rb_ary_new2(size));
- }
- else {
- rb_ary_clear(ary);
- }
+ if (memo->v2) {
+ MEMO_V1_SET(memo, rb_ary_new2(size));
+ }
+ else {
+ rb_ary_clear(ary);
+ }
}
return v;
@@ -2813,7 +3133,7 @@ enum_each_slice_size(VALUE obj, VALUE args, VALUE eobj)
if (slice_size <= 0) rb_raise(rb_eArgError, "invalid slice size");
size = enum_size(obj, 0, 0);
- if (size == Qnil) return Qnil;
+ if (NIL_P(size)) return Qnil;
if (RB_FLOAT_TYPE_P(size) && RTEST(rb_funcall(size, infinite_p, 0))) {
return size;
}
@@ -2824,18 +3144,22 @@ enum_each_slice_size(VALUE obj, VALUE args, VALUE eobj)
/*
* call-seq:
- * enum.each_slice(n) { ... } -> nil
- * enum.each_slice(n) -> an_enumerator
+ * each_slice(n) { ... } -> self
+ * each_slice(n) -> enumerator
*
- * Iterates the given block for each slice of <n> elements. If no
- * block is given, returns an enumerator.
+ * Calls the block with each successive disjoint +n+-tuple of elements;
+ * returns +self+:
*
- * (1..10).each_slice(3) { |a| p a }
- * # outputs below
- * [1, 2, 3]
- * [4, 5, 6]
- * [7, 8, 9]
- * [10]
+ * a = []
+ * (1..10).each_slice(3) {|tuple| a.push(tuple) }
+ * a # => [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]
+ *
+ * a = []
+ * h = {foo: 0, bar: 1, baz: 2, bat: 3, bam: 4}
+ * h.each_slice(2) {|tuple| a.push(tuple) }
+ * a # => [[[:foo, 0], [:bar, 1]], [[:baz, 2], [:bat, 3]], [[:bam, 4]]]
+ *
+ * With no block given, returns an Enumerator.
*
*/
static VALUE
@@ -2856,7 +3180,7 @@ enum_each_slice(VALUE obj, VALUE n)
ary = memo->v1;
if (RARRAY_LEN(ary) > 0) rb_yield(ary);
- return Qnil;
+ return obj;
}
static VALUE
@@ -2869,14 +3193,14 @@ each_cons_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
ENUM_WANT_SVALUE();
if (RARRAY_LEN(ary) == size) {
- rb_ary_shift(ary);
+ rb_ary_shift(ary);
}
rb_ary_push(ary, i);
if (RARRAY_LEN(ary) == size) {
- if (memo->v2) {
- ary = rb_ary_dup(ary);
- }
- v = rb_yield(ary);
+ if (memo->v2) {
+ ary = rb_ary_dup(ary);
+ }
+ v = rb_yield(ary);
}
return v;
}
@@ -2884,38 +3208,36 @@ each_cons_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
static VALUE
enum_each_cons_size(VALUE obj, VALUE args, VALUE eobj)
{
- struct cmp_opt_data cmp_opt = { 0, 0 };
const VALUE zero = LONG2FIX(0);
VALUE n, size;
long cons_size = NUM2LONG(RARRAY_AREF(args, 0));
if (cons_size <= 0) rb_raise(rb_eArgError, "invalid size");
size = enum_size(obj, 0, 0);
- if (size == Qnil) return Qnil;
+ if (NIL_P(size)) return Qnil;
n = add_int(size, 1 - cons_size);
- return (OPTIMIZED_CMP(n, zero, cmp_opt) == -1) ? zero : n;
+ return (OPTIMIZED_CMP(n, zero) == -1) ? zero : n;
}
/*
* call-seq:
- * enum.each_cons(n) { ... } -> nil
- * enum.each_cons(n) -> an_enumerator
- *
- * Iterates the given block for each array of consecutive <n>
- * elements. If no block is given, returns an enumerator.
- *
- * e.g.:
- * (1..10).each_cons(3) { |a| p a }
- * # outputs below
- * [1, 2, 3]
- * [2, 3, 4]
- * [3, 4, 5]
- * [4, 5, 6]
- * [5, 6, 7]
- * [6, 7, 8]
- * [7, 8, 9]
- * [8, 9, 10]
+ * each_cons(n) { ... } -> self
+ * each_cons(n) -> enumerator
+ *
+ * Calls the block with each successive overlapped +n+-tuple of elements;
+ * returns +self+:
+ *
+ * a = []
+ * (1..5).each_cons(3) {|element| a.push(element) }
+ * a # => [[1, 2, 3], [2, 3, 4], [3, 4, 5]]
+ *
+ * a = []
+ * h = {foo: 0, bar: 1, baz: 2, bam: 3}
+ * h.each_cons(2) {|element| a.push(element) }
+ * a # => [[[:foo, 0], [:bar, 1]], [[:bar, 1], [:baz, 2]], [[:baz, 2], [:bam, 3]]]
+ *
+ * With no block given, returns an Enumerator.
*
*/
static VALUE
@@ -2928,11 +3250,11 @@ enum_each_cons(VALUE obj, VALUE n)
if (size <= 0) rb_raise(rb_eArgError, "invalid size");
RETURN_SIZED_ENUMERATOR(obj, 1, &n, enum_each_cons_size);
arity = rb_block_arity();
- if (enum_size_over_p(obj, size)) return Qnil;
+ if (enum_size_over_p(obj, size)) return obj;
memo = MEMO_NEW(rb_ary_new2(size), dont_recycle_block_arg(arity), size);
rb_block_call(obj, id_each, 0, 0, each_cons_i, (VALUE)memo);
- return Qnil;
+ return obj;
}
static VALUE
@@ -2944,16 +3266,19 @@ each_with_object_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, memo))
/*
* call-seq:
- * enum.each_with_object(obj) { |(*args), memo_obj| ... } -> obj
- * enum.each_with_object(obj) -> an_enumerator
+ * each_with_object(object) { |(*args), memo_object| ... } -> object
+ * each_with_object(object) -> enumerator
*
- * Iterates the given block for each element with an arbitrary
- * object given, and returns the initially given object.
+ * Calls the block once for each element, passing both the element
+ * and the given object:
*
- * If no block is given, returns an enumerator.
+ * (1..4).each_with_object([]) {|i, a| a.push(i**2) }
+ * # => [1, 4, 9, 16]
*
- * evens = (1..10).each_with_object([]) { |i, a| a << i*2 }
- * #=> [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
+ * {foo: 0, bar: 1, baz: 2}.each_with_object({}) {|(k, v), h| h[v] = k }
+ * # => {0=>:foo, 1=>:bar, 2=>:baz}
+ *
+ * With no block given, returns an Enumerator.
*
*/
static VALUE
@@ -2979,20 +3304,20 @@ zip_ary(RB_BLOCK_CALL_FUNC_ARGLIST(val, memoval))
tmp = rb_ary_new2(RARRAY_LEN(args) + 1);
rb_ary_store(tmp, 0, rb_enum_values_pack(argc, argv));
for (i=0; i<RARRAY_LEN(args); i++) {
- VALUE e = RARRAY_AREF(args, i);
+ VALUE e = RARRAY_AREF(args, i);
- if (RARRAY_LEN(e) <= n) {
- rb_ary_push(tmp, Qnil);
- }
- else {
- rb_ary_push(tmp, RARRAY_AREF(e, n));
- }
+ if (RARRAY_LEN(e) <= n) {
+ rb_ary_push(tmp, Qnil);
+ }
+ else {
+ rb_ary_push(tmp, RARRAY_AREF(e, n));
+ }
}
if (NIL_P(result)) {
- enum_yield_array(tmp);
+ enum_yield_array(tmp);
}
else {
- rb_ary_push(result, tmp);
+ rb_ary_push(result, tmp);
}
RB_GC_GUARD(args);
@@ -3026,26 +3351,26 @@ zip_i(RB_BLOCK_CALL_FUNC_ARGLIST(val, memoval))
tmp = rb_ary_new2(RARRAY_LEN(args) + 1);
rb_ary_store(tmp, 0, rb_enum_values_pack(argc, argv));
for (i=0; i<RARRAY_LEN(args); i++) {
- if (NIL_P(RARRAY_AREF(args, i))) {
- rb_ary_push(tmp, Qnil);
- }
- else {
- VALUE v[2];
-
- v[1] = RARRAY_AREF(args, i);
- rb_rescue2(call_next, (VALUE)v, call_stop, (VALUE)v, rb_eStopIteration, (VALUE)0);
- if (v[0] == Qundef) {
- RARRAY_ASET(args, i, Qnil);
- v[0] = Qnil;
- }
- rb_ary_push(tmp, v[0]);
- }
+ if (NIL_P(RARRAY_AREF(args, i))) {
+ rb_ary_push(tmp, Qnil);
+ }
+ else {
+ VALUE v[2];
+
+ v[1] = RARRAY_AREF(args, i);
+ rb_rescue2(call_next, (VALUE)v, call_stop, (VALUE)v, rb_eStopIteration, (VALUE)0);
+ if (UNDEF_P(v[0])) {
+ RARRAY_ASET(args, i, Qnil);
+ v[0] = Qnil;
+ }
+ rb_ary_push(tmp, v[0]);
+ }
}
if (NIL_P(result)) {
- enum_yield_array(tmp);
+ enum_yield_array(tmp);
}
else {
- rb_ary_push(result, tmp);
+ rb_ary_push(result, tmp);
}
RB_GC_GUARD(args);
@@ -3055,29 +3380,68 @@ zip_i(RB_BLOCK_CALL_FUNC_ARGLIST(val, memoval))
/*
* call-seq:
- * enum.zip(arg, ...) -> an_array_of_array
- * enum.zip(arg, ...) { |arr| block } -> nil
- *
- * Takes one element from <i>enum</i> and merges corresponding
- * elements from each <i>args</i>. This generates a sequence of
- * <em>n</em>-element arrays, where <em>n</em> is one more than the
- * count of arguments. The length of the resulting sequence will be
- * <code>enum#size</code>. If the size of any argument is less than
- * <code>enum#size</code>, <code>nil</code> values are supplied. If
- * a block is given, it is invoked for each output array, otherwise
- * an array of arrays is returned.
- *
- * a = [ 4, 5, 6 ]
- * b = [ 7, 8, 9 ]
- *
- * a.zip(b) #=> [[4, 7], [5, 8], [6, 9]]
- * [1, 2, 3].zip(a, b) #=> [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
- * [1, 2].zip(a, b) #=> [[1, 4, 7], [2, 5, 8]]
- * a.zip([1, 2], [8]) #=> [[4, 1, 8], [5, 2, nil], [6, nil, nil]]
- *
- * c = []
- * a.zip(b) { |x, y| c << x + y } #=> nil
- * c #=> [11, 13, 15]
+ * zip(*other_enums) -> array
+ * zip(*other_enums) {|array| ... } -> nil
+ *
+ * With no block given, returns a new array +new_array+ of size self.size
+ * whose elements are arrays.
+ * Each nested array <tt>new_array[n]</tt>
+ * is of size <tt>other_enums.size+1</tt>, and contains:
+ *
+ * - The +n+-th element of self.
+ * - The +n+-th element of each of the +other_enums+.
+ *
+ * If all +other_enums+ and self are the same size,
+ * all elements are included in the result, and there is no +nil+-filling:
+ *
+ * a = [:a0, :a1, :a2, :a3]
+ * b = [:b0, :b1, :b2, :b3]
+ * c = [:c0, :c1, :c2, :c3]
+ * d = a.zip(b, c)
+ * d # => [[:a0, :b0, :c0], [:a1, :b1, :c1], [:a2, :b2, :c2], [:a3, :b3, :c3]]
+ *
+ * f = {foo: 0, bar: 1, baz: 2}
+ * g = {goo: 3, gar: 4, gaz: 5}
+ * h = {hoo: 6, har: 7, haz: 8}
+ * d = f.zip(g, h)
+ * d # => [
+ * # [[:foo, 0], [:goo, 3], [:hoo, 6]],
+ * # [[:bar, 1], [:gar, 4], [:har, 7]],
+ * # [[:baz, 2], [:gaz, 5], [:haz, 8]]
+ * # ]
+ *
+ * If any enumerable in other_enums is smaller than self,
+ * fills to <tt>self.size</tt> with +nil+:
+ *
+ * a = [:a0, :a1, :a2, :a3]
+ * b = [:b0, :b1, :b2]
+ * c = [:c0, :c1]
+ * d = a.zip(b, c)
+ * d # => [[:a0, :b0, :c0], [:a1, :b1, :c1], [:a2, :b2, nil], [:a3, nil, nil]]
+ *
+ * If any enumerable in other_enums is larger than self,
+ * its trailing elements are ignored:
+ *
+ * a = [:a0, :a1, :a2, :a3]
+ * b = [:b0, :b1, :b2, :b3, :b4]
+ * c = [:c0, :c1, :c2, :c3, :c4, :c5]
+ * d = a.zip(b, c)
+ * d # => [[:a0, :b0, :c0], [:a1, :b1, :c1], [:a2, :b2, :c2], [:a3, :b3, :c3]]
+ *
+ * When a block is given, calls the block with each of the sub-arrays
+ * (formed as above); returns nil:
+ *
+ * a = [:a0, :a1, :a2, :a3]
+ * b = [:b0, :b1, :b2, :b3]
+ * c = [:c0, :c1, :c2, :c3]
+ * a.zip(b, c) {|sub_array| p sub_array} # => nil
+ *
+ * Output:
+ *
+ * [:a0, :b0, :c0]
+ * [:a1, :b1, :c1]
+ * [:a2, :b2, :c2]
+ * [:a3, :b3, :c3]
*
*/
@@ -3093,26 +3457,26 @@ enum_zip(int argc, VALUE *argv, VALUE obj)
argv = RARRAY_PTR(args);
for (i=0; i<argc; i++) {
- VALUE ary = rb_check_array_type(argv[i]);
- if (NIL_P(ary)) {
- allary = FALSE;
- break;
- }
- argv[i] = ary;
+ VALUE ary = rb_check_array_type(argv[i]);
+ if (NIL_P(ary)) {
+ allary = FALSE;
+ break;
+ }
+ argv[i] = ary;
}
if (!allary) {
- static const VALUE sym_each = STATIC_ID2SYM(id_each);
- CONST_ID(conv, "to_enum");
- for (i=0; i<argc; i++) {
- if (!rb_respond_to(argv[i], id_each)) {
- rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (must respond to :each)",
- rb_obj_class(argv[i]));
+ static const VALUE sym_each = STATIC_ID2SYM(id_each);
+ CONST_ID(conv, "to_enum");
+ for (i=0; i<argc; i++) {
+ if (!rb_respond_to(argv[i], id_each)) {
+ rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (must respond to :each)",
+ rb_obj_class(argv[i]));
}
- argv[i] = rb_funcallv(argv[i], conv, 1, &sym_each);
- }
+ argv[i] = rb_funcallv(argv[i], conv, 1, &sym_each);
+ }
}
if (!rb_block_given_p()) {
- result = rb_ary_new();
+ result = rb_ary_new();
}
/* TODO: use NODE_DOT2 as memo(v, v, -) */
@@ -3133,13 +3497,16 @@ take_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
/*
* call-seq:
- * enum.take(n) -> array
+ * take(n) -> array
*
- * Returns first n elements from <i>enum</i>.
+ * For non-negative integer +n+, returns the first +n+ elements:
*
- * a = [1, 2, 3, 4, 5, 0]
- * a.take(3) #=> [1, 2, 3]
- * a.take(30) #=> [1, 2, 3, 4, 5, 0]
+ * r = (1..4)
+ * r.take(2) # => [1, 2]
+ * r.take(0) # => []
+ *
+ * h = {foo: 0, bar: 1, baz: 2, bat: 3}
+ * h.take(2) # => [[:foo, 0], [:bar, 1]]
*
*/
@@ -3151,7 +3518,7 @@ enum_take(VALUE obj, VALUE n)
long len = NUM2LONG(n);
if (len < 0) {
- rb_raise(rb_eArgError, "attempt to take negative size");
+ rb_raise(rb_eArgError, "attempt to take negative size");
}
if (len == 0) return rb_ary_new2(0);
@@ -3172,16 +3539,20 @@ take_while_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, ary))
/*
* call-seq:
- * enum.take_while { |obj| block } -> array
- * enum.take_while -> an_enumerator
+ * take_while {|element| ... } -> array
+ * take_while -> enumerator
*
- * Passes elements to the block until the block returns +nil+ or +false+,
- * then stops iterating and returns an array of all prior elements.
+ * Calls the block with successive elements as long as the block
+ * returns a truthy value;
+ * returns an array of all elements up to that point:
*
- * If no block is given, an enumerator is returned instead.
*
- * a = [1, 2, 3, 4, 5, 0]
- * a.take_while { |i| i < 3 } #=> [1, 2]
+ * (1..4).take_while{|i| i < 3 } # => [1, 2]
+ * h = {foo: 0, bar: 1, baz: 2}
+ * h.take_while{|element| key, value = *element; value < 2 }
+ * # => [[:foo, 0], [:bar, 1]]
+ *
+ * With no block given, returns an Enumerator.
*
*/
@@ -3201,23 +3572,30 @@ drop_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
{
struct MEMO *memo = MEMO_CAST(args);
if (memo->u3.cnt == 0) {
- rb_ary_push(memo->v1, rb_enum_values_pack(argc, argv));
+ rb_ary_push(memo->v1, rb_enum_values_pack(argc, argv));
}
else {
- memo->u3.cnt--;
+ memo->u3.cnt--;
}
return Qnil;
}
/*
* call-seq:
- * enum.drop(n) -> array
+ * drop(n) -> array
+ *
+ * For positive integer +n+, returns an array containing
+ * all but the first +n+ elements:
*
- * Drops first n elements from <i>enum</i>, and returns rest elements
- * in an array.
+ * r = (1..4)
+ * r.drop(3) # => [4]
+ * r.drop(2) # => [3, 4]
+ * r.drop(1) # => [2, 3, 4]
+ * r.drop(0) # => [1, 2, 3, 4]
+ * r.drop(50) # => []
*
- * a = [1, 2, 3, 4, 5, 0]
- * a.drop(3) #=> [4, 5, 0]
+ * h = {foo: 0, bar: 1, baz: 2, bat: 3}
+ * h.drop(2) # => [[:baz, 2], [:bat, 3]]
*
*/
@@ -3229,7 +3607,7 @@ enum_drop(VALUE obj, VALUE n)
long len = NUM2LONG(n);
if (len < 0) {
- rb_raise(rb_eArgError, "attempt to drop negative size");
+ rb_raise(rb_eArgError, "attempt to drop negative size");
}
result = rb_ary_new();
@@ -3246,27 +3624,30 @@ drop_while_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
ENUM_WANT_SVALUE();
if (!memo->u3.state && !RTEST(enum_yield(argc, i))) {
- memo->u3.state = TRUE;
+ memo->u3.state = TRUE;
}
if (memo->u3.state) {
- rb_ary_push(memo->v1, i);
+ rb_ary_push(memo->v1, i);
}
return Qnil;
}
/*
* call-seq:
- * enum.drop_while { |obj| block } -> array
- * enum.drop_while -> an_enumerator
+ * drop_while {|element| ... } -> array
+ * drop_while -> enumerator
+ *
+ * Calls the block with successive elements as long as the block
+ * returns a truthy value;
+ * returns an array of all elements after that point:
*
- * Drops elements up to, but not including, the first element for
- * which the block returns +nil+ or +false+ and returns an array
- * containing the remaining elements.
*
- * If no block is given, an enumerator is returned instead.
+ * (1..4).drop_while{|i| i < 3 } # => [3, 4]
+ * h = {foo: 0, bar: 1, baz: 2}
+ * a = h.drop_while{|element| key, value = *element; value < 2 }
+ * a # => [[:baz, 2]]
*
- * a = [1, 2, 3, 4, 5, 0]
- * a.drop_while { |i| i < 3 } #=> [3, 4, 5, 0]
+ * With no block given, returns an Enumerator.
*
*/
@@ -3301,8 +3682,8 @@ enum_cycle_size(VALUE self, VALUE args, VALUE eobj)
VALUE size;
if (args && (RARRAY_LEN(args) > 0)) {
- n = RARRAY_AREF(args, 0);
- if (!NIL_P(n)) mul = NUM2LONG(n);
+ n = RARRAY_AREF(args, 0);
+ if (!NIL_P(n)) mul = NUM2LONG(n);
}
size = enum_size(self, args, 0);
@@ -3316,22 +3697,28 @@ enum_cycle_size(VALUE self, VALUE args, VALUE eobj)
/*
* call-seq:
- * enum.cycle(n=nil) { |obj| block } -> nil
- * enum.cycle(n=nil) -> an_enumerator
+ * cycle(n = nil) {|element| ...} -> nil
+ * cycle(n = nil) -> enumerator
*
- * Calls <i>block</i> for each element of <i>enum</i> repeatedly _n_
- * times or forever if none or +nil+ is given. If a non-positive
- * number is given or the collection is empty, does nothing. Returns
- * +nil+ if the loop has finished without getting interrupted.
+ * When called with positive integer argument +n+ and a block,
+ * calls the block with each element, then does so again,
+ * until it has done so +n+ times; returns +nil+:
*
- * Enumerable#cycle saves elements in an internal array so changes
- * to <i>enum</i> after the first pass have no effect.
+ * a = []
+ * (1..4).cycle(3) {|element| a.push(element) } # => nil
+ * a # => [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]
+ * a = []
+ * ('a'..'d').cycle(2) {|element| a.push(element) }
+ * a # => ["a", "b", "c", "d", "a", "b", "c", "d"]
+ * a = []
+ * {foo: 0, bar: 1, baz: 2}.cycle(2) {|element| a.push(element) }
+ * a # => [[:foo, 0], [:bar, 1], [:baz, 2], [:foo, 0], [:bar, 1], [:baz, 2]]
*
- * If no block is given, an enumerator is returned instead.
+ * If count is zero or negative, does not call the block.
*
- * a = ["a", "b", "c"]
- * a.cycle { |x| puts x } # print, a, b, c, a, b, c,.. forever.
- * a.cycle(2) { |x| puts x } # print, a, b, c, a, b, c.
+ * When called with a block and +n+ is +nil+, cycles forever.
+ *
+ * When no block is given, returns an Enumerator.
*
*/
@@ -3359,7 +3746,7 @@ enum_cycle(int argc, VALUE *argv, VALUE obj)
if (len == 0) return Qnil;
while (n < 0 || 0 < --n) {
for (i=0; i<len; i++) {
- enum_yield_array(RARRAY_AREF(ary, i));
+ enum_yield_array(RARRAY_AREF(ary, i));
}
}
return Qnil;
@@ -3386,22 +3773,22 @@ chunk_ii(RB_BLOCK_CALL_FUNC_ARGLIST(i, _argp))
if (v == alone) {
if (!NIL_P(argp->prev_value)) {
- s = rb_assoc_new(argp->prev_value, argp->prev_elts);
+ s = rb_assoc_new(argp->prev_value, argp->prev_elts);
rb_funcallv(argp->yielder, id_lshift, 1, &s);
argp->prev_value = argp->prev_elts = Qnil;
}
- v = rb_assoc_new(v, rb_ary_new3(1, i));
+ v = rb_assoc_new(v, rb_ary_new3(1, i));
rb_funcallv(argp->yielder, id_lshift, 1, &v);
}
else if (NIL_P(v) || v == separator) {
if (!NIL_P(argp->prev_value)) {
- v = rb_assoc_new(argp->prev_value, argp->prev_elts);
+ v = rb_assoc_new(argp->prev_value, argp->prev_elts);
rb_funcallv(argp->yielder, id_lshift, 1, &v);
argp->prev_value = argp->prev_elts = Qnil;
}
}
else if (SYMBOL_P(v) && (s = rb_sym2str(v), RSTRING_PTR(s)[0] == '_')) {
- rb_raise(rb_eRuntimeError, "symbols beginning with an underscore are reserved");
+ rb_raise(rb_eRuntimeError, "symbols beginning with an underscore are reserved");
}
else {
if (NIL_P(argp->prev_value)) {
@@ -3413,7 +3800,7 @@ chunk_ii(RB_BLOCK_CALL_FUNC_ARGLIST(i, _argp))
rb_ary_push(argp->prev_elts, i);
}
else {
- s = rb_assoc_new(argp->prev_value, argp->prev_elts);
+ s = rb_assoc_new(argp->prev_value, argp->prev_elts);
rb_funcallv(argp->yielder, id_lshift, 1, &s);
argp->prev_value = v;
argp->prev_elts = rb_ary_new3(1, i);
@@ -3439,58 +3826,86 @@ chunk_i(RB_BLOCK_CALL_FUNC_ARGLIST(yielder, enumerator))
rb_block_call(enumerable, id_each, 0, 0, chunk_ii, arg);
memo = MEMO_FOR(struct chunk_arg, arg);
if (!NIL_P(memo->prev_elts)) {
- arg = rb_assoc_new(memo->prev_value, memo->prev_elts);
- rb_funcallv(memo->yielder, id_lshift, 1, &arg);
+ arg = rb_assoc_new(memo->prev_value, memo->prev_elts);
+ rb_funcallv(memo->yielder, id_lshift, 1, &arg);
}
return Qnil;
}
/*
* call-seq:
- * enum.chunk { |elt| ... } -> an_enumerator
+ * chunk {|array| ... } -> enumerator
*
- * Enumerates over the items, chunking them together based on the return
- * value of the block.
+ * Each element in the returned enumerator is a 2-element array consisting of:
*
- * Consecutive elements which return the same block value are chunked together.
+ * - A value returned by the block.
+ * - An array ("chunk") containing the element for which that value was returned,
+ * and all following elements for which the block returned the same value:
*
- * For example, consecutive even numbers and odd numbers can be
- * chunked as follows.
+ * So that:
*
- * [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5].chunk { |n|
- * n.even?
- * }.each { |even, ary|
- * p [even, ary]
- * }
- * #=> [false, [3, 1]]
- * # [true, [4]]
- * # [false, [1, 5, 9]]
- * # [true, [2, 6]]
- * # [false, [5, 3, 5]]
+ * - Each block return value that is different from its predecessor
+ * begins a new chunk.
+ * - Each block return value that is the same as its predecessor
+ * continues the same chunk.
+ *
+ * Example:
+ *
+ * e = (0..10).chunk {|i| (i / 3).floor } # => #<Enumerator: ...>
+ * # The enumerator elements.
+ * e.next # => [0, [0, 1, 2]]
+ * e.next # => [1, [3, 4, 5]]
+ * e.next # => [2, [6, 7, 8]]
+ * e.next # => [3, [9, 10]]
+ *
+ * \Method +chunk+ is especially useful for an enumerable that is already sorted.
+ * This example counts words for each initial letter in a large array of words:
*
- * This method is especially useful for sorted series of elements.
- * The following example counts words for each initial letter.
+ * # Get sorted words from a web page.
+ * url = 'https://raw.githubusercontent.com/eneko/data-repository/master/data/words.txt'
+ * words = URI::open(url).readlines
+ * # Make chunks, one for each letter.
+ * e = words.chunk {|word| word.upcase[0] } # => #<Enumerator: ...>
+ * # Display 'A' through 'F'.
+ * e.each {|c, words| p [c, words.length]; break if c == 'F' }
*
- * open("/usr/share/dict/words", "r:iso-8859-1") { |f|
- * f.chunk { |line| line.upcase.ord }.each { |ch, lines| p [ch.chr, lines.length] }
+ * Output:
+ *
+ * ["A", 17096]
+ * ["B", 11070]
+ * ["C", 19901]
+ * ["D", 10896]
+ * ["E", 8736]
+ * ["F", 6860]
+ *
+ * You can use the special symbol <tt>:_alone</tt> to force an element
+ * into its own separate chuck:
+ *
+ * a = [0, 0, 1, 1]
+ * e = a.chunk{|i| i.even? ? :_alone : true }
+ * e.to_a # => [[:_alone, [0]], [:_alone, [0]], [true, [1, 1]]]
+ *
+ * For example, you can put each line that contains a URL into its own chunk:
+ *
+ * pattern = /http/
+ * open(filename) { |f|
+ * f.chunk { |line| line =~ pattern ? :_alone : true }.each { |key, lines|
+ * pp lines
+ * }
* }
- * #=> ["\n", 1]
- * # ["A", 1327]
- * # ["B", 1372]
- * # ["C", 1507]
- * # ["D", 791]
- * # ...
*
- * The following key values have special meaning:
- * - +nil+ and +:_separator+ specifies that the elements should be dropped.
- * - +:_alone+ specifies that the element should be chunked by itself.
+ * You can use the special symbol <tt>:_separator</tt> or +nil+
+ * to force an element to be ignored (not included in any chunk):
*
- * Any other symbols that begin with an underscore will raise an error:
+ * a = [0, 0, -1, 1, 1]
+ * e = a.chunk{|i| i < 0 ? :_separator : true }
+ * e.to_a # => [[true, [0, 0]], [true, [1, 1]]]
*
- * items.chunk { |item| :_underscore }
- * #=> RuntimeError: symbols beginning with an underscore are reserved
+ * Note that the separator does end the chunk:
*
- * +nil+ and +:_separator+ can be used to ignore some elements.
+ * a = [0, 0, -1, 1, -1, 1]
+ * e = a.chunk{|i| i < 0 ? :_separator : true }
+ * e.to_a # => [[true, [0, 0]], [true, [1]], [true, [1]]]
*
* For example, the sequence of hyphens in svn log can be eliminated as follows:
*
@@ -3520,18 +3935,6 @@ chunk_i(RB_BLOCK_CALL_FUNC_ARGLIST(yielder, enumerator))
* pp lines
* }
*
- * +:_alone+ can be used to force items into their own chunk.
- * For example, you can put lines that contain a URL by themselves,
- * and chunk the rest of the lines together, like this:
- *
- * pattern = /http/
- * open(filename) { |f|
- * f.chunk { |line| line =~ pattern ? :_alone : true }.each { |key, lines|
- * pp lines
- * }
- * }
- *
- * If no block is given, an enumerator to `chunk` is returned instead.
*/
static VALUE
enum_chunk(VALUE enumerable)
@@ -3604,24 +4007,41 @@ slicebefore_i(RB_BLOCK_CALL_FUNC_ARGLIST(yielder, enumerator))
/*
* call-seq:
- * enum.slice_before(pattern) -> an_enumerator
- * enum.slice_before { |elt| bool } -> an_enumerator
+ * slice_before(pattern) -> enumerator
+ * slice_before {|elt| ... } -> enumerator
*
- * Creates an enumerator for each chunked elements.
- * The beginnings of chunks are defined by _pattern_ and the block.
-
- * If <code>_pattern_ === _elt_</code> returns <code>true</code> or the block
- * returns <code>true</code> for the element, the element is beginning of a
- * chunk.
-
- * The <code>===</code> and _block_ is called from the first element to the last
- * element of _enum_. The result for the first element is ignored.
-
- * The result enumerator yields the chunked elements as an array.
- * So +each+ method can be called as follows:
+ * With argument +pattern+, returns an enumerator that uses the pattern
+ * to partition elements into arrays ("slices").
+ * An element begins a new slice if <tt>element === pattern</tt>
+ * (or if it is the first element).
+ *
+ * a = %w[foo bar fop for baz fob fog bam foy]
+ * e = a.slice_before(/ba/) # => #<Enumerator: ...>
+ * e.each {|array| p array }
*
- * enum.slice_before(pattern).each { |ary| ... }
- * enum.slice_before { |elt| bool }.each { |ary| ... }
+ * Output:
+ *
+ * ["foo"]
+ * ["bar", "fop", "for"]
+ * ["baz", "fob", "fog"]
+ * ["bam", "foy"]
+ *
+ * With a block, returns an enumerator that uses the block
+ * to partition elements into arrays.
+ * An element begins a new slice if its block return is a truthy value
+ * (or if it is the first element):
+ *
+ * e = (1..20).slice_before {|i| i % 4 == 2 } # => #<Enumerator: ...>
+ * e.each {|array| p array }
+ *
+ * Output:
+ *
+ * [1]
+ * [2, 3, 4, 5]
+ * [6, 7, 8, 9]
+ * [10, 11, 12, 13]
+ * [14, 15, 16, 17]
+ * [18, 19, 20]
*
* Other methods of the Enumerator class and Enumerable module,
* such as +to_a+, +map+, etc., are also usable.
@@ -3639,7 +4059,6 @@ slicebefore_i(RB_BLOCK_CALL_FUNC_ARGLIST(yielder, enumerator))
* f.slice_before { |line| /\A\S/ === line }.each { |e| pp e }
* }
*
- *
* "svn proplist -R" produces multiline output for each file.
* They can be chunked as follows:
*
@@ -3910,15 +4329,15 @@ slicewhen_ii(RB_BLOCK_CALL_FUNC_ARGLIST(i, _memo))
ENUM_WANT_SVALUE();
- if (memo->prev_elt == Qundef) {
+ if (UNDEF_P(memo->prev_elt)) {
/* The first element */
memo->prev_elt = i;
memo->prev_elts = rb_ary_new3(1, i);
}
else {
- VALUE args[2];
- args[0] = memo->prev_elt;
- args[1] = i;
+ VALUE args[2];
+ args[0] = memo->prev_elt;
+ args[1] = i;
split_p = RTEST(rb_funcallv(memo->pred, id_call, 2, args));
UPDATE_MEMO;
@@ -3947,7 +4366,7 @@ slicewhen_i(RB_BLOCK_CALL_FUNC_ARGLIST(yielder, enumerator))
VALUE enumerable;
VALUE arg;
struct slicewhen_arg *memo =
- NEW_PARTIAL_MEMO_FOR(struct slicewhen_arg, arg, inverted);
+ NEW_PARTIAL_MEMO_FOR(struct slicewhen_arg, arg, inverted);
enumerable = rb_ivar_get(enumerator, id_slicewhen_enum);
memo->pred = rb_attr_get(enumerator, id_slicewhen_pred);
@@ -4119,7 +4538,7 @@ struct enum_sum_memo {
static void
sum_iter_normalize_memo(struct enum_sum_memo *memo)
{
- assert(FIXABLE(memo->n));
+ RUBY_ASSERT(FIXABLE(memo->n));
memo->v = rb_fix_plus(LONG2FIX(memo->n), memo->v);
memo->n = 0;
@@ -4150,7 +4569,7 @@ sum_iter_bignum(VALUE i, struct enum_sum_memo *memo)
static void
sum_iter_rational(VALUE i, struct enum_sum_memo *memo)
{
- if (memo->r == Qundef) {
+ if (UNDEF_P(memo->r)) {
memo->r = i;
}
else {
@@ -4221,7 +4640,7 @@ sum_iter_Kahan_Babuska(VALUE i, struct enum_sum_memo *memo)
static void
sum_iter(VALUE i, struct enum_sum_memo *memo)
{
- assert(memo != NULL);
+ RUBY_ASSERT(memo != NULL);
if (memo->block_given) {
i = rb_yield(i);
}
@@ -4272,8 +4691,8 @@ hash_sum_i(VALUE key, VALUE value, VALUE arg)
static void
hash_sum(VALUE hash, struct enum_sum_memo *memo)
{
- assert(RB_TYPE_P(hash, T_HASH));
- assert(memo != NULL);
+ RUBY_ASSERT(RB_TYPE_P(hash, T_HASH));
+ RUBY_ASSERT(memo != NULL);
rb_hash_foreach(hash, hash_sum_i, (VALUE)memo);
}
@@ -4300,36 +4719,35 @@ int_range_sum(VALUE beg, VALUE end, int excl, VALUE init)
}
/*
- * call-seq:
- * enum.sum(init=0) -> number
- * enum.sum(init=0) {|e| expr } -> number
- *
- * Returns the sum of elements in an Enumerable.
+ * call-seq:
+ * sum(initial_value = 0) -> number
+ * sum(initial_value = 0) {|element| ... } -> object
*
- * If a block is given, the block is applied to each element
- * before addition.
+ * With no block given,
+ * returns the sum of +initial_value+ and the elements:
*
- * If <i>enum</i> is empty, it returns <i>init</i>.
+ * (1..100).sum # => 5050
+ * (1..100).sum(1) # => 5051
+ * ('a'..'d').sum('foo') # => "fooabcd"
*
- * For example:
+ * Generally, the sum is computed using methods <tt>+</tt> and +each+;
+ * for performance optimizations, those methods may not be used,
+ * and so any redefinition of those methods may not have effect here.
*
- * { 1 => 10, 2 => 20 }.sum {|k, v| k * v } #=> 50
- * (1..10).sum #=> 55
- * (1..10).sum {|v| v * 2 } #=> 110
- * ('a'..'z').sum #=> TypeError
+ * One such optimization: When possible, computes using Gauss's summation
+ * formula <em>n(n+1)/2</em>:
*
- * This method can be used for non-numeric objects by
- * explicit <i>init</i> argument.
+ * 100 * (100 + 1) / 2 # => 5050
*
- * { 1 => 10, 2 => 20 }.sum([]) #=> [1, 10, 2, 20]
- * "a\nb\nc".each_line.lazy.map(&:chomp).sum("") #=> "abc"
+ * With a block given, calls the block with each element;
+ * returns the sum of +initial_value+ and the block return values:
*
- * If the method is applied to an Integer range without a block,
- * the sum is not done by iteration, but instead using Gauss's summation
- * formula.
+ * (1..4).sum {|i| i*i } # => 30
+ * (1..4).sum(100) {|i| i*i } # => 130
+ * h = {a: 0, b: 1, c: 2, d: 3, e: 4, f: 5}
+ * h.sum {|key, value| value.odd? ? value : 0 } # => 9
+ * ('a'..'f').sum('x') {|c| c < 'd' ? c : '' } # => "xabc"
*
- * Enumerable#sum method may not respect method redefinition of "+"
- * methods such as Integer#+, or "each" methods such as Range#each.
*/
static VALUE
enum_sum(int argc, VALUE* argv, VALUE obj)
@@ -4372,7 +4790,7 @@ enum_sum(int argc, VALUE* argv, VALUE obj)
else {
if (memo.n != 0)
memo.v = rb_fix_plus(LONG2FIX(memo.n), memo.v);
- if (memo.r != Qundef) {
+ if (!UNDEF_P(memo.r)) {
memo.v = rb_rational_plus(memo.r, memo.v);
}
return memo.v;
@@ -4397,12 +4815,23 @@ uniq_iter(RB_BLOCK_CALL_FUNC_ARGLIST(i, hash))
/*
* call-seq:
- * enum.uniq -> new_ary
- * enum.uniq { |item| ... } -> new_ary
+ * uniq -> array
+ * uniq {|element| ... } -> array
*
- * Returns a new array by removing duplicate values in +self+.
+ * With no block, returns a new array containing only unique elements;
+ * the array has no two elements +e0+ and +e1+ such that <tt>e0.eql?(e1)</tt>:
+ *
+ * %w[a b c c b a a b c].uniq # => ["a", "b", "c"]
+ * [0, 1, 2, 2, 1, 0, 0, 1, 2].uniq # => [0, 1, 2]
+ *
+ * With a block, returns a new array containing elements only for which the block
+ * returns a unique value:
+ *
+ * a = [0, 1, 2, 3, 4, 5, 5, 4, 3, 2, 1]
+ * a.uniq {|i| i.even? ? i : 0 } # => [0, 2, 4]
+ * a = %w[a b c d e e d c b a a b c d e]
+ * a.uniq {|c| c < 'c' } # => ["a", "c"]
*
- * See also Array#uniq.
*/
static VALUE
@@ -4410,7 +4839,7 @@ enum_uniq(VALUE obj)
{
VALUE hash, ret;
rb_block_call_func *const func =
- rb_block_given_p() ? uniq_iter : uniq_func;
+ rb_block_given_p() ? uniq_iter : uniq_func;
hash = rb_obj_hide(rb_hash_new());
rb_block_call(obj, id_each, 0, 0, func, hash);
@@ -4432,21 +4861,13 @@ compact_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, ary))
/*
* call-seq:
- * enum.compact -> array
+ * compact -> array
*
- * Returns an array of all non-+nil+ elements from enumeration.
- *
- * def with_nils
- * yield 1
- * yield 2
- * yield nil
- * yield 3
- * end
+ * Returns an array of all non-+nil+ elements:
*
- * to_enum(:with_nils).compact
- * # => [1, 2, 3]
+ * a = [nil, 0, nil, 'a', false, nil, false, nil, 'a', nil, 0, nil]
+ * a.compact # => [0, "a", false, false, "a", 0]
*
- * See also Array#compact.
*/
static VALUE
@@ -4465,132 +4886,173 @@ enum_compact(VALUE obj)
* == What's Here
*
* \Module \Enumerable provides methods that are useful to a collection class for:
- * - {Querying}[#module-Enumerable-label-Methods+for+Querying]
- * - {Fetching}[#module-Enumerable-label-Methods+for+Fetching]
- * - {Searching}[#module-Enumerable-label-Methods+for+Searching]
- * - {Sorting}[#module-Enumerable-label-Methods+for+Sorting]
- * - {Iterating}[#module-Enumerable-label-Methods+for+Iterating]
- * - {And more....}[#module-Enumerable-label-Other+Methods]
+ *
+ * - {Querying}[rdoc-ref:Enumerable@Methods+for+Querying]
+ * - {Fetching}[rdoc-ref:Enumerable@Methods+for+Fetching]
+ * - {Searching and Filtering}[rdoc-ref:Enumerable@Methods+for+Searching+and+Filtering]
+ * - {Sorting}[rdoc-ref:Enumerable@Methods+for+Sorting]
+ * - {Iterating}[rdoc-ref:Enumerable@Methods+for+Iterating]
+ * - {And more....}[rdoc-ref:Enumerable@Other+Methods]
*
* === Methods for Querying
*
* These methods return information about the \Enumerable other than the elements themselves:
*
- * #include?, #member?:: Returns +true+ if self == object, +false+ otherwise.
- * #all?:: Returns +true+ if all elements meet a specified criterion; +false+ otherwise.
- * #any?:: Returns +true+ if any element meets a specified criterion; +false+ otherwise.
- * #none?:: Returns +true+ if no element meets a specified criterion; +false+ otherwise.
- * #one?:: Returns +true+ if exactly one element meets a specified criterion; +false+ otherwise.
- * #count:: Returns the count of elements,
- * based on an argument or block criterion, if given.
- * #tally:: Returns a new \Hash containing the counts of occurrences of each element.
+ * - #include?, #member?: Returns +true+ if <tt>self == object</tt>, +false+ otherwise.
+ * - #all?: Returns +true+ if all elements meet a specified criterion; +false+ otherwise.
+ * - #any?: Returns +true+ if any element meets a specified criterion; +false+ otherwise.
+ * - #none?: Returns +true+ if no element meets a specified criterion; +false+ otherwise.
+ * - #one?: Returns +true+ if exactly one element meets a specified criterion; +false+ otherwise.
+ * - #count: Returns the count of elements,
+ * based on an argument or block criterion, if given.
+ * - #tally: Returns a new Hash containing the counts of occurrences of each element.
*
* === Methods for Fetching
*
* These methods return entries from the \Enumerable, without modifying it:
*
* <i>Leading, trailing, or all elements</i>:
- * #entries, #to_a:: Returns all elements.
- * #first:: Returns the first element or leading elements.
- * #take:: Returns a specified number of leading elements.
- * #drop:: Returns a specified number of trailing elements.
- * #take_while:: Returns leading elements as specified by the given block.
- * #drop_while:: Returns trailing elements as specified by the given block.
+ *
+ * - #entries, #to_a: Returns all elements.
+ * - #first: Returns the first element or leading elements.
+ * - #take: Returns a specified number of leading elements.
+ * - #drop: Returns a specified number of trailing elements.
+ * - #take_while: Returns leading elements as specified by the given block.
+ * - #drop_while: Returns trailing elements as specified by the given block.
*
* <i>Minimum and maximum value elements</i>:
- * #min:: Returns the elements whose values are smallest among the elements,
- * as determined by <tt><=></tt> or a given block.
- * #max:: Returns the elements whose values are largest among the elements,
- * as determined by <tt><=></tt> or a given block.
- * #minmax:: Returns a 2-element \Array containing the smallest and largest elements.
- * #min_by:: Returns the smallest element, as determined by the given block.
- * #max_by:: Returns the largest element, as determined by the given block.
- * #minmax_by:: Returns the smallest and largest elements, as determined by the given block.
+ *
+ * - #min: Returns the elements whose values are smallest among the elements,
+ * as determined by <tt><=></tt> or a given block.
+ * - #max: Returns the elements whose values are largest among the elements,
+ * as determined by <tt><=></tt> or a given block.
+ * - #minmax: Returns a 2-element Array containing the smallest and largest elements.
+ * - #min_by: Returns the smallest element, as determined by the given block.
+ * - #max_by: Returns the largest element, as determined by the given block.
+ * - #minmax_by: Returns the smallest and largest elements, as determined by the given block.
*
* <i>Groups, slices, and partitions</i>:
- * #group_by:: Returns a \Hash that partitions the elements into groups.
- * #partition:: Returns elements partitioned into two new Arrays, as determined by the given block.
- * #slice_after:: Returns a new \Enumerator whose entries are a partition of +self+,
- based either on a given +object+ or a given block.
- * #slice_before:: Returns a new \Enumerator whose entries are a partition of +self+,
- based either on a given +object+ or a given block.
- * #slice_when:: Returns a new \Enumerator whose entries are a partition of +self+
- based on the given block.
- * #chunk:: Returns elements organized into chunks as specified by the given block.
- * #chunk_while:: Returns elements organized into chunks as specified by the given block.
+ *
+ * - #group_by: Returns a Hash that partitions the elements into groups.
+ * - #partition: Returns elements partitioned into two new Arrays, as determined by the given block.
+ * - #slice_after: Returns a new Enumerator whose entries are a partition of +self+,
+ * based either on a given +object+ or a given block.
+ * - #slice_before: Returns a new Enumerator whose entries are a partition of +self+,
+ * based either on a given +object+ or a given block.
+ * - #slice_when: Returns a new Enumerator whose entries are a partition of +self+
+ * based on the given block.
+ * - #chunk: Returns elements organized into chunks as specified by the given block.
+ * - #chunk_while: Returns elements organized into chunks as specified by the given block.
*
* === Methods for Searching and Filtering
*
- * These methods return elements that meet a specified criterion.
+ * These methods return elements that meet a specified criterion:
*
- * #find, #detect:: Returns an element selected by the block.
- * #find_all, #filter, #select:: Returns elements selected by the block.
- * #find_index:: Returns the index of an element selected by a given object or block.
- * #reject:: Returns elements not rejected by the block.
- * #uniq:: Returns elements that are not duplicates.
+ * - #find, #detect: Returns an element selected by the block.
+ * - #find_all, #filter, #select: Returns elements selected by the block.
+ * - #find_index: Returns the index of an element selected by a given object or block.
+ * - #reject: Returns elements not rejected by the block.
+ * - #uniq: Returns elements that are not duplicates.
*
* === Methods for Sorting
*
- * These methods return elements in sorted order.
+ * These methods return elements in sorted order:
*
- * #sort:: Returns the elements, sorted by <tt><=></tt> or the given block.
- * #sort_by:: Returns the elements, sorted by the given block.
+ * - #sort: Returns the elements, sorted by <tt><=></tt> or the given block.
+ * - #sort_by: Returns the elements, sorted by the given block.
*
* === Methods for Iterating
*
- * #each_entry:: Calls the block with each successive element
- * (slightly different from #each).
- * #each_with_index:: Calls the block with each successive element and its index.
- * #each_with_object:: Calls the block with each successive element and a given object.
- * #each_slice:: Calls the block with successive non-overlapping slices.
- * #each_cons:: Calls the block with successive overlapping slices.
- * (different from #each_slice).
- * #reverse_each:: Calls the block with each successive element, in reverse order.
+ * - #each_entry: Calls the block with each successive element
+ * (slightly different from #each).
+ * - #each_with_index: Calls the block with each successive element and its index.
+ * - #each_with_object: Calls the block with each successive element and a given object.
+ * - #each_slice: Calls the block with successive non-overlapping slices.
+ * - #each_cons: Calls the block with successive overlapping slices.
+ * (different from #each_slice).
+ * - #reverse_each: Calls the block with each successive element, in reverse order.
*
* === Other Methods
*
- * #map, #collect:: Returns objects returned by the block.
- * #filter_map:: Returns truthy objects returned by the block.
- * #flat_map, #collect_concat:: Returns flattened objects returned by the block.
- * #grep:: Returns elements selected by a given object
- * or objects returned by a given block.
- * #grep_v:: Returns elements selected by a given object
- * or objects returned by a given block.
- * #reduce, #inject:: Returns the object formed by combining all elements.
- * #sum:: Returns the sum of the elements, using method +++.
- * #zip:: Combines each element with elements from other enumerables;
- * returns the n-tuples or calls the block with each.
- * #cycle:: Calls the block with each element, cycling repeatedly.
+ * - #map, #collect: Returns objects returned by the block.
+ * - #filter_map: Returns truthy objects returned by the block.
+ * - #flat_map, #collect_concat: Returns flattened objects returned by the block.
+ * - #grep: Returns elements selected by a given object
+ * or objects returned by a given block.
+ * - #grep_v: Returns elements selected by a given object
+ * or objects returned by a given block.
+ * - #reduce, #inject: Returns the object formed by combining all elements.
+ * - #sum: Returns the sum of the elements, using method <tt>+</tt>.
+ * - #zip: Combines each element with elements from other enumerables;
+ * returns the n-tuples or calls the block with each.
+ * - #cycle: Calls the block with each element, cycling repeatedly.
*
* == Usage
*
* To use module \Enumerable in a collection class:
+ *
* - Include it:
+ *
* include Enumerable
+ *
* - Implement method <tt>#each</tt>
* which must yield successive elements of the collection.
- * This method will be called by almost any \Enumerable method.
+ * The method will be called by almost any \Enumerable method.
+ *
+ * Example:
+ *
+ * class Foo
+ * include Enumerable
+ * def each
+ * yield 1
+ * yield 1, 2
+ * yield
+ * end
+ * end
+ * Foo.new.each_entry{ |element| p element }
+ *
+ * Output:
+ *
+ * 1
+ * [1, 2]
+ * nil
+ *
+ * == \Enumerable in Ruby Classes
*
- * == \Enumerable in Ruby Core Classes
- * Some Ruby classes include \Enumerable:
+ * These Ruby core classes include (or extend) \Enumerable:
+ *
+ * - ARGF
* - Array
* - Dir
+ * - Enumerator
+ * - ENV (extends)
* - Hash
* - IO
* - Range
- * - Set
* - Struct
+ *
+ * These Ruby standard library classes include \Enumerable:
+ *
+ * - CSV
+ * - CSV::Table
+ * - CSV::Row
+ * - Set
+ *
* Virtually all methods in \Enumerable call method +#each+ in the including class:
- * - <tt>Hash#each</tt> yields the next key-value pair as a 2-element \Array.
- * - <tt>Struct#each</tt> yields the next name-value pair as a 2-element \Array.
+ *
+ * - <tt>Hash#each</tt> yields the next key-value pair as a 2-element Array.
+ * - <tt>Struct#each</tt> yields the next name-value pair as a 2-element Array.
* - For the other classes above, +#each+ yields the next object from the collection.
*
* == About the Examples
+ *
* The example code snippets for the \Enumerable methods:
- * - Always show the use of one or more \Array-like classes (often \Array itself).
- * - Sometimes show the use of a \Hash-like class.
+ *
+ * - Always show the use of one or more Array-like classes (often Array itself).
+ * - Sometimes show the use of a Hash-like class.
* For some methods, though, the usage would not make sense,
- * and so it is not shown. Example: #tally would find exactly one of each \Hash entry.
+ * and so it is not shown. Example: #tally would find exactly one of each Hash entry.
+ *
*/
void