Project

General

Profile

Actions

Feature #6588

closed

Set#intersect?

Added by marcandre (Marc-Andre Lafortune) over 12 years ago. Updated over 11 years ago.

Status:
Closed
Target version:
[ruby-core:45641]

Description

There is Set#superset?, Set#subset? with their proper variants, but there is no Set#intersect? nor Set#disjoint?

To check if two sets intersect, one can do

set.intersection(other).empty?

This cycles through all elements, though. To be efficient, one can instead do the iteration manually:

other.any? { |x| set.include?(x) }

I think it would be natural to add Set#intersect? and its reverse Set#disjoint?

class Set
def intersect?(enum)
enum.any? { |x| include?(x) }
end
end

Maybe it would be worth it to optimize it if enum is a larger Set by starting it with

  return any? { |x| enum.include?(x) } if enum.is_a?(Set) && enum.size > size
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0