This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Raw integrate of latest perl
[perl5.git] / av.c
diff --git a/av.c b/av.c
index 554f261..5a86460 100644 (file)
--- a/av.c
+++ b/av.c
@@ -1,6 +1,6 @@
 /*    av.c
  *
- *    Copyright (c) 1991-1994, Larry Wall
+ *    Copyright (c) 1991-1997, Larry Wall
  *
  *    You may distribute under the terms of either the GNU General Public
  *    License or the Artistic License, as specified in the README file.
 #include "EXTERN.h"
 #include "perl.h"
 
-static void    av_reify _((AV* av));
-
-static void
-av_reify(av)
-AV* av;
+void
+av_reify(AV *av)
 {
     I32 key;
     SV* sv;
-    
+
+    if (AvREAL(av))
+       return;
     key = AvMAX(av) + 1;
     while (key > AvFILL(av) + 1)
        AvARRAY(av)[--key] = &sv_undef;
     while (key) {
        sv = AvARRAY(av)[--key];
        assert(sv);
-       if (sv != &sv_undef)
+       if (sv != &sv_undef) {
+           dTHR;
            (void)SvREFCNT_inc(sv);
+       }
     }
+    key = AvARRAY(av) - AvALLOC(av);
+    while (key)
+       AvALLOC(av)[--key] = &sv_undef;
     AvREAL_on(av);
 }
 
 void
-av_extend(av,key)
-AV *av;
-I32 key;
+av_extend(AV *av, I32 key)
 {
+    dTHR;                      /* only necessary if we have to extend stack */
     if (key > AvMAX(av)) {
        SV** ary;
        I32 tmp;
@@ -84,10 +87,8 @@ I32 key;
                newmax = tmp - 1;
                New(2,ary, newmax+1, SV*);
                Copy(AvALLOC(av), ary, AvMAX(av)+1, SV*);
-               if (AvMAX(av) > 64 && !nice_chunk) {
-                   nice_chunk = (char*)AvALLOC(av);
-                   nice_chunk_size = (AvMAX(av) + 1) * sizeof(SV*);
-               }
+               if (AvMAX(av) > 64)
+                   offer_nice_chunk(AvALLOC(av), (AvMAX(av)+1) * sizeof(SV*));
                else
                    Safefree(AvALLOC(av));
                AvALLOC(av) = ary;
@@ -119,10 +120,7 @@ I32 key;
 }
 
 SV**
