ruby-changes:48245
From: knu <ko1@a...>
Date: Sun, 22 Oct 2017 21:25:41 +0900 (JST)
Subject: [ruby-changes:48245] knu:r60360 (trunk): Add `Set#reset`
knu 2017-10-22 21:25:34 +0900 (Sun, 22 Oct 2017) New Revision: 60360 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=60360 Log: Add `Set#reset` This method resets the internal state of a set after modification to existing elements, reindexing and deduplicating them. [Feature #6589] Modified files: trunk/lib/set.rb trunk/test/test_set.rb Index: lib/set.rb =================================================================== --- lib/set.rb (revision 60359) +++ lib/set.rb (revision 60360) @@ -477,6 +477,19 @@ class Set https://github.com/ruby/ruby/blob/trunk/lib/set.rb#L477 @hash.eql?(o.instance_variable_get(:@hash)) end + # Resets the internal state after modification to existing elements + # and returns self. + # + # Elements will be reindexed and deduplicated. + def reset + if @hash.respond_to?(:rehash) + @hash.rehash # This should perform frozenness check. + else + raise "can't modify frozen #{self.class.name}" if frozen? + end + self + end + # Returns true if obj is a member of the set, and false otherwise. # # Used in case statements: @@ -731,6 +744,11 @@ class SortedSet < Set https://github.com/ruby/ruby/blob/trunk/lib/set.rb#L744 to_a super end + + def rehash + @keys = nil + super + end END end # a hack to shut up warning Index: test/test_set.rb =================================================================== --- test/test_set.rb (revision 60359) +++ test/test_set.rb (revision 60360) @@ -761,6 +761,19 @@ class TC_Set < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/test_set.rb#L761 assert_equal(3, set.size) assert_equal(array.uniq.sort, set.sort) end + + def test_reset + [Set, Class.new(Set)].each { |klass| + a = [1, 2] + b = [1] + set = klass.new([a, b]) + + b << 2 + set.reset + + assert_equal(klass.new([a]), set, klass.name) + } + end end class TC_SortedSet < Test::Unit::TestCase -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/