(void)av_store(av,fill,&sv_undef);
}
+
+HV*
+avhv_keys(AV *av)
+{
+ SV **keysp;
+ HV *keys = Nullhv;
+
+ keysp = av_fetch(av, 0, FALSE);
+ if (keysp) {
+ if (SvGMAGICAL(*keysp))
+ mg_get(*keysp);
+ if (SvROK(*keysp)) {
+ SV *hash = SvRV(*keysp);
+ if (SvTYPE(hash) == SVt_PVHV)
+ keys = (HV*)hash;
+ }
+ }
+ if (!keys)
+ croak("Can't coerce array into hash");
+ return keys;
+}
+
SV**
avhv_fetch(AV *av, char *key, U32 klen, I32 lval)
{
- SV **keys, **indsvp;
+ SV **indsvp;
+ HV *keys = avhv_keys(av);
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);
+ indsvp = hv_fetch(keys, key, klen, FALSE);
if (indsvp) {
ind = SvIV(*indsvp);
if (ind < 1)
return 0;
ind = AvFILL(av) + 1;
- hv_store((HV*)SvRV(*keys), key, klen, newSViv(ind), 0);
+ hv_store(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;
+ SV **indsvp;
+ HV *keys = avhv_keys(av);
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);
+ he = hv_fetch_ent(keys, keysv, FALSE, hash);
if (he) {
ind = SvIV(HeVAL(he));
if (ind < 1)
return 0;
ind = AvFILL(av) + 1;
- hv_store_ent((HV*)SvRV(*keys), keysv, newSViv(ind), 0);
+ hv_store_ent(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;
+ SV **indsvp;
+ HV *keys = avhv_keys(av);
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);
+ indsvp = hv_fetch(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);
+ hv_store(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;
+ HV *keys = avhv_keys(av);
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);
+ he = hv_fetch_ent(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);
+ hv_store_ent(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);
+ HV *keys = avhv_keys(av);
+ return hv_exists_ent(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);
+ HV *keys = avhv_keys(av);
+ return hv_exists(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;
+ HV *keys = avhv_keys(av);
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);
+ sv = hv_delete(keys, key, klen, 0);
if (!sv)
return Nullsv;
ind = SvIV(sv);
SV *
avhv_delete_ent(AV *av, SV *keysv, I32 flags, U32 hash)
{
- SV **keys;
+ HV *keys = avhv_keys(av);
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);
+ sv = hv_delete_ent(keys, keysv, 0, hash);
if (!sv)
return Nullsv;
ind = SvIV(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));
+ HV *keys = avhv_keys(av);
+ return hv_iterinit(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));
+ HV *keys = avhv_keys(av);
+ return hv_iternext(keys);
}
SV *
avhv_iterval(AV *av, register HE *entry)
{
- SV **keys;
+ HV *keys = avhv_keys(av);
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);
+ sv = hv_iterval(keys, entry);
ind = SvIV(sv);
if (ind < 1)
croak("Bad index while coercing array into hash");
SV *
avhv_iternextsv(AV *av, char **key, I32 *retlen)
{
- SV **keys;
+ HV *keys = avhv_keys(av);
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;
+ he = hv_iternext(keys);
+ if (!he)
+ return Nullsv;
*key = hv_iterkey(he, retlen);
- sv = hv_iterval((HV*)SvRV(*keys), he);
+ sv = hv_iterval(keys, he);
ind = SvIV(sv);
if (ind < 1)
croak("Bad index while coercing array into hash");