1 | /*
|
---|
2 | ** array.c - Array class
|
---|
3 | **
|
---|
4 | ** See Copyright Notice in mruby.h
|
---|
5 | */
|
---|
6 |
|
---|
7 | #include <mruby.h>
|
---|
8 | #include <mruby/array.h>
|
---|
9 | #include <mruby/class.h>
|
---|
10 | #include <mruby/string.h>
|
---|
11 | #include <mruby/range.h>
|
---|
12 | #include <mruby/proc.h>
|
---|
13 | #include <mruby/opcode.h>
|
---|
14 | #include "value_array.h"
|
---|
15 |
|
---|
16 | #define ARY_DEFAULT_LEN 4
|
---|
17 | #define ARY_SHRINK_RATIO 5 /* must be larger than 2 */
|
---|
18 | #define ARY_C_MAX_SIZE (SIZE_MAX / sizeof(mrb_value))
|
---|
19 | #define ARY_MAX_SIZE ((mrb_int)((ARY_C_MAX_SIZE < (size_t)MRB_INT_MAX) ? ARY_C_MAX_SIZE : MRB_INT_MAX-1))
|
---|
20 |
|
---|
21 | static struct RArray*
|
---|
22 | ary_new_capa(mrb_state *mrb, mrb_int capa)
|
---|
23 | {
|
---|
24 | struct RArray *a;
|
---|
25 | size_t blen;
|
---|
26 |
|
---|
27 | if (capa > ARY_MAX_SIZE) {
|
---|
28 | mrb_raise(mrb, E_ARGUMENT_ERROR, "array size too big");
|
---|
29 | }
|
---|
30 | blen = capa * sizeof(mrb_value);
|
---|
31 |
|
---|
32 | a = (struct RArray*)mrb_obj_alloc(mrb, MRB_TT_ARRAY, mrb->array_class);
|
---|
33 | if (capa <= MRB_ARY_EMBED_LEN_MAX) {
|
---|
34 | ARY_SET_EMBED_LEN(a, 0);
|
---|
35 | }
|
---|
36 | else {
|
---|
37 | a->as.heap.ptr = (mrb_value *)mrb_malloc(mrb, blen);
|
---|
38 | a->as.heap.aux.capa = capa;
|
---|
39 | a->as.heap.len = 0;
|
---|
40 | }
|
---|
41 |
|
---|
42 | return a;
|
---|
43 | }
|
---|
44 |
|
---|
45 | MRB_API mrb_value
|
---|
46 | mrb_ary_new_capa(mrb_state *mrb, mrb_int capa)
|
---|
47 | {
|
---|
48 | struct RArray *a = ary_new_capa(mrb, capa);
|
---|
49 | return mrb_obj_value(a);
|
---|
50 | }
|
---|
51 |
|
---|
52 | MRB_API mrb_value
|
---|
53 | mrb_ary_new(mrb_state *mrb)
|
---|
54 | {
|
---|
55 | return mrb_ary_new_capa(mrb, 0);
|
---|
56 | }
|
---|
57 |
|
---|
58 | /*
|
---|
59 | * to copy array, use this instead of memcpy because of portability
|
---|
60 | * * gcc on ARM may fail optimization of memcpy
|
---|
61 | * http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/ka3934.html
|
---|
62 | * * gcc on MIPS also fail
|
---|
63 | * http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39755
|
---|
64 | * * memcpy doesn't exist on freestanding environment
|
---|
65 | *
|
---|
66 | * If you optimize for binary size, use memcpy instead of this at your own risk
|
---|
67 | * of above portability issue.
|
---|
68 | *
|
---|
69 | * see also http://togetter.com/li/462898
|
---|
70 | *
|
---|
71 | */
|
---|
72 | static inline void
|
---|
73 | array_copy(mrb_value *dst, const mrb_value *src, mrb_int size)
|
---|
74 | {
|
---|
75 | mrb_int i;
|
---|
76 |
|
---|
77 | for (i = 0; i < size; i++) {
|
---|
78 | dst[i] = src[i];
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | static struct RArray*
|
---|
83 | ary_new_from_values(mrb_state *mrb, mrb_int size, const mrb_value *vals)
|
---|
84 | {
|
---|
85 | struct RArray *a = ary_new_capa(mrb, size);
|
---|
86 |
|
---|
87 | array_copy(ARY_PTR(a), vals, size);
|
---|
88 | ARY_SET_LEN(a, size);
|
---|
89 |
|
---|
90 | return a;
|
---|
91 | }
|
---|
92 |
|
---|
93 | MRB_API mrb_value
|
---|
94 | mrb_ary_new_from_values(mrb_state *mrb, mrb_int size, const mrb_value *vals)
|
---|
95 | {
|
---|
96 | struct RArray *a = ary_new_from_values(mrb, size, vals);
|
---|
97 | return mrb_obj_value(a);
|
---|
98 | }
|
---|
99 |
|
---|
100 | MRB_API mrb_value
|
---|
101 | mrb_assoc_new(mrb_state *mrb, mrb_value car, mrb_value cdr)
|
---|
102 | {
|
---|
103 | struct RArray *a;
|
---|
104 |
|
---|
105 | a = ary_new_capa(mrb, 2);
|
---|
106 | ARY_PTR(a)[0] = car;
|
---|
107 | ARY_PTR(a)[1] = cdr;
|
---|
108 | ARY_SET_LEN(a, 2);
|
---|
109 | return mrb_obj_value(a);
|
---|
110 | }
|
---|
111 |
|
---|
112 | static void
|
---|
113 | ary_fill_with_nil(mrb_value *ptr, mrb_int size)
|
---|
114 | {
|
---|
115 | mrb_value nil = mrb_nil_value();
|
---|
116 |
|
---|
117 | while (size--) {
|
---|
118 | *ptr++ = nil;
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | static void
|
---|
123 | ary_modify_check(mrb_state *mrb, struct RArray *a)
|
---|
124 | {
|
---|
125 | mrb_check_frozen(mrb, a);
|
---|
126 | }
|
---|
127 |
|
---|
128 | static void
|
---|
129 | ary_modify(mrb_state *mrb, struct RArray *a)
|
---|
130 | {
|
---|
131 | ary_modify_check(mrb, a);
|
---|
132 |
|
---|
133 | if (ARY_SHARED_P(a)) {
|
---|
134 | mrb_shared_array *shared = a->as.heap.aux.shared;
|
---|
135 |
|
---|
136 | if (shared->refcnt == 1 && a->as.heap.ptr == shared->ptr) {
|
---|
137 | a->as.heap.ptr = shared->ptr;
|
---|
138 | a->as.heap.aux.capa = a->as.heap.len;
|
---|
139 | mrb_free(mrb, shared);
|
---|
140 | }
|
---|
141 | else {
|
---|
142 | mrb_value *ptr, *p;
|
---|
143 | mrb_int len;
|
---|
144 |
|
---|
145 | p = a->as.heap.ptr;
|
---|
146 | len = a->as.heap.len * sizeof(mrb_value);
|
---|
147 | ptr = (mrb_value *)mrb_malloc(mrb, len);
|
---|
148 | if (p) {
|
---|
149 | array_copy(ptr, p, a->as.heap.len);
|
---|
150 | }
|
---|
151 | a->as.heap.ptr = ptr;
|
---|
152 | a->as.heap.aux.capa = a->as.heap.len;
|
---|
153 | mrb_ary_decref(mrb, shared);
|
---|
154 | }
|
---|
155 | ARY_UNSET_SHARED_FLAG(a);
|
---|
156 | }
|
---|
157 | }
|
---|
158 |
|
---|
159 | MRB_API void
|
---|
160 | mrb_ary_modify(mrb_state *mrb, struct RArray* a)
|
---|
161 | {
|
---|
162 | mrb_write_barrier(mrb, (struct RBasic*)a);
|
---|
163 | ary_modify(mrb, a);
|
---|
164 | }
|
---|
165 |
|
---|
166 | static void
|
---|
167 | ary_make_shared(mrb_state *mrb, struct RArray *a)
|
---|
168 | {
|
---|
169 | if (!ARY_SHARED_P(a) && !ARY_EMBED_P(a)) {
|
---|
170 | mrb_shared_array *shared = (mrb_shared_array *)mrb_malloc(mrb, sizeof(mrb_shared_array));
|
---|
171 | mrb_value *ptr = a->as.heap.ptr;
|
---|
172 | mrb_int len = a->as.heap.len;
|
---|
173 |
|
---|
174 | shared->refcnt = 1;
|
---|
175 | if (a->as.heap.aux.capa > len) {
|
---|
176 | a->as.heap.ptr = shared->ptr = (mrb_value *)mrb_realloc(mrb, ptr, sizeof(mrb_value)*len+1);
|
---|
177 | }
|
---|
178 | else {
|
---|
179 | shared->ptr = ptr;
|
---|
180 | }
|
---|
181 | shared->len = len;
|
---|
182 | a->as.heap.aux.shared = shared;
|
---|
183 | ARY_SET_SHARED_FLAG(a);
|
---|
184 | }
|
---|
185 | }
|
---|
186 |
|
---|
187 | static void
|
---|
188 | ary_expand_capa(mrb_state *mrb, struct RArray *a, mrb_int len)
|
---|
189 | {
|
---|
190 | mrb_int capa = ARY_CAPA(a);
|
---|
191 |
|
---|
192 | if (len > ARY_MAX_SIZE || len < 0) {
|
---|
193 | size_error:
|
---|
194 | mrb_raise(mrb, E_ARGUMENT_ERROR, "array size too big");
|
---|
195 | }
|
---|
196 |
|
---|
197 | if (capa < ARY_DEFAULT_LEN) {
|
---|
198 | capa = ARY_DEFAULT_LEN;
|
---|
199 | }
|
---|
200 | while (capa < len) {
|
---|
201 | if (capa <= ARY_MAX_SIZE / 2) {
|
---|
202 | capa *= 2;
|
---|
203 | }
|
---|
204 | else {
|
---|
205 | capa = len;
|
---|
206 | }
|
---|
207 | }
|
---|
208 | if (capa < len || capa > ARY_MAX_SIZE) {
|
---|
209 | goto size_error;
|
---|
210 | }
|
---|
211 |
|
---|
212 | if (ARY_EMBED_P(a)) {
|
---|
213 | mrb_value *ptr = ARY_EMBED_PTR(a);
|
---|
214 | mrb_int len = ARY_EMBED_LEN(a);
|
---|
215 | mrb_value *expanded_ptr = (mrb_value *)mrb_malloc(mrb, sizeof(mrb_value)*capa);
|
---|
216 |
|
---|
217 | ARY_UNSET_EMBED_FLAG(a);
|
---|
218 | array_copy(expanded_ptr, ptr, len);
|
---|
219 | a->as.heap.len = len;
|
---|
220 | a->as.heap.aux.capa = capa;
|
---|
221 | a->as.heap.ptr = expanded_ptr;
|
---|
222 | }
|
---|
223 | else if (capa > a->as.heap.aux.capa) {
|
---|
224 | mrb_value *expanded_ptr = (mrb_value *)mrb_realloc(mrb, a->as.heap.ptr, sizeof(mrb_value)*capa);
|
---|
225 |
|
---|
226 | a->as.heap.aux.capa = capa;
|
---|
227 | a->as.heap.ptr = expanded_ptr;
|
---|
228 | }
|
---|
229 | }
|
---|
230 |
|
---|
231 | static void
|
---|
232 | ary_shrink_capa(mrb_state *mrb, struct RArray *a)
|
---|
233 | {
|
---|
234 |
|
---|
235 | mrb_int capa;
|
---|
236 |
|
---|
237 | if (ARY_EMBED_P(a)) return;
|
---|
238 |
|
---|
239 | capa = a->as.heap.aux.capa;
|
---|
240 | if (capa < ARY_DEFAULT_LEN * 2) return;
|
---|
241 | if (capa <= a->as.heap.len * ARY_SHRINK_RATIO) return;
|
---|
242 |
|
---|
243 | do {
|
---|
244 | capa /= 2;
|
---|
245 | if (capa < ARY_DEFAULT_LEN) {
|
---|
246 | capa = ARY_DEFAULT_LEN;
|
---|
247 | break;
|
---|
248 | }
|
---|
249 | } while (capa > a->as.heap.len * ARY_SHRINK_RATIO);
|
---|
250 |
|
---|
251 | if (capa > a->as.heap.len && capa < a->as.heap.aux.capa) {
|
---|
252 | a->as.heap.aux.capa = capa;
|
---|
253 | a->as.heap.ptr = (mrb_value *)mrb_realloc(mrb, a->as.heap.ptr, sizeof(mrb_value)*capa);
|
---|
254 | }
|
---|
255 | }
|
---|
256 |
|
---|
257 | MRB_API mrb_value
|
---|
258 | mrb_ary_resize(mrb_state *mrb, mrb_value ary, mrb_int new_len)
|
---|
259 | {
|
---|
260 | mrb_int old_len;
|
---|
261 | struct RArray *a = mrb_ary_ptr(ary);
|
---|
262 |
|
---|
263 | ary_modify(mrb, a);
|
---|
264 | old_len = RARRAY_LEN(ary);
|
---|
265 | if (old_len != new_len) {
|
---|
266 | if (new_len < old_len) {
|
---|
267 | ary_shrink_capa(mrb, a);
|
---|
268 | }
|
---|
269 | else {
|
---|
270 | ary_expand_capa(mrb, a, new_len);
|
---|
271 | ary_fill_with_nil(ARY_PTR(a) + old_len, new_len - old_len);
|
---|
272 | }
|
---|
273 | ARY_SET_LEN(a, new_len);
|
---|
274 | }
|
---|
275 |
|
---|
276 | return ary;
|
---|
277 | }
|
---|
278 |
|
---|
279 | static mrb_value
|
---|
280 | mrb_ary_s_create(mrb_state *mrb, mrb_value klass)
|
---|
281 | {
|
---|
282 | mrb_value ary;
|
---|
283 | mrb_value *vals;
|
---|
284 | mrb_int len;
|
---|
285 | struct RArray *a;
|
---|
286 |
|
---|
287 | mrb_get_args(mrb, "*!", &vals, &len);
|
---|
288 | ary = mrb_ary_new_from_values(mrb, len, vals);
|
---|
289 | a = mrb_ary_ptr(ary);
|
---|
290 | a->c = mrb_class_ptr(klass);
|
---|
291 |
|
---|
292 | return ary;
|
---|
293 | }
|
---|
294 |
|
---|
295 | static void ary_replace(mrb_state*, struct RArray*, struct RArray*);
|
---|
296 |
|
---|
297 | static void
|
---|
298 | ary_concat(mrb_state *mrb, struct RArray *a, struct RArray *a2)
|
---|
299 | {
|
---|
300 | mrb_int len;
|
---|
301 |
|
---|
302 | if (ARY_LEN(a) == 0) {
|
---|
303 | ary_replace(mrb, a, a2);
|
---|
304 | return;
|
---|
305 | }
|
---|
306 | if (ARY_LEN(a2) > ARY_MAX_SIZE - ARY_LEN(a)) {
|
---|
307 | mrb_raise(mrb, E_ARGUMENT_ERROR, "array size too big");
|
---|
308 | }
|
---|
309 | len = ARY_LEN(a) + ARY_LEN(a2);
|
---|
310 |
|
---|
311 | ary_modify(mrb, a);
|
---|
312 | if (ARY_CAPA(a) < len) {
|
---|
313 | ary_expand_capa(mrb, a, len);
|
---|
314 | }
|
---|
315 | array_copy(ARY_PTR(a)+ARY_LEN(a), ARY_PTR(a2), ARY_LEN(a2));
|
---|
316 | mrb_write_barrier(mrb, (struct RBasic*)a);
|
---|
317 | ARY_SET_LEN(a, len);
|
---|
318 | }
|
---|
319 |
|
---|
320 | MRB_API void
|
---|
321 | mrb_ary_concat(mrb_state *mrb, mrb_value self, mrb_value other)
|
---|
322 | {
|
---|
323 | struct RArray *a2 = mrb_ary_ptr(other);
|
---|
324 |
|
---|
325 | ary_concat(mrb, mrb_ary_ptr(self), a2);
|
---|
326 | }
|
---|
327 |
|
---|
328 | static mrb_value
|
---|
329 | mrb_ary_concat_m(mrb_state *mrb, mrb_value self)
|
---|
330 | {
|
---|
331 | mrb_value ary;
|
---|
332 |
|
---|
333 | mrb_get_args(mrb, "A", &ary);
|
---|
334 | mrb_ary_concat(mrb, self, ary);
|
---|
335 | return self;
|
---|
336 | }
|
---|
337 |
|
---|
338 | static mrb_value
|
---|
339 | mrb_ary_plus(mrb_state *mrb, mrb_value self)
|
---|
340 | {
|
---|
341 | struct RArray *a1 = mrb_ary_ptr(self);
|
---|
342 | struct RArray *a2;
|
---|
343 | mrb_value *ptr;
|
---|
344 | mrb_int blen, len1;
|
---|
345 |
|
---|
346 | mrb_get_args(mrb, "a", &ptr, &blen);
|
---|
347 | if (ARY_MAX_SIZE - blen < ARY_LEN(a1)) {
|
---|
348 | mrb_raise(mrb, E_ARGUMENT_ERROR, "array size too big");
|
---|
349 | }
|
---|
350 | len1 = ARY_LEN(a1);
|
---|
351 | a2 = ary_new_capa(mrb, len1 + blen);
|
---|
352 | array_copy(ARY_PTR(a2), ARY_PTR(a1), len1);
|
---|
353 | array_copy(ARY_PTR(a2) + len1, ptr, blen);
|
---|
354 | ARY_SET_LEN(a2, len1+blen);
|
---|
355 |
|
---|
356 | return mrb_obj_value(a2);
|
---|
357 | }
|
---|
358 |
|
---|
359 | #define ARY_REPLACE_SHARED_MIN 20
|
---|
360 |
|
---|
361 | static void
|
---|
362 | ary_replace(mrb_state *mrb, struct RArray *a, struct RArray *b)
|
---|
363 | {
|
---|
364 | mrb_int len = ARY_LEN(b);
|
---|
365 |
|
---|
366 | ary_modify_check(mrb, a);
|
---|
367 | if (a == b) return;
|
---|
368 | if (ARY_SHARED_P(a)) {
|
---|
369 | mrb_ary_decref(mrb, a->as.heap.aux.shared);
|
---|
370 | a->as.heap.aux.capa = 0;
|
---|
371 | a->as.heap.len = 0;
|
---|
372 | a->as.heap.ptr = NULL;
|
---|
373 | ARY_UNSET_SHARED_FLAG(a);
|
---|
374 | }
|
---|
375 | if (ARY_SHARED_P(b)) {
|
---|
376 | shared_b:
|
---|
377 | if (ARY_EMBED_P(a)) {
|
---|
378 | ARY_UNSET_EMBED_FLAG(a);
|
---|
379 | }
|
---|
380 | else {
|
---|
381 | mrb_free(mrb, a->as.heap.ptr);
|
---|
382 | }
|
---|
383 | a->as.heap.ptr = b->as.heap.ptr;
|
---|
384 | a->as.heap.len = len;
|
---|
385 | a->as.heap.aux.shared = b->as.heap.aux.shared;
|
---|
386 | a->as.heap.aux.shared->refcnt++;
|
---|
387 | ARY_SET_SHARED_FLAG(a);
|
---|
388 | mrb_write_barrier(mrb, (struct RBasic*)a);
|
---|
389 | return;
|
---|
390 | }
|
---|
391 | if (!mrb_frozen_p(b) && len > ARY_REPLACE_SHARED_MIN) {
|
---|
392 | ary_make_shared(mrb, b);
|
---|
393 | goto shared_b;
|
---|
394 | }
|
---|
395 | if (ARY_CAPA(a) < len)
|
---|
396 | ary_expand_capa(mrb, a, len);
|
---|
397 | array_copy(ARY_PTR(a), ARY_PTR(b), len);
|
---|
398 | mrb_write_barrier(mrb, (struct RBasic*)a);
|
---|
399 | ARY_SET_LEN(a, len);
|
---|
400 | }
|
---|
401 |
|
---|
402 | MRB_API void
|
---|
403 | mrb_ary_replace(mrb_state *mrb, mrb_value self, mrb_value other)
|
---|
404 | {
|
---|
405 | struct RArray *a1 = mrb_ary_ptr(self);
|
---|
406 | struct RArray *a2 = mrb_ary_ptr(other);
|
---|
407 |
|
---|
408 | if (a1 != a2) {
|
---|
409 | ary_replace(mrb, a1, a2);
|
---|
410 | }
|
---|
411 | }
|
---|
412 |
|
---|
413 | static mrb_value
|
---|
414 | mrb_ary_replace_m(mrb_state *mrb, mrb_value self)
|
---|
415 | {
|
---|
416 | mrb_value other;
|
---|
417 |
|
---|
418 | mrb_get_args(mrb, "A", &other);
|
---|
419 | mrb_ary_replace(mrb, self, other);
|
---|
420 |
|
---|
421 | return self;
|
---|
422 | }
|
---|
423 |
|
---|
424 | static mrb_value
|
---|
425 | mrb_ary_times(mrb_state *mrb, mrb_value self)
|
---|
426 | {
|
---|
427 | struct RArray *a1 = mrb_ary_ptr(self);
|
---|
428 | struct RArray *a2;
|
---|
429 | mrb_value *ptr;
|
---|
430 | mrb_int times, len1;
|
---|
431 |
|
---|
432 | mrb_get_args(mrb, "i", ×);
|
---|
433 | if (times < 0) {
|
---|
434 | mrb_raise(mrb, E_ARGUMENT_ERROR, "negative argument");
|
---|
435 | }
|
---|
436 | if (times == 0) return mrb_ary_new(mrb);
|
---|
437 | if (ARY_MAX_SIZE / times < ARY_LEN(a1)) {
|
---|
438 | mrb_raise(mrb, E_ARGUMENT_ERROR, "array size too big");
|
---|
439 | }
|
---|
440 | len1 = ARY_LEN(a1);
|
---|
441 | a2 = ary_new_capa(mrb, len1 * times);
|
---|
442 | ARY_SET_LEN(a2, len1 * times);
|
---|
443 | ptr = ARY_PTR(a2);
|
---|
444 | while (times--) {
|
---|
445 | array_copy(ptr, ARY_PTR(a1), len1);
|
---|
446 | ptr += len1;
|
---|
447 | }
|
---|
448 |
|
---|
449 | return mrb_obj_value(a2);
|
---|
450 | }
|
---|
451 |
|
---|
452 | static mrb_value
|
---|
453 | mrb_ary_reverse_bang(mrb_state *mrb, mrb_value self)
|
---|
454 | {
|
---|
455 | struct RArray *a = mrb_ary_ptr(self);
|
---|
456 | mrb_int len = ARY_LEN(a);
|
---|
457 |
|
---|
458 | if (len > 1) {
|
---|
459 | mrb_value *p1, *p2;
|
---|
460 |
|
---|
461 | ary_modify(mrb, a);
|
---|
462 | p1 = ARY_PTR(a);
|
---|
463 | p2 = p1 + len - 1;
|
---|
464 |
|
---|
465 | while (p1 < p2) {
|
---|
466 | mrb_value tmp = *p1;
|
---|
467 | *p1++ = *p2;
|
---|
468 | *p2-- = tmp;
|
---|
469 | }
|
---|
470 | }
|
---|
471 | return self;
|
---|
472 | }
|
---|
473 |
|
---|
474 | static mrb_value
|
---|
475 | mrb_ary_reverse(mrb_state *mrb, mrb_value self)
|
---|
476 | {
|
---|
477 | struct RArray *a = mrb_ary_ptr(self), *b = ary_new_capa(mrb, ARY_LEN(a));
|
---|
478 | mrb_int len = ARY_LEN(a);
|
---|
479 |
|
---|
480 | if (len > 0) {
|
---|
481 | mrb_value *p1, *p2, *e;
|
---|
482 |
|
---|
483 | p1 = ARY_PTR(a);
|
---|
484 | e = p1 + len;
|
---|
485 | p2 = ARY_PTR(b) + len - 1;
|
---|
486 | while (p1 < e) {
|
---|
487 | *p2-- = *p1++;
|
---|
488 | }
|
---|
489 | ARY_SET_LEN(b, len);
|
---|
490 | }
|
---|
491 | return mrb_obj_value(b);
|
---|
492 | }
|
---|
493 |
|
---|
494 | MRB_API void
|
---|
495 | mrb_ary_push(mrb_state *mrb, mrb_value ary, mrb_value elem)
|
---|
496 | {
|
---|
497 | struct RArray *a = mrb_ary_ptr(ary);
|
---|
498 | mrb_int len = ARY_LEN(a);
|
---|
499 |
|
---|
500 | ary_modify(mrb, a);
|
---|
501 | if (len == ARY_CAPA(a))
|
---|
502 | ary_expand_capa(mrb, a, len + 1);
|
---|
503 | ARY_PTR(a)[len] = elem;
|
---|
504 | ARY_SET_LEN(a, len+1);
|
---|
505 | mrb_field_write_barrier_value(mrb, (struct RBasic*)a, elem);
|
---|
506 | }
|
---|
507 |
|
---|
508 | static mrb_value
|
---|
509 | mrb_ary_push_m(mrb_state *mrb, mrb_value self)
|
---|
510 | {
|
---|
511 | mrb_value *argv;
|
---|
512 | mrb_int len, len2, alen;
|
---|
513 | struct RArray *a;
|
---|
514 |
|
---|
515 | mrb_get_args(mrb, "*!", &argv, &alen);
|
---|
516 | a = mrb_ary_ptr(self);
|
---|
517 | ary_modify(mrb, a);
|
---|
518 | len = ARY_LEN(a);
|
---|
519 | len2 = len + alen;
|
---|
520 | if (ARY_CAPA(a) < len2) {
|
---|
521 | ary_expand_capa(mrb, a, len2);
|
---|
522 | }
|
---|
523 | array_copy(ARY_PTR(a)+len, argv, alen);
|
---|
524 | ARY_SET_LEN(a, len2);
|
---|
525 | mrb_write_barrier(mrb, (struct RBasic*)a);
|
---|
526 |
|
---|
527 | return self;
|
---|
528 | }
|
---|
529 |
|
---|
530 | MRB_API mrb_value
|
---|
531 | mrb_ary_pop(mrb_state *mrb, mrb_value ary)
|
---|
532 | {
|
---|
533 | struct RArray *a = mrb_ary_ptr(ary);
|
---|
534 | mrb_int len = ARY_LEN(a);
|
---|
535 |
|
---|
536 | ary_modify_check(mrb, a);
|
---|
537 | if (len == 0) return mrb_nil_value();
|
---|
538 | ARY_SET_LEN(a, len-1);
|
---|
539 | return ARY_PTR(a)[len-1];
|
---|
540 | }
|
---|
541 |
|
---|
542 | #define ARY_SHIFT_SHARED_MIN 10
|
---|
543 |
|
---|
544 | MRB_API mrb_value
|
---|
545 | mrb_ary_shift(mrb_state *mrb, mrb_value self)
|
---|
546 | {
|
---|
547 | struct RArray *a = mrb_ary_ptr(self);
|
---|
548 | mrb_int len = ARY_LEN(a);
|
---|
549 | mrb_value val;
|
---|
550 |
|
---|
551 | ary_modify_check(mrb, a);
|
---|
552 | if (len == 0) return mrb_nil_value();
|
---|
553 | if (ARY_SHARED_P(a)) {
|
---|
554 | L_SHIFT:
|
---|
555 | val = a->as.heap.ptr[0];
|
---|
556 | a->as.heap.ptr++;
|
---|
557 | a->as.heap.len--;
|
---|
558 | return val;
|
---|
559 | }
|
---|
560 | if (len > ARY_SHIFT_SHARED_MIN) {
|
---|
561 | ary_make_shared(mrb, a);
|
---|
562 | goto L_SHIFT;
|
---|
563 | }
|
---|
564 | else {
|
---|
565 | mrb_value *ptr = ARY_PTR(a);
|
---|
566 | mrb_int size = len;
|
---|
567 |
|
---|
568 | val = *ptr;
|
---|
569 | while (--size) {
|
---|
570 | *ptr = *(ptr+1);
|
---|
571 | ++ptr;
|
---|
572 | }
|
---|
573 | ARY_SET_LEN(a, len-1);
|
---|
574 | }
|
---|
575 | return val;
|
---|
576 | }
|
---|
577 |
|
---|
578 | /* self = [1,2,3]
|
---|
579 | item = 0
|
---|
580 | self.unshift item
|
---|
581 | p self #=> [0, 1, 2, 3] */
|
---|
582 | MRB_API mrb_value
|
---|
583 | mrb_ary_unshift(mrb_state *mrb, mrb_value self, mrb_value item)
|
---|
584 | {
|
---|
585 | struct RArray *a = mrb_ary_ptr(self);
|
---|
586 | mrb_int len = ARY_LEN(a);
|
---|
587 |
|
---|
588 | if (ARY_SHARED_P(a)
|
---|
589 | && a->as.heap.aux.shared->refcnt == 1 /* shared only referenced from this array */
|
---|
590 | && a->as.heap.ptr - a->as.heap.aux.shared->ptr >= 1) /* there's room for unshifted item */ {
|
---|
591 | a->as.heap.ptr--;
|
---|
592 | a->as.heap.ptr[0] = item;
|
---|
593 | }
|
---|
594 | else {
|
---|
595 | mrb_value *ptr;
|
---|
596 |
|
---|
597 | ary_modify(mrb, a);
|
---|
598 | if (ARY_CAPA(a) < len + 1)
|
---|
599 | ary_expand_capa(mrb, a, len + 1);
|
---|
600 | ptr = ARY_PTR(a);
|
---|
601 | value_move(ptr + 1, ptr, len);
|
---|
602 | ptr[0] = item;
|
---|
603 | }
|
---|
604 | ARY_SET_LEN(a, len+1);
|
---|
605 | mrb_field_write_barrier_value(mrb, (struct RBasic*)a, item);
|
---|
606 |
|
---|
607 | return self;
|
---|
608 | }
|
---|
609 |
|
---|
610 | static mrb_value
|
---|
611 | mrb_ary_unshift_m(mrb_state *mrb, mrb_value self)
|
---|
612 | {
|
---|
613 | struct RArray *a = mrb_ary_ptr(self);
|
---|
614 | mrb_value *vals, *ptr;
|
---|
615 | mrb_int alen, len;
|
---|
616 |
|
---|
617 | mrb_get_args(mrb, "*!", &vals, &alen);
|
---|
618 | if (alen == 0) {
|
---|
619 | ary_modify_check(mrb, a);
|
---|
620 | return self;
|
---|
621 | }
|
---|
622 | len = ARY_LEN(a);
|
---|
623 | if (alen > ARY_MAX_SIZE - len) {
|
---|
624 | mrb_raise(mrb, E_ARGUMENT_ERROR, "array size too big");
|
---|
625 | }
|
---|
626 | if (ARY_SHARED_P(a)
|
---|
627 | && a->as.heap.aux.shared->refcnt == 1 /* shared only referenced from this array */
|
---|
628 | && a->as.heap.ptr - a->as.heap.aux.shared->ptr >= alen) /* there's room for unshifted item */ {
|
---|
629 | ary_modify_check(mrb, a);
|
---|
630 | a->as.heap.ptr -= alen;
|
---|
631 | ptr = a->as.heap.ptr;
|
---|
632 | }
|
---|
633 | else {
|
---|
634 | mrb_bool same = vals == ARY_PTR(a);
|
---|
635 | ary_modify(mrb, a);
|
---|
636 | if (ARY_CAPA(a) < len + alen)
|
---|
637 | ary_expand_capa(mrb, a, len + alen);
|
---|
638 | ptr = ARY_PTR(a);
|
---|
639 | value_move(ptr + alen, ptr, len);
|
---|
640 | if (same) vals = ptr;
|
---|
641 | }
|
---|
642 | array_copy(ptr, vals, alen);
|
---|
643 | ARY_SET_LEN(a, len+alen);
|
---|
644 | while (alen--) {
|
---|
645 | mrb_field_write_barrier_value(mrb, (struct RBasic*)a, vals[alen]);
|
---|
646 | }
|
---|
647 |
|
---|
648 | return self;
|
---|
649 | }
|
---|
650 |
|
---|
651 | MRB_API mrb_value
|
---|
652 | mrb_ary_ref(mrb_state *mrb, mrb_value ary, mrb_int n)
|
---|
653 | {
|
---|
654 | struct RArray *a = mrb_ary_ptr(ary);
|
---|
655 | mrb_int len = ARY_LEN(a);
|
---|
656 |
|
---|
657 | /* range check */
|
---|
658 | if (n < 0) n += len;
|
---|
659 | if (n < 0 || len <= n) return mrb_nil_value();
|
---|
660 |
|
---|
661 | return ARY_PTR(a)[n];
|
---|
662 | }
|
---|
663 |
|
---|
664 | MRB_API void
|
---|
665 | mrb_ary_set(mrb_state *mrb, mrb_value ary, mrb_int n, mrb_value val)
|
---|
666 | {
|
---|
667 | struct RArray *a = mrb_ary_ptr(ary);
|
---|
668 | mrb_int len = ARY_LEN(a);
|
---|
669 |
|
---|
670 | ary_modify(mrb, a);
|
---|
671 | /* range check */
|
---|
672 | if (n < 0) {
|
---|
673 | n += len;
|
---|
674 | if (n < 0) {
|
---|
675 | mrb_raisef(mrb, E_INDEX_ERROR, "index %i out of array", n - len);
|
---|
676 | }
|
---|
677 | }
|
---|
678 | if (len <= n) {
|
---|
679 | if (ARY_CAPA(a) <= n)
|
---|
680 | ary_expand_capa(mrb, a, n + 1);
|
---|
681 | ary_fill_with_nil(ARY_PTR(a) + len, n + 1 - len);
|
---|
682 | ARY_SET_LEN(a, n+1);
|
---|
683 | }
|
---|
684 |
|
---|
685 | ARY_PTR(a)[n] = val;
|
---|
686 | mrb_field_write_barrier_value(mrb, (struct RBasic*)a, val);
|
---|
687 | }
|
---|
688 |
|
---|
689 | static struct RArray*
|
---|
690 | ary_dup(mrb_state *mrb, struct RArray *a)
|
---|
691 | {
|
---|
692 | return ary_new_from_values(mrb, ARY_LEN(a), ARY_PTR(a));
|
---|
693 | }
|
---|
694 |
|
---|
695 | MRB_API mrb_value
|
---|
696 | mrb_ary_splice(mrb_state *mrb, mrb_value ary, mrb_int head, mrb_int len, mrb_value rpl)
|
---|
697 | {
|
---|
698 | struct RArray *a = mrb_ary_ptr(ary);
|
---|
699 | mrb_int alen = ARY_LEN(a);
|
---|
700 | const mrb_value *argv;
|
---|
701 | mrb_int argc;
|
---|
702 | mrb_int tail;
|
---|
703 |
|
---|
704 | ary_modify(mrb, a);
|
---|
705 |
|
---|
706 | /* len check */
|
---|
707 | if (len < 0) mrb_raisef(mrb, E_INDEX_ERROR, "negative length (%i)", len);
|
---|
708 |
|
---|
709 | /* range check */
|
---|
710 | if (head < 0) {
|
---|
711 | head += alen;
|
---|
712 | if (head < 0) {
|
---|
713 | mrb_raise(mrb, E_INDEX_ERROR, "index is out of array");
|
---|
714 | }
|
---|
715 | }
|
---|
716 | tail = head + len;
|
---|
717 | if (alen < len || alen < tail) {
|
---|
718 | len = alen - head;
|
---|
719 | }
|
---|
720 |
|
---|
721 | /* size check */
|
---|
722 | if (mrb_array_p(rpl)) {
|
---|
723 | argc = RARRAY_LEN(rpl);
|
---|
724 | argv = RARRAY_PTR(rpl);
|
---|
725 | if (argv == ARY_PTR(a)) {
|
---|
726 | struct RArray *r;
|
---|
727 |
|
---|
728 | if (argc > 32767) {
|
---|
729 | mrb_raise(mrb, E_ARGUMENT_ERROR, "too big recursive splice");
|
---|
730 | }
|
---|
731 | r = ary_dup(mrb, a);
|
---|
732 | argv = ARY_PTR(r);
|
---|
733 | }
|
---|
734 | }
|
---|
735 | else {
|
---|
736 | argc = 1;
|
---|
737 | argv = &rpl;
|
---|
738 | }
|
---|
739 | if (head >= alen) {
|
---|
740 | if (head > ARY_MAX_SIZE - argc) {
|
---|
741 | mrb_raisef(mrb, E_INDEX_ERROR, "index %i too big", head);
|
---|
742 | }
|
---|
743 | len = head + argc;
|
---|
744 | if (len > ARY_CAPA(a)) {
|
---|
745 | ary_expand_capa(mrb, a, head + argc);
|
---|
746 | }
|
---|
747 | ary_fill_with_nil(ARY_PTR(a) + alen, head - alen);
|
---|
748 | if (argc > 0) {
|
---|
749 | array_copy(ARY_PTR(a) + head, argv, argc);
|
---|
750 | }
|
---|
751 | ARY_SET_LEN(a, len);
|
---|
752 | }
|
---|
753 | else {
|
---|
754 | mrb_int newlen;
|
---|
755 |
|
---|
756 | if (alen - len > ARY_MAX_SIZE - argc) {
|
---|
757 | mrb_raisef(mrb, E_INDEX_ERROR, "index %i too big", alen + argc - len);
|
---|
758 | }
|
---|
759 | newlen = alen + argc - len;
|
---|
760 | if (newlen > ARY_CAPA(a)) {
|
---|
761 | ary_expand_capa(mrb, a, newlen);
|
---|
762 | }
|
---|
763 |
|
---|
764 | if (len != argc) {
|
---|
765 | mrb_value *ptr = ARY_PTR(a);
|
---|
766 | tail = head + len;
|
---|
767 | value_move(ptr + head + argc, ptr + tail, alen - tail);
|
---|
768 | ARY_SET_LEN(a, newlen);
|
---|
769 | }
|
---|
770 | if (argc > 0) {
|
---|
771 | value_move(ARY_PTR(a) + head, argv, argc);
|
---|
772 | }
|
---|
773 | }
|
---|
774 | mrb_write_barrier(mrb, (struct RBasic*)a);
|
---|
775 | return ary;
|
---|
776 | }
|
---|
777 |
|
---|
778 | void
|
---|
779 | mrb_ary_decref(mrb_state *mrb, mrb_shared_array *shared)
|
---|
780 | {
|
---|
781 | shared->refcnt--;
|
---|
782 | if (shared->refcnt == 0) {
|
---|
783 | mrb_free(mrb, shared->ptr);
|
---|
784 | mrb_free(mrb, shared);
|
---|
785 | }
|
---|
786 | }
|
---|
787 |
|
---|
788 | static mrb_value
|
---|
789 | ary_subseq(mrb_state *mrb, struct RArray *a, mrb_int beg, mrb_int len)
|
---|
790 | {
|
---|
791 | struct RArray *b;
|
---|
792 |
|
---|
793 | if (!ARY_SHARED_P(a) && len <= ARY_SHIFT_SHARED_MIN) {
|
---|
794 | return mrb_ary_new_from_values(mrb, len, ARY_PTR(a)+beg);
|
---|
795 | }
|
---|
796 | ary_make_shared(mrb, a);
|
---|
797 | b = (struct RArray*)mrb_obj_alloc(mrb, MRB_TT_ARRAY, mrb->array_class);
|
---|
798 | b->as.heap.ptr = a->as.heap.ptr + beg;
|
---|
799 | b->as.heap.len = len;
|
---|
800 | b->as.heap.aux.shared = a->as.heap.aux.shared;
|
---|
801 | b->as.heap.aux.shared->refcnt++;
|
---|
802 | ARY_SET_SHARED_FLAG(b);
|
---|
803 |
|
---|
804 | return mrb_obj_value(b);
|
---|
805 | }
|
---|
806 |
|
---|
807 | static mrb_int
|
---|
808 | aget_index(mrb_state *mrb, mrb_value index)
|
---|
809 | {
|
---|
810 | if (mrb_fixnum_p(index)) {
|
---|
811 | return mrb_fixnum(index);
|
---|
812 | }
|
---|
813 | #ifndef MRB_WITHOUT_FLOAT
|
---|
814 | else if (mrb_float_p(index)) {
|
---|
815 | return (mrb_int)mrb_float(index);
|
---|
816 | }
|
---|
817 | #endif
|
---|
818 | else {
|
---|
819 | mrb_int i, argc;
|
---|
820 | mrb_value *argv;
|
---|
821 |
|
---|
822 | mrb_get_args(mrb, "i*!", &i, &argv, &argc);
|
---|
823 | return i;
|
---|
824 | }
|
---|
825 | }
|
---|
826 |
|
---|
827 | /*
|
---|
828 | * call-seq:
|
---|
829 | * ary[index] -> obj or nil
|
---|
830 | * ary[start, length] -> new_ary or nil
|
---|
831 | * ary[range] -> new_ary or nil
|
---|
832 | * ary.slice(index) -> obj or nil
|
---|
833 | * ary.slice(start, length) -> new_ary or nil
|
---|
834 | * ary.slice(range) -> new_ary or nil
|
---|
835 | *
|
---|
836 | * Element Reference --- Returns the element at +index+, or returns a
|
---|
837 | * subarray starting at the +start+ index and continuing for +length+
|
---|
838 | * elements, or returns a subarray specified by +range+ of indices.
|
---|
839 | *
|
---|
840 | * Negative indices count backward from the end of the array (-1 is the last
|
---|
841 | * element). For +start+ and +range+ cases the starting index is just before
|
---|
842 | * an element. Additionally, an empty array is returned when the starting
|
---|
843 | * index for an element range is at the end of the array.
|
---|
844 | *
|
---|
845 | * Returns +nil+ if the index (or starting index) are out of range.
|
---|
846 | *
|
---|
847 | * a = [ "a", "b", "c", "d", "e" ]
|
---|
848 | * a[1] => "b"
|
---|
849 | * a[1,2] => ["b", "c"]
|
---|
850 | * a[1..-2] => ["b", "c", "d"]
|
---|
851 | *
|
---|
852 | */
|
---|
853 |
|
---|
854 | static mrb_value
|
---|
855 | mrb_ary_aget(mrb_state *mrb, mrb_value self)
|
---|
856 | {
|
---|
857 | struct RArray *a = mrb_ary_ptr(self);
|
---|
858 | mrb_int i, len, alen;
|
---|
859 | mrb_value index;
|
---|
860 |
|
---|
861 | if (mrb_get_args(mrb, "o|i", &index, &len) == 1) {
|
---|
862 | switch (mrb_type(index)) {
|
---|
863 | /* a[n..m] */
|
---|
864 | case MRB_TT_RANGE:
|
---|
865 | if (mrb_range_beg_len(mrb, index, &i, &len, ARY_LEN(a), TRUE) == MRB_RANGE_OK) {
|
---|
866 | return ary_subseq(mrb, a, i, len);
|
---|
867 | }
|
---|
868 | else {
|
---|
869 | return mrb_nil_value();
|
---|
870 | }
|
---|
871 | case MRB_TT_FIXNUM:
|
---|
872 | return mrb_ary_ref(mrb, self, mrb_fixnum(index));
|
---|
873 | default:
|
---|
874 | return mrb_ary_ref(mrb, self, aget_index(mrb, index));
|
---|
875 | }
|
---|
876 | }
|
---|
877 |
|
---|
878 | i = aget_index(mrb, index);
|
---|
879 | alen = ARY_LEN(a);
|
---|
880 | if (i < 0) i += alen;
|
---|
881 | if (i < 0 || alen < i) return mrb_nil_value();
|
---|
882 | if (len < 0) return mrb_nil_value();
|
---|
883 | if (alen == i) return mrb_ary_new(mrb);
|
---|
884 | if (len > alen - i) len = alen - i;
|
---|
885 |
|
---|
886 | return ary_subseq(mrb, a, i, len);
|
---|
887 | }
|
---|
888 |
|
---|
889 | /*
|
---|
890 | * call-seq:
|
---|
891 | * ary[index] = obj -> obj
|
---|
892 | * ary[start, length] = obj or other_ary or nil -> obj or other_ary or nil
|
---|
893 | * ary[range] = obj or other_ary or nil -> obj or other_ary or nil
|
---|
894 | *
|
---|
895 | * Element Assignment --- Sets the element at +index+, or replaces a subarray
|
---|
896 | * from the +start+ index for +length+ elements, or replaces a subarray
|
---|
897 | * specified by the +range+ of indices.
|
---|
898 | *
|
---|
899 | * If indices are greater than the current capacity of the array, the array
|
---|
900 | * grows automatically. Elements are inserted into the array at +start+ if
|
---|
901 | * +length+ is zero.
|
---|
902 | *
|
---|
903 | * Negative indices will count backward from the end of the array. For
|
---|
904 | * +start+ and +range+ cases the starting index is just before an element.
|
---|
905 | *
|
---|
906 | * An IndexError is raised if a negative index points past the beginning of
|
---|
907 | * the array.
|
---|
908 | *
|
---|
909 | * See also Array#push, and Array#unshift.
|
---|
910 | *
|
---|
911 | * a = Array.new
|
---|
912 | * a[4] = "4"; #=> [nil, nil, nil, nil, "4"]
|
---|
913 | * a[0, 3] = [ 'a', 'b', 'c' ] #=> ["a", "b", "c", nil, "4"]
|
---|
914 | * a[1..2] = [ 1, 2 ] #=> ["a", 1, 2, nil, "4"]
|
---|
915 | * a[0, 2] = "?" #=> ["?", 2, nil, "4"]
|
---|
916 | * a[0..2] = "A" #=> ["A", "4"]
|
---|
917 | * a[-1] = "Z" #=> ["A", "Z"]
|
---|
918 | * a[1..-1] = nil #=> ["A", nil]
|
---|
919 | * a[1..-1] = [] #=> ["A"]
|
---|
920 | * a[0, 0] = [ 1, 2 ] #=> [1, 2, "A"]
|
---|
921 | * a[3, 0] = "B" #=> [1, 2, "A", "B"]
|
---|
922 | */
|
---|
923 |
|
---|
924 | static mrb_value
|
---|
925 | mrb_ary_aset(mrb_state *mrb, mrb_value self)
|
---|
926 | {
|
---|
927 | mrb_value v1, v2, v3;
|
---|
928 | mrb_int i, len;
|
---|
929 |
|
---|
930 | mrb_ary_modify(mrb, mrb_ary_ptr(self));
|
---|
931 | if (mrb_get_args(mrb, "oo|o", &v1, &v2, &v3) == 2) {
|
---|
932 | /* a[n..m] = v */
|
---|
933 | switch (mrb_range_beg_len(mrb, v1, &i, &len, RARRAY_LEN(self), FALSE)) {
|
---|
934 | case MRB_RANGE_TYPE_MISMATCH:
|
---|
935 | mrb_ary_set(mrb, self, aget_index(mrb, v1), v2);
|
---|
936 | break;
|
---|
937 | case MRB_RANGE_OK:
|
---|
938 | mrb_ary_splice(mrb, self, i, len, v2);
|
---|
939 | break;
|
---|
940 | case MRB_RANGE_OUT:
|
---|
941 | mrb_raisef(mrb, E_RANGE_ERROR, "%v out of range", v1);
|
---|
942 | break;
|
---|
943 | }
|
---|
944 | return v2;
|
---|
945 | }
|
---|
946 |
|
---|
947 | /* a[n,m] = v */
|
---|
948 | mrb_ary_splice(mrb, self, aget_index(mrb, v1), aget_index(mrb, v2), v3);
|
---|
949 | return v3;
|
---|
950 | }
|
---|
951 |
|
---|
952 | static mrb_value
|
---|
953 | mrb_ary_delete_at(mrb_state *mrb, mrb_value self)
|
---|
954 | {
|
---|
955 | struct RArray *a = mrb_ary_ptr(self);
|
---|
956 | mrb_int index;
|
---|
957 | mrb_value val;
|
---|
958 | mrb_value *ptr;
|
---|
959 | mrb_int len, alen;
|
---|
960 |
|
---|
961 | mrb_get_args(mrb, "i", &index);
|
---|
962 | alen = ARY_LEN(a);
|
---|
963 | if (index < 0) index += alen;
|
---|
964 | if (index < 0 || alen <= index) return mrb_nil_value();
|
---|
965 |
|
---|
966 | ary_modify(mrb, a);
|
---|
967 | ptr = ARY_PTR(a);
|
---|
968 | val = ptr[index];
|
---|
969 |
|
---|
970 | ptr += index;
|
---|
971 | len = alen - index;
|
---|
972 | while (--len) {
|
---|
973 | *ptr = *(ptr+1);
|
---|
974 | ++ptr;
|
---|
975 | }
|
---|
976 | ARY_SET_LEN(a, alen-1);
|
---|
977 |
|
---|
978 | ary_shrink_capa(mrb, a);
|
---|
979 |
|
---|
980 | return val;
|
---|
981 | }
|
---|
982 |
|
---|
983 | static mrb_value
|
---|
984 | mrb_ary_first(mrb_state *mrb, mrb_value self)
|
---|
985 | {
|
---|
986 | struct RArray *a = mrb_ary_ptr(self);
|
---|
987 | mrb_int size, alen;
|
---|
988 |
|
---|
989 | if (mrb_get_argc(mrb) == 0) {
|
---|
990 | return (ARY_LEN(a) > 0)? ARY_PTR(a)[0]: mrb_nil_value();
|
---|
991 | }
|
---|
992 | mrb_get_args(mrb, "|i", &size);
|
---|
993 | if (size < 0) {
|
---|
994 | mrb_raise(mrb, E_ARGUMENT_ERROR, "negative array size");
|
---|
995 | }
|
---|
996 |
|
---|
997 | alen = ARY_LEN(a);
|
---|
998 | if (size > alen) size = alen;
|
---|
999 | if (ARY_SHARED_P(a)) {
|
---|
1000 | return ary_subseq(mrb, a, 0, size);
|
---|
1001 | }
|
---|
1002 | return mrb_ary_new_from_values(mrb, size, ARY_PTR(a));
|
---|
1003 | }
|
---|
1004 |
|
---|
1005 | static mrb_value
|
---|
1006 | mrb_ary_last(mrb_state *mrb, mrb_value self)
|
---|
1007 | {
|
---|
1008 | struct RArray *a = mrb_ary_ptr(self);
|
---|
1009 | mrb_int n, size, alen;
|
---|
1010 |
|
---|
1011 | n = mrb_get_args(mrb, "|i", &size);
|
---|
1012 | alen = ARY_LEN(a);
|
---|
1013 | if (n == 0) {
|
---|
1014 | return (alen > 0) ? ARY_PTR(a)[alen - 1]: mrb_nil_value();
|
---|
1015 | }
|
---|
1016 |
|
---|
1017 | if (size < 0) {
|
---|
1018 | mrb_raise(mrb, E_ARGUMENT_ERROR, "negative array size");
|
---|
1019 | }
|
---|
1020 | if (size > alen) size = alen;
|
---|
1021 | if (ARY_SHARED_P(a) || size > ARY_DEFAULT_LEN) {
|
---|
1022 | return ary_subseq(mrb, a, alen - size, size);
|
---|
1023 | }
|
---|
1024 | return mrb_ary_new_from_values(mrb, size, ARY_PTR(a) + alen - size);
|
---|
1025 | }
|
---|
1026 |
|
---|
1027 | static mrb_value
|
---|
1028 | mrb_ary_index_m(mrb_state *mrb, mrb_value self)
|
---|
1029 | {
|
---|
1030 | mrb_value obj;
|
---|
1031 | mrb_int i;
|
---|
1032 |
|
---|
1033 | mrb_get_args(mrb, "o", &obj);
|
---|
1034 | for (i = 0; i < RARRAY_LEN(self); i++) {
|
---|
1035 | if (mrb_equal(mrb, RARRAY_PTR(self)[i], obj)) {
|
---|
1036 | return mrb_fixnum_value(i);
|
---|
1037 | }
|
---|
1038 | }
|
---|
1039 | return mrb_nil_value();
|
---|
1040 | }
|
---|
1041 |
|
---|
1042 | static mrb_value
|
---|
1043 | mrb_ary_rindex_m(mrb_state *mrb, mrb_value self)
|
---|
1044 | {
|
---|
1045 | mrb_value obj;
|
---|
1046 | mrb_int i, len;
|
---|
1047 |
|
---|
1048 | mrb_get_args(mrb, "o", &obj);
|
---|
1049 | for (i = RARRAY_LEN(self) - 1; i >= 0; i--) {
|
---|
1050 | if (mrb_equal(mrb, RARRAY_PTR(self)[i], obj)) {
|
---|
1051 | return mrb_fixnum_value(i);
|
---|
1052 | }
|
---|
1053 | if (i > (len = RARRAY_LEN(self))) {
|
---|
1054 | i = len;
|
---|
1055 | }
|
---|
1056 | }
|
---|
1057 | return mrb_nil_value();
|
---|
1058 | }
|
---|
1059 |
|
---|
1060 | MRB_API mrb_value
|
---|
1061 | mrb_ary_splat(mrb_state *mrb, mrb_value v)
|
---|
1062 | {
|
---|
1063 | mrb_value a;
|
---|
1064 |
|
---|
1065 | if (mrb_array_p(v)) {
|
---|
1066 | return v;
|
---|
1067 | }
|
---|
1068 |
|
---|
1069 | if (!mrb_respond_to(mrb, v, mrb_intern_lit(mrb, "to_a"))) {
|
---|
1070 | return mrb_ary_new_from_values(mrb, 1, &v);
|
---|
1071 | }
|
---|
1072 |
|
---|
1073 | a = mrb_funcall(mrb, v, "to_a", 0);
|
---|
1074 | if (mrb_nil_p(a)) {
|
---|
1075 | return mrb_ary_new_from_values(mrb, 1, &v);
|
---|
1076 | }
|
---|
1077 | mrb_ensure_array_type(mrb, a);
|
---|
1078 | return a;
|
---|
1079 | }
|
---|
1080 |
|
---|
1081 | static mrb_value
|
---|
1082 | mrb_ary_size(mrb_state *mrb, mrb_value self)
|
---|
1083 | {
|
---|
1084 | struct RArray *a = mrb_ary_ptr(self);
|
---|
1085 |
|
---|
1086 | return mrb_fixnum_value(ARY_LEN(a));
|
---|
1087 | }
|
---|
1088 |
|
---|
1089 | MRB_API mrb_value
|
---|
1090 | mrb_ary_clear(mrb_state *mrb, mrb_value self)
|
---|
1091 | {
|
---|
1092 | struct RArray *a = mrb_ary_ptr(self);
|
---|
1093 |
|
---|
1094 | ary_modify(mrb, a);
|
---|
1095 | if (ARY_SHARED_P(a)) {
|
---|
1096 | mrb_ary_decref(mrb, a->as.heap.aux.shared);
|
---|
1097 | ARY_UNSET_SHARED_FLAG(a);
|
---|
1098 | }
|
---|
1099 | else if (!ARY_EMBED_P(a)){
|
---|
1100 | mrb_free(mrb, a->as.heap.ptr);
|
---|
1101 | }
|
---|
1102 | ARY_SET_EMBED_LEN(a, 0);
|
---|
1103 |
|
---|
1104 | return self;
|
---|
1105 | }
|
---|
1106 |
|
---|
1107 | static mrb_value
|
---|
1108 | mrb_ary_clear_m(mrb_state *mrb, mrb_value self)
|
---|
1109 | {
|
---|
1110 | return mrb_ary_clear(mrb, self);
|
---|
1111 | }
|
---|
1112 |
|
---|
1113 | static mrb_value
|
---|
1114 | mrb_ary_empty_p(mrb_state *mrb, mrb_value self)
|
---|
1115 | {
|
---|
1116 | struct RArray *a = mrb_ary_ptr(self);
|
---|
1117 |
|
---|
1118 | return mrb_bool_value(ARY_LEN(a) == 0);
|
---|
1119 | }
|
---|
1120 |
|
---|
1121 | MRB_API mrb_value
|
---|
1122 | mrb_ary_entry(mrb_value ary, mrb_int offset)
|
---|
1123 | {
|
---|
1124 | if (offset < 0) {
|
---|
1125 | offset += RARRAY_LEN(ary);
|
---|
1126 | }
|
---|
1127 | if (offset < 0 || RARRAY_LEN(ary) <= offset) {
|
---|
1128 | return mrb_nil_value();
|
---|
1129 | }
|
---|
1130 | return RARRAY_PTR(ary)[offset];
|
---|
1131 | }
|
---|
1132 |
|
---|
1133 | static mrb_value
|
---|
1134 | join_ary(mrb_state *mrb, mrb_value ary, mrb_value sep, mrb_value list)
|
---|
1135 | {
|
---|
1136 | mrb_int i;
|
---|
1137 | mrb_value result, val, tmp;
|
---|
1138 |
|
---|
1139 | /* check recursive */
|
---|
1140 | for (i=0; i<RARRAY_LEN(list); i++) {
|
---|
1141 | if (mrb_obj_equal(mrb, ary, RARRAY_PTR(list)[i])) {
|
---|
1142 | mrb_raise(mrb, E_ARGUMENT_ERROR, "recursive array join");
|
---|
1143 | }
|
---|
1144 | }
|
---|
1145 |
|
---|
1146 | mrb_ary_push(mrb, list, ary);
|
---|
1147 |
|
---|
1148 | result = mrb_str_new_capa(mrb, 64);
|
---|
1149 |
|
---|
1150 | for (i=0; i<RARRAY_LEN(ary); i++) {
|
---|
1151 | if (i > 0 && !mrb_nil_p(sep)) {
|
---|
1152 | mrb_str_cat_str(mrb, result, sep);
|
---|
1153 | }
|
---|
1154 |
|
---|
1155 | val = RARRAY_PTR(ary)[i];
|
---|
1156 | switch (mrb_type(val)) {
|
---|
1157 | case MRB_TT_ARRAY:
|
---|
1158 | ary_join:
|
---|
1159 | val = join_ary(mrb, val, sep, list);
|
---|
1160 | /* fall through */
|
---|
1161 |
|
---|
1162 | case MRB_TT_STRING:
|
---|
1163 | str_join:
|
---|
1164 | mrb_str_cat_str(mrb, result, val);
|
---|
1165 | break;
|
---|
1166 |
|
---|
1167 | default:
|
---|
1168 | if (!mrb_immediate_p(val)) {
|
---|
1169 | tmp = mrb_check_string_type(mrb, val);
|
---|
1170 | if (!mrb_nil_p(tmp)) {
|
---|
1171 | val = tmp;
|
---|
1172 | goto str_join;
|
---|
1173 | }
|
---|
1174 | tmp = mrb_check_array_type(mrb, val);
|
---|
1175 | if (!mrb_nil_p(tmp)) {
|
---|
1176 | val = tmp;
|
---|
1177 | goto ary_join;
|
---|
1178 | }
|
---|
1179 | }
|
---|
1180 | val = mrb_obj_as_string(mrb, val);
|
---|
1181 | goto str_join;
|
---|
1182 | }
|
---|
1183 | }
|
---|
1184 |
|
---|
1185 | mrb_ary_pop(mrb, list);
|
---|
1186 |
|
---|
1187 | return result;
|
---|
1188 | }
|
---|
1189 |
|
---|
1190 | MRB_API mrb_value
|
---|
1191 | mrb_ary_join(mrb_state *mrb, mrb_value ary, mrb_value sep)
|
---|
1192 | {
|
---|
1193 | if (!mrb_nil_p(sep)) {
|
---|
1194 | sep = mrb_obj_as_string(mrb, sep);
|
---|
1195 | }
|
---|
1196 | return join_ary(mrb, ary, sep, mrb_ary_new(mrb));
|
---|
1197 | }
|
---|
1198 |
|
---|
1199 | /*
|
---|
1200 | * call-seq:
|
---|
1201 | * ary.join(sep="") -> str
|
---|
1202 | *
|
---|
1203 | * Returns a string created by converting each element of the array to
|
---|
1204 | * a string, separated by <i>sep</i>.
|
---|
1205 | *
|
---|
1206 | * [ "a", "b", "c" ].join #=> "abc"
|
---|
1207 | * [ "a", "b", "c" ].join("-") #=> "a-b-c"
|
---|
1208 | */
|
---|
1209 |
|
---|
1210 | static mrb_value
|
---|
1211 | mrb_ary_join_m(mrb_state *mrb, mrb_value ary)
|
---|
1212 | {
|
---|
1213 | mrb_value sep = mrb_nil_value();
|
---|
1214 |
|
---|
1215 | mrb_get_args(mrb, "|S!", &sep);
|
---|
1216 | return mrb_ary_join(mrb, ary, sep);
|
---|
1217 | }
|
---|
1218 |
|
---|
1219 | static mrb_value
|
---|
1220 | mrb_ary_eq(mrb_state *mrb, mrb_value ary1)
|
---|
1221 | {
|
---|
1222 | mrb_value ary2;
|
---|
1223 |
|
---|
1224 | mrb_get_args(mrb, "o", &ary2);
|
---|
1225 | if (mrb_obj_equal(mrb, ary1, ary2)) return mrb_true_value();
|
---|
1226 | if (!mrb_array_p(ary2)) {
|
---|
1227 | return mrb_false_value();
|
---|
1228 | }
|
---|
1229 | if (RARRAY_LEN(ary1) != RARRAY_LEN(ary2)) return mrb_false_value();
|
---|
1230 |
|
---|
1231 | return ary2;
|
---|
1232 | }
|
---|
1233 |
|
---|
1234 | static mrb_value
|
---|
1235 | mrb_ary_cmp(mrb_state *mrb, mrb_value ary1)
|
---|
1236 | {
|
---|
1237 | mrb_value ary2;
|
---|
1238 |
|
---|
1239 | mrb_get_args(mrb, "o", &ary2);
|
---|
1240 | if (mrb_obj_equal(mrb, ary1, ary2)) return mrb_fixnum_value(0);
|
---|
1241 | if (!mrb_array_p(ary2)) {
|
---|
1242 | return mrb_nil_value();
|
---|
1243 | }
|
---|
1244 |
|
---|
1245 | return ary2;
|
---|
1246 | }
|
---|
1247 |
|
---|
1248 | /* internal method to convert multi-value to single value */
|
---|
1249 | static mrb_value
|
---|
1250 | mrb_ary_svalue(mrb_state *mrb, mrb_value ary)
|
---|
1251 | {
|
---|
1252 | switch (RARRAY_LEN(ary)) {
|
---|
1253 | case 0:
|
---|
1254 | return mrb_nil_value();
|
---|
1255 | case 1:
|
---|
1256 | return RARRAY_PTR(ary)[0];
|
---|
1257 | default:
|
---|
1258 | return ary;
|
---|
1259 | }
|
---|
1260 | }
|
---|
1261 |
|
---|
1262 | static const mrb_code each_iseq[] = {
|
---|
1263 | OP_ENTER, 0x0, 0x00, 0x1, /* OP_ENTER 0:0:0:0:0:0:1 */
|
---|
1264 | OP_JMPIF, 0x1, 0x0, 19, /* OP_JMPIF R1 19 */
|
---|
1265 | OP_LOADSELF, 0x3, /* OP_LOADSELF R3 */
|
---|
1266 | OP_LOADSYM, 0x4, 0x0, /* OP_LOADSYM R4 :each*/
|
---|
1267 | OP_SEND, 0x3, 0x1, 0x1, /* OP_SEND R3 :to_enum 1 */
|
---|
1268 | OP_RETURN, 0x3, /* OP_RETURN R3 */
|
---|
1269 | OP_LOADI_0, 0x2, /* OP_LOADI_0 R2 */
|
---|
1270 | OP_JMP, 0x0, 43, /* OP_JMP 49 */
|
---|
1271 | OP_MOVE, 0x3, 0x1, /* OP_MOVE R3 R1 */
|
---|
1272 | OP_LOADSELF, 0x4, /* OP_LOADSELF R4 */
|
---|
1273 | OP_MOVE, 0x5, 0x2, /* OP_MOVE R5 R2 */
|
---|
1274 | OP_SEND, 0x4, 0x2, 0x1, /* OP_SEND R4 :[] 1 */
|
---|
1275 | OP_SEND, 0x3, 0x3, 0x1, /* OP_SEND R3 :call 1 */
|
---|
1276 | OP_ADDI, 0x2, 1, /* OP_ADDI R3 1 */
|
---|
1277 | OP_MOVE, 0x3, 0x2, /* OP_MOVE R3 R2 */
|
---|
1278 | OP_LOADSELF, 0x4, /* OP_LOADSELF R4 */
|
---|
1279 | OP_SEND, 0x4, 0x4, 0x0, /* OP_SEND R4 :length 0 */
|
---|
1280 | OP_LT, 0x3, /* OP_LT R3 */
|
---|
1281 | OP_JMPIF, 0x3, 0x0, 24, /* OP_JMPIF R3 24 */
|
---|
1282 | OP_RETURN, 0x0 /* OP_RETURN R3 */
|
---|
1283 | };
|
---|
1284 |
|
---|
1285 | static void
|
---|
1286 | init_ary_each(mrb_state *mrb, struct RClass *ary)
|
---|
1287 | {
|
---|
1288 | struct RProc *p;
|
---|
1289 | mrb_method_t m;
|
---|
1290 | mrb_irep *each_irep = (mrb_irep*)mrb_malloc(mrb, sizeof(mrb_irep));
|
---|
1291 | static const mrb_irep mrb_irep_zero = { 0 };
|
---|
1292 |
|
---|
1293 | *each_irep = mrb_irep_zero;
|
---|
1294 | each_irep->syms = (mrb_sym*)mrb_malloc(mrb, sizeof(mrb_sym)*5);
|
---|
1295 | each_irep->syms[0] = mrb_intern_lit(mrb, "each");
|
---|
1296 | each_irep->syms[1] = mrb_intern_lit(mrb, "to_enum");
|
---|
1297 | each_irep->syms[2] = mrb_intern_lit(mrb, "[]");
|
---|
1298 | each_irep->syms[3] = mrb_intern_lit(mrb, "call");
|
---|
1299 | each_irep->syms[4] = mrb_intern_lit(mrb, "length");
|
---|
1300 | each_irep->slen = 5;
|
---|
1301 | each_irep->flags = MRB_ISEQ_NO_FREE;
|
---|
1302 | each_irep->iseq = each_iseq;
|
---|
1303 | each_irep->ilen = sizeof(each_iseq);
|
---|
1304 | each_irep->nregs = 7;
|
---|
1305 | each_irep->nlocals = 3;
|
---|
1306 | p = mrb_proc_new(mrb, each_irep);
|
---|
1307 | p->flags |= MRB_PROC_SCOPE | MRB_PROC_STRICT;
|
---|
1308 | MRB_METHOD_FROM_PROC(m, p);
|
---|
1309 | mrb_define_method_raw(mrb, ary, mrb_intern_lit(mrb, "each"), m);
|
---|
1310 | }
|
---|
1311 |
|
---|
1312 | void
|
---|
1313 | mrb_init_array(mrb_state *mrb)
|
---|
1314 | {
|
---|
1315 | struct RClass *a;
|
---|
1316 |
|
---|
1317 | mrb->array_class = a = mrb_define_class(mrb, "Array", mrb->object_class); /* 15.2.12 */
|
---|
1318 | MRB_SET_INSTANCE_TT(a, MRB_TT_ARRAY);
|
---|
1319 |
|
---|
1320 | mrb_define_class_method(mrb, a, "[]", mrb_ary_s_create, MRB_ARGS_ANY()); /* 15.2.12.4.1 */
|
---|
1321 |
|
---|
1322 | mrb_define_method(mrb, a, "+", mrb_ary_plus, MRB_ARGS_REQ(1)); /* 15.2.12.5.1 */
|
---|
1323 | mrb_define_method(mrb, a, "*", mrb_ary_times, MRB_ARGS_REQ(1)); /* 15.2.12.5.2 */
|
---|
1324 | mrb_define_method(mrb, a, "<<", mrb_ary_push_m, MRB_ARGS_REQ(1)); /* 15.2.12.5.3 */
|
---|
1325 | mrb_define_method(mrb, a, "[]", mrb_ary_aget, MRB_ARGS_ARG(1,1)); /* 15.2.12.5.4 */
|
---|
1326 | mrb_define_method(mrb, a, "[]=", mrb_ary_aset, MRB_ARGS_ARG(2,1)); /* 15.2.12.5.5 */
|
---|
1327 | mrb_define_method(mrb, a, "clear", mrb_ary_clear_m, MRB_ARGS_NONE()); /* 15.2.12.5.6 */
|
---|
1328 | mrb_define_method(mrb, a, "concat", mrb_ary_concat_m, MRB_ARGS_REQ(1)); /* 15.2.12.5.8 */
|
---|
1329 | mrb_define_method(mrb, a, "delete_at", mrb_ary_delete_at, MRB_ARGS_REQ(1)); /* 15.2.12.5.9 */
|
---|
1330 | mrb_define_method(mrb, a, "empty?", mrb_ary_empty_p, MRB_ARGS_NONE()); /* 15.2.12.5.12 */
|
---|
1331 | mrb_define_method(mrb, a, "first", mrb_ary_first, MRB_ARGS_OPT(1)); /* 15.2.12.5.13 */
|
---|
1332 | mrb_define_method(mrb, a, "index", mrb_ary_index_m, MRB_ARGS_REQ(1)); /* 15.2.12.5.14 */
|
---|
1333 | mrb_define_method(mrb, a, "initialize_copy", mrb_ary_replace_m, MRB_ARGS_REQ(1)); /* 15.2.12.5.16 */
|
---|
1334 | mrb_define_method(mrb, a, "join", mrb_ary_join_m, MRB_ARGS_OPT(1)); /* 15.2.12.5.17 */
|
---|
1335 | mrb_define_method(mrb, a, "last", mrb_ary_last, MRB_ARGS_OPT(1)); /* 15.2.12.5.18 */
|
---|
1336 | mrb_define_method(mrb, a, "length", mrb_ary_size, MRB_ARGS_NONE()); /* 15.2.12.5.19 */
|
---|
1337 | mrb_define_method(mrb, a, "pop", mrb_ary_pop, MRB_ARGS_NONE()); /* 15.2.12.5.21 */
|
---|
1338 | mrb_define_method(mrb, a, "push", mrb_ary_push_m, MRB_ARGS_ANY()); /* 15.2.12.5.22 */
|
---|
1339 | mrb_define_method(mrb, a, "replace", mrb_ary_replace_m, MRB_ARGS_REQ(1)); /* 15.2.12.5.23 */
|
---|
1340 | mrb_define_method(mrb, a, "reverse", mrb_ary_reverse, MRB_ARGS_NONE()); /* 15.2.12.5.24 */
|
---|
1341 | mrb_define_method(mrb, a, "reverse!", mrb_ary_reverse_bang, MRB_ARGS_NONE()); /* 15.2.12.5.25 */
|
---|
1342 | mrb_define_method(mrb, a, "rindex", mrb_ary_rindex_m, MRB_ARGS_REQ(1)); /* 15.2.12.5.26 */
|
---|
1343 | mrb_define_method(mrb, a, "shift", mrb_ary_shift, MRB_ARGS_NONE()); /* 15.2.12.5.27 */
|
---|
1344 | mrb_define_method(mrb, a, "size", mrb_ary_size, MRB_ARGS_NONE()); /* 15.2.12.5.28 */
|
---|
1345 | mrb_define_method(mrb, a, "slice", mrb_ary_aget, MRB_ARGS_ARG(1,1)); /* 15.2.12.5.29 */
|
---|
1346 | mrb_define_method(mrb, a, "unshift", mrb_ary_unshift_m, MRB_ARGS_ANY()); /* 15.2.12.5.30 */
|
---|
1347 |
|
---|
1348 | mrb_define_method(mrb, a, "__ary_eq", mrb_ary_eq, MRB_ARGS_REQ(1));
|
---|
1349 | mrb_define_method(mrb, a, "__ary_cmp", mrb_ary_cmp, MRB_ARGS_REQ(1));
|
---|
1350 | mrb_define_method(mrb, a, "__ary_index", mrb_ary_index_m, MRB_ARGS_REQ(1)); /* kept for mruby-array-ext */
|
---|
1351 | mrb_define_method(mrb, a, "__svalue", mrb_ary_svalue, MRB_ARGS_NONE());
|
---|
1352 |
|
---|
1353 | init_ary_each(mrb, a);
|
---|
1354 | }
|
---|