* lib/set.rb (Hash#flatten!, #add?, #delete?, #collect!, #reject!,
#select!, #^, #classify): Micro-optimize some methods for performance and readability. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52591 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
468cf2704c
commit
4f018492d3
@ -1,3 +1,9 @@
|
|||||||
|
Mon Nov 16 16:28:30 2015 Akinori MUSHA <knu@iDaemons.org>
|
||||||
|
|
||||||
|
* lib/set.rb (Hash#flatten!, #add?, #delete?, #collect!, #reject!,
|
||||||
|
#select!, #^, #classify): Micro-optimize some methods for
|
||||||
|
performance and readability.
|
||||||
|
|
||||||
Mon Nov 16 16:17:58 2015 SHIBATA Hiroshi <hsbt@ruby-lang.org>
|
Mon Nov 16 16:17:58 2015 SHIBATA Hiroshi <hsbt@ruby-lang.org>
|
||||||
|
|
||||||
* ChangeLog: fixed accidentally commit.
|
* ChangeLog: fixed accidentally commit.
|
||||||
|
31
lib/set.rb
31
lib/set.rb
@ -200,11 +200,7 @@ class Set
|
|||||||
# Equivalent to Set#flatten, but replaces the receiver with the
|
# Equivalent to Set#flatten, but replaces the receiver with the
|
||||||
# result in place. Returns nil if no modifications were made.
|
# result in place. Returns nil if no modifications were made.
|
||||||
def flatten!
|
def flatten!
|
||||||
if detect { |e| e.is_a?(Set) }
|
replace(flatten()) if any? { |e| e.is_a?(Set) }
|
||||||
replace(flatten())
|
|
||||||
else
|
|
||||||
nil
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns true if the set contains the given object.
|
# Returns true if the set contains the given object.
|
||||||
@ -320,11 +316,7 @@ class Set
|
|||||||
# Adds the given object to the set and returns self. If the
|
# Adds the given object to the set and returns self. If the
|
||||||
# object is already in the set, returns nil.
|
# object is already in the set, returns nil.
|
||||||
def add?(o)
|
def add?(o)
|
||||||
if include?(o)
|
add(o) unless include?(o)
|
||||||
nil
|
|
||||||
else
|
|
||||||
add(o)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Deletes the given object from the set and returns self. Use +subtract+ to
|
# Deletes the given object from the set and returns self. Use +subtract+ to
|
||||||
@ -337,11 +329,7 @@ class Set
|
|||||||
# Deletes the given object from the set and returns self. If the
|
# Deletes the given object from the set and returns self. If the
|
||||||
# object is not in the set, returns nil.
|
# object is not in the set, returns nil.
|
||||||
def delete?(o)
|
def delete?(o)
|
||||||
if include?(o)
|
delete(o) if include?(o)
|
||||||
delete(o)
|
|
||||||
else
|
|
||||||
nil
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Deletes every element of the set for which block evaluates to
|
# Deletes every element of the set for which block evaluates to
|
||||||
@ -367,9 +355,7 @@ class Set
|
|||||||
# Replaces the elements with ones returned by collect().
|
# Replaces the elements with ones returned by collect().
|
||||||
def collect!
|
def collect!
|
||||||
block_given? or return enum_for(__method__)
|
block_given? or return enum_for(__method__)
|
||||||
set = self.class.new
|
replace(self.class.new(self) { |o| yield(o) })
|
||||||
each { |o| set << yield(o) }
|
|
||||||
replace(set)
|
|
||||||
end
|
end
|
||||||
alias map! collect!
|
alias map! collect!
|
||||||
|
|
||||||
@ -379,7 +365,7 @@ class Set
|
|||||||
block or return enum_for(__method__)
|
block or return enum_for(__method__)
|
||||||
n = size
|
n = size
|
||||||
delete_if(&block)
|
delete_if(&block)
|
||||||
size == n ? nil : self
|
self if size != n
|
||||||
end
|
end
|
||||||
|
|
||||||
# Equivalent to Set#keep_if, but returns nil if no changes were
|
# Equivalent to Set#keep_if, but returns nil if no changes were
|
||||||
@ -388,7 +374,7 @@ class Set
|
|||||||
block or return enum_for(__method__)
|
block or return enum_for(__method__)
|
||||||
n = size
|
n = size
|
||||||
keep_if(&block)
|
keep_if(&block)
|
||||||
size == n ? nil : self
|
self if size != n
|
||||||
end
|
end
|
||||||
|
|
||||||
# Merges the elements of the given enumerable object to the set and
|
# Merges the elements of the given enumerable object to the set and
|
||||||
@ -439,7 +425,7 @@ class Set
|
|||||||
# ((set | enum) - (set & enum)).
|
# ((set | enum) - (set & enum)).
|
||||||
def ^(enum)
|
def ^(enum)
|
||||||
n = Set.new(enum)
|
n = Set.new(enum)
|
||||||
each { |o| if n.include?(o) then n.delete(o) else n.add(o) end }
|
each { |o| n.add(o) unless n.delete?(o) }
|
||||||
n
|
n
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -485,8 +471,7 @@ class Set
|
|||||||
h = {}
|
h = {}
|
||||||
|
|
||||||
each { |i|
|
each { |i|
|
||||||
x = yield(i)
|
(h[yield(i)] ||= self.class.new).add(i)
|
||||||
(h[x] ||= self.class.new).add(i)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
h
|
h
|
||||||
|
Loading…
x
Reference in New Issue
Block a user