3 * Copyright (c) 1991-1997, Larry Wall
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
11 * "...for the Entwives desired order, and plenty, and peace (by which they
12 * meant that things should remain where they had set them)." --Treebeard
27 while (key > AvFILL(av) + 1)
28 AvARRAY(av)[--key] = &sv_undef;
30 sv = AvARRAY(av)[--key];
32 if (sv != &sv_undef) {
34 (void)SvREFCNT_inc(sv);
37 key = AvARRAY(av) - AvALLOC(av);
39 AvALLOC(av)[--key] = &sv_undef;
44 av_extend(AV *av, I32 key)
46 dTHR; /* only necessary if we have to extend stack */
47 if (key > AvMAX(av)) {
52 if (AvALLOC(av) != AvARRAY(av)) {
53 ary = AvALLOC(av) + AvFILL(av) + 1;
54 tmp = AvARRAY(av) - AvALLOC(av);
55 Move(AvARRAY(av), AvALLOC(av), AvFILL(av)+1, SV*);
57 SvPVX(av) = (char*)AvALLOC(av);
60 ary[--tmp] = &sv_undef;
63 if (key > AvMAX(av) - 10) {
64 newmax = key + AvMAX(av);
70 #ifndef STRANGE_MALLOC
74 newmax = key + AvMAX(av) / 5;
77 Renew(AvALLOC(av),newmax+1, SV*);
79 bytes = (newmax + 1) * sizeof(SV*);
80 #define MALLOC_OVERHEAD 16
81 tmp = MALLOC_OVERHEAD;
82 while (tmp - MALLOC_OVERHEAD < bytes)
84 tmp -= MALLOC_OVERHEAD;
88 New(2,ary, newmax+1, SV*);
89 Copy(AvALLOC(av), ary, AvMAX(av)+1, SV*);
91 offer_nice_chunk(AvALLOC(av), (AvMAX(av)+1) * sizeof(SV*));
93 Safefree(AvALLOC(av));
96 ary = AvALLOC(av) + AvMAX(av) + 1;
97 tmp = newmax - AvMAX(av);
98 if (av == curstack) { /* Oops, grew stack (via av_store()?) */
99 stack_sp = AvALLOC(av) + (stack_sp - stack_base);
100 stack_base = AvALLOC(av);
101 stack_max = stack_base + newmax;
105 newmax = key < 4 ? 4 : key;
106 New(2,AvALLOC(av), newmax+1, SV*);
107 ary = AvALLOC(av) + 1;
109 AvALLOC(av)[0] = &sv_undef; /* For the stacks */
113 ary[--tmp] = &sv_undef;
116 SvPVX(av) = (char*)AvALLOC(av);
123 av_fetch(register AV *av, I32 key, I32 lval)
130 if (SvRMAGICAL(av)) {
131 if (mg_find((SV*)av,'P')) {
134 mg_copy((SV*)av, sv, 0, key);
141 key += AvFILL(av) + 1;
145 else if (key > AvFILL(av)) {
152 return av_store(av,key,sv);
154 if (AvARRAY(av)[key] == &sv_undef) {
158 return av_store(av,key,sv);
163 && (!AvARRAY(av)[key] /* eg. @_ could have freed elts */
164 || SvTYPE(AvARRAY(av)[key]) == SVTYPEMASK)) {
165 AvARRAY(av)[key] = &sv_undef; /* 1/2 reify */
168 return &AvARRAY(av)[key];
172 av_store(register AV *av, I32 key, SV *val)
181 if (SvRMAGICAL(av)) {
182 if (mg_find((SV*)av,'P')) {
183 if (val != &sv_undef)
184 mg_copy((SV*)av, val, 0, key);
190 key += AvFILL(av) + 1;
194 if (SvREADONLY(av) && key >= AvFILL(av))
196 if (!AvREAL(av) && AvREIFY(av))
201 if (AvFILL(av) < key) {
204 if (av == curstack && key > stack_sp - stack_base)
205 stack_sp = stack_base + key; /* XPUSH in disguise */
207 ary[++AvFILL(av)] = &sv_undef;
208 while (AvFILL(av) < key);
213 SvREFCNT_dec(ary[key]);
215 if (SvSMAGICAL(av)) {
216 if (val != &sv_undef) {
217 MAGIC* mg = SvMAGIC(av);
218 sv_magic(val, (SV*)av, toLOWER(mg->mg_type), 0, key);
230 av = (AV*)NEWSV(3,0);
231 sv_upgrade((SV *)av, SVt_PVAV);
235 AvMAX(av) = AvFILL(av) = -1;
240 av_make(register I32 size, register SV **strp)
246 av = (AV*)NEWSV(8,0);
247 sv_upgrade((SV *) av,SVt_PVAV);
248 AvFLAGS(av) = AVf_REAL;
249 if (size) { /* `defined' was returning undef for size==0 anyway. */
252 SvPVX(av) = (char*)ary;
253 AvFILL(av) = size - 1;
254 AvMAX(av) = size - 1;
255 for (i = 0; i < size; i++) {
258 sv_setsv(ary[i], *strp);
266 av_fake(register I32 size, register SV **strp)
271 av = (AV*)NEWSV(9,0);
272 sv_upgrade((SV *)av, SVt_PVAV);
273 New(4,ary,size+1,SV*);
275 Copy(strp,ary,size,SV*);
276 AvFLAGS(av) = AVf_REIFY;
277 SvPVX(av) = (char*)ary;
278 AvFILL(av) = size - 1;
279 AvMAX(av) = size - 1;
289 av_clear(register AV *av)
295 if (SvREFCNT(av) <= 0) {
296 warn("Attempt to clear deleted array");
299 if (!av || AvMAX(av) < 0)
305 key = AvFILL(av) + 1;
307 SvREFCNT_dec(ary[--key]);
308 ary[key] = &sv_undef;
311 if (key = AvARRAY(av) - AvALLOC(av)) {
313 SvPVX(av) = (char*)AvALLOC(av);
322 av_undef(register AV *av)
330 key = AvFILL(av) + 1;
332 SvREFCNT_dec(AvARRAY(av)[--key]);
334 Safefree(AvALLOC(av));
337 AvMAX(av) = AvFILL(av) = -1;
339 SvREFCNT_dec(AvARYLEN(av));
345 av_push(register AV *av, SV *val)
349 av_store(av,AvFILL(av)+1,val);
353 av_pop(register AV *av)
357 if (!av || AvFILL(av) < 0)
361 retval = AvARRAY(av)[AvFILL(av)];
362 AvARRAY(av)[AvFILL(av)--] = &sv_undef;
369 av_unshift(register AV *av, register I32 num)
372 register SV **sstr,**dstr;
378 if (!AvREAL(av) && AvREIFY(av))
380 i = AvARRAY(av) - AvALLOC(av);
388 SvPVX(av) = (char*)(AvARRAY(av) - i);
391 av_extend(av,AvFILL(av)+num);
393 dstr = AvARRAY(av) + AvFILL(av);
396 # pragma loop_opt(off) /* don't loop-optimize the following code */
397 #endif /* BUGGY_MSC5 */
398 for (i = AvFILL(av) - num; i >= 0; --i) {
401 # pragma loop_opt() /* loop-optimization back to command-line setting */
402 #endif /* BUGGY_MSC5 */
405 AvARRAY(av)[--num] = &sv_undef;
410 av_shift(register AV *av)
414 if (!av || AvFILL(av) < 0)
418 retval = *AvARRAY(av);
420 *AvARRAY(av) = &sv_undef;
421 SvPVX(av) = (char*)(AvARRAY(av) + 1);
430 av_len(register AV *av)
436 av_fill(register AV *av, I32 fill)
439 croak("panic: null array");
442 if (fill <= AvMAX(av)) {
443 I32 key = AvFILL(av);
444 SV** ary = AvARRAY(av);
448 SvREFCNT_dec(ary[key]);
449 ary[key--] = &sv_undef;
454 ary[++key] = &sv_undef;
462 (void)av_store(av,fill,&sv_undef);
472 keysp = av_fetch(av, 0, FALSE);
474 if (SvGMAGICAL(*keysp))
477 SV *hash = SvRV(*keysp);
478 if (SvTYPE(hash) == SVt_PVHV)
483 croak("Can't coerce array into hash");
488 avhv_fetch(AV *av, char *key, U32 klen, I32 lval)
491 HV *keys = avhv_keys(av);
494 indsvp = hv_fetch(keys, key, klen, FALSE);
498 croak("Bad index while coercing array into hash");
503 ind = AvFILL(av) + 1;
504 hv_store(keys, key, klen, newSViv(ind), 0);
506 return av_fetch(av, ind, lval);
510 avhv_fetch_ent(AV *av, SV *keysv, I32 lval, U32 hash)
513 HV *keys = avhv_keys(av);
517 he = hv_fetch_ent(keys, keysv, FALSE, hash);
519 ind = SvIV(HeVAL(he));
521 croak("Bad index while coercing array into hash");
526 ind = AvFILL(av) + 1;
527 hv_store_ent(keys, keysv, newSViv(ind), 0);
529 return av_fetch(av, ind, lval);
533 avhv_store(AV *av, char *key, U32 klen, SV *val, U32 hash)
536 HV *keys = avhv_keys(av);
539 indsvp = hv_fetch(keys, key, klen, FALSE);
543 croak("Bad index while coercing array into hash");
545 ind = AvFILL(av) + 1;
546 hv_store(keys, key, klen, newSViv(ind), hash);
548 return av_store(av, ind, val);
552 avhv_store_ent(AV *av, SV *keysv, SV *val, U32 hash)
554 HV *keys = avhv_keys(av);
558 he = hv_fetch_ent(keys, keysv, FALSE, hash);
560 ind = SvIV(HeVAL(he));
562 croak("Bad index while coercing array into hash");
564 ind = AvFILL(av) + 1;
565 hv_store_ent(keys, keysv, newSViv(ind), hash);
567 return av_store(av, ind, val);
571 avhv_exists_ent(AV *av, SV *keysv, U32 hash)
573 HV *keys = avhv_keys(av);
574 return hv_exists_ent(keys, keysv, hash);
578 avhv_exists(AV *av, char *key, U32 klen)
580 HV *keys = avhv_keys(av);
581 return hv_exists(keys, key, klen);
584 /* avhv_delete leaks. Caller can re-index and compress if so desired. */
586 avhv_delete(AV *av, char *key, U32 klen, I32 flags)
588 HV *keys = avhv_keys(av);
593 sv = hv_delete(keys, key, klen, 0);
598 croak("Bad index while coercing array into hash");
599 svp = av_fetch(av, ind, FALSE);
602 if (flags & G_DISCARD) {
606 sv = sv_2mortal(*svp);
612 /* avhv_delete_ent leaks. Caller can re-index and compress if so desired. */
614 avhv_delete_ent(AV *av, SV *keysv, I32 flags, U32 hash)
616 HV *keys = avhv_keys(av);
621 sv = hv_delete_ent(keys, keysv, 0, hash);
626 croak("Bad index while coercing array into hash");
627 svp = av_fetch(av, ind, FALSE);
630 if (flags & G_DISCARD) {
634 sv = sv_2mortal(*svp);
641 avhv_iterinit(AV *av)
643 HV *keys = avhv_keys(av);
644 return hv_iterinit(keys);
648 avhv_iternext(AV *av)
650 HV *keys = avhv_keys(av);
651 return hv_iternext(keys);
655 avhv_iterval(AV *av, register HE *entry)
657 HV *keys = avhv_keys(av);
661 sv = hv_iterval(keys, entry);
664 croak("Bad index while coercing array into hash");
665 return *av_fetch(av, ind, TRUE);
669 avhv_iternextsv(AV *av, char **key, I32 *retlen)
671 HV *keys = avhv_keys(av);
676 he = hv_iternext(keys);
679 *key = hv_iterkey(he, retlen);
680 sv = hv_iterval(keys, he);
683 croak("Bad index while coercing array into hash");
684 return *av_fetch(av, ind, TRUE);