From bd1de148f3be7f324796fdf232de1510dec310a5 Mon Sep 17 00:00:00 2001 From: Nico Buchholz Date: Sun, 20 Nov 2022 13:01:32 +0100 Subject: [PATCH] picosat-632 --- LICENSE | 20 + README | 1 + README.md | 1 - app.c | 744 ++++++++ configure | 109 ++ main.c | 7 + makefile.in | 30 + mkconfig | 19 + picosat.c | 4996 +++++++++++++++++++++++++++++++++++++++++++++++++++ picosat.h | 188 ++ 10 files changed, 6114 insertions(+), 1 deletion(-) create mode 100644 LICENSE create mode 100644 README delete mode 100644 README.md create mode 100644 app.c create mode 100755 configure create mode 100644 main.c create mode 100644 makefile.in create mode 100755 mkconfig create mode 100644 picosat.c create mode 100644 picosat.h diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..5e71311 --- /dev/null +++ b/LICENSE @@ -0,0 +1,20 @@ +Copyright (c) 2006 - 2007, Armin Biere, Johannes Kepler University. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to +deal in the Software without restriction, including without limitation the +rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +sell copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +IN THE SOFTWARE. + diff --git a/README b/README new file mode 100644 index 0000000..486cf2f --- /dev/null +++ b/README @@ -0,0 +1 @@ +This is the PicoSAT solver. To compile run './configure && make'. diff --git a/README.md b/README.md deleted file mode 100644 index 8ea01d4..0000000 --- a/README.md +++ /dev/null @@ -1 +0,0 @@ -# picoSAT-mirror \ No newline at end of file diff --git a/app.c b/app.c new file mode 100644 index 0000000..65c3665 --- /dev/null +++ b/app.c @@ -0,0 +1,744 @@ +#include "picosat.h" + +#include +#include +#include +#include + +#define GUNZIP "gunzip -c %s" +#define GZIP "gzip -c -f > %s" + +FILE * popen (const char *, const char*); +int pclose (FILE *); + +static int lineno; +static FILE *input; +static FILE *output; +static int verbose; +static char buffer[100]; +static char *bhead = buffer; +static const char *eob = buffer + 80; +static FILE * incremental_rup_file; + +static int +next (void) +{ + int res = getc (input); + if (res == '\n') + lineno++; + return res; +} + +static const char * +parse (int force) +{ + int ch, sign, lit, vars, clauses; + + lineno = 1; + +SKIP_COMMENTS: + ch = next (); + if (ch == 'c') + { + while ((ch = next ()) != EOF && ch != '\n') + ; + goto SKIP_COMMENTS; + } + + if (isspace (ch)) + goto SKIP_COMMENTS; + + if (ch != 'p') +INVALID_HEADER: + return "missing or invalid 'p cnf ' header"; + + if (fscanf (input, " cnf %d %d\n", &vars, &clauses) != 2) + goto INVALID_HEADER; + + if (verbose) + { + fprintf (output, "c parsed header 'p cnf %d %d'\n", vars, clauses); + fflush (output); + } + + picosat_adjust (vars); + + if (incremental_rup_file) + picosat_set_incremental_rup_file (incremental_rup_file, vars, clauses); + + lit = 0; +READ_LITERAL: + ch = next (); + + if (ch == 'c') + { + while ((ch = next ()) != EOF && ch != '\n') + ; + goto READ_LITERAL; + } + + if (ch == EOF) + { + if (lit) + return "trailing 0 missing"; + + if (clauses && !force) + return "clause missing"; + + return 0; + } + + if (isspace (ch)) + goto READ_LITERAL; + + sign = 1; + if (ch == '-') + { + sign = -1; + ch = next (); + } + + if (!isdigit (ch)) + return "expected number"; + + lit = ch - '0'; + while (isdigit (ch = next ())) + lit = 10 * lit + (ch - '0'); + + if (!clauses && !force) + return "too many clauses"; + + if (lit) + { + if (lit > vars && !force) + return "maximal variable index exceeded"; + + lit *= sign; + } + else + clauses--; + + picosat_add (lit); + + goto READ_LITERAL; +} + +static void +bflush (void) +{ + *bhead = 0; + fputs (buffer, output); + fputc ('\n', output); + bhead = buffer; +} + +static void +printi (int i) +{ + char *next; + int l; + +REENTER: + if (bhead == buffer) + *bhead++ = 'v'; + + l = sprintf (bhead, " %d", i); + next = bhead + l; + + if (next >= eob) + { + bflush (); + goto REENTER; + } + else + bhead = next; +} + +static void +printa (void) +{ + int max_idx = picosat_variables (), i, lit; + + assert (bhead == buffer); + + for (i = 1; i <= max_idx; i++) + { + lit = (picosat_deref (i) > 0) ? i : -i; + printi (lit); + } + + printi (0); + if (bhead > buffer) + bflush (); +} + +static int +has_suffix (const char *str, const char *suffix) +{ + const char *tmp = strstr (str, suffix); + if (!tmp) + return 0; + + return str + strlen (str) - strlen (suffix) == tmp; +} + +static void +write_core_variables (FILE * file) +{ + int i, max_idx = picosat_variables (); + for (i = 1; i <= max_idx; i++) + if (picosat_corelit (i)) + fprintf (file, "%d\n", i); +} + +static void +write_used_variables (FILE * file) +{ + int i, max_idx = picosat_variables (); + for (i = 1; i <= max_idx; i++) + if (picosat_usedlit (i)) + fprintf (file, "%d\n", i); +} + +static void +write_to_file (const char *name, const char *type, void (*writer) (FILE *)) +{ + int pclose_file, zipped = has_suffix (name, ".gz"); + FILE *file; + char *cmd; + + if (zipped) + { + cmd = malloc (strlen (GZIP) + strlen (name)); + sprintf (cmd, GZIP, name); + file = popen (cmd, "w"); + free (cmd); + pclose_file = 1; + } + else + { + file = fopen (name, "w"); + pclose_file = 0; + } + + if (file) + { + if (verbose) + fprintf (output, + "c\nc writing %s%s to '%s'\n", + zipped ? "gzipped " : "", type, name); + + writer (file); + + if (pclose_file) + pclose (file); + else + fclose (file); + } + else + fprintf (output, "*** picosat: can not write to '%s'\n", name); +} + +#define USAGE \ +"usage: picosat [