X-Git-Url: http://git.vpit.fr/?p=perl%2Fmodules%2FSub-Nary.git;a=blobdiff_plain;f=Nary.xs;fp=Nary.xs;h=0329eea6439cb62c80bcadb24546d02ef7f1abe8;hp=285bfc39973766318639023337c2f0916e90fb0f;hb=abc14695a4ce4fd4733c9185d87b774ee7c1a220;hpb=9e42e470d0042056765361fbcbf828492ee58d9c diff --git a/Nary.xs b/Nary.xs index 285bfc3..0329eea 100644 --- a/Nary.xs +++ b/Nary.xs @@ -6,6 +6,10 @@ #include "perl.h" #include "XSUB.h" +#ifndef Newx +# define Newx(v, n, c) New(0, v, n, c) +#endif + #ifndef mPUSHi # define mPUSHi(I) PUSHs(sv_2mortal(newSViv(I))) #endif /* !mPUSHi */ @@ -25,8 +29,8 @@ STATIC void sn_store_ent(pTHX_ HV *tb, SV *key, SV *val, U32 hash) { if (!hv_store_ent(tb, key, val, hash)) SvREFCNT_dec(val); } - STATIC U32 sn_hash_list = 0; +STATIC U32 sn_hash_exit = 0; /* --- XS ------------------------------------------------------------------ */ @@ -37,6 +41,7 @@ PROTOTYPES: ENABLE BOOT: { PERL_HASH(sn_hash_list, "list", 4); + PERL_HASH(sn_hash_exit, "exit", 4); } void @@ -241,95 +246,83 @@ void combine(...) PROTOTYPE: @ PREINIT: - HV *res[2]; - SV *cur, *val; - SV *list1, *list2; + HV *res[3]; + SV *val; + SV *exit1, *list1; SV *temp; HE *key, *old; I32 i; I32 n = 0, o; I32 j, n1, n2; - UV shift = 0, do_shift = 0; + NV pl = 0, pe = 0; sn_combcache *cache = NULL; - I32 cachelen = 0; + I32 cachelen = 1; CODE: if (!items) XSRETURN_UNDEF; - res[0] = res[1] = NULL; - for (i = 0; i < items; ++i) { - cur = ST(i); - if (!SvOK(cur)) - continue; - if (!SvROK(cur)) { - if (strEQ(SvPV_nolen(cur), "list")) { - res[0] = newHV(); - n = 0; - sn_store(res[0], "list", 4, newSVuv(1), sn_hash_list); - i = items; - if (!shift) - do_shift = 0; - break; - } else { - shift += SvUV(cur); - do_shift = 1; - continue; - } - } - cur = SvRV(cur); - res[0] = newHV(); - while (key = hv_iternext((HV *) cur)) - sn_store_ent(res[0], HeSVKEY_force(key), newSVsv(HeVAL(key)), 0); - n = 0; - if (!shift) - do_shift = 0; - break; - } + res[0] = newHV(); + res[1] = NULL; + res[2] = NULL; + Newx(cache, 1, sn_combcache); + n = 0; temp = sv_2mortal(newSViv(0)); - for (++i; i < items; ++i) { - cur = ST(i); + for (i = 0; i < items; ++i) { + SV *cur = ST(i); + NV pe1 = 0, pl1 = 0; if (!SvOK(cur)) continue; if (!SvROK(cur)) { - if (strEQ(SvPV_nolen(cur), "list")) { - hv_clear(res[n]); - sn_store(res[n], "list", 4, newSVuv(1), sn_hash_list); - shift = 0; - do_shift = 0; - break; - } else { - shift += SvUV(cur); - continue; - } - } - cur = SvRV(cur); - o = 1 - n; + if (!res[2]) + res[2] = newHV(); + else + hv_clear(res[2]); + sn_store_ent(res[2], cur, newSVuv(1), 0); + cur = (SV *) res[2]; + } else + cur = SvRV(cur); + o = 1 - n; if (!res[o]) res[o] = newHV(); else hv_clear(res[o]); - list1 = hv_delete((HV *) cur, "list", 4, 0); + exit1 = hv_delete((HV *) cur, "exit", 4, 0); n1 = hv_iterinit((HV *) cur); - list2 = hv_delete(res[n], "list", 4, 0); - n2 = hv_iterinit(res[n]); - if ((list1 && !n1) || (list2 && !n2)) { - sn_store(res[o], "list", 4, newSViv(1), sn_hash_list); - n = o; - break; - } else if (list1 || list2) { - NV l1 = list1 ? SvNV(list1) : 0; - NV l2 = list2 ? SvNV(list2) : 0; - val = newSVnv(l1 + l2 - l1 * l2); - sn_store(res[o], "list", 4, val, sn_hash_list); + if (exit1) { + if (!n1) { + pe = 1; + pl = 0; + n = o; + break; + } + pe1 = SvNV(exit1); } - if (n2 > cachelen) { - Renew(cache, n2, sn_combcache); - cachelen = n2; + list1 = hv_delete((HV *) cur, "list", 4, 0); + if (list1) { + if (n1 == 1) { + pl = 1 - pe; + n = o; + break; + } + pl1 = SvNV(list1); } - j = 0; - while (key = hv_iternext(res[n])) { - cache[j].k = SvUV(HeSVKEY_force(key)); - cache[j].v = SvNV(HeVAL(key)); - ++j; + pl = pl1 * (1 - pe) + pl * (1 - pe1) - pl * pl1; + pe = pe + (1 - pe) * pe1; + n2 = hv_iterinit(res[n]); + if (!n2) { + cache[0].k = 0; + cache[0].v = 1; + n2 = 1; + } else { + if (n2 > cachelen) { + Renew(cache, n2, sn_combcache); + cachelen = n2; + } + j = 0; + while (key = hv_iternext(res[n])) { + cache[j].k = SvUV(HeSVKEY_force(key)); + cache[j].v = SvNV(HeVAL(key)); + ++j; + } } while (key = hv_iternext((HV *) cur)) { IV k = SvUV(HeSVKEY_force(key)); @@ -347,35 +340,22 @@ CODE: n = o; } Safefree(cache); - if (shift || do_shift) { - if (!res[n]) { - res[n] = newHV(); - sv_setiv(temp, shift); - sn_store_ent(res[n], temp, newSViv(1), 0); - } else { - o = 1 - n; - if (!res[o]) - res[o] = newHV(); - else - hv_clear(res[o]); - list1 = hv_delete(res[n], "list", 4, 0); - hv_iterinit(res[n]); - while (key = hv_iternext(res[n])) { - sv_setiv(temp, SvUV(HeSVKEY_force(key)) + shift); - sn_store_ent(res[o], temp, newSVsv(HeVAL(key)), 0); - } - if (list1) - sn_store(res[o], "list", 4, newSVsv(list1), sn_hash_list); - n = o; - } - } else if (!res[0] && !res[1]) - XSRETURN_UNDEF; + SvREFCNT_dec(res[2]); + if (pe) + sn_store(res[n], "exit", 4, newSVnv(pe), sn_hash_exit); + if (pl) + sn_store(res[n], "list", 4, newSVnv(pl), sn_hash_list); if (n == 1) SvREFCNT_dec(res[0]); else if (res[1]) SvREFCNT_dec(res[1]); - ST(0) = sv_2mortal(newRV_noinc((SV *) res[n])); - XSRETURN(1); + if (!hv_iterinit(res[n])) { + SvREFCNT_dec(res[n]); + XSRETURN_UNDEF; + } else { + ST(0) = sv_2mortal(newRV_noinc((SV *) res[n])); + XSRETURN(1); + } void scalops()