Bug #3089

limited size for Array#combination [patch]

Added by marcandre (Marc-Andre Lafortune) about 2 years ago. Updated about 1 year ago.

[ruby-core:29240]
Status:Closed Start date:04/03/2010
Priority:Low Due date:
Assignee:matz (Yukihiro Matsumoto) % Done:

100%

Category:core
Target version:1.9.2
ruby -v:ruby 1.9.2dev (2010-04-03 trunk 27200) [x86_64-darwin10.2.0]

Description

I would expect the following to work, but it raises an error

  (0..100).to_a.combination(50).first(42)
   # => RangeError: too big for combination

There is a limit of 1 << 31 potential results (or 1 <<63, depending on the platform).

Since there is an explicit test for this, I learned the hard way I better ask if there is a valid reason for this artificial limit for #combination ?

If not, and unless there is objection, I'll commit the attached patch.

Note: I noticed this when implementing the block form of #product where a similar limit exists. Since #product without a block needs to return an array of this size, it is sensible that an error is raised in that case.
When an block is given, though, I didn't see why there should be a limit, so the limitation applies only to the form without a block. This does mean, though, that arr.product(...) might not behave the same as arr.to_enum(:product, ...).to_a, at least until Enumerators have a length method (see issue #2673)

combination_unlimited.diff (1.8 kB) marcandre (Marc-Andre Lafortune), 04/04/2010 05:54 am

Associated revisions

Revision 27210
Added by marcandre (Marc-Andre Lafortune) about 2 years ago

* array.c (rb_ary_product): Don't limit based on size when a block is given cf [ruby-core:29240] * test/ruby/test_array.rb (class): Test for above

Revision 27252
Added by marcandre (Marc-Andre Lafortune) about 2 years ago

* array.c (rb_ary_permutation): Remove limitation for lengthy permutations [ruby-core:29240] * test/ruby/test_array.rb: ditto

History

Updated by yugui (Yuki Sonoda) about 2 years ago

  • Assignee set to matz (Yukihiro Matsumoto)
> I learned the hard way I better ask if there is a valid reason for this artificial limit for #combination ?

hmm, matz, is this behavior intended?  If not, I regard the limitation as a bug. It means, The patch can be merged into ruby 1.9.2.

Updated by matz (Yukihiro Matsumoto) about 2 years ago

Hi,

In message "Re: [ruby-core:29241] [Bug #3089] limited size for Array#combination [patch]"
    on Sun, 4 Apr 2010 07:32:44 +0900, Yuki Sonoda <redmine@ruby-lang.org> writes:

|> I learned the hard way I better ask if there is a valid reason for this artificial limit for #combination ?
|
|hmm, matz, is this behavior intended?  If not, I regard the limitation as a bug. It means, The patch can be merged into ruby 1.9.2.

It's not intended, so that the patch can be applied.  Will you apply?

							matz.

Updated by marcandre (Marc-Andre Lafortune) about 2 years ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100
This issue was solved with changeset r27252.
Marc-Andre, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.

Also available in: Atom PDF