Project

General

Profile

Actions

Feature #6588

closed

Set#intersect?

Added by marcandre (Marc-Andre Lafortune) almost 12 years ago. Updated over 10 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

Updated by mame (Yusuke Endoh) almost 12 years ago

  • Status changed from Open to Assigned

Updated by yhara (Yutaka HARA) over 11 years ago

  • Target version changed from 2.0.0 to 2.6

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

Comment about these simple features would be appreciated.

Updated by alexeymuranov (Alexey Muranov) over 11 years ago

+1. Maybe #meet? instead of #intersect? ? It can be argued that any set intersects any other, just the intersection is sometimes empty :).

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

alexeymuranov (Alexey Muranov) wrote:

+1.

Thanks for the +1

It can be argued that any set intersects any other, just the intersection is sometimes empty :).

No, I believe it would be wrong to argue that. From wikipedia: "We say that A intersects B if A intersects B at some element"

Moreover: "We say that A and B are disjoint if A does not intersect B. In plain language, they have no elements in common"

I believe that both intersect? and disjoint? are the established terms for the concept I'm proposing.

Updated by knu (Akinori MUSHA) over 10 years ago

OK, accepted. I'll work on it.

Actions #7

Updated by knu (Akinori MUSHA) over 10 years ago

  • Status changed from Assigned to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r42253.
Marc-Andre, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


Add Set#intersect? and #disjoint?.

  • lib/set.rb (Set#intersect?, Set#disjoint?): Add new methods for
    testing if two sets have any element in common.
    [ruby-core:45641] [Feature #6588] Based on the code by marcandre.

Updated by knu (Akinori MUSHA) over 10 years ago

I followed superset?() and the like, and made the new methods accept only a set for the moment, because I couldn't come up with an idea of how to deal with Range. For example:

  • if Set[2].intersect?(1.5..2.5) should return true
  • if Set[2].intersect?(3..(1.0/0)) should immediately return false using some knowledge on Range
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0