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