3 * Copyright (c) 1991-2001, 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_ 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 (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_ 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)
517 if (!av || AvFILL(av) < 0)
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;
537 retval = AvARRAY(av)[AvFILLp(av)];
538 AvARRAY(av)[AvFILLp(av)--] = &PL_sv_undef;
545 =for apidoc av_unshift
547 Unshift the given number of C<undef> values onto the beginning of the
548 array. The array will grow automatically to accommodate the addition. You
549 must then use C<av_store> to assign values to these new elements.
555 Perl_av_unshift(pTHX_ register AV *av, register I32 num)
565 Perl_croak(aTHX_ PL_no_modify);
567 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
569 PUSHSTACKi(PERLSI_MAGIC);
572 PUSHs(SvTIED_obj((SV*)av, mg));
578 call_method("UNSHIFT", G_SCALAR|G_DISCARD);
584 if (!AvREAL(av) && AvREIFY(av))
586 i = AvARRAY(av) - AvALLOC(av);
594 SvPVX(av) = (char*)(AvARRAY(av) - i);
598 /* Create extra elements */
599 slide = i > 0 ? i : 0;
601 av_extend(av, i + num);
604 Move(ary, ary + num, i + 1, SV*);
606 ary[--num] = &PL_sv_undef;
608 /* Make extra elements into a buffer */
610 AvFILLp(av) -= slide;
611 SvPVX(av) = (char*)(AvARRAY(av) + slide);
618 Shifts an SV off the beginning of the array.
624 Perl_av_shift(pTHX_ register AV *av)
629 if (!av || AvFILL(av) < 0)
632 Perl_croak(aTHX_ PL_no_modify);
633 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
635 PUSHSTACKi(PERLSI_MAGIC);
637 XPUSHs(SvTIED_obj((SV*)av, mg));
640 if (call_method("SHIFT", G_SCALAR)) {
641 retval = newSVsv(*PL_stack_sp--);
643 retval = &PL_sv_undef;
649 retval = *AvARRAY(av);
651 *AvARRAY(av) = &PL_sv_undef;
652 SvPVX(av) = (char*)(AvARRAY(av) + 1);
663 Returns the highest index in the array. Returns -1 if the array is
670 Perl_av_len(pTHX_ register AV *av)
678 Ensure than an array has a given number of elements, equivalent to
679 Perl's C<$#array = $fill;>.
684 Perl_av_fill(pTHX_ register AV *av, I32 fill)
688 Perl_croak(aTHX_ "panic: null array");
691 if ((mg = SvTIED_mg((SV*)av, PERL_MAGIC_tied))) {
695 PUSHSTACKi(PERLSI_MAGIC);
698 PUSHs(SvTIED_obj((SV*)av, mg));
699 PUSHs(sv_2mortal(newSViv(fill+1)));
701 call_method("STORESIZE", G_SCALAR|G_DISCARD);
707 if (fill <= AvMAX(av)) {
708 I32 key = AvFILLp(av);
709 SV** ary = AvARRAY(av);
713 SvREFCNT_dec(ary[key]);
714 ary[key--] = &PL_sv_undef;
719 ary[++key] = &PL_sv_undef;
727 (void)av_store(av,fill,&PL_sv_undef);
731 =for apidoc av_delete
733 Deletes the element indexed by C<key> from the array. Returns the
734 deleted element. C<flags> is currently ignored.
739 Perl_av_delete(pTHX_ AV *av, I32 key, I32 flags)
746 Perl_croak(aTHX_ PL_no_modify);
748 key += AvFILL(av) + 1;
752 if (SvRMAGICAL(av)) {
754 if ((mg_find((SV*)av, PERL_MAGIC_tied) ||
755 mg_find((SV*)av, PERL_MAGIC_regdata))
756 && (svp = av_fetch(av, key, TRUE)))
760 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
761 sv_unmagic(sv, PERL_MAGIC_tiedelem); /* No longer an element */
764 return Nullsv; /* element cannot be deleted */
767 if (key > AvFILLp(av))
770 sv = AvARRAY(av)[key];
771 if (key == AvFILLp(av)) {
772 AvARRAY(av)[key] = &PL_sv_undef;
775 } while (--key >= 0 && AvARRAY(av)[key] == &PL_sv_undef);
778 AvARRAY(av)[key] = &PL_sv_undef;
782 if (flags & G_DISCARD) {
790 =for apidoc av_exists
792 Returns true if the element indexed by C<key> has been initialized.
794 This relies on the fact that uninitialized array elements are set to
800 Perl_av_exists(pTHX_ AV *av, I32 key)
805 key += AvFILL(av) + 1;
809 if (SvRMAGICAL(av)) {
810 if (mg_find((SV*)av, PERL_MAGIC_tied) ||
811 mg_find((SV*)av, PERL_MAGIC_regdata))
813 SV *sv = sv_newmortal();
816 mg_copy((SV*)av, sv, 0, key);
817 mg = mg_find(sv, PERL_MAGIC_tiedelem);
819 magic_existspack(sv, mg);
824 if (key <= AvFILLp(av) && AvARRAY(av)[key] != &PL_sv_undef
833 /* AVHV: Support for treating arrays as if they were hashes. The
834 * first element of the array should be a hash reference that maps
835 * hash keys to array indices.
839 S_avhv_index_sv(pTHX_ SV* sv)
841 I32 index = SvIV(sv);
843 Perl_croak(aTHX_ "Bad index while coercing array into hash");
848 S_avhv_index(pTHX_ AV *av, SV *keysv, U32 hash)
854 keys = avhv_keys(av);
855 he = hv_fetch_ent(keys, keysv, FALSE, hash);
857 Perl_croak(aTHX_ "No such pseudo-hash field \"%s\"", SvPV(keysv,n_a));
858 return avhv_index_sv(HeVAL(he));
862 Perl_avhv_keys(pTHX_ AV *av)
864 SV **keysp = av_fetch(av, 0, FALSE);
871 if (SvTYPE(sv) == SVt_PVHV)
875 Perl_croak(aTHX_ "Can't coerce array into hash");
880 Perl_avhv_store_ent(pTHX_ AV *av, SV *keysv, SV *val, U32 hash)
882 return av_store(av, avhv_index(av, keysv, hash), val);
886 Perl_avhv_fetch_ent(pTHX_ AV *av, SV *keysv, I32 lval, U32 hash)
888 return av_fetch(av, avhv_index(av, keysv, hash), lval);
892 Perl_avhv_delete_ent(pTHX_ AV *av, SV *keysv, I32 flags, U32 hash)
894 HV *keys = avhv_keys(av);
897 he = hv_fetch_ent(keys, keysv, FALSE, hash);
898 if (!he || !SvOK(HeVAL(he)))
901 return av_delete(av, avhv_index_sv(HeVAL(he)), flags);
904 /* Check for the existence of an element named by a given key.
908 Perl_avhv_exists_ent(pTHX_ AV *av, SV *keysv, U32 hash)
910 HV *keys = avhv_keys(av);
913 he = hv_fetch_ent(keys, keysv, FALSE, hash);
914 if (!he || !SvOK(HeVAL(he)))
917 return av_exists(av, avhv_index_sv(HeVAL(he)));
921 Perl_avhv_iternext(pTHX_ AV *av)
923 HV *keys = avhv_keys(av);
924 return hv_iternext(keys);
928 Perl_avhv_iterval(pTHX_ AV *av, register HE *entry)
930 SV *sv = hv_iterval(avhv_keys(av), entry);
931 return *av_fetch(av, avhv_index_sv(sv), TRUE);