Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 451 lines (387 sloc) 9.837 kb
cde1e57a »
2008-04-09 * enumerator.c, inits.c (rb_call_inits), ruby.h, intern.h,
1 /************************************************
2
3 enumerator.c - provides Enumerator class
4
5 $Author$
6
7 Copyright (C) 2001-2003 Akinori MUSHA
8
9 $Idaemons: /home/cvs/rb/enumerator/enumerator.c,v 1.1.1.1 2001/07/15 10:12:48 knu Exp $
10 $RoughId: enumerator.c,v 1.6 2003/07/27 11:03:24 nobu Exp $
11 $Id$
12
13 ************************************************/
14
15 #include "ruby.h"
16
17 /*
18 * Document-class: Enumerable::Enumerator
19 *
20 * A class which provides a method `each' to be used as an Enumerable
21 * object.
22 */
23 VALUE rb_cEnumerator;
ce1089ab »
2008-04-20 * enumerator.c: Resolve the method every time an enumeration
24 static VALUE sym_each;
cde1e57a »
2008-04-09 * enumerator.c, inits.c (rb_call_inits), ruby.h, intern.h,
25
8b854871 »
2008-04-10 * enumerator.c (rb_eStopIteration), eval.c (rb_f_loop), ruby.h:
26 VALUE rb_eStopIteration;
27
cde1e57a »
2008-04-09 * enumerator.c, inits.c (rb_call_inits), ruby.h, intern.h,
28 struct enumerator {
ce1089ab »
2008-04-20 * enumerator.c: Resolve the method every time an enumeration
29 VALUE obj;
10899ae9 »
2008-04-21 Merge from ruby_1_8.
30 ID meth;
cde1e57a »
2008-04-09 * enumerator.c, inits.c (rb_call_inits), ruby.h, intern.h,
31 VALUE proc;
32 VALUE args;
33 rb_block_call_func *iter;
34 };
35
69d59629 »
2008-04-14 * enumerator.c (enumerator_mark, enumerator_iter_i, enumerator_each_i,
36 static void enumerator_mark _((void *));
cde1e57a »
2008-04-09 * enumerator.c, inits.c (rb_call_inits), ruby.h, intern.h,
37 static void
38 enumerator_mark(p)
39 void *p;
40 {
41 struct enumerator *ptr = p;
ce1089ab »
2008-04-20 * enumerator.c: Resolve the method every time an enumeration
42 rb_gc_mark(ptr->obj);
cde1e57a »
2008-04-09 * enumerator.c, inits.c (rb_call_inits), ruby.h, intern.h,
43 rb_gc_mark(ptr->args);
44 }
45
46 static struct enumerator *
47 enumerator_ptr(obj)
48 VALUE obj;
49 {
50 struct enumerator *ptr;
51
52 Data_Get_Struct(obj, struct enumerator, ptr);
53 if (RDATA(obj)->dmark != enumerator_mark) {
54 rb_raise(rb_eTypeError,
55 "wrong argument type %s (expected Enumerable::Enumerator)",
56 rb_obj_classname(obj));
57 }
a96b4573 »
2008-06-03 * enumerator.c (enumerator_allocate, enumerator_ptr): Properly
58 if (!ptr || ptr->obj == Qundef) {
cde1e57a »
2008-04-09 * enumerator.c, inits.c (rb_call_inits), ruby.h, intern.h,
59 rb_raise(rb_eArgError, "uninitialized enumerator");
60 }
61 return ptr;
62 }
63
64 /*
65 * call-seq:
66 * obj.to_enum(method = :each, *args)
67 * obj.enum_for(method = :each, *args)
68 *
69 * Returns Enumerable::Enumerator.new(self, method, *args).
70 *
71 * e.g.:
72 *
73 * str = "xyz"
74 *
75 * enum = str.enum_for(:each_byte)
76 * a = enum.map {|b| '%02x' % b } #=> ["78", "79", "7a"]
77 *
78 * # protects an array from being modified
79 * a = [1, 2, 3]
80 * some_method(a.to_enum)
81 *
82 */
83 static VALUE
84 obj_to_enum(argc, argv, obj)
85 int argc;
86 VALUE *argv;
87 VALUE obj;
88 {
89 VALUE meth = sym_each;
90
91 if (argc > 0) {
92 --argc;
93 meth = *argv++;
94 }
95 return rb_enumeratorize(obj, meth, argc, argv);
96 }
97
98 static VALUE
99 each_slice_i(val, memo)
100 VALUE val;
101 VALUE *memo;
102 {
103 VALUE ary = memo[0];
104 VALUE v = Qnil;
105 long size = (long)memo[1];
106
107 rb_ary_push(ary, val);
108
109 if (RARRAY_LEN(ary) == size) {
110 v = rb_yield(ary);
111 memo[0] = rb_ary_new2(size);
112 }
113
114 return v;
115 }
116
117 /*
118 * call-seq:
119 * e.each_slice(n) {...}
aa2c73f0 »
2008-05-18 Merge -r16241:16456 from ruby_1_8.
120 * e.each_slice(n)
cde1e57a »
2008-04-09 * enumerator.c, inits.c (rb_call_inits), ruby.h, intern.h,
121 *
aa2c73f0 »
2008-05-18 Merge -r16241:16456 from ruby_1_8.
122 * Iterates the given block for each slice of <n> elements. If no
123 * block is given, returns an enumerator.
cde1e57a »
2008-04-09 * enumerator.c, inits.c (rb_call_inits), ruby.h, intern.h,
124 *
125 * e.g.:
126 * (1..10).each_slice(3) {|a| p a}
127 * # outputs below
128 * [1, 2, 3]
129 * [4, 5, 6]
130 * [7, 8, 9]
131 * [10]
132 *
133 */
134 static VALUE
135 enum_each_slice(obj, n)
136 VALUE obj, n;
137 {
138 long size = NUM2LONG(n);
139 VALUE args[2], ary;
140
141 if (size <= 0) rb_raise(rb_eArgError, "invalid slice size");
142 RETURN_ENUMERATOR(obj, 1, &n);
143 args[0] = rb_ary_new2(size);
144 args[1] = (VALUE)size;
145
146 rb_block_call(obj, SYM2ID(sym_each), 0, 0, each_slice_i, (VALUE)args);
147
148 ary = args[0];
149 if (RARRAY_LEN(ary) > 0) rb_yield(ary);
150
151 return Qnil;
152 }
153
154 static VALUE
155 each_cons_i(val, memo)
156 VALUE val;
157 VALUE *memo;
158 {
159 VALUE ary = memo[0];
160 VALUE v = Qnil;
161 long size = (long)memo[1];
162
163 if (RARRAY_LEN(ary) == size) {
164 rb_ary_shift(ary);
165 }
166 rb_ary_push(ary, val);
167 if (RARRAY_LEN(ary) == size) {
168 v = rb_yield(rb_ary_dup(ary));
169 }
170 return v;
171 }
172
173 /*
174 * call-seq:
175 * each_cons(n) {...}
aa2c73f0 »
2008-05-18 Merge -r16241:16456 from ruby_1_8.
176 * each_cons(n)
cde1e57a »
2008-04-09 * enumerator.c, inits.c (rb_call_inits), ruby.h, intern.h,
177 *
178 * Iterates the given block for each array of consecutive <n>
aa2c73f0 »
2008-05-18 Merge -r16241:16456 from ruby_1_8.
179 * elements. If no block is given, returns an enumerator.a
cde1e57a »
2008-04-09 * enumerator.c, inits.c (rb_call_inits), ruby.h, intern.h,
180 *
181 * e.g.:
182 * (1..10).each_cons(3) {|a| p a}
183 * # outputs below
184 * [1, 2, 3]
185 * [2, 3, 4]
186 * [3, 4, 5]
187 * [4, 5, 6]
188 * [5, 6, 7]
189 * [6, 7, 8]
190 * [7, 8, 9]
191 * [8, 9, 10]
192 *
193 */
194 static VALUE
195 enum_each_cons(obj, n)
196 VALUE obj, n;
197 {
198 long size = NUM2LONG(n);
199 VALUE args[2];
200
201 if (size <= 0) rb_raise(rb_eArgError, "invalid size");
202 RETURN_ENUMERATOR(obj, 1, &n);
203 args[0] = rb_ary_new2(size);
204 args[1] = (VALUE)size;
205
206 rb_block_call(obj, SYM2ID(sym_each), 0, 0, each_cons_i, (VALUE)args);
207
208 return Qnil;
209 }
210
69d59629 »
2008-04-14 * enumerator.c (enumerator_mark, enumerator_iter_i, enumerator_each_i,
211 static VALUE enumerator_allocate _((VALUE));
cde1e57a »
2008-04-09 * enumerator.c, inits.c (rb_call_inits), ruby.h, intern.h,
212 static VALUE
213 enumerator_allocate(klass)
214 VALUE klass;
215 {
216 struct enumerator *ptr;
a96b4573 »
2008-06-03 * enumerator.c (enumerator_allocate, enumerator_ptr): Properly
217 VALUE enum_obj;
218
219 enum_obj = Data_Make_Struct(klass, struct enumerator,
220 enumerator_mark, -1, ptr);
221 ptr->obj = Qundef;
222
223 return enum_obj;
cde1e57a »
2008-04-09 * enumerator.c, inits.c (rb_call_inits), ruby.h, intern.h,
224 }
225
7c65674c »
2008-04-14 * ruby.h (rb_block_call_func): Fix prototype.
226 static VALUE enumerator_each_i _((VALUE, VALUE));
cde1e57a »
2008-04-09 * enumerator.c, inits.c (rb_call_inits), ruby.h, intern.h,
227 static VALUE
7c65674c »
2008-04-14 * ruby.h (rb_block_call_func): Fix prototype.
228 enumerator_each_i(v, enum_obj)
cde1e57a »
2008-04-09 * enumerator.c, inits.c (rb_call_inits), ruby.h, intern.h,
229 VALUE v;
230 VALUE enum_obj;
231 {
232 return rb_yield(v);
233 }
234
235 static VALUE
236 enumerator_init(enum_obj, obj, meth, argc, argv)
237 VALUE enum_obj;
238 VALUE obj;
239 VALUE meth;
240 int argc;
241 VALUE *argv;
242 {
a96b4573 »
2008-06-03 * enumerator.c (enumerator_allocate, enumerator_ptr): Properly
243 struct enumerator *ptr;
244
245 Data_Get_Struct(enum_obj, struct enumerator, ptr);
246
247 if (!ptr) {
248 rb_raise(rb_eArgError, "unallocated enumerator");
249 }
cde1e57a »
2008-04-09 * enumerator.c, inits.c (rb_call_inits), ruby.h, intern.h,
250
ce1089ab »
2008-04-20 * enumerator.c: Resolve the method every time an enumeration
251 ptr->obj = obj;
10899ae9 »
2008-04-21 Merge from ruby_1_8.
252 ptr->meth = rb_to_id(meth);
64ad39d5 »
2008-04-22 Merge from ruby_1_8.
253 ptr->iter = enumerator_each_i;
cde1e57a »
2008-04-09 * enumerator.c, inits.c (rb_call_inits), ruby.h, intern.h,
254 if (argc) ptr->args = rb_ary_new4(argc, argv);
255
256 return enum_obj;
257 }
258
259 /*
260 * call-seq:
261 * Enumerable::Enumerator.new(obj, method = :each, *args)
262 *
263 * Creates a new Enumerable::Enumerator object, which is to be
264 * used as an Enumerable object using the given object's given
265 * method with the given arguments.
266 *
a96b4573 »
2008-06-03 * enumerator.c (enumerator_allocate, enumerator_ptr): Properly
267 * Use of this method is discouraged. Use Kernel#enum_for() instead.
cde1e57a »
2008-04-09 * enumerator.c, inits.c (rb_call_inits), ruby.h, intern.h,
268 */
269 static VALUE
270 enumerator_initialize(argc, argv, obj)
271 int argc;
272 VALUE *argv;
273 VALUE obj;
274 {
275 VALUE recv, meth = sym_each;
276
277 if (argc == 0)
278 rb_raise(rb_eArgError, "wrong number of argument (0 for 1)");
279 recv = *argv++;
280 if (--argc) {
281 meth = *argv++;
282 --argc;
283 }
284 return enumerator_init(obj, recv, meth, argc, argv);
285 }
286
287 /* :nodoc: */
288 static VALUE
289 enumerator_init_copy(obj, orig)
290 VALUE obj;
291 VALUE orig;
292 {
293 struct enumerator *ptr0, *ptr1;
294
295 ptr0 = enumerator_ptr(orig);
51c2ae29 »
2008-06-03 * enumerator.c (enumerator_init_copy): Take care of
296
297 Data_Get_Struct(obj, struct enumerator, ptr1);
298
299 if (!ptr1) {
300 rb_raise(rb_eArgError, "unallocated enumerator");
301 }
cde1e57a »
2008-04-09 * enumerator.c, inits.c (rb_call_inits), ruby.h, intern.h,
302
ce1089ab »
2008-04-20 * enumerator.c: Resolve the method every time an enumeration
303 ptr1->obj = ptr0->obj;
304 ptr1->meth = ptr0->meth;
cde1e57a »
2008-04-09 * enumerator.c, inits.c (rb_call_inits), ruby.h, intern.h,
305 ptr1->iter = ptr0->iter;
306 ptr1->args = ptr0->args;
307
308 return obj;
309 }
310
311 VALUE
312 rb_enumeratorize(obj, meth, argc, argv)
313 VALUE obj;
314 VALUE meth;
315 int argc;
316 VALUE *argv;
317 {
318 return enumerator_init(enumerator_allocate(rb_cEnumerator), obj, meth, argc, argv);
319 }
320
321 /*
322 * call-seq:
323 * enum.each {...}
324 *
325 * Iterates the given block using the object and the method specified
aa2c73f0 »
2008-05-18 Merge -r16241:16456 from ruby_1_8.
326 * in the first place. If no block is given, returns self.
cde1e57a »
2008-04-09 * enumerator.c, inits.c (rb_call_inits), ruby.h, intern.h,
327 *
328 */
329 static VALUE
330 enumerator_each(obj)
331 VALUE obj;
332 {
333 struct enumerator *e;
334 int argc = 0;
335 VALUE *argv = 0;
336
337 if (!rb_block_given_p()) return obj;
338 e = enumerator_ptr(obj);
339 if (e->args) {
340 argc = RARRAY_LEN(e->args);
341 argv = RARRAY_PTR(e->args);
342 }
10899ae9 »
2008-04-21 Merge from ruby_1_8.
343 return rb_block_call(e->obj, e->meth, argc, argv, e->iter, (VALUE)e);
cde1e57a »
2008-04-09 * enumerator.c, inits.c (rb_call_inits), ruby.h, intern.h,
344 }
345
346 static VALUE
347 enumerator_with_index_i(val, memo)
348 VALUE val;
349 VALUE *memo;
350 {
351 val = rb_yield_values(2, val, INT2FIX(*memo));
352 ++*memo;
353 return val;
354 }
355
356 /*
357 * call-seq:
358 * e.with_index {|(*args), idx| ... }
aa2c73f0 »
2008-05-18 Merge -r16241:16456 from ruby_1_8.
359 * e.with_index
cde1e57a »
2008-04-09 * enumerator.c, inits.c (rb_call_inits), ruby.h, intern.h,
360 *
361 * Iterates the given block for each elements with an index, which
aa2c73f0 »
2008-05-18 Merge -r16241:16456 from ruby_1_8.
362 * start from 0. If no block is given, returns an enumerator.
cde1e57a »
2008-04-09 * enumerator.c, inits.c (rb_call_inits), ruby.h, intern.h,
363 *
364 */
365 static VALUE
366 enumerator_with_index(obj)
367 VALUE obj;
368 {
369 struct enumerator *e = enumerator_ptr(obj);
370 VALUE memo = 0;
371 int argc = 0;
372 VALUE *argv = 0;
373
374 RETURN_ENUMERATOR(obj, 0, 0);
375 if (e->args) {
376 argc = RARRAY_LEN(e->args);
377 argv = RARRAY_PTR(e->args);
378 }
10899ae9 »
2008-04-21 Merge from ruby_1_8.
379 return rb_block_call(e->obj, e->meth, argc, argv,
cde1e57a »
2008-04-09 * enumerator.c, inits.c (rb_call_inits), ruby.h, intern.h,
380 enumerator_with_index_i, (VALUE)&memo);
381 }
382
8b854871 »
2008-04-10 * enumerator.c (rb_eStopIteration), eval.c (rb_f_loop), ruby.h:
383 /*
384 * call-seq:
385 * e.next => object
386 *
387 * Returns the next object in the enumerator, and move the internal
388 * position forward. When the position reached at the end, internal
389 * position is rewinded then StopIteration is raised.
390 *
391 * Note that enumeration sequence by next method does not affect other
392 * non-external enumeration methods, unless underlying iteration
393 * methods itself has side-effect, e.g. IO#each_line.
394 *
395 * Caution: Calling this method causes the "generator" library to be
396 * loaded.
397 */
398
399 static VALUE
400 enumerator_next(obj)
401 VALUE obj;
402 {
403 rb_require("generator");
404 return rb_funcall(obj, rb_intern("next"), 0, 0);
405 }
406
407 /*
408 * call-seq:
409 * e.rewind => e
410 *
411 * Rewinds the enumeration sequence by the next method.
412 */
413
414 static VALUE
415 enumerator_rewind(obj)
416 VALUE obj;
417 {
418 rb_require("generator");
419 return rb_funcall(obj, rb_intern("rewind"), 0, 0);
420 }
421
cde1e57a »
2008-04-09 * enumerator.c, inits.c (rb_call_inits), ruby.h, intern.h,
422 void
423 Init_Enumerator()
424 {
425 rb_define_method(rb_mKernel, "to_enum", obj_to_enum, -1);
426 rb_define_method(rb_mKernel, "enum_for", obj_to_enum, -1);
427
428 rb_define_method(rb_mEnumerable, "each_slice", enum_each_slice, 1);
429 rb_define_method(rb_mEnumerable, "enum_slice", enum_each_slice, 1);
430 rb_define_method(rb_mEnumerable, "each_cons", enum_each_cons, 1);
431 rb_define_method(rb_mEnumerable, "enum_cons", enum_each_cons, 1);
432
433 rb_cEnumerator = rb_define_class_under(rb_mEnumerable, "Enumerator", rb_cObject);
434 rb_include_module(rb_cEnumerator, rb_mEnumerable);
435
436 rb_define_alloc_func(rb_cEnumerator, enumerator_allocate);
437 rb_define_method(rb_cEnumerator, "initialize", enumerator_initialize, -1);
438 rb_define_method(rb_cEnumerator, "initialize_copy", enumerator_init_copy, 1);
439 rb_define_method(rb_cEnumerator, "each", enumerator_each, 0);
81b81c77 »
2008-05-24 Merge from ruby_1_8.
440 rb_define_method(rb_cEnumerator, "each_with_index", enumerator_with_index, 0);
cde1e57a »
2008-04-09 * enumerator.c, inits.c (rb_call_inits), ruby.h, intern.h,
441 rb_define_method(rb_cEnumerator, "with_index", enumerator_with_index, 0);
8b854871 »
2008-04-10 * enumerator.c (rb_eStopIteration), eval.c (rb_f_loop), ruby.h:
442 rb_define_method(rb_cEnumerator, "next", enumerator_next, 0);
443 rb_define_method(rb_cEnumerator, "rewind", enumerator_rewind, 0);
444
445 rb_eStopIteration = rb_define_class("StopIteration", rb_eIndexError);
cde1e57a »
2008-04-09 * enumerator.c, inits.c (rb_call_inits), ruby.h, intern.h,
446
ce1089ab »
2008-04-20 * enumerator.c: Resolve the method every time an enumeration
447 sym_each = ID2SYM(rb_intern("each"));
cde1e57a »
2008-04-09 * enumerator.c, inits.c (rb_call_inits), ruby.h, intern.h,
448
449 rb_provide("enumerator.so"); /* for backward compatibility */
450 }
Something went wrong with that request. Please try again.