-
Notifications
You must be signed in to change notification settings - Fork 13
/
array.rb
286 lines (268 loc) · 8.4 KB
/
array.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
class Array
# call-seq:
# shuffle!(random: Random) -> self
#
# Shuffles all elements in +self+ into a random order,
# as selected by the object given by keyword argument +random+;
# returns +self+:
#
# a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# a.shuffle! # => [5, 3, 8, 7, 6, 1, 9, 4, 2, 0]
# a.shuffle! # => [9, 4, 0, 6, 2, 8, 1, 5, 3, 7]
#
# Duplicate elements are included:
#
# a = [0, 1, 0, 1, 0, 1, 0, 1, 0, 1]
# a.shuffle! # => [1, 0, 0, 1, 1, 0, 1, 0, 0, 1]
# a.shuffle! # => [0, 1, 0, 1, 1, 0, 1, 0, 1, 0]
#
# The object given with keyword argument +random+ is used as the random number generator.
#
# Related: see {Methods for Assigning}[rdoc-ref:Array@Methods+for+Assigning].
def shuffle!(random: Random)
Primitive.rb_ary_shuffle_bang(random)
end
# call-seq:
# shuffle(random: Random) -> new_array
#
# Returns a new array containing all elements from +self+ in a random order,
# as selected by the object given by keyword argument +random+:
#
# a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# a.shuffle # => [0, 8, 1, 9, 6, 3, 4, 7, 2, 5]
# a.shuffle # => [8, 9, 0, 5, 1, 2, 6, 4, 7, 3]
#
# Duplicate elements are included:
#
# a = [0, 1, 0, 1, 0, 1, 0, 1, 0, 1]
# a.shuffle # => [1, 0, 1, 1, 0, 0, 1, 0, 0, 1]
# a.shuffle # => [1, 1, 0, 0, 0, 1, 1, 0, 0, 1]
#
# The object given with keyword argument +random+ is used as the random number generator.
#
# Related: see {Methods for Fetching}[rdoc-ref:Array@Methods+for+Fetching].
def shuffle(random: Random)
Primitive.rb_ary_shuffle(random)
end
# call-seq:
# sample(random: Random) -> object
# sample(count, random: Random) -> new_ary
#
# Returns random elements from +self+,
# as selected by the object given by keyword argument +random+.
#
# With no argument +count+ given, returns one random element from +self+:
#
# a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# a.sample # => 3
# a.sample # => 8
#
# Returns +nil+ if +self+ is empty:
#
# [].sample # => nil
#
#
# With non-negative numeric argument +count+ given,
# returns a new array containing +count+ random elements from +self+:
#
# a.sample(3) # => [8, 9, 2]
# a.sample(6) # => [9, 6, 0, 3, 1, 4]
#
# The order of the result array is unrelated to the order of +self+.
#
# Returns a new empty +Array+ if +self+ is empty:
#
# [].sample(4) # => []
#
# May return duplicates in +self+:
#
# a = [1, 1, 1, 2, 2, 3]
# a.sample(a.size) # => [1, 1, 3, 2, 1, 2]
#
# Returns no more than <tt>a.size</tt> elements
# (because no new duplicates are introduced):
#
# a.sample(50) # => [6, 4, 1, 8, 5, 9, 0, 2, 3, 7]
#
# The object given with keyword argument +random+ is used as the random number generator:
#
# a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# a.sample(random: Random.new(1)) #=> 6
# a.sample(4, random: Random.new(1)) #=> [6, 10, 9, 2]
#
# Related: see {Methods for Fetching}[rdoc-ref:Array@Methods+for+Fetching].
def sample(n = (ary = false), random: Random)
if Primitive.mandatory_only?
# Primitive.cexpr! %{ rb_ary_sample(self, rb_cRandom, Qfalse, Qfalse) }
Primitive.ary_sample0
else
# Primitive.cexpr! %{ rb_ary_sample(self, random, n, ary) }
Primitive.ary_sample(random, n, ary)
end
end
# call-seq:
# first -> object or nil
# first(count) -> new_array
#
# Returns elements from +self+, or +nil+; does not modify +self+.
#
# With no argument given, returns the first element (if available):
#
# a = [:foo, 'bar', 2]
# a.first # => :foo
# a # => [:foo, "bar", 2]
#
# If +self+ is empty, returns +nil+.
#
# [].first # => nil
#
# With non-negative integer argument +count+ given,
# returns the first +count+ elements (as available) in a new array:
#
# a.first(0) # => []
# a.first(2) # => [:foo, "bar"]
# a.first(50) # => [:foo, "bar", 2]
#
# Related: see {Methods for Querying}[rdoc-ref:Array@Methods+for+Querying].
def first n = unspecified = true
if Primitive.mandatory_only?
Primitive.attr! :leaf
Primitive.cexpr! %q{ ary_first(self) }
else
if unspecified
Primitive.cexpr! %q{ ary_first(self) }
else
Primitive.cexpr! %q{ ary_take_first_or_last_n(self, NUM2LONG(n), ARY_TAKE_FIRST) }
end
end
end
# call-seq:
# last -> last_object or nil
# last(n) -> new_array
#
# Returns elements from +self+, or +nil+; +self+ is not modified.
#
# With no argument given, returns the last element, or +nil+ if +self+ is empty:
#
# a = [:foo, 'bar', 2]
# a.last # => 2
# a # => [:foo, "bar", 2]
# [].last # => nil
#
#
# With non-negative integer argument +n+ is given,
# returns a new array containing the trailing +n+ elements of +self+, as available:
#
# a = [:foo, 'bar', 2]
# a.last(2) # => ["bar", 2]
# a.last(50) # => [:foo, "bar", 2]
# a.last(0) # => []
# [].last(3) # => []
#
# Related: see {Methods for Fetching}[rdoc-ref:Array@Methods+for+Fetching].
def last n = unspecified = true
if Primitive.mandatory_only?
Primitive.attr! :leaf
Primitive.cexpr! %q{ ary_last(self) }
else
if unspecified
Primitive.cexpr! %q{ ary_last(self) }
else
Primitive.cexpr! %q{ ary_take_first_or_last_n(self, NUM2LONG(n), ARY_TAKE_LAST) }
end
end
end
# call-seq:
# fetch_values(*indexes) -> new_array
# fetch_values(*indexes) {|index| ... } -> new_array
#
# With no block given, returns a new array containing the elements of +self+
# at the offsets given by +indexes+;
# each of the +indexes+ must be an
# {integer-convertible object}[rdoc-ref:implicit_conversion.rdoc@Integer-Convertible+Objects]:
#
# a = [:foo, :bar, :baz]
# a.fetch_values(3, 1) # => [:baz, :foo]
# a.fetch_values(3.1, 1) # => [:baz, :foo]
# a.fetch_values # => []
#
# For a negative index, counts backwards from the end of the array:
#
# a.fetch_values([-2, -1]) # [:bar, :baz]
#
# When no block is given, raises an exception if any index is out of range.
#
# With a block given, for each index:
#
# - If the index in in range, uses an element of +self+ (as above).
# - Otherwise calls, the block with the index, and uses the block's return value.
#
# Example:
#
# a = [:foo, :bar, :baz]
# a.fetch_values(1, 0, 42, 777) {|index| index.to_s}
# # => [:bar, :foo, "42", "777"]
#
# Related: see {Methods for Fetching}[rdoc-ref:Array@Methods+for+Fetching].
def fetch_values(*indexes, &block)
indexes.map! { |i| fetch(i, &block) }
indexes
end
with_yjit do
if Primitive.rb_builtin_basic_definition_p(:each)
undef :each
def each # :nodoc:
Primitive.attr! :inline_block, :c_trace
unless defined?(yield)
return Primitive.cexpr! 'SIZED_ENUMERATOR(self, 0, 0, ary_enum_length)'
end
_i = 0
value = nil
while Primitive.cexpr!(%q{ ary_fetch_next(self, LOCAL_PTR(_i), LOCAL_PTR(value)) })
yield value
end
self
end
end
if Primitive.rb_builtin_basic_definition_p(:map)
undef :map
def map # :nodoc:
Primitive.attr! :inline_block, :c_trace
unless defined?(yield)
return Primitive.cexpr! 'SIZED_ENUMERATOR(self, 0, 0, ary_enum_length)'
end
_i = 0
value = nil
result = Primitive.ary_sized_alloc
while Primitive.cexpr!(%q{ ary_fetch_next(self, LOCAL_PTR(_i), LOCAL_PTR(value)) })
result << yield(value)
end
result
end
if Primitive.rb_builtin_basic_definition_p(:collect)
undef :collect
alias collect map
end
end
if Primitive.rb_builtin_basic_definition_p(:select)
undef :select
def select # :nodoc:
Primitive.attr! :inline_block, :c_trace
unless defined?(yield)
return Primitive.cexpr! 'SIZED_ENUMERATOR(self, 0, 0, ary_enum_length)'
end
_i = 0
value = nil
result = Primitive.ary_sized_alloc
while Primitive.cexpr!(%q{ ary_fetch_next(self, LOCAL_PTR(_i), LOCAL_PTR(value)) })
result << value if yield value
end
result
end
if Primitive.rb_builtin_basic_definition_p(:filter)
undef :filter
alias filter select
end
end
end
end