Testing latest pari + WASM + node.js... and it works?! Wow.
License: GPL3
ubuntu2004
/* Copyright (C) 2017 The PARI group.12This file is part of the PARI/GP package.34PARI/GP is free software; you can redistribute it and/or modify it under the5terms of the GNU General Public License as published by the Free Software6Foundation; either version 2 of the License, or (at your option) any later7version. It is distributed in the hope that it will be useful, but WITHOUT8ANY WARRANTY WHATSOEVER.910Check the License for details. You should have received a copy of it, along11with the package; see the file 'COPYING'. If not, write to the Free Software12Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */1314#include "pari.h"15#include "paripriv.h"1617void18forksubset_init(forsubset_t *T, long n, long k)19{20T->all = 0;21T->first = 1;22T->n = n;23T->k = k;24T->v = identity_perm(k);25}2627void28forallsubset_init(forsubset_t *T, long n)29{30T->all = 1;31T->first = 1;32T->n = n;33T->k = 0;34T->v = vecsmalltrunc_init(n + 1);35}3637static GEN38forksubset_next(forsubset_t *T)39{40GEN v = T->v;41long i, n = T->n, k = T->k;4243if (T->first) { T->first = 0; return (k >= 0 && k <= n) ? v: NULL; }44if (k <= 0 || k >= n) return NULL;4546if (v[k] < n) { v[k]++; return v; }47for (i = k - 1; i >= 1 && v[i+1] == v[i] + 1; i--);48if (i == 0) return NULL;4950v[i]++;51for (; i < k; i++) v[i+1] = v[i] + 1;52return v;53}54static GEN55forallsubset_next(forsubset_t *T)56{57long i;5859if (forksubset_next(T)) return T->v;60else if (T->k < T->n)61{62(T->k)++;63setlg(T->v, T->k+1);64for (i = 1; i <= T->k; i++) (T->v)[i] = i;65return T->v;66}67return NULL;68}69GEN70forsubset_next(forsubset_t *T)71{ return T->all? forallsubset_next(T): forksubset_next(T); }72void73forsubset_init(forsubset_t *T, GEN nk)74{75switch(typ(nk))76{77case t_INT: forallsubset_init(T, itos(nk)); return;78case t_VEC:79if (lg(nk) == 3)80{81GEN n = gel(nk,1), k = gel(nk,2);82if (typ(n) == t_INT && typ(k) == t_INT)83return forksubset_init(T, itos(n),itos(k));84}85default:86pari_err_TYPE("forsubset", nk);87}88}89void90forsubset0(GEN nk, GEN code)91{92pari_sp av = avma;93forsubset_t T;94void *E = (void*)code;95GEN v;96push_lex(gen_0, code);97forsubset_init(&T, nk);98while ((v = forsubset_next(&T)))99if (gp_evalvoid(E, v)) break;100pop_lex(1); set_avma(av);101}102103104