https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112012-05-30T13:14:06ZRuby Issue Tracking SystemRuby master - Feature #6515: array.c: added method that verifies if an Array is part of anotherhttps://redmine.ruby-lang.org/issues/6515?journal_id=269002012-05-30T13:14:06Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>% Done</strong> changed from <i>100</i> to <i>0</i></li></ul><p>Why attaching whole files?</p> Ruby master - Feature #6515: array.c: added method that verifies if an Array is part of anotherhttps://redmine.ruby-lang.org/issues/6515?journal_id=269012012-05-30T14:02:22Zlellisga (Li Ellis Galardo)lellisga@gmail.com
<ul></ul><p>Sorry, I wasn;t sure what to upload. Please remove them.</p>
<p>nobu (Nobuyoshi Nakada) wrote:</p>
<blockquote>
<p>Why attaching whole files?</p>
</blockquote> Ruby master - Feature #6515: array.c: added method that verifies if an Array is part of anotherhttps://redmine.ruby-lang.org/issues/6515?journal_id=269042012-05-30T21:00:07Zzzak (zzak _)
<ul><li><strong>File</strong> <a href="/attachments/2735">feature_6515_array_part_of.patch</a> <a class="icon-only icon-download" title="Download" href="/attachments/download/2735/feature_6515_array_part_of.patch">feature_6515_array_part_of.patch</a> added</li></ul><p>I've added Li's patch from GH#127</p> Ruby master - Feature #6515: array.c: added method that verifies if an Array is part of anotherhttps://redmine.ruby-lang.org/issues/6515?journal_id=269062012-05-30T21:49:50Znaruse (Yui NARUSE)naruse@airemix.jp
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Assigned</i></li><li><strong>Assignee</strong> set to <i>matz (Yukihiro Matsumoto)</i></li></ul> Ruby master - Feature #6515: array.c: added method that verifies if an Array is part of anotherhttps://redmine.ruby-lang.org/issues/6515?journal_id=269072012-05-30T23:32:57Zlellisga (Li Ellis Galardo)lellisga@gmail.com
<ul><li><strong>File</strong> <a href="/attachments/2736">feature_6515_array_part_of_v2.patch</a> <a class="icon-only icon-download" title="Download" href="/attachments/download/2736/feature_6515_array_part_of_v2.patch">feature_6515_array_part_of_v2.patch</a> added</li></ul><p>I'm adding a new patch that fixed a typo. I also did some refactor to the code.</p> Ruby master - Feature #6515: array.c: added method that verifies if an Array is part of anotherhttps://redmine.ruby-lang.org/issues/6515?journal_id=269192012-05-31T23:17:31Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>=begin<br>
I'm not in favor with this proposal, because of its name mainly.<br>
'Array ((|X|)) is part of array ((|Y|))' sounds like 'all elements in ((|X|)) are<br>
arranged in ((|Y|)) successively in the same order', to me.</p>
<p>If the order is not your concern, why it isn't a method of (({Set}))?</p>
<p>Also, (({Array#&})) isn't necessary here.<br>
(ary1 - ary2).empty?<br>
would work enough.<br>
=end</p> Ruby master - Feature #6515: array.c: added method that verifies if an Array is part of anotherhttps://redmine.ruby-lang.org/issues/6515?journal_id=269572012-06-02T03:00:57Zlellisga (Li Ellis Galardo)lellisga@gmail.com
<ul><li><strong>File</strong> <a href="/attachments/2742">feature_6515_array_part_of_v3.patch</a> <a class="icon-only icon-download" title="Download" href="/attachments/download/2742/feature_6515_array_part_of_v3.patch">feature_6515_array_part_of_v3.patch</a> added</li></ul><p><a class="user active user-mention" href="https://redmine.ruby-lang.org/users/4">@nobu (Nobuyoshi Nakada)</a> you are right. I'll change the method to (arry1 - arry2).empty?. About the method functionally I thought of it like the subset method from Set. That's why it doesn't care about the order.</p> Ruby master - Feature #6515: array.c: added method that verifies if an Array is part of anotherhttps://redmine.ruby-lang.org/issues/6515?journal_id=269692012-06-02T09:50:17Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Assigned</i> to <i>Feedback</i></li></ul><p>=begin<br>
Then why don't you use (({Set#subset?})) method?</p>
<p>What's the rationale that (({Array})) also should have similar method?<br>
And why different name?<br>
=end</p> Ruby master - Feature #6515: array.c: added method that verifies if an Array is part of anotherhttps://redmine.ruby-lang.org/issues/6515?journal_id=270032012-06-04T14:36:40Zlellisga (Li Ellis Galardo)lellisga@gmail.com
<ul></ul><p>Sorry but i'm not sure what you are asking. In other words you really think it's easier to do this:</p>
<p>a = [1,2,3,4,5,6,7].to_set<br>
=> #<Set: {1, 2, 3, 4, 5, 6, 7}><br>
b = [1,2,3].to_set<br>
=> #<Set: {1, 2, 3}><br>
b.subset? a<br>
=> true</p>
<p>If you answer is yes, then we are doing this wrong because subset? or part_of method should be in a parent class (maybe Enumerable class ) in order for it to work for subset, array, hash and any data structure that inherit from it Enumerable.</p>
<p>There's a difference between subset and arrays isn't it? they should have different methods, eventually some of them will work for subset just as they work for arrays.</p> Ruby master - Feature #6515: array.c: added method that verifies if an Array is part of anotherhttps://redmine.ruby-lang.org/issues/6515?journal_id=270062012-06-04T17:35:03ZEregon (Benoit Daloze)
<ul></ul><p>lellisga (Li Ellis Galardo) wrote:</p>
<blockquote>
<p>a = [1,2,3,4,5,6,7].to_set<br>
=> #<Set: {1, 2, 3, 4, 5, 6, 7}><br>
b = [1,2,3].to_set<br>
=> #<Set: {1, 2, 3}><br>
b.subset? a<br>
=> true</p>
<p>If you answer is yes, then we are doing this wrong because subset? or part_of method should be in a parent class (maybe Enumerable class ) in order for it to work for subset, array, hash and any data structure that inherit from it Enumerable.</p>
</blockquote>
<p>Taking you example of a the first set and b the maybe-subset:<br>
Well, probably because #subset? is O(m) (m is the size of b), and doing it naively on Array/Enumerable is O(m*n) (n being the size of a). Using (b-a).empty? is actually O(n+m) because it creates a hash (think set in this case) underneath and iterates b. Note Array#- mentions the Set library, because it's likely what you want if you need this kind of operations. Of course you might need both Array and Set operations, in which case you may:</p>
<ul>
<li>
<p>transform them to set when needed: which is O(n+m + m) so O(n+m).</p>
</li>
<li>
<p>use (a-b).empty? which I believe is clear and concise (although a comment might help for other readers) but creates the diff Array for nothing. You might want something like:</p>
<p>as = a.to_set<br>
b.all? { |e| as.include?(e) }</p>
</li>
</ul>
<p>Which does not allocate an intermediary Array, and is O(n+m) as well.</p>
<p>Often, when you want this kind of behavior, you don't need Array-specific operations, which is why using a Set directly is better (and usually more efficient).</p> Ruby master - Feature #6515: array.c: added method that verifies if an Array is part of anotherhttps://redmine.ruby-lang.org/issues/6515?journal_id=270072012-06-04T17:37:46ZEregon (Benoit Daloze)
<ul></ul><p>Eregon (Benoit Daloze) wrote:</p>
<blockquote>
<p>as = a.to_set<br>
b.all? { |e| as.include?(e) }</p>
<p>Which does not allocate an intermediary Array, and is O(n+m) as well.</p>
</blockquote>
<p>But does allocate the Set for it of course (so likely not better).</p> Ruby master - Feature #6515: array.c: added method that verifies if an Array is part of anotherhttps://redmine.ruby-lang.org/issues/6515?journal_id=333082012-11-20T22:57:06Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul><li><strong>Target version</strong> changed from <i>2.0.0</i> to <i>2.6</i></li></ul> Ruby master - Feature #6515: array.c: added method that verifies if an Array is part of anotherhttps://redmine.ruby-lang.org/issues/6515?journal_id=687872017-12-25T18:15:05Znaruse (Yui NARUSE)naruse@airemix.jp
<ul><li><strong>Target version</strong> deleted (<del><i>2.6</i></del>)</li></ul>