Ignore:
Timestamp:
Jul 9, 2020, 8:51:43 AM (4 years ago)
Author:
coas-nagasima
Message:

mrubyを2.1.1に更新

Location:
EcnlProtoTool/trunk/mruby-2.1.1
Files:
1 edited
1 moved

Legend:

Unmodified
Added
Removed
  • EcnlProtoTool/trunk/mruby-2.1.1/mrblib/array.rb

    r331 r439  
     1# coding: utf-8
    12##
    23# Array
     
    1011  #
    1112  # ISO 15.2.12.5.10
    12   def each(&block)
    13     return to_enum :each unless block_given?
    14 
    15     idx = 0
    16     while idx < length
    17       block.call(self[idx])
    18       idx += 1
    19     end
    20     self
    21   end
     13  # def each(&block)
     14  #   return to_enum :each unless block
     15
     16  #   idx = 0
     17  #   while idx < length
     18  #     block.call(self[idx])
     19  #     idx += 1
     20  #   end
     21  #   self
     22  # end
    2223
    2324  ##
     
    2728  # ISO 15.2.12.5.11
    2829  def each_index(&block)
    29     return to_enum :each_index unless block_given?
     30    return to_enum :each_index unless block
    3031
    3132    idx = 0
     
    4445  # ISO 15.2.12.5.7
    4546  def collect!(&block)
    46     return to_enum :collect! unless block_given?
    47 
    48     self.each_index { |idx| self[idx] = block.call(self[idx]) }
     47    return to_enum :collect! unless block
     48
     49    idx = 0
     50    len = size
     51    while idx < len
     52      self[idx] = block.call self[idx]
     53      idx += 1
     54    end
    4955    self
    5056  end
     
    6167  # ISO 15.2.12.5.15
    6268  def initialize(size=0, obj=nil, &block)
    63     raise TypeError, "expected Integer for 1st argument" unless size.kind_of? Integer
     69    size = size.__to_int
    6470    raise ArgumentError, "negative array size" if size < 0
    6571
     
    7884  end
    7985
    80   def _inspect
    81     return "[]" if self.size == 0
    82     "["+self.map{|x|x.inspect}.join(", ")+"]"
     86  def _inspect(recur_list)
     87    size = self.size
     88    return "[]" if size == 0
     89    return "[...]" if recur_list[self.object_id]
     90    recur_list[self.object_id] = true
     91    ary=[]
     92    i=0
     93    while i<size
     94      ary<<self[i]._inspect(recur_list)
     95      i+=1
     96    end
     97    "["+ary.join(", ")+"]"
    8398  end
    8499  ##
     
    87102  # ISO 15.2.12.5.31 (x)
    88103  def inspect
    89     begin
    90       self._inspect
    91     rescue SystemStackError
    92       "[...]"
    93     end
     104    self._inspect({})
    94105  end
    95106  # ISO 15.2.12.5.32 (x)
     
    179190    ret
    180191  end
    181 
    182   # internal method to convert multi-value to single value
    183   def __svalue
    184     return self.first if self.size < 2
    185     self
    186   end
    187192end
    188193
     
    197202  # elements.
    198203  def sort!(&block)
    199     self.replace(self.sort(&block))
     204    stack = [ [ 0, self.size - 1 ] ]
     205    until stack.empty?
     206      left, mid, right = stack.pop
     207      if right == nil
     208        right = mid
     209        # sort self[left..right]
     210        if left < right
     211          if left + 1 == right
     212            lval = self[left]
     213            rval = self[right]
     214            cmp = if block then block.call(lval,rval) else lval <=> rval end
     215            if cmp.nil?
     216              raise ArgumentError, "comparison of #{lval.inspect} and #{rval.inspect} failed"
     217            end
     218            if cmp > 0
     219              self[left]  = rval
     220              self[right] = lval
     221            end
     222          else
     223            mid = ((left + right + 1) / 2).floor
     224            stack.push [ left, mid, right ]
     225            stack.push [ mid, right ]
     226            stack.push [ left, (mid - 1) ] if left < mid - 1
     227          end
     228        end
     229      else
     230        lary = self[left, mid - left]
     231        lsize = lary.size
     232
     233        # The entity sharing between lary and self may cause a large memory
     234        # copy operation in the merge loop below.  This harmless operation
     235        # cancels the sharing and provides a huge performance gain.
     236        lary[0] = lary[0]
     237
     238        # merge
     239        lidx = 0
     240        ridx = mid
     241        (left..right).each { |i|
     242          if lidx >= lsize
     243            break
     244          elsif ridx > right
     245            self[i, lsize - lidx] = lary[lidx, lsize - lidx]
     246            break
     247          else
     248            lval = lary[lidx]
     249            rval = self[ridx]
     250            cmp = if block then block.call(lval,rval) else lval <=> rval end
     251            if cmp.nil?
     252              raise ArgumentError, "comparison of #{lval.inspect} and #{rval.inspect} failed"
     253            end
     254            if cmp <= 0
     255              self[i] = lval
     256              lidx += 1
     257            else
     258              self[i] = rval
     259              ridx += 1
     260            end
     261          end
     262        }
     263      end
     264    end
     265    self
     266  end
     267
     268  def sort(&block)
     269    self.dup.sort!(&block)
    200270  end
    201271end
Note: See TracChangeset for help on using the changeset viewer.