1 /******************************************
2 Copyright (c) 2016, Mate Soos
3 
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 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
12 all copies or substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21 ***********************************************/
22 
23 #ifndef MATRIXFINDER_H
24 #define MATRIXFINDER_H
25 
26 #include <vector>
27 #include <map>
28 #include <set>
29 #include "xor.h"
30 #include "constants.h"
31 
32 namespace CMSat {
33 
34 class Solver;
35 
36 using std::map;
37 using std::vector;
38 using std::pair;
39 using std::set;
40 
41 class MatrixFinder {
42 
43     public:
44         MatrixFinder(Solver* solver);
45 
46         //NOTE "simplify_xors" should always be true except during testing
47         bool findMatrixes(bool& can_detach, bool simplify_xors = true);
48 
49         vector<Xor> unused_xors;
50         set<uint32_t> clash_vars_unused;
51         vector<Xor> xors;
52 
53     private:
54         uint32_t setMatrixes();
55         struct MatrixShape
56         {
MatrixShapeMatrixShape57             MatrixShape(uint32_t matrix_num) :
58                 num(matrix_num)
59             {}
60 
MatrixShapeMatrixShape61             MatrixShape()
62             {}
63 
64             uint32_t num;
65             uint32_t rows = 0;
66             uint32_t cols = 0;
67             uint32_t sum_xor_sizes = 0;
68             double density = 0;
69 
tot_sizeMatrixShape70             uint64_t tot_size() const
71             {
72                 return rows*cols;
73             }
74         };
75 
76         struct mysorter
77         {
operatormysorter78             bool operator () (const MatrixShape& left, const MatrixShape& right)
79             {
80                 return left.sum_xor_sizes < right.sum_xor_sizes;
81             }
82         };
83 
84         inline uint32_t fingerprint(const Xor& c) const;
85         inline bool firstPartOfSecond(const Xor& c1, const Xor& c2) const;
86         inline bool belong_same_matrix(const Xor& x);
87 
88         map<uint32_t, vector<uint32_t> > reverseTable; //matrix -> vars
89         vector<uint32_t> table; //var -> matrix
90         uint32_t matrix_no;
91 
92         Solver* solver;
93         vector<uint16_t>& seen;
94 };
95 
96 }
97 
98 #endif //MATRIXFINDER_H
99