Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/smanonr..../opt/alt/ruby31/share/ruby
File: set.rb
# frozen_string_literal: true
[0] Fix | Delete
# :markup: markdown
[1] Fix | Delete
#
[2] Fix | Delete
# set.rb - defines the Set class
[3] Fix | Delete
#
[4] Fix | Delete
# Copyright (c) 2002-2020 Akinori MUSHA <knu@iDaemons.org>
[5] Fix | Delete
#
[6] Fix | Delete
# Documentation by Akinori MUSHA and Gavin Sinclair.
[7] Fix | Delete
#
[8] Fix | Delete
# All rights reserved. You can redistribute and/or modify it under the same
[9] Fix | Delete
# terms as Ruby.
[10] Fix | Delete
[11] Fix | Delete
[12] Fix | Delete
##
[13] Fix | Delete
# This library provides the Set class, which deals with a collection
[14] Fix | Delete
# of unordered values with no duplicates. It is a hybrid of Array's
[15] Fix | Delete
# intuitive inter-operation facilities and Hash's fast lookup.
[16] Fix | Delete
#
[17] Fix | Delete
# The method `to_set` is added to Enumerable for convenience.
[18] Fix | Delete
#
[19] Fix | Delete
# Set implements a collection of unordered values with no duplicates.
[20] Fix | Delete
# This is a hybrid of Array's intuitive inter-operation facilities and
[21] Fix | Delete
# Hash's fast lookup.
[22] Fix | Delete
#
[23] Fix | Delete
# Set is easy to use with Enumerable objects (implementing `each`).
[24] Fix | Delete
# Most of the initializer methods and binary operators accept generic
[25] Fix | Delete
# Enumerable objects besides sets and arrays. An Enumerable object
[26] Fix | Delete
# can be converted to Set using the `to_set` method.
[27] Fix | Delete
#
[28] Fix | Delete
# Set uses Hash as storage, so you must note the following points:
[29] Fix | Delete
#
[30] Fix | Delete
# * Equality of elements is determined according to Object#eql? and
[31] Fix | Delete
# Object#hash. Use Set#compare_by_identity to make a set compare
[32] Fix | Delete
# its elements by their identity.
[33] Fix | Delete
# * Set assumes that the identity of each element does not change
[34] Fix | Delete
# while it is stored. Modifying an element of a set will render the
[35] Fix | Delete
# set to an unreliable state.
[36] Fix | Delete
# * When a string is to be stored, a frozen copy of the string is
[37] Fix | Delete
# stored instead unless the original string is already frozen.
[38] Fix | Delete
#
[39] Fix | Delete
# ## Comparison
[40] Fix | Delete
#
[41] Fix | Delete
# The comparison operators `<`, `>`, `<=`, and `>=` are implemented as
[42] Fix | Delete
# shorthand for the {proper_,}{subset?,superset?} methods. The `<=>`
[43] Fix | Delete
# operator reflects this order, or return `nil` for sets that both
[44] Fix | Delete
# have distinct elements (`{x, y}` vs. `{x, z}` for example).
[45] Fix | Delete
#
[46] Fix | Delete
# ## Example
[47] Fix | Delete
#
[48] Fix | Delete
# ```ruby
[49] Fix | Delete
# require 'set'
[50] Fix | Delete
# s1 = Set[1, 2] #=> #<Set: {1, 2}>
[51] Fix | Delete
# s2 = [1, 2].to_set #=> #<Set: {1, 2}>
[52] Fix | Delete
# s1 == s2 #=> true
[53] Fix | Delete
# s1.add("foo") #=> #<Set: {1, 2, "foo"}>
[54] Fix | Delete
# s1.merge([2, 6]) #=> #<Set: {1, 2, "foo", 6}>
[55] Fix | Delete
# s1.subset?(s2) #=> false
[56] Fix | Delete
# s2.subset?(s1) #=> true
[57] Fix | Delete
# ```
[58] Fix | Delete
#
[59] Fix | Delete
# ## Contact
[60] Fix | Delete
#
[61] Fix | Delete
# - Akinori MUSHA <<knu@iDaemons.org>> (current maintainer)
[62] Fix | Delete
#
[63] Fix | Delete
# ## What's Here
[64] Fix | Delete
#
[65] Fix | Delete
# First, what's elsewhere. \Class \Set:
[66] Fix | Delete
#
[67] Fix | Delete
# - Inherits from {class Object}[https://docs.ruby-lang.org/en/master/Object.html#class-Object-label-What-27s+Here].
[68] Fix | Delete
# - Includes {module Enumerable}[https://docs.ruby-lang.org/en/master/Enumerable.html#module-Enumerable-label-What-27s+Here],
[69] Fix | Delete
# which provides dozens of additional methods.
[70] Fix | Delete
#
[71] Fix | Delete
# In particular, class \Set does not have many methods of its own
[72] Fix | Delete
# for fetching or for iterating.
[73] Fix | Delete
# Instead, it relies on those in \Enumerable.
[74] Fix | Delete
#
[75] Fix | Delete
# Here, class \Set provides methods that are useful for:
[76] Fix | Delete
#
[77] Fix | Delete
# - [Creating a Set](#class-Set-label-Methods+for+Creating+a+Set)
[78] Fix | Delete
# - [Set Operations](#class-Set-label-Methods+for+Set+Operations)
[79] Fix | Delete
# - [Comparing](#class-Set-label-Methods+for+Comparing)
[80] Fix | Delete
# - [Querying](#class-Set-label-Methods+for+Querying)
[81] Fix | Delete
# - [Assigning](#class-Set-label-Methods+for+Assigning)
[82] Fix | Delete
# - [Deleting](#class-Set-label-Methods+for+Deleting)
[83] Fix | Delete
# - [Converting](#class-Set-label-Methods+for+Converting)
[84] Fix | Delete
# - [Iterating](#class-Set-label-Methods+for+Iterating)
[85] Fix | Delete
# - [And more....](#class-Set-label-Other+Methods)
[86] Fix | Delete
#
[87] Fix | Delete
# ### Methods for Creating a \Set
[88] Fix | Delete
#
[89] Fix | Delete
# - ::[] -
[90] Fix | Delete
# Returns a new set containing the given objects.
[91] Fix | Delete
# - ::new -
[92] Fix | Delete
# Returns a new set containing either the given objects
[93] Fix | Delete
# (if no block given) or the return values from the called block
[94] Fix | Delete
# (if a block given).
[95] Fix | Delete
#
[96] Fix | Delete
# ### Methods for \Set Operations
[97] Fix | Delete
#
[98] Fix | Delete
# - [|](#method-i-7C) (aliased as #union and #+) -
[99] Fix | Delete
# Returns a new set containing all elements from +self+
[100] Fix | Delete
# and all elements from a given enumerable (no duplicates).
[101] Fix | Delete
# - [&](#method-i-26) (aliased as #intersection) -
[102] Fix | Delete
# Returns a new set containing all elements common to +self+
[103] Fix | Delete
# and a given enumerable.
[104] Fix | Delete
# - [-](#method-i-2D) (aliased as #difference) -
[105] Fix | Delete
# Returns a copy of +self+ with all elements
[106] Fix | Delete
# in a given enumerable removed.
[107] Fix | Delete
# - [\^](#method-i-5E) -
[108] Fix | Delete
# Returns a new set containing all elements from +self+
[109] Fix | Delete
# and a given enumerable except those common to both.
[110] Fix | Delete
#
[111] Fix | Delete
# ### Methods for Comparing
[112] Fix | Delete
#
[113] Fix | Delete
# - [<=>](#method-i-3C-3D-3E) -
[114] Fix | Delete
# Returns -1, 0, or 1 as +self+ is less than, equal to,
[115] Fix | Delete
# or greater than a given object.
[116] Fix | Delete
# - [==](#method-i-3D-3D) -
[117] Fix | Delete
# Returns whether +self+ and a given enumerable are equal,
[118] Fix | Delete
# as determined by Object#eql?.
[119] Fix | Delete
# - \#compare_by_identity? -
[120] Fix | Delete
# Returns whether the set considers only identity
[121] Fix | Delete
# when comparing elements.
[122] Fix | Delete
#
[123] Fix | Delete
# ### Methods for Querying
[124] Fix | Delete
#
[125] Fix | Delete
# - \#length (aliased as #size) -
[126] Fix | Delete
# Returns the count of elements.
[127] Fix | Delete
# - \#empty? -
[128] Fix | Delete
# Returns whether the set has no elements.
[129] Fix | Delete
# - \#include? (aliased as #member? and #===) -
[130] Fix | Delete
# Returns whether a given object is an element in the set.
[131] Fix | Delete
# - \#subset? (aliased as [<=](#method-i-3C-3D)) -
[132] Fix | Delete
# Returns whether a given object is a subset of the set.
[133] Fix | Delete
# - \#proper_subset? (aliased as [<](#method-i-3C)) -
[134] Fix | Delete
# Returns whether a given enumerable is a proper subset of the set.
[135] Fix | Delete
# - \#superset? (aliased as [<=](#method-i-3E-3D])) -
[136] Fix | Delete
# Returns whether a given enumerable is a superset of the set.
[137] Fix | Delete
# - \#proper_superset? (aliased as [>](#method-i-3E)) -
[138] Fix | Delete
# Returns whether a given enumerable is a proper superset of the set.
[139] Fix | Delete
# - \#disjoint? -
[140] Fix | Delete
# Returns +true+ if the set and a given enumerable
[141] Fix | Delete
# have no common elements, +false+ otherwise.
[142] Fix | Delete
# - \#intersect? -
[143] Fix | Delete
# Returns +true+ if the set and a given enumerable -
[144] Fix | Delete
# have any common elements, +false+ otherwise.
[145] Fix | Delete
# - \#compare_by_identity? -
[146] Fix | Delete
# Returns whether the set considers only identity
[147] Fix | Delete
# when comparing elements.
[148] Fix | Delete
#
[149] Fix | Delete
# ### Methods for Assigning
[150] Fix | Delete
#
[151] Fix | Delete
# - \#add (aliased as #<<) -
[152] Fix | Delete
# Adds a given object to the set; returns +self+.
[153] Fix | Delete
# - \#add? -
[154] Fix | Delete
# If the given object is not an element in the set,
[155] Fix | Delete
# adds it and returns +self+; otherwise, returns +nil+.
[156] Fix | Delete
# - \#merge -
[157] Fix | Delete
# Adds each given object to the set; returns +self+.
[158] Fix | Delete
# - \#replace -
[159] Fix | Delete
# Replaces the contents of the set with the contents
[160] Fix | Delete
# of a given enumerable.
[161] Fix | Delete
#
[162] Fix | Delete
# ### Methods for Deleting
[163] Fix | Delete
#
[164] Fix | Delete
# - \#clear -
[165] Fix | Delete
# Removes all elements in the set; returns +self+.
[166] Fix | Delete
# - \#delete -
[167] Fix | Delete
# Removes a given object from the set; returns +self+.
[168] Fix | Delete
# - \#delete? -
[169] Fix | Delete
# If the given object is an element in the set,
[170] Fix | Delete
# removes it and returns +self+; otherwise, returns +nil+.
[171] Fix | Delete
# - \#subtract -
[172] Fix | Delete
# Removes each given object from the set; returns +self+.
[173] Fix | Delete
# - \#delete_if - Removes elements specified by a given block.
[174] Fix | Delete
# - \#select! (aliased as #filter!) -
[175] Fix | Delete
# Removes elements not specified by a given block.
[176] Fix | Delete
# - \#keep_if -
[177] Fix | Delete
# Removes elements not specified by a given block.
[178] Fix | Delete
# - \#reject!
[179] Fix | Delete
# Removes elements specified by a given block.
[180] Fix | Delete
#
[181] Fix | Delete
# ### Methods for Converting
[182] Fix | Delete
#
[183] Fix | Delete
# - \#classify -
[184] Fix | Delete
# Returns a hash that classifies the elements,
[185] Fix | Delete
# as determined by the given block.
[186] Fix | Delete
# - \#collect! (aliased as #map!) -
[187] Fix | Delete
# Replaces each element with a block return-value.
[188] Fix | Delete
# - \#divide -
[189] Fix | Delete
# Returns a hash that classifies the elements,
[190] Fix | Delete
# as determined by the given block;
[191] Fix | Delete
# differs from #classify in that the block may accept
[192] Fix | Delete
# either one or two arguments.
[193] Fix | Delete
# - \#flatten -
[194] Fix | Delete
# Returns a new set that is a recursive flattening of +self+.
[195] Fix | Delete
# \#flatten! -
[196] Fix | Delete
# Replaces each nested set in +self+ with the elements from that set.
[197] Fix | Delete
# - \#inspect (aliased as #to_s) -
[198] Fix | Delete
# Returns a string displaying the elements.
[199] Fix | Delete
# - \#join -
[200] Fix | Delete
# Returns a string containing all elements, converted to strings
[201] Fix | Delete
# as needed, and joined by the given record separator.
[202] Fix | Delete
# - \#to_a -
[203] Fix | Delete
# Returns an array containing all set elements.
[204] Fix | Delete
# - \#to_set -
[205] Fix | Delete
# Returns +self+ if given no arguments and no block;
[206] Fix | Delete
# with a block given, returns a new set consisting of block
[207] Fix | Delete
# return values.
[208] Fix | Delete
#
[209] Fix | Delete
# ### Methods for Iterating
[210] Fix | Delete
#
[211] Fix | Delete
# - \#each -
[212] Fix | Delete
# Calls the block with each successive element; returns +self+.
[213] Fix | Delete
#
[214] Fix | Delete
# ### Other Methods
[215] Fix | Delete
#
[216] Fix | Delete
# - \#reset -
[217] Fix | Delete
# Resets the internal state; useful if an object
[218] Fix | Delete
# has been modified while an element in the set.
[219] Fix | Delete
#
[220] Fix | Delete
class Set
[221] Fix | Delete
include Enumerable
[222] Fix | Delete
[223] Fix | Delete
# Creates a new set containing the given objects.
[224] Fix | Delete
#
[225] Fix | Delete
# Set[1, 2] # => #<Set: {1, 2}>
[226] Fix | Delete
# Set[1, 2, 1] # => #<Set: {1, 2}>
[227] Fix | Delete
# Set[1, 'c', :s] # => #<Set: {1, "c", :s}>
[228] Fix | Delete
def self.[](*ary)
[229] Fix | Delete
new(ary)
[230] Fix | Delete
end
[231] Fix | Delete
[232] Fix | Delete
# Creates a new set containing the elements of the given enumerable
[233] Fix | Delete
# object.
[234] Fix | Delete
#
[235] Fix | Delete
# If a block is given, the elements of enum are preprocessed by the
[236] Fix | Delete
# given block.
[237] Fix | Delete
#
[238] Fix | Delete
# Set.new([1, 2]) #=> #<Set: {1, 2}>
[239] Fix | Delete
# Set.new([1, 2, 1]) #=> #<Set: {1, 2}>
[240] Fix | Delete
# Set.new([1, 'c', :s]) #=> #<Set: {1, "c", :s}>
[241] Fix | Delete
# Set.new(1..5) #=> #<Set: {1, 2, 3, 4, 5}>
[242] Fix | Delete
# Set.new([1, 2, 3]) { |x| x * x } #=> #<Set: {1, 4, 9}>
[243] Fix | Delete
def initialize(enum = nil, &block) # :yields: o
[244] Fix | Delete
@hash ||= Hash.new(false)
[245] Fix | Delete
[246] Fix | Delete
enum.nil? and return
[247] Fix | Delete
[248] Fix | Delete
if block
[249] Fix | Delete
do_with_enum(enum) { |o| add(block[o]) }
[250] Fix | Delete
else
[251] Fix | Delete
merge(enum)
[252] Fix | Delete
end
[253] Fix | Delete
end
[254] Fix | Delete
[255] Fix | Delete
# Makes the set compare its elements by their identity and returns
[256] Fix | Delete
# self. This method may not be supported by all subclasses of Set.
[257] Fix | Delete
def compare_by_identity
[258] Fix | Delete
if @hash.respond_to?(:compare_by_identity)
[259] Fix | Delete
@hash.compare_by_identity
[260] Fix | Delete
self
[261] Fix | Delete
else
[262] Fix | Delete
raise NotImplementedError, "#{self.class.name}\##{__method__} is not implemented"
[263] Fix | Delete
end
[264] Fix | Delete
end
[265] Fix | Delete
[266] Fix | Delete
# Returns true if the set will compare its elements by their
[267] Fix | Delete
# identity. Also see Set#compare_by_identity.
[268] Fix | Delete
def compare_by_identity?
[269] Fix | Delete
@hash.respond_to?(:compare_by_identity?) && @hash.compare_by_identity?
[270] Fix | Delete
end
[271] Fix | Delete
[272] Fix | Delete
def do_with_enum(enum, &block) # :nodoc:
[273] Fix | Delete
if enum.respond_to?(:each_entry)
[274] Fix | Delete
enum.each_entry(&block) if block
[275] Fix | Delete
elsif enum.respond_to?(:each)
[276] Fix | Delete
enum.each(&block) if block
[277] Fix | Delete
else
[278] Fix | Delete
raise ArgumentError, "value must be enumerable"
[279] Fix | Delete
end
[280] Fix | Delete
end
[281] Fix | Delete
private :do_with_enum
[282] Fix | Delete
[283] Fix | Delete
# Dup internal hash.
[284] Fix | Delete
def initialize_dup(orig)
[285] Fix | Delete
super
[286] Fix | Delete
@hash = orig.instance_variable_get(:@hash).dup
[287] Fix | Delete
end
[288] Fix | Delete
[289] Fix | Delete
if Kernel.instance_method(:initialize_clone).arity != 1
[290] Fix | Delete
# Clone internal hash.
[291] Fix | Delete
def initialize_clone(orig, **options)
[292] Fix | Delete
super
[293] Fix | Delete
@hash = orig.instance_variable_get(:@hash).clone(**options)
[294] Fix | Delete
end
[295] Fix | Delete
else
[296] Fix | Delete
# Clone internal hash.
[297] Fix | Delete
def initialize_clone(orig)
[298] Fix | Delete
super
[299] Fix | Delete
@hash = orig.instance_variable_get(:@hash).clone
[300] Fix | Delete
end
[301] Fix | Delete
end
[302] Fix | Delete
[303] Fix | Delete
def freeze # :nodoc:
[304] Fix | Delete
@hash.freeze
[305] Fix | Delete
super
[306] Fix | Delete
end
[307] Fix | Delete
[308] Fix | Delete
# Returns the number of elements.
[309] Fix | Delete
def size
[310] Fix | Delete
@hash.size
[311] Fix | Delete
end
[312] Fix | Delete
alias length size
[313] Fix | Delete
[314] Fix | Delete
# Returns true if the set contains no elements.
[315] Fix | Delete
def empty?
[316] Fix | Delete
@hash.empty?
[317] Fix | Delete
end
[318] Fix | Delete
[319] Fix | Delete
# Removes all elements and returns self.
[320] Fix | Delete
#
[321] Fix | Delete
# set = Set[1, 'c', :s] #=> #<Set: {1, "c", :s}>
[322] Fix | Delete
# set.clear #=> #<Set: {}>
[323] Fix | Delete
# set #=> #<Set: {}>
[324] Fix | Delete
def clear
[325] Fix | Delete
@hash.clear
[326] Fix | Delete
self
[327] Fix | Delete
end
[328] Fix | Delete
[329] Fix | Delete
# Replaces the contents of the set with the contents of the given
[330] Fix | Delete
# enumerable object and returns self.
[331] Fix | Delete
#
[332] Fix | Delete
# set = Set[1, 'c', :s] #=> #<Set: {1, "c", :s}>
[333] Fix | Delete
# set.replace([1, 2]) #=> #<Set: {1, 2}>
[334] Fix | Delete
# set #=> #<Set: {1, 2}>
[335] Fix | Delete
def replace(enum)
[336] Fix | Delete
if enum.instance_of?(self.class)
[337] Fix | Delete
@hash.replace(enum.instance_variable_get(:@hash))
[338] Fix | Delete
self
[339] Fix | Delete
else
[340] Fix | Delete
do_with_enum(enum) # make sure enum is enumerable before calling clear
[341] Fix | Delete
clear
[342] Fix | Delete
merge(enum)
[343] Fix | Delete
end
[344] Fix | Delete
end
[345] Fix | Delete
[346] Fix | Delete
# Converts the set to an array. The order of elements is uncertain.
[347] Fix | Delete
#
[348] Fix | Delete
# Set[1, 2].to_a #=> [1, 2]
[349] Fix | Delete
# Set[1, 'c', :s].to_a #=> [1, "c", :s]
[350] Fix | Delete
def to_a
[351] Fix | Delete
@hash.keys
[352] Fix | Delete
end
[353] Fix | Delete
[354] Fix | Delete
# Returns self if no arguments are given. Otherwise, converts the
[355] Fix | Delete
# set to another with `klass.new(self, *args, &block)`.
[356] Fix | Delete
#
[357] Fix | Delete
# In subclasses, returns `klass.new(self, *args, &block)` unless
[358] Fix | Delete
# overridden.
[359] Fix | Delete
def to_set(klass = Set, *args, &block)
[360] Fix | Delete
return self if instance_of?(Set) && klass == Set && block.nil? && args.empty?
[361] Fix | Delete
klass.new(self, *args, &block)
[362] Fix | Delete
end
[363] Fix | Delete
[364] Fix | Delete
def flatten_merge(set, seen = Set.new) # :nodoc:
[365] Fix | Delete
set.each { |e|
[366] Fix | Delete
if e.is_a?(Set)
[367] Fix | Delete
if seen.include?(e_id = e.object_id)
[368] Fix | Delete
raise ArgumentError, "tried to flatten recursive Set"
[369] Fix | Delete
end
[370] Fix | Delete
[371] Fix | Delete
seen.add(e_id)
[372] Fix | Delete
flatten_merge(e, seen)
[373] Fix | Delete
seen.delete(e_id)
[374] Fix | Delete
else
[375] Fix | Delete
add(e)
[376] Fix | Delete
end
[377] Fix | Delete
}
[378] Fix | Delete
[379] Fix | Delete
self
[380] Fix | Delete
end
[381] Fix | Delete
protected :flatten_merge
[382] Fix | Delete
[383] Fix | Delete
# Returns a new set that is a copy of the set, flattening each
[384] Fix | Delete
# containing set recursively.
[385] Fix | Delete
def flatten
[386] Fix | Delete
self.class.new.flatten_merge(self)
[387] Fix | Delete
end
[388] Fix | Delete
[389] Fix | Delete
# Equivalent to Set#flatten, but replaces the receiver with the
[390] Fix | Delete
# result in place. Returns nil if no modifications were made.
[391] Fix | Delete
def flatten!
[392] Fix | Delete
replace(flatten()) if any? { |e| e.is_a?(Set) }
[393] Fix | Delete
end
[394] Fix | Delete
[395] Fix | Delete
# Returns true if the set contains the given object.
[396] Fix | Delete
#
[397] Fix | Delete
# Note that <code>include?</code> and <code>member?</code> do not test member
[398] Fix | Delete
# equality using <code>==</code> as do other Enumerables.
[399] Fix | Delete
#
[400] Fix | Delete
# See also Enumerable#include?
[401] Fix | Delete
def include?(o)
[402] Fix | Delete
@hash[o]
[403] Fix | Delete
end
[404] Fix | Delete
alias member? include?
[405] Fix | Delete
[406] Fix | Delete
# Returns true if the set is a superset of the given set.
[407] Fix | Delete
def superset?(set)
[408] Fix | Delete
case
[409] Fix | Delete
when set.instance_of?(self.class) && @hash.respond_to?(:>=)
[410] Fix | Delete
@hash >= set.instance_variable_get(:@hash)
[411] Fix | Delete
when set.is_a?(Set)
[412] Fix | Delete
size >= set.size && set.all? { |o| include?(o) }
[413] Fix | Delete
else
[414] Fix | Delete
raise ArgumentError, "value must be a set"
[415] Fix | Delete
end
[416] Fix | Delete
end
[417] Fix | Delete
alias >= superset?
[418] Fix | Delete
[419] Fix | Delete
# Returns true if the set is a proper superset of the given set.
[420] Fix | Delete
def proper_superset?(set)
[421] Fix | Delete
case
[422] Fix | Delete
when set.instance_of?(self.class) && @hash.respond_to?(:>)
[423] Fix | Delete
@hash > set.instance_variable_get(:@hash)
[424] Fix | Delete
when set.is_a?(Set)
[425] Fix | Delete
size > set.size && set.all? { |o| include?(o) }
[426] Fix | Delete
else
[427] Fix | Delete
raise ArgumentError, "value must be a set"
[428] Fix | Delete
end
[429] Fix | Delete
end
[430] Fix | Delete
alias > proper_superset?
[431] Fix | Delete
[432] Fix | Delete
# Returns true if the set is a subset of the given set.
[433] Fix | Delete
def subset?(set)
[434] Fix | Delete
case
[435] Fix | Delete
when set.instance_of?(self.class) && @hash.respond_to?(:<=)
[436] Fix | Delete
@hash <= set.instance_variable_get(:@hash)
[437] Fix | Delete
when set.is_a?(Set)
[438] Fix | Delete
size <= set.size && all? { |o| set.include?(o) }
[439] Fix | Delete
else
[440] Fix | Delete
raise ArgumentError, "value must be a set"
[441] Fix | Delete
end
[442] Fix | Delete
end
[443] Fix | Delete
alias <= subset?
[444] Fix | Delete
[445] Fix | Delete
# Returns true if the set is a proper subset of the given set.
[446] Fix | Delete
def proper_subset?(set)
[447] Fix | Delete
case
[448] Fix | Delete
when set.instance_of?(self.class) && @hash.respond_to?(:<)
[449] Fix | Delete
@hash < set.instance_variable_get(:@hash)
[450] Fix | Delete
when set.is_a?(Set)
[451] Fix | Delete
size < set.size && all? { |o| set.include?(o) }
[452] Fix | Delete
else
[453] Fix | Delete
raise ArgumentError, "value must be a set"
[454] Fix | Delete
end
[455] Fix | Delete
end
[456] Fix | Delete
alias < proper_subset?
[457] Fix | Delete
[458] Fix | Delete
# Returns 0 if the set are equal,
[459] Fix | Delete
# -1 / +1 if the set is a proper subset / superset of the given set,
[460] Fix | Delete
# or nil if they both have unique elements.
[461] Fix | Delete
def <=>(set)
[462] Fix | Delete
return unless set.is_a?(Set)
[463] Fix | Delete
[464] Fix | Delete
case size <=> set.size
[465] Fix | Delete
when -1 then -1 if proper_subset?(set)
[466] Fix | Delete
when +1 then +1 if proper_superset?(set)
[467] Fix | Delete
else 0 if self.==(set)
[468] Fix | Delete
end
[469] Fix | Delete
end
[470] Fix | Delete
[471] Fix | Delete
# Returns true if the set and the given enumerable have at least one
[472] Fix | Delete
# element in common.
[473] Fix | Delete
#
[474] Fix | Delete
# Set[1, 2, 3].intersect? Set[4, 5] #=> false
[475] Fix | Delete
# Set[1, 2, 3].intersect? Set[3, 4] #=> true
[476] Fix | Delete
# Set[1, 2, 3].intersect? 4..5 #=> false
[477] Fix | Delete
# Set[1, 2, 3].intersect? [3, 4] #=> true
[478] Fix | Delete
def intersect?(set)
[479] Fix | Delete
case set
[480] Fix | Delete
when Set
[481] Fix | Delete
if size < set.size
[482] Fix | Delete
any? { |o| set.include?(o) }
[483] Fix | Delete
else
[484] Fix | Delete
set.any? { |o| include?(o) }
[485] Fix | Delete
end
[486] Fix | Delete
when Enumerable
[487] Fix | Delete
set.any? { |o| include?(o) }
[488] Fix | Delete
else
[489] Fix | Delete
raise ArgumentError, "value must be enumerable"
[490] Fix | Delete
end
[491] Fix | Delete
end
[492] Fix | Delete
[493] Fix | Delete
# Returns true if the set and the given enumerable have
[494] Fix | Delete
# no element in common. This method is the opposite of `intersect?`.
[495] Fix | Delete
#
[496] Fix | Delete
# Set[1, 2, 3].disjoint? Set[3, 4] #=> false
[497] Fix | Delete
# Set[1, 2, 3].disjoint? Set[4, 5] #=> true
[498] Fix | Delete
# Set[1, 2, 3].disjoint? [3, 4] #=> false
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function