Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/alt/ruby18/lib64/ruby/1.8
File: set.rb
#!/usr/bin/env ruby
[0] Fix | Delete
#--
[1] Fix | Delete
# set.rb - defines the Set class
[2] Fix | Delete
#++
[3] Fix | Delete
# Copyright (c) 2002-2008 Akinori MUSHA <knu@iDaemons.org>
[4] Fix | Delete
#
[5] Fix | Delete
# Documentation by Akinori MUSHA and Gavin Sinclair.
[6] Fix | Delete
#
[7] Fix | Delete
# All rights reserved. You can redistribute and/or modify it under the same
[8] Fix | Delete
# terms as Ruby.
[9] Fix | Delete
#
[10] Fix | Delete
# $Id: set.rb 17051 2008-06-09 09:20:43Z knu $
[11] Fix | Delete
#
[12] Fix | Delete
# == Overview
[13] Fix | Delete
#
[14] Fix | Delete
# This library provides the Set class, which deals with a collection
[15] Fix | Delete
# of unordered values with no duplicates. It is a hybrid of Array's
[16] Fix | Delete
# intuitive inter-operation facilities and Hash's fast lookup. If you
[17] Fix | Delete
# need to keep values ordered, use the SortedSet class.
[18] Fix | Delete
#
[19] Fix | Delete
# The method +to_set+ is added to Enumerable for convenience.
[20] Fix | Delete
#
[21] Fix | Delete
# See the Set class for an example of usage.
[22] Fix | Delete
[23] Fix | Delete
[24] Fix | Delete
#
[25] Fix | Delete
# Set implements a collection of unordered values with no duplicates.
[26] Fix | Delete
# This is a hybrid of Array's intuitive inter-operation facilities and
[27] Fix | Delete
# Hash's fast lookup.
[28] Fix | Delete
#
[29] Fix | Delete
# Several methods accept any Enumerable object (implementing +each+)
[30] Fix | Delete
# for greater flexibility: new, replace, merge, subtract, |, &, -, ^.
[31] Fix | Delete
#
[32] Fix | Delete
# The equality of each couple of elements is determined according to
[33] Fix | Delete
# Object#eql? and Object#hash, since Set uses Hash as storage.
[34] Fix | Delete
#
[35] Fix | Delete
# Finally, if you are using class Set, you can also use Enumerable#to_set
[36] Fix | Delete
# for convenience.
[37] Fix | Delete
#
[38] Fix | Delete
# == Example
[39] Fix | Delete
#
[40] Fix | Delete
# require 'set'
[41] Fix | Delete
# s1 = Set.new [1, 2] # -> #<Set: {1, 2}>
[42] Fix | Delete
# s2 = [1, 2].to_set # -> #<Set: {1, 2}>
[43] Fix | Delete
# s1 == s2 # -> true
[44] Fix | Delete
# s1.add("foo") # -> #<Set: {1, 2, "foo"}>
[45] Fix | Delete
# s1.merge([2, 6]) # -> #<Set: {6, 1, 2, "foo"}>
[46] Fix | Delete
# s1.subset? s2 # -> false
[47] Fix | Delete
# s2.subset? s1 # -> true
[48] Fix | Delete
#
[49] Fix | Delete
# == Contact
[50] Fix | Delete
#
[51] Fix | Delete
# - Akinori MUSHA <knu@iDaemons.org> (current maintainer)
[52] Fix | Delete
#
[53] Fix | Delete
class Set
[54] Fix | Delete
include Enumerable
[55] Fix | Delete
[56] Fix | Delete
# Creates a new set containing the given objects.
[57] Fix | Delete
def self.[](*ary)
[58] Fix | Delete
new(ary)
[59] Fix | Delete
end
[60] Fix | Delete
[61] Fix | Delete
# Creates a new set containing the elements of the given enumerable
[62] Fix | Delete
# object.
[63] Fix | Delete
#
[64] Fix | Delete
# If a block is given, the elements of enum are preprocessed by the
[65] Fix | Delete
# given block.
[66] Fix | Delete
def initialize(enum = nil, &block) # :yields: o
[67] Fix | Delete
@hash ||= Hash.new
[68] Fix | Delete
[69] Fix | Delete
enum.nil? and return
[70] Fix | Delete
[71] Fix | Delete
if block
[72] Fix | Delete
enum.each { |o| add(block[o]) }
[73] Fix | Delete
else
[74] Fix | Delete
merge(enum)
[75] Fix | Delete
end
[76] Fix | Delete
end
[77] Fix | Delete
[78] Fix | Delete
# Copy internal hash.
[79] Fix | Delete
def initialize_copy(orig)
[80] Fix | Delete
@hash = orig.instance_eval{@hash}.dup
[81] Fix | Delete
end
[82] Fix | Delete
[83] Fix | Delete
# Returns the number of elements.
[84] Fix | Delete
def size
[85] Fix | Delete
@hash.size
[86] Fix | Delete
end
[87] Fix | Delete
alias length size
[88] Fix | Delete
[89] Fix | Delete
# Returns true if the set contains no elements.
[90] Fix | Delete
def empty?
[91] Fix | Delete
@hash.empty?
[92] Fix | Delete
end
[93] Fix | Delete
[94] Fix | Delete
# Removes all elements and returns self.
[95] Fix | Delete
def clear
[96] Fix | Delete
@hash.clear
[97] Fix | Delete
self
[98] Fix | Delete
end
[99] Fix | Delete
[100] Fix | Delete
# Replaces the contents of the set with the contents of the given
[101] Fix | Delete
# enumerable object and returns self.
[102] Fix | Delete
def replace(enum)
[103] Fix | Delete
if enum.class == self.class
[104] Fix | Delete
@hash.replace(enum.instance_eval { @hash })
[105] Fix | Delete
else
[106] Fix | Delete
enum.is_a?(Enumerable) or raise ArgumentError, "value must be enumerable"
[107] Fix | Delete
clear
[108] Fix | Delete
enum.each { |o| add(o) }
[109] Fix | Delete
end
[110] Fix | Delete
[111] Fix | Delete
self
[112] Fix | Delete
end
[113] Fix | Delete
[114] Fix | Delete
# Converts the set to an array. The order of elements is uncertain.
[115] Fix | Delete
def to_a
[116] Fix | Delete
@hash.keys
[117] Fix | Delete
end
[118] Fix | Delete
[119] Fix | Delete
def flatten_merge(set, seen = Set.new)
[120] Fix | Delete
set.each { |e|
[121] Fix | Delete
if e.is_a?(Set)
[122] Fix | Delete
if seen.include?(e_id = e.object_id)
[123] Fix | Delete
raise ArgumentError, "tried to flatten recursive Set"
[124] Fix | Delete
end
[125] Fix | Delete
[126] Fix | Delete
seen.add(e_id)
[127] Fix | Delete
flatten_merge(e, seen)
[128] Fix | Delete
seen.delete(e_id)
[129] Fix | Delete
else
[130] Fix | Delete
add(e)
[131] Fix | Delete
end
[132] Fix | Delete
}
[133] Fix | Delete
[134] Fix | Delete
self
[135] Fix | Delete
end
[136] Fix | Delete
protected :flatten_merge
[137] Fix | Delete
[138] Fix | Delete
# Returns a new set that is a copy of the set, flattening each
[139] Fix | Delete
# containing set recursively.
[140] Fix | Delete
def flatten
[141] Fix | Delete
self.class.new.flatten_merge(self)
[142] Fix | Delete
end
[143] Fix | Delete
[144] Fix | Delete
# Equivalent to Set#flatten, but replaces the receiver with the
[145] Fix | Delete
# result in place. Returns nil if no modifications were made.
[146] Fix | Delete
def flatten!
[147] Fix | Delete
if detect { |e| e.is_a?(Set) }
[148] Fix | Delete
replace(flatten())
[149] Fix | Delete
else
[150] Fix | Delete
nil
[151] Fix | Delete
end
[152] Fix | Delete
end
[153] Fix | Delete
[154] Fix | Delete
# Returns true if the set contains the given object.
[155] Fix | Delete
def include?(o)
[156] Fix | Delete
@hash.include?(o)
[157] Fix | Delete
end
[158] Fix | Delete
alias member? include?
[159] Fix | Delete
[160] Fix | Delete
# Returns true if the set is a superset of the given set.
[161] Fix | Delete
def superset?(set)
[162] Fix | Delete
set.is_a?(Set) or raise ArgumentError, "value must be a set"
[163] Fix | Delete
return false if size < set.size
[164] Fix | Delete
set.all? { |o| include?(o) }
[165] Fix | Delete
end
[166] Fix | Delete
[167] Fix | Delete
# Returns true if the set is a proper superset of the given set.
[168] Fix | Delete
def proper_superset?(set)
[169] Fix | Delete
set.is_a?(Set) or raise ArgumentError, "value must be a set"
[170] Fix | Delete
return false if size <= set.size
[171] Fix | Delete
set.all? { |o| include?(o) }
[172] Fix | Delete
end
[173] Fix | Delete
[174] Fix | Delete
# Returns true if the set is a subset of the given set.
[175] Fix | Delete
def subset?(set)
[176] Fix | Delete
set.is_a?(Set) or raise ArgumentError, "value must be a set"
[177] Fix | Delete
return false if set.size < size
[178] Fix | Delete
all? { |o| set.include?(o) }
[179] Fix | Delete
end
[180] Fix | Delete
[181] Fix | Delete
# Returns true if the set is a proper subset of the given set.
[182] Fix | Delete
def proper_subset?(set)
[183] Fix | Delete
set.is_a?(Set) or raise ArgumentError, "value must be a set"
[184] Fix | Delete
return false if set.size <= size
[185] Fix | Delete
all? { |o| set.include?(o) }
[186] Fix | Delete
end
[187] Fix | Delete
[188] Fix | Delete
# Calls the given block once for each element in the set, passing
[189] Fix | Delete
# the element as parameter. Returns an enumerator if no block is
[190] Fix | Delete
# given.
[191] Fix | Delete
def each
[192] Fix | Delete
block_given? or return enum_for(__method__)
[193] Fix | Delete
@hash.each_key { |o| yield(o) }
[194] Fix | Delete
self
[195] Fix | Delete
end
[196] Fix | Delete
[197] Fix | Delete
# Adds the given object to the set and returns self. Use +merge+ to
[198] Fix | Delete
# add several elements at once.
[199] Fix | Delete
def add(o)
[200] Fix | Delete
@hash[o] = true
[201] Fix | Delete
self
[202] Fix | Delete
end
[203] Fix | Delete
alias << add
[204] Fix | Delete
[205] Fix | Delete
# Adds the given object to the set and returns self. If the
[206] Fix | Delete
# object is already in the set, returns nil.
[207] Fix | Delete
def add?(o)
[208] Fix | Delete
if include?(o)
[209] Fix | Delete
nil
[210] Fix | Delete
else
[211] Fix | Delete
add(o)
[212] Fix | Delete
end
[213] Fix | Delete
end
[214] Fix | Delete
[215] Fix | Delete
# Deletes the given object from the set and returns self. Use +subtract+ to
[216] Fix | Delete
# delete several items at once.
[217] Fix | Delete
def delete(o)
[218] Fix | Delete
@hash.delete(o)
[219] Fix | Delete
self
[220] Fix | Delete
end
[221] Fix | Delete
[222] Fix | Delete
# Deletes the given object from the set and returns self. If the
[223] Fix | Delete
# object is not in the set, returns nil.
[224] Fix | Delete
def delete?(o)
[225] Fix | Delete
if include?(o)
[226] Fix | Delete
delete(o)
[227] Fix | Delete
else
[228] Fix | Delete
nil
[229] Fix | Delete
end
[230] Fix | Delete
end
[231] Fix | Delete
[232] Fix | Delete
# Deletes every element of the set for which block evaluates to
[233] Fix | Delete
# true, and returns self.
[234] Fix | Delete
def delete_if
[235] Fix | Delete
to_a.each { |o| @hash.delete(o) if yield(o) }
[236] Fix | Delete
self
[237] Fix | Delete
end
[238] Fix | Delete
[239] Fix | Delete
# Do collect() destructively.
[240] Fix | Delete
def collect!
[241] Fix | Delete
set = self.class.new
[242] Fix | Delete
each { |o| set << yield(o) }
[243] Fix | Delete
replace(set)
[244] Fix | Delete
end
[245] Fix | Delete
alias map! collect!
[246] Fix | Delete
[247] Fix | Delete
# Equivalent to Set#delete_if, but returns nil if no changes were
[248] Fix | Delete
# made.
[249] Fix | Delete
def reject!
[250] Fix | Delete
n = size
[251] Fix | Delete
delete_if { |o| yield(o) }
[252] Fix | Delete
size == n ? nil : self
[253] Fix | Delete
end
[254] Fix | Delete
[255] Fix | Delete
# Merges the elements of the given enumerable object to the set and
[256] Fix | Delete
# returns self.
[257] Fix | Delete
def merge(enum)
[258] Fix | Delete
if enum.is_a?(Set)
[259] Fix | Delete
@hash.update(enum.instance_eval { @hash })
[260] Fix | Delete
else
[261] Fix | Delete
enum.is_a?(Enumerable) or raise ArgumentError, "value must be enumerable"
[262] Fix | Delete
enum.each { |o| add(o) }
[263] Fix | Delete
end
[264] Fix | Delete
[265] Fix | Delete
self
[266] Fix | Delete
end
[267] Fix | Delete
[268] Fix | Delete
# Deletes every element that appears in the given enumerable object
[269] Fix | Delete
# and returns self.
[270] Fix | Delete
def subtract(enum)
[271] Fix | Delete
enum.is_a?(Enumerable) or raise ArgumentError, "value must be enumerable"
[272] Fix | Delete
enum.each { |o| delete(o) }
[273] Fix | Delete
self
[274] Fix | Delete
end
[275] Fix | Delete
[276] Fix | Delete
# Returns a new set built by merging the set and the elements of the
[277] Fix | Delete
# given enumerable object.
[278] Fix | Delete
def |(enum)
[279] Fix | Delete
enum.is_a?(Enumerable) or raise ArgumentError, "value must be enumerable"
[280] Fix | Delete
dup.merge(enum)
[281] Fix | Delete
end
[282] Fix | Delete
alias + | ##
[283] Fix | Delete
alias union | ##
[284] Fix | Delete
[285] Fix | Delete
# Returns a new set built by duplicating the set, removing every
[286] Fix | Delete
# element that appears in the given enumerable object.
[287] Fix | Delete
def -(enum)
[288] Fix | Delete
enum.is_a?(Enumerable) or raise ArgumentError, "value must be enumerable"
[289] Fix | Delete
dup.subtract(enum)
[290] Fix | Delete
end
[291] Fix | Delete
alias difference - ##
[292] Fix | Delete
[293] Fix | Delete
# Returns a new set containing elements common to the set and the
[294] Fix | Delete
# given enumerable object.
[295] Fix | Delete
def &(enum)
[296] Fix | Delete
enum.is_a?(Enumerable) or raise ArgumentError, "value must be enumerable"
[297] Fix | Delete
n = self.class.new
[298] Fix | Delete
enum.each { |o| n.add(o) if include?(o) }
[299] Fix | Delete
n
[300] Fix | Delete
end
[301] Fix | Delete
alias intersection & ##
[302] Fix | Delete
[303] Fix | Delete
# Returns a new set containing elements exclusive between the set
[304] Fix | Delete
# and the given enumerable object. (set ^ enum) is equivalent to
[305] Fix | Delete
# ((set | enum) - (set & enum)).
[306] Fix | Delete
def ^(enum)
[307] Fix | Delete
enum.is_a?(Enumerable) or raise ArgumentError, "value must be enumerable"
[308] Fix | Delete
n = Set.new(enum)
[309] Fix | Delete
each { |o| if n.include?(o) then n.delete(o) else n.add(o) end }
[310] Fix | Delete
n
[311] Fix | Delete
end
[312] Fix | Delete
[313] Fix | Delete
# Returns true if two sets are equal. The equality of each couple
[314] Fix | Delete
# of elements is defined according to Object#eql?.
[315] Fix | Delete
def ==(set)
[316] Fix | Delete
equal?(set) and return true
[317] Fix | Delete
[318] Fix | Delete
set.is_a?(Set) && size == set.size or return false
[319] Fix | Delete
[320] Fix | Delete
hash = @hash.dup
[321] Fix | Delete
set.all? { |o| hash.include?(o) }
[322] Fix | Delete
end
[323] Fix | Delete
[324] Fix | Delete
def hash # :nodoc:
[325] Fix | Delete
@hash.hash
[326] Fix | Delete
end
[327] Fix | Delete
[328] Fix | Delete
def eql?(o) # :nodoc:
[329] Fix | Delete
return false unless o.is_a?(Set)
[330] Fix | Delete
@hash.eql?(o.instance_eval{@hash})
[331] Fix | Delete
end
[332] Fix | Delete
[333] Fix | Delete
# Classifies the set by the return value of the given block and
[334] Fix | Delete
# returns a hash of {value => set of elements} pairs. The block is
[335] Fix | Delete
# called once for each element of the set, passing the element as
[336] Fix | Delete
# parameter.
[337] Fix | Delete
#
[338] Fix | Delete
# e.g.:
[339] Fix | Delete
#
[340] Fix | Delete
# require 'set'
[341] Fix | Delete
# files = Set.new(Dir.glob("*.rb"))
[342] Fix | Delete
# hash = files.classify { |f| File.mtime(f).year }
[343] Fix | Delete
# p hash # => {2000=>#<Set: {"a.rb", "b.rb"}>,
[344] Fix | Delete
# # 2001=>#<Set: {"c.rb", "d.rb", "e.rb"}>,
[345] Fix | Delete
# # 2002=>#<Set: {"f.rb"}>}
[346] Fix | Delete
def classify # :yields: o
[347] Fix | Delete
h = {}
[348] Fix | Delete
[349] Fix | Delete
each { |i|
[350] Fix | Delete
x = yield(i)
[351] Fix | Delete
(h[x] ||= self.class.new).add(i)
[352] Fix | Delete
}
[353] Fix | Delete
[354] Fix | Delete
h
[355] Fix | Delete
end
[356] Fix | Delete
[357] Fix | Delete
# Divides the set into a set of subsets according to the commonality
[358] Fix | Delete
# defined by the given block.
[359] Fix | Delete
#
[360] Fix | Delete
# If the arity of the block is 2, elements o1 and o2 are in common
[361] Fix | Delete
# if block.call(o1, o2) is true. Otherwise, elements o1 and o2 are
[362] Fix | Delete
# in common if block.call(o1) == block.call(o2).
[363] Fix | Delete
#
[364] Fix | Delete
# e.g.:
[365] Fix | Delete
#
[366] Fix | Delete
# require 'set'
[367] Fix | Delete
# numbers = Set[1, 3, 4, 6, 9, 10, 11]
[368] Fix | Delete
# set = numbers.divide { |i,j| (i - j).abs == 1 }
[369] Fix | Delete
# p set # => #<Set: {#<Set: {1}>,
[370] Fix | Delete
# # #<Set: {11, 9, 10}>,
[371] Fix | Delete
# # #<Set: {3, 4}>,
[372] Fix | Delete
# # #<Set: {6}>}>
[373] Fix | Delete
def divide(&func)
[374] Fix | Delete
if func.arity == 2
[375] Fix | Delete
require 'tsort'
[376] Fix | Delete
[377] Fix | Delete
class << dig = {} # :nodoc:
[378] Fix | Delete
include TSort
[379] Fix | Delete
[380] Fix | Delete
alias tsort_each_node each_key
[381] Fix | Delete
def tsort_each_child(node, &block)
[382] Fix | Delete
fetch(node).each(&block)
[383] Fix | Delete
end
[384] Fix | Delete
end
[385] Fix | Delete
[386] Fix | Delete
each { |u|
[387] Fix | Delete
dig[u] = a = []
[388] Fix | Delete
each{ |v| func.call(u, v) and a << v }
[389] Fix | Delete
}
[390] Fix | Delete
[391] Fix | Delete
set = Set.new()
[392] Fix | Delete
dig.each_strongly_connected_component { |css|
[393] Fix | Delete
set.add(self.class.new(css))
[394] Fix | Delete
}
[395] Fix | Delete
set
[396] Fix | Delete
else
[397] Fix | Delete
Set.new(classify(&func).values)
[398] Fix | Delete
end
[399] Fix | Delete
end
[400] Fix | Delete
[401] Fix | Delete
InspectKey = :__inspect_key__ # :nodoc:
[402] Fix | Delete
[403] Fix | Delete
# Returns a string containing a human-readable representation of the
[404] Fix | Delete
# set. ("#<Set: {element1, element2, ...}>")
[405] Fix | Delete
def inspect
[406] Fix | Delete
ids = (Thread.current[InspectKey] ||= [])
[407] Fix | Delete
[408] Fix | Delete
if ids.include?(object_id)
[409] Fix | Delete
return sprintf('#<%s: {...}>', self.class.name)
[410] Fix | Delete
end
[411] Fix | Delete
[412] Fix | Delete
begin
[413] Fix | Delete
ids << object_id
[414] Fix | Delete
return sprintf('#<%s: {%s}>', self.class, to_a.inspect[1..-2])
[415] Fix | Delete
ensure
[416] Fix | Delete
ids.pop
[417] Fix | Delete
end
[418] Fix | Delete
end
[419] Fix | Delete
[420] Fix | Delete
def pretty_print(pp) # :nodoc:
[421] Fix | Delete
pp.text sprintf('#<%s: {', self.class.name)
[422] Fix | Delete
pp.nest(1) {
[423] Fix | Delete
pp.seplist(self) { |o|
[424] Fix | Delete
pp.pp o
[425] Fix | Delete
}
[426] Fix | Delete
}
[427] Fix | Delete
pp.text "}>"
[428] Fix | Delete
end
[429] Fix | Delete
[430] Fix | Delete
def pretty_print_cycle(pp) # :nodoc:
[431] Fix | Delete
pp.text sprintf('#<%s: {%s}>', self.class.name, empty? ? '' : '...')
[432] Fix | Delete
end
[433] Fix | Delete
end
[434] Fix | Delete
[435] Fix | Delete
# SortedSet implements a set which elements are sorted in order. See Set.
[436] Fix | Delete
class SortedSet < Set
[437] Fix | Delete
@@setup = false
[438] Fix | Delete
[439] Fix | Delete
class << self
[440] Fix | Delete
def [](*ary) # :nodoc:
[441] Fix | Delete
new(ary)
[442] Fix | Delete
end
[443] Fix | Delete
[444] Fix | Delete
def setup # :nodoc:
[445] Fix | Delete
@@setup and return
[446] Fix | Delete
[447] Fix | Delete
module_eval {
[448] Fix | Delete
# a hack to shut up warning
[449] Fix | Delete
alias old_init initialize
[450] Fix | Delete
remove_method :old_init
[451] Fix | Delete
}
[452] Fix | Delete
begin
[453] Fix | Delete
require 'rbtree'
[454] Fix | Delete
[455] Fix | Delete
module_eval %{
[456] Fix | Delete
def initialize(*args, &block)
[457] Fix | Delete
@hash = RBTree.new
[458] Fix | Delete
super
[459] Fix | Delete
end
[460] Fix | Delete
}
[461] Fix | Delete
rescue LoadError
[462] Fix | Delete
module_eval %{
[463] Fix | Delete
def initialize(*args, &block)
[464] Fix | Delete
@keys = nil
[465] Fix | Delete
super
[466] Fix | Delete
end
[467] Fix | Delete
[468] Fix | Delete
def clear
[469] Fix | Delete
@keys = nil
[470] Fix | Delete
super
[471] Fix | Delete
end
[472] Fix | Delete
[473] Fix | Delete
def replace(enum)
[474] Fix | Delete
@keys = nil
[475] Fix | Delete
super
[476] Fix | Delete
end
[477] Fix | Delete
[478] Fix | Delete
def add(o)
[479] Fix | Delete
@keys = nil
[480] Fix | Delete
@hash[o] = true
[481] Fix | Delete
self
[482] Fix | Delete
end
[483] Fix | Delete
alias << add
[484] Fix | Delete
[485] Fix | Delete
def delete(o)
[486] Fix | Delete
@keys = nil
[487] Fix | Delete
@hash.delete(o)
[488] Fix | Delete
self
[489] Fix | Delete
end
[490] Fix | Delete
[491] Fix | Delete
def delete_if
[492] Fix | Delete
n = @hash.size
[493] Fix | Delete
super
[494] Fix | Delete
@keys = nil if @hash.size != n
[495] Fix | Delete
self
[496] Fix | Delete
end
[497] Fix | Delete
[498] Fix | Delete
def merge(enum)
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function