diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2017-07-25 14:42:38 +0900 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2017-07-25 14:42:38 +0900 |
| commit | 7b8d784040f1fd88fce2658b704b300f7421c885 (patch) | |
| tree | b0713b02d0c5f403749997c3874266748134c03f /mrblib/enum.rb | |
| parent | 7f9e3336473f13d0e459cdedba665a6bf14f877f (diff) | |
| download | mruby-7b8d784040f1fd88fce2658b704b300f7421c885.tar.gz mruby-7b8d784040f1fd88fce2658b704b300f7421c885.zip | |
Reimplement sort method to reduce array copying.
Diffstat (limited to 'mrblib/enum.rb')
| -rw-r--r-- | mrblib/enum.rb | 46 |
1 files changed, 1 insertions, 45 deletions
diff --git a/mrblib/enum.rb b/mrblib/enum.rb index fb55efcf8..12bd1d37b 100644 --- a/mrblib/enum.rb +++ b/mrblib/enum.rb @@ -316,45 +316,6 @@ module Enumerable alias select find_all ## - # TODO - # Does this OK? Please test it. - def __sort_sub__(sorted, work, src_ary, head, tail, &block) - if head == tail - sorted[head] = work[head] if src_ary == 1 - return - end - - # on current step, which is a src ary? - if src_ary == 0 - src, dst = sorted, work - else - src, dst = work, sorted - end - - key = src[head] # key value for dividing values - i, j = head, tail # position to store on the dst ary - - (head + 1).upto(tail){|idx| - if ((block)? block.call(src[idx], key): (src[idx] <=> key)) > 0 - # larger than key - dst[j] = src[idx] - j -= 1 - else - dst[i] = src[idx] - i += 1 - end - } - - sorted[i] = key - - # sort each sub-array - src_ary = (src_ary + 1) % 2 # exchange a src ary - __sort_sub__(sorted, work, src_ary, head, i - 1, &block) if i > head - __sort_sub__(sorted, work, src_ary, i + 1, tail, &block) if i < tail - end -# private :__sort_sub__ - - ## # Return a sorted array of all elements # which are yield by +each+. If no block # is given <=> will be invoked on each @@ -364,12 +325,7 @@ module Enumerable # # ISO 15.3.2.2.19 def sort(&block) - ary = [] - self.each{|*val| ary.push(val.__svalue)} - if ary.size > 1 - __sort_sub__(ary, ::Array.new(ary.size), 0, 0, ary.size - 1, &block) - end - ary + self.map{|*val| val.__svalue}.sort end ## |
