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

Open
wants to merge 1 commit into
base: trunk
from

Conversation

3 participants
@RGBD
Copy link

commented Nov 3, 2018

Current implementation computes set intersection s1 & s2 in O(s2.size) time.
It can be reduced to O([s1.size, s2.size].min) time.

Additional speedup comes from using #each instead of #do_with_enum.

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

This comment has been minimized.

Copy link
@taleh007

taleh007 Nov 3, 2018

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

This comment has been minimized.

Copy link
@esparta

esparta Nov 12, 2018

Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.