diff --git a/ChangeLog b/ChangeLog index ed5f50f2fe..d9f907c4d1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Mon Nov 16 15:42:36 2015 Akinori MUSHA + + * 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 * tool/rbinstall.rb: fix wrong permission for gem specification without diff --git a/lib/set.rb b/lib/set.rb index d612fdcb92..17775b07ea 100644 --- a/lib/set.rb +++ b/lib/set.rb @@ -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? diff --git a/test/test_set.rb b/test/test_set.rb index 854bcc637b..66944f576f 100644 --- a/test/test_set.rb +++ b/test/test_set.rb @@ -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)