Changeset 439 for EcnlProtoTool/trunk/mruby-2.1.1/mrblib/array.rb
- Timestamp:
- Jul 9, 2020, 8:51:43 AM (4 years ago)
- 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 1 2 ## 2 3 # Array … … 10 11 # 11 12 # ISO 15.2.12.5.10 12 def each(&block)13 return to_enum :each unless block_given?14 15 idx = 016 while idx < length17 block.call(self[idx])18 idx += 119 end20 self21 end13 # 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 22 23 23 24 ## … … 27 28 # ISO 15.2.12.5.11 28 29 def each_index(&block) 29 return to_enum :each_index unless block _given?30 return to_enum :each_index unless block 30 31 31 32 idx = 0 … … 44 45 # ISO 15.2.12.5.7 45 46 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 49 55 self 50 56 end … … 61 67 # ISO 15.2.12.5.15 62 68 def initialize(size=0, obj=nil, &block) 63 raise TypeError, "expected Integer for 1st argument" unless size.kind_of? Integer69 size = size.__to_int 64 70 raise ArgumentError, "negative array size" if size < 0 65 71 … … 78 84 end 79 85 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(", ")+"]" 83 98 end 84 99 ## … … 87 102 # ISO 15.2.12.5.31 (x) 88 103 def inspect 89 begin 90 self._inspect 91 rescue SystemStackError 92 "[...]" 93 end 104 self._inspect({}) 94 105 end 95 106 # ISO 15.2.12.5.32 (x) … … 179 190 ret 180 191 end 181 182 # internal method to convert multi-value to single value183 def __svalue184 return self.first if self.size < 2185 self186 end187 192 end 188 193 … … 197 202 # elements. 198 203 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) 200 270 end 201 271 end
Note:
See TracChangeset
for help on using the changeset viewer.