3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, by Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
12 * "...for the Entwives desired order, and plenty, and peace (by which they
13 * meant that things should remain where they had set them)." --Treebeard
17 =head1 Array Manipulation Functions
25 Perl_av_reify(pTHX_ AV *av)
33 if (SvTIED_mg((SV*)av, PERL_MAGIC_tied) && ckWARN_d(WARN_DEBUGGING))
34 Perl_warner(aTHX_ packWARN(WARN_DEBUGGING), "av_reify called on tied array");
37 while (key > AvFILLp(av) + 1)
38 AvARRAY(av)[--key] = &PL_sv_undef;
40 sv = AvARRAY(av)[--key];
42 if (sv != &PL_sv_undef)
43 (void)SvREFCNT_inc(sv);
45 key = AvARRAY(av) - AvALLOC(av);
47 AvALLOC(av)[--key] = &PL_sv_undef;
55 Pre-extend an array. The C<key> is the index to which the array should be
62 Perl_av_extend(pTHX_ AV *av, I32 key)
65 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
69 PUSHSTACKi(PERLSI_MAGIC);
72 PUSHs(SvTIED_obj((SV*)av, mg));
73 PUSHs(sv_2mortal(newSViv(key+1)));
75 call_method("EXTEND", G_SCALAR|G_DISCARD);
81 if (key > AvMAX(av)) {
86 if (AvALLOC(av) != AvARRAY(av)) {
87 ary = AvALLOC(av) + AvFILLp(av) + 1;
88 tmp = AvARRAY(av) - AvALLOC(av);
89 Move(AvARRAY(av), AvALLOC(av), AvFILLp(av)+1, SV*);
91 SvPVX(av) = (char*)AvALLOC(av);
94 ary[--tmp] = &PL_sv_undef;
97 if (key > AvMAX(av) - 10) {
98 newmax = key + AvMAX(av);
104 #if !defined(STRANGE_MALLOC) && !defined(MYMALLOC)
110 newmax = malloced_size((void*)AvALLOC(av))/sizeof(SV*) - 1;
115 newmax = key + AvMAX(av) / 5;
117 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
118 Renew(AvALLOC(av),newmax+1, SV*);
120 bytes = (newmax + 1) * sizeof(SV*);
121 #define MALLOC_OVERHEAD 16
122 itmp = MALLOC_OVERHEAD;
123 while ((MEM_SIZE)(itmp - MALLOC_OVERHEAD) < bytes)
125 itmp -= MALLOC_OVERHEAD;
127 assert(itmp > newmax);
129 assert(newmax >= AvMAX(av));
130 New(2,ary, newmax+1, SV*);
131 Copy(AvALLOC(av), ary, AvMAX(av)+1, SV*);
133 offer_nice_chunk(AvALLOC(av), (AvMAX(av)+1) * sizeof(SV*));
135 Safefree(AvALLOC(av));
141 ary = AvALLOC(av) + AvMAX(av) + 1;
142 tmp = newmax - AvMAX(av);
143 if (av == PL_curstack) { /* Oops, grew stack (via av_store()?) */
144 PL_stack_sp = AvALLOC(av) + (PL_stack_sp - PL_stack_base);
145 PL_stack_base = AvALLOC(av);
146 PL_stack_max = PL_stack_base + newmax;
150 newmax = key < 3 ? 3 : key;
151 New(2,AvALLOC(av), newmax+1, SV*);
152 ary = AvALLOC(av) + 1;
154 AvALLOC(av)[0] = &PL_sv_undef; /* For the stacks */
158 ary[--tmp] = &PL_sv_undef;
161 SvPVX(av) = (char*)AvALLOC(av);
170 Returns the SV at the specified index in the array. The C<key> is the
171 index. If C<lval> is set then the fetch will be part of a store. Check
172 that the return value is non-null before dereferencing it to a C<SV*>.
174 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for
175 more information on how to use this function on tied arrays.
181 Perl_av_fetch(pTHX_ register AV *av, I32 key, I32 lval)
188 if (SvRMAGICAL(av)) {
189 MAGIC *tied_magic = mg_find((SV*)av, PERL_MAGIC_tied);
190 if (tied_magic || mg_find((SV*)av, PERL_MAGIC_regdata)) {
191 U32 adjust_index = 1;
193 if (tied_magic && key < 0) {
194 /* Handle negative array indices 20020222 MJD */
195 SV **negative_indices_glob =
196 hv_fetch(SvSTASH(SvRV(SvTIED_obj((SV *)av,
198 NEGATIVE_INDICES_VAR, 16, 0);
200 if (negative_indices_glob
201 && SvTRUE(GvSV(*negative_indices_glob)))
205 if (key < 0 && adjust_index) {
206 key += AvFILL(av) + 1;
212 sv_upgrade(sv, SVt_PVLV);
213 mg_copy((SV*)av, sv, 0, key);
215 LvTARG(sv) = sv; /* fake (SV**) */
216 return &(LvTARG(sv));
221 key += AvFILL(av) + 1;
226 if (key > AvFILLp(av)) {
230 return av_store(av,key,sv);
232 if (AvARRAY(av)[key] == &PL_sv_undef) {
236 return av_store(av,key,sv);
241 && (!AvARRAY(av)[key] /* eg. @_ could have freed elts */
242 || SvTYPE(AvARRAY(av)[key]) == SVTYPEMASK)) {
243 AvARRAY(av)[key] = &PL_sv_undef; /* 1/2 reify */
246 return &AvARRAY(av)[key];
252 Stores an SV in an array. The array index is specified as C<key>. The
253 return value will be NULL if the operation failed or if the value did not
254 need to be actually stored within the array (as in the case of tied
255 arrays). Otherwise it can be dereferenced to get the original C<SV*>. Note
256 that the caller is responsible for suitably incrementing the reference
257 count of C<val> before the call, and decrementing it if the function
260 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for
261 more information on how to use this function on tied arrays.
267 Perl_av_store(pTHX_ register AV *av, I32 key, SV *val)
276 if (SvRMAGICAL(av)) {
277 MAGIC *tied_magic = mg_find((SV*)av, PERL_MAGIC_tied);
279 /* Handle negative array indices 20020222 MJD */
281 unsigned adjust_index = 1;
282 SV **negative_indices_glob =
283 hv_fetch(SvSTASH(SvRV(SvTIED_obj((SV *)av,
285 NEGATIVE_INDICES_VAR, 16, 0);
286 if (negative_indices_glob
287 && SvTRUE(GvSV(*negative_indices_glob)))
290 key += AvFILL(av) + 1;
295 if (val != &PL_sv_undef) {
296 mg_copy((SV*)av, val, 0, key);
304 key += AvFILL(av) + 1;
309 if (SvREADONLY(av) && key >= AvFILL(av))
310 Perl_croak(aTHX_ PL_no_modify);
312 if (!AvREAL(av) && AvREIFY(av))
317 if (AvFILLp(av) < key) {
319 if (av == PL_curstack && key > PL_stack_sp - PL_stack_base)
320 PL_stack_sp = PL_stack_base + key; /* XPUSH in disguise */
322 ary[++AvFILLp(av)] = &PL_sv_undef;
323 while (AvFILLp(av) < key);
328 SvREFCNT_dec(ary[key]);
330 if (SvSMAGICAL(av)) {
331 if (val != &PL_sv_undef) {
332 MAGIC* mg = SvMAGIC(av);
333 sv_magic(val, (SV*)av, toLOWER(mg->mg_type), 0, key);
343 Creates a new AV. The reference count is set to 1.
353 av = (AV*)NEWSV(3,0);
354 sv_upgrade((SV *)av, SVt_PVAV);
358 AvMAX(av) = AvFILLp(av) = -1;
365 Creates a new AV and populates it with a list of SVs. The SVs are copied
366 into the array, so they may be freed after the call to av_make. The new AV
367 will have a reference count of 1.
373 Perl_av_make(pTHX_ register I32 size, register SV **strp)
379 av = (AV*)NEWSV(8,0);
380 sv_upgrade((SV *) av,SVt_PVAV);
381 AvFLAGS(av) = AVf_REAL;
382 if (size) { /* `defined' was returning undef for size==0 anyway. */
385 SvPVX(av) = (char*)ary;
386 AvFILLp(av) = size - 1;
387 AvMAX(av) = size - 1;
388 for (i = 0; i < size; i++) {
391 sv_setsv(ary[i], *strp);
399 Perl_av_fake(pTHX_ register I32 size, register SV **strp)
404 av = (AV*)NEWSV(9,0);
405 sv_upgrade((SV *)av, SVt_PVAV);
406 New(4,ary,size+1,SV*);
408 Copy(strp,ary,size,SV*);
409 AvFLAGS(av) = AVf_REIFY;
410 SvPVX(av) = (char*)ary;
411 AvFILLp(av) = size - 1;
412 AvMAX(av) = size - 1;
424 Clears an array, making it empty. Does not free the memory used by the
431 Perl_av_clear(pTHX_ register AV *av)
437 if (SvREFCNT(av) == 0 && ckWARN_d(WARN_DEBUGGING)) {
438 Perl_warner(aTHX_ packWARN(WARN_DEBUGGING), "Attempt to clear deleted array");
446 Perl_croak(aTHX_ PL_no_modify);
448 /* Give any tie a chance to cleanup first */
457 key = AvFILLp(av) + 1;
459 SV * sv = ary[--key];
460 /* undef the slot before freeing the value, because a
461 * destructor might try to modify this arrray */
462 ary[key] = &PL_sv_undef;
466 if ((key = AvARRAY(av) - AvALLOC(av))) {
468 SvPVX(av) = (char*)AvALLOC(av);
477 Undefines the array. Frees the memory used by the array itself.
483 Perl_av_undef(pTHX_ register AV *av)
491 /* Give any tie a chance to cleanup first */
492 if (SvTIED_mg((SV*)av, PERL_MAGIC_tied))
493 av_fill(av, -1); /* mg_clear() ? */
496 key = AvFILLp(av) + 1;
498 SvREFCNT_dec(AvARRAY(av)[--key]);
500 Safefree(AvALLOC(av));
503 AvMAX(av) = AvFILLp(av) = -1;
505 SvREFCNT_dec(AvARYLEN(av));
513 Pushes an SV onto the end of the array. The array will grow automatically
514 to accommodate the addition.
520 Perl_av_push(pTHX_ register AV *av, SV *val)
526 Perl_croak(aTHX_ PL_no_modify);
528 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
530 PUSHSTACKi(PERLSI_MAGIC);
533 PUSHs(SvTIED_obj((SV*)av, mg));
537 call_method("PUSH", G_SCALAR|G_DISCARD);
542 av_store(av,AvFILLp(av)+1,val);
548 Pops an SV off the end of the array. Returns C<&PL_sv_undef> if the array
555 Perl_av_pop(pTHX_ register AV *av)
563 Perl_croak(aTHX_ PL_no_modify);
564 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
566 PUSHSTACKi(PERLSI_MAGIC);
568 XPUSHs(SvTIED_obj((SV*)av, mg));
571 if (call_method("POP", G_SCALAR)) {
572 retval = newSVsv(*PL_stack_sp--);
574 retval = &PL_sv_undef;
582 retval = AvARRAY(av)[AvFILLp(av)];
583 AvARRAY(av)[AvFILLp(av)--] = &PL_sv_undef;
590 =for apidoc av_unshift
592 Unshift the given number of C<undef> values onto the beginning of the
593 array. The array will grow automatically to accommodate the addition. You
594 must then use C<av_store> to assign values to these new elements.
600 Perl_av_unshift(pTHX_ register AV *av, register I32 num)
610 Perl_croak(aTHX_ PL_no_modify);
612 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
614 PUSHSTACKi(PERLSI_MAGIC);
617 PUSHs(SvTIED_obj((SV*)av, mg));
623 call_method("UNSHIFT", G_SCALAR|G_DISCARD);
631 if (!AvREAL(av) && AvREIFY(av))
633 i = AvARRAY(av) - AvALLOC(av);
641 SvPVX(av) = (char*)(AvARRAY(av) - i);
645 /* Create extra elements */
646 slide = i > 0 ? i : 0;
648 av_extend(av, i + num);
651 Move(ary, ary + num, i + 1, SV*);
653 ary[--num] = &PL_sv_undef;
655 /* Make extra elements into a buffer */
657 AvFILLp(av) -= slide;
658 SvPVX(av) = (char*)(AvARRAY(av) + slide);
665 Shifts an SV off the beginning of the array.
671 Perl_av_shift(pTHX_ register AV *av)
679 Perl_croak(aTHX_ PL_no_modify);
680 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
682 PUSHSTACKi(PERLSI_MAGIC);
684 XPUSHs(SvTIED_obj((SV*)av, mg));
687 if (call_method("SHIFT", G_SCALAR)) {
688 retval = newSVsv(*PL_stack_sp--);
690 retval = &PL_sv_undef;
698 retval = *AvARRAY(av);
700 *AvARRAY(av) = &PL_sv_undef;
701 SvPVX(av) = (char*)(AvARRAY(av) + 1);
712 Returns the highest index in the array. Returns -1 if the array is
719 Perl_av_len(pTHX_ register AV *av)
727 Ensure than an array has a given number of elements, equivalent to
728 Perl's C<$#array = $fill;>.
733 Perl_av_fill(pTHX_ register AV *av, I32 fill)
737 Perl_croak(aTHX_ "panic: null array");
740 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
744 PUSHSTACKi(PERLSI_MAGIC);
747 PUSHs(SvTIED_obj((SV*)av, mg));
748 PUSHs(sv_2mortal(newSViv(fill+1)));
750 call_method("STORESIZE", G_SCALAR|G_DISCARD);
756 if (fill <= AvMAX(av)) {
757 I32 key = AvFILLp(av);
758 SV** ary = AvARRAY(av);
762 SvREFCNT_dec(ary[key]);
763 ary[key--] = &PL_sv_undef;
768 ary[++key] = &PL_sv_undef;
776 (void)av_store(av,fill,&PL_sv_undef);
780 =for apidoc av_delete
782 Deletes the element indexed by C<key> from the array. Returns the
783 deleted element. C<flags> is currently ignored.
788 Perl_av_delete(pTHX_ AV *av, I32 key, I32 flags)
795 Perl_croak(aTHX_ PL_no_modify);
797 if (SvRMAGICAL(av)) {
798 MAGIC *tied_magic = mg_find((SV*)av, PERL_MAGIC_tied);
800 if ((tied_magic || mg_find((SV*)av, PERL_MAGIC_regdata))) {
801 /* Handle negative array indices 20020222 MJD */
803 unsigned adjust_index = 1;
805 SV **negative_indices_glob =
806 hv_fetch(SvSTASH(SvRV(SvTIED_obj((SV *)av,
808 NEGATIVE_INDICES_VAR, 16, 0);
809 if (negative_indices_glob
810 && SvTRUE(GvSV(*negative_indices_glob)))
814 key += AvFILL(av) + 1;
819 svp = av_fetch(av, key, TRUE);
823 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
824 sv_unmagic(sv, PERL_MAGIC_tiedelem); /* No longer an element */
833 key += AvFILL(av) + 1;
838 if (key > AvFILLp(av))
841 sv = AvARRAY(av)[key];
842 if (key == AvFILLp(av)) {
843 AvARRAY(av)[key] = &PL_sv_undef;
846 } while (--key >= 0 && AvARRAY(av)[key] == &PL_sv_undef);
849 AvARRAY(av)[key] = &PL_sv_undef;
853 if (flags & G_DISCARD) {
861 =for apidoc av_exists
863 Returns true if the element indexed by C<key> has been initialized.
865 This relies on the fact that uninitialized array elements are set to
871 Perl_av_exists(pTHX_ AV *av, I32 key)
877 if (SvRMAGICAL(av)) {
878 MAGIC *tied_magic = mg_find((SV*)av, PERL_MAGIC_tied);
879 if (tied_magic || mg_find((SV*)av, PERL_MAGIC_regdata)) {
880 SV *sv = sv_newmortal();
882 /* Handle negative array indices 20020222 MJD */
884 unsigned adjust_index = 1;
886 SV **negative_indices_glob =
887 hv_fetch(SvSTASH(SvRV(SvTIED_obj((SV *)av,
889 NEGATIVE_INDICES_VAR, 16, 0);
890 if (negative_indices_glob
891 && SvTRUE(GvSV(*negative_indices_glob)))
895 key += AvFILL(av) + 1;
901 mg_copy((SV*)av, sv, 0, key);
902 mg = mg_find(sv, PERL_MAGIC_tiedelem);
904 magic_existspack(sv, mg);
905 return (bool)SvTRUE(sv);
912 key += AvFILL(av) + 1;
917 if (key <= AvFILLp(av) && AvARRAY(av)[key] != &PL_sv_undef