Ruby 3.5.0dev (2025-06-27 revision 8bba087ae567421cd574b5b5772fd3039ff39b4d)
enum.c (8bba087ae567421cd574b5b5772fd3039ff39b4d)
1/**********************************************************************
2
3 enum.c -
4
5 $Author$
6 created at: Fri Oct 1 15:15:19 JST 1993
7
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
9
10**********************************************************************/
11
12#include "id.h"
13#include "internal.h"
14#include "internal/compar.h"
15#include "internal/enum.h"
16#include "internal/hash.h"
17#include "internal/imemo.h"
18#include "internal/numeric.h"
19#include "internal/object.h"
20#include "internal/proc.h"
21#include "internal/rational.h"
22#include "internal/re.h"
23#include "ruby/util.h"
24#include "ruby_assert.h"
25#include "symbol.h"
26
28
29static ID id_next;
30static ID id__alone;
31static ID id__separator;
32static ID id_chunk_categorize;
33static ID id_chunk_enumerable;
34static ID id_sliceafter_enum;
35static ID id_sliceafter_pat;
36static ID id_sliceafter_pred;
37static ID id_slicebefore_enumerable;
38static ID id_slicebefore_sep_pat;
39static ID id_slicebefore_sep_pred;
40static ID id_slicewhen_enum;
41static ID id_slicewhen_inverted;
42static ID id_slicewhen_pred;
43
44#define id_div idDiv
45#define id_each idEach
46#define id_eqq idEqq
47#define id_cmp idCmp
48#define id_lshift idLTLT
49#define id_call idCall
50#define id_size idSize
51
53rb_enum_values_pack(int argc, const VALUE *argv)
54{
55 if (argc == 0) return Qnil;
56 if (argc == 1) return argv[0];
57 return rb_ary_new4(argc, argv);
58}
59
60#define ENUM_WANT_SVALUE() do { \
61 i = rb_enum_values_pack(argc, argv); \
62} while (0)
63
64static VALUE
65enum_yield(int argc, VALUE ary)
66{
67 if (argc > 1)
68 return rb_yield_force_blockarg(ary);
69 if (argc == 1)
70 return rb_yield(ary);
71 return rb_yield_values2(0, 0);
72}
73
74static VALUE
75enum_yield_array(VALUE ary)
76{
77 long len = RARRAY_LEN(ary);
78
79 if (len > 1)
80 return rb_yield_force_blockarg(ary);
81 if (len == 1)
82 return rb_yield(RARRAY_AREF(ary, 0));
83 return rb_yield_values2(0, 0);
84}
85
86static VALUE
87grep_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
88{
89 struct MEMO *memo = MEMO_CAST(args);
90 ENUM_WANT_SVALUE();
91
92 if (RTEST(rb_funcallv(memo->v1, id_eqq, 1, &i)) == RTEST(memo->u3.value)) {
93 rb_ary_push(memo->v2, i);
94 }
95 return Qnil;
96}
97
98static VALUE
99grep_regexp_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
100{
101 struct MEMO *memo = MEMO_CAST(args);
102 VALUE converted_element, match;
103 ENUM_WANT_SVALUE();
104
105 /* In case element can't be converted to a Symbol or String: not a match (don't raise) */
106 converted_element = SYMBOL_P(i) ? i : rb_check_string_type(i);
107 match = NIL_P(converted_element) ? Qfalse : rb_reg_match_p(memo->v1, i, 0);
108 if (match == memo->u3.value) {
109 rb_ary_push(memo->v2, i);
110 }
111 return Qnil;
112}
113
114static VALUE
115grep_iter_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
116{
117 struct MEMO *memo = MEMO_CAST(args);
118 ENUM_WANT_SVALUE();
119
120 if (RTEST(rb_funcallv(memo->v1, id_eqq, 1, &i)) == RTEST(memo->u3.value)) {
121 rb_ary_push(memo->v2, enum_yield(argc, i));
122 }
123 return Qnil;
124}
125
126static VALUE
127enum_grep0(VALUE obj, VALUE pat, VALUE test)
128{
129 VALUE ary = rb_ary_new();
130 struct MEMO *memo = MEMO_NEW(pat, ary, test);
132 if (rb_block_given_p()) {
133 fn = grep_iter_i;
134 }
135 else if (RB_TYPE_P(pat, T_REGEXP) &&
136 LIKELY(rb_method_basic_definition_p(CLASS_OF(pat), idEqq))) {
137 fn = grep_regexp_i;
138 }
139 else {
140 fn = grep_i;
141 }
142 rb_block_call(obj, id_each, 0, 0, fn, (VALUE)memo);
143
144 return ary;
145}
146
147/*
148 * call-seq:
149 * grep(pattern) -> array
150 * grep(pattern) {|element| ... } -> array
151 *
152 * Returns an array of objects based elements of +self+ that match the given pattern.
153 *
154 * With no block given, returns an array containing each element
155 * for which <tt>pattern === element</tt> is +true+:
156 *
157 * a = ['foo', 'bar', 'car', 'moo']
158 * a.grep(/ar/) # => ["bar", "car"]
159 * (1..10).grep(3..8) # => [3, 4, 5, 6, 7, 8]
160 * ['a', 'b', 0, 1].grep(Integer) # => [0, 1]
161 *
162 * With a block given,
163 * calls the block with each matching element and returns an array containing each
164 * object returned by the block:
165 *
166 * a = ['foo', 'bar', 'car', 'moo']
167 * a.grep(/ar/) {|element| element.upcase } # => ["BAR", "CAR"]
168 *
169 * Related: #grep_v.
170 */
171
172static VALUE
173enum_grep(VALUE obj, VALUE pat)
174{
175 return enum_grep0(obj, pat, Qtrue);
176}
177
178/*
179 * call-seq:
180 * grep_v(pattern) -> array
181 * grep_v(pattern) {|element| ... } -> array
182 *
183 * Returns an array of objects based on elements of +self+
184 * that <em>don't</em> match the given pattern.
185 *
186 * With no block given, returns an array containing each element
187 * for which <tt>pattern === element</tt> is +false+:
188 *
189 * a = ['foo', 'bar', 'car', 'moo']
190 * a.grep_v(/ar/) # => ["foo", "moo"]
191 * (1..10).grep_v(3..8) # => [1, 2, 9, 10]
192 * ['a', 'b', 0, 1].grep_v(Integer) # => ["a", "b"]
193 *
194 * With a block given,
195 * calls the block with each non-matching element and returns an array containing each
196 * object returned by the block:
197 *
198 * a = ['foo', 'bar', 'car', 'moo']
199 * a.grep_v(/ar/) {|element| element.upcase } # => ["FOO", "MOO"]
200 *
201 * Related: #grep.
202 */
203
204static VALUE
205enum_grep_v(VALUE obj, VALUE pat)
206{
207 return enum_grep0(obj, pat, Qfalse);
208}
209
210#define COUNT_BIGNUM IMEMO_FL_USER0
211#define MEMO_V3_SET(m, v) RB_OBJ_WRITE((m), &(m)->u3.value, (v))
212
213static void
214imemo_count_up(struct MEMO *memo)
215{
216 if (memo->flags & COUNT_BIGNUM) {
217 MEMO_V3_SET(memo, rb_int_succ(memo->u3.value));
218 }
219 else if (++memo->u3.cnt == 0) {
220 /* overflow */
221 unsigned long buf[2] = {0, 1};
222 MEMO_V3_SET(memo, rb_big_unpack(buf, 2));
223 memo->flags |= COUNT_BIGNUM;
224 }
225}
226
227static VALUE
228imemo_count_value(struct MEMO *memo)
229{
230 if (memo->flags & COUNT_BIGNUM) {
231 return memo->u3.value;
232 }
233 else {
234 return ULONG2NUM(memo->u3.cnt);
235 }
236}
237
238static VALUE
239count_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, memop))
240{
241 struct MEMO *memo = MEMO_CAST(memop);
242
243 ENUM_WANT_SVALUE();
244
245 if (rb_equal(i, memo->v1)) {
246 imemo_count_up(memo);
247 }
248 return Qnil;
249}
250
251static VALUE
252count_iter_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, memop))
253{
254 struct MEMO *memo = MEMO_CAST(memop);
255
256 if (RTEST(rb_yield_values2(argc, argv))) {
257 imemo_count_up(memo);
258 }
259 return Qnil;
260}
261
262static VALUE
263count_all_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, memop))
264{
265 struct MEMO *memo = MEMO_CAST(memop);
266
267 imemo_count_up(memo);
268 return Qnil;
269}
270
271/*
272 * call-seq:
273 * count -> integer
274 * count(object) -> integer
275 * count {|element| ... } -> integer
276 *
277 * Returns the count of elements, based on an argument or block criterion, if given.
278 *
279 * With no argument and no block given, returns the number of elements:
280 *
281 * [0, 1, 2].count # => 3
282 * {foo: 0, bar: 1, baz: 2}.count # => 3
283 *
284 * With argument +object+ given,
285 * returns the number of elements that are <tt>==</tt> to +object+:
286 *
287 * [0, 1, 2, 1].count(1) # => 2
288 *
289 * With a block given, calls the block with each element
290 * and returns the number of elements for which the block returns a truthy value:
291 *
292 * [0, 1, 2, 3].count {|element| element < 2} # => 2
293 * {foo: 0, bar: 1, baz: 2}.count {|key, value| value < 2} # => 2
294 *
295 */
296
297static VALUE
298enum_count(int argc, VALUE *argv, VALUE obj)
299{
300 VALUE item = Qnil;
301 struct MEMO *memo;
302 rb_block_call_func *func;
303
304 if (argc == 0) {
305 if (rb_block_given_p()) {
306 func = count_iter_i;
307 }
308 else {
309 func = count_all_i;
310 }
311 }
312 else {
313 rb_scan_args(argc, argv, "1", &item);
314 if (rb_block_given_p()) {
315 rb_warn("given block not used");
316 }
317 func = count_i;
318 }
319
320 memo = MEMO_NEW(item, 0, 0);
321 rb_block_call(obj, id_each, 0, 0, func, (VALUE)memo);
322 return imemo_count_value(memo);
323}
324
325NORETURN(static void found(VALUE i, VALUE memop));
326static void
327found(VALUE i, VALUE memop)
328{
329 struct MEMO *memo = MEMO_CAST(memop);
330 MEMO_V1_SET(memo, i);
331 memo->u3.cnt = 1;
333}
334
335static VALUE
336find_i_fast(RB_BLOCK_CALL_FUNC_ARGLIST(i, memop))
337{
338 if (RTEST(rb_yield_values2(argc, argv))) {
339 ENUM_WANT_SVALUE();
340 found(i, memop);
341 }
342 return Qnil;
343}
344
345static VALUE
346find_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, memop))
347{
348 ENUM_WANT_SVALUE();
349
350 if (RTEST(enum_yield(argc, i))) {
351 found(i, memop);
352 }
353 return Qnil;
354}
355
356/*
357 * call-seq:
358 * find(if_none_proc = nil) {|element| ... } -> object or nil
359 * find(if_none_proc = nil) -> enumerator
360 *
361 * Returns the first element for which the block returns a truthy value.
362 *
363 * With a block given, calls the block with successive elements of the collection;
364 * returns the first element for which the block returns a truthy value:
365 *
366 * (0..9).find {|element| element > 2} # => 3
367 *
368 * If no such element is found, calls +if_none_proc+ and returns its return value.
369 *
370 * (0..9).find(proc {false}) {|element| element > 12} # => false
371 * {foo: 0, bar: 1, baz: 2}.find {|key, value| key.start_with?('b') } # => [:bar, 1]
372 * {foo: 0, bar: 1, baz: 2}.find(proc {[]}) {|key, value| key.start_with?('c') } # => []
373 *
374 * With no block given, returns an Enumerator.
375 *
376 */
377static VALUE
378enum_find(int argc, VALUE *argv, VALUE obj)
379{
380 struct MEMO *memo;
381 VALUE if_none;
382
383 if_none = rb_check_arity(argc, 0, 1) ? argv[0] : Qnil;
384 RETURN_ENUMERATOR(obj, argc, argv);
385 memo = MEMO_NEW(Qundef, 0, 0);
386 if (rb_block_pair_yield_optimizable())
387 rb_block_call2(obj, id_each, 0, 0, find_i_fast, (VALUE)memo, RB_BLOCK_NO_USE_PACKED_ARGS);
388 else
389 rb_block_call2(obj, id_each, 0, 0, find_i, (VALUE)memo, RB_BLOCK_NO_USE_PACKED_ARGS);
390 if (memo->u3.cnt) {
391 return memo->v1;
392 }
393 if (!NIL_P(if_none)) {
394 return rb_funcallv(if_none, id_call, 0, 0);
395 }
396 return Qnil;
397}
398
399static VALUE
400find_index_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, memop))
401{
402 struct MEMO *memo = MEMO_CAST(memop);
403
404 ENUM_WANT_SVALUE();
405
406 if (rb_equal(i, memo->v2)) {
407 MEMO_V1_SET(memo, imemo_count_value(memo));
409 }
410 imemo_count_up(memo);
411 return Qnil;
412}
413
414static VALUE
415find_index_iter_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, memop))
416{
417 struct MEMO *memo = MEMO_CAST(memop);
418
419 if (RTEST(rb_yield_values2(argc, argv))) {
420 MEMO_V1_SET(memo, imemo_count_value(memo));
422 }
423 imemo_count_up(memo);
424 return Qnil;
425}
426
427/*
428 * call-seq:
429 * find_index(object) -> integer or nil
430 * find_index {|element| ... } -> integer or nil
431 * find_index -> enumerator
432 *
433 * Returns the index of the first element that meets a specified criterion,
434 * or +nil+ if no such element is found.
435 *
436 * With argument +object+ given,
437 * returns the index of the first element that is <tt>==</tt> +object+:
438 *
439 * ['a', 'b', 'c', 'b'].find_index('b') # => 1
440 *
441 * With a block given, calls the block with successive elements;
442 * returns the first element for which the block returns a truthy value:
443 *
444 * ['a', 'b', 'c', 'b'].find_index {|element| element.start_with?('b') } # => 1
445 * {foo: 0, bar: 1, baz: 2}.find_index {|key, value| value > 1 } # => 2
446 *
447 * With no argument and no block given, returns an Enumerator.
448 *
449 */
450
451static VALUE
452enum_find_index(int argc, VALUE *argv, VALUE obj)
453{
454 struct MEMO *memo; /* [return value, current index, ] */
455 VALUE condition_value = Qnil;
456 rb_block_call_func *func;
457
458 if (argc == 0) {
459 RETURN_ENUMERATOR(obj, 0, 0);
460 func = find_index_iter_i;
461 }
462 else {
463 rb_scan_args(argc, argv, "1", &condition_value);
464 if (rb_block_given_p()) {
465 rb_warn("given block not used");
466 }
467 func = find_index_i;
468 }
469
470 memo = MEMO_NEW(Qnil, condition_value, 0);
471 rb_block_call(obj, id_each, 0, 0, func, (VALUE)memo);
472 return memo->v1;
473}
474
475static VALUE
476find_all_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, ary))
477{
478 ENUM_WANT_SVALUE();
479
480 if (RTEST(enum_yield(argc, i))) {
481 rb_ary_push(ary, i);
482 }
483 return Qnil;
484}
485
486static VALUE
487enum_size(VALUE self, VALUE args, VALUE eobj)
488{
489 return rb_check_funcall_default(self, id_size, 0, 0, Qnil);
490}
491
492static long
493limit_by_enum_size(VALUE obj, long n)
494{
495 unsigned long limit;
496 VALUE size = rb_check_funcall(obj, id_size, 0, 0);
497 if (!FIXNUM_P(size)) return n;
498 limit = FIX2ULONG(size);
499 return ((unsigned long)n > limit) ? (long)limit : n;
500}
501
502static int
503enum_size_over_p(VALUE obj, long n)
504{
505 VALUE size = rb_check_funcall(obj, id_size, 0, 0);
506 if (!FIXNUM_P(size)) return 0;
507 return ((unsigned long)n > FIX2ULONG(size));
508}
509
510/*
511 * call-seq:
512 * select {|element| ... } -> array
513 * select -> enumerator
514 *
515 * Returns an array containing elements selected by the block.
516 *
517 * With a block given, calls the block with successive elements;
518 * returns an array of those elements for which the block returns a truthy value:
519 *
520 * (0..9).select {|element| element % 3 == 0 } # => [0, 3, 6, 9]
521 * a = {foo: 0, bar: 1, baz: 2}.select {|key, value| key.start_with?('b') }
522 * a # => {:bar=>1, :baz=>2}
523 *
524 * With no block given, returns an Enumerator.
525 *
526 * Related: #reject.
527 */
528static VALUE
529enum_find_all(VALUE obj)
530{
531 VALUE ary;
532
533 RETURN_SIZED_ENUMERATOR(obj, 0, 0, enum_size);
534
535 ary = rb_ary_new();
536 rb_block_call(obj, id_each, 0, 0, find_all_i, ary);
537
538 return ary;
539}
540
541static VALUE
542filter_map_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, ary))
543{
544 i = rb_yield_values2(argc, argv);
545
546 if (RTEST(i)) {
547 rb_ary_push(ary, i);
548 }
549
550 return Qnil;
551}
552
553/*
554 * call-seq:
555 * filter_map {|element| ... } -> array
556 * filter_map -> enumerator
557 *
558 * Returns an array containing truthy elements returned by the block.
559 *
560 * With a block given, calls the block with successive elements;
561 * returns an array containing each truthy value returned by the block:
562 *
563 * (0..9).filter_map {|i| i * 2 if i.even? } # => [0, 4, 8, 12, 16]
564 * {foo: 0, bar: 1, baz: 2}.filter_map {|key, value| key if value.even? } # => [:foo, :baz]
565 *
566 * When no block given, returns an Enumerator.
567 *
568 */
569static VALUE
570enum_filter_map(VALUE obj)
571{
572 VALUE ary;
573
574 RETURN_SIZED_ENUMERATOR(obj, 0, 0, enum_size);
575
576 ary = rb_ary_new();
577 rb_block_call(obj, id_each, 0, 0, filter_map_i, ary);
578
579 return ary;
580}
581
582
583static VALUE
584reject_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, ary))
585{
586 ENUM_WANT_SVALUE();
587
588 if (!RTEST(enum_yield(argc, i))) {
589 rb_ary_push(ary, i);
590 }
591 return Qnil;
592}
593
594/*
595 * call-seq:
596 * reject {|element| ... } -> array
597 * reject -> enumerator
598 *
599 * Returns an array of objects rejected by the block.
600 *
601 * With a block given, calls the block with successive elements;
602 * returns an array of those elements for which the block returns +nil+ or +false+:
603 *
604 * (0..9).reject {|i| i * 2 if i.even? } # => [1, 3, 5, 7, 9]
605 * {foo: 0, bar: 1, baz: 2}.reject {|key, value| key if value.odd? } # => {:foo=>0, :baz=>2}
606 *
607 * When no block given, returns an Enumerator.
608 *
609 * Related: #select.
610 */
611
612static VALUE
613enum_reject(VALUE obj)
614{
615 VALUE ary;
616
617 RETURN_SIZED_ENUMERATOR(obj, 0, 0, enum_size);
618
619 ary = rb_ary_new();
620 rb_block_call(obj, id_each, 0, 0, reject_i, ary);
621
622 return ary;
623}
624
625static VALUE
626collect_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, ary))
627{
628 rb_ary_push(ary, rb_yield_values2(argc, argv));
629
630 return Qnil;
631}
632
633static VALUE
634collect_all(RB_BLOCK_CALL_FUNC_ARGLIST(i, ary))
635{
636 rb_ary_push(ary, rb_enum_values_pack(argc, argv));
637
638 return Qnil;
639}
640
641/*
642 * call-seq:
643 * map {|element| ... } -> array
644 * map -> enumerator
645 *
646 * Returns an array of objects returned by the block.
647 *
648 * With a block given, calls the block with successive elements;
649 * returns an array of the objects returned by the block:
650 *
651 * (0..4).map {|i| i*i } # => [0, 1, 4, 9, 16]
652 * {foo: 0, bar: 1, baz: 2}.map {|key, value| value*2} # => [0, 2, 4]
653 *
654 * With no block given, returns an Enumerator.
655 *
656 */
657static VALUE
658enum_collect(VALUE obj)
659{
660 VALUE ary;
661 int min_argc, max_argc;
662
663 RETURN_SIZED_ENUMERATOR(obj, 0, 0, enum_size);
664
665 ary = rb_ary_new();
666 min_argc = rb_block_min_max_arity(&max_argc);
667 rb_lambda_call(obj, id_each, 0, 0, collect_i, min_argc, max_argc, ary);
668
669 return ary;
670}
671
672static VALUE
673flat_map_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, ary))
674{
675 VALUE tmp;
676
677 i = rb_yield_values2(argc, argv);
678 tmp = rb_check_array_type(i);
679
680 if (NIL_P(tmp)) {
681 rb_ary_push(ary, i);
682 }
683 else {
684 rb_ary_concat(ary, tmp);
685 }
686 return Qnil;
687}
688
689/*
690 * call-seq:
691 * flat_map {|element| ... } -> array
692 * flat_map -> enumerator
693 *
694 * Returns an array of flattened objects returned by the block.
695 *
696 * With a block given, calls the block with successive elements;
697 * returns a flattened array of objects returned by the block:
698 *
699 * [0, 1, 2, 3].flat_map {|element| -element } # => [0, -1, -2, -3]
700 * [0, 1, 2, 3].flat_map {|element| [element, -element] } # => [0, 0, 1, -1, 2, -2, 3, -3]
701 * [[0, 1], [2, 3]].flat_map {|e| e + [100] } # => [0, 1, 100, 2, 3, 100]
702 * {foo: 0, bar: 1, baz: 2}.flat_map {|key, value| [key, value] } # => [:foo, 0, :bar, 1, :baz, 2]
703 *
704 * With no block given, returns an Enumerator.
705 *
706 * Alias: #collect_concat.
707 */
708static VALUE
709enum_flat_map(VALUE obj)
710{
711 VALUE ary;
712
713 RETURN_SIZED_ENUMERATOR(obj, 0, 0, enum_size);
714
715 ary = rb_ary_new();
716 rb_block_call(obj, id_each, 0, 0, flat_map_i, ary);
717
718 return ary;
719}
720
721/*
722 * call-seq:
723 * to_a(*args) -> array
724 *
725 * Returns an array containing the items in +self+:
726 *
727 * (0..4).to_a # => [0, 1, 2, 3, 4]
728 *
729 */
730static VALUE
731enum_to_a(int argc, VALUE *argv, VALUE obj)
732{
733 VALUE ary = rb_ary_new();
734
735 rb_block_call_kw(obj, id_each, argc, argv, collect_all, ary, RB_PASS_CALLED_KEYWORDS);
736
737 return ary;
738}
739
740static VALUE
741enum_hashify_into(VALUE obj, int argc, const VALUE *argv, rb_block_call_func *iter, VALUE hash)
742{
743 rb_block_call(obj, id_each, argc, argv, iter, hash);
744 return hash;
745}
746
747static VALUE
748enum_hashify(VALUE obj, int argc, const VALUE *argv, rb_block_call_func *iter)
749{
750 return enum_hashify_into(obj, argc, argv, iter, rb_hash_new());
751}
752
753static VALUE
754enum_to_h_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, hash))
755{
756 ENUM_WANT_SVALUE();
757 return rb_hash_set_pair(hash, i);
758}
759
760static VALUE
761enum_to_h_ii(RB_BLOCK_CALL_FUNC_ARGLIST(i, hash))
762{
763 return rb_hash_set_pair(hash, rb_yield_values2(argc, argv));
764}
765
766/*
767 * call-seq:
768 * to_h(*args) -> hash
769 * to_h(*args) {|element| ... } -> hash
770 *
771 * When +self+ consists of 2-element arrays,
772 * returns a hash each of whose entries is the key-value pair
773 * formed from one of those arrays:
774 *
775 * [[:foo, 0], [:bar, 1], [:baz, 2]].to_h # => {:foo=>0, :bar=>1, :baz=>2}
776 *
777 * When a block is given, the block is called with each element of +self+;
778 * the block should return a 2-element array which becomes a key-value pair
779 * in the returned hash:
780 *
781 * (0..3).to_h {|i| [i, i ** 2]} # => {0=>0, 1=>1, 2=>4, 3=>9}
782 *
783 * Raises an exception if an element of +self+ is not a 2-element array,
784 * and a block is not passed.
785 */
786
787static VALUE
788enum_to_h(int argc, VALUE *argv, VALUE obj)
789{
790 rb_block_call_func *iter = rb_block_given_p() ? enum_to_h_ii : enum_to_h_i;
791 return enum_hashify(obj, argc, argv, iter);
792}
793
794static VALUE
795inject_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, p))
796{
797 struct MEMO *memo = MEMO_CAST(p);
798
799 ENUM_WANT_SVALUE();
800
801 if (UNDEF_P(memo->v1)) {
802 MEMO_V1_SET(memo, i);
803 }
804 else {
805 MEMO_V1_SET(memo, rb_yield_values(2, memo->v1, i));
806 }
807 return Qnil;
808}
809
810static VALUE
811inject_op_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, p))
812{
813 struct MEMO *memo = MEMO_CAST(p);
814 VALUE name;
815
816 ENUM_WANT_SVALUE();
817
818 if (UNDEF_P(memo->v1)) {
819 MEMO_V1_SET(memo, i);
820 }
821 else if (SYMBOL_P(name = memo->u3.value)) {
822 const ID mid = SYM2ID(name);
823 MEMO_V1_SET(memo, rb_funcallv_public(memo->v1, mid, 1, &i));
824 }
825 else {
826 VALUE args[2];
827 args[0] = name;
828 args[1] = i;
829 MEMO_V1_SET(memo, rb_f_send(numberof(args), args, memo->v1));
830 }
831 return Qnil;
832}
833
834static VALUE
835ary_inject_op(VALUE ary, VALUE init, VALUE op)
836{
837 ID id;
838 VALUE v, e;
839 long i, n;
840
841 if (RARRAY_LEN(ary) == 0)
842 return UNDEF_P(init) ? Qnil : init;
843
844 if (UNDEF_P(init)) {
845 v = RARRAY_AREF(ary, 0);
846 i = 1;
847 if (RARRAY_LEN(ary) == 1)
848 return v;
849 }
850 else {
851 v = init;
852 i = 0;
853 }
854
855 id = SYM2ID(op);
856 if (id == idPLUS) {
857 if (RB_INTEGER_TYPE_P(v) &&
858 rb_method_basic_definition_p(rb_cInteger, idPLUS) &&
859 rb_obj_respond_to(v, idPLUS, FALSE)) {
860 n = 0;
861 for (; i < RARRAY_LEN(ary); i++) {
862 e = RARRAY_AREF(ary, i);
863 if (FIXNUM_P(e)) {
864 n += FIX2LONG(e); /* should not overflow long type */
865 if (!FIXABLE(n)) {
866 v = rb_big_plus(LONG2NUM(n), v);
867 n = 0;
868 }
869 }
870 else if (RB_BIGNUM_TYPE_P(e))
871 v = rb_big_plus(e, v);
872 else
873 goto not_integer;
874 }
875 if (n != 0)
876 v = rb_fix_plus(LONG2FIX(n), v);
877 return v;
878
879 not_integer:
880 if (n != 0)
881 v = rb_fix_plus(LONG2FIX(n), v);
882 }
883 }
884 for (; i < RARRAY_LEN(ary); i++) {
885 VALUE arg = RARRAY_AREF(ary, i);
886 v = rb_funcallv_public(v, id, 1, &arg);
887 }
888 return v;
889}
890
891/*
892 * call-seq:
893 * inject(symbol) -> object
894 * inject(initial_value, symbol) -> object
895 * inject {|memo, value| ... } -> object
896 * inject(initial_value) {|memo, value| ... } -> object
897 *
898 * Returns the result of applying a reducer to an initial value and
899 * the first element of the Enumerable. It then takes the result and applies the
900 * function to it and the second element of the collection, and so on. The
901 * return value is the result returned by the final call to the function.
902 *
903 * You can think of
904 *
905 * [ a, b, c, d ].inject(i) { |r, v| fn(r, v) }
906 *
907 * as being
908 *
909 * fn(fn(fn(fn(i, a), b), c), d)
910 *
911 * In a way the +inject+ function _injects_ the function
912 * between the elements of the enumerable.
913 *
914 * +inject+ is aliased as +reduce+. You use it when you want to
915 * _reduce_ a collection to a single value.
916 *
917 * <b>The Calling Sequences</b>
918 *
919 * Let's start with the most verbose:
920 *
921 * enum.inject(initial_value) do |result, next_value|
922 * # do something with +result+ and +next_value+
923 * # the value returned by the block becomes the
924 * # value passed in to the next iteration
925 * # as +result+
926 * end
927 *
928 * For example:
929 *
930 * product = [ 2, 3, 4 ].inject(1) do |result, next_value|
931 * result * next_value
932 * end
933 * product #=> 24
934 *
935 * When this runs, the block is first called with +1+ (the initial value) and
936 * +2+ (the first element of the array). The block returns <tt>1*2</tt>, so on
937 * the next iteration the block is called with +2+ (the previous result) and
938 * +3+. The block returns +6+, and is called one last time with +6+ and +4+.
939 * The result of the block, +24+ becomes the value returned by +inject+. This
940 * code returns the product of the elements in the enumerable.
941 *
942 * <b>First Shortcut: Default Initial value</b>
943 *
944 * In the case of the previous example, the initial value, +1+, wasn't really
945 * necessary: the calculation of the product of a list of numbers is self-contained.
946 *
947 * In these circumstances, you can omit the +initial_value+ parameter. +inject+
948 * will then initially call the block with the first element of the collection
949 * as the +result+ parameter and the second element as the +next_value+.
950 *
951 * [ 2, 3, 4 ].inject do |result, next_value|
952 * result * next_value
953 * end
954 *
955 * This shortcut is convenient, but can only be used when the block produces a result
956 * which can be passed back to it as a first parameter.
957 *
958 * Here's an example where that's not the case: it returns a hash where the keys are words
959 * and the values are the number of occurrences of that word in the enumerable.
960 *
961 * freqs = File.read("README.md")
962 * .scan(/\w{2,}/)
963 * .reduce(Hash.new(0)) do |counts, word|
964 * counts[word] += 1
965 * counts
966 * end
967 * freqs #=> {"Actions"=>4,
968 * "Status"=>5,
969 * "MinGW"=>3,
970 * "https"=>27,
971 * "github"=>10,
972 * "com"=>15, ...
973 *
974 * Note that the last line of the block is just the word +counts+. This ensures the
975 * return value of the block is the result that's being calculated.
976 *
977 * <b>Second Shortcut: a Reducer function</b>
978 *
979 * A <i>reducer function</i> is a function that takes a partial result and the next value,
980 * returning the next partial result. The block that is given to +inject+ is a reducer.
981 *
982 * You can also write a reducer as a function and pass the name of that function
983 * (as a symbol) to +inject+. However, for this to work, the function
984 *
985 * 1. Must be defined on the type of the result value
986 * 2. Must accept a single parameter, the next value in the collection, and
987 * 3. Must return an updated result which will also implement the function.
988 *
989 * Here's an example that adds elements to a string. The two calls invoke the functions
990 * String#concat and String#+ on the result so far, passing it the next value.
991 *
992 * s = [ "cat", " ", "dog" ].inject("", :concat)
993 * s #=> "cat dog"
994 * s = [ "cat", " ", "dog" ].inject("The result is:", :+)
995 * s #=> "The result is: cat dog"
996 *
997 * Here's a more complex example when the result object maintains
998 * state of a different type to the enumerable elements.
999 *
1000 * class Turtle
1001 *
1002 * def initialize
1003 * @x = @y = 0
1004 * end
1005 *
1006 * def move(dir)
1007 * case dir
1008 * when "n" then @y += 1
1009 * when "s" then @y -= 1
1010 * when "e" then @x += 1
1011 * when "w" then @x -= 1
1012 * end
1013 * self
1014 * end
1015 * end
1016 *
1017 * position = "nnneesw".chars.reduce(Turtle.new, :move)
1018 * position #=>> #<Turtle:0x00000001052f4698 @y=2, @x=1>
1019 *
1020 * <b>Third Shortcut: Reducer With no Initial Value</b>
1021 *
1022 * If your reducer returns a value that it can accept as a parameter, then you
1023 * don't have to pass in an initial value. Here <tt>:*</tt> is the name of the
1024 * _times_ function:
1025 *
1026 * product = [ 2, 3, 4 ].inject(:*)
1027 * product # => 24
1028 *
1029 * String concatenation again:
1030 *
1031 * s = [ "cat", " ", "dog" ].inject(:+)
1032 * s #=> "cat dog"
1033 *
1034 * And an example that converts a hash to an array of two-element subarrays.
1035 *
1036 * nested = {foo: 0, bar: 1}.inject([], :push)
1037 * nested # => [[:foo, 0], [:bar, 1]]
1038 *
1039 *
1040 */
1041static VALUE
1042enum_inject(int argc, VALUE *argv, VALUE obj)
1043{
1044 struct MEMO *memo;
1045 VALUE init, op;
1046 rb_block_call_func *iter = inject_i;
1047 ID id;
1048 int num_args;
1049
1050 if (rb_block_given_p()) {
1051 num_args = rb_scan_args(argc, argv, "02", &init, &op);
1052 }
1053 else {
1054 num_args = rb_scan_args(argc, argv, "11", &init, &op);
1055 }
1056
1057 switch (num_args) {
1058 case 0:
1059 init = Qundef;
1060 break;
1061 case 1:
1062 if (rb_block_given_p()) {
1063 break;
1064 }
1065 id = rb_check_id(&init);
1066 op = id ? ID2SYM(id) : init;
1067 init = Qundef;
1068 iter = inject_op_i;
1069 break;
1070 case 2:
1071 if (rb_block_given_p()) {
1072 rb_warning("given block not used");
1073 }
1074 id = rb_check_id(&op);
1075 if (id) op = ID2SYM(id);
1076 iter = inject_op_i;
1077 break;
1078 }
1079
1080 if (iter == inject_op_i &&
1081 SYMBOL_P(op) &&
1082 RB_TYPE_P(obj, T_ARRAY) &&
1083 rb_method_basic_definition_p(CLASS_OF(obj), id_each)) {
1084 return ary_inject_op(obj, init, op);
1085 }
1086
1087 memo = MEMO_NEW(init, Qnil, op);
1088 rb_block_call(obj, id_each, 0, 0, iter, (VALUE)memo);
1089 if (UNDEF_P(memo->v1)) return Qnil;
1090 return memo->v1;
1091}
1092
1093static VALUE
1094partition_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, arys))
1095{
1096 struct MEMO *memo = MEMO_CAST(arys);
1097 VALUE ary;
1098 ENUM_WANT_SVALUE();
1099
1100 if (RTEST(enum_yield(argc, i))) {
1101 ary = memo->v1;
1102 }
1103 else {
1104 ary = memo->v2;
1105 }
1106 rb_ary_push(ary, i);
1107 return Qnil;
1108}
1109
1110/*
1111 * call-seq:
1112 * partition {|element| ... } -> [true_array, false_array]
1113 * partition -> enumerator
1114 *
1115 * With a block given, returns an array of two arrays:
1116 *
1117 * - The first having those elements for which the block returns a truthy value.
1118 * - The other having all other elements.
1119 *
1120 * Examples:
1121 *
1122 * p = (1..4).partition {|i| i.even? }
1123 * p # => [[2, 4], [1, 3]]
1124 * p = ('a'..'d').partition {|c| c < 'c' }
1125 * p # => [["a", "b"], ["c", "d"]]
1126 * h = {foo: 0, bar: 1, baz: 2, bat: 3}
1127 * p = h.partition {|key, value| key.start_with?('b') }
1128 * p # => [[[:bar, 1], [:baz, 2], [:bat, 3]], [[:foo, 0]]]
1129 * p = h.partition {|key, value| value < 2 }
1130 * p # => [[[:foo, 0], [:bar, 1]], [[:baz, 2], [:bat, 3]]]
1131 *
1132 * With no block given, returns an Enumerator.
1133 *
1134 * Related: Enumerable#group_by.
1135 *
1136 */
1137
1138static VALUE
1139enum_partition(VALUE obj)
1140{
1141 struct MEMO *memo;
1142
1143 RETURN_SIZED_ENUMERATOR(obj, 0, 0, enum_size);
1144
1145 memo = MEMO_NEW(rb_ary_new(), rb_ary_new(), 0);
1146 rb_block_call(obj, id_each, 0, 0, partition_i, (VALUE)memo);
1147
1148 return rb_assoc_new(memo->v1, memo->v2);
1149}
1150
1151static VALUE
1152group_by_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, hash))
1153{
1154 VALUE group;
1155 VALUE values;
1156
1157 ENUM_WANT_SVALUE();
1158
1159 group = enum_yield(argc, i);
1160 values = rb_hash_aref(hash, group);
1161 if (!RB_TYPE_P(values, T_ARRAY)) {
1162 values = rb_ary_new3(1, i);
1163 rb_hash_aset(hash, group, values);
1164 }
1165 else {
1166 rb_ary_push(values, i);
1167 }
1168 return Qnil;
1169}
1170
1171/*
1172 * call-seq:
1173 * group_by {|element| ... } -> hash
1174 * group_by -> enumerator
1175 *
1176 * With a block given returns a hash:
1177 *
1178 * - Each key is a return value from the block.
1179 * - Each value is an array of those elements for which the block returned that key.
1180 *
1181 * Examples:
1182 *
1183 * g = (1..6).group_by {|i| i%3 }
1184 * g # => {1=>[1, 4], 2=>[2, 5], 0=>[3, 6]}
1185 * h = {foo: 0, bar: 1, baz: 0, bat: 1}
1186 * g = h.group_by {|key, value| value }
1187 * g # => {0=>[[:foo, 0], [:baz, 0]], 1=>[[:bar, 1], [:bat, 1]]}
1188 *
1189 * With no block given, returns an Enumerator.
1190 *
1191 */
1192
1193static VALUE
1194enum_group_by(VALUE obj)
1195{
1196 RETURN_SIZED_ENUMERATOR(obj, 0, 0, enum_size);
1197
1198 return enum_hashify(obj, 0, 0, group_by_i);
1199}
1200
1201static int
1202tally_up(st_data_t *group, st_data_t *value, st_data_t arg, int existing)
1203{
1204 VALUE tally = (VALUE)*value;
1205 VALUE hash = (VALUE)arg;
1206 if (!existing) {
1207 tally = INT2FIX(1);
1208 }
1209 else if (FIXNUM_P(tally) && tally < INT2FIX(FIXNUM_MAX)) {
1210 tally += INT2FIX(1) & ~FIXNUM_FLAG;
1211 }
1212 else {
1213 Check_Type(tally, T_BIGNUM);
1214 tally = rb_big_plus(tally, INT2FIX(1));
1215 RB_OBJ_WRITTEN(hash, Qundef, tally);
1216 }
1217 *value = (st_data_t)tally;
1218 return ST_CONTINUE;
1219}
1220
1221static VALUE
1222rb_enum_tally_up(VALUE hash, VALUE group)
1223{
1224 if (!rb_hash_stlike_update(hash, group, tally_up, (st_data_t)hash)) {
1225 RB_OBJ_WRITTEN(hash, Qundef, group);
1226 }
1227 return hash;
1228}
1229
1230static VALUE
1231tally_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, hash))
1232{
1233 ENUM_WANT_SVALUE();
1234 rb_enum_tally_up(hash, i);
1235 return Qnil;
1236}
1237
1238/*
1239 * call-seq:
1240 * tally(hash = {}) -> hash
1241 *
1242 * When argument +hash+ is not given,
1243 * returns a new hash whose keys are the distinct elements in +self+;
1244 * each integer value is the count of occurrences of each element:
1245 *
1246 * %w[a b c b c a c b].tally # => {"a"=>2, "b"=>3, "c"=>3}
1247 *
1248 * When argument +hash+ is given,
1249 * returns +hash+, possibly augmented; for each element +ele+ in +self+:
1250 *
1251 * - Adds it as a key with a zero value if that key does not already exist:
1252 *
1253 * hash[ele] = 0 unless hash.include?(ele)
1254 *
1255 * - Increments the value of key +ele+:
1256 *
1257 * hash[ele] += 1
1258 *
1259 * This is useful for accumulating tallies across multiple enumerables:
1260 *
1261 * h = {} # => {}
1262 * %w[a c d b c a].tally(h) # => {"a"=>2, "c"=>2, "d"=>1, "b"=>1}
1263 * %w[b a z].tally(h) # => {"a"=>3, "c"=>2, "d"=>1, "b"=>2, "z"=>1}
1264 * %w[b a m].tally(h) # => {"a"=>4, "c"=>2, "d"=>1, "b"=>3, "z"=>1, "m"=>1}
1265 *
1266 * The key to be added or found for an element depends on the class of +self+;
1267 * see {Enumerable in Ruby Classes}[rdoc-ref:Enumerable@Enumerable+in+Ruby+Classes].
1268 *
1269 * Examples:
1270 *
1271 * - Array (and certain array-like classes):
1272 * the key is the element (as above).
1273 * - Hash (and certain hash-like classes):
1274 * the key is the 2-element array formed from the key-value pair:
1275 *
1276 * h = {} # => {}
1277 * {foo: 'a', bar: 'b'}.tally(h) # => {[:foo, "a"]=>1, [:bar, "b"]=>1}
1278 * {foo: 'c', bar: 'd'}.tally(h) # => {[:foo, "a"]=>1, [:bar, "b"]=>1, [:foo, "c"]=>1, [:bar, "d"]=>1}
1279 * {foo: 'a', bar: 'b'}.tally(h) # => {[:foo, "a"]=>2, [:bar, "b"]=>2, [:foo, "c"]=>1, [:bar, "d"]=>1}
1280 * {foo: 'c', bar: 'd'}.tally(h) # => {[:foo, "a"]=>2, [:bar, "b"]=>2, [:foo, "c"]=>2, [:bar, "d"]=>2}
1281 *
1282 */
1283
1284static VALUE
1285enum_tally(int argc, VALUE *argv, VALUE obj)
1286{
1287 VALUE hash;
1288 if (rb_check_arity(argc, 0, 1)) {
1289 hash = rb_to_hash_type(argv[0]);
1290 rb_check_frozen(hash);
1291 }
1292 else {
1293 hash = rb_hash_new();
1294 }
1295
1296 return enum_hashify_into(obj, 0, 0, tally_i, hash);
1297}
1298
1299NORETURN(static VALUE first_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, params)));
1300static VALUE
1301first_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, params))
1302{
1303 struct MEMO *memo = MEMO_CAST(params);
1304 ENUM_WANT_SVALUE();
1305
1306 MEMO_V1_SET(memo, i);
1307 rb_iter_break();
1308
1310}
1311
1312static VALUE enum_take(VALUE obj, VALUE n);
1313
1314/*
1315 * call-seq:
1316 * first -> element or nil
1317 * first(n) -> array
1318 *
1319 * Returns the first element or elements.
1320 *
1321 * With no argument, returns the first element, or +nil+ if there is none:
1322 *
1323 * (1..4).first # => 1
1324 * %w[a b c].first # => "a"
1325 * {foo: 1, bar: 1, baz: 2}.first # => [:foo, 1]
1326 * [].first # => nil
1327 *
1328 * With integer argument +n+, returns an array
1329 * containing the first +n+ elements that exist:
1330 *
1331 * (1..4).first(2) # => [1, 2]
1332 * %w[a b c d].first(3) # => ["a", "b", "c"]
1333 * %w[a b c d].first(50) # => ["a", "b", "c", "d"]
1334 * {foo: 1, bar: 1, baz: 2}.first(2) # => [[:foo, 1], [:bar, 1]]
1335 * [].first(2) # => []
1336 *
1337 */
1338
1339static VALUE
1340enum_first(int argc, VALUE *argv, VALUE obj)
1341{
1342 struct MEMO *memo;
1343 rb_check_arity(argc, 0, 1);
1344 if (argc > 0) {
1345 return enum_take(obj, argv[0]);
1346 }
1347 else {
1348 memo = MEMO_NEW(Qnil, 0, 0);
1349 rb_block_call(obj, id_each, 0, 0, first_i, (VALUE)memo);
1350 return memo->v1;
1351 }
1352}
1353
1354/*
1355 * call-seq:
1356 * sort -> array
1357 * sort {|a, b| ... } -> array
1358 *
1359 * Returns an array containing the sorted elements of +self+.
1360 * The ordering of equal elements is indeterminate and may be unstable.
1361 *
1362 * With no block given, the sort compares
1363 * using the elements' own method <tt>#<=></tt>:
1364 *
1365 * %w[b c a d].sort # => ["a", "b", "c", "d"]
1366 * {foo: 0, bar: 1, baz: 2}.sort # => [[:bar, 1], [:baz, 2], [:foo, 0]]
1367 *
1368 * With a block given, comparisons in the block determine the ordering.
1369 * The block is called with two elements +a+ and +b+, and must return:
1370 *
1371 * - A negative integer if <tt>a < b</tt>.
1372 * - Zero if <tt>a == b</tt>.
1373 * - A positive integer if <tt>a > b</tt>.
1374 *
1375 * Examples:
1376 *
1377 * a = %w[b c a d]
1378 * a.sort {|a, b| b <=> a } # => ["d", "c", "b", "a"]
1379 * h = {foo: 0, bar: 1, baz: 2}
1380 * h.sort {|a, b| b <=> a } # => [[:foo, 0], [:baz, 2], [:bar, 1]]
1381 *
1382 * See also #sort_by. It implements a Schwartzian transform
1383 * which is useful when key computation or comparison is expensive.
1384 */
1385
1386static VALUE
1387enum_sort(VALUE obj)
1388{
1389 return rb_ary_sort_bang(enum_to_a(0, 0, obj));
1390}
1391
1392#define SORT_BY_BUFSIZE 16
1393#define SORT_BY_UNIFORMED(num, flo, fix) (((num&1)<<2)|((flo&1)<<1)|fix)
1395 const VALUE ary;
1396 const VALUE buf;
1397 uint8_t n;
1398 uint8_t primitive_uniformed;
1399};
1400
1401static VALUE
1402sort_by_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, _data))
1403{
1404 struct sort_by_data *data = (struct sort_by_data *)&MEMO_CAST(_data)->v1;
1405 VALUE ary = data->ary;
1406 VALUE v;
1407
1408 ENUM_WANT_SVALUE();
1409
1410 v = enum_yield(argc, i);
1411
1412 if (RBASIC(ary)->klass) {
1413 rb_raise(rb_eRuntimeError, "sort_by reentered");
1414 }
1415 if (RARRAY_LEN(data->buf) != SORT_BY_BUFSIZE*2) {
1416 rb_raise(rb_eRuntimeError, "sort_by reentered");
1417 }
1418
1419 if (data->primitive_uniformed) {
1420 data->primitive_uniformed &= SORT_BY_UNIFORMED((FIXNUM_P(v)) || (RB_FLOAT_TYPE_P(v)),
1421 RB_FLOAT_TYPE_P(v),
1422 FIXNUM_P(v));
1423 }
1424 RARRAY_ASET(data->buf, data->n*2, v);
1425 RARRAY_ASET(data->buf, data->n*2+1, i);
1426 data->n++;
1427 if (data->n == SORT_BY_BUFSIZE) {
1428 rb_ary_concat(ary, data->buf);
1429 data->n = 0;
1430 }
1431 return Qnil;
1432}
1433
1434static int
1435sort_by_cmp(const void *ap, const void *bp, void *data)
1436{
1437 VALUE a;
1438 VALUE b;
1439 VALUE ary = (VALUE)data;
1440
1441 if (RBASIC(ary)->klass) {
1442 rb_raise(rb_eRuntimeError, "sort_by reentered");
1443 }
1444
1445 a = *(VALUE *)ap;
1446 b = *(VALUE *)bp;
1447
1448 return OPTIMIZED_CMP(a, b);
1449}
1450
1451
1452/*
1453 This is parts of uniform sort
1454*/
1455
1456#define uless rb_uniform_is_less
1457#define UNIFORM_SWAP(a,b)\
1458 do{struct rb_uniform_sort_data tmp = a; a = b; b = tmp;} while(0)
1459
1461 VALUE v;
1462 VALUE i;
1463};
1464
1465static inline bool
1466rb_uniform_is_less(VALUE a, VALUE b)
1467{
1468
1469 if (FIXNUM_P(a) && FIXNUM_P(b)) {
1470 return (SIGNED_VALUE)a < (SIGNED_VALUE)b;
1471 }
1472 else if (FIXNUM_P(a)) {
1474 return rb_float_cmp(b, a) > 0;
1475 }
1476 else {
1478 return rb_float_cmp(a, b) < 0;
1479 }
1480}
1481
1482static inline bool
1483rb_uniform_is_larger(VALUE a, VALUE b)
1484{
1485
1486 if (FIXNUM_P(a) && FIXNUM_P(b)) {
1487 return (SIGNED_VALUE)a > (SIGNED_VALUE)b;
1488 }
1489 else if (FIXNUM_P(a)) {
1491 return rb_float_cmp(b, a) < 0;
1492 }
1493 else {
1495 return rb_float_cmp(a, b) > 0;
1496 }
1497}
1498
1499#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))
1500
1501static void
1502rb_uniform_insertionsort_2(struct rb_uniform_sort_data* ptr_begin,
1503 struct rb_uniform_sort_data* ptr_end)
1504{
1505 if ((ptr_end - ptr_begin) < 2) return;
1506 struct rb_uniform_sort_data tmp, *j, *k,
1507 *index = ptr_begin+1;
1508 for (; index < ptr_end; index++) {
1509 tmp = *index;
1510 j = k = index;
1511 if (uless(tmp.v, ptr_begin->v)) {
1512 while (ptr_begin < j) {
1513 *j = *(--k);
1514 j = k;
1515 }
1516 }
1517 else {
1518 while (uless(tmp.v, (--k)->v)) {
1519 *j = *k;
1520 j = k;
1521 }
1522 }
1523 *j = tmp;
1524 }
1525}
1526
1527static inline void
1528rb_uniform_heap_down_2(struct rb_uniform_sort_data* ptr_begin,
1529 size_t offset, size_t len)
1530{
1531 size_t c;
1532 struct rb_uniform_sort_data tmp = ptr_begin[offset];
1533 while ((c = (offset<<1)+1) <= len) {
1534 if (c < len && uless(ptr_begin[c].v, ptr_begin[c+1].v)) {
1535 c++;
1536 }
1537 if (!uless(tmp.v, ptr_begin[c].v)) break;
1538 ptr_begin[offset] = ptr_begin[c];
1539 offset = c;
1540 }
1541 ptr_begin[offset] = tmp;
1542}
1543
1544static void
1545rb_uniform_heapsort_2(struct rb_uniform_sort_data* ptr_begin,
1546 struct rb_uniform_sort_data* ptr_end)
1547{
1548 size_t n = ptr_end - ptr_begin;
1549 if (n < 2) return;
1550
1551 for (size_t offset = n>>1; offset > 0;) {
1552 rb_uniform_heap_down_2(ptr_begin, --offset, n-1);
1553 }
1554 for (size_t offset = n-1; offset > 0;) {
1555 UNIFORM_SWAP(*ptr_begin, ptr_begin[offset]);
1556 rb_uniform_heap_down_2(ptr_begin, 0, --offset);
1557 }
1558}
1559
1560
1561static void
1562rb_uniform_quicksort_intro_2(struct rb_uniform_sort_data* ptr_begin,
1563 struct rb_uniform_sort_data* ptr_end, size_t d)
1564{
1565
1566 if (ptr_end - ptr_begin <= 16) {
1567 rb_uniform_insertionsort_2(ptr_begin, ptr_end);
1568 return;
1569 }
1570 if (d == 0) {
1571 rb_uniform_heapsort_2(ptr_begin, ptr_end);
1572 return;
1573 }
1574
1575 VALUE x = med3_val(ptr_begin->v,
1576 ptr_begin[(ptr_end - ptr_begin)>>1].v,
1577 ptr_end[-1].v);
1578 struct rb_uniform_sort_data *i = ptr_begin;
1579 struct rb_uniform_sort_data *j = ptr_end-1;
1580
1581 do {
1582 while (uless(i->v, x)) i++;
1583 while (uless(x, j->v)) j--;
1584 if (i <= j) {
1585 UNIFORM_SWAP(*i, *j);
1586 i++;
1587 j--;
1588 }
1589 } while (i <= j);
1590 j++;
1591 if (ptr_end - j > 1) rb_uniform_quicksort_intro_2(j, ptr_end, d-1);
1592 if (i - ptr_begin > 1) rb_uniform_quicksort_intro_2(ptr_begin, i, d-1);
1593}
1594
1600static void
1601rb_uniform_intro_sort_2(struct rb_uniform_sort_data* ptr_begin,
1602 struct rb_uniform_sort_data* ptr_end)
1603{
1604 size_t n = ptr_end - ptr_begin;
1605 size_t d = CHAR_BIT * sizeof(n) - nlz_intptr(n) - 1;
1606 bool sorted_flag = true;
1607
1608 for (struct rb_uniform_sort_data* ptr = ptr_begin+1; ptr < ptr_end; ptr++) {
1609 if (rb_uniform_is_larger((ptr-1)->v, (ptr)->v)) {
1610 sorted_flag = false;
1611 break;
1612 }
1613 }
1614
1615 if (sorted_flag) {
1616 return;
1617 }
1618 rb_uniform_quicksort_intro_2(ptr_begin, ptr_end, d<<1);
1619}
1620
1621#undef uless
1622
1623
1624/*
1625 * call-seq:
1626 * sort_by {|element| ... } -> array
1627 * sort_by -> enumerator
1628 *
1629 * With a block given, returns an array of elements of +self+,
1630 * sorted according to the value returned by the block for each element.
1631 * The ordering of equal elements is indeterminate and may be unstable.
1632 *
1633 * Examples:
1634 *
1635 * a = %w[xx xxx x xxxx]
1636 * a.sort_by {|s| s.size } # => ["x", "xx", "xxx", "xxxx"]
1637 * a.sort_by {|s| -s.size } # => ["xxxx", "xxx", "xx", "x"]
1638 * h = {foo: 2, bar: 1, baz: 0}
1639 * h.sort_by{|key, value| value } # => [[:baz, 0], [:bar, 1], [:foo, 2]]
1640 * h.sort_by{|key, value| key } # => [[:bar, 1], [:baz, 0], [:foo, 2]]
1641 *
1642 * With no block given, returns an Enumerator.
1643 *
1644 * The current implementation of #sort_by generates an array of
1645 * tuples containing the original collection element and the mapped
1646 * value. This makes #sort_by fairly expensive when the keysets are
1647 * simple.
1648 *
1649 * require 'benchmark'
1650 *
1651 * a = (1..100000).map { rand(100000) }
1652 *
1653 * Benchmark.bm(10) do |b|
1654 * b.report("Sort") { a.sort }
1655 * b.report("Sort by") { a.sort_by { |a| a } }
1656 * end
1657 *
1658 * <em>produces:</em>
1659 *
1660 * user system total real
1661 * Sort 0.180000 0.000000 0.180000 ( 0.175469)
1662 * Sort by 1.980000 0.040000 2.020000 ( 2.013586)
1663 *
1664 * However, consider the case where comparing the keys is a non-trivial
1665 * operation. The following code sorts some files on modification time
1666 * using the basic #sort method.
1667 *
1668 * files = Dir["*"]
1669 * sorted = files.sort { |a, b| File.new(a).mtime <=> File.new(b).mtime }
1670 * sorted #=> ["mon", "tues", "wed", "thurs"]
1671 *
1672 * This sort is inefficient: it generates two new File
1673 * objects during every comparison. A slightly better technique is to
1674 * use the Kernel#test method to generate the modification
1675 * times directly.
1676 *
1677 * files = Dir["*"]
1678 * sorted = files.sort { |a, b|
1679 * test(?M, a) <=> test(?M, b)
1680 * }
1681 * sorted #=> ["mon", "tues", "wed", "thurs"]
1682 *
1683 * This still generates many unnecessary Time objects. A more
1684 * efficient technique is to cache the sort keys (modification times
1685 * in this case) before the sort. Perl users often call this approach
1686 * a Schwartzian transform, after Randal Schwartz. We construct a
1687 * temporary array, where each element is an array containing our
1688 * sort key along with the filename. We sort this array, and then
1689 * extract the filename from the result.
1690 *
1691 * sorted = Dir["*"].collect { |f|
1692 * [test(?M, f), f]
1693 * }.sort.collect { |f| f[1] }
1694 * sorted #=> ["mon", "tues", "wed", "thurs"]
1695 *
1696 * This is exactly what #sort_by does internally.
1697 *
1698 * sorted = Dir["*"].sort_by { |f| test(?M, f) }
1699 * sorted #=> ["mon", "tues", "wed", "thurs"]
1700 *
1701 * To produce the reverse of a specific order, the following can be used:
1702 *
1703 * ary.sort_by { ... }.reverse!
1704 */
1705
1706static VALUE
1707enum_sort_by(VALUE obj)
1708{
1709 VALUE ary, buf;
1710 struct MEMO *memo;
1711 long i;
1712 struct sort_by_data *data;
1713
1714 RETURN_SIZED_ENUMERATOR(obj, 0, 0, enum_size);
1715
1716 if (RB_TYPE_P(obj, T_ARRAY) && RARRAY_LEN(obj) <= LONG_MAX/2) {
1717 ary = rb_ary_new2(RARRAY_LEN(obj)*2);
1718 }
1719 else {
1720 ary = rb_ary_new();
1721 }
1722 RBASIC_CLEAR_CLASS(ary);
1723 buf = rb_ary_hidden_new(SORT_BY_BUFSIZE*2);
1724 rb_ary_store(buf, SORT_BY_BUFSIZE*2-1, Qnil);
1725 memo = MEMO_NEW(0, 0, 0);
1726 data = (struct sort_by_data *)&memo->v1;
1727 RB_OBJ_WRITE(memo, &data->ary, ary);
1728 RB_OBJ_WRITE(memo, &data->buf, buf);
1729 data->n = 0;
1730 data->primitive_uniformed = SORT_BY_UNIFORMED((CMP_OPTIMIZABLE(FLOAT) && CMP_OPTIMIZABLE(INTEGER)),
1731 CMP_OPTIMIZABLE(FLOAT),
1732 CMP_OPTIMIZABLE(INTEGER));
1733 rb_block_call(obj, id_each, 0, 0, sort_by_i, (VALUE)memo);
1734 ary = data->ary;
1735 buf = data->buf;
1736 if (data->n) {
1737 rb_ary_resize(buf, data->n*2);
1738 rb_ary_concat(ary, buf);
1739 }
1740 if (RARRAY_LEN(ary) > 2) {
1741 if (data->primitive_uniformed) {
1742 RARRAY_PTR_USE(ary, ptr,
1743 rb_uniform_intro_sort_2((struct rb_uniform_sort_data*)ptr,
1744 (struct rb_uniform_sort_data*)(ptr + RARRAY_LEN(ary))));
1745 }
1746 else {
1747 RARRAY_PTR_USE(ary, ptr,
1748 ruby_qsort(ptr, RARRAY_LEN(ary)/2, 2*sizeof(VALUE),
1749 sort_by_cmp, (void *)ary));
1750 }
1751 }
1752 if (RBASIC(ary)->klass) {
1753 rb_raise(rb_eRuntimeError, "sort_by reentered");
1754 }
1755 for (i=1; i<RARRAY_LEN(ary); i+=2) {
1756 RARRAY_ASET(ary, i/2, RARRAY_AREF(ary, i));
1757 }
1758 rb_ary_resize(ary, RARRAY_LEN(ary)/2);
1759 RBASIC_SET_CLASS_RAW(ary, rb_cArray);
1760
1761 return ary;
1762}
1763
1764#define ENUMFUNC(name) argc ? name##_eqq : rb_block_given_p() ? name##_iter_i : name##_i
1765
1766#define ENUM_BLOCK_CALL(name) \
1767 rb_block_call2(obj, id_each, 0, 0, ENUMFUNC(name), (VALUE)memo, rb_block_given_p() && rb_block_pair_yield_optimizable() ? RB_BLOCK_NO_USE_PACKED_ARGS : 0);
1768
1769#define MEMO_ENUM_NEW(v1) (rb_check_arity(argc, 0, 1), MEMO_NEW((v1), (argc ? *argv : 0), 0))
1770
1771#define DEFINE_ENUMFUNCS(name) \
1772static VALUE enum_##name##_func(VALUE result, struct MEMO *memo); \
1773\
1774static VALUE \
1775name##_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, memo)) \
1776{ \
1777 return enum_##name##_func(rb_enum_values_pack(argc, argv), MEMO_CAST(memo)); \
1778} \
1779\
1780static VALUE \
1781name##_iter_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, memo)) \
1782{ \
1783 return enum_##name##_func(rb_yield_values2(argc, argv), MEMO_CAST(memo)); \
1784} \
1785\
1786static VALUE \
1787name##_eqq(RB_BLOCK_CALL_FUNC_ARGLIST(i, memo)) \
1788{ \
1789 ENUM_WANT_SVALUE(); \
1790 return enum_##name##_func(rb_funcallv(MEMO_CAST(memo)->v2, id_eqq, 1, &i), MEMO_CAST(memo)); \
1791} \
1792\
1793static VALUE \
1794enum_##name##_func(VALUE result, struct MEMO *memo)
1795
1796#define WARN_UNUSED_BLOCK(argc) do { \
1797 if ((argc) > 0 && rb_block_given_p()) { \
1798 rb_warn("given block not used"); \
1799 } \
1800} while (0)
1801
1802DEFINE_ENUMFUNCS(all)
1803{
1804 if (!RTEST(result)) {
1805 MEMO_V1_SET(memo, Qfalse);
1806 rb_iter_break();
1807 }
1808 return Qnil;
1809}
1810
1811/*
1812 * call-seq:
1813 * all? -> true or false
1814 * all?(pattern) -> true or false
1815 * all? {|element| ... } -> true or false
1816 *
1817 * Returns whether every element meets a given criterion.
1818 *
1819 * If +self+ has no element, returns +true+ and argument or block
1820 * are not used.
1821 *
1822 * With no argument and no block,
1823 * returns whether every element is truthy:
1824 *
1825 * (1..4).all? # => true
1826 * %w[a b c d].all? # => true
1827 * [1, 2, nil].all? # => false
1828 * ['a','b', false].all? # => false
1829 * [].all? # => true
1830 *
1831 * With argument +pattern+ and no block,
1832 * returns whether for each element +element+,
1833 * <tt>pattern === element</tt>:
1834 *
1835 * (1..4).all?(Integer) # => true
1836 * (1..4).all?(Numeric) # => true
1837 * (1..4).all?(Float) # => false
1838 * %w[bar baz bat bam].all?(/ba/) # => true
1839 * %w[bar baz bat bam].all?(/bar/) # => false
1840 * %w[bar baz bat bam].all?('ba') # => false
1841 * {foo: 0, bar: 1, baz: 2}.all?(Array) # => true
1842 * {foo: 0, bar: 1, baz: 2}.all?(Hash) # => false
1843 * [].all?(Integer) # => true
1844 *
1845 * With a block given, returns whether the block returns a truthy value
1846 * for every element:
1847 *
1848 * (1..4).all? {|element| element < 5 } # => true
1849 * (1..4).all? {|element| element < 4 } # => false
1850 * {foo: 0, bar: 1, baz: 2}.all? {|key, value| value < 3 } # => true
1851 * {foo: 0, bar: 1, baz: 2}.all? {|key, value| value < 2 } # => false
1852 *
1853 * Related: #any?, #none? #one?.
1854 *
1855 */
1856
1857static VALUE
1858enum_all(int argc, VALUE *argv, VALUE obj)
1859{
1860 struct MEMO *memo = MEMO_ENUM_NEW(Qtrue);
1861 WARN_UNUSED_BLOCK(argc);
1862 ENUM_BLOCK_CALL(all);
1863 return memo->v1;
1864}
1865
1866DEFINE_ENUMFUNCS(any)
1867{
1868 if (RTEST(result)) {
1869 MEMO_V1_SET(memo, Qtrue);
1870 rb_iter_break();
1871 }
1872 return Qnil;
1873}
1874
1875/*
1876 * call-seq:
1877 * any? -> true or false
1878 * any?(pattern) -> true or false
1879 * any? {|element| ... } -> true or false
1880 *
1881 * Returns whether any element meets a given criterion.
1882 *
1883 * If +self+ has no element, returns +false+ and argument or block
1884 * are not used.
1885 *
1886 * With no argument and no block,
1887 * returns whether any element is truthy:
1888 *
1889 * (1..4).any? # => true
1890 * %w[a b c d].any? # => true
1891 * [1, false, nil].any? # => true
1892 * [].any? # => false
1893 *
1894 * With argument +pattern+ and no block,
1895 * returns whether for any element +element+,
1896 * <tt>pattern === element</tt>:
1897 *
1898 * [nil, false, 0].any?(Integer) # => true
1899 * [nil, false, 0].any?(Numeric) # => true
1900 * [nil, false, 0].any?(Float) # => false
1901 * %w[bar baz bat bam].any?(/m/) # => true
1902 * %w[bar baz bat bam].any?(/foo/) # => false
1903 * %w[bar baz bat bam].any?('ba') # => false
1904 * {foo: 0, bar: 1, baz: 2}.any?(Array) # => true
1905 * {foo: 0, bar: 1, baz: 2}.any?(Hash) # => false
1906 * [].any?(Integer) # => false
1907 *
1908 * With a block given, returns whether the block returns a truthy value
1909 * for any element:
1910 *
1911 * (1..4).any? {|element| element < 2 } # => true
1912 * (1..4).any? {|element| element < 1 } # => false
1913 * {foo: 0, bar: 1, baz: 2}.any? {|key, value| value < 1 } # => true
1914 * {foo: 0, bar: 1, baz: 2}.any? {|key, value| value < 0 } # => false
1915 *
1916 * Related: #all?, #none?, #one?.
1917 */
1918
1919static VALUE
1920enum_any(int argc, VALUE *argv, VALUE obj)
1921{
1922 struct MEMO *memo = MEMO_ENUM_NEW(Qfalse);
1923 WARN_UNUSED_BLOCK(argc);
1924 ENUM_BLOCK_CALL(any);
1925 return memo->v1;
1926}
1927
1928DEFINE_ENUMFUNCS(one)
1929{
1930 if (RTEST(result)) {
1931 if (UNDEF_P(memo->v1)) {
1932 MEMO_V1_SET(memo, Qtrue);
1933 }
1934 else if (memo->v1 == Qtrue) {
1935 MEMO_V1_SET(memo, Qfalse);
1936 rb_iter_break();
1937 }
1938 }
1939 return Qnil;
1940}
1941
1943 long n;
1944 long bufmax;
1945 long curlen;
1946 VALUE buf;
1947 VALUE limit;
1948 int (*cmpfunc)(const void *, const void *, void *);
1949 int rev: 1; /* max if 1 */
1950 int by: 1; /* min_by if 1 */
1951};
1952
1953static VALUE
1954cmpint_reenter_check(struct nmin_data *data, VALUE val)
1955{
1956 if (RBASIC(data->buf)->klass) {
1957 rb_raise(rb_eRuntimeError, "%s%s reentered",
1958 data->rev ? "max" : "min",
1959 data->by ? "_by" : "");
1960 }
1961 return val;
1962}
1963
1964static int
1965nmin_cmp(const void *ap, const void *bp, void *_data)
1966{
1967 struct nmin_data *data = (struct nmin_data *)_data;
1968 VALUE a = *(const VALUE *)ap, b = *(const VALUE *)bp;
1969#define rb_cmpint(cmp, a, b) rb_cmpint(cmpint_reenter_check(data, (cmp)), a, b)
1970 return OPTIMIZED_CMP(a, b);
1971#undef rb_cmpint
1972}
1973
1974static int
1975nmin_block_cmp(const void *ap, const void *bp, void *_data)
1976{
1977 struct nmin_data *data = (struct nmin_data *)_data;
1978 VALUE a = *(const VALUE *)ap, b = *(const VALUE *)bp;
1979 VALUE cmp = rb_yield_values(2, a, b);
1980 cmpint_reenter_check(data, cmp);
1981 return rb_cmpint(cmp, a, b);
1982}
1983
1984static void
1985nmin_filter(struct nmin_data *data)
1986{
1987 long n;
1988 VALUE *beg;
1989 int eltsize;
1990 long numelts;
1991
1992 long left, right;
1993 long store_index;
1994
1995 long i, j;
1996
1997 if (data->curlen <= data->n)
1998 return;
1999
2000 n = data->n;
2001 beg = RARRAY_PTR(data->buf);
2002 eltsize = data->by ? 2 : 1;
2003 numelts = data->curlen;
2004
2005 left = 0;
2006 right = numelts-1;
2007
2008#define GETPTR(i) (beg+(i)*eltsize)
2009
2010#define SWAP(i, j) do { \
2011 VALUE tmp[2]; \
2012 memcpy(tmp, GETPTR(i), sizeof(VALUE)*eltsize); \
2013 memcpy(GETPTR(i), GETPTR(j), sizeof(VALUE)*eltsize); \
2014 memcpy(GETPTR(j), tmp, sizeof(VALUE)*eltsize); \
2015} while (0)
2016
2017 while (1) {
2018 long pivot_index = left + (right-left)/2;
2019 long num_pivots = 1;
2020
2021 SWAP(pivot_index, right);
2022 pivot_index = right;
2023
2024 store_index = left;
2025 i = left;
2026 while (i <= right-num_pivots) {
2027 int c = data->cmpfunc(GETPTR(i), GETPTR(pivot_index), data);
2028 if (data->rev)
2029 c = -c;
2030 if (c == 0) {
2031 SWAP(i, right-num_pivots);
2032 num_pivots++;
2033 continue;
2034 }
2035 if (c < 0) {
2036 SWAP(i, store_index);
2037 store_index++;
2038 }
2039 i++;
2040 }
2041 j = store_index;
2042 for (i = right; right-num_pivots < i; i--) {
2043 if (i <= j)
2044 break;
2045 SWAP(j, i);
2046 j++;
2047 }
2048
2049 if (store_index <= n && n <= store_index+num_pivots)
2050 break;
2051
2052 if (n < store_index) {
2053 right = store_index-1;
2054 }
2055 else {
2056 left = store_index+num_pivots;
2057 }
2058 }
2059#undef GETPTR
2060#undef SWAP
2061
2062 data->limit = RARRAY_AREF(data->buf, store_index*eltsize); /* the last pivot */
2063 data->curlen = data->n;
2064 rb_ary_resize(data->buf, data->n * eltsize);
2065}
2066
2067static VALUE
2068nmin_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, _data))
2069{
2070 struct nmin_data *data = (struct nmin_data *)_data;
2071 VALUE cmpv;
2072
2073 ENUM_WANT_SVALUE();
2074
2075 if (data->by)
2076 cmpv = enum_yield(argc, i);
2077 else
2078 cmpv = i;
2079
2080 if (!UNDEF_P(data->limit)) {
2081 int c = data->cmpfunc(&cmpv, &data->limit, data);
2082 if (data->rev)
2083 c = -c;
2084 if (c >= 0)
2085 return Qnil;
2086 }
2087
2088 if (data->by)
2089 rb_ary_push(data->buf, cmpv);
2090 rb_ary_push(data->buf, i);
2091
2092 data->curlen++;
2093
2094 if (data->curlen == data->bufmax) {
2095 nmin_filter(data);
2096 }
2097
2098 return Qnil;
2099}
2100
2101VALUE
2102rb_nmin_run(VALUE obj, VALUE num, int by, int rev, int ary)
2103{
2104 VALUE result;
2105 struct nmin_data data;
2106
2107 data.n = NUM2LONG(num);
2108 if (data.n < 0)
2109 rb_raise(rb_eArgError, "negative size (%ld)", data.n);
2110 if (data.n == 0)
2111 return rb_ary_new2(0);
2112 if (LONG_MAX/4/(by ? 2 : 1) < data.n)
2113 rb_raise(rb_eArgError, "too big size");
2114 data.bufmax = data.n * 4;
2115 data.curlen = 0;
2116 data.buf = rb_ary_hidden_new(data.bufmax * (by ? 2 : 1));
2117 data.limit = Qundef;
2118 data.cmpfunc = by ? nmin_cmp :
2119 rb_block_given_p() ? nmin_block_cmp :
2120 nmin_cmp;
2121 data.rev = rev;
2122 data.by = by;
2123 if (ary) {
2124 long i;
2125 for (i = 0; i < RARRAY_LEN(obj); i++) {
2126 VALUE args[1];
2127 args[0] = RARRAY_AREF(obj, i);
2128 nmin_i(obj, (VALUE)&data, 1, args, Qundef);
2129 }
2130 }
2131 else {
2132 rb_block_call(obj, id_each, 0, 0, nmin_i, (VALUE)&data);
2133 }
2134 nmin_filter(&data);
2135 result = data.buf;
2136 if (by) {
2137 long i;
2138 RARRAY_PTR_USE(result, ptr, {
2139 ruby_qsort(ptr,
2140 RARRAY_LEN(result)/2,
2141 sizeof(VALUE)*2,
2142 data.cmpfunc, (void *)&data);
2143 for (i=1; i<RARRAY_LEN(result); i+=2) {
2144 ptr[i/2] = ptr[i];
2145 }
2146 });
2147 rb_ary_resize(result, RARRAY_LEN(result)/2);
2148 }
2149 else {
2150 RARRAY_PTR_USE(result, ptr, {
2151 ruby_qsort(ptr, RARRAY_LEN(result), sizeof(VALUE),
2152 data.cmpfunc, (void *)&data);
2153 });
2154 }
2155 if (rev) {
2156 rb_ary_reverse(result);
2157 }
2158 RBASIC_SET_CLASS(result, rb_cArray);
2159 return result;
2160
2161}
2162
2163/*
2164 * call-seq:
2165 * one? -> true or false
2166 * one?(pattern) -> true or false
2167 * one? {|element| ... } -> true or false
2168 *
2169 * Returns whether exactly one element meets a given criterion.
2170 *
2171 * With no argument and no block,
2172 * returns whether exactly one element is truthy:
2173 *
2174 * (1..1).one? # => true
2175 * [1, nil, false].one? # => true
2176 * (1..4).one? # => false
2177 * {foo: 0}.one? # => true
2178 * {foo: 0, bar: 1}.one? # => false
2179 * [].one? # => false
2180 *
2181 * With argument +pattern+ and no block,
2182 * returns whether for exactly one element +element+,
2183 * <tt>pattern === element</tt>:
2184 *
2185 * [nil, false, 0].one?(Integer) # => true
2186 * [nil, false, 0].one?(Numeric) # => true
2187 * [nil, false, 0].one?(Float) # => false
2188 * %w[bar baz bat bam].one?(/m/) # => true
2189 * %w[bar baz bat bam].one?(/foo/) # => false
2190 * %w[bar baz bat bam].one?('ba') # => false
2191 * {foo: 0, bar: 1, baz: 2}.one?(Array) # => false
2192 * {foo: 0}.one?(Array) # => true
2193 * [].one?(Integer) # => false
2194 *
2195 * With a block given, returns whether the block returns a truthy value
2196 * for exactly one element:
2197 *
2198 * (1..4).one? {|element| element < 2 } # => true
2199 * (1..4).one? {|element| element < 1 } # => false
2200 * {foo: 0, bar: 1, baz: 2}.one? {|key, value| value < 1 } # => true
2201 * {foo: 0, bar: 1, baz: 2}.one? {|key, value| value < 2 } # => false
2202 *
2203 * Related: #none?, #all?, #any?.
2204 *
2205 */
2206static VALUE
2207enum_one(int argc, VALUE *argv, VALUE obj)
2208{
2209 struct MEMO *memo = MEMO_ENUM_NEW(Qundef);
2210 VALUE result;
2211
2212 WARN_UNUSED_BLOCK(argc);
2213 ENUM_BLOCK_CALL(one);
2214 result = memo->v1;
2215 if (UNDEF_P(result)) return Qfalse;
2216 return result;
2217}
2218
2219DEFINE_ENUMFUNCS(none)
2220{
2221 if (RTEST(result)) {
2222 MEMO_V1_SET(memo, Qfalse);
2223 rb_iter_break();
2224 }
2225 return Qnil;
2226}
2227
2228/*
2229 * call-seq:
2230 * none? -> true or false
2231 * none?(pattern) -> true or false
2232 * none? {|element| ... } -> true or false
2233 *
2234 * Returns whether no element meets a given criterion.
2235 *
2236 * With no argument and no block,
2237 * returns whether no element is truthy:
2238 *
2239 * (1..4).none? # => false
2240 * [nil, false].none? # => true
2241 * {foo: 0}.none? # => false
2242 * {foo: 0, bar: 1}.none? # => false
2243 * [].none? # => true
2244 *
2245 * With argument +pattern+ and no block,
2246 * returns whether for no element +element+,
2247 * <tt>pattern === element</tt>:
2248 *
2249 * [nil, false, 1.1].none?(Integer) # => true
2250 * %w[bar baz bat bam].none?(/m/) # => false
2251 * %w[bar baz bat bam].none?(/foo/) # => true
2252 * %w[bar baz bat bam].none?('ba') # => true
2253 * {foo: 0, bar: 1, baz: 2}.none?(Hash) # => true
2254 * {foo: 0}.none?(Array) # => false
2255 * [].none?(Integer) # => true
2256 *
2257 * With a block given, returns whether the block returns a truthy value
2258 * for no element:
2259 *
2260 * (1..4).none? {|element| element < 1 } # => true
2261 * (1..4).none? {|element| element < 2 } # => false
2262 * {foo: 0, bar: 1, baz: 2}.none? {|key, value| value < 0 } # => true
2263 * {foo: 0, bar: 1, baz: 2}.none? {|key, value| value < 1 } # => false
2264 *
2265 * Related: #one?, #all?, #any?.
2266 *
2267 */
2268static VALUE
2269enum_none(int argc, VALUE *argv, VALUE obj)
2270{
2271 struct MEMO *memo = MEMO_ENUM_NEW(Qtrue);
2272
2273 WARN_UNUSED_BLOCK(argc);
2274 ENUM_BLOCK_CALL(none);
2275 return memo->v1;
2276}
2277
2278struct min_t {
2279 VALUE min;
2280};
2281
2282static VALUE
2283min_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
2284{
2285 struct min_t *memo = MEMO_FOR(struct min_t, args);
2286
2287 ENUM_WANT_SVALUE();
2288
2289 if (UNDEF_P(memo->min)) {
2290 memo->min = i;
2291 }
2292 else {
2293 if (OPTIMIZED_CMP(i, memo->min) < 0) {
2294 memo->min = i;
2295 }
2296 }
2297 return Qnil;
2298}
2299
2300static VALUE
2301min_ii(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
2302{
2303 VALUE cmp;
2304 struct min_t *memo = MEMO_FOR(struct min_t, args);
2305
2306 ENUM_WANT_SVALUE();
2307
2308 if (UNDEF_P(memo->min)) {
2309 memo->min = i;
2310 }
2311 else {
2312 cmp = rb_yield_values(2, i, memo->min);
2313 if (rb_cmpint(cmp, i, memo->min) < 0) {
2314 memo->min = i;
2315 }
2316 }
2317 return Qnil;
2318}
2319
2320
2321/*
2322 * call-seq:
2323 * min -> element
2324 * min(n) -> array
2325 * min {|a, b| ... } -> element
2326 * min(n) {|a, b| ... } -> array
2327 *
2328 * Returns the element with the minimum element according to a given criterion.
2329 * The ordering of equal elements is indeterminate and may be unstable.
2330 *
2331 * With no argument and no block, returns the minimum element,
2332 * using the elements' own method <tt>#<=></tt> for comparison:
2333 *
2334 * (1..4).min # => 1
2335 * (-4..-1).min # => -4
2336 * %w[d c b a].min # => "a"
2337 * {foo: 0, bar: 1, baz: 2}.min # => [:bar, 1]
2338 * [].min # => nil
2339 *
2340 * With positive integer argument +n+ given, and no block,
2341 * returns an array containing the first +n+ minimum elements that exist:
2342 *
2343 * (1..4).min(2) # => [1, 2]
2344 * (-4..-1).min(2) # => [-4, -3]
2345 * %w[d c b a].min(2) # => ["a", "b"]
2346 * {foo: 0, bar: 1, baz: 2}.min(2) # => [[:bar, 1], [:baz, 2]]
2347 * [].min(2) # => []
2348 *
2349 * With a block given, the block determines the minimum elements.
2350 * The block is called with two elements +a+ and +b+, and must return:
2351 *
2352 * - A negative integer if <tt>a < b</tt>.
2353 * - Zero if <tt>a == b</tt>.
2354 * - A positive integer if <tt>a > b</tt>.
2355 *
2356 * With a block given and no argument,
2357 * returns the minimum element as determined by the block:
2358 *
2359 * %w[xxx x xxxx xx].min {|a, b| a.size <=> b.size } # => "x"
2360 * h = {foo: 0, bar: 1, baz: 2}
2361 * h.min {|pair1, pair2| pair1[1] <=> pair2[1] } # => [:foo, 0]
2362 * [].min {|a, b| a <=> b } # => nil
2363 *
2364 * With a block given and positive integer argument +n+ given,
2365 * returns an array containing the first +n+ minimum elements that exist,
2366 * as determined by the block.
2367 *
2368 * %w[xxx x xxxx xx].min(2) {|a, b| a.size <=> b.size } # => ["x", "xx"]
2369 * h = {foo: 0, bar: 1, baz: 2}
2370 * h.min(2) {|pair1, pair2| pair1[1] <=> pair2[1] }
2371 * # => [[:foo, 0], [:bar, 1]]
2372 * [].min(2) {|a, b| a <=> b } # => []
2373 *
2374 * Related: #min_by, #minmax, #max.
2375 *
2376 */
2377
2378static VALUE
2379enum_min(int argc, VALUE *argv, VALUE obj)
2380{
2381 VALUE memo;
2382 struct min_t *m = NEW_MEMO_FOR(struct min_t, memo);
2383 VALUE result;
2384 VALUE num;
2385
2386 if (rb_check_arity(argc, 0, 1) && !NIL_P(num = argv[0]))
2387 return rb_nmin_run(obj, num, 0, 0, 0);
2388
2389 m->min = Qundef;
2390 if (rb_block_given_p()) {
2391 rb_block_call(obj, id_each, 0, 0, min_ii, memo);
2392 }
2393 else {
2394 rb_block_call(obj, id_each, 0, 0, min_i, memo);
2395 }
2396 result = m->min;
2397 if (UNDEF_P(result)) return Qnil;
2398 return result;
2399}
2400
2401struct max_t {
2402 VALUE max;
2403};
2404
2405static VALUE
2406max_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
2407{
2408 struct max_t *memo = MEMO_FOR(struct max_t, args);
2409
2410 ENUM_WANT_SVALUE();
2411
2412 if (UNDEF_P(memo->max)) {
2413 memo->max = i;
2414 }
2415 else {
2416 if (OPTIMIZED_CMP(i, memo->max) > 0) {
2417 memo->max = i;
2418 }
2419 }
2420 return Qnil;
2421}
2422
2423static VALUE
2424max_ii(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
2425{
2426 struct max_t *memo = MEMO_FOR(struct max_t, args);
2427 VALUE cmp;
2428
2429 ENUM_WANT_SVALUE();
2430
2431 if (UNDEF_P(memo->max)) {
2432 memo->max = i;
2433 }
2434 else {
2435 cmp = rb_yield_values(2, i, memo->max);
2436 if (rb_cmpint(cmp, i, memo->max) > 0) {
2437 memo->max = i;
2438 }
2439 }
2440 return Qnil;
2441}
2442
2443/*
2444 * call-seq:
2445 * max -> element
2446 * max(n) -> array
2447 * max {|a, b| ... } -> element
2448 * max(n) {|a, b| ... } -> array
2449 *
2450 * Returns the element with the maximum element according to a given criterion.
2451 * The ordering of equal elements is indeterminate and may be unstable.
2452 *
2453 * With no argument and no block, returns the maximum element,
2454 * using the elements' own method <tt>#<=></tt> for comparison:
2455 *
2456 * (1..4).max # => 4
2457 * (-4..-1).max # => -1
2458 * %w[d c b a].max # => "d"
2459 * {foo: 0, bar: 1, baz: 2}.max # => [:foo, 0]
2460 * [].max # => nil
2461 *
2462 * With positive integer argument +n+ given, and no block,
2463 * returns an array containing the first +n+ maximum elements that exist:
2464 *
2465 * (1..4).max(2) # => [4, 3]
2466 * (-4..-1).max(2) # => [-1, -2]
2467 * %w[d c b a].max(2) # => ["d", "c"]
2468 * {foo: 0, bar: 1, baz: 2}.max(2) # => [[:foo, 0], [:baz, 2]]
2469 * [].max(2) # => []
2470 *
2471 * With a block given, the block determines the maximum elements.
2472 * The block is called with two elements +a+ and +b+, and must return:
2473 *
2474 * - A negative integer if <tt>a < b</tt>.
2475 * - Zero if <tt>a == b</tt>.
2476 * - A positive integer if <tt>a > b</tt>.
2477 *
2478 * With a block given and no argument,
2479 * returns the maximum element as determined by the block:
2480 *
2481 * %w[xxx x xxxx xx].max {|a, b| a.size <=> b.size } # => "xxxx"
2482 * h = {foo: 0, bar: 1, baz: 2}
2483 * h.max {|pair1, pair2| pair1[1] <=> pair2[1] } # => [:baz, 2]
2484 * [].max {|a, b| a <=> b } # => nil
2485 *
2486 * With a block given and positive integer argument +n+ given,
2487 * returns an array containing the first +n+ maximum elements that exist,
2488 * as determined by the block.
2489 *
2490 * %w[xxx x xxxx xx].max(2) {|a, b| a.size <=> b.size } # => ["xxxx", "xxx"]
2491 * h = {foo: 0, bar: 1, baz: 2}
2492 * h.max(2) {|pair1, pair2| pair1[1] <=> pair2[1] }
2493 * # => [[:baz, 2], [:bar, 1]]
2494 * [].max(2) {|a, b| a <=> b } # => []
2495 *
2496 * Related: #min, #minmax, #max_by.
2497 *
2498 */
2499
2500static VALUE
2501enum_max(int argc, VALUE *argv, VALUE obj)
2502{
2503 VALUE memo;
2504 struct max_t *m = NEW_MEMO_FOR(struct max_t, memo);
2505 VALUE result;
2506 VALUE num;
2507
2508 if (rb_check_arity(argc, 0, 1) && !NIL_P(num = argv[0]))
2509 return rb_nmin_run(obj, num, 0, 1, 0);
2510
2511 m->max = Qundef;
2512 if (rb_block_given_p()) {
2513 rb_block_call(obj, id_each, 0, 0, max_ii, (VALUE)memo);
2514 }
2515 else {
2516 rb_block_call(obj, id_each, 0, 0, max_i, (VALUE)memo);
2517 }
2518 result = m->max;
2519 if (UNDEF_P(result)) return Qnil;
2520 return result;
2521}
2522
2523struct minmax_t {
2524 VALUE min;
2525 VALUE max;
2526 VALUE last;
2527};
2528
2529static void
2530minmax_i_update(VALUE i, VALUE j, struct minmax_t *memo)
2531{
2532 int n;
2533
2534 if (UNDEF_P(memo->min)) {
2535 memo->min = i;
2536 memo->max = j;
2537 }
2538 else {
2539 n = OPTIMIZED_CMP(i, memo->min);
2540 if (n < 0) {
2541 memo->min = i;
2542 }
2543 n = OPTIMIZED_CMP(j, memo->max);
2544 if (n > 0) {
2545 memo->max = j;
2546 }
2547 }
2548}
2549
2550static VALUE
2551minmax_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, _memo))
2552{
2553 struct minmax_t *memo = MEMO_FOR(struct minmax_t, _memo);
2554 int n;
2555 VALUE j;
2556
2557 ENUM_WANT_SVALUE();
2558
2559 if (UNDEF_P(memo->last)) {
2560 memo->last = i;
2561 return Qnil;
2562 }
2563 j = memo->last;
2564 memo->last = Qundef;
2565
2566 n = OPTIMIZED_CMP(j, i);
2567 if (n == 0)
2568 i = j;
2569 else if (n < 0) {
2570 VALUE tmp;
2571 tmp = i;
2572 i = j;
2573 j = tmp;
2574 }
2575
2576 minmax_i_update(i, j, memo);
2577
2578 return Qnil;
2579}
2580
2581static void
2582minmax_ii_update(VALUE i, VALUE j, struct minmax_t *memo)
2583{
2584 int n;
2585
2586 if (UNDEF_P(memo->min)) {
2587 memo->min = i;
2588 memo->max = j;
2589 }
2590 else {
2591 n = rb_cmpint(rb_yield_values(2, i, memo->min), i, memo->min);
2592 if (n < 0) {
2593 memo->min = i;
2594 }
2595 n = rb_cmpint(rb_yield_values(2, j, memo->max), j, memo->max);
2596 if (n > 0) {
2597 memo->max = j;
2598 }
2599 }
2600}
2601
2602static VALUE
2603minmax_ii(RB_BLOCK_CALL_FUNC_ARGLIST(i, _memo))
2604{
2605 struct minmax_t *memo = MEMO_FOR(struct minmax_t, _memo);
2606 int n;
2607 VALUE j;
2608
2609 ENUM_WANT_SVALUE();
2610
2611 if (UNDEF_P(memo->last)) {
2612 memo->last = i;
2613 return Qnil;
2614 }
2615 j = memo->last;
2616 memo->last = Qundef;
2617
2618 n = rb_cmpint(rb_yield_values(2, j, i), j, i);
2619 if (n == 0)
2620 i = j;
2621 else if (n < 0) {
2622 VALUE tmp;
2623 tmp = i;
2624 i = j;
2625 j = tmp;
2626 }
2627
2628 minmax_ii_update(i, j, memo);
2629
2630 return Qnil;
2631}
2632
2633/*
2634 * call-seq:
2635 * minmax -> [minimum, maximum]
2636 * minmax {|a, b| ... } -> [minimum, maximum]
2637 *
2638 * Returns a 2-element array containing the minimum and maximum elements
2639 * according to a given criterion.
2640 * The ordering of equal elements is indeterminate and may be unstable.
2641 *
2642 * With no argument and no block, returns the minimum and maximum elements,
2643 * using the elements' own method <tt>#<=></tt> for comparison:
2644 *
2645 * (1..4).minmax # => [1, 4]
2646 * (-4..-1).minmax # => [-4, -1]
2647 * %w[d c b a].minmax # => ["a", "d"]
2648 * {foo: 0, bar: 1, baz: 2}.minmax # => [[:bar, 1], [:foo, 0]]
2649 * [].minmax # => [nil, nil]
2650 *
2651 * With a block given, returns the minimum and maximum elements
2652 * as determined by the block:
2653 *
2654 * %w[xxx x xxxx xx].minmax {|a, b| a.size <=> b.size } # => ["x", "xxxx"]
2655 * h = {foo: 0, bar: 1, baz: 2}
2656 * h.minmax {|pair1, pair2| pair1[1] <=> pair2[1] }
2657 * # => [[:foo, 0], [:baz, 2]]
2658 * [].minmax {|a, b| a <=> b } # => [nil, nil]
2659 *
2660 * Related: #min, #max, #minmax_by.
2661 *
2662 */
2663
2664static VALUE
2665enum_minmax(VALUE obj)
2666{
2667 VALUE memo;
2668 struct minmax_t *m = NEW_MEMO_FOR(struct minmax_t, memo);
2669
2670 m->min = Qundef;
2671 m->last = Qundef;
2672 if (rb_block_given_p()) {
2673 rb_block_call(obj, id_each, 0, 0, minmax_ii, memo);
2674 if (!UNDEF_P(m->last))
2675 minmax_ii_update(m->last, m->last, m);
2676 }
2677 else {
2678 rb_block_call(obj, id_each, 0, 0, minmax_i, memo);
2679 if (!UNDEF_P(m->last))
2680 minmax_i_update(m->last, m->last, m);
2681 }
2682 if (!UNDEF_P(m->min)) {
2683 return rb_assoc_new(m->min, m->max);
2684 }
2685 return rb_assoc_new(Qnil, Qnil);
2686}
2687
2688static VALUE
2689min_by_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
2690{
2691 struct MEMO *memo = MEMO_CAST(args);
2692 VALUE v;
2693
2694 ENUM_WANT_SVALUE();
2695
2696 v = enum_yield(argc, i);
2697 if (UNDEF_P(memo->v1)) {
2698 MEMO_V1_SET(memo, v);
2699 MEMO_V2_SET(memo, i);
2700 }
2701 else if (OPTIMIZED_CMP(v, memo->v1) < 0) {
2702 MEMO_V1_SET(memo, v);
2703 MEMO_V2_SET(memo, i);
2704 }
2705 return Qnil;
2706}
2707
2708/*
2709 * call-seq:
2710 * min_by {|element| ... } -> element
2711 * min_by(n) {|element| ... } -> array
2712 * min_by -> enumerator
2713 * min_by(n) -> enumerator
2714 *
2715 * Returns the elements for which the block returns the minimum values.
2716 *
2717 * With a block given and no argument,
2718 * returns the element for which the block returns the minimum value:
2719 *
2720 * (1..4).min_by {|element| -element } # => 4
2721 * %w[a b c d].min_by {|element| -element.ord } # => "d"
2722 * {foo: 0, bar: 1, baz: 2}.min_by {|key, value| -value } # => [:baz, 2]
2723 * [].min_by {|element| -element } # => nil
2724 *
2725 * With a block given and positive integer argument +n+ given,
2726 * returns an array containing the +n+ elements
2727 * for which the block returns minimum values:
2728 *
2729 * (1..4).min_by(2) {|element| -element }
2730 * # => [4, 3]
2731 * %w[a b c d].min_by(2) {|element| -element.ord }
2732 * # => ["d", "c"]
2733 * {foo: 0, bar: 1, baz: 2}.min_by(2) {|key, value| -value }
2734 * # => [[:baz, 2], [:bar, 1]]
2735 * [].min_by(2) {|element| -element }
2736 * # => []
2737 *
2738 * Returns an Enumerator if no block is given.
2739 *
2740 * Related: #min, #minmax, #max_by.
2741 *
2742 */
2743
2744static VALUE
2745enum_min_by(int argc, VALUE *argv, VALUE obj)
2746{
2747 struct MEMO *memo;
2748 VALUE num;
2749
2750 rb_check_arity(argc, 0, 1);
2751
2752 RETURN_SIZED_ENUMERATOR(obj, argc, argv, enum_size);
2753
2754 if (argc && !NIL_P(num = argv[0]))
2755 return rb_nmin_run(obj, num, 1, 0, 0);
2756
2757 memo = MEMO_NEW(Qundef, Qnil, 0);
2758 rb_block_call(obj, id_each, 0, 0, min_by_i, (VALUE)memo);
2759 return memo->v2;
2760}
2761
2762static VALUE
2763max_by_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
2764{
2765 struct MEMO *memo = MEMO_CAST(args);
2766 VALUE v;
2767
2768 ENUM_WANT_SVALUE();
2769
2770 v = enum_yield(argc, i);
2771 if (UNDEF_P(memo->v1)) {
2772 MEMO_V1_SET(memo, v);
2773 MEMO_V2_SET(memo, i);
2774 }
2775 else if (OPTIMIZED_CMP(v, memo->v1) > 0) {
2776 MEMO_V1_SET(memo, v);
2777 MEMO_V2_SET(memo, i);
2778 }
2779 return Qnil;
2780}
2781
2782/*
2783 * call-seq:
2784 * max_by {|element| ... } -> element
2785 * max_by(n) {|element| ... } -> array
2786 * max_by -> enumerator
2787 * max_by(n) -> enumerator
2788 *
2789 * Returns the elements for which the block returns the maximum values.
2790 *
2791 * With a block given and no argument,
2792 * returns the element for which the block returns the maximum value:
2793 *
2794 * (1..4).max_by {|element| -element } # => 1
2795 * %w[a b c d].max_by {|element| -element.ord } # => "a"
2796 * {foo: 0, bar: 1, baz: 2}.max_by {|key, value| -value } # => [:foo, 0]
2797 * [].max_by {|element| -element } # => nil
2798 *
2799 * With a block given and positive integer argument +n+ given,
2800 * returns an array containing the +n+ elements
2801 * for which the block returns maximum values:
2802 *
2803 * (1..4).max_by(2) {|element| -element }
2804 * # => [1, 2]
2805 * %w[a b c d].max_by(2) {|element| -element.ord }
2806 * # => ["a", "b"]
2807 * {foo: 0, bar: 1, baz: 2}.max_by(2) {|key, value| -value }
2808 * # => [[:foo, 0], [:bar, 1]]
2809 * [].max_by(2) {|element| -element }
2810 * # => []
2811 *
2812 * Returns an Enumerator if no block is given.
2813 *
2814 * Related: #max, #minmax, #min_by.
2815 *
2816 */
2817
2818static VALUE
2819enum_max_by(int argc, VALUE *argv, VALUE obj)
2820{
2821 struct MEMO *memo;
2822 VALUE num;
2823
2824 rb_check_arity(argc, 0, 1);
2825
2826 RETURN_SIZED_ENUMERATOR(obj, argc, argv, enum_size);
2827
2828 if (argc && !NIL_P(num = argv[0]))
2829 return rb_nmin_run(obj, num, 1, 1, 0);
2830
2831 memo = MEMO_NEW(Qundef, Qnil, 0);
2832 rb_block_call(obj, id_each, 0, 0, max_by_i, (VALUE)memo);
2833 return memo->v2;
2834}
2835
2837 VALUE min_bv;
2838 VALUE max_bv;
2839 VALUE min;
2840 VALUE max;
2841 VALUE last_bv;
2842 VALUE last;
2843};
2844
2845static void
2846minmax_by_i_update(VALUE v1, VALUE v2, VALUE i1, VALUE i2, struct minmax_by_t *memo)
2847{
2848 if (UNDEF_P(memo->min_bv)) {
2849 memo->min_bv = v1;
2850 memo->max_bv = v2;
2851 memo->min = i1;
2852 memo->max = i2;
2853 }
2854 else {
2855 if (OPTIMIZED_CMP(v1, memo->min_bv) < 0) {
2856 memo->min_bv = v1;
2857 memo->min = i1;
2858 }
2859 if (OPTIMIZED_CMP(v2, memo->max_bv) > 0) {
2860 memo->max_bv = v2;
2861 memo->max = i2;
2862 }
2863 }
2864}
2865
2866static VALUE
2867minmax_by_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, _memo))
2868{
2869 struct minmax_by_t *memo = MEMO_FOR(struct minmax_by_t, _memo);
2870 VALUE vi, vj, j;
2871 int n;
2872
2873 ENUM_WANT_SVALUE();
2874
2875 vi = enum_yield(argc, i);
2876
2877 if (UNDEF_P(memo->last_bv)) {
2878 memo->last_bv = vi;
2879 memo->last = i;
2880 return Qnil;
2881 }
2882 vj = memo->last_bv;
2883 j = memo->last;
2884 memo->last_bv = Qundef;
2885
2886 n = OPTIMIZED_CMP(vj, vi);
2887 if (n == 0) {
2888 i = j;
2889 vi = vj;
2890 }
2891 else if (n < 0) {
2892 VALUE tmp;
2893 tmp = i;
2894 i = j;
2895 j = tmp;
2896 tmp = vi;
2897 vi = vj;
2898 vj = tmp;
2899 }
2900
2901 minmax_by_i_update(vi, vj, i, j, memo);
2902
2903 return Qnil;
2904}
2905
2906/*
2907 * call-seq:
2908 * minmax_by {|element| ... } -> [minimum, maximum]
2909 * minmax_by -> enumerator
2910 *
2911 * Returns a 2-element array containing the elements
2912 * for which the block returns minimum and maximum values:
2913 *
2914 * (1..4).minmax_by {|element| -element }
2915 * # => [4, 1]
2916 * %w[a b c d].minmax_by {|element| -element.ord }
2917 * # => ["d", "a"]
2918 * {foo: 0, bar: 1, baz: 2}.minmax_by {|key, value| -value }
2919 * # => [[:baz, 2], [:foo, 0]]
2920 * [].minmax_by {|element| -element }
2921 * # => [nil, nil]
2922 *
2923 * Returns an Enumerator if no block is given.
2924 *
2925 * Related: #max_by, #minmax, #min_by.
2926 *
2927 */
2928
2929static VALUE
2930enum_minmax_by(VALUE obj)
2931{
2932 VALUE memo;
2933 struct minmax_by_t *m = NEW_MEMO_FOR(struct minmax_by_t, memo);
2934
2935 RETURN_SIZED_ENUMERATOR(obj, 0, 0, enum_size);
2936
2937 m->min_bv = Qundef;
2938 m->max_bv = Qundef;
2939 m->min = Qnil;
2940 m->max = Qnil;
2941 m->last_bv = Qundef;
2942 m->last = Qundef;
2943 rb_block_call(obj, id_each, 0, 0, minmax_by_i, memo);
2944 if (!UNDEF_P(m->last_bv))
2945 minmax_by_i_update(m->last_bv, m->last_bv, m->last, m->last, m);
2946 m = MEMO_FOR(struct minmax_by_t, memo);
2947 return rb_assoc_new(m->min, m->max);
2948}
2949
2950static VALUE
2951member_i(RB_BLOCK_CALL_FUNC_ARGLIST(iter, args))
2952{
2953 struct MEMO *memo = MEMO_CAST(args);
2954
2955 if (rb_equal(rb_enum_values_pack(argc, argv), memo->v1)) {
2956 MEMO_V2_SET(memo, Qtrue);
2957 rb_iter_break();
2958 }
2959 return Qnil;
2960}
2961
2962/*
2963 * call-seq:
2964 * include?(object) -> true or false
2965 *
2966 * Returns whether for any element <tt>object == element</tt>:
2967 *
2968 * (1..4).include?(2) # => true
2969 * (1..4).include?(5) # => false
2970 * (1..4).include?('2') # => false
2971 * %w[a b c d].include?('b') # => true
2972 * %w[a b c d].include?('2') # => false
2973 * {foo: 0, bar: 1, baz: 2}.include?(:foo) # => true
2974 * {foo: 0, bar: 1, baz: 2}.include?('foo') # => false
2975 * {foo: 0, bar: 1, baz: 2}.include?(0) # => false
2976 *
2977 */
2978
2979static VALUE
2980enum_member(VALUE obj, VALUE val)
2981{
2982 struct MEMO *memo = MEMO_NEW(val, Qfalse, 0);
2983
2984 rb_block_call(obj, id_each, 0, 0, member_i, (VALUE)memo);
2985 return memo->v2;
2986}
2987
2988static VALUE
2989each_with_index_i(RB_BLOCK_CALL_FUNC_ARGLIST(_, index))
2990{
2991 struct vm_ifunc *ifunc = rb_current_ifunc();
2992 ifunc->data = (const void *)rb_int_succ(index);
2993
2994 return rb_yield_values(2, rb_enum_values_pack(argc, argv), index);
2995}
2996
2997/*
2998 * call-seq:
2999 * each_with_index(*args) {|element, i| ..... } -> self
3000 * each_with_index(*args) -> enumerator
3001 *
3002 * Invoke <tt>self.each</tt> with <tt>*args</tt>.
3003 * With a block given, the block receives each element and its index;
3004 * returns +self+:
3005 *
3006 * h = {}
3007 * (1..4).each_with_index {|element, i| h[element] = i } # => 1..4
3008 * h # => {1=>0, 2=>1, 3=>2, 4=>3}
3009 *
3010 * h = {}
3011 * %w[a b c d].each_with_index {|element, i| h[element] = i }
3012 * # => ["a", "b", "c", "d"]
3013 * h # => {"a"=>0, "b"=>1, "c"=>2, "d"=>3}
3014 *
3015 * a = []
3016 * h = {foo: 0, bar: 1, baz: 2}
3017 * h.each_with_index {|element, i| a.push([i, element]) }
3018 * # => {:foo=>0, :bar=>1, :baz=>2}
3019 * a # => [[0, [:foo, 0]], [1, [:bar, 1]], [2, [:baz, 2]]]
3020 *
3021 * With no block given, returns an Enumerator.
3022 *
3023 */
3024
3025static VALUE
3026enum_each_with_index(int argc, VALUE *argv, VALUE obj)
3027{
3028 RETURN_SIZED_ENUMERATOR(obj, argc, argv, enum_size);
3029
3030 rb_block_call(obj, id_each, argc, argv, each_with_index_i, INT2FIX(0));
3031 return obj;
3032}
3033
3034
3035/*
3036 * call-seq:
3037 * reverse_each(*args) {|element| ... } -> self
3038 * reverse_each(*args) -> enumerator
3039 *
3040 * With a block given, calls the block with each element,
3041 * but in reverse order; returns +self+:
3042 *
3043 * a = []
3044 * (1..4).reverse_each {|element| a.push(-element) } # => 1..4
3045 * a # => [-4, -3, -2, -1]
3046 *
3047 * a = []
3048 * %w[a b c d].reverse_each {|element| a.push(element) }
3049 * # => ["a", "b", "c", "d"]
3050 * a # => ["d", "c", "b", "a"]
3051 *
3052 * a = []
3053 * h.reverse_each {|element| a.push(element) }
3054 * # => {:foo=>0, :bar=>1, :baz=>2}
3055 * a # => [[:baz, 2], [:bar, 1], [:foo, 0]]
3056 *
3057 * With no block given, returns an Enumerator.
3058 *
3059 */
3060
3061static VALUE
3062enum_reverse_each(int argc, VALUE *argv, VALUE obj)
3063{
3064 VALUE ary;
3065 long len;
3066
3067 RETURN_SIZED_ENUMERATOR(obj, argc, argv, enum_size);
3068
3069 ary = enum_to_a(argc, argv, obj);
3070
3071 len = RARRAY_LEN(ary);
3072 while (len--) {
3073 long nlen;
3074 rb_yield(RARRAY_AREF(ary, len));
3075 nlen = RARRAY_LEN(ary);
3076 if (nlen < len) {
3077 len = nlen;
3078 }
3079 }
3080
3081 return obj;
3082}
3083
3084
3085static VALUE
3086each_val_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, p))
3087{
3088 ENUM_WANT_SVALUE();
3089 enum_yield(argc, i);
3090 return Qnil;
3091}
3092
3093/*
3094 * call-seq:
3095 * each_entry(*args) {|element| ... } -> self
3096 * each_entry(*args) -> enumerator
3097 *
3098 * Calls the given block with each element,
3099 * converting multiple values from yield to an array; returns +self+:
3100 *
3101 * a = []
3102 * (1..4).each_entry {|element| a.push(element) } # => 1..4
3103 * a # => [1, 2, 3, 4]
3104 *
3105 * a = []
3106 * h = {foo: 0, bar: 1, baz:2}
3107 * h.each_entry {|element| a.push(element) }
3108 * # => {:foo=>0, :bar=>1, :baz=>2}
3109 * a # => [[:foo, 0], [:bar, 1], [:baz, 2]]
3110 *
3111 * class Foo
3112 * include Enumerable
3113 * def each
3114 * yield 1
3115 * yield 1, 2
3116 * yield
3117 * end
3118 * end
3119 * Foo.new.each_entry {|yielded| p yielded }
3120 *
3121 * Output:
3122 *
3123 * 1
3124 * [1, 2]
3125 * nil
3126 *
3127 * With no block given, returns an Enumerator.
3128 *
3129 */
3130
3131static VALUE
3132enum_each_entry(int argc, VALUE *argv, VALUE obj)
3133{
3134 RETURN_SIZED_ENUMERATOR(obj, argc, argv, enum_size);
3135 rb_block_call(obj, id_each, argc, argv, each_val_i, 0);
3136 return obj;
3137}
3138
3139static VALUE
3140add_int(VALUE x, long n)
3141{
3142 const VALUE y = LONG2NUM(n);
3143 if (RB_INTEGER_TYPE_P(x)) return rb_int_plus(x, y);
3144 return rb_funcallv(x, '+', 1, &y);
3145}
3146
3147static VALUE
3148div_int(VALUE x, long n)
3149{
3150 const VALUE y = LONG2NUM(n);
3151 if (RB_INTEGER_TYPE_P(x)) return rb_int_idiv(x, y);
3152 return rb_funcallv(x, id_div, 1, &y);
3153}
3154
3155#define dont_recycle_block_arg(arity) ((arity) == 1 || (arity) < 0)
3156
3157static VALUE
3158each_slice_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, m))
3159{
3160 struct MEMO *memo = MEMO_CAST(m);
3161 VALUE ary = memo->v1;
3162 VALUE v = Qnil;
3163 long size = memo->u3.cnt;
3164 ENUM_WANT_SVALUE();
3165
3166 rb_ary_push(ary, i);
3167
3168 if (RARRAY_LEN(ary) == size) {
3169 v = rb_yield(ary);
3170
3171 if (memo->v2) {
3172 MEMO_V1_SET(memo, rb_ary_new2(size));
3173 }
3174 else {
3175 rb_ary_clear(ary);
3176 }
3177 }
3178
3179 return v;
3180}
3181
3182static VALUE
3183enum_each_slice_size(VALUE obj, VALUE args, VALUE eobj)
3184{
3185 VALUE n, size;
3186 long slice_size = NUM2LONG(RARRAY_AREF(args, 0));
3187 ID infinite_p;
3188 CONST_ID(infinite_p, "infinite?");
3189 if (slice_size <= 0) rb_raise(rb_eArgError, "invalid slice size");
3190
3191 size = enum_size(obj, 0, 0);
3192 if (NIL_P(size)) return Qnil;
3193 if (RB_FLOAT_TYPE_P(size) && RTEST(rb_funcall(size, infinite_p, 0))) {
3194 return size;
3195 }
3196
3197 n = add_int(size, slice_size-1);
3198 return div_int(n, slice_size);
3199}
3200
3201/*
3202 * call-seq:
3203 * each_slice(n) { ... } -> self
3204 * each_slice(n) -> enumerator
3205 *
3206 * Calls the block with each successive disjoint +n+-tuple of elements;
3207 * returns +self+:
3208 *
3209 * a = []
3210 * (1..10).each_slice(3) {|tuple| a.push(tuple) }
3211 * a # => [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]
3212 *
3213 * a = []
3214 * h = {foo: 0, bar: 1, baz: 2, bat: 3, bam: 4}
3215 * h.each_slice(2) {|tuple| a.push(tuple) }
3216 * a # => [[[:foo, 0], [:bar, 1]], [[:baz, 2], [:bat, 3]], [[:bam, 4]]]
3217 *
3218 * With no block given, returns an Enumerator.
3219 *
3220 */
3221static VALUE
3222enum_each_slice(VALUE obj, VALUE n)
3223{
3224 long size = NUM2LONG(n);
3225 VALUE ary;
3226 struct MEMO *memo;
3227 int arity;
3228
3229 if (size <= 0) rb_raise(rb_eArgError, "invalid slice size");
3230 RETURN_SIZED_ENUMERATOR(obj, 1, &n, enum_each_slice_size);
3231 size = limit_by_enum_size(obj, size);
3232 ary = rb_ary_new2(size);
3233 arity = rb_block_arity();
3234 memo = MEMO_NEW(ary, dont_recycle_block_arg(arity), size);
3235 rb_block_call(obj, id_each, 0, 0, each_slice_i, (VALUE)memo);
3236 ary = memo->v1;
3237 if (RARRAY_LEN(ary) > 0) rb_yield(ary);
3238
3239 return obj;
3240}
3241
3242static VALUE
3243each_cons_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
3244{
3245 struct MEMO *memo = MEMO_CAST(args);
3246 VALUE ary = memo->v1;
3247 VALUE v = Qnil;
3248 long size = memo->u3.cnt;
3249 ENUM_WANT_SVALUE();
3250
3251 if (RARRAY_LEN(ary) == size) {
3252 rb_ary_shift(ary);
3253 }
3254 rb_ary_push(ary, i);
3255 if (RARRAY_LEN(ary) == size) {
3256 if (memo->v2) {
3257 ary = rb_ary_dup(ary);
3258 }
3259 v = rb_yield(ary);
3260 }
3261 return v;
3262}
3263
3264static VALUE
3265enum_each_cons_size(VALUE obj, VALUE args, VALUE eobj)
3266{
3267 const VALUE zero = LONG2FIX(0);
3268 VALUE n, size;
3269 long cons_size = NUM2LONG(RARRAY_AREF(args, 0));
3270 if (cons_size <= 0) rb_raise(rb_eArgError, "invalid size");
3271
3272 size = enum_size(obj, 0, 0);
3273 if (NIL_P(size)) return Qnil;
3274
3275 n = add_int(size, 1 - cons_size);
3276 return (OPTIMIZED_CMP(n, zero) == -1) ? zero : n;
3277}
3278
3279/*
3280 * call-seq:
3281 * each_cons(n) { ... } -> self
3282 * each_cons(n) -> enumerator
3283 *
3284 * Calls the block with each successive overlapped +n+-tuple of elements;
3285 * returns +self+:
3286 *
3287 * a = []
3288 * (1..5).each_cons(3) {|element| a.push(element) }
3289 * a # => [[1, 2, 3], [2, 3, 4], [3, 4, 5]]
3290 *
3291 * a = []
3292 * h = {foo: 0, bar: 1, baz: 2, bam: 3}
3293 * h.each_cons(2) {|element| a.push(element) }
3294 * a # => [[[:foo, 0], [:bar, 1]], [[:bar, 1], [:baz, 2]], [[:baz, 2], [:bam, 3]]]
3295 *
3296 * With no block given, returns an Enumerator.
3297 *
3298 */
3299static VALUE
3300enum_each_cons(VALUE obj, VALUE n)
3301{
3302 long size = NUM2LONG(n);
3303 struct MEMO *memo;
3304 int arity;
3305
3306 if (size <= 0) rb_raise(rb_eArgError, "invalid size");
3307 RETURN_SIZED_ENUMERATOR(obj, 1, &n, enum_each_cons_size);
3308 arity = rb_block_arity();
3309 if (enum_size_over_p(obj, size)) return obj;
3310 memo = MEMO_NEW(rb_ary_new2(size), dont_recycle_block_arg(arity), size);
3311 rb_block_call(obj, id_each, 0, 0, each_cons_i, (VALUE)memo);
3312
3313 return obj;
3314}
3315
3316static VALUE
3317each_with_object_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, memo))
3318{
3319 ENUM_WANT_SVALUE();
3320 return rb_yield_values(2, i, memo);
3321}
3322
3323/*
3324 * call-seq:
3325 * each_with_object(object) { |(*args), memo_object| ... } -> object
3326 * each_with_object(object) -> enumerator
3327 *
3328 * Calls the block once for each element, passing both the element
3329 * and the given object:
3330 *
3331 * (1..4).each_with_object([]) {|i, a| a.push(i**2) }
3332 * # => [1, 4, 9, 16]
3333 *
3334 * {foo: 0, bar: 1, baz: 2}.each_with_object({}) {|(k, v), h| h[v] = k }
3335 * # => {0=>:foo, 1=>:bar, 2=>:baz}
3336 *
3337 * With no block given, returns an Enumerator.
3338 *
3339 */
3340static VALUE
3341enum_each_with_object(VALUE obj, VALUE memo)
3342{
3343 RETURN_SIZED_ENUMERATOR(obj, 1, &memo, enum_size);
3344
3345 rb_block_call(obj, id_each, 0, 0, each_with_object_i, memo);
3346
3347 return memo;
3348}
3349
3350static VALUE
3351zip_ary(RB_BLOCK_CALL_FUNC_ARGLIST(val, memoval))
3352{
3353 struct MEMO *memo = (struct MEMO *)memoval;
3354 VALUE result = memo->v1;
3355 VALUE args = memo->v2;
3356 long n = memo->u3.cnt++;
3357 VALUE tmp;
3358 int i;
3359
3360 tmp = rb_ary_new2(RARRAY_LEN(args) + 1);
3361 rb_ary_store(tmp, 0, rb_enum_values_pack(argc, argv));
3362 for (i=0; i<RARRAY_LEN(args); i++) {
3363 VALUE e = RARRAY_AREF(args, i);
3364
3365 if (RARRAY_LEN(e) <= n) {
3366 rb_ary_push(tmp, Qnil);
3367 }
3368 else {
3369 rb_ary_push(tmp, RARRAY_AREF(e, n));
3370 }
3371 }
3372 if (NIL_P(result)) {
3373 enum_yield_array(tmp);
3374 }
3375 else {
3376 rb_ary_push(result, tmp);
3377 }
3378
3379 RB_GC_GUARD(args);
3380
3381 return Qnil;
3382}
3383
3384static VALUE
3385call_next(VALUE w)
3386{
3387 VALUE *v = (VALUE *)w;
3388 return v[0] = rb_funcallv(v[1], id_next, 0, 0);
3389}
3390
3391static VALUE
3392call_stop(VALUE w, VALUE _)
3393{
3394 VALUE *v = (VALUE *)w;
3395 return v[0] = Qundef;
3396}
3397
3398static VALUE
3399zip_i(RB_BLOCK_CALL_FUNC_ARGLIST(val, memoval))
3400{
3401 struct MEMO *memo = (struct MEMO *)memoval;
3402 VALUE result = memo->v1;
3403 VALUE args = memo->v2;
3404 VALUE tmp;
3405 int i;
3406
3407 tmp = rb_ary_new2(RARRAY_LEN(args) + 1);
3408 rb_ary_store(tmp, 0, rb_enum_values_pack(argc, argv));
3409 for (i=0; i<RARRAY_LEN(args); i++) {
3410 if (NIL_P(RARRAY_AREF(args, i))) {
3411 rb_ary_push(tmp, Qnil);
3412 }
3413 else {
3414 VALUE v[2];
3415
3416 v[1] = RARRAY_AREF(args, i);
3417 rb_rescue2(call_next, (VALUE)v, call_stop, (VALUE)v, rb_eStopIteration, (VALUE)0);
3418 if (UNDEF_P(v[0])) {
3419 RARRAY_ASET(args, i, Qnil);
3420 v[0] = Qnil;
3421 }
3422 rb_ary_push(tmp, v[0]);
3423 }
3424 }
3425 if (NIL_P(result)) {
3426 enum_yield_array(tmp);
3427 }
3428 else {
3429 rb_ary_push(result, tmp);
3430 }
3431
3432 RB_GC_GUARD(args);
3433
3434 return Qnil;
3435}
3436
3437/*
3438 * call-seq:
3439 * zip(*other_enums) -> array
3440 * zip(*other_enums) {|array| ... } -> nil
3441 *
3442 * With no block given, returns a new array +new_array+ of size self.size
3443 * whose elements are arrays.
3444 * Each nested array <tt>new_array[n]</tt>
3445 * is of size <tt>other_enums.size+1</tt>, and contains:
3446 *
3447 * - The +n+-th element of self.
3448 * - The +n+-th element of each of the +other_enums+.
3449 *
3450 * If all +other_enums+ and self are the same size,
3451 * all elements are included in the result, and there is no +nil+-filling:
3452 *
3453 * a = [:a0, :a1, :a2, :a3]
3454 * b = [:b0, :b1, :b2, :b3]
3455 * c = [:c0, :c1, :c2, :c3]
3456 * d = a.zip(b, c)
3457 * d # => [[:a0, :b0, :c0], [:a1, :b1, :c1], [:a2, :b2, :c2], [:a3, :b3, :c3]]
3458 *
3459 * f = {foo: 0, bar: 1, baz: 2}
3460 * g = {goo: 3, gar: 4, gaz: 5}
3461 * h = {hoo: 6, har: 7, haz: 8}
3462 * d = f.zip(g, h)
3463 * d # => [
3464 * # [[:foo, 0], [:goo, 3], [:hoo, 6]],
3465 * # [[:bar, 1], [:gar, 4], [:har, 7]],
3466 * # [[:baz, 2], [:gaz, 5], [:haz, 8]]
3467 * # ]
3468 *
3469 * If any enumerable in other_enums is smaller than self,
3470 * fills to <tt>self.size</tt> with +nil+:
3471 *
3472 * a = [:a0, :a1, :a2, :a3]
3473 * b = [:b0, :b1, :b2]
3474 * c = [:c0, :c1]
3475 * d = a.zip(b, c)
3476 * d # => [[:a0, :b0, :c0], [:a1, :b1, :c1], [:a2, :b2, nil], [:a3, nil, nil]]
3477 *
3478 * If any enumerable in other_enums is larger than self,
3479 * its trailing elements are ignored:
3480 *
3481 * a = [:a0, :a1, :a2, :a3]
3482 * b = [:b0, :b1, :b2, :b3, :b4]
3483 * c = [:c0, :c1, :c2, :c3, :c4, :c5]
3484 * d = a.zip(b, c)
3485 * d # => [[:a0, :b0, :c0], [:a1, :b1, :c1], [:a2, :b2, :c2], [:a3, :b3, :c3]]
3486 *
3487 * When a block is given, calls the block with each of the sub-arrays
3488 * (formed as above); returns nil:
3489 *
3490 * a = [:a0, :a1, :a2, :a3]
3491 * b = [:b0, :b1, :b2, :b3]
3492 * c = [:c0, :c1, :c2, :c3]
3493 * a.zip(b, c) {|sub_array| p sub_array} # => nil
3494 *
3495 * Output:
3496 *
3497 * [:a0, :b0, :c0]
3498 * [:a1, :b1, :c1]
3499 * [:a2, :b2, :c2]
3500 * [:a3, :b3, :c3]
3501 *
3502 */
3503
3504static VALUE
3505enum_zip(int argc, VALUE *argv, VALUE obj)
3506{
3507 int i;
3508 ID conv;
3509 struct MEMO *memo;
3510 VALUE result = Qnil;
3511 VALUE args = rb_ary_new4(argc, argv);
3512 int allary = TRUE;
3513
3514 argv = RARRAY_PTR(args);
3515 for (i=0; i<argc; i++) {
3516 VALUE ary = rb_check_array_type(argv[i]);
3517 if (NIL_P(ary)) {
3518 allary = FALSE;
3519 break;
3520 }
3521 argv[i] = ary;
3522 }
3523 if (!allary) {
3524 static const VALUE sym_each = STATIC_ID2SYM(id_each);
3525 CONST_ID(conv, "to_enum");
3526 for (i=0; i<argc; i++) {
3527 if (!rb_respond_to(argv[i], id_each)) {
3528 rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (must respond to :each)",
3529 rb_obj_class(argv[i]));
3530 }
3531 argv[i] = rb_funcallv(argv[i], conv, 1, &sym_each);
3532 }
3533 }
3534 if (!rb_block_given_p()) {
3535 result = rb_ary_new();
3536 }
3537
3538 /* TODO: use NODE_DOT2 as memo(v, v, -) */
3539 memo = MEMO_NEW(result, args, 0);
3540 rb_block_call(obj, id_each, 0, 0, allary ? zip_ary : zip_i, (VALUE)memo);
3541
3542 return result;
3543}
3544
3545static VALUE
3546take_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
3547{
3548 struct MEMO *memo = MEMO_CAST(args);
3549 rb_ary_push(memo->v1, rb_enum_values_pack(argc, argv));
3550 if (--memo->u3.cnt == 0) rb_iter_break();
3551 return Qnil;
3552}
3553
3554/*
3555 * call-seq:
3556 * take(n) -> array
3557 *
3558 * For non-negative integer +n+, returns the first +n+ elements:
3559 *
3560 * r = (1..4)
3561 * r.take(2) # => [1, 2]
3562 * r.take(0) # => []
3563 *
3564 * h = {foo: 0, bar: 1, baz: 2, bat: 3}
3565 * h.take(2) # => [[:foo, 0], [:bar, 1]]
3566 *
3567 */
3568
3569static VALUE
3570enum_take(VALUE obj, VALUE n)
3571{
3572 struct MEMO *memo;
3573 VALUE result;
3574 long len = NUM2LONG(n);
3575
3576 if (len < 0) {
3577 rb_raise(rb_eArgError, "attempt to take negative size");
3578 }
3579
3580 if (len == 0) return rb_ary_new2(0);
3581 result = rb_ary_new2(len);
3582 memo = MEMO_NEW(result, 0, len);
3583 rb_block_call(obj, id_each, 0, 0, take_i, (VALUE)memo);
3584 return result;
3585}
3586
3587
3588static VALUE
3589take_while_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, ary))
3590{
3591 if (!RTEST(rb_yield_values2(argc, argv))) rb_iter_break();
3592 rb_ary_push(ary, rb_enum_values_pack(argc, argv));
3593 return Qnil;
3594}
3595
3596/*
3597 * call-seq:
3598 * take_while {|element| ... } -> array
3599 * take_while -> enumerator
3600 *
3601 * Calls the block with successive elements as long as the block
3602 * returns a truthy value;
3603 * returns an array of all elements up to that point:
3604 *
3605 *
3606 * (1..4).take_while{|i| i < 3 } # => [1, 2]
3607 * h = {foo: 0, bar: 1, baz: 2}
3608 * h.take_while{|element| key, value = *element; value < 2 }
3609 * # => [[:foo, 0], [:bar, 1]]
3610 *
3611 * With no block given, returns an Enumerator.
3612 *
3613 */
3614
3615static VALUE
3616enum_take_while(VALUE obj)
3617{
3618 VALUE ary;
3619
3620 RETURN_ENUMERATOR(obj, 0, 0);
3621 ary = rb_ary_new();
3622 rb_block_call(obj, id_each, 0, 0, take_while_i, ary);
3623 return ary;
3624}
3625
3626static VALUE
3627drop_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
3628{
3629 struct MEMO *memo = MEMO_CAST(args);
3630 if (memo->u3.cnt == 0) {
3631 rb_ary_push(memo->v1, rb_enum_values_pack(argc, argv));
3632 }
3633 else {
3634 memo->u3.cnt--;
3635 }
3636 return Qnil;
3637}
3638
3639/*
3640 * call-seq:
3641 * drop(n) -> array
3642 *
3643 * For positive integer +n+, returns an array containing
3644 * all but the first +n+ elements:
3645 *
3646 * r = (1..4)
3647 * r.drop(3) # => [4]
3648 * r.drop(2) # => [3, 4]
3649 * r.drop(1) # => [2, 3, 4]
3650 * r.drop(0) # => [1, 2, 3, 4]
3651 * r.drop(50) # => []
3652 *
3653 * h = {foo: 0, bar: 1, baz: 2, bat: 3}
3654 * h.drop(2) # => [[:baz, 2], [:bat, 3]]
3655 *
3656 */
3657
3658static VALUE
3659enum_drop(VALUE obj, VALUE n)
3660{
3661 VALUE result;
3662 struct MEMO *memo;
3663 long len = NUM2LONG(n);
3664
3665 if (len < 0) {
3666 rb_raise(rb_eArgError, "attempt to drop negative size");
3667 }
3668
3669 result = rb_ary_new();
3670 memo = MEMO_NEW(result, 0, len);
3671 rb_block_call(obj, id_each, 0, 0, drop_i, (VALUE)memo);
3672 return result;
3673}
3674
3675
3676static VALUE
3677drop_while_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
3678{
3679 struct MEMO *memo = MEMO_CAST(args);
3680 ENUM_WANT_SVALUE();
3681
3682 if (!memo->u3.state && !RTEST(enum_yield(argc, i))) {
3683 memo->u3.state = TRUE;
3684 }
3685 if (memo->u3.state) {
3686 rb_ary_push(memo->v1, i);
3687 }
3688 return Qnil;
3689}
3690
3691/*
3692 * call-seq:
3693 * drop_while {|element| ... } -> array
3694 * drop_while -> enumerator
3695 *
3696 * Calls the block with successive elements as long as the block
3697 * returns a truthy value;
3698 * returns an array of all elements after that point:
3699 *
3700 *
3701 * (1..4).drop_while{|i| i < 3 } # => [3, 4]
3702 * h = {foo: 0, bar: 1, baz: 2}
3703 * a = h.drop_while{|element| key, value = *element; value < 2 }
3704 * a # => [[:baz, 2]]
3705 *
3706 * With no block given, returns an Enumerator.
3707 *
3708 * e = (1..4).drop_while
3709 * p e #=> #<Enumerator: 1..4:drop_while>
3710 * i = e.next; p i; e.feed(i < 3) #=> 1
3711 * i = e.next; p i; e.feed(i < 3) #=> 2
3712 * i = e.next; p i; e.feed(i < 3) #=> 3
3713 * begin
3714 * e.next
3715 * rescue StopIteration
3716 * p $!.result #=> [3, 4]
3717 * end
3718 *
3719 */
3720
3721static VALUE
3722enum_drop_while(VALUE obj)
3723{
3724 VALUE result;
3725 struct MEMO *memo;
3726
3727 RETURN_ENUMERATOR(obj, 0, 0);
3728 result = rb_ary_new();
3729 memo = MEMO_NEW(result, 0, FALSE);
3730 rb_block_call(obj, id_each, 0, 0, drop_while_i, (VALUE)memo);
3731 return result;
3732}
3733
3734static VALUE
3735cycle_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, ary))
3736{
3737 ENUM_WANT_SVALUE();
3738
3739 rb_ary_push(ary, argc > 1 ? i : rb_ary_new_from_values(argc, argv));
3740 enum_yield(argc, i);
3741 return Qnil;
3742}
3743
3744static VALUE
3745enum_cycle_size(VALUE self, VALUE args, VALUE eobj)
3746{
3747 long mul = 0;
3748 VALUE n = Qnil;
3749 VALUE size;
3750
3751 if (args && (RARRAY_LEN(args) > 0)) {
3752 n = RARRAY_AREF(args, 0);
3753 if (!NIL_P(n)) mul = NUM2LONG(n);
3754 }
3755
3756 size = enum_size(self, args, 0);
3757 if (NIL_P(size) || FIXNUM_ZERO_P(size)) return size;
3758
3759 if (NIL_P(n)) return DBL2NUM(HUGE_VAL);
3760 if (mul <= 0) return INT2FIX(0);
3761 n = LONG2FIX(mul);
3762 return rb_funcallv(size, '*', 1, &n);
3763}
3764
3765/*
3766 * call-seq:
3767 * cycle(n = nil) {|element| ...} -> nil
3768 * cycle(n = nil) -> enumerator
3769 *
3770 * When called with positive integer argument +n+ and a block,
3771 * calls the block with each element, then does so again,
3772 * until it has done so +n+ times; returns +nil+:
3773 *
3774 * a = []
3775 * (1..4).cycle(3) {|element| a.push(element) } # => nil
3776 * a # => [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]
3777 * a = []
3778 * ('a'..'d').cycle(2) {|element| a.push(element) }
3779 * a # => ["a", "b", "c", "d", "a", "b", "c", "d"]
3780 * a = []
3781 * {foo: 0, bar: 1, baz: 2}.cycle(2) {|element| a.push(element) }
3782 * a # => [[:foo, 0], [:bar, 1], [:baz, 2], [:foo, 0], [:bar, 1], [:baz, 2]]
3783 *
3784 * If count is zero or negative, does not call the block.
3785 *
3786 * When called with a block and +n+ is +nil+, cycles forever.
3787 *
3788 * When no block is given, returns an Enumerator.
3789 *
3790 */
3791
3792static VALUE
3793enum_cycle(int argc, VALUE *argv, VALUE obj)
3794{
3795 VALUE ary;
3796 VALUE nv = Qnil;
3797 long n, i, len;
3798
3799 rb_check_arity(argc, 0, 1);
3800
3801 RETURN_SIZED_ENUMERATOR(obj, argc, argv, enum_cycle_size);
3802 if (!argc || NIL_P(nv = argv[0])) {
3803 n = -1;
3804 }
3805 else {
3806 n = NUM2LONG(nv);
3807 if (n <= 0) return Qnil;
3808 }
3809 ary = rb_ary_new();
3810 RBASIC_CLEAR_CLASS(ary);
3811 rb_block_call(obj, id_each, 0, 0, cycle_i, ary);
3812 len = RARRAY_LEN(ary);
3813 if (len == 0) return Qnil;
3814 while (n < 0 || 0 < --n) {
3815 for (i=0; i<len; i++) {
3816 enum_yield_array(RARRAY_AREF(ary, i));
3817 }
3818 }
3819 return Qnil;
3820}
3821
3823 VALUE categorize;
3824 VALUE prev_value;
3825 VALUE prev_elts;
3826 VALUE yielder;
3827};
3828
3829static VALUE
3830chunk_ii(RB_BLOCK_CALL_FUNC_ARGLIST(i, _argp))
3831{
3832 struct chunk_arg *argp = MEMO_FOR(struct chunk_arg, _argp);
3833 VALUE v, s;
3834 VALUE alone = ID2SYM(id__alone);
3835 VALUE separator = ID2SYM(id__separator);
3836
3837 ENUM_WANT_SVALUE();
3838
3839 v = rb_funcallv(argp->categorize, id_call, 1, &i);
3840
3841 if (v == alone) {
3842 if (!NIL_P(argp->prev_value)) {
3843 s = rb_assoc_new(argp->prev_value, argp->prev_elts);
3844 rb_funcallv(argp->yielder, id_lshift, 1, &s);
3845 argp->prev_value = argp->prev_elts = Qnil;
3846 }
3847 v = rb_assoc_new(v, rb_ary_new3(1, i));
3848 rb_funcallv(argp->yielder, id_lshift, 1, &v);
3849 }
3850 else if (NIL_P(v) || v == separator) {
3851 if (!NIL_P(argp->prev_value)) {
3852 v = rb_assoc_new(argp->prev_value, argp->prev_elts);
3853 rb_funcallv(argp->yielder, id_lshift, 1, &v);
3854 argp->prev_value = argp->prev_elts = Qnil;
3855 }
3856 }
3857 else if (SYMBOL_P(v) && (s = rb_sym2str(v), RSTRING_PTR(s)[0] == '_')) {
3858 rb_raise(rb_eRuntimeError, "symbols beginning with an underscore are reserved");
3859 }
3860 else {
3861 if (NIL_P(argp->prev_value)) {
3862 argp->prev_value = v;
3863 argp->prev_elts = rb_ary_new3(1, i);
3864 }
3865 else {
3866 if (rb_equal(argp->prev_value, v)) {
3867 rb_ary_push(argp->prev_elts, i);
3868 }
3869 else {
3870 s = rb_assoc_new(argp->prev_value, argp->prev_elts);
3871 rb_funcallv(argp->yielder, id_lshift, 1, &s);
3872 argp->prev_value = v;
3873 argp->prev_elts = rb_ary_new3(1, i);
3874 }
3875 }
3876 }
3877 return Qnil;
3878}
3879
3880static VALUE
3882{
3883 VALUE enumerable;
3884 VALUE arg;
3885 struct chunk_arg *memo = NEW_MEMO_FOR(struct chunk_arg, arg);
3886
3887 enumerable = rb_ivar_get(enumerator, id_chunk_enumerable);
3888 memo->categorize = rb_ivar_get(enumerator, id_chunk_categorize);
3889 memo->prev_value = Qnil;
3890 memo->prev_elts = Qnil;
3891 memo->yielder = yielder;
3892
3893 rb_block_call(enumerable, id_each, 0, 0, chunk_ii, arg);
3894 memo = MEMO_FOR(struct chunk_arg, arg);
3895 if (!NIL_P(memo->prev_elts)) {
3896 arg = rb_assoc_new(memo->prev_value, memo->prev_elts);
3897 rb_funcallv(memo->yielder, id_lshift, 1, &arg);
3898 }
3899 return Qnil;
3900}
3901
3902/*
3903 * call-seq:
3904 * chunk {|array| ... } -> enumerator
3905 *
3906 * Each element in the returned enumerator is a 2-element array consisting of:
3907 *
3908 * - A value returned by the block.
3909 * - An array ("chunk") containing the element for which that value was returned,
3910 * and all following elements for which the block returned the same value:
3911 *
3912 * So that:
3913 *
3914 * - Each block return value that is different from its predecessor
3915 * begins a new chunk.
3916 * - Each block return value that is the same as its predecessor
3917 * continues the same chunk.
3918 *
3919 * Example:
3920 *
3921 * e = (0..10).chunk {|i| (i / 3).floor } # => #<Enumerator: ...>
3922 * # The enumerator elements.
3923 * e.next # => [0, [0, 1, 2]]
3924 * e.next # => [1, [3, 4, 5]]
3925 * e.next # => [2, [6, 7, 8]]
3926 * e.next # => [3, [9, 10]]
3927 *
3928 * Method +chunk+ is especially useful for an enumerable that is already sorted.
3929 * This example counts words for each initial letter in a large array of words:
3930 *
3931 * # Get sorted words from a web page.
3932 * url = 'https://raw.githubusercontent.com/eneko/data-repository/master/data/words.txt'
3933 * words = URI::open(url).readlines
3934 * # Make chunks, one for each letter.
3935 * e = words.chunk {|word| word.upcase[0] } # => #<Enumerator: ...>
3936 * # Display 'A' through 'F'.
3937 * e.each {|c, words| p [c, words.length]; break if c == 'F' }
3938 *
3939 * Output:
3940 *
3941 * ["A", 17096]
3942 * ["B", 11070]
3943 * ["C", 19901]
3944 * ["D", 10896]
3945 * ["E", 8736]
3946 * ["F", 6860]
3947 *
3948 * You can use the special symbol <tt>:_alone</tt> to force an element
3949 * into its own separate chuck:
3950 *
3951 * a = [0, 0, 1, 1]
3952 * e = a.chunk{|i| i.even? ? :_alone : true }
3953 * e.to_a # => [[:_alone, [0]], [:_alone, [0]], [true, [1, 1]]]
3954 *
3955 * For example, you can put each line that contains a URL into its own chunk:
3956 *
3957 * pattern = /http/
3958 * open(filename) { |f|
3959 * f.chunk { |line| line =~ pattern ? :_alone : true }.each { |key, lines|
3960 * pp lines
3961 * }
3962 * }
3963 *
3964 * You can use the special symbol <tt>:_separator</tt> or +nil+
3965 * to force an element to be ignored (not included in any chunk):
3966 *
3967 * a = [0, 0, -1, 1, 1]
3968 * e = a.chunk{|i| i < 0 ? :_separator : true }
3969 * e.to_a # => [[true, [0, 0]], [true, [1, 1]]]
3970 *
3971 * Note that the separator does end the chunk:
3972 *
3973 * a = [0, 0, -1, 1, -1, 1]
3974 * e = a.chunk{|i| i < 0 ? :_separator : true }
3975 * e.to_a # => [[true, [0, 0]], [true, [1]], [true, [1]]]
3976 *
3977 * For example, the sequence of hyphens in svn log can be eliminated as follows:
3978 *
3979 * sep = "-"*72 + "\n"
3980 * IO.popen("svn log README") { |f|
3981 * f.chunk { |line|
3982 * line != sep || nil
3983 * }.each { |_, lines|
3984 * pp lines
3985 * }
3986 * }
3987 * #=> ["r20018 | knu | 2008-10-29 13:20:42 +0900 (Wed, 29 Oct 2008) | 2 lines\n",
3988 * # "\n",
3989 * # "* README, README.ja: Update the portability section.\n",
3990 * # "\n"]
3991 * # ["r16725 | knu | 2008-05-31 23:34:23 +0900 (Sat, 31 May 2008) | 2 lines\n",
3992 * # "\n",
3993 * # "* README, README.ja: Add a note about default C flags.\n",
3994 * # "\n"]
3995 * # ...
3996 *
3997 * Paragraphs separated by empty lines can be parsed as follows:
3998 *
3999 * File.foreach("README").chunk { |line|
4000 * /\A\s*\z/ !~ line || nil
4001 * }.each { |_, lines|
4002 * pp lines
4003 * }
4004 *
4005 */
4006static VALUE
4007enum_chunk(VALUE enumerable)
4008{
4010
4011 RETURN_SIZED_ENUMERATOR(enumerable, 0, 0, enum_size);
4012
4014 rb_ivar_set(enumerator, id_chunk_enumerable, enumerable);
4015 rb_ivar_set(enumerator, id_chunk_categorize, rb_block_proc());
4016 rb_block_call(enumerator, idInitialize, 0, 0, chunk_i, enumerator);
4017 return enumerator;
4018}
4019
4020
4022 VALUE sep_pred;
4023 VALUE sep_pat;
4024 VALUE prev_elts;
4025 VALUE yielder;
4026};
4027
4028static VALUE
4029slicebefore_ii(RB_BLOCK_CALL_FUNC_ARGLIST(i, _argp))
4030{
4031 struct slicebefore_arg *argp = MEMO_FOR(struct slicebefore_arg, _argp);
4032 VALUE header_p;
4033
4034 ENUM_WANT_SVALUE();
4035
4036 if (!NIL_P(argp->sep_pat))
4037 header_p = rb_funcallv(argp->sep_pat, id_eqq, 1, &i);
4038 else
4039 header_p = rb_funcallv(argp->sep_pred, id_call, 1, &i);
4040 if (RTEST(header_p)) {
4041 if (!NIL_P(argp->prev_elts))
4042 rb_funcallv(argp->yielder, id_lshift, 1, &argp->prev_elts);
4043 argp->prev_elts = rb_ary_new3(1, i);
4044 }
4045 else {
4046 if (NIL_P(argp->prev_elts))
4047 argp->prev_elts = rb_ary_new3(1, i);
4048 else
4049 rb_ary_push(argp->prev_elts, i);
4050 }
4051
4052 return Qnil;
4053}
4054
4055static VALUE
4057{
4058 VALUE enumerable;
4059 VALUE arg;
4060 struct slicebefore_arg *memo = NEW_MEMO_FOR(struct slicebefore_arg, arg);
4061
4062 enumerable = rb_ivar_get(enumerator, id_slicebefore_enumerable);
4063 memo->sep_pred = rb_attr_get(enumerator, id_slicebefore_sep_pred);
4064 memo->sep_pat = NIL_P(memo->sep_pred) ? rb_ivar_get(enumerator, id_slicebefore_sep_pat) : Qnil;
4065 memo->prev_elts = Qnil;
4066 memo->yielder = yielder;
4067
4068 rb_block_call(enumerable, id_each, 0, 0, slicebefore_ii, arg);
4069 memo = MEMO_FOR(struct slicebefore_arg, arg);
4070 if (!NIL_P(memo->prev_elts))
4071 rb_funcallv(memo->yielder, id_lshift, 1, &memo->prev_elts);
4072 return Qnil;
4073}
4074
4075/*
4076 * call-seq:
4077 * slice_before(pattern) -> enumerator
4078 * slice_before {|elt| ... } -> enumerator
4079 *
4080 * With argument +pattern+, returns an enumerator that uses the pattern
4081 * to partition elements into arrays ("slices").
4082 * An element begins a new slice if <tt>element === pattern</tt>
4083 * (or if it is the first element).
4084 *
4085 * a = %w[foo bar fop for baz fob fog bam foy]
4086 * e = a.slice_before(/ba/) # => #<Enumerator: ...>
4087 * e.each {|array| p array }
4088 *
4089 * Output:
4090 *
4091 * ["foo"]
4092 * ["bar", "fop", "for"]
4093 * ["baz", "fob", "fog"]
4094 * ["bam", "foy"]
4095 *
4096 * With a block, returns an enumerator that uses the block
4097 * to partition elements into arrays.
4098 * An element begins a new slice if its block return is a truthy value
4099 * (or if it is the first element):
4100 *
4101 * e = (1..20).slice_before {|i| i % 4 == 2 } # => #<Enumerator: ...>
4102 * e.each {|array| p array }
4103 *
4104 * Output:
4105 *
4106 * [1]
4107 * [2, 3, 4, 5]
4108 * [6, 7, 8, 9]
4109 * [10, 11, 12, 13]
4110 * [14, 15, 16, 17]
4111 * [18, 19, 20]
4112 *
4113 * Other methods of the Enumerator class and Enumerable module,
4114 * such as +to_a+, +map+, etc., are also usable.
4115 *
4116 * For example, iteration over ChangeLog entries can be implemented as
4117 * follows:
4118 *
4119 * # iterate over ChangeLog entries.
4120 * open("ChangeLog") { |f|
4121 * f.slice_before(/\A\S/).each { |e| pp e }
4122 * }
4123 *
4124 * # same as above. block is used instead of pattern argument.
4125 * open("ChangeLog") { |f|
4126 * f.slice_before { |line| /\A\S/ === line }.each { |e| pp e }
4127 * }
4128 *
4129 * "svn proplist -R" produces multiline output for each file.
4130 * They can be chunked as follows:
4131 *
4132 * IO.popen([{"LC_ALL"=>"C"}, "svn", "proplist", "-R"]) { |f|
4133 * f.lines.slice_before(/\AProp/).each { |lines| p lines }
4134 * }
4135 * #=> ["Properties on '.':\n", " svn:ignore\n", " svk:merge\n"]
4136 * # ["Properties on 'goruby.c':\n", " svn:eol-style\n"]
4137 * # ["Properties on 'complex.c':\n", " svn:mime-type\n", " svn:eol-style\n"]
4138 * # ["Properties on 'regparse.c':\n", " svn:eol-style\n"]
4139 * # ...
4140 *
4141 * If the block needs to maintain state over multiple elements,
4142 * local variables can be used.
4143 * For example, three or more consecutive increasing numbers can be squashed
4144 * as follows (see +chunk_while+ for a better way):
4145 *
4146 * a = [0, 2, 3, 4, 6, 7, 9]
4147 * prev = a[0]
4148 * p a.slice_before { |e|
4149 * prev, prev2 = e, prev
4150 * prev2 + 1 != e
4151 * }.map { |es|
4152 * es.length <= 2 ? es.join(",") : "#{es.first}-#{es.last}"
4153 * }.join(",")
4154 * #=> "0,2-4,6,7,9"
4155 *
4156 * However local variables should be used carefully
4157 * if the result enumerator is enumerated twice or more.
4158 * The local variables should be initialized for each enumeration.
4159 * Enumerator.new can be used to do it.
4160 *
4161 * # Word wrapping. This assumes all characters have same width.
4162 * def wordwrap(words, maxwidth)
4163 * Enumerator.new {|y|
4164 * # cols is initialized in Enumerator.new.
4165 * cols = 0
4166 * words.slice_before { |w|
4167 * cols += 1 if cols != 0
4168 * cols += w.length
4169 * if maxwidth < cols
4170 * cols = w.length
4171 * true
4172 * else
4173 * false
4174 * end
4175 * }.each {|ws| y.yield ws }
4176 * }
4177 * end
4178 * text = (1..20).to_a.join(" ")
4179 * enum = wordwrap(text.split(/\s+/), 10)
4180 * puts "-"*10
4181 * enum.each { |ws| puts ws.join(" ") } # first enumeration.
4182 * puts "-"*10
4183 * enum.each { |ws| puts ws.join(" ") } # second enumeration generates same result as the first.
4184 * puts "-"*10
4185 * #=> ----------
4186 * # 1 2 3 4 5
4187 * # 6 7 8 9 10
4188 * # 11 12 13
4189 * # 14 15 16
4190 * # 17 18 19
4191 * # 20
4192 * # ----------
4193 * # 1 2 3 4 5
4194 * # 6 7 8 9 10
4195 * # 11 12 13
4196 * # 14 15 16
4197 * # 17 18 19
4198 * # 20
4199 * # ----------
4200 *
4201 * mbox contains series of mails which start with Unix From line.
4202 * So each mail can be extracted by slice before Unix From line.
4203 *
4204 * # parse mbox
4205 * open("mbox") { |f|
4206 * f.slice_before { |line|
4207 * line.start_with? "From "
4208 * }.each { |mail|
4209 * unix_from = mail.shift
4210 * i = mail.index("\n")
4211 * header = mail[0...i]
4212 * body = mail[(i+1)..-1]
4213 * body.pop if body.last == "\n"
4214 * fields = header.slice_before { |line| !" \t".include?(line[0]) }.to_a
4215 * p unix_from
4216 * pp fields
4217 * pp body
4218 * }
4219 * }
4220 *
4221 * # split mails in mbox (slice before Unix From line after an empty line)
4222 * open("mbox") { |f|
4223 * emp = true
4224 * f.slice_before { |line|
4225 * prevemp = emp
4226 * emp = line == "\n"
4227 * prevemp && line.start_with?("From ")
4228 * }.each { |mail|
4229 * mail.pop if mail.last == "\n"
4230 * pp mail
4231 * }
4232 * }
4233 *
4234 */
4235static VALUE
4236enum_slice_before(int argc, VALUE *argv, VALUE enumerable)
4237{
4239
4240 if (rb_block_given_p()) {
4241 if (argc != 0)
4242 rb_error_arity(argc, 0, 0);
4244 rb_ivar_set(enumerator, id_slicebefore_sep_pred, rb_block_proc());
4245 }
4246 else {
4247 VALUE sep_pat;
4248 rb_scan_args(argc, argv, "1", &sep_pat);
4250 rb_ivar_set(enumerator, id_slicebefore_sep_pat, sep_pat);
4251 }
4252 rb_ivar_set(enumerator, id_slicebefore_enumerable, enumerable);
4253 rb_block_call(enumerator, idInitialize, 0, 0, slicebefore_i, enumerator);
4254 return enumerator;
4255}
4256
4257
4259 VALUE pat;
4260 VALUE pred;
4261 VALUE prev_elts;
4262 VALUE yielder;
4263};
4264
4265static VALUE
4266sliceafter_ii(RB_BLOCK_CALL_FUNC_ARGLIST(i, _memo))
4267{
4268#define UPDATE_MEMO ((void)(memo = MEMO_FOR(struct sliceafter_arg, _memo)))
4269 struct sliceafter_arg *memo;
4270 int split_p;
4271 UPDATE_MEMO;
4272
4273 ENUM_WANT_SVALUE();
4274
4275 if (NIL_P(memo->prev_elts)) {
4276 memo->prev_elts = rb_ary_new3(1, i);
4277 }
4278 else {
4279 rb_ary_push(memo->prev_elts, i);
4280 }
4281
4282 if (NIL_P(memo->pred)) {
4283 split_p = RTEST(rb_funcallv(memo->pat, id_eqq, 1, &i));
4284 UPDATE_MEMO;
4285 }
4286 else {
4287 split_p = RTEST(rb_funcallv(memo->pred, id_call, 1, &i));
4288 UPDATE_MEMO;
4289 }
4290
4291 if (split_p) {
4292 rb_funcallv(memo->yielder, id_lshift, 1, &memo->prev_elts);
4293 UPDATE_MEMO;
4294 memo->prev_elts = Qnil;
4295 }
4296
4297 return Qnil;
4298#undef UPDATE_MEMO
4299}
4300
4301static VALUE
4303{
4304 VALUE enumerable;
4305 VALUE arg;
4306 struct sliceafter_arg *memo = NEW_MEMO_FOR(struct sliceafter_arg, arg);
4307
4308 enumerable = rb_ivar_get(enumerator, id_sliceafter_enum);
4309 memo->pat = rb_ivar_get(enumerator, id_sliceafter_pat);
4310 memo->pred = rb_attr_get(enumerator, id_sliceafter_pred);
4311 memo->prev_elts = Qnil;
4312 memo->yielder = yielder;
4313
4314 rb_block_call(enumerable, id_each, 0, 0, sliceafter_ii, arg);
4315 memo = MEMO_FOR(struct sliceafter_arg, arg);
4316 if (!NIL_P(memo->prev_elts))
4317 rb_funcallv(memo->yielder, id_lshift, 1, &memo->prev_elts);
4318 return Qnil;
4319}
4320
4321/*
4322 * call-seq:
4323 * enum.slice_after(pattern) -> an_enumerator
4324 * enum.slice_after { |elt| bool } -> an_enumerator
4325 *
4326 * Creates an enumerator for each chunked elements.
4327 * The ends of chunks are defined by _pattern_ and the block.
4328 *
4329 * If <code>_pattern_ === _elt_</code> returns <code>true</code> or the block
4330 * returns <code>true</code> for the element, the element is end of a
4331 * chunk.
4332 *
4333 * The <code>===</code> and _block_ is called from the first element to the last
4334 * element of _enum_.
4335 *
4336 * The result enumerator yields the chunked elements as an array.
4337 * So +each+ method can be called as follows:
4338 *
4339 * enum.slice_after(pattern).each { |ary| ... }
4340 * enum.slice_after { |elt| bool }.each { |ary| ... }
4341 *
4342 * Other methods of the Enumerator class and Enumerable module,
4343 * such as +map+, etc., are also usable.
4344 *
4345 * For example, continuation lines (lines end with backslash) can be
4346 * concatenated as follows:
4347 *
4348 * lines = ["foo\n", "bar\\\n", "baz\n", "\n", "qux\n"]
4349 * e = lines.slice_after(/(?<!\\‍)\n\z/)
4350 * p e.to_a
4351 * #=> [["foo\n"], ["bar\\\n", "baz\n"], ["\n"], ["qux\n"]]
4352 * p e.map {|ll| ll[0...-1].map {|l| l.sub(/\\\n\z/, "") }.join + ll.last }
4353 * #=>["foo\n", "barbaz\n", "\n", "qux\n"]
4354 *
4355 */
4356
4357static VALUE
4358enum_slice_after(int argc, VALUE *argv, VALUE enumerable)
4359{
4361 VALUE pat = Qnil, pred = Qnil;
4362
4363 if (rb_block_given_p()) {
4364 if (0 < argc)
4365 rb_raise(rb_eArgError, "both pattern and block are given");
4366 pred = rb_block_proc();
4367 }
4368 else {
4369 rb_scan_args(argc, argv, "1", &pat);
4370 }
4371
4373 rb_ivar_set(enumerator, id_sliceafter_enum, enumerable);
4374 rb_ivar_set(enumerator, id_sliceafter_pat, pat);
4375 rb_ivar_set(enumerator, id_sliceafter_pred, pred);
4376
4377 rb_block_call(enumerator, idInitialize, 0, 0, sliceafter_i, enumerator);
4378 return enumerator;
4379}
4380
4382 VALUE pred;
4383 VALUE prev_elt;
4384 VALUE prev_elts;
4385 VALUE yielder;
4386 int inverted; /* 0 for slice_when and 1 for chunk_while. */
4387};
4388
4389static VALUE
4390slicewhen_ii(RB_BLOCK_CALL_FUNC_ARGLIST(i, _memo))
4391{
4392#define UPDATE_MEMO ((void)(memo = MEMO_FOR(struct slicewhen_arg, _memo)))
4393 struct slicewhen_arg *memo;
4394 int split_p;
4395 UPDATE_MEMO;
4396
4397 ENUM_WANT_SVALUE();
4398
4399 if (UNDEF_P(memo->prev_elt)) {
4400 /* The first element */
4401 memo->prev_elt = i;
4402 memo->prev_elts = rb_ary_new3(1, i);
4403 }
4404 else {
4405 VALUE args[2];
4406 args[0] = memo->prev_elt;
4407 args[1] = i;
4408 split_p = RTEST(rb_funcallv(memo->pred, id_call, 2, args));
4409 UPDATE_MEMO;
4410
4411 if (memo->inverted)
4412 split_p = !split_p;
4413
4414 if (split_p) {
4415 rb_funcallv(memo->yielder, id_lshift, 1, &memo->prev_elts);
4416 UPDATE_MEMO;
4417 memo->prev_elts = rb_ary_new3(1, i);
4418 }
4419 else {
4420 rb_ary_push(memo->prev_elts, i);
4421 }
4422
4423 memo->prev_elt = i;
4424 }
4425
4426 return Qnil;
4427#undef UPDATE_MEMO
4428}
4429
4430static VALUE
4432{
4433 VALUE enumerable;
4434 VALUE arg;
4435 struct slicewhen_arg *memo =
4436 NEW_PARTIAL_MEMO_FOR(struct slicewhen_arg, arg, inverted);
4437
4438 enumerable = rb_ivar_get(enumerator, id_slicewhen_enum);
4439 memo->pred = rb_attr_get(enumerator, id_slicewhen_pred);
4440 memo->prev_elt = Qundef;
4441 memo->prev_elts = Qnil;
4442 memo->yielder = yielder;
4443 memo->inverted = RTEST(rb_attr_get(enumerator, id_slicewhen_inverted));
4444
4445 rb_block_call(enumerable, id_each, 0, 0, slicewhen_ii, arg);
4446 memo = MEMO_FOR(struct slicewhen_arg, arg);
4447 if (!NIL_P(memo->prev_elts))
4448 rb_funcallv(memo->yielder, id_lshift, 1, &memo->prev_elts);
4449 return Qnil;
4450}
4451
4452/*
4453 * call-seq:
4454 * enum.slice_when {|elt_before, elt_after| bool } -> an_enumerator
4455 *
4456 * Creates an enumerator for each chunked elements.
4457 * The beginnings of chunks are defined by the block.
4458 *
4459 * This method splits each chunk using adjacent elements,
4460 * _elt_before_ and _elt_after_,
4461 * in the receiver enumerator.
4462 * This method split chunks between _elt_before_ and _elt_after_ where
4463 * the block returns <code>true</code>.
4464 *
4465 * The block is called the length of the receiver enumerator minus one.
4466 *
4467 * The result enumerator yields the chunked elements as an array.
4468 * So +each+ method can be called as follows:
4469 *
4470 * enum.slice_when { |elt_before, elt_after| bool }.each { |ary| ... }
4471 *
4472 * Other methods of the Enumerator class and Enumerable module,
4473 * such as +to_a+, +map+, etc., are also usable.
4474 *
4475 * For example, one-by-one increasing subsequence can be chunked as follows:
4476 *
4477 * a = [1,2,4,9,10,11,12,15,16,19,20,21]
4478 * b = a.slice_when {|i, j| i+1 != j }
4479 * p b.to_a #=> [[1, 2], [4], [9, 10, 11, 12], [15, 16], [19, 20, 21]]
4480 * c = b.map {|a| a.length < 3 ? a : "#{a.first}-#{a.last}" }
4481 * p c #=> [[1, 2], [4], "9-12", [15, 16], "19-21"]
4482 * d = c.join(",")
4483 * p d #=> "1,2,4,9-12,15,16,19-21"
4484 *
4485 * Near elements (threshold: 6) in sorted array can be chunked as follows:
4486 *
4487 * a = [3, 11, 14, 25, 28, 29, 29, 41, 55, 57]
4488 * p a.slice_when {|i, j| 6 < j - i }.to_a
4489 * #=> [[3], [11, 14], [25, 28, 29, 29], [41], [55, 57]]
4490 *
4491 * Increasing (non-decreasing) subsequence can be chunked as follows:
4492 *
4493 * a = [0, 9, 2, 2, 3, 2, 7, 5, 9, 5]
4494 * p a.slice_when {|i, j| i > j }.to_a
4495 * #=> [[0, 9], [2, 2, 3], [2, 7], [5, 9], [5]]
4496 *
4497 * Adjacent evens and odds can be chunked as follows:
4498 * (Enumerable#chunk is another way to do it.)
4499 *
4500 * a = [7, 5, 9, 2, 0, 7, 9, 4, 2, 0]
4501 * p a.slice_when {|i, j| i.even? != j.even? }.to_a
4502 * #=> [[7, 5, 9], [2, 0], [7, 9], [4, 2, 0]]
4503 *
4504 * Paragraphs (non-empty lines with trailing empty lines) can be chunked as follows:
4505 * (See Enumerable#chunk to ignore empty lines.)
4506 *
4507 * lines = ["foo\n", "bar\n", "\n", "baz\n", "qux\n"]
4508 * p lines.slice_when {|l1, l2| /\A\s*\z/ =~ l1 && /\S/ =~ l2 }.to_a
4509 * #=> [["foo\n", "bar\n", "\n"], ["baz\n", "qux\n"]]
4510 *
4511 * Enumerable#chunk_while does the same, except splitting when the block
4512 * returns <code>false</code> instead of <code>true</code>.
4513 */
4514static VALUE
4515enum_slice_when(VALUE enumerable)
4516{
4518 VALUE pred;
4519
4520 pred = rb_block_proc();
4521
4523 rb_ivar_set(enumerator, id_slicewhen_enum, enumerable);
4524 rb_ivar_set(enumerator, id_slicewhen_pred, pred);
4525 rb_ivar_set(enumerator, id_slicewhen_inverted, Qfalse);
4526
4527 rb_block_call(enumerator, idInitialize, 0, 0, slicewhen_i, enumerator);
4528 return enumerator;
4529}
4530
4531/*
4532 * call-seq:
4533 * enum.chunk_while {|elt_before, elt_after| bool } -> an_enumerator
4534 *
4535 * Creates an enumerator for each chunked elements.
4536 * The beginnings of chunks are defined by the block.
4537 *
4538 * This method splits each chunk using adjacent elements,
4539 * _elt_before_ and _elt_after_,
4540 * in the receiver enumerator.
4541 * This method split chunks between _elt_before_ and _elt_after_ where
4542 * the block returns <code>false</code>.
4543 *
4544 * The block is called the length of the receiver enumerator minus one.
4545 *
4546 * The result enumerator yields the chunked elements as an array.
4547 * So +each+ method can be called as follows:
4548 *
4549 * enum.chunk_while { |elt_before, elt_after| bool }.each { |ary| ... }
4550 *
4551 * Other methods of the Enumerator class and Enumerable module,
4552 * such as +to_a+, +map+, etc., are also usable.
4553 *
4554 * For example, one-by-one increasing subsequence can be chunked as follows:
4555 *
4556 * a = [1,2,4,9,10,11,12,15,16,19,20,21]
4557 * b = a.chunk_while {|i, j| i+1 == j }
4558 * p b.to_a #=> [[1, 2], [4], [9, 10, 11, 12], [15, 16], [19, 20, 21]]
4559 * c = b.map {|a| a.length < 3 ? a : "#{a.first}-#{a.last}" }
4560 * p c #=> [[1, 2], [4], "9-12", [15, 16], "19-21"]
4561 * d = c.join(",")
4562 * p d #=> "1,2,4,9-12,15,16,19-21"
4563 *
4564 * Increasing (non-decreasing) subsequence can be chunked as follows:
4565 *
4566 * a = [0, 9, 2, 2, 3, 2, 7, 5, 9, 5]
4567 * p a.chunk_while {|i, j| i <= j }.to_a
4568 * #=> [[0, 9], [2, 2, 3], [2, 7], [5, 9], [5]]
4569 *
4570 * Adjacent evens and odds can be chunked as follows:
4571 * (Enumerable#chunk is another way to do it.)
4572 *
4573 * a = [7, 5, 9, 2, 0, 7, 9, 4, 2, 0]
4574 * p a.chunk_while {|i, j| i.even? == j.even? }.to_a
4575 * #=> [[7, 5, 9], [2, 0], [7, 9], [4, 2, 0]]
4576 *
4577 * Enumerable#slice_when does the same, except splitting when the block
4578 * returns <code>true</code> instead of <code>false</code>.
4579 */
4580static VALUE
4581enum_chunk_while(VALUE enumerable)
4582{
4584 VALUE pred;
4585
4586 pred = rb_block_proc();
4587
4589 rb_ivar_set(enumerator, id_slicewhen_enum, enumerable);
4590 rb_ivar_set(enumerator, id_slicewhen_pred, pred);
4591 rb_ivar_set(enumerator, id_slicewhen_inverted, Qtrue);
4592
4593 rb_block_call(enumerator, idInitialize, 0, 0, slicewhen_i, enumerator);
4594 return enumerator;
4595}
4596
4598 VALUE v, r;
4599 long n;
4600 double f, c;
4601 int block_given;
4602 int float_value;
4603};
4604
4605static void
4606sum_iter_normalize_memo(struct enum_sum_memo *memo)
4607{
4608 RUBY_ASSERT(FIXABLE(memo->n));
4609 memo->v = rb_fix_plus(LONG2FIX(memo->n), memo->v);
4610 memo->n = 0;
4611
4612 switch (TYPE(memo->r)) {
4613 case T_RATIONAL: memo->v = rb_rational_plus(memo->r, memo->v); break;
4614 case T_UNDEF: break;
4615 default: UNREACHABLE; /* or ...? */
4616 }
4617 memo->r = Qundef;
4618}
4619
4620static void
4621sum_iter_fixnum(VALUE i, struct enum_sum_memo *memo)
4622{
4623 memo->n += FIX2LONG(i); /* should not overflow long type */
4624 if (! FIXABLE(memo->n)) {
4625 memo->v = rb_big_plus(LONG2NUM(memo->n), memo->v);
4626 memo->n = 0;
4627 }
4628}
4629
4630static void
4631sum_iter_bignum(VALUE i, struct enum_sum_memo *memo)
4632{
4633 memo->v = rb_big_plus(i, memo->v);
4634}
4635
4636static void
4637sum_iter_rational(VALUE i, struct enum_sum_memo *memo)
4638{
4639 if (UNDEF_P(memo->r)) {
4640 memo->r = i;
4641 }
4642 else {
4643 memo->r = rb_rational_plus(memo->r, i);
4644 }
4645}
4646
4647static void
4648sum_iter_some_value(VALUE i, struct enum_sum_memo *memo)
4649{
4650 memo->v = rb_funcallv(memo->v, idPLUS, 1, &i);
4651}
4652
4653static void
4654sum_iter_Kahan_Babuska(VALUE i, struct enum_sum_memo *memo)
4655{
4656 /*
4657 * Kahan-Babuska balancing compensated summation algorithm
4658 * See https://link.springer.com/article/10.1007/s00607-005-0139-x
4659 */
4660 double x;
4661
4662 switch (TYPE(i)) {
4663 case T_FLOAT: x = RFLOAT_VALUE(i); break;
4664 case T_FIXNUM: x = FIX2LONG(i); break;
4665 case T_BIGNUM: x = rb_big2dbl(i); break;
4666 case T_RATIONAL: x = rb_num2dbl(i); break;
4667 default:
4668 memo->v = DBL2NUM(memo->f);
4669 memo->float_value = 0;
4670 sum_iter_some_value(i, memo);
4671 return;
4672 }
4673
4674 double f = memo->f;
4675
4676 if (isnan(f)) {
4677 return;
4678 }
4679 else if (! isfinite(x)) {
4680 if (isinf(x) && isinf(f) && signbit(x) != signbit(f)) {
4681 i = DBL2NUM(f);
4682 x = nan("");
4683 }
4684 memo->v = i;
4685 memo->f = x;
4686 return;
4687 }
4688 else if (isinf(f)) {
4689 return;
4690 }
4691
4692 double c = memo->c;
4693 double t = f + x;
4694
4695 if (fabs(f) >= fabs(x)) {
4696 c += ((f - t) + x);
4697 }
4698 else {
4699 c += ((x - t) + f);
4700 }
4701 f = t;
4702
4703 memo->f = f;
4704 memo->c = c;
4705}
4706
4707static void
4708sum_iter(VALUE i, struct enum_sum_memo *memo)
4709{
4710 RUBY_ASSERT(memo != NULL);
4711 if (memo->block_given) {
4712 i = rb_yield(i);
4713 }
4714
4715 if (memo->float_value) {
4716 sum_iter_Kahan_Babuska(i, memo);
4717 }
4718 else switch (TYPE(memo->v)) {
4719 default: sum_iter_some_value(i, memo); return;
4720 case T_FLOAT:
4721 case T_FIXNUM:
4722 case T_BIGNUM:
4723 case T_RATIONAL:
4724 switch (TYPE(i)) {
4725 case T_FIXNUM: sum_iter_fixnum(i, memo); return;
4726 case T_BIGNUM: sum_iter_bignum(i, memo); return;
4727 case T_RATIONAL: sum_iter_rational(i, memo); return;
4728 case T_FLOAT:
4729 sum_iter_normalize_memo(memo);
4730 memo->f = NUM2DBL(memo->v);
4731 memo->c = 0.0;
4732 memo->float_value = 1;
4733 sum_iter_Kahan_Babuska(i, memo);
4734 return;
4735 default:
4736 sum_iter_normalize_memo(memo);
4737 sum_iter_some_value(i, memo);
4738 return;
4739 }
4740 }
4741}
4742
4743static VALUE
4744enum_sum_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
4745{
4746 ENUM_WANT_SVALUE();
4747 sum_iter(i, (struct enum_sum_memo *) args);
4748 return Qnil;
4749}
4750
4751static int
4752hash_sum_i(VALUE key, VALUE value, VALUE arg)
4753{
4754 sum_iter(rb_assoc_new(key, value), (struct enum_sum_memo *) arg);
4755 return ST_CONTINUE;
4756}
4757
4758static void
4759hash_sum(VALUE hash, struct enum_sum_memo *memo)
4760{
4762 RUBY_ASSERT(memo != NULL);
4763
4764 rb_hash_foreach(hash, hash_sum_i, (VALUE)memo);
4765}
4766
4767static VALUE
4768int_range_sum(VALUE beg, VALUE end, int excl, VALUE init)
4769{
4770 if (excl) {
4771 if (FIXNUM_P(end))
4772 end = LONG2FIX(FIX2LONG(end) - 1);
4773 else
4774 end = rb_big_minus(end, LONG2FIX(1));
4775 }
4776
4777 if (rb_int_ge(end, beg)) {
4778 VALUE a;
4779 a = rb_int_plus(rb_int_minus(end, beg), LONG2FIX(1));
4780 a = rb_int_mul(a, rb_int_plus(end, beg));
4781 a = rb_int_idiv(a, LONG2FIX(2));
4782 return rb_int_plus(init, a);
4783 }
4784
4785 return init;
4786}
4787
4788/*
4789 * call-seq:
4790 * sum(initial_value = 0) -> number
4791 * sum(initial_value = 0) {|element| ... } -> object
4792 *
4793 * With no block given,
4794 * returns the sum of +initial_value+ and the elements:
4795 *
4796 * (1..100).sum # => 5050
4797 * (1..100).sum(1) # => 5051
4798 * ('a'..'d').sum('foo') # => "fooabcd"
4799 *
4800 * Generally, the sum is computed using methods <tt>+</tt> and +each+;
4801 * for performance optimizations, those methods may not be used,
4802 * and so any redefinition of those methods may not have effect here.
4803 *
4804 * One such optimization: When possible, computes using Gauss's summation
4805 * formula <em>n(n+1)/2</em>:
4806 *
4807 * 100 * (100 + 1) / 2 # => 5050
4808 *
4809 * With a block given, calls the block with each element;
4810 * returns the sum of +initial_value+ and the block return values:
4811 *
4812 * (1..4).sum {|i| i*i } # => 30
4813 * (1..4).sum(100) {|i| i*i } # => 130
4814 * h = {a: 0, b: 1, c: 2, d: 3, e: 4, f: 5}
4815 * h.sum {|key, value| value.odd? ? value : 0 } # => 9
4816 * ('a'..'f').sum('x') {|c| c < 'd' ? c : '' } # => "xabc"
4817 *
4818 */
4819static VALUE
4820enum_sum(int argc, VALUE* argv, VALUE obj)
4821{
4822 struct enum_sum_memo memo;
4823 VALUE beg, end;
4824 int excl;
4825
4826 memo.v = (rb_check_arity(argc, 0, 1) == 0) ? LONG2FIX(0) : argv[0];
4827 memo.block_given = rb_block_given_p();
4828 memo.n = 0;
4829 memo.r = Qundef;
4830
4831 if ((memo.float_value = RB_FLOAT_TYPE_P(memo.v))) {
4832 memo.f = RFLOAT_VALUE(memo.v);
4833 memo.c = 0.0;
4834 }
4835 else {
4836 memo.f = 0.0;
4837 memo.c = 0.0;
4838 }
4839
4840 if (RTEST(rb_range_values(obj, &beg, &end, &excl))) {
4841 if (!memo.block_given && !memo.float_value &&
4842 (FIXNUM_P(beg) || RB_BIGNUM_TYPE_P(beg)) &&
4843 (FIXNUM_P(end) || RB_BIGNUM_TYPE_P(end))) {
4844 return int_range_sum(beg, end, excl, memo.v);
4845 }
4846 }
4847
4848 if (RB_TYPE_P(obj, T_HASH) &&
4849 rb_method_basic_definition_p(CLASS_OF(obj), id_each))
4850 hash_sum(obj, &memo);
4851 else
4852 rb_block_call(obj, id_each, 0, 0, enum_sum_i, (VALUE)&memo);
4853
4854 if (memo.float_value) {
4855 return DBL2NUM(memo.f + memo.c);
4856 }
4857 else {
4858 if (memo.n != 0)
4859 memo.v = rb_fix_plus(LONG2FIX(memo.n), memo.v);
4860 if (!UNDEF_P(memo.r)) {
4861 memo.v = rb_rational_plus(memo.r, memo.v);
4862 }
4863 return memo.v;
4864 }
4865}
4866
4867static VALUE
4868uniq_func(RB_BLOCK_CALL_FUNC_ARGLIST(i, hash))
4869{
4870 ENUM_WANT_SVALUE();
4871 rb_hash_add_new_element(hash, i, i);
4872 return Qnil;
4873}
4874
4875static VALUE
4876uniq_iter(RB_BLOCK_CALL_FUNC_ARGLIST(i, hash))
4877{
4878 ENUM_WANT_SVALUE();
4879 rb_hash_add_new_element(hash, rb_yield_values2(argc, argv), i);
4880 return Qnil;
4881}
4882
4883/*
4884 * call-seq:
4885 * uniq -> array
4886 * uniq {|element| ... } -> array
4887 *
4888 * With no block, returns a new array containing only unique elements;
4889 * the array has no two elements +e0+ and +e1+ such that <tt>e0.eql?(e1)</tt>:
4890 *
4891 * %w[a b c c b a a b c].uniq # => ["a", "b", "c"]
4892 * [0, 1, 2, 2, 1, 0, 0, 1, 2].uniq # => [0, 1, 2]
4893 *
4894 * With a block, returns a new array containing elements only for which the block
4895 * returns a unique value:
4896 *
4897 * a = [0, 1, 2, 3, 4, 5, 5, 4, 3, 2, 1]
4898 * a.uniq {|i| i.even? ? i : 0 } # => [0, 2, 4]
4899 * a = %w[a b c d e e d c b a a b c d e]
4900 * a.uniq {|c| c < 'c' } # => ["a", "c"]
4901 *
4902 */
4903
4904static VALUE
4905enum_uniq(VALUE obj)
4906{
4907 VALUE hash, ret;
4908 rb_block_call_func *const func =
4909 rb_block_given_p() ? uniq_iter : uniq_func;
4910
4911 hash = rb_obj_hide(rb_hash_new());
4912 rb_block_call(obj, id_each, 0, 0, func, hash);
4913 ret = rb_hash_values(hash);
4914 rb_hash_clear(hash);
4915 return ret;
4916}
4917
4918static VALUE
4919compact_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, ary))
4920{
4921 ENUM_WANT_SVALUE();
4922
4923 if (!NIL_P(i)) {
4924 rb_ary_push(ary, i);
4925 }
4926 return Qnil;
4927}
4928
4929/*
4930 * call-seq:
4931 * compact -> array
4932 *
4933 * Returns an array of all non-+nil+ elements:
4934 *
4935 * a = [nil, 0, nil, 'a', false, nil, false, nil, 'a', nil, 0, nil]
4936 * a.compact # => [0, "a", false, false, "a", 0]
4937 *
4938 */
4939
4940static VALUE
4941enum_compact(VALUE obj)
4942{
4943 VALUE ary;
4944
4945 ary = rb_ary_new();
4946 rb_block_call(obj, id_each, 0, 0, compact_i, ary);
4947
4948 return ary;
4949}
4950
4951
4952/*
4953 * == What's Here
4954 *
4955 * Module \Enumerable provides methods that are useful to a collection class for:
4956 *
4957 * - {Querying}[rdoc-ref:Enumerable@Methods+for+Querying]
4958 * - {Fetching}[rdoc-ref:Enumerable@Methods+for+Fetching]
4959 * - {Searching and Filtering}[rdoc-ref:Enumerable@Methods+for+Searching+and+Filtering]
4960 * - {Sorting}[rdoc-ref:Enumerable@Methods+for+Sorting]
4961 * - {Iterating}[rdoc-ref:Enumerable@Methods+for+Iterating]
4962 * - {And more....}[rdoc-ref:Enumerable@Other+Methods]
4963 *
4964 * === Methods for Querying
4965 *
4966 * These methods return information about the \Enumerable other than the elements themselves:
4967 *
4968 * - #member? (aliased as #include?): Returns +true+ if <tt>self == object</tt>, +false+ otherwise.
4969 * - #all?: Returns +true+ if all elements meet a specified criterion; +false+ otherwise.
4970 * - #any?: Returns +true+ if any element meets a specified criterion; +false+ otherwise.
4971 * - #none?: Returns +true+ if no element meets a specified criterion; +false+ otherwise.
4972 * - #one?: Returns +true+ if exactly one element meets a specified criterion; +false+ otherwise.
4973 * - #count: Returns the count of elements,
4974 * based on an argument or block criterion, if given.
4975 * - #tally: Returns a new Hash containing the counts of occurrences of each element.
4976 *
4977 * === Methods for Fetching
4978 *
4979 * These methods return entries from the \Enumerable, without modifying it:
4980 *
4981 * <i>Leading, trailing, or all elements</i>:
4982 *
4983 * - #to_a (aliased as #entries): Returns all elements.
4984 * - #first: Returns the first element or leading elements.
4985 * - #take: Returns a specified number of leading elements.
4986 * - #drop: Returns a specified number of trailing elements.
4987 * - #take_while: Returns leading elements as specified by the given block.
4988 * - #drop_while: Returns trailing elements as specified by the given block.
4989 *
4990 * <i>Minimum and maximum value elements</i>:
4991 *
4992 * - #min: Returns the elements whose values are smallest among the elements,
4993 * as determined by <tt>#<=></tt> or a given block.
4994 * - #max: Returns the elements whose values are largest among the elements,
4995 * as determined by <tt>#<=></tt> or a given block.
4996 * - #minmax: Returns a 2-element Array containing the smallest and largest elements.
4997 * - #min_by: Returns the smallest element, as determined by the given block.
4998 * - #max_by: Returns the largest element, as determined by the given block.
4999 * - #minmax_by: Returns the smallest and largest elements, as determined by the given block.
5000 *
5001 * <i>Groups, slices, and partitions</i>:
5002 *
5003 * - #group_by: Returns a Hash that partitions the elements into groups.
5004 * - #partition: Returns elements partitioned into two new Arrays, as determined by the given block.
5005 * - #slice_after: Returns a new Enumerator whose entries are a partition of +self+,
5006 * based either on a given +object+ or a given block.
5007 * - #slice_before: Returns a new Enumerator whose entries are a partition of +self+,
5008 * based either on a given +object+ or a given block.
5009 * - #slice_when: Returns a new Enumerator whose entries are a partition of +self+
5010 * based on the given block.
5011 * - #chunk: Returns elements organized into chunks as specified by the given block.
5012 * - #chunk_while: Returns elements organized into chunks as specified by the given block.
5013 *
5014 * === Methods for Searching and Filtering
5015 *
5016 * These methods return elements that meet a specified criterion:
5017 *
5018 * - #find (aliased as #detect): Returns an element selected by the block.
5019 * - #find_all (aliased as #filter, #select): Returns elements selected by the block.
5020 * - #find_index: Returns the index of an element selected by a given object or block.
5021 * - #reject: Returns elements not rejected by the block.
5022 * - #uniq: Returns elements that are not duplicates.
5023 *
5024 * === Methods for Sorting
5025 *
5026 * These methods return elements in sorted order:
5027 *
5028 * - #sort: Returns the elements, sorted by <tt>#<=></tt> or the given block.
5029 * - #sort_by: Returns the elements, sorted by the given block.
5030 *
5031 * === Methods for Iterating
5032 *
5033 * - #each_entry: Calls the block with each successive element
5034 * (slightly different from #each).
5035 * - #each_with_index: Calls the block with each successive element and its index.
5036 * - #each_with_object: Calls the block with each successive element and a given object.
5037 * - #each_slice: Calls the block with successive non-overlapping slices.
5038 * - #each_cons: Calls the block with successive overlapping slices.
5039 * (different from #each_slice).
5040 * - #reverse_each: Calls the block with each successive element, in reverse order.
5041 *
5042 * === Other Methods
5043 *
5044 * - #collect (aliased as #map): Returns objects returned by the block.
5045 * - #filter_map: Returns truthy objects returned by the block.
5046 * - #flat_map (aliased as #collect_concat): Returns flattened objects returned by the block.
5047 * - #grep: Returns elements selected by a given object
5048 * or objects returned by a given block.
5049 * - #grep_v: Returns elements not selected by a given object
5050 * or objects returned by a given block.
5051 * - #inject (aliased as #reduce): Returns the object formed by combining all elements.
5052 * - #sum: Returns the sum of the elements, using method <tt>+</tt>.
5053 * - #zip: Combines each element with elements from other enumerables;
5054 * returns the n-tuples or calls the block with each.
5055 * - #cycle: Calls the block with each element, cycling repeatedly.
5056 *
5057 * == Usage
5058 *
5059 * To use module \Enumerable in a collection class:
5060 *
5061 * - Include it:
5062 *
5063 * include Enumerable
5064 *
5065 * - Implement method <tt>#each</tt>
5066 * which must yield successive elements of the collection.
5067 * The method will be called by almost any \Enumerable method.
5068 *
5069 * Example:
5070 *
5071 * class Foo
5072 * include Enumerable
5073 * def each
5074 * yield 1
5075 * yield 1, 2
5076 * yield
5077 * end
5078 * end
5079 * Foo.new.each_entry{ |element| p element }
5080 *
5081 * Output:
5082 *
5083 * 1
5084 * [1, 2]
5085 * nil
5086 *
5087 * == \Enumerable in Ruby Classes
5088 *
5089 * These Ruby core classes include (or extend) \Enumerable:
5090 *
5091 * - ARGF
5092 * - Array
5093 * - Dir
5094 * - Enumerator
5095 * - ENV (extends)
5096 * - Hash
5097 * - IO
5098 * - Range
5099 * - Struct
5100 *
5101 * These Ruby standard library classes include \Enumerable:
5102 *
5103 * - CSV
5104 * - CSV::Table
5105 * - CSV::Row
5106 * - Set
5107 *
5108 * Virtually all methods in \Enumerable call method +#each+ in the including class:
5109 *
5110 * - <tt>Hash#each</tt> yields the next key-value pair as a 2-element Array.
5111 * - <tt>Struct#each</tt> yields the next name-value pair as a 2-element Array.
5112 * - For the other classes above, +#each+ yields the next object from the collection.
5113 *
5114 * == About the Examples
5115 *
5116 * The example code snippets for the \Enumerable methods:
5117 *
5118 * - Always show the use of one or more Array-like classes (often Array itself).
5119 * - Sometimes show the use of a Hash-like class.
5120 * For some methods, though, the usage would not make sense,
5121 * and so it is not shown. Example: #tally would find exactly one of each Hash entry.
5122 *
5123 * == Extended Methods
5124 *
5125 * A Enumerable class may define extended methods. This section describes the standard
5126 * behavior of extension methods for reference purposes.
5127 *
5128 * === #size
5129 *
5130 * \Enumerator has a #size method.
5131 * It uses the size function argument passed to +Enumerator.new+.
5132 *
5133 * e = Enumerator.new(-> { 3 }) {|y| p y; y.yield :a; y.yield :b; y.yield :c; :z }
5134 * p e.size #=> 3
5135 * p e.next #=> :a
5136 * p e.next #=> :b
5137 * p e.next #=> :c
5138 * begin
5139 * e.next
5140 * rescue StopIteration
5141 * p $!.result #=> :z
5142 * end
5143 *
5144 * The result of the size function should represent the number of iterations
5145 * (i.e., the number of times Enumerator::Yielder#yield is called).
5146 * In the above example, the block calls #yield three times, and
5147 * the size function, +-> { 3 }+, returns 3 accordingly.
5148 * The result of the size function can be an integer, +Float::INFINITY+,
5149 * or +nil+.
5150 * An integer means the exact number of times #yield will be called,
5151 * as shown above.
5152 * +Float::INFINITY+ indicates an infinite number of #yield calls.
5153 * +nil+ means the number of #yield calls is difficult or impossible to
5154 * determine.
5155 *
5156 * Many iteration methods return an \Enumerator object with an
5157 * appropriate size function if no block is given.
5158 *
5159 * Examples:
5160 *
5161 * ["a", "b", "c"].each.size #=> 3
5162 * {a: "x", b: "y", c: "z"}.each.size #=> 3
5163 * (0..20).to_a.permutation.size #=> 51090942171709440000
5164 * loop.size #=> Float::INFINITY
5165 * (1..100).drop_while.size #=> nil # size depends on the block's behavior
5166 * STDIN.each.size #=> nil # cannot be computed without consuming input
5167 * File.open("/etc/resolv.conf").each.size #=> nil # cannot be computed without reading the file
5168 *
5169 * The behavior of #size for Range-based enumerators depends on the #begin element:
5170 *
5171 * - If the #begin element is an Integer, the #size method returns an Integer or +Float::INFINITY+.
5172 * - If the #begin element is an object with a #succ method (other than Integer), #size returns +nil+.
5173 * (Computing the size would require repeatedly calling #succ, which may be too slow.)
5174 * - If the #begin element does not have a #succ method, #size raises a TypeError.
5175 *
5176 * Examples:
5177 *
5178 * (10..42).each.size #=> 33
5179 * (10..42.9).each.size #=> 33 (the #end element may be a non-integer numeric)
5180 * (10..).each.size #=> Float::INFINITY
5181 * ("a".."z").each.size #=> nil
5182 * ("a"..).each.size #=> nil
5183 * (1.0..9.0).each.size # raises TypeError (Float does not have #succ)
5184 * (..10).each.size # raises TypeError (beginless range has nil as its #begin)
5185 *
5186 * The \Enumerable module itself does not define a #size method.
5187 * A class that includes \Enumerable may define its own #size method.
5188 * It is recommended that such a #size method be consistent with
5189 * Enumerator#size.
5190 *
5191 * Array and Hash implement #size and return values consistent with
5192 * Enumerator#size.
5193 * IO and Dir do not define #size, which is also consistent because the
5194 * corresponding enumerator's size function returns +nil+.
5195 *
5196 * However, it is not strictly required for a class's #size method to match Enumerator#size.
5197 * For example, File#size returns the number of bytes in the file, not the number of lines.
5198 *
5199 */
5200
5201void
5202Init_Enumerable(void)
5203{
5204 rb_mEnumerable = rb_define_module("Enumerable");
5205
5206 rb_define_method(rb_mEnumerable, "to_a", enum_to_a, -1);
5207 rb_define_method(rb_mEnumerable, "entries", enum_to_a, -1);
5208 rb_define_method(rb_mEnumerable, "to_h", enum_to_h, -1);
5209
5210 rb_define_method(rb_mEnumerable, "sort", enum_sort, 0);
5211 rb_define_method(rb_mEnumerable, "sort_by", enum_sort_by, 0);
5212 rb_define_method(rb_mEnumerable, "grep", enum_grep, 1);
5213 rb_define_method(rb_mEnumerable, "grep_v", enum_grep_v, 1);
5214 rb_define_method(rb_mEnumerable, "count", enum_count, -1);
5215 rb_define_method(rb_mEnumerable, "find", enum_find, -1);
5216 rb_define_method(rb_mEnumerable, "detect", enum_find, -1);
5217 rb_define_method(rb_mEnumerable, "find_index", enum_find_index, -1);
5218 rb_define_method(rb_mEnumerable, "find_all", enum_find_all, 0);
5219 rb_define_method(rb_mEnumerable, "select", enum_find_all, 0);
5220 rb_define_method(rb_mEnumerable, "filter", enum_find_all, 0);
5221 rb_define_method(rb_mEnumerable, "filter_map", enum_filter_map, 0);
5222 rb_define_method(rb_mEnumerable, "reject", enum_reject, 0);
5223 rb_define_method(rb_mEnumerable, "collect", enum_collect, 0);
5224 rb_define_method(rb_mEnumerable, "map", enum_collect, 0);
5225 rb_define_method(rb_mEnumerable, "flat_map", enum_flat_map, 0);
5226 rb_define_method(rb_mEnumerable, "collect_concat", enum_flat_map, 0);
5227 rb_define_method(rb_mEnumerable, "inject", enum_inject, -1);
5228 rb_define_method(rb_mEnumerable, "reduce", enum_inject, -1);
5229 rb_define_method(rb_mEnumerable, "partition", enum_partition, 0);
5230 rb_define_method(rb_mEnumerable, "group_by", enum_group_by, 0);
5231 rb_define_method(rb_mEnumerable, "tally", enum_tally, -1);
5232 rb_define_method(rb_mEnumerable, "first", enum_first, -1);
5233 rb_define_method(rb_mEnumerable, "all?", enum_all, -1);
5234 rb_define_method(rb_mEnumerable, "any?", enum_any, -1);
5235 rb_define_method(rb_mEnumerable, "one?", enum_one, -1);
5236 rb_define_method(rb_mEnumerable, "none?", enum_none, -1);
5237 rb_define_method(rb_mEnumerable, "min", enum_min, -1);
5238 rb_define_method(rb_mEnumerable, "max", enum_max, -1);
5239 rb_define_method(rb_mEnumerable, "minmax", enum_minmax, 0);
5240 rb_define_method(rb_mEnumerable, "min_by", enum_min_by, -1);
5241 rb_define_method(rb_mEnumerable, "max_by", enum_max_by, -1);
5242 rb_define_method(rb_mEnumerable, "minmax_by", enum_minmax_by, 0);
5243 rb_define_method(rb_mEnumerable, "member?", enum_member, 1);
5244 rb_define_method(rb_mEnumerable, "include?", enum_member, 1);
5245 rb_define_method(rb_mEnumerable, "each_with_index", enum_each_with_index, -1);
5246 rb_define_method(rb_mEnumerable, "reverse_each", enum_reverse_each, -1);
5247 rb_define_method(rb_mEnumerable, "each_entry", enum_each_entry, -1);
5248 rb_define_method(rb_mEnumerable, "each_slice", enum_each_slice, 1);
5249 rb_define_method(rb_mEnumerable, "each_cons", enum_each_cons, 1);
5250 rb_define_method(rb_mEnumerable, "each_with_object", enum_each_with_object, 1);
5251 rb_define_method(rb_mEnumerable, "zip", enum_zip, -1);
5252 rb_define_method(rb_mEnumerable, "take", enum_take, 1);
5253 rb_define_method(rb_mEnumerable, "take_while", enum_take_while, 0);
5254 rb_define_method(rb_mEnumerable, "drop", enum_drop, 1);
5255 rb_define_method(rb_mEnumerable, "drop_while", enum_drop_while, 0);
5256 rb_define_method(rb_mEnumerable, "cycle", enum_cycle, -1);
5257 rb_define_method(rb_mEnumerable, "chunk", enum_chunk, 0);
5258 rb_define_method(rb_mEnumerable, "slice_before", enum_slice_before, -1);
5259 rb_define_method(rb_mEnumerable, "slice_after", enum_slice_after, -1);
5260 rb_define_method(rb_mEnumerable, "slice_when", enum_slice_when, 0);
5261 rb_define_method(rb_mEnumerable, "chunk_while", enum_chunk_while, 0);
5262 rb_define_method(rb_mEnumerable, "sum", enum_sum, -1);
5263 rb_define_method(rb_mEnumerable, "uniq", enum_uniq, 0);
5264 rb_define_method(rb_mEnumerable, "compact", enum_compact, 0);
5265
5266 id__alone = rb_intern_const("_alone");
5267 id__separator = rb_intern_const("_separator");
5268 id_chunk_categorize = rb_intern_const("chunk_categorize");
5269 id_chunk_enumerable = rb_intern_const("chunk_enumerable");
5270 id_next = rb_intern_const("next");
5271 id_sliceafter_enum = rb_intern_const("sliceafter_enum");
5272 id_sliceafter_pat = rb_intern_const("sliceafter_pat");
5273 id_sliceafter_pred = rb_intern_const("sliceafter_pred");
5274 id_slicebefore_enumerable = rb_intern_const("slicebefore_enumerable");
5275 id_slicebefore_sep_pat = rb_intern_const("slicebefore_sep_pat");
5276 id_slicebefore_sep_pred = rb_intern_const("slicebefore_sep_pred");
5277 id_slicewhen_enum = rb_intern_const("slicewhen_enum");
5278 id_slicewhen_inverted = rb_intern_const("slicewhen_inverted");
5279 id_slicewhen_pred = rb_intern_const("slicewhen_pred");
5280}
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:219
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
VALUE rb_define_module(const char *name)
Defines a top-level module.
Definition class.c:1602
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Retrieves argument from argc and argv to given VALUE references according to the format string.
Definition class.c:3138
int rb_block_given_p(void)
Determines if the current method is given a block.
Definition eval.c:943
#define TYPE(_)
Old name of rb_type.
Definition value_type.h:108
#define RB_INTEGER_TYPE_P
Old name of rb_integer_type_p.
Definition value_type.h:87
#define RFLOAT_VALUE
Old name of rb_float_value.
Definition double.h:28
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
Definition long.h:48
#define UNREACHABLE
Old name of RBIMPL_UNREACHABLE.
Definition assume.h:28
#define T_FLOAT
Old name of RUBY_T_FLOAT.
Definition value_type.h:64
#define ID2SYM
Old name of RB_ID2SYM.
Definition symbol.h:44
#define T_BIGNUM
Old name of RUBY_T_BIGNUM.
Definition value_type.h:57
#define ULONG2NUM
Old name of RB_ULONG2NUM.
Definition long.h:60
#define T_FIXNUM
Old name of RUBY_T_FIXNUM.
Definition value_type.h:63
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
Definition assume.h:29
#define SYM2ID
Old name of RB_SYM2ID.
Definition symbol.h:45
#define CLASS_OF
Old name of rb_class_of.
Definition globals.h:205
#define rb_ary_new4
Old name of rb_ary_new_from_values.
Definition array.h:659
#define FIXABLE
Old name of RB_FIXABLE.
Definition fixnum.h:25
#define LONG2FIX
Old name of RB_INT2FIX.
Definition long.h:49
#define FIX2ULONG
Old name of RB_FIX2ULONG.
Definition long.h:47
#define T_RATIONAL
Old name of RUBY_T_RATIONAL.
Definition value_type.h:76
#define T_HASH
Old name of RUBY_T_HASH.
Definition value_type.h:65
#define NUM2DBL
Old name of rb_num2dbl.
Definition double.h:27
#define rb_ary_new3
Old name of rb_ary_new_from_args.
Definition array.h:658
#define LONG2NUM
Old name of RB_LONG2NUM.
Definition long.h:50
#define T_UNDEF
Old name of RUBY_T_UNDEF.
Definition value_type.h:82
#define Qtrue
Old name of RUBY_Qtrue.
#define FIXNUM_MAX
Old name of RUBY_FIXNUM_MAX.
Definition fixnum.h:26
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define FIX2LONG
Old name of RB_FIX2LONG.
Definition long.h:46
#define T_ARRAY
Old name of RUBY_T_ARRAY.
Definition value_type.h:56
#define NIL_P
Old name of RB_NIL_P.
#define DBL2NUM
Old name of rb_float_new.
Definition double.h:29
#define NUM2LONG
Old name of RB_NUM2LONG.
Definition long.h:51
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define CONST_ID
Old name of RUBY_CONST_ID.
Definition symbol.h:47
#define rb_ary_new2
Old name of rb_ary_new_capa.
Definition array.h:657
#define SYMBOL_P
Old name of RB_SYMBOL_P.
Definition value_type.h:88
#define T_REGEXP
Old name of RUBY_T_REGEXP.
Definition value_type.h:77
void rb_iter_break(void)
Breaks from a block.
Definition vm.c:2111
VALUE rb_eTypeError
TypeError exception.
Definition error.c:1430
VALUE rb_eRuntimeError
RuntimeError exception.
Definition error.c:1428
VALUE rb_eStopIteration
StopIteration exception.
Definition enumerator.c:180
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
Definition error.c:466
void rb_warning(const char *fmt,...)
Issues a warning.
Definition error.c:497
VALUE rb_cArray
Array class.
VALUE rb_obj_alloc(VALUE klass)
Allocates an instance of the given class.
Definition object.c:2125
VALUE rb_mEnumerable
Enumerable module.
Definition enum.c:27
VALUE rb_cEnumerator
Enumerator class.
Definition enumerator.c:163
VALUE rb_cInteger
Module class.
Definition numeric.c:198
VALUE rb_obj_hide(VALUE obj)
Make the object invisible from Ruby code.
Definition object.c:101
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
Definition object.c:243
double rb_num2dbl(VALUE num)
Converts an instance of rb_cNumeric into C's double.
Definition object.c:3746
VALUE rb_equal(VALUE lhs, VALUE rhs)
This function is an optimised version of calling #==.
Definition object.c:175
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
Definition gc.h:615
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition gc.h:603
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Definition vm_eval.c:1117
VALUE rb_funcallv_public(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcallv(), except it only takes public methods into account.
Definition vm_eval.c:1168
VALUE rb_ary_new_from_values(long n, const VALUE *elts)
Identical to rb_ary_new_from_args(), except how objects are passed.
VALUE rb_ary_concat(VALUE lhs, VALUE rhs)
Destructively appends the contents of latter into the end of former.
VALUE rb_ary_reverse(VALUE ary)
Destructively reverses the passed array in-place.
VALUE rb_ary_shift(VALUE ary)
Destructively deletes an element from the beginning of the passed array and returns what was deleted.
VALUE rb_ary_dup(VALUE ary)
Duplicates an array.
VALUE rb_check_array_type(VALUE obj)
Try converting an object to its array representation using its to_ary method, if any.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_resize(VALUE ary, long len)
Expands or shrinks the passed array to the passed length.
VALUE rb_ary_hidden_new(long capa)
Allocates a hidden (no class) empty array.
VALUE rb_ary_clear(VALUE ary)
Destructively removes everything form an array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_ary_sort_bang(VALUE ary)
Destructively sorts the passed array in-place, according to each elements' <=> result.
VALUE rb_assoc_new(VALUE car, VALUE cdr)
Identical to rb_ary_new_from_values(), except it expects exactly two parameters.
void rb_ary_store(VALUE ary, long key, VALUE val)
Destructively stores the passed value to the passed array's passed index.
#define RETURN_SIZED_ENUMERATOR(obj, argc, argv, size_fn)
This roughly resembles return enum_for(__callee__) unless block_given?.
Definition enumerator.h:206
#define RETURN_ENUMERATOR(obj, argc, argv)
Identical to RETURN_SIZED_ENUMERATOR(), except its size is unknown.
Definition enumerator.h:239
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
Definition error.h:284
VALUE rb_block_proc(void)
Constructs a Proc object from implicitly passed components.
Definition proc.c:847
int rb_range_values(VALUE range, VALUE *begp, VALUE *endp, int *exclp)
Deconstructs a range into its components.
Definition range.c:1838
VALUE rb_check_string_type(VALUE obj)
Try converting an object to its stringised representation using its to_str method,...
Definition string.c:3255
VALUE rb_ivar_set(VALUE obj, ID name, VALUE val)
Identical to rb_iv_set(), except it accepts the name as an ID instead of a C string.
Definition variable.c:2079
VALUE rb_ivar_get(VALUE obj, ID name)
Identical to rb_iv_get(), except it accepts the name as an ID instead of a C string.
Definition variable.c:1443
int rb_respond_to(VALUE obj, ID mid)
Queries if the object responds to the method.
Definition vm_method.c:3094
VALUE rb_check_funcall(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcallv(), except it returns RUBY_Qundef instead of raising rb_eNoMethodError.
Definition vm_eval.c:686
int rb_obj_respond_to(VALUE obj, ID mid, int private_p)
Identical to rb_respond_to(), except it additionally takes the visibility parameter.
Definition vm_method.c:3078
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
Definition symbol.h:284
ID rb_check_id(volatile VALUE *namep)
Detects if the given name is already interned or not.
Definition symbol.c:1117
VALUE rb_sym2str(VALUE symbol)
Obtain a frozen string representation of a symbol (not including the leading colon).
Definition symbol.c:972
int len
Length of the buffer.
Definition io.h:8
void ruby_qsort(void *, const size_t, const size_t, int(*)(const void *, const void *, void *), void *)
Reentrant implementation of quick sort.
#define RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg)
Shim for block function parameters.
Definition iterator.h:58
VALUE rb_yield_values(int n,...)
Identical to rb_yield(), except it takes variadic number of parameters and pass them to the block.
Definition vm_eval.c:1384
VALUE rb_yield_values2(int n, const VALUE *argv)
Identical to rb_yield_values(), except it takes the parameters as a C array instead of variadic argum...
Definition vm_eval.c:1406
VALUE rb_yield(VALUE val)
Yields the block.
Definition vm_eval.c:1372
rb_block_call_func * rb_block_call_func_t
Shorthand type that represents an iterator-written-in-C function pointer.
Definition iterator.h:88
VALUE rb_block_call_func(RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg))
This is the type of a function that the interpreter expect for C-backended blocks.
Definition iterator.h:83
VALUE rb_block_call_kw(VALUE obj, ID mid, int argc, const VALUE *argv, rb_block_call_func_t proc, VALUE data2, int kw_splat)
Identical to rb_funcallv_kw(), except it additionally passes a function as a block.
Definition vm_eval.c:1559
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
Definition memory.h:167
VALUE rb_block_call(VALUE q, ID w, int e, const VALUE *r, type *t, VALUE y)
Call a method with a block.
void rb_hash_foreach(VALUE q, int_type *w, VALUE e)
Iteration over the given hash.
VALUE rb_rescue2(type *q, VALUE w, type *e, VALUE r,...)
An equivalent of rescue clause.
#define RARRAY_LEN
Just another name of rb_array_len.
Definition rarray.h:51
static void RARRAY_ASET(VALUE ary, long i, VALUE v)
Assigns an object in an array.
Definition rarray.h:386
#define RARRAY_PTR_USE(ary, ptr_name, expr)
Declares a section of code where raw pointers are used.
Definition rarray.h:348
static VALUE * RARRAY_PTR(VALUE ary)
Wild use of a C pointer.
Definition rarray.h:366
#define RARRAY_AREF(a, i)
Definition rarray.h:403
#define RBASIC(obj)
Convenient casting macro.
Definition rbasic.h:40
#define RB_PASS_CALLED_KEYWORDS
Pass keywords if current method is called with keywords, useful for argument delegation.
Definition scan_args.h:78
#define RTEST
This is an old name of RB_TEST.
#define _(args)
This was a transition path from K&R to ANSI.
Definition stdarg.h:35
MEMO.
Definition imemo.h:108
Definition enum.c:2401
Definition enum.c:2278
IFUNC (Internal FUNCtion)
Definition imemo.h:87
intptr_t SIGNED_VALUE
A signed integer type that has the same width with VALUE.
Definition value.h:63
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
static bool RB_FLOAT_TYPE_P(VALUE obj)
Queries if the object is an instance of rb_cFloat.
Definition value_type.h:264
static void Check_Type(VALUE v, enum ruby_value_type t)
Identical to RB_TYPE_P(), except it raises exceptions on predication failure.
Definition value_type.h:433
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition value_type.h:376