summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorOleg Zubchenko <RedGreenBlueDiamond@gmail.com>2018-11-03 15:27:10 +0300
committerAkinori MUSHA <knu@idaemons.org>2019-12-31 20:52:41 +0900
commit4ce28b58cbf3f3b5ab0bcd3fa4479d4f6d427158 (patch)
tree90168bda785785f4f365603e0b91ae04721db2d7 /lib
parent0cf75e38506a412a7b4acd39a93e32aff1e8016c (diff)
speed up set intersect
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/2003
Diffstat (limited to 'lib')
-rw-r--r--lib/set.rb10
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 &