Debian Codegraph @

vendor/mruby-source/mrblib/array.rb

raw
1 ##
2 # Array
3 #
4 # ISO 15.2.12
5 class Array
6 ##
7 # call-seq:
8 # array.each {|element| ... } -> self
9 # array.each -> Enumerator
10 #
11 # Calls the given block for each element of +self+
12 # and pass the respective element.
13 #
14 # ISO 15.2.12.5.10
15 def each(&block)
16 return to_enum :each unless block
17
18 idx = 0
19 while idx < length
20 block.call(self[idx])
21 idx += 1
22 end
23 self
24 end
25
26 ##
27 # call-seq:
28 # array.each_index {|index| ... } -> self
29 # array.each_index -> Enumerator
30 #
31 # Calls the given block for each element of +self+
32 # and pass the index of the respective element.
33 #
34 # ISO 15.2.12.5.11
35 def each_index(&block)
36 return to_enum :each_index unless block
37
38 idx = 0
39 while idx < length
40 block.call(idx)
41 idx += 1
42 end
43 self
44 end
45
46 ##
47 # call-seq:
48 # array.collect! {|element| ... } -> self
49 # array.collect! -> new_enumerator
50 #
51 # Calls the given block for each element of +self+
52 # and pass the respective element. Each element will
53 # be replaced by the resulting values.
54 #
55 # ISO 15.2.12.5.7
56 def collect!(&block)
57 return to_enum :collect! unless block
58
59 idx = 0
60 len = size
61 while idx < len
62 self[idx] = block.call self[idx]
63 idx += 1
64 end
65 self
66 end
67
68 ##
69 # call-seq:
70 # array.map! {|element| ... } -> self
71 # array.map! -> new_enumerator
72 #
73 # Alias for collect!
74 #
75 # ISO 15.2.12.5.20
76 alias map! collect!
77
78 ##
79 # Private method for Array creation.
80 #
81 # ISO 15.2.12.5.15
82 def initialize(size=0, obj=nil, &block)
83 if size.is_a?(Array) && obj==nil && block == nil
84 self.replace(size)
85 return self
86 end
87 size = size.__to_int
88 raise ArgumentError, "negative array size" if size < 0
89
90 self.clear
91 if size > 0
92 self[size - 1] = nil # allocate
93
94 idx = 0
95 while idx < size
96 self[idx] = (block)? block.call(idx): obj
97 idx += 1
98 end
99 end
100
101 self
102 end
103
104 ##
105 # call-seq:
106 # array == other -> true or false
107 #
108 # Equality---Two arrays are equal if they contain the same number
109 # of elements and if each element is equal to (according to
110 # Object.==) the corresponding element in the other array.
111 #
112 def ==(other)
113 other = self.__ary_eq(other)
114 return false if other == false
115 return true if other == true
116 len = self.size
117 i = 0
118 while i < len
119 return false if self[i] != other[i]
120 i += 1
121 end
122 return true
123 end
124
125 ##
126 # call-seq:
127 # array.eql? other_array -> true or false
128 #
129 # Returns <code>true</code> if +self+ and _other_ are the same object,
130 # or are both arrays with the same content.
131 #
132 def eql?(other)
133 other = self.__ary_eq(other)
134 return false if other == false
135 return true if other == true
136 len = self.size
137 i = 0
138 while i < len
139 return false unless self[i].eql?(other[i])
140 i += 1
141 end
142 return true
143 end
144
145 ##
146 # call-seq:
147 # array <=> other_array -> -1, 0, or 1
148 #
149 # Comparison---Returns an integer (-1, 0, or +1)
150 # if this array is less than, equal to, or greater than <i>other_ary</i>.
151 # Each object in each array is compared (using <=>). If any value isn't
152 # equal, then that inequality is the return value. If all the
153 # values found are equal, then the return is based on a
154 # comparison of the array lengths. Thus, two arrays are
155 # "equal" according to <code>Array#<=></code> if and only if they have
156 # the same length and the value of each element is equal to the
157 # value of the corresponding element in the other array.
158 #
159 def <=>(other)
160 other = self.__ary_cmp(other)
161 return 0 if 0 == other
162 return nil if nil == other
163
164 len = self.size
165 n = other.size
166 len = n if len > n
167 i = 0
168 begin
169 while i < len
170 n = (self[i] <=> other[i])
171 return n if n.nil? || n != 0
172 i += 1
173 end
174 rescue NoMethodError
175 return nil
176 end
177 len = self.size - other.size
178 if len == 0
179 0
180 elsif len > 0
181 1
182 else
183 -1
184 end
185 end
186
187 ##
188 # call-seq:
189 # array.delete(obj) -> deleted_object
190 # array.delete(obj) {|nosuch| ... } -> deleted_object or block_return
191 #
192 # Delete element with index +key+
193 def delete(key, &block)
194 while i = self.index(key)
195 self.delete_at(i)
196 ret = key
197 end
198 return block.call if ret.nil? && block
199 ret
200 end
201
202 ##
203 # call-seq:
204 # array.sort! -> self
205 # array.sort! {|a, b| ... } -> self
206 #
207 # Sort all elements and replace +self+ with these
208 # elements.
209 def sort!(&block)
210 stack = [ [ 0, self.size - 1 ] ]
211 until stack.empty?
212 left, mid, right = stack.pop
213 if right == nil
214 right = mid
215 # sort self[left..right]
216 if left < right
217 if left + 1 == right
218 lval = self[left]
219 rval = self[right]
220 cmp = if block then block.call(lval,rval) else lval <=> rval end
221 if cmp.nil?
222 raise ArgumentError, "comparison of #{lval.inspect} and #{rval.inspect} failed"
223 end
224 if cmp > 0
225 self[left] = rval
226 self[right] = lval
227 end
228 else
229 mid = ((left + right + 1) / 2).floor
230 stack.push [ left, mid, right ]
231 stack.push [ mid, right ]
232 stack.push [ left, (mid - 1) ] if left < mid - 1
233 end
234 end
235 else
236 lary = self[left, mid - left]
237 lsize = lary.size
238
239 # The entity sharing between lary and self may cause a large memory
240 # copy operation in the merge loop below. This harmless operation
241 # cancels the sharing and provides a huge performance gain.
242 lary[0] = lary[0]
243
244 # merge
245 lidx = 0
246 ridx = mid
247 (left..right).each { |i|
248 if lidx >= lsize
249 break
250 elsif ridx > right
251 self[i, lsize - lidx] = lary[lidx, lsize - lidx]
252 break
253 else
254 lval = lary[lidx]
255 rval = self[ridx]
256 cmp = if block then block.call(lval,rval) else lval <=> rval end
257 if cmp.nil?
258 raise ArgumentError, "comparison of #{lval.inspect} and #{rval.inspect} failed"
259 end
260 if cmp <= 0
261 self[i] = lval
262 lidx += 1
263 else
264 self[i] = rval
265 ridx += 1
266 end
267 end
268 }
269 end
270 end
271 self
272 end
273
274 ##
275 # call-seq:
276 # array.sort -> new_array
277 # array.sort {|a, b| ... } -> new_array
278 #
279 # Returns a new Array whose elements are those from +self+, sorted.
280 def sort(&block)
281 self.dup.sort!(&block)
282 end
283
284 ##
285 # call-seq:
286 # array.to_a -> self
287 #
288 # Returns self, no need to convert.
289 def to_a
290 self
291 end
292 alias entries to_a
293
294 ##
295 # Array is enumerable
296 # ISO 15.2.12.3
297 include Enumerable
298 end