-av_fetch(av,key,lval)
-register AV *av;
-I32 key;
-I32 lval;
+av_fetch(register AV *av, I32 key, I32 lval)
 {
     SV *sv;
 
@@ -131,6 +129,7 @@ I32 lval;
 
     if (SvRMAGICAL(av)) {
        if (mg_find((SV*)av,'P')) {
+           dTHR;
            sv = sv_newmortal();
            mg_copy((SV*)av, sv, 0, key);
            Sv = sv;
@@ -170,10 +169,7 @@ I32 lval;
 }
 
 SV**
-av_store(av,key,val)
-register AV *av;
-I32 key;
-SV *val;
+av_store(register AV *av, I32 key, SV *val)
 {
     SV** ary;
 
@@ -197,14 +193,14 @@ SV *val;
     }
     if (SvREADONLY(av) && key >= AvFILL(av))
        croak(no_modify);
+    if (!AvREAL(av) && AvREIFY(av))
+       av_reify(av);
     if (key > AvMAX(av))
        av_extend(av,key);
-    if (AvREIFY(av))
-       av_reify(av);
-
     ary = AvARRAY(av);
     if (AvFILL(av) < key) {
        if (!AvREAL(av)) {
+           dTHR;
            if (av == curstack && key > stack_sp - stack_base)
                stack_sp = stack_base + key;    /* XPUSH in disguise */
            do
@@ -227,7 +223,7 @@ SV *val;
 }
 
 AV *
-newAV()
+newAV(void)
 {
     register AV *av;
 
@@ -241,9 +237,7 @@ newAV()
 }
 
 AV *
-av_make(size,strp)
-register I32 size;
-register SV **strp;
+av_make(register I32 size, register SV **strp)
 {
     register AV *av;
     register I32 i;
@@ -251,25 +245,25 @@ register SV **strp;
 
     av = (AV*)NEWSV(8,0);
     sv_upgrade((SV *) av,SVt_PVAV);
-    New(4,ary,size+1,SV*);
-    AvALLOC(av) = ary;
     AvFLAGS(av) = AVf_REAL;
-    SvPVX(av) = (char*)ary;
-    AvFILL(av) = size - 1;
-    AvMAX(av) = size - 1;
-    for (i = 0; i < size; i++) {
-       assert (*strp);
-       ary[i] = NEWSV(7,0);
-       sv_setsv(ary[i], *strp);
-       strp++;
+    if (size) {                /* `defined' was returning undef for size==0 anyway. */
+       New(4,ary,size,SV*);
+       AvALLOC(av) = ary;
+       SvPVX(av) = (char*)ary;
+       AvFILL(av) = size - 1;
+       AvMAX(av) = size - 1;
+       for (i = 0; i < size; i++) {
+           assert (*strp);
+           ary[i] = NEWSV(7,0);
+           sv_setsv(ary[i], *strp);
+           strp++;
+       }
     }
     return av;
 }
 
 AV *
-av_fake(size,strp)
-register I32 size;
-register SV **strp;
+av_fake(register I32 size, register SV **strp)
 {
     register AV *av;
     register SV** ary;
@@ -292,8 +286,7 @@ register SV **strp;
 }
 
 void
-av_clear(av)
-register AV *av;
+av_clear(register AV *av)
 {
     register I32 key;
     SV** ary;
@@ -320,11 +313,13 @@ register AV *av;
        SvPVX(av) = (char*)AvALLOC(av);
     }
     AvFILL(av) = -1;
+
+    if (SvRMAGICAL(av))
+       mg_clear((SV*)av); 
 }
 
 void
-av_undef(av)
-register AV *av;
+av_undef(register AV *av)
 {
     register I32 key;
 
@@ -347,9 +342,7 @@ register AV *av;
 }
 
 void
-av_push(av,val)
-register AV *av;
-SV *val;
+av_push(register AV *av, SV *val)
 {
     if (!av)
        return;
@@ -357,8 +350,7 @@ SV *val;
 }
 
 SV *
-av_pop(av)
-register AV *av;
+av_pop(register AV *av)
 {
     SV *retval;
 
@@ -374,9 +366,7 @@ register AV *av;
 }
 
 void
-av_unshift(av,num)
-register AV *av;
-register I32 num;
+av_unshift(register AV *av, register I32 num)
 {
     register I32 i;
     register SV **sstr,**dstr;
@@ -385,12 +375,8 @@ register I32 num;
        return;
     if (SvREADONLY(av))
        croak(no_modify);
-    if (!AvREAL(av)) {
-       if (AvREIFY(av))
-           av_reify(av);
-       else
-           croak("Can't unshift");
-    }
+    if (!AvREAL(av) && AvREIFY(av))
+       av_reify(av);
     i = AvARRAY(av) - AvALLOC(av);
     if (i) {
        if (i > num)
@@ -421,8 +407,7 @@ register I32 num;
 }
 
 SV *
-av_shift(av)
-register AV *av;
+av_shift(register AV *av)
 {
     SV *retval;
 
@@ -442,16 +427,13 @@ register AV *av;
 }
 
 I32
-av_len(av)
-register AV *av;
+av_len(register AV *av)
 {
     return AvFILL(av);
 }
 
 void
-av_fill(av, fill)
-register AV *av;
-I32 fill;
+av_fill(register AV *av, I32 fill)
 {
     if (!av)
        croak("panic: null array");
@@ -479,3 +461,239 @@ I32 fill;
     else
        (void)av_store(av,fill,&sv_undef);
 }
+
+SV**
+avhv_fetch(AV *av, char *key, U32 klen, I32 lval)
+{
+    SV **keys, **indsvp;
+    I32 ind;
+    
+    keys = av_fetch(av, 0, FALSE);
+    if (!keys || !SvROK(*keys) || SvTYPE(SvRV(*keys)) != SVt_PVHV)
+       croak("Can't coerce array into hash");
+    indsvp = hv_fetch((HV*)SvRV(*keys), key, klen, FALSE);
+    if (indsvp) {
+       ind = SvIV(*indsvp);
+       if (ind < 1)
+           croak("Bad index while coercing array into hash");
+    } else {
+       if (!lval)
+           return 0;
+       
+       ind = AvFILL(av) + 1;
+       hv_store((HV*)SvRV(*keys), key, klen, newSViv(ind), 0);
+    }
+    return av_fetch(av, ind, lval);
+}
+
+SV**
+avhv_fetch_ent(AV *av, SV *keysv, I32 lval, U32 hash)
+{
+    SV **keys, **indsvp;
+    HE *he;
+    I32 ind;
+    
+    keys = av_fetch(av, 0, FALSE);
+    if (!keys || !SvROK(*keys) || SvTYPE(SvRV(*keys)) != SVt_PVHV)
+       croak("Can't coerce array into hash");
+    he = hv_fetch_ent((HV*)SvRV(*keys), keysv, FALSE, hash);
+    if (he) {
+       ind = SvIV(HeVAL(he));
+       if (ind < 1)
+           croak("Bad index while coercing array into hash");
+    } else {
+       if (!lval)
+           return 0;
+       
+       ind = AvFILL(av) + 1;
+       hv_store_ent((HV*)SvRV(*keys), keysv, newSViv(ind), 0);
+    }
+    return av_fetch(av, ind, lval);
+}
+
+SV**
+avhv_store(AV *av, char *key, U32 klen, SV *val, U32 hash)
+{
+    SV **keys, **indsvp;
+    I32 ind;
+    
+    keys = av_fetch(av, 0, FALSE);
+    if (!keys || !SvROK(*keys) || SvTYPE(SvRV(*keys)) != SVt_PVHV)
+       croak("Can't coerce array into hash");
+    indsvp = hv_fetch((HV*)SvRV(*keys), key, klen, FALSE);
+    if (indsvp) {
+       ind = SvIV(*indsvp);
+       if (ind < 1)
+           croak("Bad index while coercing array into hash");
+    } else {
+       ind = AvFILL(av) + 1;
+       hv_store((HV*)SvRV(*keys), key, klen, newSViv(ind), hash);
+    }
+    return av_store(av, ind, val);
+}
+
+SV**
+avhv_store_ent(AV *av, SV *keysv, SV *val, U32 hash)
+{
+    SV **keys;
+    HE *he;
+    I32 ind;
+    
+    keys = av_fetch(av, 0, FALSE);
+    if (!keys || !SvROK(*keys) || SvTYPE(SvRV(*keys)) != SVt_PVHV)
+       croak("Can't coerce array into hash");
+    he = hv_fetch_ent((HV*)SvRV(*keys), keysv, FALSE, hash);
+    if (he) {
+       ind = SvIV(HeVAL(he));
+       if (ind < 1)
+           croak("Bad index while coercing array into hash");
+    } else {
+       ind = AvFILL(av) + 1;
+       hv_store_ent((HV*)SvRV(*keys), keysv, newSViv(ind), hash);
+    }
+    return av_store(av, ind, val);
+}
+
+bool
+avhv_exists_ent(AV *av, SV *keysv, U32 hash)
+{
+    SV **keys;
+    
+    keys = av_fetch(av, 0, FALSE);
+    if (!keys || !SvROK(*keys) || SvTYPE(SvRV(*keys)) != SVt_PVHV)
+       croak("Can't coerce array into hash");
+    return hv_exists_ent((HV*)SvRV(*keys), keysv, hash);
+}
+
+bool
+avhv_exists(AV *av, char *key, U32 klen)
+{
+    SV **keys;
+    
+    keys = av_fetch(av, 0, FALSE);
+    if (!keys || !SvROK(*keys) || SvTYPE(SvRV(*keys)) != SVt_PVHV)
+       croak("Can't coerce array into hash");
+    return hv_exists((HV*)SvRV(*keys), key, klen);
+}
+
+/* avhv_delete leaks. Caller can re-index and compress if so desired. */
+SV *
+avhv_delete(AV *av, char *key, U32 klen, I32 flags)
+{
+    SV **keys;
+    SV *sv;
+    SV **svp;
+    I32 ind;
+    
+    keys = av_fetch(av, 0, FALSE);
+    if (!keys || !SvROK(*keys) || SvTYPE(SvRV(*keys)) != SVt_PVHV)
+       croak("Can't coerce array into hash");
+    sv = hv_delete((HV*)SvRV(*keys), key, klen, 0);
+    if (!sv)
+       return Nullsv;
+    ind = SvIV(sv);
+    if (ind < 1)
+       croak("Bad index while coercing array into hash");
+    svp = av_fetch(av, ind, FALSE);
+    if (!svp)
+       return Nullsv;
+    if (flags & G_DISCARD) {
+       sv = Nullsv;
+       SvREFCNT_dec(*svp);
+    } else {
+       sv = sv_2mortal(*svp);
+    }
+    *svp = &sv_undef;
+    return sv;
+}
+
+/* avhv_delete_ent leaks. Caller can re-index and compress if so desired. */
+SV *
+avhv_delete_ent(AV *av, SV *keysv, I32 flags, U32 hash)
+{
+    SV **keys;
+    SV *sv;
+    SV **svp;
+    I32 ind;
+    
+    keys = av_fetch(av, 0, FALSE);
+    if (!keys || !SvROK(*keys) || SvTYPE(SvRV(*keys)) != SVt_PVHV)
+       croak("Can't coerce array into hash");
+    sv = hv_delete_ent((HV*)SvRV(*keys), keysv, 0, hash);
+    if (!sv)
+       return Nullsv;
+    ind = SvIV(sv);
+    if (ind < 1)
+       croak("Bad index while coercing array into hash");
+    svp = av_fetch(av, ind, FALSE);
+    if (!svp)
+       return Nullsv;
+    if (flags & G_DISCARD) {
+       sv = Nullsv;
+       SvREFCNT_dec(*svp);
+    } else {
+       sv = sv_2mortal(*svp);
+    }
+    *svp = &sv_undef;
+    return sv;
+}
+
+I32
+avhv_iterinit(AV *av)
+{
+    SV **keys;
+    
+    keys = av_fetch(av, 0, FALSE);
+    if (!keys || !SvROK(*keys) || SvTYPE(SvRV(*keys)) != SVt_PVHV)
+       croak("Can't coerce array into hash");
+    return hv_iterinit((HV*)SvRV(*keys));
+}
+
+HE *
+avhv_iternext(AV *av)
+{
+    SV **keys;
+    
+    keys = av_fetch(av, 0, FALSE);
+    if (!keys || !SvROK(*keys) || SvTYPE(SvRV(*keys)) != SVt_PVHV)
+       croak("Can't coerce array into hash");
+    return hv_iternext((HV*)SvRV(*keys));
+}
+
+SV *
+avhv_iterval(AV *av, register HE *entry)
+{
+    SV **keys;
+    SV *sv;
+    I32 ind;
+    
+    keys = av_fetch(av, 0, FALSE);
+    if (!keys || !SvROK(*keys) || SvTYPE(SvRV(*keys)) != SVt_PVHV)
+       croak("Can't coerce array into hash");
+    sv = hv_iterval((HV*)SvRV(*keys), entry);
+    ind = SvIV(sv);
+    if (ind < 1)
+       croak("Bad index while coercing array into hash");
+    return *av_fetch(av, ind, TRUE);
+}
+
+SV *
+avhv_iternextsv(AV *av, char **key, I32 *retlen)
+{
+    SV **keys;
+    HE *he;
+    SV *sv;
+    I32 ind;
+    
+    keys = av_fetch(av, 0, FALSE);
+    if (!keys || !SvROK(*keys) || SvTYPE(SvRV(*keys)) != SVt_PVHV)
+       croak("Can't coerce array into hash");
+    if ( (he = hv_iternext((HV*)SvRV(*keys))) == NULL)
+        return NULL;
+    *key = hv_iterkey(he, retlen);
+    sv = hv_iterval((HV*)SvRV(*keys), he);
+    ind = SvIV(sv);
+    if (ind < 1)
+       croak("Bad index while coercing array into hash");
+    return *av_fetch(av, ind, TRUE);
+}