Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

speed up set intersect #2003

Merged
merged 2 commits into from Dec 31, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/set.rb
Expand Up @@ -474,7 +474,15 @@ def -(enum)
# 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)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if enum.is_a?(Set)
if enum.is_a?(self.class)

Copy link
Contributor

@esparta esparta Nov 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This suggestion looks like a good idea, but will have no effect (defaulting to do_with_enum) when using inheritance and the classes happen to not use the same base class.
Having Set as a verification secures most of the cases when having multiple levels of inheritance that will break if using self.class

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 &
Expand Down