abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Dimacs.h
Go to the documentation of this file.
1 /****************************************************************************************[Dimacs.h]
2 Copyright (c) 2003-2006, Niklas Een, Niklas Sorensson
3 Copyright (c) 2007-2010, Niklas Sorensson
4 
5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
6 associated documentation files (the "Software"), to deal in the Software without restriction,
7 including without limitation the rights to use, copy, modify, merge, publish, distribute,
8 sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in all copies or
12 substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
15 NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
17 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
18 OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 **************************************************************************************************/
20 
21 #ifndef Minisat_Dimacs_h
22 #define Minisat_Dimacs_h
23 
24 #include <stdio.h>
25 
26 #include "ParseUtils.h"
27 #include "SolverTypes.h"
28 
29 namespace Minisat {
30 
31 //=================================================================================================
32 // DIMACS Parser:
33 
34 template<class B, class Solver>
35 static void readClause(B& in, Solver& S, vec<Lit>& lits) {
36  int parsed_lit, var;
37  lits.clear();
38  for (;;){
39  parsed_lit = parseInt(in);
40  if (parsed_lit == 0) break;
41  var = abs(parsed_lit)-1;
42  while (var >= S.nVars()) S.newVar();
43  lits.push( (parsed_lit > 0) ? mkLit(var) : ~mkLit(var) );
44  }
45 }
46 
47 template<class B, class Solver>
48 static void parse_DIMACS_main(B& in, Solver& S) {
49  vec<Lit> lits;
50  int vars = 0;
51  int clauses = 0;
52  int cnt = 0;
53  for (;;){
54  skipWhitespace(in);
55  if (*in == EOF) break;
56  else if (*in == 'p'){
57  if (eagerMatch(in, "p cnf")){
58  vars = parseInt(in);
59  clauses = parseInt(in);
60  // SATRACE'06 hack
61  // if (clauses > 4000000)
62  // S.eliminate(true);
63  }else{
64  printf("PARSE ERROR! Unexpected char: %c\n", *in), exit(3);
65  }
66  } else if (*in == 'c' || *in == 'p')
67  skipLine(in);
68  else{
69  cnt++;
70  readClause(in, S, lits);
71  S.addClause_(lits); }
72  }
73  if (vars != S.nVars())
74  fprintf(stderr, "WARNING! DIMACS header mismatch: wrong number of variables.\n");
75  if (cnt != clauses)
76  fprintf(stderr, "WARNING! DIMACS header mismatch: wrong number of clauses.\n");
77 }
78 
79 // Inserts problem into solver.
80 //
81 template<class Solver>
82 static void parse_DIMACS(gzFile input_stream, Solver& S) {
83  StreamBuffer in(input_stream);
84  parse_DIMACS_main(in, S); }
85 
86 //=================================================================================================
87 }
88 
89 #endif
VOID_HACK exit()
static bool eagerMatch(B &in, const char *str)
Definition: ParseUtils.h:112
static void readClause(B &in, Solver &S, vec< Lit > &lits)
Definition: Dimacs.h:35
int var(Lit p)
Definition: SolverTypes.h:62
Lit mkLit(Var var, bool sign)
Definition: SolverTypes.h:58
voidp gzFile
Definition: zlib.h:1173
void push(void)
Definition: Vec.h:73
static int parseInt(B &in)
Definition: ParseUtils.h:83
int nVars() const
Definition: Solver.h:328
static void parse_DIMACS_main(B &in, Solver &S)
Definition: Dimacs.h:48
static void skipWhitespace(B &in)
Definition: ParseUtils.h:69
bool addClause_(vec< Lit > &ps)
Definition: Solver.cpp:132
void clear(bool dealloc=false)
Definition: Vec.h:121
Var newVar(bool polarity=true, bool dvar=true)
Definition: Solver.cpp:114
static void parse_DIMACS(gzFile input_stream, Solver &S)
Definition: Dimacs.h:82
static shot S[256]
Definition: kitPerm.c:40
static void skipLine(B &in)
Definition: ParseUtils.h:75