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