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

speed up set intersect

This commit is contained in:
Oleg Zubchenko 2018-11-03 15:27:10 +03:00 committed by Akinori MUSHA
parent 0cf75e3850
commit 4ce28b58cb
Notes: git 2019-12-31 20:53:05 +09:00

View file

@ -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 &