diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2011-05-19 00:07:25 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2011-05-19 00:07:25 +0000 |
commit | d371e3583e3b1e0692f92343017b62d2628190ff (patch) | |
tree | be82924ada754f8542f394d0a3d28e0a0b02a098 /lib/set.rb | |
parent | 1fae66fca28167ca0e25625091c5df49405b8023 (diff) |
* lib: revert r31635-r31638 and untabify with expand(1).
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31641 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/set.rb')
-rwxr-xr-x | lib/set.rb | 506 |
1 files changed, 253 insertions, 253 deletions
diff --git a/lib/set.rb b/lib/set.rb index 51f4e70ce2..43edc7557a 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 @@ -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 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 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 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 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 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 + end end -end -def hash # :nodoc: - @hash.hash -end + def hash # :nodoc: + @hash.hash + end -def eql?(o) # :nodoc: - return false unless o.is_a?(Set) - @hash.eql?(o.instance_eval{@hash}) -end + def eql?(o) # :nodoc: + return false unless o.is_a?(Set) + @hash.eql?(o.instance_eval{@hash}) + 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 + # 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__) -# 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 + h = {} - each { |u| - dig[u] = a = [] - each{ |v| func.call(u, v) and a << v } + each { |i| + x = yield(i) + (h[x] ||= self.class.new).add(i) } - set = Set.new() - dig.each_strongly_connected_component { |css| - set.add(self.class.new(css)) - } - set - else - Set.new(classify(&func).values) + h end -end -InspectKey = :__inspect_key__ # :nodoc: + # 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 -# Returns a string containing a human-readable representation of the -# set. ("#<Set: {element1, element2, ...}>") -def inspect - ids = (Thread.current[InspectKey] ||= []) + each { |u| + dig[u] = a = [] + each{ |v| func.call(u, v) and a << v } + } - if ids.include?(object_id) - return sprintf('#<%s: {...}>', self.class.name) + set = Set.new() + dig.each_strongly_connected_component { |css| + set.add(self.class.new(css)) + } + set + else + Set.new(classify(&func).values) + end end - begin - ids << object_id - return sprintf('#<%s: {%s}>', self.class, to_a.inspect[1..-2]) - ensure - ids.pop + InspectKey = :__inspect_key__ # :nodoc: + + # Returns a string containing a human-readable representation of the + # set. ("#<Set: {element1, element2, ...}>") + def inspect + ids = (Thread.current[InspectKey] ||= []) + + if ids.include?(object_id) + return sprintf('#<%s: {...}>', self.class.name) + end + + begin + ids << object_id + return sprintf('#<%s: {%s}>', self.class, to_a.inspect[1..-2]) + ensure + ids.pop + end end -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(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 + 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 { @@ -531,78 +531,78 @@ class SortedSet < Set 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 + 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 + 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 + n = @hash.size + super + @keys = nil if @hash.size != n + self + 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 + 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 @@ -610,7 +610,7 @@ class SortedSet < Set 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) || [email protected](self, o) -# nil -# else -# @hash[o] = true -# self -# end -# end +# def add?(o) +# if include?(o) || [email protected](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) || [email protected](o) -# nil -# else -# @hash[o] = true -# self -# end -# end +# def add?(o) +# if include?(o) || [email protected](o) +# nil +# else +# @hash[o] = true +# self +# end +# end # } # end # @@ -838,10 +838,10 @@ class TC_Set < Test::Unit::TestCase 5, Set[7, Set[0] - ], - Set[6,2], - 1 - ], + ], + 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(', ')}" } @@ -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) |