From 8c90432af72a59d8934b2c94a1bc847449e1f393 Mon Sep 17 00:00:00 2001 From: knu Date: Sun, 22 Oct 2017 12:25:34 +0000 Subject: Add `Set#reset` This method resets the internal state of a set after modification to existing elements, reindexing and deduplicating them. [Feature #6589] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60360 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/set.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'lib/set.rb') diff --git a/lib/set.rb b/lib/set.rb index b668738ebb..5da196d1e0 100644 --- a/lib/set.rb +++ b/lib/set.rb @@ -477,6 +477,19 @@ class Set @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 to_a super end + + def rehash + @keys = nil + super + end END end # a hack to shut up warning -- cgit v1.2.1