diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2018-09-20 23:56:53 +0900 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2018-09-20 23:56:53 +0900 |
| commit | dd346be99aaf6d0220f067c78decb29548c7dfe6 (patch) | |
| tree | 4433aabb03c3b9c9e38906947e7babf6b7e6c2b0 /mrbgems/mruby-array-ext/mrblib/array.rb | |
| parent | 5ec5a41fd28d6042619f7c9ce899c3cd0ed9362b (diff) | |
| download | mruby-dd346be99aaf6d0220f067c78decb29548c7dfe6.tar.gz mruby-dd346be99aaf6d0220f067c78decb29548c7dfe6.zip | |
Improve performance of `Array#uniq!`.
Diffstat (limited to 'mrbgems/mruby-array-ext/mrblib/array.rb')
| -rw-r--r-- | mrbgems/mruby-array-ext/mrblib/array.rb | 17 |
1 files changed, 10 insertions, 7 deletions
diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb index bb9e61bdd..4f676a121 100644 --- a/mrbgems/mruby-array-ext/mrblib/array.rb +++ b/mrbgems/mruby-array-ext/mrblib/array.rb @@ -41,19 +41,22 @@ class Array # c.uniq! { |s| s.first } # => [["student", "sam"], ["teacher", "matz"]] # def uniq!(&block) - ary = self.dup - result = [] if block hash = {} - while ary.size > 0 - val = ary.shift + self.each do |val| key = block.call(val) - hash[key] = val unless hash.has_key?(key) + hash[key] = val unless hash.key?(key) end - hash.each_value do |value| - result << value + result = hash.values + elsif self.size > 20 + hash = {} + self.each do |val| + hash[val] = val end + result = hash.values else + ary = self.dup + result = [] while ary.size > 0 result << ary.shift ary.delete(result.last) |
