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:
parent
0cf75e3850
commit
4ce28b58cb
Notes:
git
2019-12-31 20:53:05 +09:00
1 changed files with 9 additions and 1 deletions
10
lib/set.rb
10
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 &
|
||||
|
|
Loading…
Reference in a new issue