9 # Calls the given block for each element of +self+
10 # and pass the respective element.
14 # return to_enum :each unless block
18 # block.call(self[idx])
25 # Calls the given block for each element of +self+
26 # and pass the index of the respective element.
29 def each_index(&block)
30 return to_enum :each_index unless block
41 # Calls the given block for each element of +self+
42 # and pass the respective element. Each element will
43 # be replaced by the resulting values.
47 return to_enum :collect! unless block
52 self[idx] = block.call self[idx]
65 # Private method for Array creation.
68 def initialize(size=0, obj=nil, &block)
70 raise ArgumentError, "negative array size" if size < 0
74 self[size - 1] = nil # allocate
78 self[idx] = (block)? block.call(idx): obj
86 def _inspect(recur_list)
88 return "[]" if size == 0
89 return "[...]" if recur_list[self.object_id]
90 recur_list[self.object_id] = true
94 ary<<self[i]._inspect(recur_list)
97 "["+ary.join(", ")+"]"
100 # Return the contents of this array as a string.
102 # ISO 15.2.12.5.31 (x)
106 # ISO 15.2.12.5.32 (x)
110 # Equality---Two arrays are equal if they contain the same number
111 # of elements and if each element is equal to (according to
112 # Object.==) the corresponding element in the other array.
114 # ISO 15.2.12.5.33 (x)
116 other = self.__ary_eq(other)
117 return false if other == false
118 return true if other == true
122 return false if self[i] != other[i]
129 # Returns <code>true</code> if +self+ and _other_ are the same object,
130 # or are both arrays with the same content.
132 # ISO 15.2.12.5.34 (x)
134 other = self.__ary_eq(other)
135 return false if other == false
136 return true if other == true
140 return false unless self[i].eql?(other[i])
147 # Comparison---Returns an integer (-1, 0, or +1)
148 # if this array is less than, equal to, or greater than <i>other_ary</i>.
149 # Each object in each array is compared (using <=>). If any value isn't
150 # equal, then that inequality is the return value. If all the
151 # values found are equal, then the return is based on a
152 # comparison of the array lengths. Thus, two arrays are
153 # "equal" according to <code>Array#<=></code> if and only if they have
154 # the same length and the value of each element is equal to the
155 # value of the corresponding element in the other array.
157 # ISO 15.2.12.5.36 (x)
159 other = self.__ary_cmp(other)
160 return 0 if 0 == other
161 return nil if nil == other
168 n = (self[i] <=> other[i])
169 return n if n.nil? || n != 0
172 len = self.size - other.size
183 # Delete element with index +key+
184 def delete(key, &block)
185 while i = self.index(key)
189 return block.call if ret.nil? && block
195 # Array is enumerable
201 # Sort all elements and replace +self+ with these
204 stack = [ [ 0, self.size - 1 ] ]
206 left, mid, right = stack.pop
209 # sort self[left..right]
214 cmp = if block then block.call(lval,rval) else lval <=> rval end
216 raise ArgumentError, "comparison of #{lval.inspect} and #{rval.inspect} failed"
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
230 lary = self[left, mid - left]
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.
241 (left..right).each { |i|
245 self[i, lsize - lidx] = lary[lidx, lsize - lidx]
250 cmp = if block then block.call(lval,rval) else lval <=> rval end
252 raise ArgumentError, "comparison of #{lval.inspect} and #{rval.inspect} failed"
269 self.dup.sort!(&block)