forked from samehkhamis/RegionPushRelabel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DimacsReader.tpl
196 lines (173 loc) · 4.07 KB
/
DimacsReader.tpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/////////////////////////////////////////////////////////////////////////////
// Filename: DimacsReader.h
// Author: Sameh Khamis
//
// Description: Dimacs reader
/////////////////////////////////////////////////////////////////////////////
#pragma once
#include "DimacsReader.h"
template <typename Solver>
DimacsReader<Solver>::DimacsReader(string filename, long* sizes)
{
file.open(filename, 8*1024*1024); // 8 MB
dims = sizes;
solver = NULL;
}
template <typename Solver>
DimacsReader<Solver>::~DimacsReader()
{
file.close();
handle_destruction();
}
template <typename Solver>
inline void DimacsReader<Solver>::handle_construction(IdType nnodes, IdType nedges)
{
if (solver == NULL)
{
//solver = new Solver(nnodes, nedges);
solver = new Solver(dims);
solver->add_node(nnodes);
}
}
template <typename Solver>
inline void DimacsReader<Solver>::handle_destruction()
{
if (solver != NULL)
{
delete solver;
solver = NULL;
}
}
template <typename Solver>
inline void DimacsReader<Solver>::handle_comment(string comment)
{
}
template <typename Solver>
inline void DimacsReader<Solver>::handle_from_source_edge(IdType node, FlowType cap)
{
solver->add_terminal_weights(node, cap, (FlowType)0);
}
template <typename Solver>
inline void DimacsReader<Solver>::handle_to_sink_edge(IdType node, FlowType cap)
{
solver->add_terminal_weights(node, (FlowType)0, cap);
}
template <typename Solver>
inline void DimacsReader<Solver>::handle_edge(IdType node_i, IdType node_j, CapType cap)
{
// If this is the sister of the previous edge, add them together
if (prev_from == node_j && prev_to == node_i)
{
solver->add_edge(prev_from, prev_to, prev_cap, cap);
prev_from = -1;
}
else
{
if (prev_from != -1)
solver->add_edge(prev_from, prev_to, prev_cap, (CapType)0);
prev_from = node_i;
prev_to = node_j;
prev_cap = cap;
}
}
template <typename Solver>
inline void DimacsReader<Solver>::handle_source_sink_edge(CapType cap)
{
solver->add_constant_to_flow(cap);
}
template <typename Solver>
bool DimacsReader<Solver>::parse()
{
if (!file.is_open())
{
return false;
}
prev_from = -1;
file.seekg(0, ios_base::beg);
bool problem = false;
IdType source = 1, sink = 2;
size_t lineno = 0;
string line;
const char* cline;
while (!file.eof())
{
getline(file, line);
cline = line.c_str();
lineno++;
if (line.empty())
continue;
if (cline[0] == 'c')
{
if (line.length() > 2)
handle_comment(line.substr(2));
}
else if (cline[0] == 'p')
{
size_t nnodes, nedges;
if (sscanf(cline, "%*c %*3s %lld %lld", &nnodes, &nedges) != 2)
break;
handle_construction((IdType)nnodes - 2, (IdType)nedges);
problem = true;
}
else if (cline[0] == 'n')
{
size_t node;
char type;
if (sscanf(cline, "%*c %lld %c", &node, &type) != 2)
break;
if (type == 's')
source = node;
else if (type == 't')
sink = node;
else
break;
}
else if (cline[0] == 'a')
{
size_t node_i, node_j;
long long cap;
if (sscanf(cline, "%*c %lld %lld %lld", &node_i, &node_j, &cap) != 3)
break;
if (node_i == source)
{
if (node_j == sink)
handle_source_sink_edge((CapType)cap);
else
{
if (source < node_j) node_j--;
if (sink <= node_j) node_j--;
handle_from_source_edge((IdType)node_j - 1, (FlowType)cap);
}
}
else if (node_j == sink)
{
if (source < node_i) node_i--;
if (sink <= node_i) node_i--;
handle_to_sink_edge((IdType)node_i - 1, (FlowType)cap);
}
else
{
if (source < node_i) node_i--;
if (sink <= node_i) node_i--;
if (source < node_j) node_j--;
if (sink <= node_j) node_j--;
handle_edge((IdType)node_i - 1, (IdType)node_j - 1, (CapType)cap);
}
}
else
{
break;
}
}
if (!file.eof())
{
handle_destruction();
return false;
}
else
{
// Handle last edge if not handled already
handle_edge((IdType)-1, (IdType)-1, (CapType)0);
return true;
}
}