X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=Nary.xs;h=116040c30e788468ee61455a2ca670e48adea5c9;hb=ac0f3ea3b3aba67758f18170c2df60a7fc536bf2;hp=2415760185f879bcddfba0c20c1ade5d93ab449f;hpb=cb3c15c6ac05289a612b1053102fd6f7442d9a40;p=perl%2Fmodules%2FSub-Nary.git diff --git a/Nary.xs b/Nary.xs index 2415760..116040c 100644 --- a/Nary.xs +++ b/Nary.xs @@ -50,6 +50,23 @@ CODE: res = hv_exists(hv, "0", 1) && hv_iterinit(hv) == 1; XSRETURN_IV(res); +void +list(SV *sv) +PROTOTYPE: $ +PREINIT: + HV *hv; + IV res; +CODE: + if (!SvOK(sv)) + XSRETURN_IV(0); + if (!SvROK(sv)) { + res = strEQ(SvPV_nolen(sv), "list"); + XSRETURN_IV(res); + } + hv = (HV *) SvRV(sv); + res = hv_exists(hv, "list", 4) && hv_iterinit(hv) == 1; + XSRETURN_IV(res); + void count(SV *sv) PROTOTYPE: $ @@ -69,6 +86,47 @@ CODE: } XSRETURN_NV(c); +void +normalize(SV *sv) +PROTOTYPE: $ +PREINIT: + HV *hv, *res; + HE *key; + SV *val; + NV c = 0; +CODE: + if (!SvOK(sv)) + XSRETURN_UNDEF; + res = newHV(); + if (!SvROK(sv)) { + val = newSVuv(1); + if (!hv_store_ent(res, sv, val, 0)) { + SvREFCNT_dec(val); + XSRETURN_UNDEF; + } + } else { + hv = (HV *) SvRV(sv); + if (!hv_iterinit(hv)) { + val = newSVuv(1); + if (!hv_store(res, "0", 1, val, 0)) { + SvREFCNT_dec(val); + XSRETURN_UNDEF; + } + } else { + while (key = hv_iternext(hv)) { + c += SvNV(HeVAL(key)); + } + hv_iterinit(hv); + while (key = hv_iternext(hv)) { + val = newSVnv(SvNV(HeVAL(key)) / c); + if (!hv_store_ent(res, HeSVKEY_force(key), val, HeHASH(key))) + SvREFCNT_dec(val); + } + } + } + ST(0) = sv_2mortal(newRV_noinc((SV *) res)); + XSRETURN(1); + void scalops() PROTOTYPE: