diff options
author | Oleg Zubchenko <RedGreenBlueDiamond@gmail.com> | 2018-11-03 15:27:10 +0300 |
---|---|---|
committer | Akinori MUSHA <knu@idaemons.org> | 2019-12-31 20:52:41 +0900 |
commit | 4ce28b58cbf3f3b5ab0bcd3fa4479d4f6d427158 (patch) | |
tree | 90168bda785785f4f365603e0b91ae04721db2d7 /lib | |
parent | 0cf75e38506a412a7b4acd39a93e32aff1e8016c (diff) | |
download | ruby-4ce28b58cbf3f3b5ab0bcd3fa4479d4f6d427158.tar.gz |
speed up set intersect
Diffstat (limited to 'lib')
-rw-r--r-- | lib/set.rb | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/lib/set.rb b/lib/set.rb index 5a96c81832..e7d1be4f9f 100644 --- a/lib/set.rb +++ b/lib/set.rb @@ -464,7 +464,15 @@ class Set # Set['a', 'b', 'z'] & ['a', 'b', 'c'] #=> #<Set: {"a", "b"}> def &(enum) n = self.class.new - do_with_enum(enum) { |o| n.add(o) if include?(o) } + if enum.is_a?(Set) + if enum.size > size + each { |o| n.add(o) if enum.include?(o) } + else + enum.each { |o| n.add(o) if include?(o) } + end + else + do_with_enum(enum) { |o| n.add(o) if include?(o) } + end n end alias intersection & |