* enumerator.c (ary2sv): add dup argument.

(enumerator_next): call ary2sv with dup=0.
  (enumerator_peek): call ary2sv with dup=1 to return duplicated array.
  (enumerator_peek_values_m): new function to return duplicated array.
  (Init_Enumerator): use enumerator_peek_values_m as
  Enumerator#peek_value.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@24619 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2009-08-21 15:51:35 +00:00
Родитель 7609494ffd
Коммит 461bd62fec
3 изменённых файлов: 50 добавлений и 11 удалений

Просмотреть файл

@ -1,3 +1,12 @@
Sat Aug 22 00:48:08 2009 Tanaka Akira <akr@fsij.org>
* enumerator.c (ary2sv): add dup argument.
(enumerator_next): call ary2sv with dup=0.
(enumerator_peek): call ary2sv with dup=1 to return duplicated array.
(enumerator_peek_values_m): new function to return duplicated array.
(Init_Enumerator): use enumerator_peek_values_m as
Enumerator#peek_value.
Sat Aug 22 00:03:19 2009 Yusuke Endoh <mame@tsg.ne.jp>
* thread.c (rb_check_deadlock): decrease number of sleepers before

Просмотреть файл

@ -680,7 +680,7 @@ enumerator_next_values(VALUE obj)
}
static VALUE
ary2sv(VALUE args)
ary2sv(VALUE args, int dup)
{
if (TYPE(args) != T_ARRAY)
return args;
@ -693,6 +693,8 @@ ary2sv(VALUE args)
return RARRAY_PTR(args)[0];
default:
if (dup)
return rb_ary_dup(args);
return args;
}
}
@ -715,7 +717,18 @@ static VALUE
enumerator_next(VALUE obj)
{
VALUE vs = enumerator_next_values(obj);
return ary2sv(vs);
return ary2sv(vs, 0);
}
static VALUE
enumerator_peek_values(VALUE obj)
{
struct enumerator *e = enumerator_ptr(obj);
if (e->lookahead == Qundef) {
e->lookahead = get_next_values(obj, e);
}
return e->lookahead;
}
/*
@ -744,14 +757,9 @@ enumerator_next(VALUE obj)
*/
static VALUE
enumerator_peek_values(VALUE obj)
enumerator_peek_values_m(VALUE obj)
{
struct enumerator *e = enumerator_ptr(obj);
if (e->lookahead == Qundef) {
e->lookahead = get_next_values(obj, e);
}
return e->lookahead;
return rb_ary_dup(enumerator_peek_values(obj));
}
/*
@ -768,7 +776,7 @@ static VALUE
enumerator_peek(VALUE obj)
{
VALUE vs = enumerator_peek_values(obj);
return ary2sv(vs);
return ary2sv(vs, 1);
}
/*
@ -1153,7 +1161,7 @@ Init_Enumerator(void)
rb_define_method(rb_cEnumerator, "with_index", enumerator_with_index, -1);
rb_define_method(rb_cEnumerator, "with_object", enumerator_with_object, 1);
rb_define_method(rb_cEnumerator, "next_values", enumerator_next_values, 0);
rb_define_method(rb_cEnumerator, "peek_values", enumerator_peek_values, 0);
rb_define_method(rb_cEnumerator, "peek_values", enumerator_peek_values_m, 0);
rb_define_method(rb_cEnumerator, "next", enumerator_next, 0);
rb_define_method(rb_cEnumerator, "peek", enumerator_peek, 0);
rb_define_method(rb_cEnumerator, "feed", enumerator_feed, 1);

Просмотреть файл

@ -141,6 +141,28 @@ class TestEnumerator < Test::Unit::TestCase
assert_raise(StopIteration) { e.peek }
end
def test_peek_modify
o = Object.new
def o.each
yield 1,2
end
e = o.to_enum
a = e.peek
a << 3
assert_equal([1,2], e.peek)
end
def test_peek_values_modify
o = Object.new
def o.each
yield 1,2
end
e = o.to_enum
a = e.peek_values
a << 3
assert_equal([1,2], e.peek)
end
def test_next_after_stopiteration
a = [1]
e = a.each