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