1
- /* ***********************************************************************************[ParseUtils.h]
2
- Copyright (c) 2003-2006, Niklas Een, Niklas Sorensson
3
- Copyright (c) 2007-2010, Niklas Sorensson
4
1
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 zmaxsat_ParseUtils_h
22
- #define zmaxsat_ParseUtils_h
23
-
24
- #include < stdlib.h>
25
2
#include < stdio.h>
26
-
27
3
#include < zlib.h>
4
+ // #include "SolverTypes.h"
5
+ #include " Solver.h"
28
6
29
7
namespace zmaxsat {
30
-
31
- // -------------------------------------------------------------------------------------------------
32
- // A simple buffered character stream class:
33
-
8
+ // ==============================================================================================================
9
+ // data structure for input function --parseutils.h
34
10
static const int buffer_size = 1048576 ;
35
-
36
-
37
11
class StreamBuffer {
38
12
gzFile in;
39
13
unsigned char buf[buffer_size];
@@ -44,7 +18,7 @@ class StreamBuffer {
44
18
if (pos >= size) {
45
19
pos = 0 ;
46
20
size = gzread (in, buf, sizeof (buf)); } }
47
-
21
+ // circulate reading
48
22
public:
49
23
explicit StreamBuffer (gzFile i) : in(i), pos(0 ), size(0 ) { assureLookahead (); }
50
24
@@ -53,32 +27,24 @@ class StreamBuffer {
53
27
int position () const { return pos; }
54
28
};
55
29
56
-
57
30
// -------------------------------------------------------------------------------------------------
58
31
// End-of-file detection functions for StreamBuffer and char*:
59
-
60
-
61
32
static inline bool isEof (StreamBuffer& in) { return *in == EOF; }
62
33
static inline bool isEof (const char * in) { return *in == ' \0 ' ; }
63
-
64
34
// -------------------------------------------------------------------------------------------------
65
35
// Generic parse functions parametrized over the input-stream type.
66
-
67
-
68
36
template <class B >
69
37
static void skipWhitespace (B& in) {
70
38
while ((*in >= 9 && *in <= 13 ) || *in == 32 )
71
39
++in; }
72
40
73
-
74
41
template <class B >
75
42
static void skipLine (B& in) {
76
43
for (;;){
77
44
if (isEof (in)) return ;
78
45
if (*in == ' \n ' ) { ++in; return ; }
79
46
++in; } }
80
-
81
-
47
+ // 函数功能是把字符串转换成数字
82
48
template <class B >
83
49
static int parseInt (B& in) {
84
50
int val = 0 ;
@@ -91,8 +57,6 @@ static int parseInt(B& in) {
91
57
val = val*10 + (*in - ' 0' ),
92
58
++in;
93
59
return neg ? -val : val; }
94
-
95
-
96
60
// String matching: in case of a match the input iterator will be advanced the corresponding
97
61
// number of characters.
98
62
template <class B >
@@ -101,10 +65,8 @@ static bool match(B& in, const char* str) {
101
65
for (i = 0 ; str[i] != ' \0 ' ; i++)
102
66
if (in[i] != str[i])
103
67
return false ;
104
-
105
68
in += i;
106
-
107
- return true ;
69
+ return true ;
108
70
}
109
71
110
72
// String matching: consumes characters eagerly, but does not require random access iterator.
@@ -115,8 +77,71 @@ static bool eagerMatch(B& in, const char* str) {
115
77
return false ;
116
78
return true ; }
117
79
80
+ // =================================================================================================
81
+ // input fuctions for instance:
82
+
83
+ template <class B , class Solver >
84
+ static void readClause (B& in, Solver& S, vec<Lit>& lits) {
85
+ int parsed_lit, var;
86
+ lits.clear ();
87
+ for (;;){
88
+ parsed_lit = parseInt (in);
89
+ if (parsed_lit == 0 ) break ;
90
+ var = abs (parsed_lit)-1 ;
91
+ while (var>= S.nVars ()) S.newVar ();
92
+ lits.push ( (parsed_lit > 0 ) ? mkLit (var) : ~ mkLit (var) );
93
+ }
94
+ }
95
+
96
+ template <class B , class Solver >
97
+ static void parse_DIMACS_main (B& in, Solver& S) {
98
+ vec<Lit> lits;
99
+ int cnt= 0 ;
100
+ for (;;){
101
+ skipWhitespace (in); // 略过空格
102
+ if (*in == EOF) break ; // 读文件结束
103
+ else if (*in == ' p' ){
104
+ if (eagerMatch (in, " p cnf" )){
105
+ S.setnVars (parseInt (in)); // 获取变元个数
106
+ S.setnClauses (parseInt (in)); // 获取子句个数
107
+ S.setisWeight (0 );
108
+ }
109
+ else if (eagerMatch (in, " p wcnf" )){
110
+ S.setnVars (parseInt (in)); // 获取变元个数
111
+ S.setnClauses (parseInt (in)); // 获取子句个数
112
+ S.setisWeight (1 );
113
+ if (*in!=' \n ' ){
114
+ skipWhitespace (in);
115
+ int hardweight=0 ;
116
+ while (*in >= ' 0' && *in <= ' 9' ){
117
+ hardweight= hardweight*10 + (*in - ' 0' ),
118
+ ++in;
119
+ }
120
+ S.setHardWeight (hardweight);
121
+ }
122
+ if (S.getHardWeight ()>0 )
123
+ S.setPartial (1 );
124
+ }
125
+ }
126
+ else if (*in == ' c' || *in!= ' p' )
127
+ skipLine (in);// 略去文件开始部分的说明性信息
128
+ else { // 开始读入每个子句
129
+ cnt++;
130
+ readClause (in, S, lits);
131
+ S.addClause (lits);
132
+ }
133
+ }// end for
134
+ if (cnt!=S.getnClauses ())
135
+ fprintf (stderr, " WARNING! DIMACS header mismatch: wrong number of clauses.\n " );
136
+ }
137
+
138
+ // Inserts problem into solver.
139
+ //
140
+ template <class Solver >
141
+ static void buildInstance (gzFile input_stream, Solver& S) {
142
+ StreamBuffer in (input_stream); // 为文件分配一个读入的字符流缓存
143
+ parse_DIMACS_main (in, S); }// 读入算例的子句等信息
118
144
119
145
// =================================================================================================
120
146
}
121
147
122
- #endif
0 commit comments