3 * Copyright (c) 1991-2002, 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
16 =head1 Array Manipulation Functions
24 Perl_av_reify(pTHX_ AV *av)
32 if (SvTIED_mg((SV*)av, PERL_MAGIC_tied) && ckWARN_d(WARN_DEBUGGING))
33 Perl_warner(aTHX_ packWARN(WARN_DEBUGGING), "av_reify called on tied array");
36 while (key > AvFILLp(av) + 1)
37 AvARRAY(av)[--key] = &PL_sv_undef;
39 sv = AvARRAY(av)[--key];
41 if (sv != &PL_sv_undef)
42 (void)SvREFCNT_inc(sv);
44 key = AvARRAY(av) - AvALLOC(av);
46 AvALLOC(av)[--key] = &PL_sv_undef;
54 Pre-extend an array. The C<key> is the index to which the array should be
61 Perl_av_extend(pTHX_ AV *av, I32 key)
64 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
68 PUSHSTACKi(PERLSI_MAGIC);
71 PUSHs(SvTIED_obj((SV*)av, mg));
72 PUSHs(sv_2mortal(newSViv(key+1)));
74 call_method("EXTEND", G_SCALAR|G_DISCARD);
80 if (key > AvMAX(av)) {
85 if (AvALLOC(av) != AvARRAY(av)) {
86 ary = AvALLOC(av) + AvFILLp(av) + 1;
87 tmp = AvARRAY(av) - AvALLOC(av);
88 Move(AvARRAY(av), AvALLOC(av), AvFILLp(av)+1, SV*);
90 SvPVX(av) = (char*)AvALLOC(av);
93 ary[--tmp] = &PL_sv_undef;
96 if (key > AvMAX(av) - 10) {
97 newmax = key + AvMAX(av);
103 #if !defined(STRANGE_MALLOC) && !defined(MYMALLOC)
108 #if defined(MYMALLOC) && !defined(LEAKTEST)
109 newmax = malloced_size((void*)AvALLOC(av))/sizeof(SV*) - 1;
114 newmax = key + AvMAX(av) / 5;
116 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
117 Renew(AvALLOC(av),newmax+1, SV*);
119 bytes = (newmax + 1) * sizeof(SV*);
120 #define MALLOC_OVERHEAD 16
121 itmp = MALLOC_OVERHEAD;
122 while ((MEM_SIZE)(itmp - MALLOC_OVERHEAD) < bytes)
124 itmp -= MALLOC_OVERHEAD;
126 assert(itmp > newmax);
128 assert(newmax >= AvMAX(av));
129 New(2,ary, newmax+1, SV*);
130 Copy(AvALLOC(av), ary, AvMAX(av)+1, SV*);
132 offer_nice_chunk(AvALLOC(av), (AvMAX(av)+1) * sizeof(SV*));
134 Safefree(AvALLOC(av));
137 #if defined(MYMALLOC) && !defined(LEAKTEST)
140 ary = AvALLOC(av) + AvMAX(av) + 1;
141 tmp = newmax - AvMAX(av);
142 if (av == PL_curstack) { /* Oops, grew stack (via av_store()?) */
143 PL_stack_sp = AvALLOC(av) + (PL_stack_sp - PL_stack_base);
144 PL_stack_base = AvALLOC(av);
145 PL_stack_max = PL_stack_base + newmax;
149 newmax = key < 3 ? 3 : key;
150 New(2,AvALLOC(av), newmax+1, SV*);
151 ary = AvALLOC(av) + 1;
153 AvALLOC(av)[0] = &PL_sv_undef; /* For the stacks */
157 ary[--tmp] = &PL_sv_undef;
160 SvPVX(av) = (char*)AvALLOC(av);
169 Returns the SV at the specified index in the array. The C<key> is the
170 index. If C<lval> is set then the fetch will be part of a store. Check
171 that the return value is non-null before dereferencing it to a C<SV*>.
173 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for
174 more information on how to use this function on tied arrays.
180 Perl_av_fetch(pTHX_ register AV *av, I32 key, I32 lval)
188 key += AvFILL(av) + 1;
193 if (SvRMAGICAL(av)) {
194 if (mg_find((SV*)av, PERL_MAGIC_tied) ||
195 mg_find((SV*)av, PERL_MAGIC_regdata))
198 mg_copy((SV*)av, sv, 0, key);
200 return &PL_av_fetch_sv;
204 if (key > AvFILLp(av)) {
208 return av_store(av,key,sv);
210 if (AvARRAY(av)[key] == &PL_sv_undef) {
214 return av_store(av,key,sv);
219 && (!AvARRAY(av)[key] /* eg. @_ could have freed elts */
220 || SvTYPE(AvARRAY(av)[key]) == SVTYPEMASK)) {
221 AvARRAY(av)[key] = &PL_sv_undef; /* 1/2 reify */
224 return &AvARRAY(av)[key];
230 Stores an SV in an array. The array index is specified as C<key>. The
231 return value will be NULL if the operation failed or if the value did not
232 need to be actually stored within the array (as in the case of tied
233 arrays). Otherwise it can be dereferenced to get the original C<SV*>. Note
234 that the caller is responsible for suitably incrementing the reference
235 count of C<val> before the call, and decrementing it if the function
238 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for
239 more information on how to use this function on tied arrays.
245 Perl_av_store(pTHX_ register AV *av, I32 key, SV *val)
255 key += AvFILL(av) + 1;
260 if (SvREADONLY(av) && key >= AvFILL(av))
261 Perl_croak(aTHX_ PL_no_modify);
263 if (SvRMAGICAL(av)) {
264 if (mg_find((SV*)av, PERL_MAGIC_tied)) {
265 if (val != &PL_sv_undef) {
266 mg_copy((SV*)av, val, 0, key);
272 if (!AvREAL(av) && AvREIFY(av))
277 if (AvFILLp(av) < key) {
279 if (av == PL_curstack && key > PL_stack_sp - PL_stack_base)
280 PL_stack_sp = PL_stack_base + key; /* XPUSH in disguise */
282 ary[++AvFILLp(av)] = &PL_sv_undef;
283 while (AvFILLp(av) < key);
288 SvREFCNT_dec(ary[key]);
290 if (SvSMAGICAL(av)) {
291 if (val != &PL_sv_undef) {
292 MAGIC* mg = SvMAGIC(av);
293 sv_magic(val, (SV*)av, toLOWER(mg->mg_type), 0, key);
303 Creates a new AV. The reference count is set to 1.
313 av = (AV*)NEWSV(3,0);
314 sv_upgrade((SV *)av, SVt_PVAV);
318 AvMAX(av) = AvFILLp(av) = -1;
325 Creates a new AV and populates it with a list of SVs. The SVs are copied
326 into the array, so they may be freed after the call to av_make. The new AV
327 will have a reference count of 1.
333 Perl_av_make(pTHX_ register I32 size, register SV **strp)
339 av = (AV*)NEWSV(8,0);
340 sv_upgrade((SV *) av,SVt_PVAV);
341 AvFLAGS(av) = AVf_REAL;
342 if (size) { /* `defined' was returning undef for size==0 anyway. */
345 SvPVX(av) = (char*)ary;
346 AvFILLp(av) = size - 1;
347 AvMAX(av) = size - 1;
348 for (i = 0; i < size; i++) {
351 sv_setsv(ary[i], *strp);
359 Perl_av_fake(pTHX_ register I32 size, register SV **strp)
364 av = (AV*)NEWSV(9,0);
365 sv_upgrade((SV *)av, SVt_PVAV);
366 New(4,ary,size+1,SV*);
368 Copy(strp,ary,size,SV*);
369 AvFLAGS(av) = AVf_REIFY;
370 SvPVX(av) = (char*)ary;
371 AvFILLp(av) = size - 1;
372 AvMAX(av) = size - 1;
384 Clears an array, making it empty. Does not free the memory used by the
391 Perl_av_clear(pTHX_ register AV *av)
397 if (SvREFCNT(av) == 0 && ckWARN_d(WARN_DEBUGGING)) {
398 Perl_warner(aTHX_ packWARN(WARN_DEBUGGING), "Attempt to clear deleted array");
406 Perl_croak(aTHX_ PL_no_modify);
408 /* Give any tie a chance to cleanup first */
417 key = AvFILLp(av) + 1;
419 SvREFCNT_dec(ary[--key]);
420 ary[key] = &PL_sv_undef;
423 if ((key = AvARRAY(av) - AvALLOC(av))) {
425 SvPVX(av) = (char*)AvALLOC(av);
434 Undefines the array. Frees the memory used by the array itself.
440 Perl_av_undef(pTHX_ register AV *av)
448 /* Give any tie a chance to cleanup first */
449 if (SvTIED_mg((SV*)av, PERL_MAGIC_tied))
450 av_fill(av, -1); /* mg_clear() ? */
453 key = AvFILLp(av) + 1;
455 SvREFCNT_dec(AvARRAY(av)[--key]);
457 Safefree(AvALLOC(av));
460 AvMAX(av) = AvFILLp(av) = -1;
462 SvREFCNT_dec(AvARYLEN(av));
470 Pushes an SV onto the end of the array. The array will grow automatically
471 to accommodate the addition.
477 Perl_av_push(pTHX_ register AV *av, SV *val)
483 Perl_croak(aTHX_ PL_no_modify);
485 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
487 PUSHSTACKi(PERLSI_MAGIC);
490 PUSHs(SvTIED_obj((SV*)av, mg));
494 call_method("PUSH", G_SCALAR|G_DISCARD);
499 av_store(av,AvFILLp(av)+1,val);
505 Pops an SV off the end of the array. Returns C<&PL_sv_undef> if the array
512 Perl_av_pop(pTHX_ register AV *av)
520 Perl_croak(aTHX_ PL_no_modify);
521 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
523 PUSHSTACKi(PERLSI_MAGIC);
525 XPUSHs(SvTIED_obj((SV*)av, mg));
528 if (call_method("POP", G_SCALAR)) {
529 retval = newSVsv(*PL_stack_sp--);
531 retval = &PL_sv_undef;
539 retval = AvARRAY(av)[AvFILLp(av)];
540 AvARRAY(av)[AvFILLp(av)--] = &PL_sv_undef;
547 =for apidoc av_unshift
549 Unshift the given number of C<undef> values onto the beginning of the
550 array. The array will grow automatically to accommodate the addition. You
551 must then use C<av_store> to assign values to these new elements.
557 Perl_av_unshift(pTHX_ register AV *av, register I32 num)
567 Perl_croak(aTHX_ PL_no_modify);
569 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
571 PUSHSTACKi(PERLSI_MAGIC);
574 PUSHs(SvTIED_obj((SV*)av, mg));
580 call_method("UNSHIFT", G_SCALAR|G_DISCARD);
588 if (!AvREAL(av) && AvREIFY(av))
590 i = AvARRAY(av) - AvALLOC(av);
598 SvPVX(av) = (char*)(AvARRAY(av) - i);
602 /* Create extra elements */
603 slide = i > 0 ? i : 0;
605 av_extend(av, i + num);
608 Move(ary, ary + num, i + 1, SV*);
610 ary[--num] = &PL_sv_undef;
612 /* Make extra elements into a buffer */
614 AvFILLp(av) -= slide;
615 SvPVX(av) = (char*)(AvARRAY(av) + slide);
622 Shifts an SV off the beginning of the array.
628 Perl_av_shift(pTHX_ register AV *av)
636 Perl_croak(aTHX_ PL_no_modify);
637 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
639 PUSHSTACKi(PERLSI_MAGIC);
641 XPUSHs(SvTIED_obj((SV*)av, mg));
644 if (call_method("SHIFT", G_SCALAR)) {
645 retval = newSVsv(*PL_stack_sp--);
647 retval = &PL_sv_undef;
655 retval = *AvARRAY(av);
657 *AvARRAY(av) = &PL_sv_undef;
658 SvPVX(av) = (char*)(AvARRAY(av) + 1);
669 Returns the highest index in the array. Returns -1 if the array is
676 Perl_av_len(pTHX_ register AV *av)
684 Ensure than an array has a given number of elements, equivalent to
685 Perl's C<$#array = $fill;>.
690 Perl_av_fill(pTHX_ register AV *av, I32 fill)
694 Perl_croak(aTHX_ "panic: null array");
697 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
701 PUSHSTACKi(PERLSI_MAGIC);
704 PUSHs(SvTIED_obj((SV*)av, mg));
705 PUSHs(sv_2mortal(newSViv(fill+1)));
707 call_method("STORESIZE", G_SCALAR|G_DISCARD);
713 if (fill <= AvMAX(av)) {
714 I32 key = AvFILLp(av);
715 SV** ary = AvARRAY(av);
719 SvREFCNT_dec(ary[key]);
720 ary[key--] = &PL_sv_undef;
725 ary[++key] = &PL_sv_undef;
733 (void)av_store(av,fill,&PL_sv_undef);
737 =for apidoc av_delete
739 Deletes the element indexed by C<key> from the array. Returns the
740 deleted element. C<flags> is currently ignored.
745 Perl_av_delete(pTHX_ AV *av, I32 key, I32 flags)
752 Perl_croak(aTHX_ PL_no_modify);
754 key += AvFILL(av) + 1;
758 if (SvRMAGICAL(av)) {
760 if ((mg_find((SV*)av, PERL_MAGIC_tied) ||
761 mg_find((SV*)av, PERL_MAGIC_regdata))
762 && (svp = av_fetch(av, key, TRUE)))
766 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
767 sv_unmagic(sv, PERL_MAGIC_tiedelem); /* No longer an element */
770 return Nullsv; /* element cannot be deleted */
773 if (key > AvFILLp(av))
776 sv = AvARRAY(av)[key];
777 if (key == AvFILLp(av)) {
778 AvARRAY(av)[key] = &PL_sv_undef;
781 } while (--key >= 0 && AvARRAY(av)[key] == &PL_sv_undef);
784 AvARRAY(av)[key] = &PL_sv_undef;
788 if (flags & G_DISCARD) {
796 =for apidoc av_exists
798 Returns true if the element indexed by C<key> has been initialized.
800 This relies on the fact that uninitialized array elements are set to
806 Perl_av_exists(pTHX_ AV *av, I32 key)
811 key += AvFILL(av) + 1;
815 if (SvRMAGICAL(av)) {
816 if (mg_find((SV*)av, PERL_MAGIC_tied) ||
817 mg_find((SV*)av, PERL_MAGIC_regdata))
819 SV *sv = sv_newmortal();
822 mg_copy((SV*)av, sv, 0, key);
823 mg = mg_find(sv, PERL_MAGIC_tiedelem);
825 magic_existspack(sv, mg);
826 return (bool)SvTRUE(sv);
830 if (key <= AvFILLp(av) && AvARRAY(av)[key] != &PL_sv_undef
839 /* AVHV: Support for treating arrays as if they were hashes. The
840 * first element of the array should be a hash reference that maps
841 * hash keys to array indices.
845 S_avhv_index_sv(pTHX_ SV* sv)
847 I32 index = SvIV(sv);
849 Perl_croak(aTHX_ "Bad index while coercing array into hash");
854 S_avhv_index(pTHX_ AV *av, SV *keysv, U32 hash)
860 keys = avhv_keys(av);
861 he = hv_fetch_ent(keys, keysv, FALSE, hash);
863 Perl_croak(aTHX_ "No such pseudo-hash field \"%s\"", SvPV(keysv,n_a));
864 return avhv_index_sv(HeVAL(he));
868 Perl_avhv_keys(pTHX_ AV *av)
870 SV **keysp = av_fetch(av, 0, FALSE);
877 if (SvTYPE(sv) == SVt_PVHV)
881 Perl_croak(aTHX_ "Can't coerce array into hash");
886 Perl_avhv_store_ent(pTHX_ AV *av, SV *keysv, SV *val, U32 hash)
888 return av_store(av, avhv_index(av, keysv, hash), val);
892 Perl_avhv_fetch_ent(pTHX_ AV *av, SV *keysv, I32 lval, U32 hash)
894 return av_fetch(av, avhv_index(av, keysv, hash), lval);
898 Perl_avhv_delete_ent(pTHX_ AV *av, SV *keysv, I32 flags, U32 hash)
900 HV *keys = avhv_keys(av);
903 he = hv_fetch_ent(keys, keysv, FALSE, hash);
904 if (!he || !SvOK(HeVAL(he)))
907 return av_delete(av, avhv_index_sv(HeVAL(he)), flags);
910 /* Check for the existence of an element named by a given key.
914 Perl_avhv_exists_ent(pTHX_ AV *av, SV *keysv, U32 hash)
916 HV *keys = avhv_keys(av);
919 he = hv_fetch_ent(keys, keysv, FALSE, hash);
920 if (!he || !SvOK(HeVAL(he)))
923 return av_exists(av, avhv_index_sv(HeVAL(he)));
927 Perl_avhv_iternext(pTHX_ AV *av)
929 HV *keys = avhv_keys(av);
930 return hv_iternext(keys);
934 Perl_avhv_iterval(pTHX_ AV *av, register HE *entry)
936 SV *sv = hv_iterval(avhv_keys(av), entry);
937 return *av_fetch(av, avhv_index_sv(sv), TRUE);