~ubuntu-branches/ubuntu/hardy/ruby1.8/hardy-updates

« back to all changes in this revision

Viewing changes to enum.c

  • Committer: Bazaar Package Importer
  • Author(s): akira yamada
  • Date: 2007-03-13 22:11:58 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20070313221158-h3oql37brlaf2go2
Tags: 1.8.6-1
* new upstream version, 1.8.6.
* libruby1.8 conflicts with libopenssl-ruby1.8 (< 1.8.6) (closes: #410018)
* changed packaging style to cdbs from dbs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**********************************************************************
 
2
 
 
3
  enum.c -
 
4
 
 
5
  $Author: shyouhei $
 
6
  $Date: 2007-02-13 08:01:19 +0900 (Tue, 13 Feb 2007) $
 
7
  created at: Fri Oct  1 15:15:19 JST 1993
 
8
 
 
9
  Copyright (C) 1993-2003 Yukihiro Matsumoto
 
10
 
 
11
**********************************************************************/
 
12
 
 
13
#include "ruby.h"
 
14
#include "node.h"
 
15
#include "util.h"
 
16
 
 
17
VALUE rb_mEnumerable;
 
18
static ID id_each, id_eqq, id_cmp;
 
19
 
 
20
VALUE
 
21
rb_each(obj)
 
22
    VALUE obj;
 
23
{
 
24
    return rb_funcall(obj, id_each, 0, 0);
 
25
}
 
26
 
 
27
static VALUE
 
28
grep_i(i, arg)
 
29
    VALUE i, *arg;
 
30
{
 
31
    if (RTEST(rb_funcall(arg[0], id_eqq, 1, i))) {
 
32
        rb_ary_push(arg[1], i);
 
33
    }
 
34
    return Qnil;
 
35
}
 
36
 
 
37
static VALUE
 
38
grep_iter_i(i, arg)
 
39
    VALUE i, *arg;
 
40
{
 
41
    if (RTEST(rb_funcall(arg[0], id_eqq, 1, i))) {
 
42
        rb_ary_push(arg[1], rb_yield(i));
 
43
    }
 
44
    return Qnil;
 
45
}
 
46
 
 
47
/*
 
48
 *  call-seq:
 
49
 *     enum.grep(pattern)                   => array
 
50
 *     enum.grep(pattern) {| obj | block }  => array
 
51
 *  
 
52
 *  Returns an array of every element in <i>enum</i> for which
 
53
 *  <code>Pattern === element</code>. If the optional <em>block</em> is
 
54
 *  supplied, each matching element is passed to it, and the block's
 
55
 *  result is stored in the output array.
 
56
 *     
 
57
 *     (1..100).grep 38..44   #=> [38, 39, 40, 41, 42, 43, 44]
 
58
 *     c = IO.constants
 
59
 *     c.grep(/SEEK/)         #=> ["SEEK_END", "SEEK_SET", "SEEK_CUR"]
 
60
 *     res = c.grep(/SEEK/) {|v| IO.const_get(v) }
 
61
 *     res                    #=> [2, 0, 1]
 
62
 *     
 
63
 */
 
64
 
 
65
static VALUE
 
66
enum_grep(obj, pat)
 
67
    VALUE obj, pat;
 
68
{
 
69
    VALUE ary = rb_ary_new();
 
70
    VALUE arg[2];
 
71
 
 
72
    arg[0] = pat;
 
73
    arg[1] = ary;
 
74
 
 
75
    rb_iterate(rb_each, obj, rb_block_given_p() ? grep_iter_i : grep_i, (VALUE)arg);
 
76
    
 
77
    return ary;
 
78
}
 
79
 
 
80
static VALUE
 
81
find_i(i, memo)
 
82
    VALUE i;
 
83
    VALUE *memo;
 
84
{
 
85
    if (RTEST(rb_yield(i))) {
 
86
        *memo = i;
 
87
        rb_iter_break();
 
88
    }
 
89
    return Qnil;
 
90
}
 
91
 
 
92
/*
 
93
 *  call-seq:
 
94
 *     enum.detect(ifnone = nil) {| obj | block }  => obj or nil
 
95
 *     enum.find(ifnone = nil)   {| obj | block }  => obj or nil
 
96
 *  
 
97
 *  Passes each entry in <i>enum</i> to <em>block</em>. Returns the
 
98
 *  first for which <em>block</em> is not <code>false</code>.  If no
 
99
 *  object matches, calls <i>ifnone</i> and returns its result when it
 
100
 *  is specified, or returns <code>nil</code>
 
101
 *     
 
102
 *     (1..10).detect  {|i| i % 5 == 0 and i % 7 == 0 }   #=> nil
 
103
 *     (1..100).detect {|i| i % 5 == 0 and i % 7 == 0 }   #=> 35
 
104
 *     
 
105
 */
 
106
 
 
107
static VALUE
 
108
enum_find(argc, argv, obj)
 
109
    int argc;
 
110
    VALUE* argv;
 
111
    VALUE obj;
 
112
{
 
113
    VALUE memo = Qundef;
 
114
    VALUE if_none;
 
115
 
 
116
    rb_scan_args(argc, argv, "01", &if_none);
 
117
    rb_iterate(rb_each, obj, find_i, (VALUE)&memo);
 
118
    if (memo != Qundef) {
 
119
        return memo;
 
120
    }
 
121
    if (!NIL_P(if_none)) {
 
122
        return rb_funcall(if_none, rb_intern("call"), 0, 0);
 
123
    }
 
124
    return Qnil;
 
125
}
 
126
 
 
127
static VALUE
 
128
find_all_i(i, ary)
 
129
    VALUE i, ary;
 
130
{
 
131
    if (RTEST(rb_yield(i))) {
 
132
        rb_ary_push(ary, i);
 
133
    }
 
134
    return Qnil;
 
135
}
 
136
 
 
137
/*
 
138
 *  call-seq:
 
139
 *     enum.find_all {| obj | block }  => array
 
140
 *     enum.select   {| obj | block }  => array
 
141
 *  
 
142
 *  Returns an array containing all elements of <i>enum</i> for which
 
143
 *  <em>block</em> is not <code>false</code> (see also
 
144
 *  <code>Enumerable#reject</code>).
 
145
 *     
 
146
 *     (1..10).find_all {|i|  i % 3 == 0 }   #=> [3, 6, 9]
 
147
 *     
 
148
 */
 
149
 
 
150
static VALUE
 
151
enum_find_all(obj)
 
152
    VALUE obj;
 
153
{
 
154
    VALUE ary = rb_ary_new();
 
155
    
 
156
    rb_iterate(rb_each, obj, find_all_i, ary);
 
157
 
 
158
    return ary;
 
159
}
 
160
 
 
161
static VALUE
 
162
reject_i(i, ary)
 
163
    VALUE i, ary;
 
164
{
 
165
    if (!RTEST(rb_yield(i))) {
 
166
        rb_ary_push(ary, i);
 
167
    }
 
168
    return Qnil;
 
169
}
 
170
 
 
171
/*
 
172
 *  call-seq:
 
173
 *     enum.reject {| obj | block }  => array
 
174
 *  
 
175
 *  Returns an array for all elements of <i>enum</i> for which
 
176
 *  <em>block</em> is false (see also <code>Enumerable#find_all</code>).
 
177
 *     
 
178
 *     (1..10).reject {|i|  i % 3 == 0 }   #=> [1, 2, 4, 5, 7, 8, 10]
 
179
 *     
 
180
 */
 
181
 
 
182
static VALUE
 
183
enum_reject(obj)
 
184
    VALUE obj;
 
185
{
 
186
    VALUE ary = rb_ary_new();
 
187
    
 
188
    rb_iterate(rb_each, obj, reject_i, ary);
 
189
 
 
190
    return ary;
 
191
}
 
192
 
 
193
static VALUE
 
194
collect_i(i, ary)
 
195
    VALUE i, ary;
 
196
{
 
197
    rb_ary_push(ary, rb_yield(i));
 
198
 
 
199
    return Qnil;
 
200
}
 
201
 
 
202
static VALUE
 
203
collect_all(i, ary)
 
204
    VALUE i, ary;
 
205
{
 
206
    rb_ary_push(ary, i);
 
207
 
 
208
    return Qnil;
 
209
}
 
210
 
 
211
/*
 
212
 *  call-seq:
 
213
 *     enum.collect {| obj | block }  => array
 
214
 *     enum.map     {| obj | block }  => array
 
215
 *  
 
216
 *  Returns a new array with the results of running <em>block</em> once
 
217
 *  for every element in <i>enum</i>.
 
218
 *     
 
219
 *     (1..4).collect {|i| i*i }   #=> [1, 4, 9, 16]
 
220
 *     (1..4).collect { "cat"  }   #=> ["cat", "cat", "cat", "cat"]
 
221
 *     
 
222
 */
 
223
 
 
224
static VALUE
 
225
enum_collect(obj)
 
226
    VALUE obj;
 
227
{
 
228
    VALUE ary = rb_ary_new();
 
229
 
 
230
    rb_iterate(rb_each, obj, rb_block_given_p() ? collect_i : collect_all, ary);
 
231
 
 
232
    return ary;
 
233
}
 
234
 
 
235
/*
 
236
 *  call-seq:
 
237
 *     enum.to_a      =>    array
 
238
 *     enum.entries   =>    array
 
239
 *  
 
240
 *  Returns an array containing the items in <i>enum</i>.
 
241
 *     
 
242
 *     (1..7).to_a                       #=> [1, 2, 3, 4, 5, 6, 7]
 
243
 *     { 'a'=>1, 'b'=>2, 'c'=>3 }.to_a   #=> [["a", 1], ["b", 2], ["c", 3]]
 
244
 */
 
245
static VALUE
 
246
enum_to_a(obj)
 
247
    VALUE obj;
 
248
{
 
249
    VALUE ary = rb_ary_new();
 
250
 
 
251
    rb_iterate(rb_each, obj, collect_all, ary);
 
252
 
 
253
    return ary;
 
254
}
 
255
 
 
256
static VALUE
 
257
inject_i(i, memo)
 
258
    VALUE i;
 
259
    VALUE *memo;
 
260
{
 
261
    if (*memo == Qundef) {
 
262
        *memo = i;
 
263
    }
 
264
    else {
 
265
        *memo = rb_yield_values(2, *memo, i);
 
266
    }
 
267
    return Qnil;
 
268
}
 
269
 
 
270
/*
 
271
 *  call-seq:
 
272
 *     enum.inject(initial) {| memo, obj | block }  => obj
 
273
 *     enum.inject          {| memo, obj | block }  => obj
 
274
 *  
 
275
 *  Combines the elements of <i>enum</i> by applying the block to an
 
276
 *  accumulator value (<i>memo</i>) and each element in turn. At each
 
277
 *  step, <i>memo</i> is set to the value returned by the block. The
 
278
 *  first form lets you supply an initial value for <i>memo</i>. The
 
279
 *  second form uses the first element of the collection as a the
 
280
 *  initial value (and skips that element while iterating).
 
281
 *     
 
282
 *     # Sum some numbers
 
283
 *     (5..10).inject {|sum, n| sum + n }              #=> 45
 
284
 *     # Multiply some numbers
 
285
 *     (5..10).inject(1) {|product, n| product * n }   #=> 151200
 
286
 *     
 
287
 *     # find the longest word
 
288
 *     longest = %w{ cat sheep bear }.inject do |memo,word|
 
289
 *        memo.length > word.length ? memo : word
 
290
 *     end
 
291
 *     longest                                         #=> "sheep"
 
292
 *     
 
293
 *     # find the length of the longest word
 
294
 *     longest = %w{ cat sheep bear }.inject(0) do |memo,word|
 
295
 *        memo >= word.length ? memo : word.length
 
296
 *     end
 
297
 *     longest                                         #=> 5
 
298
 *     
 
299
 */
 
300
 
 
301
static VALUE
 
302
enum_inject(argc, argv, obj)
 
303
    int argc;
 
304
    VALUE *argv, obj;
 
305
{
 
306
    VALUE memo = Qundef;
 
307
 
 
308
    if (rb_scan_args(argc, argv, "01", &memo) == 0)
 
309
        memo = Qundef;
 
310
    rb_iterate(rb_each, obj, inject_i, (VALUE)&memo);
 
311
    if (memo == Qundef) return Qnil;
 
312
    return memo;
 
313
}
 
314
 
 
315
static VALUE
 
316
partition_i(i, ary)
 
317
    VALUE i, *ary;
 
318
{
 
319
    if (RTEST(rb_yield(i))) {
 
320
        rb_ary_push(ary[0], i);
 
321
    }
 
322
    else {
 
323
        rb_ary_push(ary[1], i);
 
324
    }
 
325
    return Qnil;
 
326
}
 
327
 
 
328
/*
 
329
 *  call-seq:
 
330
 *     enum.partition {| obj | block }  => [ true_array, false_array ]
 
331
 *  
 
332
 *  Returns two arrays, the first containing the elements of
 
333
 *  <i>enum</i> for which the block evaluates to true, the second
 
334
 *  containing the rest.
 
335
 *     
 
336
 *     (1..6).partition {|i| (i&1).zero?}   #=> [[2, 4, 6], [1, 3, 5]]
 
337
 *     
 
338
 */
 
339
 
 
340
static VALUE
 
341
enum_partition(obj)
 
342
    VALUE obj;
 
343
{
 
344
    VALUE ary[2];
 
345
 
 
346
    ary[0] = rb_ary_new();
 
347
    ary[1] = rb_ary_new();
 
348
    rb_iterate(rb_each, obj, partition_i, (VALUE)ary);
 
349
 
 
350
    return rb_assoc_new(ary[0], ary[1]);
 
351
}
 
352
 
 
353
/*
 
354
 *  call-seq:
 
355
 *     enum.sort                     => array
 
356
 *     enum.sort {| a, b | block }   => array
 
357
 *  
 
358
 *  Returns an array containing the items in <i>enum</i> sorted,
 
359
 *  either according to their own <code><=></code> method, or by using
 
360
 *  the results of the supplied block. The block should return -1, 0, or
 
361
 *  +1 depending on the comparison between <i>a</i> and <i>b</i>. As of
 
362
 *  Ruby 1.8, the method <code>Enumerable#sort_by</code> implements a
 
363
 *  built-in Schwartzian Transform, useful when key computation or
 
364
 *  comparison is expensive..
 
365
 *     
 
366
 *     %w(rhea kea flea).sort         #=> ["flea", "kea", "rhea"]
 
367
 *     (1..10).sort {|a,b| b <=> a}   #=> [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
 
368
 */
 
369
 
 
370
static VALUE
 
371
enum_sort(obj)
 
372
    VALUE obj;
 
373
{
 
374
    return rb_ary_sort(enum_to_a(obj));
 
375
}
 
376
 
 
377
static VALUE
 
378
sort_by_i(i, ary)
 
379
    VALUE i, ary;
 
380
{
 
381
    VALUE v;
 
382
    NODE *memo;
 
383
 
 
384
    v = rb_yield(i);
 
385
    if (RBASIC(ary)->klass) {
 
386
        rb_raise(rb_eRuntimeError, "sort_by reentered");
 
387
    }
 
388
    memo = rb_node_newnode(NODE_MEMO, v, i, 0);
 
389
    rb_ary_push(ary, (VALUE)memo);
 
390
    return Qnil;
 
391
}
 
392
 
 
393
static int
 
394
sort_by_cmp(aa, bb)
 
395
    NODE **aa, **bb;
 
396
{
 
397
    VALUE a = aa[0]->u1.value;
 
398
    VALUE b = bb[0]->u1.value;
 
399
 
 
400
    return rb_cmpint(rb_funcall(a, id_cmp, 1, b), a, b);
 
401
}
 
402
 
 
403
/*
 
404
 *  call-seq:
 
405
 *     enum.sort_by {| obj | block }    => array
 
406
 *  
 
407
 *  Sorts <i>enum</i> using a set of keys generated by mapping the
 
408
 *  values in <i>enum</i> through the given block.
 
409
 *     
 
410
 *     %w{ apple pear fig }.sort_by {|word| word.length}
 
411
                    #=> ["fig", "pear", "apple"]
 
412
 *     
 
413
 *  The current implementation of <code>sort_by</code> generates an
 
414
 *  array of tuples containing the original collection element and the
 
415
 *  mapped value. This makes <code>sort_by</code> fairly expensive when
 
416
 *  the keysets are simple
 
417
 *     
 
418
 *     require 'benchmark'
 
419
 *     include Benchmark
 
420
 *     
 
421
 *     a = (1..100000).map {rand(100000)}
 
422
 *     
 
423
 *     bm(10) do |b|
 
424
 *       b.report("Sort")    { a.sort }
 
425
 *       b.report("Sort by") { a.sort_by {|a| a} }
 
426
 *     end
 
427
 *     
 
428
 *  <em>produces:</em>
 
429
 *     
 
430
 *     user     system      total        real
 
431
 *     Sort        0.180000   0.000000   0.180000 (  0.175469)
 
432
 *     Sort by     1.980000   0.040000   2.020000 (  2.013586)
 
433
 *     
 
434
 *  However, consider the case where comparing the keys is a non-trivial
 
435
 *  operation. The following code sorts some files on modification time
 
436
 *  using the basic <code>sort</code> method.
 
437
 *     
 
438
 *     files = Dir["*"]
 
439
 *     sorted = files.sort {|a,b| File.new(a).mtime <=> File.new(b).mtime}
 
440
 *     sorted   #=> ["mon", "tues", "wed", "thurs"]
 
441
 *     
 
442
 *  This sort is inefficient: it generates two new <code>File</code>
 
443
 *  objects during every comparison. A slightly better technique is to
 
444
 *  use the <code>Kernel#test</code> method to generate the modification
 
445
 *  times directly.
 
446
 *     
 
447
 *     files = Dir["*"]
 
448
 *     sorted = files.sort { |a,b|
 
449
 *       test(?M, a) <=> test(?M, b)
 
450
 *     }
 
451
 *     sorted   #=> ["mon", "tues", "wed", "thurs"]
 
452
 *     
 
453
 *  This still generates many unnecessary <code>Time</code> objects. A
 
454
 *  more efficient technique is to cache the sort keys (modification
 
455
 *  times in this case) before the sort. Perl users often call this
 
456
 *  approach a Schwartzian Transform, after Randal Schwartz. We
 
457
 *  construct a temporary array, where each element is an array
 
458
 *  containing our sort key along with the filename. We sort this array,
 
459
 *  and then extract the filename from the result.
 
460
 *     
 
461
 *     sorted = Dir["*"].collect { |f|
 
462
 *        [test(?M, f), f]
 
463
 *     }.sort.collect { |f| f[1] }
 
464
 *     sorted   #=> ["mon", "tues", "wed", "thurs"]
 
465
 *     
 
466
 *  This is exactly what <code>sort_by</code> does internally.
 
467
 *     
 
468
 *     sorted = Dir["*"].sort_by {|f| test(?M, f)}
 
469
 *     sorted   #=> ["mon", "tues", "wed", "thurs"]
 
470
 */
 
471
 
 
472
static VALUE
 
473
enum_sort_by(obj)
 
474
    VALUE obj;
 
475
{
 
476
    VALUE ary;
 
477
    long i;
 
478
 
 
479
    if (TYPE(obj) == T_ARRAY) {
 
480
        ary  = rb_ary_new2(RARRAY(obj)->len);
 
481
    }
 
482
    else {
 
483
        ary = rb_ary_new();
 
484
    }
 
485
    RBASIC(ary)->klass = 0;
 
486
    rb_iterate(rb_each, obj, sort_by_i, ary);
 
487
    if (RARRAY(ary)->len > 1) {
 
488
        qsort(RARRAY(ary)->ptr, RARRAY(ary)->len, sizeof(VALUE), sort_by_cmp, 0);
 
489
    }
 
490
    if (RBASIC(ary)->klass) {
 
491
        rb_raise(rb_eRuntimeError, "sort_by reentered");
 
492
    }
 
493
    for (i=0; i<RARRAY(ary)->len; i++) {
 
494
        RARRAY(ary)->ptr[i] = RNODE(RARRAY(ary)->ptr[i])->u2.value;
 
495
    }
 
496
    RBASIC(ary)->klass = rb_cArray;
 
497
    return ary;
 
498
}
 
499
 
 
500
static VALUE
 
501
all_iter_i(i, memo)
 
502
    VALUE i;
 
503
    VALUE *memo;
 
504
{
 
505
    if (!RTEST(rb_yield(i))) {
 
506
        *memo = Qfalse;
 
507
        rb_iter_break();
 
508
    }
 
509
    return Qnil;
 
510
}
 
511
 
 
512
static VALUE
 
513
all_i(i, memo)
 
514
    VALUE i;
 
515
    VALUE *memo;
 
516
{
 
517
    if (!RTEST(i)) {
 
518
        *memo = Qfalse;
 
519
        rb_iter_break();
 
520
    }
 
521
    return Qnil;
 
522
}
 
523
 
 
524
/*
 
525
 *  call-seq:
 
526
 *     enum.all? [{|obj| block } ]   => true or false
 
527
 *  
 
528
 *  Passes each element of the collection to the given block. The method
 
529
 *  returns <code>true</code> if the block never returns
 
530
 *  <code>false</code> or <code>nil</code>. If the block is not given,
 
531
 *  Ruby adds an implicit block of <code>{|obj| obj}</code> (that is
 
532
 *  <code>all?</code> will return <code>true</code> only if none of the
 
533
 *  collection members are <code>false</code> or <code>nil</code>.)
 
534
 *     
 
535
 *     %w{ ant bear cat}.all? {|word| word.length >= 3}   #=> true
 
536
 *     %w{ ant bear cat}.all? {|word| word.length >= 4}   #=> false
 
537
 *     [ nil, true, 99 ].all?                             #=> false
 
538
 *     
 
539
 */
 
540
 
 
541
static VALUE
 
542
enum_all(obj)
 
543
    VALUE obj;
 
544
{
 
545
    VALUE result = Qtrue;
 
546
 
 
547
    rb_iterate(rb_each, obj, rb_block_given_p() ? all_iter_i : all_i, (VALUE)&result);
 
548
    return result;
 
549
}
 
550
 
 
551
static VALUE
 
552
any_iter_i(i, memo)
 
553
    VALUE i;
 
554
    VALUE *memo;
 
555
{
 
556
    if (RTEST(rb_yield(i))) {
 
557
        *memo = Qtrue;
 
558
        rb_iter_break();
 
559
    }
 
560
    return Qnil;
 
561
}
 
562
 
 
563
static VALUE
 
564
any_i(i, memo)
 
565
    VALUE i;
 
566
    VALUE *memo;
 
567
{
 
568
    if (RTEST(i)) {
 
569
        *memo = Qtrue;
 
570
        rb_iter_break();
 
571
    }
 
572
    return Qnil;
 
573
}
 
574
 
 
575
/*
 
576
 *  call-seq:
 
577
 *     enum.any? [{|obj| block } ]   => true or false
 
578
 *  
 
579
 *  Passes each element of the collection to the given block. The method
 
580
 *  returns <code>true</code> if the block ever returns a value other
 
581
 *  than <code>false</code> or <code>nil</code>. If the block is not
 
582
 *  given, Ruby adds an implicit block of <code>{|obj| obj}</code> (that
 
583
 *  is <code>any?</code> will return <code>true</code> if at least one
 
584
 *  of the collection members is not <code>false</code> or
 
585
 *  <code>nil</code>.
 
586
 *     
 
587
 *     %w{ ant bear cat}.any? {|word| word.length >= 3}   #=> true
 
588
 *     %w{ ant bear cat}.any? {|word| word.length >= 4}   #=> true
 
589
 *     [ nil, true, 99 ].any?                             #=> true
 
590
 *     
 
591
 */
 
592
 
 
593
static VALUE
 
594
enum_any(obj)
 
595
    VALUE obj;
 
596
{
 
597
    VALUE result = Qfalse;
 
598
 
 
599
    rb_iterate(rb_each, obj, rb_block_given_p() ? any_iter_i : any_i, (VALUE)&result);
 
600
    return result;
 
601
}
 
602
 
 
603
static VALUE
 
604
min_i(i, memo)
 
605
    VALUE i;
 
606
    VALUE *memo;
 
607
{
 
608
    VALUE cmp;
 
609
 
 
610
    if (*memo == Qundef) {
 
611
        *memo = i;
 
612
    }
 
613
    else {
 
614
        cmp = rb_funcall(i, id_cmp, 1, *memo);
 
615
        if (rb_cmpint(cmp, i, *memo) < 0) {
 
616
            *memo = i;
 
617
        }
 
618
    }
 
619
    return Qnil;
 
620
}
 
621
 
 
622
static VALUE
 
623
min_ii(i, memo)
 
624
    VALUE i;
 
625
    VALUE *memo;
 
626
{
 
627
    VALUE cmp;
 
628
 
 
629
    if (*memo == Qundef) {
 
630
        *memo = i;
 
631
    }
 
632
    else {
 
633
        cmp = rb_yield_values(2, i, *memo);
 
634
        if (rb_cmpint(cmp, i, *memo) < 0) {
 
635
            *memo = i;
 
636
        }
 
637
    }
 
638
    return Qnil;
 
639
}
 
640
 
 
641
 
 
642
/*
 
643
 *  call-seq:
 
644
 *     enum.min                    => obj
 
645
 *     enum.min {| a,b | block }   => obj
 
646
 *  
 
647
 *  Returns the object in <i>enum</i> with the minimum value. The
 
648
 *  first form assumes all objects implement <code>Comparable</code>;
 
649
 *  the second uses the block to return <em>a <=> b</em>.
 
650
 *     
 
651
 *     a = %w(albatross dog horse)
 
652
 *     a.min                                  #=> "albatross"
 
653
 *     a.min {|a,b| a.length <=> b.length }   #=> "dog"
 
654
 */
 
655
 
 
656
static VALUE
 
657
enum_min(obj)
 
658
    VALUE obj;
 
659
{
 
660
    VALUE result = Qundef;
 
661
 
 
662
    rb_iterate(rb_each, obj, rb_block_given_p() ? min_ii : min_i, (VALUE)&result);
 
663
    if (result == Qundef) return Qnil;
 
664
    return result;
 
665
}
 
666
 
 
667
/*
 
668
 *  call-seq:
 
669
 *     enum.max                    => obj
 
670
 *     enum.max {| a,b | block }   => obj
 
671
 *  
 
672
 *  Returns the object in <i>enum</i> with the maximum value. The
 
673
 *  first form assumes all objects implement <code>Comparable</code>;
 
674
 *  the second uses the block to return <em>a <=> b</em>.
 
675
 *     
 
676
 *     a = %w(albatross dog horse)
 
677
 *     a.max                                  #=> "horse"
 
678
 *     a.max {|a,b| a.length <=> b.length }   #=> "albatross"
 
679
 */
 
680
 
 
681
static VALUE
 
682
max_i(i, memo)
 
683
    VALUE i;
 
684
    VALUE *memo;
 
685
{
 
686
    VALUE cmp;
 
687
 
 
688
    if (*memo == Qundef) {
 
689
        *memo = i;
 
690
    }
 
691
    else {
 
692
        cmp = rb_funcall(i, id_cmp, 1, *memo);
 
693
        if (rb_cmpint(cmp, i, *memo) > 0) {
 
694
            *memo = i;
 
695
        }
 
696
    }
 
697
    return Qnil;
 
698
}
 
699
 
 
700
static VALUE
 
701
max_ii(i, memo)
 
702
    VALUE i;
 
703
    VALUE *memo;
 
704
{
 
705
    VALUE cmp;
 
706
 
 
707
    if (*memo == Qundef) {
 
708
        *memo = i;
 
709
    }
 
710
    else {
 
711
        cmp = rb_yield_values(2, i, *memo);
 
712
        if (rb_cmpint(cmp, i, *memo) > 0) {
 
713
            *memo = i;
 
714
        }
 
715
    }
 
716
    return Qnil;
 
717
}
 
718
 
 
719
/*
 
720
 *  call-seq:
 
721
 *     enum.max                   => obj
 
722
 *     enum.max {|a,b| block }    => obj
 
723
 *  
 
724
 *  Returns the object in _enum_ with the maximum value. The
 
725
 *  first form assumes all objects implement <code>Comparable</code>;
 
726
 *  the second uses the block to return <em>a <=> b</em>.
 
727
 *     
 
728
 *     a = %w(albatross dog horse)
 
729
 *     a.max                                  #=> "horse"
 
730
 *     a.max {|a,b| a.length <=> b.length }   #=> "albatross"
 
731
 */  
 
732
 
 
733
static VALUE
 
734
enum_max(obj)
 
735
    VALUE obj;
 
736
{
 
737
    VALUE result = Qundef;
 
738
 
 
739
    rb_iterate(rb_each, obj, rb_block_given_p() ? max_ii : max_i, (VALUE)&result);
 
740
    if (result == Qundef) return Qnil;
 
741
    return result;
 
742
}
 
743
 
 
744
static VALUE
 
745
member_i(item, memo)
 
746
    VALUE item;
 
747
    VALUE *memo;
 
748
{
 
749
    if (rb_equal(item, memo[0])) {
 
750
        memo[1] = Qtrue;
 
751
        rb_iter_break();
 
752
    }
 
753
    return Qnil;
 
754
}
 
755
 
 
756
/*
 
757
 *  call-seq:
 
758
 *     enum.include?(obj)     => true or false
 
759
 *     enum.member?(obj)      => true or false
 
760
 *  
 
761
 *  Returns <code>true</code> if any member of <i>enum</i> equals
 
762
 *  <i>obj</i>. Equality is tested using <code>==</code>.
 
763
 *     
 
764
 *     IO.constants.include? "SEEK_SET"          #=> true
 
765
 *     IO.constants.include? "SEEK_NO_FURTHER"   #=> false
 
766
 *     
 
767
 */
 
768
 
 
769
static VALUE
 
770
enum_member(obj, val)
 
771
    VALUE obj, val;
 
772
{
 
773
    VALUE memo[2];
 
774
 
 
775
    memo[0] = val;
 
776
    memo[1] = Qfalse;
 
777
    rb_iterate(rb_each, obj, member_i, (VALUE)memo);
 
778
    return memo[1];
 
779
}
 
780
 
 
781
static VALUE
 
782
each_with_index_i(val, memo)
 
783
    VALUE val;
 
784
    VALUE *memo;
 
785
{
 
786
    rb_yield_values(2, val, INT2FIX(*memo));
 
787
    ++*memo;
 
788
    return Qnil;
 
789
}
 
790
 
 
791
/*
 
792
 *  call-seq:
 
793
 *     enum.each_with_index {|obj, i| block }  -> enum
 
794
 *  
 
795
 *  Calls <em>block</em> with two arguments, the item and its index, for
 
796
 *  each item in <i>enum</i>.
 
797
 *     
 
798
 *     hash = Hash.new
 
799
 *     %w(cat dog wombat).each_with_index {|item, index|
 
800
 *       hash[item] = index
 
801
 *     }
 
802
 *     hash   #=> {"cat"=>0, "wombat"=>2, "dog"=>1}
 
803
 *     
 
804
 */
 
805
 
 
806
static VALUE
 
807
enum_each_with_index(obj)
 
808
    VALUE obj;
 
809
{
 
810
    VALUE memo = 0;
 
811
 
 
812
    rb_need_block();
 
813
    rb_iterate(rb_each, obj, each_with_index_i, (VALUE)&memo);
 
814
    return obj;
 
815
}
 
816
 
 
817
static VALUE
 
818
zip_i(val, memo)
 
819
    VALUE val;
 
820
    VALUE *memo;
 
821
{
 
822
    VALUE result = memo[0];
 
823
    VALUE args = memo[1];
 
824
    int idx = memo[2]++;
 
825
    VALUE tmp;
 
826
    int i;
 
827
 
 
828
    tmp = rb_ary_new2(RARRAY(args)->len + 1);
 
829
    rb_ary_store(tmp, 0, val);
 
830
    for (i=0; i<RARRAY(args)->len; i++) {
 
831
        rb_ary_push(tmp, rb_ary_entry(RARRAY(args)->ptr[i], idx));
 
832
    }
 
833
    if (rb_block_given_p()) {
 
834
        rb_yield(tmp);
 
835
    }
 
836
    else {
 
837
        rb_ary_push(result, tmp);
 
838
    }
 
839
    return Qnil;
 
840
}
 
841
 
 
842
/*
 
843
 *  call-seq:
 
844
 *     enum.zip(arg, ...)                   => array
 
845
 *     enum.zip(arg, ...) {|arr| block }    => nil
 
846
 *  
 
847
 *  Converts any arguments to arrays, then merges elements of
 
848
 *  <i>enum</i> with corresponding elements from each argument. This
 
849
 *  generates a sequence of <code>enum#size</code> <em>n</em>-element
 
850
 *  arrays, where <em>n</em> is one more that the count of arguments. If
 
851
 *  the size of any argument is less than <code>enum#size</code>,
 
852
 *  <code>nil</code> values are supplied. If a block given, it is
 
853
 *  invoked for each output array, otherwise an array of arrays is
 
854
 *  returned.
 
855
 *     
 
856
 *     a = [ 4, 5, 6 ]
 
857
 *     b = [ 7, 8, 9 ]
 
858
 *     
 
859
 *     (1..3).zip(a, b)      #=> [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
 
860
 *     "cat\ndog".zip([1])   #=> [["cat\n", 1], ["dog", nil]]
 
861
 *     (1..3).zip            #=> [[1], [2], [3]]
 
862
 *     
 
863
 */
 
864
 
 
865
static VALUE
 
866
enum_zip(argc, argv, obj)
 
867
    int argc;
 
868
    VALUE *argv;
 
869
    VALUE obj;
 
870
{
 
871
    int i;
 
872
    VALUE result;
 
873
    VALUE memo[3];
 
874
 
 
875
    for (i=0; i<argc; i++) {
 
876
        argv[i] = rb_convert_type(argv[i], T_ARRAY, "Array", "to_a");
 
877
    }
 
878
    result = rb_block_given_p() ? Qnil : rb_ary_new();
 
879
    memo[0] = result;
 
880
    memo[1] = rb_ary_new4(argc, argv);
 
881
    memo[2] = 0;
 
882
    rb_iterate(rb_each, obj, zip_i, (VALUE)memo);
 
883
 
 
884
    return result;
 
885
}
 
886
 
 
887
/*
 
888
 *  The <code>Enumerable</code> mixin provides collection classes with
 
889
 *  several traversal and searching methods, and with the ability to
 
890
 *  sort. The class must provide a method <code>each</code>, which
 
891
 *  yields successive members of the collection. If
 
892
 *  <code>Enumerable#max</code>, <code>#min</code>, or
 
893
 *  <code>#sort</code> is used, the objects in the collection must also
 
894
 *  implement a meaningful <code><=></code> operator, as these methods
 
895
 *  rely on an ordering between members of the collection.
 
896
 */
 
897
 
 
898
void
 
899
Init_Enumerable()
 
900
{
 
901
    rb_mEnumerable = rb_define_module("Enumerable");
 
902
 
 
903
    rb_define_method(rb_mEnumerable,"to_a", enum_to_a, 0);
 
904
    rb_define_method(rb_mEnumerable,"entries", enum_to_a, 0);
 
905
 
 
906
    rb_define_method(rb_mEnumerable,"sort", enum_sort, 0);
 
907
    rb_define_method(rb_mEnumerable,"sort_by", enum_sort_by, 0);
 
908
    rb_define_method(rb_mEnumerable,"grep", enum_grep, 1);
 
909
    rb_define_method(rb_mEnumerable,"find", enum_find, -1);
 
910
    rb_define_method(rb_mEnumerable,"detect", enum_find, -1);
 
911
    rb_define_method(rb_mEnumerable,"find_all", enum_find_all, 0);
 
912
    rb_define_method(rb_mEnumerable,"select", enum_find_all, 0);
 
913
    rb_define_method(rb_mEnumerable,"reject", enum_reject, 0);
 
914
    rb_define_method(rb_mEnumerable,"collect", enum_collect, 0);
 
915
    rb_define_method(rb_mEnumerable,"map", enum_collect, 0);
 
916
    rb_define_method(rb_mEnumerable,"inject", enum_inject, -1);
 
917
    rb_define_method(rb_mEnumerable,"partition", enum_partition, 0);
 
918
    rb_define_method(rb_mEnumerable,"all?", enum_all, 0);
 
919
    rb_define_method(rb_mEnumerable,"any?", enum_any, 0);
 
920
    rb_define_method(rb_mEnumerable,"min", enum_min, 0);
 
921
    rb_define_method(rb_mEnumerable,"max", enum_max, 0);
 
922
    rb_define_method(rb_mEnumerable,"member?", enum_member, 1);
 
923
    rb_define_method(rb_mEnumerable,"include?", enum_member, 1);
 
924
    rb_define_method(rb_mEnumerable,"each_with_index", enum_each_with_index, 0);
 
925
    rb_define_method(rb_mEnumerable, "zip", enum_zip, -1);
 
926
 
 
927
    id_eqq  = rb_intern("===");
 
928
    id_each = rb_intern("each");
 
929
    id_cmp  = rb_intern("<=>");
 
930
}
 
931