Add index to larger segment lists for performance

parent 8ffd4e47
# coding: cp932
class Array class Array
## ##
# call-seq: # call-seq:
...@@ -41,26 +42,19 @@ class Array ...@@ -41,26 +42,19 @@ class Array
# c.uniq! { |s| s.first } # => [["student", "sam"], ["teacher", "matz"]] # c.uniq! { |s| s.first } # => [["student", "sam"], ["teacher", "matz"]]
# #
def uniq!(&block) def uniq!(&block)
hash = {}
if block if block
hash = {}
self.each do |val| self.each do |val|
key = block.call(val) key = block.call(val)
hash[key] = val unless hash.key?(key) hash[key] = val unless hash.key?(key)
end end
result = hash.values result = hash.values
elsif self.size > 20 else
hash = {} hash = {}
self.each do |val| self.each do |val|
hash[val] = val hash[val] = val
end end
result = hash.values result = hash.keys
else
ary = self.dup
result = []
while ary.size > 0
result << ary.shift
ary.delete(result.last)
end
end end
if result.size == self.size if result.size == self.size
nil nil
......
...@@ -334,7 +334,7 @@ class Hash ...@@ -334,7 +334,7 @@ class Hash
# h["AA"] #=> "b" # h["AA"] #=> "b"
# #
def rehash def rehash
self.size # do nothing (for now)
self self
end end
end end
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment