-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecv.cpp
More file actions
316 lines (261 loc) · 9.09 KB
/
Copy pathecv.cpp
File metadata and controls
316 lines (261 loc) · 9.09 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/**
* @file ecv.cpp
* @brief Implementation of \a ecv.hpp
* @author lhm
*/
// Project's headers
#include <ecv.hpp>
namespace ecv {
namespace {
struct Column;
/*****************************************************************************/
template<typename T>
struct IDataObj
{
virtual ~IDataObj() noexcept = default;
virtual void remove(void) noexcept = 0;
virtual void restore(void) noexcept = 0;
virtual void erase(void) noexcept = 0;
T *_l{ nullptr }, *_r{ nullptr }, *_u{ nullptr }, *_d{ nullptr };
};
/*****************************************************************************/
struct Node : public IDataObj<Node>
{
virtual void remove(void) noexcept override;
virtual void restore(void) noexcept override;
virtual void erase(void) noexcept override;
int _row{ -1 };
Column* _col{ nullptr }; // Head
};
/*****************************************************************************/
struct Column : public IDataObj<Column>
{
virtual void remove(void) noexcept override;
virtual void restore(void) noexcept override;
virtual void erase(void) noexcept override;
public:
Node _head; // Head
int _size; // Number of ones in the column, used for branching optimization
bool _primary; // Does it correspond to an essential or optional constraint
};
/*****************************************************************************/
void
Node::remove(void) noexcept
{
_u->_d = _d;
_d->_u = _u;
--_col->_size;
}
/*****************************************************************************/
void
Node::restore(void) noexcept
{
_u->_d = this;
_d->_u = this;
++_col->_size;
}
/*****************************************************************************/
void
Node::erase(void) noexcept
{
remove();
_l->_r = _r;
_r->_l = _l;
}
/*****************************************************************************/
void
Column::remove(void) noexcept
{
_l->_r = _r;
_r->_l = _l;
for (auto ccell{ _head._d }; ccell != &_head; ccell = ccell->_d)
for (auto rcell{ ccell->_r }; rcell != ccell; rcell = rcell->_r)
rcell->remove();
}
/*****************************************************************************/
void
Column::restore(void) noexcept
{
for (auto ccell{ _head._u }; ccell != &_head; ccell = ccell->_u)
for (auto rcell{ ccell->_l }; rcell != ccell; rcell = rcell->_l)
rcell->restore();
_l->_r = this;
_r->_l = this;
}
/*****************************************************************************/
void
Column::erase(void) noexcept
{
remove();
_l->_r = _r;
_r->_l = _l;
}
} // anonymous
struct DLX::Impl
{
Column _head;
std::vector<Column> _cols;
std::vector<Node> _nodes;
std::vector<Solution> _solutions;
std::vector<int> _curSol;
Column* col_select(void) noexcept;
[[maybe_unused]] bool init(const std::vector<bool>& data,
size_t R,
size_t C,
const std::vector<int>& rowsList,
int primary) noexcept;
std::vector<Solution> solve(uint32_t) noexcept;
bool _solve(const uint32_t&, uint32_t&) noexcept;
auto zeros(void) noexcept { return (_head._r == &_head) && (_head._l == &_head); }
};
/*****************************************************************************/
Column*
DLX::Impl::col_select(void) noexcept
{
auto ret{ _head._r };
for (auto cdt{ ret->_r }; (cdt->_primary && &_head != cdt); cdt = cdt->_r) {
if (cdt->_size < ret->_size)
ret = cdt;
}
return ret;
}
/*****************************************************************************/
bool
DLX::Impl::init(const std::vector<bool>& data,
size_t R,
size_t C,
const std::vector<int>& rowsList,
int primary) noexcept
{
if (0 == R || 0 == C || std::size(data) != R * C)
return false;
if (0 > primary)
primary = C;
// Either rows ids are not provided, or incomplete
// In both case, use indexes instead.
bool rowsIdByIdx{ std::empty(rowsList) || (R != std::size(rowsList)) };
_curSol.reserve(R);
_cols.resize(C);
_nodes.resize(R * C);
_head._r = &_cols[0];
_head._l = &_cols[C - 1];
_head._primary = false;
_cols[0]._l = &_head;
_cols[C - 1]._r = &_head;
for (size_t i{ 0 }; i < C - 1; ++i) {
_cols[i]._r = &_cols[i + 1];
_cols[i + 1]._l = &_cols[i];
}
for (size_t i{ 0 }; i < C; ++i) {
_cols[i]._size = R;
_cols[i]._primary = (static_cast<int>(i) < primary);
}
for (size_t i{ 0 }, k{ 0 }; i < R; ++i) {
for (size_t j{ 0 }; j < C; ++j, ++k) {
if (k < C)
_cols[j]._head._d = &_nodes[k];
if (k >= (R - 1) * C)
_cols[j]._head._u = &_nodes[k];
_nodes[k]._row = rowsIdByIdx ? i : rowsList[i];
_nodes[k]._col = &_cols[j];
_nodes[k]._l = (0 == k % C) ? &_nodes[k + C - 1] : &_nodes[k - 1];
_nodes[k]._r = (C - 1 == k % C) ? &_nodes[k - C + 1] : &_nodes[k + 1];
_nodes[k]._u = (k < C) ? &_cols[j]._head : &_nodes[k - C];
_nodes[k]._d = (k >= (R - 1) * C) ? &_cols[j]._head : &_nodes[k + C];
}
}
for (size_t i{ 0 }, k{ 0 }; i < R; ++i)
for (size_t j{ 0 }; j < C; ++j, ++k)
if (!data[k])
_nodes[k].erase();
return true;
}
/*****************************************************************************/
std::vector<DLX::Solution>
DLX::Impl::solve(uint32_t max_solutions = std::numeric_limits<uint32_t>::max()) noexcept
{
uint32_t sol_count{ 0 };
_solutions.clear();
if (!std::empty(_nodes))
_solve(max_solutions, sol_count);
return _solutions;
}
/*****************************************************************************/
DLX::DLX(const std::vector<bool>& data,
size_t rows,
size_t cols,
const std::vector<int>& rowsList,
int primary) noexcept
: pimpl{ std::make_shared<Impl>() }
{
pimpl->init(data, rows, cols, rowsList, primary);
}
/*****************************************************************************/
bool
DLX::Impl::_solve(const uint32_t& max_solutions, uint32_t& sol_count) noexcept
{
if (max_solutions == sol_count)
return true;
// Apply DLX algorithm (recursive, non-deterministic)
// No more primary constraints, only optionals. We are good to go
if (!_head._r->_primary || zeros()) { // success
_solutions.emplace_back(_curSol);
++sol_count;
return true;
}
auto curCol{ col_select() };
if (0 == curCol->_size) // failure
return false;
// The recursive dance
curCol->remove();
for (auto cRow{ curCol->_head._d }; &curCol->_head != cRow; cRow = cRow->_d) {
for (auto cCol{ cRow->_r }; cRow != cCol; cCol = cCol->_r) {
cCol->_col->remove();
_curSol.push_back(cCol->_row);
}
_solve(max_solutions, sol_count);
for (auto cCol{ cRow->_l }; cRow != cCol; cCol = cCol->_l) {
cCol->_col->restore();
_curSol.pop_back();
}
}
curCol->restore();
return false;
}
/*****************************************************************************/
std::vector<DLX::Solution>
DLX::solve(uint32_t max_solutions) noexcept
{
return pimpl->solve(max_solutions);
}
/*****************************************************************************/
std::unique_ptr<GenericProblem>
GenericProblem::generate(const std::vector<bool>& data,
size_t rows,
size_t cols,
int primary) noexcept
{
struct shared_enabler : public GenericProblem
{
shared_enabler(const std::vector<bool>& data, size_t rows, size_t cols, int primary)
: GenericProblem(data, rows, cols, primary)
{}
};
return std::make_unique<shared_enabler>(data, rows, cols, primary);
}
/*****************************************************************************/
GenericProblem::GenericProblem(const std::vector<bool>& data,
size_t rows,
size_t cols,
int primary) noexcept
: DLX(data, rows, cols, {}, primary)
{}
/*****************************************************************************/
ConcreteProblem::ConcreteProblem(const std::vector<bool>& data,
size_t rows,
size_t cols,
const std::vector<int>& rowsList,
int primary) noexcept
: DLX(data, rows, cols, rowsList, primary)
{}
} // namespace ecv