yosys-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, bool strictp = false) {
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 (strictp && cnt != clauses)
74  printf("PARSE ERROR! DIMACS header mismatch: wrong number of clauses\n");
75 }
76 
77 // Inserts problem into solver.
78 //
79 template<class Solver>
80 static void parse_DIMACS(gzFile input_stream, Solver& S, bool strictp = false) {
81  StreamBuffer in(input_stream);
82  parse_DIMACS_main(in, S, strictp); }
83 
84 //=================================================================================================
85 }
86 
87 #endif
static bool eagerMatch(B &in, const char *str)
Definition: ParseUtils.h:109
static void readClause(B &in, Solver &S, vec< Lit > &lits)
Definition: Dimacs.h:35
Lit mkLit(Var var, bool sign=false)
Definition: SolverTypes.h:63
static void parse_DIMACS_main(B &in, Solver &S, bool strictp=false)
Definition: Dimacs.h:48
int var(Lit p)
Definition: SolverTypes.h:67
static int parseInt(B &in)
Definition: ParseUtils.h:80
int nVars() const
Definition: Solver.h:357
static void skipWhitespace(B &in)
Definition: ParseUtils.h:66
void push(void)
Definition: Vec.h:74
bool addClause_(vec< Lit > &ps)
Definition: Solver.cc:156
Var newVar(lbool upol=l_Undef, bool dvar=true)
Definition: Solver.cc:121
void clear(bool dealloc=false)
Definition: Vec.h:125
static void parse_DIMACS(gzFile input_stream, Solver &S, bool strictp=false)
Definition: Dimacs.h:80
static void skipLine(B &in)
Definition: ParseUtils.h:72