2015-11-16 02:41:30 -05:00
|
|
|
# frozen_string_literal: true
|
2020-12-22 07:41:44 -05:00
|
|
|
# :markup: markdown
|
2015-11-16 02:41:30 -05:00
|
|
|
#
|
2003-09-19 02:53:02 -04:00
|
|
|
# set.rb - defines the Set class
|
2020-12-22 07:41:44 -05:00
|
|
|
#
|
|
|
|
# Copyright (c) 2002-2020 Akinori MUSHA <knu@iDaemons.org>
|
2002-08-30 09:47:49 -04:00
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
# Documentation by Akinori MUSHA and Gavin Sinclair.
|
2002-08-30 09:47:49 -04:00
|
|
|
#
|
2003-09-19 02:53:02 -04:00
|
|
|
# All rights reserved. You can redistribute and/or modify it under the same
|
|
|
|
# terms as Ruby.
|
2020-12-22 07:41:44 -05:00
|
|
|
|
|
|
|
|
|
|
|
##
|
2003-10-17 09:16:03 -04:00
|
|
|
# This library provides the Set class, which deals with a collection
|
|
|
|
# of unordered values with no duplicates. It is a hybrid of Array's
|
2020-09-20 09:03:33 -04:00
|
|
|
# intuitive inter-operation facilities and Hash's fast lookup.
|
2002-12-24 00:29:04 -05:00
|
|
|
#
|
2020-12-22 07:41:44 -05:00
|
|
|
# The method `to_set` is added to Enumerable for convenience.
|
2003-09-19 02:53:02 -04:00
|
|
|
#
|
2002-12-24 00:29:04 -05:00
|
|
|
# Set implements a collection of unordered values with no duplicates.
|
|
|
|
# This is a hybrid of Array's intuitive inter-operation facilities and
|
|
|
|
# Hash's fast lookup.
|
|
|
|
#
|
2020-12-22 07:41:44 -05:00
|
|
|
# Set is easy to use with Enumerable objects (implementing `each`).
|
2007-03-19 22:09:10 -04:00
|
|
|
# Most of the initializer methods and binary operators accept generic
|
|
|
|
# Enumerable objects besides sets and arrays. An Enumerable object
|
2020-12-22 07:41:44 -05:00
|
|
|
# can be converted to Set using the `to_set` method.
|
2003-09-19 02:53:02 -04:00
|
|
|
#
|
2013-07-31 01:58:17 -04:00
|
|
|
# Set uses Hash as storage, so you must note the following points:
|
|
|
|
#
|
|
|
|
# * Equality of elements is determined according to Object#eql? and
|
2016-11-05 05:23:14 -04:00
|
|
|
# Object#hash. Use Set#compare_by_identity to make a set compare
|
|
|
|
# its elements by their identity.
|
2013-07-31 01:58:17 -04:00
|
|
|
# * Set assumes that the identity of each element does not change
|
|
|
|
# while it is stored. Modifying an element of a set will render the
|
|
|
|
# set to an unreliable state.
|
|
|
|
# * When a string is to be stored, a frozen copy of the string is
|
|
|
|
# stored instead unless the original string is already frozen.
|
|
|
|
#
|
2020-12-22 07:41:44 -05:00
|
|
|
# ## Comparison
|
2012-08-30 00:01:58 -04:00
|
|
|
#
|
2020-12-22 07:41:44 -05:00
|
|
|
# The comparison operators `<`, `>`, `<=`, and `>=` are implemented as
|
|
|
|
# shorthand for the {proper_,}{subset?,superset?} methods. The `<=>`
|
|
|
|
# operator reflects this order, or return `nil` for sets that both
|
|
|
|
# have distinct elements (`{x, y}` vs. `{x, z}` for example).
|
2012-08-30 00:01:58 -04:00
|
|
|
#
|
2020-12-22 07:41:44 -05:00
|
|
|
# ## Example
|
2003-09-19 02:53:02 -04:00
|
|
|
#
|
2020-12-22 07:41:44 -05:00
|
|
|
# ```ruby
|
|
|
|
# require 'set'
|
|
|
|
# s1 = Set[1, 2] #=> #<Set: {1, 2}>
|
|
|
|
# s2 = [1, 2].to_set #=> #<Set: {1, 2}>
|
|
|
|
# s1 == s2 #=> true
|
|
|
|
# s1.add("foo") #=> #<Set: {1, 2, "foo"}>
|
|
|
|
# s1.merge([2, 6]) #=> #<Set: {1, 2, "foo", 6}>
|
|
|
|
# s1.subset?(s2) #=> false
|
|
|
|
# s2.subset?(s1) #=> true
|
|
|
|
# ```
|
2003-09-19 02:53:02 -04:00
|
|
|
#
|
2020-12-22 07:41:44 -05:00
|
|
|
# ## Contact
|
2008-03-21 08:15:06 -04:00
|
|
|
#
|
2020-12-22 07:41:44 -05:00
|
|
|
# - Akinori MUSHA <<knu@iDaemons.org>> (current maintainer)
|
2008-03-21 08:15:06 -04:00
|
|
|
#
|
2021-04-14 12:57:40 -04:00
|
|
|
# ## What's Here
|
|
|
|
#
|
2021-06-21 15:33:02 -04:00
|
|
|
# First, what's elsewhere. \Class \Set:
|
|
|
|
#
|
2021-06-21 15:38:33 -04:00
|
|
|
# - Inherits from {class Object}[https://docs.ruby-lang.org/en/master/Object.html#class-Object-label-What-27s+Here].
|
|
|
|
# - Includes {module Enumerable}[https://docs.ruby-lang.org/en/master/Enumerable.html#module-Enumerable-label-What-27s+Here],
|
2021-06-21 15:33:02 -04:00
|
|
|
# which provides dozens of additional methods.
|
2021-04-14 12:57:40 -04:00
|
|
|
#
|
|
|
|
# In particular, class \Set does not have many methods of its own
|
|
|
|
# for fetching or for iterating.
|
|
|
|
# Instead, it relies on those in \Enumerable.
|
|
|
|
#
|
|
|
|
# Here, class \Set provides methods that are useful for:
|
|
|
|
#
|
|
|
|
# - [Creating a Set](#class-Set-label-Methods+for+Creating+a+Set)
|
|
|
|
# - [Set Operations](#class-Set-label-Methods+for+Set+Operations)
|
|
|
|
# - [Comparing](#class-Set-label-Methods+for+Comparing)
|
|
|
|
# - [Querying](#class-Set-label-Methods+for+Querying)
|
|
|
|
# - [Assigning](#class-Set-label-Methods+for+Assigning)
|
|
|
|
# - [Deleting](#class-Set-label-Methods+for+Deleting)
|
|
|
|
# - [Converting](#class-Set-label-Methods+for+Converting)
|
|
|
|
# - [Iterating](#class-Set-label-Methods+for+Iterating)
|
|
|
|
# - [And more....](#class-Set-label-Other+Methods)
|
|
|
|
#
|
|
|
|
# ### Methods for Creating a \Set
|
|
|
|
#
|
2022-03-30 15:10:20 -04:00
|
|
|
# - ::[]:
|
2021-04-14 12:57:40 -04:00
|
|
|
# Returns a new set containing the given objects.
|
2022-03-30 15:10:20 -04:00
|
|
|
# - ::new:
|
2021-04-14 12:57:40 -04:00
|
|
|
# Returns a new set containing either the given objects
|
|
|
|
# (if no block given) or the return values from the called block
|
|
|
|
# (if a block given).
|
|
|
|
#
|
|
|
|
# ### Methods for \Set Operations
|
|
|
|
#
|
2022-03-30 15:10:20 -04:00
|
|
|
# - [|](#method-i-7C) (aliased as #union and #+):
|
2021-04-14 12:57:40 -04:00
|
|
|
# Returns a new set containing all elements from +self+
|
|
|
|
# and all elements from a given enumerable (no duplicates).
|
2022-03-30 15:10:20 -04:00
|
|
|
# - [&](#method-i-26) (aliased as #intersection):
|
2021-04-14 12:57:40 -04:00
|
|
|
# Returns a new set containing all elements common to +self+
|
|
|
|
# and a given enumerable.
|
2022-03-30 15:10:20 -04:00
|
|
|
# - [-](#method-i-2D) (aliased as #difference):
|
2021-04-14 12:57:40 -04:00
|
|
|
# Returns a copy of +self+ with all elements
|
|
|
|
# in a given enumerable removed.
|
2022-03-30 15:10:20 -04:00
|
|
|
# - [\^](#method-i-5E):
|
2021-04-14 12:57:40 -04:00
|
|
|
# Returns a new set containing all elements from +self+
|
|
|
|
# and a given enumerable except those common to both.
|
|
|
|
#
|
|
|
|
# ### Methods for Comparing
|
|
|
|
#
|
2022-03-30 15:10:20 -04:00
|
|
|
# - [<=>](#method-i-3C-3D-3E):
|
2021-04-14 12:57:40 -04:00
|
|
|
# Returns -1, 0, or 1 as +self+ is less than, equal to,
|
|
|
|
# or greater than a given object.
|
2022-03-30 15:10:20 -04:00
|
|
|
# - [==](#method-i-3D-3D):
|
2021-04-14 12:57:40 -04:00
|
|
|
# Returns whether +self+ and a given enumerable are equal,
|
|
|
|
# as determined by Object#eql?.
|
2022-03-30 15:10:20 -04:00
|
|
|
# - \#compare_by_identity?:
|
2021-04-14 12:57:40 -04:00
|
|
|
# Returns whether the set considers only identity
|
|
|
|
# when comparing elements.
|
|
|
|
#
|
|
|
|
# ### Methods for Querying
|
|
|
|
#
|
2022-03-30 15:10:20 -04:00
|
|
|
# - \#length (aliased as #size):
|
2021-04-14 12:57:40 -04:00
|
|
|
# Returns the count of elements.
|
2022-03-30 15:10:20 -04:00
|
|
|
# - \#empty?:
|
2021-04-14 12:57:40 -04:00
|
|
|
# Returns whether the set has no elements.
|
2022-03-30 15:10:20 -04:00
|
|
|
# - \#include? (aliased as #member? and #===):
|
2021-04-14 12:57:40 -04:00
|
|
|
# Returns whether a given object is an element in the set.
|
2022-03-30 15:10:20 -04:00
|
|
|
# - \#subset? (aliased as [<=](#method-i-3C-3D)):
|
2021-04-14 12:57:40 -04:00
|
|
|
# Returns whether a given object is a subset of the set.
|
2022-03-30 15:10:20 -04:00
|
|
|
# - \#proper_subset? (aliased as [<](#method-i-3C)):
|
2021-04-14 12:57:40 -04:00
|
|
|
# Returns whether a given enumerable is a proper subset of the set.
|
2022-03-30 15:10:20 -04:00
|
|
|
# - \#superset? (aliased as [<=](#method-i-3E-3D])):
|
2021-04-14 12:57:40 -04:00
|
|
|
# Returns whether a given enumerable is a superset of the set.
|
2022-03-30 15:10:20 -04:00
|
|
|
# - \#proper_superset? (aliased as [>](#method-i-3E)):
|
2021-04-14 12:57:40 -04:00
|
|
|
# Returns whether a given enumerable is a proper superset of the set.
|
2022-03-30 15:10:20 -04:00
|
|
|
# - \#disjoint?:
|
2021-04-14 12:57:40 -04:00
|
|
|
# Returns +true+ if the set and a given enumerable
|
|
|
|
# have no common elements, +false+ otherwise.
|
2022-03-30 15:10:20 -04:00
|
|
|
# - \#intersect?:
|
|
|
|
# Returns +true+ if the set and a given enumerable:
|
2021-04-14 12:57:40 -04:00
|
|
|
# have any common elements, +false+ otherwise.
|
2022-03-30 15:10:20 -04:00
|
|
|
# - \#compare_by_identity?:
|
2021-04-14 12:57:40 -04:00
|
|
|
# Returns whether the set considers only identity
|
|
|
|
# when comparing elements.
|
|
|
|
#
|
|
|
|
# ### Methods for Assigning
|
|
|
|
#
|
2022-03-30 15:10:20 -04:00
|
|
|
# - \#add (aliased as #<<):
|
2021-04-14 12:57:40 -04:00
|
|
|
# Adds a given object to the set; returns +self+.
|
2022-03-30 15:10:20 -04:00
|
|
|
# - \#add?:
|
2021-04-14 12:57:40 -04:00
|
|
|
# If the given object is not an element in the set,
|
|
|
|
# adds it and returns +self+; otherwise, returns +nil+.
|
2022-03-30 15:10:20 -04:00
|
|
|
# - \#merge:
|
2021-04-14 12:57:40 -04:00
|
|
|
# Adds each given object to the set; returns +self+.
|
2022-03-30 15:10:20 -04:00
|
|
|
# - \#replace:
|
2021-04-14 12:57:40 -04:00
|
|
|
# Replaces the contents of the set with the contents
|
|
|
|
# of a given enumerable.
|
|
|
|
#
|
|
|
|
# ### Methods for Deleting
|
|
|
|
#
|
2022-03-30 15:10:20 -04:00
|
|
|
# - \#clear:
|
2021-04-14 12:57:40 -04:00
|
|
|
# Removes all elements in the set; returns +self+.
|
2022-03-30 15:10:20 -04:00
|
|
|
# - \#delete:
|
2021-04-14 12:57:40 -04:00
|
|
|
# Removes a given object from the set; returns +self+.
|
2022-03-30 15:10:20 -04:00
|
|
|
# - \#delete?:
|
2021-04-14 12:57:40 -04:00
|
|
|
# If the given object is an element in the set,
|
|
|
|
# removes it and returns +self+; otherwise, returns +nil+.
|
2022-03-30 15:10:20 -04:00
|
|
|
# - \#subtract:
|
2021-04-14 12:57:40 -04:00
|
|
|
# Removes each given object from the set; returns +self+.
|
|
|
|
# - \#delete_if - Removes elements specified by a given block.
|
2022-03-30 15:10:20 -04:00
|
|
|
# - \#select! (aliased as #filter!):
|
2021-04-14 12:57:40 -04:00
|
|
|
# Removes elements not specified by a given block.
|
2022-03-30 15:10:20 -04:00
|
|
|
# - \#keep_if:
|
2021-04-14 12:57:40 -04:00
|
|
|
# Removes elements not specified by a given block.
|
|
|
|
# - \#reject!
|
|
|
|
# Removes elements specified by a given block.
|
|
|
|
#
|
|
|
|
# ### Methods for Converting
|
|
|
|
#
|
2022-03-30 15:10:20 -04:00
|
|
|
# - \#classify:
|
2021-04-14 12:57:40 -04:00
|
|
|
# Returns a hash that classifies the elements,
|
|
|
|
# as determined by the given block.
|
2022-03-30 15:10:20 -04:00
|
|
|
# - \#collect! (aliased as #map!):
|
2021-04-14 12:57:40 -04:00
|
|
|
# Replaces each element with a block return-value.
|
2022-03-30 15:10:20 -04:00
|
|
|
# - \#divide:
|
2021-04-14 12:57:40 -04:00
|
|
|
# Returns a hash that classifies the elements,
|
|
|
|
# as determined by the given block;
|
|
|
|
# differs from #classify in that the block may accept
|
|
|
|
# either one or two arguments.
|
2022-03-30 15:10:20 -04:00
|
|
|
# - \#flatten:
|
2021-04-14 12:57:40 -04:00
|
|
|
# Returns a new set that is a recursive flattening of +self+.
|
2022-03-30 15:10:20 -04:00
|
|
|
# \#flatten!:
|
2021-04-14 12:57:40 -04:00
|
|
|
# Replaces each nested set in +self+ with the elements from that set.
|
2022-03-30 15:10:20 -04:00
|
|
|
# - \#inspect (aliased as #to_s):
|
2021-04-14 12:57:40 -04:00
|
|
|
# Returns a string displaying the elements.
|
2022-03-30 15:10:20 -04:00
|
|
|
# - \#join:
|
2021-04-14 12:57:40 -04:00
|
|
|
# Returns a string containing all elements, converted to strings
|
|
|
|
# as needed, and joined by the given record separator.
|
2022-03-30 15:10:20 -04:00
|
|
|
# - \#to_a:
|
2021-04-14 12:57:40 -04:00
|
|
|
# Returns an array containing all set elements.
|
2022-03-30 15:10:20 -04:00
|
|
|
# - \#to_set:
|
2021-04-14 12:57:40 -04:00
|
|
|
# Returns +self+ if given no arguments and no block;
|
|
|
|
# with a block given, returns a new set consisting of block
|
|
|
|
# return values.
|
|
|
|
#
|
|
|
|
# ### Methods for Iterating
|
|
|
|
#
|
2022-03-30 15:10:20 -04:00
|
|
|
# - \#each:
|
2021-04-14 12:57:40 -04:00
|
|
|
# Calls the block with each successive element; returns +self+.
|
|
|
|
#
|
|
|
|
# ### Other Methods
|
|
|
|
#
|
2022-03-30 15:10:20 -04:00
|
|
|
# - \#reset:
|
2021-04-14 12:57:40 -04:00
|
|
|
# Resets the internal state; useful if an object
|
|
|
|
# has been modified while an element in the set.
|
|
|
|
#
|
2002-08-30 09:47:49 -04:00
|
|
|
class Set
|
|
|
|
include Enumerable
|
|
|
|
|
2002-12-24 00:29:04 -05:00
|
|
|
# Creates a new set containing the given objects.
|
2017-11-17 04:48:47 -05:00
|
|
|
#
|
|
|
|
# Set[1, 2] # => #<Set: {1, 2}>
|
2017-11-22 15:58:24 -05:00
|
|
|
# Set[1, 2, 1] # => #<Set: {1, 2}>
|
2017-11-17 04:48:47 -05:00
|
|
|
# Set[1, 'c', :s] # => #<Set: {1, "c", :s}>
|
2002-08-30 09:47:49 -04:00
|
|
|
def self.[](*ary)
|
|
|
|
new(ary)
|
|
|
|
end
|
|
|
|
|
2002-12-24 00:29:04 -05:00
|
|
|
# Creates a new set containing the elements of the given enumerable
|
|
|
|
# object.
|
|
|
|
#
|
|
|
|
# If a block is given, the elements of enum are preprocessed by the
|
|
|
|
# given block.
|
2017-11-17 04:48:47 -05:00
|
|
|
#
|
|
|
|
# Set.new([1, 2]) #=> #<Set: {1, 2}>
|
2017-11-22 15:58:24 -05:00
|
|
|
# Set.new([1, 2, 1]) #=> #<Set: {1, 2}>
|
2017-11-17 04:48:47 -05:00
|
|
|
# Set.new([1, 'c', :s]) #=> #<Set: {1, "c", :s}>
|
2017-11-22 15:58:24 -05:00
|
|
|
# Set.new(1..5) #=> #<Set: {1, 2, 3, 4, 5}>
|
2017-11-17 04:48:47 -05:00
|
|
|
# Set.new([1, 2, 3]) { |x| x * x } #=> #<Set: {1, 4, 9}>
|
2003-01-21 11:38:42 -05:00
|
|
|
def initialize(enum = nil, &block) # :yields: o
|
2015-02-11 14:04:16 -05:00
|
|
|
@hash ||= Hash.new(false)
|
2002-08-30 09:47:49 -04:00
|
|
|
|
2002-09-07 06:48:14 -04:00
|
|
|
enum.nil? and return
|
|
|
|
|
2002-09-20 06:46:52 -04:00
|
|
|
if block
|
2010-02-11 12:38:05 -05:00
|
|
|
do_with_enum(enum) { |o| add(block[o]) }
|
2002-09-20 06:46:52 -04:00
|
|
|
else
|
|
|
|
merge(enum)
|
|
|
|
end
|
2002-08-30 09:47:49 -04:00
|
|
|
end
|
|
|
|
|
2016-11-05 05:23:14 -04:00
|
|
|
# Makes the set compare its elements by their identity and returns
|
|
|
|
# self. This method may not be supported by all subclasses of Set.
|
|
|
|
def compare_by_identity
|
|
|
|
if @hash.respond_to?(:compare_by_identity)
|
|
|
|
@hash.compare_by_identity
|
|
|
|
self
|
|
|
|
else
|
|
|
|
raise NotImplementedError, "#{self.class.name}\##{__method__} is not implemented"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns true if the set will compare its elements by their
|
|
|
|
# identity. Also see Set#compare_by_identity.
|
|
|
|
def compare_by_identity?
|
|
|
|
@hash.respond_to?(:compare_by_identity?) && @hash.compare_by_identity?
|
|
|
|
end
|
|
|
|
|
2011-05-11 19:17:52 -04:00
|
|
|
def do_with_enum(enum, &block) # :nodoc:
|
2010-02-11 12:38:05 -05:00
|
|
|
if enum.respond_to?(:each_entry)
|
2014-08-06 07:28:21 -04:00
|
|
|
enum.each_entry(&block) if block
|
2010-02-11 12:38:05 -05:00
|
|
|
elsif enum.respond_to?(:each)
|
2014-08-06 07:28:21 -04:00
|
|
|
enum.each(&block) if block
|
2010-02-11 12:38:05 -05:00
|
|
|
else
|
|
|
|
raise ArgumentError, "value must be enumerable"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
private :do_with_enum
|
|
|
|
|
2014-08-06 06:13:34 -04:00
|
|
|
# Dup internal hash.
|
|
|
|
def initialize_dup(orig)
|
|
|
|
super
|
2012-09-01 05:51:48 -04:00
|
|
|
@hash = orig.instance_variable_get(:@hash).dup
|
2002-08-30 09:47:49 -04:00
|
|
|
end
|
|
|
|
|
2020-09-20 10:23:13 -04:00
|
|
|
if Kernel.instance_method(:initialize_clone).arity != 1
|
|
|
|
# Clone internal hash.
|
|
|
|
def initialize_clone(orig, **options)
|
|
|
|
super
|
|
|
|
@hash = orig.instance_variable_get(:@hash).clone(**options)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
# Clone internal hash.
|
|
|
|
def initialize_clone(orig)
|
|
|
|
super
|
|
|
|
@hash = orig.instance_variable_get(:@hash).clone
|
|
|
|
end
|
2014-08-06 06:13:34 -04:00
|
|
|
end
|
|
|
|
|
2011-05-18 20:07:25 -04:00
|
|
|
def freeze # :nodoc:
|
2007-03-19 22:09:10 -04:00
|
|
|
@hash.freeze
|
2013-06-01 05:01:16 -04:00
|
|
|
super
|
2007-03-19 22:09:10 -04:00
|
|
|
end
|
|
|
|
|
2002-12-24 00:29:04 -05:00
|
|
|
# Returns the number of elements.
|
2002-08-30 09:47:49 -04:00
|
|
|
def size
|
|
|
|
@hash.size
|
|
|
|
end
|
|
|
|
alias length size
|
|
|
|
|
2002-12-24 00:29:04 -05:00
|
|
|
# Returns true if the set contains no elements.
|
2002-08-30 09:47:49 -04:00
|
|
|
def empty?
|
|
|
|
@hash.empty?
|
|
|
|
end
|
|
|
|
|
2002-12-24 00:29:04 -05:00
|
|
|
# Removes all elements and returns self.
|
2017-11-17 04:48:47 -05:00
|
|
|
#
|
|
|
|
# set = Set[1, 'c', :s] #=> #<Set: {1, "c", :s}>
|
|
|
|
# set.clear #=> #<Set: {}>
|
|
|
|
# set #=> #<Set: {}>
|
2002-08-30 09:47:49 -04:00
|
|
|
def clear
|
|
|
|
@hash.clear
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2002-12-24 00:29:04 -05:00
|
|
|
# Replaces the contents of the set with the contents of the given
|
|
|
|
# enumerable object and returns self.
|
2017-11-22 15:58:24 -05:00
|
|
|
#
|
|
|
|
# set = Set[1, 'c', :s] #=> #<Set: {1, "c", :s}>
|
|
|
|
# set.replace([1, 2]) #=> #<Set: {1, 2}>
|
|
|
|
# set #=> #<Set: {1, 2}>
|
2002-08-30 09:47:49 -04:00
|
|
|
def replace(enum)
|
2010-10-10 05:45:36 -04:00
|
|
|
if enum.instance_of?(self.class)
|
|
|
|
@hash.replace(enum.instance_variable_get(:@hash))
|
2014-08-06 07:28:21 -04:00
|
|
|
self
|
2002-09-20 06:46:52 -04:00
|
|
|
else
|
2015-11-16 02:41:18 -05:00
|
|
|
do_with_enum(enum) # make sure enum is enumerable before calling clear
|
2002-09-20 06:46:52 -04:00
|
|
|
clear
|
2010-02-11 12:38:05 -05:00
|
|
|
merge(enum)
|
2002-09-20 06:46:52 -04:00
|
|
|
end
|
2002-08-30 09:47:49 -04:00
|
|
|
end
|
|
|
|
|
2003-01-21 10:09:12 -05:00
|
|
|
# Converts the set to an array. The order of elements is uncertain.
|
2017-11-17 04:48:47 -05:00
|
|
|
#
|
|
|
|
# Set[1, 2].to_a #=> [1, 2]
|
|
|
|
# Set[1, 'c', :s].to_a #=> [1, "c", :s]
|
2002-08-30 09:47:49 -04:00
|
|
|
def to_a
|
|
|
|
@hash.keys
|
|
|
|
end
|
|
|
|
|
2013-07-18 22:22:11 -04:00
|
|
|
# Returns self if no arguments are given. Otherwise, converts the
|
2020-12-22 07:41:44 -05:00
|
|
|
# set to another with `klass.new(self, *args, &block)`.
|
2013-07-18 22:22:11 -04:00
|
|
|
#
|
2020-12-22 07:41:44 -05:00
|
|
|
# In subclasses, returns `klass.new(self, *args, &block)` unless
|
2013-07-18 22:22:11 -04:00
|
|
|
# overridden.
|
|
|
|
def to_set(klass = Set, *args, &block)
|
|
|
|
return self if instance_of?(Set) && klass == Set && block.nil? && args.empty?
|
|
|
|
klass.new(self, *args, &block)
|
|
|
|
end
|
|
|
|
|
2011-05-11 19:17:52 -04:00
|
|
|
def flatten_merge(set, seen = Set.new) # :nodoc:
|
2002-09-07 06:32:23 -04:00
|
|
|
set.each { |e|
|
|
|
|
if e.is_a?(Set)
|
2011-05-18 17:19:18 -04:00
|
|
|
if seen.include?(e_id = e.object_id)
|
|
|
|
raise ArgumentError, "tried to flatten recursive Set"
|
|
|
|
end
|
2002-08-30 09:47:49 -04:00
|
|
|
|
2011-05-18 17:19:18 -04:00
|
|
|
seen.add(e_id)
|
|
|
|
flatten_merge(e, seen)
|
|
|
|
seen.delete(e_id)
|
2002-08-30 09:47:49 -04:00
|
|
|
else
|
2011-05-18 17:19:18 -04:00
|
|
|
add(e)
|
2002-08-30 09:47:49 -04:00
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2002-09-07 06:32:23 -04:00
|
|
|
self
|
2002-08-30 09:47:49 -04:00
|
|
|
end
|
2002-09-07 06:32:23 -04:00
|
|
|
protected :flatten_merge
|
2002-08-30 09:47:49 -04:00
|
|
|
|
2002-12-24 00:29:04 -05:00
|
|
|
# Returns a new set that is a copy of the set, flattening each
|
|
|
|
# containing set recursively.
|
2002-08-30 09:47:49 -04:00
|
|
|
def flatten
|
2002-10-02 12:45:35 -04:00
|
|
|
self.class.new.flatten_merge(self)
|
2002-08-30 09:47:49 -04:00
|
|
|
end
|
|
|
|
|
2002-12-24 00:29:04 -05:00
|
|
|
# Equivalent to Set#flatten, but replaces the receiver with the
|
|
|
|
# result in place. Returns nil if no modifications were made.
|
2002-08-30 09:47:49 -04:00
|
|
|
def flatten!
|
2015-11-16 02:29:37 -05:00
|
|
|
replace(flatten()) if any? { |e| e.is_a?(Set) }
|
2002-08-30 09:47:49 -04:00
|
|
|
end
|
|
|
|
|
2002-12-24 00:29:04 -05:00
|
|
|
# Returns true if the set contains the given object.
|
2015-02-16 20:47:28 -05:00
|
|
|
#
|
|
|
|
# Note that <code>include?</code> and <code>member?</code> do not test member
|
|
|
|
# equality using <code>==</code> as do other Enumerables.
|
|
|
|
#
|
|
|
|
# See also Enumerable#include?
|
2002-08-30 09:47:49 -04:00
|
|
|
def include?(o)
|
2015-02-11 14:04:16 -05:00
|
|
|
@hash[o]
|
2002-08-30 09:47:49 -04:00
|
|
|
end
|
|
|
|
alias member? include?
|
|
|
|
|
2003-01-21 10:15:26 -05:00
|
|
|
# Returns true if the set is a superset of the given set.
|
2002-11-09 13:52:04 -05:00
|
|
|
def superset?(set)
|
2015-11-16 01:43:43 -05:00
|
|
|
case
|
2017-10-21 12:28:52 -04:00
|
|
|
when set.instance_of?(self.class) && @hash.respond_to?(:>=)
|
2015-11-16 01:43:43 -05:00
|
|
|
@hash >= set.instance_variable_get(:@hash)
|
|
|
|
when set.is_a?(Set)
|
|
|
|
size >= set.size && set.all? { |o| include?(o) }
|
|
|
|
else
|
|
|
|
raise ArgumentError, "value must be a set"
|
|
|
|
end
|
2002-11-09 13:52:04 -05:00
|
|
|
end
|
2012-08-30 00:01:58 -04:00
|
|
|
alias >= superset?
|
2002-11-09 13:52:04 -05:00
|
|
|
|
2003-01-21 10:15:26 -05:00
|
|
|
# Returns true if the set is a proper superset of the given set.
|
2002-11-09 13:52:04 -05:00
|
|
|
def proper_superset?(set)
|
2015-11-16 01:43:43 -05:00
|
|
|
case
|
2017-10-21 12:28:52 -04:00
|
|
|
when set.instance_of?(self.class) && @hash.respond_to?(:>)
|
2015-11-16 01:43:43 -05:00
|
|
|
@hash > set.instance_variable_get(:@hash)
|
|
|
|
when set.is_a?(Set)
|
|
|
|
size > set.size && set.all? { |o| include?(o) }
|
|
|
|
else
|
|
|
|
raise ArgumentError, "value must be a set"
|
|
|
|
end
|
2002-11-09 13:52:04 -05:00
|
|
|
end
|
2012-08-30 00:01:58 -04:00
|
|
|
alias > proper_superset?
|
2002-11-09 13:52:04 -05:00
|
|
|
|
2003-01-21 10:15:26 -05:00
|
|
|
# Returns true if the set is a subset of the given set.
|
2002-11-09 13:52:04 -05:00
|
|
|
def subset?(set)
|
2015-11-16 01:43:43 -05:00
|
|
|
case
|
2017-10-21 12:28:52 -04:00
|
|
|
when set.instance_of?(self.class) && @hash.respond_to?(:<=)
|
2015-11-16 01:43:43 -05:00
|
|
|
@hash <= set.instance_variable_get(:@hash)
|
|
|
|
when set.is_a?(Set)
|
|
|
|
size <= set.size && all? { |o| set.include?(o) }
|
|
|
|
else
|
|
|
|
raise ArgumentError, "value must be a set"
|
|
|
|
end
|
2002-11-09 13:52:04 -05:00
|
|
|
end
|
2012-08-30 00:01:58 -04:00
|
|
|
alias <= subset?
|
2002-11-09 13:52:04 -05:00
|
|
|
|
2002-12-24 00:29:04 -05:00
|
|
|
# Returns true if the set is a proper subset of the given set.
|
2002-11-09 13:52:04 -05:00
|
|
|
def proper_subset?(set)
|
2015-11-16 01:43:43 -05:00
|
|
|
case
|
2017-10-21 12:28:52 -04:00
|
|
|
when set.instance_of?(self.class) && @hash.respond_to?(:<)
|
2015-11-16 01:43:43 -05:00
|
|
|
@hash < set.instance_variable_get(:@hash)
|
|
|
|
when set.is_a?(Set)
|
|
|
|
size < set.size && all? { |o| set.include?(o) }
|
|
|
|
else
|
|
|
|
raise ArgumentError, "value must be a set"
|
|
|
|
end
|
2002-08-30 09:47:49 -04:00
|
|
|
end
|
2012-08-30 00:01:58 -04:00
|
|
|
alias < proper_subset?
|
2002-08-30 09:47:49 -04:00
|
|
|
|
2020-10-01 23:55:46 -04:00
|
|
|
# Returns 0 if the set are equal,
|
|
|
|
# -1 / +1 if the set is a proper subset / superset of the given set,
|
|
|
|
# or nil if they both have unique elements.
|
|
|
|
def <=>(set)
|
|
|
|
return unless set.is_a?(Set)
|
|
|
|
|
|
|
|
case size <=> set.size
|
|
|
|
when -1 then -1 if proper_subset?(set)
|
|
|
|
when +1 then +1 if proper_superset?(set)
|
|
|
|
else 0 if self.==(set)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-18 11:24:45 -04:00
|
|
|
# Returns true if the set and the given enumerable have at least one
|
2013-07-30 05:58:13 -04:00
|
|
|
# element in common.
|
2014-01-28 01:19:19 -05:00
|
|
|
#
|
2020-12-22 07:41:44 -05:00
|
|
|
# Set[1, 2, 3].intersect? Set[4, 5] #=> false
|
|
|
|
# Set[1, 2, 3].intersect? Set[3, 4] #=> true
|
2021-06-18 11:24:45 -04:00
|
|
|
# Set[1, 2, 3].intersect? 4..5 #=> false
|
|
|
|
# Set[1, 2, 3].intersect? [3, 4] #=> true
|
2013-07-30 05:58:13 -04:00
|
|
|
def intersect?(set)
|
2021-06-15 14:08:45 -04:00
|
|
|
case set
|
|
|
|
when Set
|
2021-06-15 18:14:54 -04:00
|
|
|
if size < set.size
|
|
|
|
any? { |o| set.include?(o) }
|
|
|
|
else
|
|
|
|
set.any? { |o| include?(o) }
|
|
|
|
end
|
|
|
|
when Enumerable
|
2013-07-30 05:58:13 -04:00
|
|
|
set.any? { |o| include?(o) }
|
2021-06-15 18:14:54 -04:00
|
|
|
else
|
|
|
|
raise ArgumentError, "value must be enumerable"
|
2013-07-30 05:58:13 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-18 11:24:45 -04:00
|
|
|
# Returns true if the set and the given enumerable have
|
|
|
|
# no element in common. This method is the opposite of `intersect?`.
|
2014-01-28 01:19:19 -05:00
|
|
|
#
|
2020-12-22 07:41:44 -05:00
|
|
|
# Set[1, 2, 3].disjoint? Set[3, 4] #=> false
|
|
|
|
# Set[1, 2, 3].disjoint? Set[4, 5] #=> true
|
2021-06-18 11:24:45 -04:00
|
|
|
# Set[1, 2, 3].disjoint? [3, 4] #=> false
|
|
|
|
# Set[1, 2, 3].disjoint? 4..5 #=> true
|
2013-07-30 05:58:13 -04:00
|
|
|
def disjoint?(set)
|
|
|
|
!intersect?(set)
|
|
|
|
end
|
|
|
|
|
2002-12-24 00:29:04 -05:00
|
|
|
# Calls the given block once for each element in the set, passing
|
2008-04-22 22:58:46 -04:00
|
|
|
# the element as parameter. Returns an enumerator if no block is
|
|
|
|
# given.
|
2012-08-31 04:43:09 -04:00
|
|
|
def each(&block)
|
2015-06-15 01:37:38 -04:00
|
|
|
block or return enum_for(__method__) { size }
|
2012-08-31 04:43:09 -04:00
|
|
|
@hash.each_key(&block)
|
2003-07-27 14:10:54 -04:00
|
|
|
self
|
2002-08-30 09:47:49 -04:00
|
|
|
end
|
|
|
|
|
2020-12-22 07:41:44 -05:00
|
|
|
# Adds the given object to the set and returns self. Use `merge` to
|
2007-03-19 22:09:10 -04:00
|
|
|
# add many elements at once.
|
2017-11-17 04:48:47 -05:00
|
|
|
#
|
|
|
|
# Set[1, 2].add(3) #=> #<Set: {1, 2, 3}>
|
2017-11-22 15:58:24 -05:00
|
|
|
# Set[1, 2].add([3, 4]) #=> #<Set: {1, 2, [3, 4]}>
|
2017-11-17 04:48:47 -05:00
|
|
|
# Set[1, 2].add(2) #=> #<Set: {1, 2}>
|
2002-08-30 09:47:49 -04:00
|
|
|
def add(o)
|
2005-06-25 02:22:05 -04:00
|
|
|
@hash[o] = true
|
2002-08-30 09:47:49 -04:00
|
|
|
self
|
|
|
|
end
|
|
|
|
alias << add
|
|
|
|
|
2003-09-18 21:51:17 -04:00
|
|
|
# Adds the given object to the set and returns self. If the
|
2002-12-24 00:29:04 -05:00
|
|
|
# object is already in the set, returns nil.
|
2017-11-17 04:48:47 -05:00
|
|
|
#
|
|
|
|
# Set[1, 2].add?(3) #=> #<Set: {1, 2, 3}>
|
2017-11-22 15:58:24 -05:00
|
|
|
# Set[1, 2].add?([3, 4]) #=> #<Set: {1, 2, [3, 4]}>
|
2017-11-17 04:48:47 -05:00
|
|
|
# Set[1, 2].add?(2) #=> nil
|
2002-09-20 06:46:52 -04:00
|
|
|
def add?(o)
|
2015-11-16 02:29:37 -05:00
|
|
|
add(o) unless include?(o)
|
2002-09-20 06:46:52 -04:00
|
|
|
end
|
|
|
|
|
2020-12-22 07:41:44 -05:00
|
|
|
# Deletes the given object from the set and returns self. Use
|
|
|
|
# `subtract` to delete many items at once.
|
2002-08-30 09:47:49 -04:00
|
|
|
def delete(o)
|
2002-09-20 06:46:52 -04:00
|
|
|
@hash.delete(o)
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2002-12-24 00:29:04 -05:00
|
|
|
# Deletes the given object from the set and returns self. If the
|
|
|
|
# object is not in the set, returns nil.
|
2002-09-20 06:46:52 -04:00
|
|
|
def delete?(o)
|
2015-11-16 02:29:37 -05:00
|
|
|
delete(o) if include?(o)
|
2002-08-30 09:47:49 -04:00
|
|
|
end
|
|
|
|
|
2002-12-24 00:29:04 -05:00
|
|
|
# Deletes every element of the set for which block evaluates to
|
2016-05-04 05:07:01 -04:00
|
|
|
# true, and returns self. Returns an enumerator if no block is
|
|
|
|
# given.
|
2002-08-30 09:47:49 -04:00
|
|
|
def delete_if
|
2016-05-04 05:06:59 -04:00
|
|
|
block_given? or return enum_for(__method__) { size }
|
2013-05-19 04:33:27 -04:00
|
|
|
# @hash.delete_if should be faster, but using it breaks the order
|
|
|
|
# of enumeration in subclasses.
|
2013-05-20 09:28:32 -04:00
|
|
|
select { |o| yield o }.each { |o| @hash.delete(o) }
|
2002-08-30 09:47:49 -04:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2010-05-30 09:19:09 -04:00
|
|
|
# Deletes every element of the set for which block evaluates to
|
2016-05-04 05:07:01 -04:00
|
|
|
# false, and returns self. Returns an enumerator if no block is
|
|
|
|
# given.
|
2010-05-30 09:19:09 -04:00
|
|
|
def keep_if
|
2016-05-04 05:06:59 -04:00
|
|
|
block_given? or return enum_for(__method__) { size }
|
2013-05-19 04:33:27 -04:00
|
|
|
# @hash.keep_if should be faster, but using it breaks the order of
|
|
|
|
# enumeration in subclasses.
|
2013-05-20 09:28:32 -04:00
|
|
|
reject { |o| yield o }.each { |o| @hash.delete(o) }
|
2010-05-30 09:19:09 -04:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2020-12-22 07:41:44 -05:00
|
|
|
# Replaces the elements with ones returned by `collect()`.
|
2016-05-04 05:07:01 -04:00
|
|
|
# Returns an enumerator if no block is given.
|
2008-06-03 00:42:32 -04:00
|
|
|
def collect!
|
2016-05-04 05:06:59 -04:00
|
|
|
block_given? or return enum_for(__method__) { size }
|
2017-10-21 13:03:40 -04:00
|
|
|
set = self.class.new
|
|
|
|
each { |o| set << yield(o) }
|
|
|
|
replace(set)
|
2002-09-20 06:46:52 -04:00
|
|
|
end
|
|
|
|
alias map! collect!
|
|
|
|
|
2002-12-24 00:29:04 -05:00
|
|
|
# Equivalent to Set#delete_if, but returns nil if no changes were
|
2016-05-04 05:07:01 -04:00
|
|
|
# made. Returns an enumerator if no block is given.
|
2012-08-31 04:43:09 -04:00
|
|
|
def reject!(&block)
|
2016-05-04 05:06:59 -04:00
|
|
|
block or return enum_for(__method__) { size }
|
2002-09-20 06:46:52 -04:00
|
|
|
n = size
|
2012-08-31 04:43:09 -04:00
|
|
|
delete_if(&block)
|
2015-11-16 02:29:37 -05:00
|
|
|
self if size != n
|
2002-08-30 09:47:49 -04:00
|
|
|
end
|
|
|
|
|
2010-05-30 09:19:09 -04:00
|
|
|
# Equivalent to Set#keep_if, but returns nil if no changes were
|
2016-05-04 05:07:01 -04:00
|
|
|
# made. Returns an enumerator if no block is given.
|
2012-08-31 04:43:09 -04:00
|
|
|
def select!(&block)
|
2016-05-04 05:06:59 -04:00
|
|
|
block or return enum_for(__method__) { size }
|
2010-05-30 09:19:09 -04:00
|
|
|
n = size
|
2012-08-31 04:43:09 -04:00
|
|
|
keep_if(&block)
|
2015-11-16 02:29:37 -05:00
|
|
|
self if size != n
|
2010-05-30 09:19:09 -04:00
|
|
|
end
|
|
|
|
|
2018-02-25 08:52:07 -05:00
|
|
|
# Equivalent to Set#select!
|
|
|
|
alias filter! select!
|
|
|
|
|
2002-12-24 00:29:04 -05:00
|
|
|
# Merges the elements of the given enumerable object to the set and
|
|
|
|
# returns self.
|
2002-08-30 09:47:49 -04:00
|
|
|
def merge(enum)
|
2009-05-01 03:52:09 -04:00
|
|
|
if enum.instance_of?(self.class)
|
|
|
|
@hash.update(enum.instance_variable_get(:@hash))
|
2002-09-20 06:46:52 -04:00
|
|
|
else
|
2010-02-11 12:38:05 -05:00
|
|
|
do_with_enum(enum) { |o| add(o) }
|
2002-09-20 06:46:52 -04:00
|
|
|
end
|
|
|
|
|
2002-08-30 09:47:49 -04:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2002-12-24 00:29:04 -05:00
|
|
|
# Deletes every element that appears in the given enumerable object
|
|
|
|
# and returns self.
|
2002-08-30 09:47:49 -04:00
|
|
|
def subtract(enum)
|
2010-02-11 12:38:05 -05:00
|
|
|
do_with_enum(enum) { |o| delete(o) }
|
2002-08-30 09:47:49 -04:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2002-12-24 00:29:04 -05:00
|
|
|
# Returns a new set built by merging the set and the elements of the
|
|
|
|
# given enumerable object.
|
2017-11-17 04:48:47 -05:00
|
|
|
#
|
2017-11-22 15:58:24 -05:00
|
|
|
# Set[1, 2, 3] | Set[2, 4, 5] #=> #<Set: {1, 2, 3, 4, 5}>
|
2017-11-17 04:48:47 -05:00
|
|
|
# Set[1, 5, 'z'] | (1..6) #=> #<Set: {1, 5, "z", 2, 3, 4, 6}>
|
2002-11-09 13:52:04 -05:00
|
|
|
def |(enum)
|
2002-09-20 06:46:52 -04:00
|
|
|
dup.merge(enum)
|
2002-08-30 09:47:49 -04:00
|
|
|
end
|
2017-11-22 16:13:51 -05:00
|
|
|
alias + |
|
|
|
|
alias union |
|
2002-08-30 09:47:49 -04:00
|
|
|
|
2011-05-18 20:07:25 -04:00
|
|
|
# Returns a new set built by duplicating the set, removing every
|
|
|
|
# element that appears in the given enumerable object.
|
2017-11-17 04:48:47 -05:00
|
|
|
#
|
|
|
|
# Set[1, 3, 5] - Set[1, 5] #=> #<Set: {3}>
|
|
|
|
# Set['a', 'b', 'z'] - ['a', 'c'] #=> #<Set: {"b", "z"}>
|
2011-05-18 20:07:25 -04:00
|
|
|
def -(enum)
|
|
|
|
dup.subtract(enum)
|
|
|
|
end
|
2017-11-22 16:13:51 -05:00
|
|
|
alias difference -
|
2011-05-18 17:19:18 -04:00
|
|
|
|
2011-05-18 20:07:25 -04:00
|
|
|
# Returns a new set containing elements common to the set and the
|
|
|
|
# given enumerable object.
|
2017-11-17 04:48:47 -05:00
|
|
|
#
|
|
|
|
# Set[1, 3, 5] & Set[3, 2, 1] #=> #<Set: {3, 1}>
|
|
|
|
# Set['a', 'b', 'z'] & ['a', 'b', 'c'] #=> #<Set: {"a", "b"}>
|
2011-05-18 20:07:25 -04:00
|
|
|
def &(enum)
|
|
|
|
n = self.class.new
|
2018-11-03 08:27:10 -04:00
|
|
|
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
|
2011-05-18 20:07:25 -04:00
|
|
|
n
|
|
|
|
end
|
2017-11-22 16:13:51 -05:00
|
|
|
alias intersection &
|
2002-08-30 09:47:49 -04:00
|
|
|
|
2011-05-18 20:07:25 -04:00
|
|
|
# Returns a new set containing elements exclusive between the set
|
2020-12-22 07:41:44 -05:00
|
|
|
# and the given enumerable object. `(set ^ enum)` is equivalent to
|
|
|
|
# `((set | enum) - (set & enum))`.
|
2017-11-17 04:48:47 -05:00
|
|
|
#
|
2017-11-22 15:58:24 -05:00
|
|
|
# Set[1, 2] ^ Set[2, 3] #=> #<Set: {3, 1}>
|
2017-11-17 04:48:47 -05:00
|
|
|
# Set[1, 'b', 'c'] ^ ['b', 'd'] #=> #<Set: {"d", 1, "c"}>
|
2011-05-18 20:07:25 -04:00
|
|
|
def ^(enum)
|
|
|
|
n = Set.new(enum)
|
2015-11-16 02:29:37 -05:00
|
|
|
each { |o| n.add(o) unless n.delete?(o) }
|
2011-05-18 20:07:25 -04:00
|
|
|
n
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns true if two sets are equal. The equality of each couple
|
|
|
|
# of elements is defined according to Object#eql?.
|
2017-11-17 04:48:47 -05:00
|
|
|
#
|
|
|
|
# Set[1, 2] == Set[2, 1] #=> true
|
|
|
|
# Set[1, 3, 5] == Set[1, 5] #=> false
|
|
|
|
# Set['a', 'b', 'c'] == Set['a', 'c', 'b'] #=> true
|
|
|
|
# Set['a', 'b', 'c'] == ['a', 'c', 'b'] #=> false
|
2011-05-18 20:07:25 -04:00
|
|
|
def ==(other)
|
|
|
|
if self.equal?(other)
|
|
|
|
true
|
|
|
|
elsif other.instance_of?(self.class)
|
|
|
|
@hash == other.instance_variable_get(:@hash)
|
|
|
|
elsif other.is_a?(Set) && self.size == other.size
|
|
|
|
other.all? { |o| @hash.include?(o) }
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
2002-08-30 09:47:49 -04:00
|
|
|
end
|
|
|
|
|
2011-05-18 20:07:25 -04:00
|
|
|
def hash # :nodoc:
|
|
|
|
@hash.hash
|
|
|
|
end
|
2002-08-30 09:47:49 -04:00
|
|
|
|
2011-05-18 20:07:25 -04:00
|
|
|
def eql?(o) # :nodoc:
|
|
|
|
return false unless o.is_a?(Set)
|
2012-09-01 05:51:48 -04:00
|
|
|
@hash.eql?(o.instance_variable_get(:@hash))
|
2011-05-18 20:07:25 -04:00
|
|
|
end
|
2002-08-30 09:47:49 -04:00
|
|
|
|
2017-10-22 08:25:34 -04:00
|
|
|
# Resets the internal state after modification to existing elements
|
|
|
|
# and returns self.
|
|
|
|
#
|
|
|
|
# Elements will be reindexed and deduplicated.
|
|
|
|
def reset
|
|
|
|
if @hash.respond_to?(:rehash)
|
|
|
|
@hash.rehash # This should perform frozenness check.
|
|
|
|
else
|
2019-01-19 23:44:25 -05:00
|
|
|
raise FrozenError, "can't modify frozen #{self.class.name}" if frozen?
|
2017-10-22 08:25:34 -04:00
|
|
|
end
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2017-10-29 16:59:33 -04:00
|
|
|
# Returns true if the given object is a member of the set,
|
|
|
|
# and false otherwise.
|
2017-09-19 04:45:12 -04:00
|
|
|
#
|
|
|
|
# Used in case statements:
|
|
|
|
#
|
2020-12-22 07:41:44 -05:00
|
|
|
# require 'set'
|
2017-10-29 16:59:33 -04:00
|
|
|
#
|
2020-12-22 07:41:44 -05:00
|
|
|
# case :apple
|
|
|
|
# when Set[:potato, :carrot]
|
|
|
|
# "vegetable"
|
|
|
|
# when Set[:apple, :banana]
|
|
|
|
# "fruit"
|
|
|
|
# end
|
|
|
|
# # => "fruit"
|
2017-09-19 04:45:12 -04:00
|
|
|
#
|
|
|
|
# Or by itself:
|
|
|
|
#
|
2020-12-22 07:41:44 -05:00
|
|
|
# Set[1, 2, 3] === 2 #=> true
|
|
|
|
# Set[1, 2, 3] === 4 #=> false
|
2017-09-19 04:45:12 -04:00
|
|
|
#
|
|
|
|
alias === include?
|
|
|
|
|
2011-05-18 20:07:25 -04:00
|
|
|
# Classifies the set by the return value of the given block and
|
|
|
|
# returns a hash of {value => set of elements} pairs. The block is
|
|
|
|
# called once for each element of the set, passing the element as
|
|
|
|
# parameter.
|
|
|
|
#
|
2020-12-22 07:41:44 -05:00
|
|
|
# require 'set'
|
|
|
|
# files = Set.new(Dir.glob("*.rb"))
|
|
|
|
# hash = files.classify { |f| File.mtime(f).year }
|
|
|
|
# hash #=> {2000=>#<Set: {"a.rb", "b.rb"}>,
|
|
|
|
# # 2001=>#<Set: {"c.rb", "d.rb", "e.rb"}>,
|
|
|
|
# # 2002=>#<Set: {"f.rb"}>}
|
2016-05-04 05:07:01 -04:00
|
|
|
#
|
|
|
|
# Returns an enumerator if no block is given.
|
2011-05-18 20:07:25 -04:00
|
|
|
def classify # :yields: o
|
2016-05-04 05:06:59 -04:00
|
|
|
block_given? or return enum_for(__method__) { size }
|
2002-08-30 09:47:49 -04:00
|
|
|
|
2011-05-18 20:07:25 -04:00
|
|
|
h = {}
|
2002-08-30 09:47:49 -04:00
|
|
|
|
2011-05-18 20:07:25 -04:00
|
|
|
each { |i|
|
2015-11-16 02:29:37 -05:00
|
|
|
(h[yield(i)] ||= self.class.new).add(i)
|
2011-05-18 17:19:18 -04:00
|
|
|
}
|
2002-08-30 09:47:49 -04:00
|
|
|
|
2011-05-18 20:07:25 -04:00
|
|
|
h
|
2002-08-30 09:47:49 -04:00
|
|
|
end
|
|
|
|
|
2011-05-18 20:07:25 -04:00
|
|
|
# Divides the set into a set of subsets according to the commonality
|
|
|
|
# defined by the given block.
|
|
|
|
#
|
|
|
|
# If the arity of the block is 2, elements o1 and o2 are in common
|
|
|
|
# if block.call(o1, o2) is true. Otherwise, elements o1 and o2 are
|
|
|
|
# in common if block.call(o1) == block.call(o2).
|
|
|
|
#
|
2020-12-22 07:41:44 -05:00
|
|
|
# require 'set'
|
|
|
|
# numbers = Set[1, 3, 4, 6, 9, 10, 11]
|
|
|
|
# set = numbers.divide { |i,j| (i - j).abs == 1 }
|
|
|
|
# set #=> #<Set: {#<Set: {1}>,
|
|
|
|
# # #<Set: {11, 9, 10}>,
|
|
|
|
# # #<Set: {3, 4}>,
|
|
|
|
# # #<Set: {6}>}>
|
2016-05-04 05:07:01 -04:00
|
|
|
#
|
|
|
|
# Returns an enumerator if no block is given.
|
2011-05-18 20:07:25 -04:00
|
|
|
def divide(&func)
|
2016-05-04 05:06:59 -04:00
|
|
|
func or return enum_for(__method__) { size }
|
2011-05-18 20:07:25 -04:00
|
|
|
|
|
|
|
if func.arity == 2
|
|
|
|
require 'tsort'
|
|
|
|
|
|
|
|
class << dig = {} # :nodoc:
|
|
|
|
include TSort
|
|
|
|
|
|
|
|
alias tsort_each_node each_key
|
|
|
|
def tsort_each_child(node, &block)
|
|
|
|
fetch(node).each(&block)
|
|
|
|
end
|
|
|
|
end
|
2002-08-30 09:47:49 -04:00
|
|
|
|
2011-05-18 20:07:25 -04:00
|
|
|
each { |u|
|
|
|
|
dig[u] = a = []
|
|
|
|
each{ |v| func.call(u, v) and a << v }
|
|
|
|
}
|
2002-08-30 09:47:49 -04:00
|
|
|
|
2011-05-18 20:07:25 -04:00
|
|
|
set = Set.new()
|
|
|
|
dig.each_strongly_connected_component { |css|
|
|
|
|
set.add(self.class.new(css))
|
|
|
|
}
|
|
|
|
set
|
|
|
|
else
|
|
|
|
Set.new(classify(&func).values)
|
|
|
|
end
|
2002-08-30 09:47:49 -04:00
|
|
|
end
|
|
|
|
|
2020-12-21 22:20:21 -05:00
|
|
|
# Returns a string created by converting each element of the set to a string
|
|
|
|
# See also: Array#join
|
|
|
|
def join(separator=nil)
|
|
|
|
to_a.join(separator)
|
|
|
|
end
|
|
|
|
|
2011-05-18 20:07:25 -04:00
|
|
|
InspectKey = :__inspect_key__ # :nodoc:
|
|
|
|
|
|
|
|
# Returns a string containing a human-readable representation of the
|
2017-11-22 15:58:24 -05:00
|
|
|
# set ("#<Set: {element1, element2, ...}>").
|
2011-05-18 20:07:25 -04:00
|
|
|
def inspect
|
|
|
|
ids = (Thread.current[InspectKey] ||= [])
|
|
|
|
|
|
|
|
if ids.include?(object_id)
|
|
|
|
return sprintf('#<%s: {...}>', self.class.name)
|
|
|
|
end
|
|
|
|
|
2015-11-16 02:41:30 -05:00
|
|
|
ids << object_id
|
2011-05-18 20:07:25 -04:00
|
|
|
begin
|
|
|
|
return sprintf('#<%s: {%s}>', self.class, to_a.inspect[1..-2])
|
|
|
|
ensure
|
|
|
|
ids.pop
|
|
|
|
end
|
2002-08-30 09:47:49 -04:00
|
|
|
end
|
|
|
|
|
2017-07-14 04:46:13 -04:00
|
|
|
alias to_s inspect
|
|
|
|
|
2011-05-18 20:07:25 -04:00
|
|
|
def pretty_print(pp) # :nodoc:
|
2021-05-23 07:10:21 -04:00
|
|
|
pp.group(1, sprintf('#<%s:', self.class.name), '>') {
|
|
|
|
pp.breakable
|
|
|
|
pp.group(1, '{', '}') {
|
|
|
|
pp.seplist(self) { |o|
|
|
|
|
pp.pp o
|
|
|
|
}
|
2011-05-18 20:07:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
2011-05-18 17:19:18 -04:00
|
|
|
|
2011-05-18 20:07:25 -04:00
|
|
|
def pretty_print_cycle(pp) # :nodoc:
|
|
|
|
pp.text sprintf('#<%s: {%s}>', self.class.name, empty? ? '' : '...')
|
|
|
|
end
|
2002-08-30 09:47:49 -04:00
|
|
|
end
|
|
|
|
|
2002-09-20 06:46:52 -04:00
|
|
|
module Enumerable
|
2002-12-24 00:29:04 -05:00
|
|
|
# Makes a set from the enumerable object with given arguments.
|
2020-12-22 07:41:44 -05:00
|
|
|
# Needs to `require "set"` to use this method.
|
2002-09-20 06:46:52 -04:00
|
|
|
def to_set(klass = Set, *args, &block)
|
|
|
|
klass.new(self, *args, &block)
|
2022-02-17 04:02:42 -05:00
|
|
|
end unless method_defined?(:to_set)
|
2002-09-20 06:46:52 -04:00
|
|
|
end
|
2020-12-21 22:20:21 -05:00
|
|
|
|
|
|
|
autoload :SortedSet, "#{__dir__}/set/sorted_set"
|