Commit 6e9a088e authored by Utkarsh Kukreti's avatar Utkarsh Kukreti

Speed up Array#select! from O(n^2) to O(n).

parent 220f3124
......@@ -675,16 +675,11 @@ class Array
def select!(&block)
return to_enum :select! unless block_given?
idx = 0
len = self.size
while idx < self.size do
if block.call(self[idx])
idx += 1
else
self.delete_at(idx)
end
result = []
self.each do |x|
result << x if block.call(x)
end
return nil if self.size == len
self
return nil if self.size == result.size
self.replace(result)
end
end
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