summaryrefslogtreecommitdiff
path: root/av.c
diff options
context:
space:
mode:
authorGurusamy Sarathy <gsar@cpan.org>2000-01-13 06:49:03 +0000
committerGurusamy Sarathy <gsar@cpan.org>2000-01-13 06:49:03 +0000
commit010205895f86f073b0b2a20bd4cfbb05f0134888 (patch)
treec882a2e58a11ea7da9d88e25008bea82fca68ee2 /av.c
parent629ae16350754a5d73cb2e1548dcefcae5ddeda1 (diff)
downloadperl-010205895f86f073b0b2a20bd4cfbb05f0134888.tar.gz
support delete() and exists() on array, tied array, and pseudo-hash
elements or slices p4raw-id: //depot/perl@4796
Diffstat (limited to 'av.c')
-rw-r--r--av.c106
1 files changed, 91 insertions, 15 deletions
diff --git a/av.c b/av.c
index 8f3b4f8bda..3b7e81338a 100644
--- a/av.c
+++ b/av.c
@@ -591,6 +591,83 @@ Perl_av_fill(pTHX_ register AV *av, I32 fill)
(void)av_store(av,fill,&PL_sv_undef);
}
+SV *
+Perl_av_delete(pTHX_ AV *av, I32 key, I32 flags)
+{
+ SV *sv;
+
+ if (!av)
+ return Nullsv;
+ if (SvREADONLY(av))
+ Perl_croak(aTHX_ PL_no_modify);
+ if (key < 0) {
+ key += AvFILL(av) + 1;
+ if (key < 0)
+ return Nullsv;
+ }
+ if (SvRMAGICAL(av)) {
+ SV **svp;
+ if ((mg_find((SV*)av,'P') || mg_find((SV*)av,'D'))
+ && (svp = av_fetch(av, key, TRUE)))
+ {
+ sv = *svp;
+ mg_clear(sv);
+ if (mg_find(sv, 'p')) {
+ sv_unmagic(sv, 'p'); /* No longer an element */
+ return sv;
+ }
+ return Nullsv; /* element cannot be deleted */
+ }
+ }
+ if (key > AvFILLp(av))
+ return Nullsv;
+ else {
+ sv = AvARRAY(av)[key];
+ if (key == AvFILLp(av)) {
+ do {
+ AvFILLp(av)--;
+ } while (--key >= 0 && AvARRAY(av)[key] == &PL_sv_undef);
+ }
+ else
+ AvARRAY(av)[key] = &PL_sv_undef;
+ if (SvSMAGICAL(av))
+ mg_set((SV*)av);
+ }
+ if (flags & G_DISCARD) {
+ SvREFCNT_dec(sv);
+ sv = Nullsv;
+ }
+ return sv;
+}
+
+/*
+ * This relies on the fact that uninitialized array elements
+ * are set to &PL_sv_undef.
+ */
+
+bool
+Perl_av_exists(pTHX_ AV *av, I32 key)
+{
+ if (!av)
+ return FALSE;
+ if (key < 0) {
+ key += AvFILL(av) + 1;
+ if (key < 0)
+ return FALSE;
+ }
+ if (SvRMAGICAL(av)) {
+ if (mg_find((SV*)av,'P') || mg_find((SV*)av,'D')) {
+ SV *sv = sv_newmortal();
+ mg_copy((SV*)av, sv, 0, key);
+ magic_existspack(sv, mg_find(sv, 'p'));
+ return SvTRUE(sv);
+ }
+ }
+ if (av_fetch(av, key, 0))
+ return TRUE;
+ else
+ return FALSE;
+}
/* AVHV: Support for treating arrays as if they were hashes. The
* first element of the array should be a hash reference that maps
@@ -638,34 +715,33 @@ Perl_avhv_fetch_ent(pTHX_ AV *av, SV *keysv, I32 lval, U32 hash)
return av_fetch(av, avhv_index_sv(HeVAL(he)), lval);
}
+SV *
+Perl_avhv_delete_ent(pTHX_ AV *av, SV *keysv, I32 flags, U32 hash)
+{
+ HV *keys = avhv_keys(av);
+ HE *he;
+
+ he = hv_fetch_ent(keys, keysv, FALSE, hash);
+ if (!he || !SvOK(HeVAL(he)))
+ return Nullsv;
+
+ return av_delete(av, avhv_index_sv(HeVAL(he)), flags);
+}
+
/* Check for the existence of an element named by a given key.
*
- * This relies on the fact that uninitialized array elements
- * are set to &PL_sv_undef.
*/
bool
Perl_avhv_exists_ent(pTHX_ AV *av, SV *keysv, U32 hash)
{
HV *keys = avhv_keys(av);
HE *he;
- IV ix;
he = hv_fetch_ent(keys, keysv, FALSE, hash);
if (!he || !SvOK(HeVAL(he)))
return FALSE;
- ix = SvIV(HeVAL(he));
-
- /* If the array hasn't been extended to reach the key yet then
- * it hasn't been accessed and thus does not exist. We use
- * AvFILL() rather than AvFILLp() to handle tied av. */
- if (ix > 0 && ix <= AvFILL(av)
- && (SvRMAGICAL(av)
- || (AvARRAY(av)[ix] && AvARRAY(av)[ix] != &PL_sv_undef)))
- {
- return TRUE;
- }
- return FALSE;
+ return av_exists(av, avhv_index_sv(HeVAL(he)));
}
HE *