mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Add Set#intersect? and #disjoint?.
* lib/set.rb (Set#intersect?, Set#disjoint?): Add new methods for testing if two sets have any element in common. [ruby-core:45641] [Feature #6588] Based on the code by marcandre. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42253 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
2e0fc4b21e
commit
bd304ed85b
4 changed files with 68 additions and 0 deletions
|
@ -1,3 +1,9 @@
|
|||
Tue Jul 30 18:52:27 2013 Akinori MUSHA <knu@iDaemons.org>
|
||||
|
||||
* lib/set.rb (Set#intersect?, Set#disjoint?): Add new methods for
|
||||
testing if two sets have any element in common.
|
||||
[ruby-core:45641] [Feature #6588] Based on the code by marcandre.
|
||||
|
||||
Tue Jul 30 17:16:15 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||
|
||||
* sprintf.c (ruby__sfvextra): add QUOTE flag to escape unprintable
|
||||
|
|
5
NEWS
5
NEWS
|
@ -141,6 +141,11 @@ with all sufficient information, see the ChangeLog file.
|
|||
* REXML::Text#<< supports method chain like 'text << "XXX" << "YYY"'.
|
||||
* REXML::Text#<< supports not "raw" mode.
|
||||
|
||||
* Set
|
||||
* New methods:
|
||||
* Set#intersect?
|
||||
* Set#disjoint?
|
||||
|
||||
=== Stdlib compatibility issues (excluding feature bug fixes)
|
||||
|
||||
* Set
|
||||
|
|
17
lib/set.rb
17
lib/set.rb
|
@ -231,6 +231,23 @@ class Set
|
|||
end
|
||||
alias < proper_subset?
|
||||
|
||||
# Returns true if the set and the given set have at least one
|
||||
# element in common.
|
||||
def intersect?(set)
|
||||
set.is_a?(Set) or raise ArgumentError, "value must be a set"
|
||||
if size < set.size
|
||||
any? { |o| set.include?(o) }
|
||||
else
|
||||
set.any? { |o| include?(o) }
|
||||
end
|
||||
end
|
||||
|
||||
# Returns true if the set and the given set have no element in
|
||||
# common. This method is the opposite of +intersect?+.
|
||||
def disjoint?(set)
|
||||
!intersect?(set)
|
||||
end
|
||||
|
||||
# Calls the given block once for each element in the set, passing
|
||||
# the element as parameter. Returns an enumerator if no block is
|
||||
# given.
|
||||
|
|
|
@ -297,6 +297,46 @@ class TC_Set < Test::Unit::TestCase
|
|||
assert_equal(false, Set[].proper_subset?(Set[]))
|
||||
end
|
||||
|
||||
def assert_intersect(expected, set, other)
|
||||
case expected
|
||||
when true
|
||||
assert_send([set, :intersect?, other])
|
||||
assert_send([other, :intersect?, set])
|
||||
assert_not_send([set, :disjoint?, other])
|
||||
assert_not_send([other, :disjoint?, set])
|
||||
when false
|
||||
assert_not_send([set, :intersect?, other])
|
||||
assert_not_send([other, :intersect?, set])
|
||||
assert_send([set, :disjoint?, other])
|
||||
assert_send([other, :disjoint?, set])
|
||||
when Class
|
||||
assert_raises(expected) {
|
||||
set.intersect?(other)
|
||||
}
|
||||
assert_raises(expected) {
|
||||
set.disjoint?(other)
|
||||
}
|
||||
else
|
||||
raise ArgumentError, "%s: unsupported expected value: %s" % [__method__, expected.inspect]
|
||||
end
|
||||
end
|
||||
|
||||
def test_intersect?
|
||||
set = Set[3,4,5]
|
||||
|
||||
assert_intersect(ArgumentError, set, 3)
|
||||
assert_intersect(ArgumentError, set, [2,4,6])
|
||||
|
||||
assert_intersect(true, set, Set[2,4])
|
||||
assert_intersect(true, set, Set[5,6,7])
|
||||
assert_intersect(true, set, Set[1,2,6,8,4])
|
||||
|
||||
assert_intersect(false, set, Set[])
|
||||
assert_intersect(false, set, Set[0,2])
|
||||
assert_intersect(false, set, Set[0,2,6])
|
||||
assert_intersect(false, set, Set[0,2,6,8,10])
|
||||
end
|
||||
|
||||
def test_each
|
||||
ary = [1,3,5,7,10,20]
|
||||
set = Set.new(ary)
|
||||
|
|
Loading…
Reference in a new issue