Skip to content

HTTPS clone URL

Subversion checkout URL

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