summaryrefslogtreecommitdiff
path: root/lib/set.rb
diff options
context:
space:
mode:
authordrbrain <drbrain@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-05-18 21:19:18 +0000
committerdrbrain <drbrain@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-05-18 21:19:18 +0000
commit7bbf2f308580f468802cd7d32c94fce1b9f1779e (patch)
tree0a680f59e70a78d60e776d7b763e618fb2fec624 /lib/set.rb
parent34276148c4450faa77bb298cfbe005f7c263f802 (diff)
* lib: Convert tabs to spaces for ruby files per
http://redmine.ruby-lang.org/projects/ruby/wiki/DeveloperHowto#coding-style Patch by Steve Klabnik [Ruby 1.9 - Bug #4730] Patch by Jason Dew [Ruby 1.9 - Feature #4718] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31635 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/set.rb')
-rwxr-xr-xlib/set.rb558
1 files changed, 279 insertions, 279 deletions
diff --git a/lib/set.rb b/lib/set.rb
index 226641d..51f4e70 100755
--- a/lib/set.rb
+++ b/lib/set.rb
@@ -92,19 +92,19 @@ class Set
@hash = orig.instance_eval{@hash}.dup
end
- def freeze # :nodoc:
+ def freeze # :nodoc:
super
@hash.freeze
self
end
- def taint # :nodoc:
+ def taint # :nodoc:
super
@hash.taint
self
end
- def untaint # :nodoc:
+ def untaint # :nodoc:
super
@hash.untaint
self
@@ -148,15 +148,15 @@ class Set
def flatten_merge(set, seen = Set.new) # :nodoc:
set.each { |e|
if e.is_a?(Set)
- if seen.include?(e_id = e.object_id)
- raise ArgumentError, "tried to flatten recursive Set"
- end
+ if seen.include?(e_id = e.object_id)
+ raise ArgumentError, "tried to flatten recursive Set"
+ end
- seen.add(e_id)
- flatten_merge(e, seen)
- seen.delete(e_id)
+ seen.add(e_id)
+ flatten_merge(e, seen)
+ seen.delete(e_id)
else
- add(e)
+ add(e)
end
}
@@ -325,161 +325,161 @@ class Set
def |(enum)
dup.merge(enum)
end
- alias + | ##
- alias union | ##
+ alias + | ##
+ alias union | ##
- # Returns a new set built by duplicating the set, removing every
- # element that appears in the given enumerable object.
- def -(enum)
- dup.subtract(enum)
- end
- alias difference - ##
-
- # Returns a new set containing elements common to the set and the
- # given enumerable object.
- def &(enum)
- n = self.class.new
- do_with_enum(enum) { |o| n.add(o) if include?(o) }
- n
- end
- alias intersection & ##
-
- # Returns a new set containing elements exclusive between the set
- # and the given enumerable object. (set ^ enum) is equivalent to
- # ((set | enum) - (set & enum)).
- def ^(enum)
- n = Set.new(enum)
- each { |o| if n.include?(o) then n.delete(o) else n.add(o) end }
- n
- end
-
- # Returns true if two sets are equal. The equality of each couple
- # of elements is defined according to Object#eql?.
- 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
+ # Returns a new set built by duplicating the set, removing every
+ # element that appears in the given enumerable object.
+ def -(enum)
+ dup.subtract(enum)
end
- end
-
- def hash # :nodoc:
- @hash.hash
- end
+ alias difference - ##
+
+ # Returns a new set containing elements common to the set and the
+ # given enumerable object.
+ def &(enum)
+ n = self.class.new
+ do_with_enum(enum) { |o| n.add(o) if include?(o) }
+ n
+ end
+ alias intersection & ##
+
+ # Returns a new set containing elements exclusive between the set
+ # and the given enumerable object. (set ^ enum) is equivalent to
+ # ((set | enum) - (set & enum)).
+ def ^(enum)
+ n = Set.new(enum)
+ each { |o| if n.include?(o) then n.delete(o) else n.add(o) end }
+ n
+end
- def eql?(o) # :nodoc:
- return false unless o.is_a?(Set)
- @hash.eql?(o.instance_eval{@hash})
+# Returns true if two sets are equal. The equality of each couple
+# of elements is defined according to Object#eql?.
+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
+end
- # 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.
- #
- # e.g.:
- #
- # require 'set'
- # files = Set.new(Dir.glob("*.rb"))
- # hash = files.classify { |f| File.mtime(f).year }
- # p hash # => {2000=>#<Set: {"a.rb", "b.rb"}>,
- # # 2001=>#<Set: {"c.rb", "d.rb", "e.rb"}>,
- # # 2002=>#<Set: {"f.rb"}>}
- def classify # :yields: o
- block_given? or return enum_for(__method__)
-
- h = {}
+def hash # :nodoc:
+ @hash.hash
+end
- each { |i|
- x = yield(i)
- (h[x] ||= self.class.new).add(i)
- }
+def eql?(o) # :nodoc:
+ return false unless o.is_a?(Set)
+ @hash.eql?(o.instance_eval{@hash})
+end
- h
- end
+# 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.
+#
+# e.g.:
+#
+# require 'set'
+# files = Set.new(Dir.glob("*.rb"))
+# hash = files.classify { |f| File.mtime(f).year }
+# p hash # => {2000=>#<Set: {"a.rb", "b.rb"}>,
+# # 2001=>#<Set: {"c.rb", "d.rb", "e.rb"}>,
+# # 2002=>#<Set: {"f.rb"}>}
+def classify # :yields: o
+ block_given? or return enum_for(__method__)
+
+ h = {}
+
+ each { |i|
+ x = yield(i)
+ (h[x] ||= self.class.new).add(i)
+ }
+
+ h
+end
- # 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).
- #
- # e.g.:
- #
- # require 'set'
- # numbers = Set[1, 3, 4, 6, 9, 10, 11]
- # set = numbers.divide { |i,j| (i - j).abs == 1 }
- # p set # => #<Set: {#<Set: {1}>,
- # # #<Set: {11, 9, 10}>,
- # # #<Set: {3, 4}>,
- # # #<Set: {6}>}>
- def divide(&func)
- func or return enum_for(__method__)
-
- 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
+# 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).
+#
+# e.g.:
+#
+# require 'set'
+# numbers = Set[1, 3, 4, 6, 9, 10, 11]
+# set = numbers.divide { |i,j| (i - j).abs == 1 }
+# p set # => #<Set: {#<Set: {1}>,
+# # #<Set: {11, 9, 10}>,
+# # #<Set: {3, 4}>,
+# # #<Set: {6}>}>
+def divide(&func)
+ func or return enum_for(__method__)
+
+ 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
- each { |u|
- dig[u] = a = []
- each{ |v| func.call(u, v) and a << v }
- }
+ each { |u|
+ dig[u] = a = []
+ each{ |v| func.call(u, v) and a << v }
+ }
- set = Set.new()
- dig.each_strongly_connected_component { |css|
- set.add(self.class.new(css))
- }
- set
- else
- Set.new(classify(&func).values)
- end
+ set = Set.new()
+ dig.each_strongly_connected_component { |css|
+ set.add(self.class.new(css))
+ }
+ set
+ else
+ Set.new(classify(&func).values)
end
+end
- InspectKey = :__inspect_key__ # :nodoc:
-
- # Returns a string containing a human-readable representation of the
- # set. ("#<Set: {element1, element2, ...}>")
- def inspect
- ids = (Thread.current[InspectKey] ||= [])
+InspectKey = :__inspect_key__ # :nodoc:
- if ids.include?(object_id)
- return sprintf('#<%s: {...}>', self.class.name)
- end
+# Returns a string containing a human-readable representation of the
+# set. ("#<Set: {element1, element2, ...}>")
+def inspect
+ ids = (Thread.current[InspectKey] ||= [])
- begin
- ids << object_id
- return sprintf('#<%s: {%s}>', self.class, to_a.inspect[1..-2])
- ensure
- ids.pop
- end
+ if ids.include?(object_id)
+ return sprintf('#<%s: {...}>', self.class.name)
end
- def pretty_print(pp) # :nodoc:
- pp.text sprintf('#<%s: {', self.class.name)
- pp.nest(1) {
- pp.seplist(self) { |o|
- pp.pp o
- }
- }
- pp.text "}>"
+ begin
+ ids << object_id
+ return sprintf('#<%s: {%s}>', self.class, to_a.inspect[1..-2])
+ ensure
+ ids.pop
end
+end
- def pretty_print_cycle(pp) # :nodoc:
- pp.text sprintf('#<%s: {%s}>', self.class.name, empty? ? '' : '...')
- end
+def pretty_print(pp) # :nodoc:
+ pp.text sprintf('#<%s: {', self.class.name)
+ pp.nest(1) {
+ pp.seplist(self) { |o|
+ pp.pp o
+ }
+ }
+ pp.text "}>"
+end
+
+def pretty_print_cycle(pp) # :nodoc:
+ pp.text sprintf('#<%s: {%s}>', self.class.name, empty? ? '' : '...')
+end
end
#
@@ -515,11 +515,11 @@ class SortedSet < Set
@@setup = false
class << self
- def [](*ary) # :nodoc:
+ def [](*ary) # :nodoc:
new(ary)
end
- def setup # :nodoc:
+ def setup # :nodoc:
@@setup and return
module_eval {
@@ -528,89 +528,89 @@ class SortedSet < Set
remove_method :old_init
}
begin
- require 'rbtree'
-
- module_eval %{
- def initialize(*args, &block)
- @hash = RBTree.new
- super
- end
-
- def add(o)
- o.respond_to?(:<=>) or raise ArgumentError, "value must respond to <=>"
- super
- end
- alias << add
- }
+ require 'rbtree'
+
+ module_eval %{
+ def initialize(*args, &block)
+ @hash = RBTree.new
+ super
+ end
+
+ def add(o)
+ o.respond_to?(:<=>) or raise ArgumentError, "value must respond to <=>"
+ super
+ end
+ alias << add
+ }
rescue LoadError
- module_eval %{
- def initialize(*args, &block)
- @keys = nil
- super
- end
-
- def clear
- @keys = nil
- super
- end
-
- def replace(enum)
- @keys = nil
- super
- end
-
- def add(o)
- o.respond_to?(:<=>) or raise ArgumentError, "value must respond to <=>"
- @keys = nil
- super
- end
- alias << add
-
- def delete(o)
- @keys = nil
- @hash.delete(o)
- self
- end
-
- def delete_if
+ module_eval %{
+ def initialize(*args, &block)
+ @keys = nil
+ super
+ end
+
+ def clear
+ @keys = nil
+ super
+ end
+
+ def replace(enum)
+ @keys = nil
+ super
+ end
+
+ def add(o)
+ o.respond_to?(:<=>) or raise ArgumentError, "value must respond to <=>"
+ @keys = nil
+ super
+ end
+ alias << add
+
+ def delete(o)
+ @keys = nil
+ @hash.delete(o)
+ self
+ end
+
+ def delete_if
block_given? or return enum_for(__method__)
- n = @hash.size
- super
- @keys = nil if @hash.size != n
- self
- end
-
- def keep_if
- block_given? or return enum_for(__method__)
- n = @hash.size
- super
- @keys = nil if @hash.size != n
- self
- end
-
- def merge(enum)
- @keys = nil
- super
- end
-
- def each
- block_given? or return enum_for(__method__)
- to_a.each { |o| yield(o) }
- self
- end
-
- def to_a
- (@keys = @hash.keys).sort! unless @keys
- @keys
- end
- }
+ n = @hash.size
+ super
+ @keys = nil if @hash.size != n
+ self
+ end
+
+ def keep_if
+ block_given? or return enum_for(__method__)
+ n = @hash.size
+ super
+ @keys = nil if @hash.size != n
+ self
+ end
+
+ def merge(enum)
+ @keys = nil
+ super
+ end
+
+ def each
+ block_given? or return enum_for(__method__)
+ to_a.each { |o| yield(o) }
+ self
+ end
+
+ def to_a
+ (@keys = @hash.keys).sort! unless @keys
+ @keys
+ end
+ }
end
@@setup = true
end
end
- def initialize(*args, &block) # :nodoc:
+ def initialize(*args, &block) # :nodoc:
SortedSet.setup
initialize(*args, &block)
end
@@ -657,54 +657,54 @@ end
#
# if @proc.arity == 2
# instance_eval %{
-# def add(o)
-# @hash[o] = true if @proc.call(self, o)
-# self
-# end
-# alias << add
+# def add(o)
+# @hash[o] = true if @proc.call(self, o)
+# self
+# end
+# alias << add
#
-# def add?(o)
-# if include?(o) || !@proc.call(self, o)
-# nil
-# else
-# @hash[o] = true
-# self
-# end
-# end
+# def add?(o)
+# if include?(o) || !@proc.call(self, o)
+# nil
+# else
+# @hash[o] = true
+# self
+# end
+# end
#
-# def replace(enum)
-# enum.respond_to?(:each) or raise ArgumentError, "value must be enumerable"
-# clear
-# enum.each_entry { |o| add(o) }
+# def replace(enum)
+# enum.respond_to?(:each) or raise ArgumentError, "value must be enumerable"
+# clear
+# enum.each_entry { |o| add(o) }
#
-# self
-# end
+# self
+# end
#
-# def merge(enum)
-# enum.respond_to?(:each) or raise ArgumentError, "value must be enumerable"
-# enum.each_entry { |o| add(o) }
+# def merge(enum)
+# enum.respond_to?(:each) or raise ArgumentError, "value must be enumerable"
+# enum.each_entry { |o| add(o) }
#
-# self
-# end
+# self
+# end
# }
# else
# instance_eval %{
-# def add(o)
+# def add(o)
# if @proc.call(o)
-# @hash[o] = true
+# @hash[o] = true
# end
-# self
-# end
-# alias << add
+# self
+# end
+# alias << add
#
-# def add?(o)
-# if include?(o) || !@proc.call(o)
-# nil
-# else
-# @hash[o] = true
-# self
-# end
-# end
+# def add?(o)
+# if include?(o) || !@proc.call(o)
+# nil
+# else
+# @hash[o] = true
+# self
+# end
+# end
# }
# end
#
@@ -835,13 +835,13 @@ class TC_Set < Test::Unit::TestCase
set1 = Set[
1,
Set[
- 5,
- Set[7,
- Set[0]
- ],
- Set[6,2],
- 1
- ],
+ 5,
+ Set[7,
+ Set[0]
+ ],
+ Set[6,2],
+ 1
+ ],
3,
Set[3,4]
]
@@ -1020,8 +1020,8 @@ class TC_Set < Test::Unit::TestCase
assert_nothing_raised {
set.each { |o|
- ary.delete(o) or raise "unexpected element: #{o}"
- }
+ ary.delete(o) or raise "unexpected element: #{o}"
+ }
ary.empty? or raise "forgotten elements: #{ary.join(', ')}"
}
@@ -1085,11 +1085,11 @@ class TC_Set < Test::Unit::TestCase
ret = set.collect! { |i|
case i
when Numeric
- i * 2
+ i * 2
when String
- i.upcase
+ i.upcase
else
- nil
+ nil
end
}
@@ -1168,8 +1168,8 @@ class TC_Set < Test::Unit::TestCase
set2 = Set["a", "b", set1]
set1 = set1.add(set1.clone)
-# assert_equal(set1, set2)
-# assert_equal(set2, set1)
+ # assert_equal(set1, set2)
+ # assert_equal(set2, set1)
assert_equal(set2, set2.clone)
assert_equal(set1.clone, set1)
@@ -1214,15 +1214,15 @@ class TC_Set < Test::Unit::TestCase
assert_equal(set, ret.flatten)
ret.each { |s|
if s.include?(0)
- assert_equal(Set[0,1], s)
+ assert_equal(Set[0,1], s)
elsif s.include?(3)
- assert_equal(Set[3,4,5], s)
+ assert_equal(Set[3,4,5], s)
elsif s.include?(7)
- assert_equal(Set[7], s)
+ assert_equal(Set[7], s)
elsif s.include?(9)
- assert_equal(Set[9,10,11], s)
+ assert_equal(Set[9,10,11], s)
else
- raise "unexpected group: #{s.inspect}"
+ raise "unexpected group: #{s.inspect}"
end
}
end