1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* lib/set.rb (#>=, #>, #<=, #<): Make use of Hash#>=, #>, #<, and

#<= when comparing against an instance of the same kind.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52586 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
knu 2015-11-16 06:43:43 +00:00
parent 8fef6aa20b
commit d736eded9d
3 changed files with 79 additions and 43 deletions

View file

@ -1,3 +1,8 @@
Mon Nov 16 15:42:36 2015 Akinori MUSHA <knu@iDaemons.org>
* lib/set.rb (#>=, #>, #<=, #<): Make use of Hash#>=, #>, #<, and
#<= when comparing against an instance of the same kind.
Mon Nov 16 15:33:11 2015 SHIBATA Hiroshi <hsbt@ruby-lang.org>
* tool/rbinstall.rb: fix wrong permission for gem specification without

View file

@ -220,33 +220,53 @@ class Set
# Returns true if the set is a superset of the given set.
def superset?(set)
set.is_a?(Set) or raise ArgumentError, "value must be a set"
return false if size < set.size
set.all? { |o| include?(o) }
case
when set.instance_of?(self.class)
@hash >= set.instance_variable_get(:@hash)
when set.is_a?(Set)
size >= set.size && set.all? { |o| include?(o) }
else
raise ArgumentError, "value must be a set"
end
end
alias >= superset?
# Returns true if the set is a proper superset of the given set.
def proper_superset?(set)
set.is_a?(Set) or raise ArgumentError, "value must be a set"
return false if size <= set.size
set.all? { |o| include?(o) }
case
when set.instance_of?(self.class)
@hash > set.instance_variable_get(:@hash)
when set.is_a?(Set)
size > set.size && set.all? { |o| include?(o) }
else
raise ArgumentError, "value must be a set"
end
end
alias > proper_superset?
# Returns true if the set is a subset of the given set.
def subset?(set)
set.is_a?(Set) or raise ArgumentError, "value must be a set"
return false if set.size < size
all? { |o| set.include?(o) }
case
when set.instance_of?(self.class)
@hash <= set.instance_variable_get(:@hash)
when set.is_a?(Set)
size <= set.size && all? { |o| set.include?(o) }
else
raise ArgumentError, "value must be a set"
end
end
alias <= subset?
# Returns true if the set is a proper subset of the given set.
def proper_subset?(set)
set.is_a?(Set) or raise ArgumentError, "value must be a set"
return false if set.size <= size
all? { |o| set.include?(o) }
case
when set.instance_of?(self.class)
@hash < set.instance_variable_get(:@hash)
when set.is_a?(Set)
size < set.size && all? { |o| set.include?(o) }
else
raise ArgumentError, "value must be a set"
end
end
alias < proper_subset?

View file

@ -2,6 +2,9 @@ require 'test/unit'
require 'set'
class TC_Set < Test::Unit::TestCase
class Set2 < Set
end
def test_aref
assert_nothing_raised {
Set[]
@ -211,16 +214,18 @@ class TC_Set < Test::Unit::TestCase
set.superset?([2])
}
assert_equal(true, set.superset?(Set[]))
assert_equal(true, set.superset?(Set[1,2]))
assert_equal(true, set.superset?(Set[1,2,3]))
assert_equal(false, set.superset?(Set[1,2,3,4]))
assert_equal(false, set.superset?(Set[1,4]))
[Set, Set2].each { |klass|
assert_equal(true, set.superset?(klass[]), klass.name)
assert_equal(true, set.superset?(klass[1,2]), klass.name)
assert_equal(true, set.superset?(klass[1,2,3]), klass.name)
assert_equal(false, set.superset?(klass[1,2,3,4]), klass.name)
assert_equal(false, set.superset?(klass[1,4]), klass.name)
assert_equal(true, set >= Set[1,2,3])
assert_equal(true, set >= Set[1,2])
assert_equal(true, set >= klass[1,2,3], klass.name)
assert_equal(true, set >= klass[1,2], klass.name)
assert_equal(true, Set[].superset?(Set[]))
assert_equal(true, Set[].superset?(klass[]), klass.name)
}
end
def test_proper_superset?
@ -238,16 +243,18 @@ class TC_Set < Test::Unit::TestCase
set.proper_superset?([2])
}
assert_equal(true, set.proper_superset?(Set[]))
assert_equal(true, set.proper_superset?(Set[1,2]))
assert_equal(false, set.proper_superset?(Set[1,2,3]))
assert_equal(false, set.proper_superset?(Set[1,2,3,4]))
assert_equal(false, set.proper_superset?(Set[1,4]))
[Set, Set2].each { |klass|
assert_equal(true, set.proper_superset?(klass[]), klass.name)
assert_equal(true, set.proper_superset?(klass[1,2]), klass.name)
assert_equal(false, set.proper_superset?(klass[1,2,3]), klass.name)
assert_equal(false, set.proper_superset?(klass[1,2,3,4]), klass.name)
assert_equal(false, set.proper_superset?(klass[1,4]), klass.name)
assert_equal(false, set > Set[1,2,3])
assert_equal(true, set > Set[1,2])
assert_equal(false, set > klass[1,2,3], klass.name)
assert_equal(true, set > klass[1,2], klass.name)
assert_equal(false, Set[].proper_superset?(Set[]))
assert_equal(false, Set[].proper_superset?(klass[]), klass.name)
}
end
def test_subset?
@ -265,16 +272,18 @@ class TC_Set < Test::Unit::TestCase
set.subset?([2])
}
assert_equal(true, set.subset?(Set[1,2,3,4]))
assert_equal(true, set.subset?(Set[1,2,3]))
assert_equal(false, set.subset?(Set[1,2]))
assert_equal(false, set.subset?(Set[]))
[Set, Set2].each { |klass|
assert_equal(true, set.subset?(klass[1,2,3,4]), klass.name)
assert_equal(true, set.subset?(klass[1,2,3]), klass.name)
assert_equal(false, set.subset?(klass[1,2]), klass.name)
assert_equal(false, set.subset?(klass[]), klass.name)
assert_equal(true, set <= Set[1,2,3])
assert_equal(true, set <= Set[1,2,3,4])
assert_equal(true, set <= klass[1,2,3], klass.name)
assert_equal(true, set <= klass[1,2,3,4], klass.name)
assert_equal(true, Set[].subset?(Set[1]))
assert_equal(true, Set[].subset?(Set[]))
assert_equal(true, Set[].subset?(klass[1]), klass.name)
assert_equal(true, Set[].subset?(klass[]), klass.name)
}
end
def test_proper_subset?
@ -292,15 +301,17 @@ class TC_Set < Test::Unit::TestCase
set.proper_subset?([2])
}
assert_equal(true, set.proper_subset?(Set[1,2,3,4]))
assert_equal(false, set.proper_subset?(Set[1,2,3]))
assert_equal(false, set.proper_subset?(Set[1,2]))
assert_equal(false, set.proper_subset?(Set[]))
[Set, Set2].each { |klass|
assert_equal(true, set.proper_subset?(klass[1,2,3,4]), klass.name)
assert_equal(false, set.proper_subset?(klass[1,2,3]), klass.name)
assert_equal(false, set.proper_subset?(klass[1,2]), klass.name)
assert_equal(false, set.proper_subset?(klass[]), klass.name)
assert_equal(false, set < Set[1,2,3])
assert_equal(true, set < Set[1,2,3,4])
assert_equal(false, set < klass[1,2,3], klass.name)
assert_equal(true, set < klass[1,2,3,4], klass.name)
assert_equal(false, Set[].proper_subset?(Set[]))
assert_equal(false, Set[].proper_subset?(klass[]), klass.name)
}
end
def assert_intersect(expected, set, other)