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/* for loop over permutations in lexicographic order15* This implements the algorithm L in D. Knuth "The Art Of Computer Programming"16* Fascicle 2b */1718#include "pari.h"19#include "paripriv.h"2021void22forperm_init(forperm_t *T, GEN k)23{24switch (typ(k))25{26case t_INT:27if (signe(k) < 0) pari_err_DOMAIN("forperm", "a", "<", gen_0, k);28T->v = identity_perm(itou(k)); break;29case t_VEC:30T->v = vec_to_vecsmall(k); break;31case t_VECSMALL:32T->v = vecsmall_copy(k); break;33default:34pari_err_TYPE("forperm", k);35return; /* LCOV_EXCL_LINE */36}37T->first = 1;38T->k = lg(T->v) - 1;39}4041GEN42forperm_next(forperm_t *T)43{44long k = T->k, m1, m2, *p, *q;45GEN v = T->v;4647if (T->first) { T->first = 0; return v; }48m1 = k-1; while (m1 > 0 && v[m1] >= v[m1+1]) m1--;49if (m1 <= 0) return NULL;5051m2 = k; while (v[m1] >= v[m2]) m2--;52lswap(v[m1], v[m2]);53p = v + m1 + 1;54q = v + k;55while (p < q) { lswap(*p, *q); p++; q--; }56return v;57}5859void60forperm(void *E, long call(void *, GEN), GEN k)61{62pari_sp av = avma;63forperm_t T;64GEN v;6566forperm_init(&T, k);67while ((v = forperm_next(&T)))68if (call(E, v)) break;69set_avma(av);70}7172void73forperm0(GEN k, GEN code)74{75push_lex(gen_0, code);76forperm((void *)code, &gp_evalvoid, k);77pop_lex(1);78}79808182