Adding Enumerator::Lazy#uniq and Enumerator::Lazy#grep_v to proc chaining

[Feature #14994]

[Fix GH-1930]

From: Anmol Chopra <chopraanmol1@gmail.com>

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64385 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2018-08-16 00:58:21 +00:00
parent 2c195f64cf
commit 7d6a26f2a1
7 changed files with 77 additions and 45 deletions

View File

@ -0,0 +1,4 @@
grep_data = (1..10).to_a * 1000
N = 100
enum = grep_data.lazy.grep_v(->(i){i == 0}).grep_v(->(i){i == 0})
N.times {enum.each {}}

View File

@ -0,0 +1,4 @@
grep_data = (1..10).to_a * 1000
N = 100
enum = grep_data.lazy.grep_v(->(i){i > 2}).grep_v(->(i){i > 2})
N.times {enum.each {}}

View File

@ -0,0 +1,4 @@
grep_data = (1..10).to_a * 1000
N = 100
enum = grep_data.lazy.grep_v(->(i){i > 5}).grep_v(->(i){i > 5})
N.times {enum.each {}}

View File

@ -0,0 +1,4 @@
uniq_data = (1..10_000).to_a
N = 100
enum = uniq_data.lazy.uniq {|i| i % 10000}.uniq {|i| i % 10000}
N.times {enum.each {}}

View File

@ -0,0 +1,4 @@
uniq_data = (1..10_000).to_a
N = 100
enum = uniq_data.lazy.uniq {|i| i % 2000}.uniq {|i| i % 2000}
N.times {enum.each {}}

View File

@ -0,0 +1,4 @@
uniq_data = (1..10_000).to_a
N = 100
enum = uniq_data.lazy.uniq {|i| i % 5000}.uniq {|i| i % 5000}
N.times {enum.each {}}

View File

@ -1980,38 +1980,43 @@ lazy_grep(VALUE obj, VALUE pattern)
return lazy_add_method(obj, 0, 0, pattern, rb_ary_new3(1, pattern), funcs); return lazy_add_method(obj, 0, 0, pattern, rb_ary_new3(1, pattern), funcs);
} }
static VALUE static struct MEMO *
lazy_grep_v_func(RB_BLOCK_CALL_FUNC_ARGLIST(val, m)) lazy_grep_v_proc(VALUE proc_entry, struct MEMO *result, VALUE memos, long memo_index)
{ {
VALUE i = rb_enum_values_pack(argc - 1, argv + 1); struct proc_entry *entry = proc_entry_ptr(proc_entry);
VALUE result = rb_funcall(m, id_eqq, 1, i); VALUE chain = rb_funcall(entry->memo, id_eqq, 1, result->memo_value);
if (RTEST(chain)) return 0;
if (!RTEST(result)) { return result;
rb_funcall(argv[0], id_yield, 1, i);
}
return Qnil;
} }
static VALUE static struct MEMO *
lazy_grep_v_iter(RB_BLOCK_CALL_FUNC_ARGLIST(val, m)) lazy_grep_v_iter_proc(VALUE proc_entry, struct MEMO *result, VALUE memos, long memo_index)
{ {
VALUE i = rb_enum_values_pack(argc - 1, argv + 1); struct proc_entry *entry = proc_entry_ptr(proc_entry);
VALUE result = rb_funcall(m, id_eqq, 1, i); VALUE value, chain = rb_funcall(entry->memo, id_eqq, 1, result->memo_value);
if (!RTEST(result)) { if (RTEST(chain)) return 0;
rb_funcall(argv[0], id_yield, 1, rb_yield(i)); value = rb_proc_call_with_block(entry->proc, 1, &(result->memo_value), Qnil);
} LAZY_MEMO_SET_VALUE(result, value);
return Qnil; LAZY_MEMO_RESET_PACKED(result);
return result;
} }
static const lazyenum_funcs lazy_grep_v_iter_funcs = {
lazy_grep_v_iter_proc, 0,
};
static const lazyenum_funcs lazy_grep_v_funcs = {
lazy_grep_v_proc, 0,
};
static VALUE static VALUE
lazy_grep_v(VALUE obj, VALUE pattern) lazy_grep_v(VALUE obj, VALUE pattern)
{ {
return lazy_set_method(rb_block_call(rb_cLazy, id_new, 1, &obj, const lazyenum_funcs *const funcs = rb_block_given_p() ?
rb_block_given_p() ? &lazy_grep_v_iter_funcs : &lazy_grep_v_funcs;
lazy_grep_v_iter : lazy_grep_v_func, return lazy_add_method(obj, 0, 0, pattern, rb_ary_new3(1, pattern), funcs);
pattern),
rb_ary_new3(1, pattern), 0);
} }
static VALUE static VALUE
@ -2275,46 +2280,49 @@ lazy_drop_while(VALUE obj)
return lazy_add_method(obj, 0, 0, Qfalse, Qnil, &lazy_drop_while_funcs); return lazy_add_method(obj, 0, 0, Qfalse, Qnil, &lazy_drop_while_funcs);
} }
static VALUE static int
lazy_uniq_i(VALUE i, int argc, const VALUE *argv, VALUE yielder) lazy_uniq_check(VALUE chain, VALUE memos, long memo_index)
{ {
VALUE hash; VALUE hash = rb_ary_entry(memos, memo_index);;
hash = rb_attr_get(yielder, id_memo);
if (NIL_P(hash)) { if (NIL_P(hash)) {
hash = rb_obj_hide(rb_hash_new()); hash = rb_obj_hide(rb_hash_new());
rb_ivar_set(yielder, id_memo, hash); rb_ary_store(memos, memo_index, hash);
} }
if (rb_hash_add_new_element(hash, i, Qfalse)) return rb_hash_add_new_element(hash, chain, Qfalse);
return Qnil;
return rb_funcallv(yielder, id_yield, argc, argv);
} }
static VALUE static struct MEMO *
lazy_uniq_func(RB_BLOCK_CALL_FUNC_ARGLIST(i, m)) lazy_uniq_proc(VALUE proc_entry, struct MEMO *result, VALUE memos, long memo_index)
{ {
VALUE yielder = (--argc, *argv++); if (lazy_uniq_check(result->memo_value, memos, memo_index)) return 0;
i = rb_enum_values_pack(argc, argv); return result;
return lazy_uniq_i(i, argc, argv, yielder);
} }
static VALUE static struct MEMO *
lazy_uniq_iter(RB_BLOCK_CALL_FUNC_ARGLIST(i, m)) lazy_uniq_iter_proc(VALUE proc_entry, struct MEMO *result, VALUE memos, long memo_index)
{ {
VALUE yielder = (--argc, *argv++); VALUE chain = lazyenum_yield(proc_entry, result);
i = rb_yield_values2(argc, argv);
return lazy_uniq_i(i, argc, argv, yielder); if (lazy_uniq_check(chain, memos, memo_index)) return 0;
return result;
} }
static const lazyenum_funcs lazy_uniq_iter_funcs = {
lazy_uniq_iter_proc, 0,
};
static const lazyenum_funcs lazy_uniq_funcs = {
lazy_uniq_proc, 0,
};
static VALUE static VALUE
lazy_uniq(VALUE obj) lazy_uniq(VALUE obj)
{ {
rb_block_call_func *const func = const lazyenum_funcs *const funcs =
rb_block_given_p() ? lazy_uniq_iter : lazy_uniq_func; rb_block_given_p() ? &lazy_uniq_iter_funcs : &lazy_uniq_funcs;
return lazy_set_method(rb_block_call(rb_cLazy, id_new, 1, &obj, return lazy_add_method(obj, 0, 0, Qnil, Qnil, funcs);
func, 0),
0, 0);
} }
static VALUE static VALUE