fix argument order and offset
[kdevelop:kdevelop-pg-qt.git] / mkunionstruct.cpp
1 /// WARNING! This file is a technical experiment!!
2 #include <iostream>
3 #include <vector>
4 #include <deque>
5 #include <list>
6 #include <algorithm>
7 #include <map>
8 #include <stdint.h>
9 #include <cmath>
10 #include <QtCore/qglobal.h>
11 using namespace std;
12
13 struct state
14 {
15     uint64_t confl[64];
16     uchar pos[64];
17     uchar num, size;
18     state()
19     {
20         num = 0;
21         size = 0;
22         for(int i = 0; i != 64; ++i)
23             confl[i] = 0;
24     }
25     state(const state& o)
26     {
27         for(int i = 0; i != 64; ++i)
28         {
29             pos[i] = o.pos[i];
30             confl[i] = o.confl[i];
31         }
32         num = o.num;
33         size = o.size;
34     }
35 };
36
37 int main()
38 {
39     vector<uint64_t> orig, in;
40     orig.push_back(0x3);
41     orig.push_back(0x1);
42     orig.push_back(0x2);
43     orig.push_back(0xc);
44     orig.push_back(0x4);
45     orig.push_back(0x8);
46     /*orig.push_back(0xf);
47     orig.push_back(0xf0);
48     orig.push_back(0x3);
49     orig.push_back(0xbb);
50     orig.push_back(0x44);
51     orig.push_back(0xf00);
52     orig.push_back(0x0f0);
53     orig.push_back(0xbb0);
54     orig.push_back(0x7777);
55     orig.push_back(0xf0f0);
56     orig.push_back(0xf0000);*/
57     vector<uchar> rules[64];
58     vector<uchar> map[64] = {};
59     for(int i = 0; i != orig.size(); ++i)
60     {
61         /*for(int j = 0; j != in.size(); ++j)
62         {
63             if((in[j] & orig[i]) == orig[i])
64             {
65                 map[j].push_back(i);
66                 goto fail;
67             }
68         }*/
69         cout << hex << orig[i] << endl;
70         map[in.size()].push_back(i);
71         in.push_back(orig[i]);
72         for(int j = 0; j != 64; ++j)
73             if(in.back() & (1 << j))
74                 rules[j].push_back(in.size()-1);
75         //fail:;
76     }
77     cout << dec;
78     {
79         bool printOr = false;
80         for(int i = 0; i != 64; ++i)
81         {
82             if(rules[i].size() != 0)
83             {
84                 if(printOr)
85                     cout << " |";
86                 else
87                     cout << "  ";
88                 printOr = true;
89                 for(int j = 0; j != rules[i].size(); ++j)
90                 {
91                     for(int k = 0; k != map[rules[i][j]].size(); ++k)
92                         cout << " f" << (int)map[rules[i][j]][k];
93                 }
94                 cout << endl;
95             }
96         }
97     }
98     uchar num = in.size();
99     deque<state> s;
100     s.push_back(state());
101     while(true)
102     {
103         state curr = s.front();
104         s.pop_front();
105         if(curr.num == num)
106         {
107             vector<uchar> rpos[64] = {};
108             for(int i = 0; i != curr.num; ++i)
109                 rpos[curr.pos[i]].push_back(i);
110             for(int i = 0; i != 64 && rpos[i].size() != 0; ++i)
111             {
112                 cout << "union { ";
113                 for(int j = 0; j != rpos[i].size(); ++j)
114                 {
115                     for(int k = 0; k != map[rpos[i][j]].size(); ++k)
116                         cout << "f" << (int)map[rpos[i][j]][k] << "; ";
117                 }
118                 cout << "};" << endl;
119             }
120             break;
121         }
122         uint64_t next = in[curr.num];
123         ++curr.num;
124         for(int i = 0; i != curr.size; ++i)
125         {
126             if((curr.confl[i] & next) == 0)
127             {
128                 state ncurr(curr);
129                 ncurr.confl[i] |= next;
130                 ncurr.pos[curr.num-1] = i;
131                 s.push_front(ncurr);
132             }
133         }
134         curr.confl[curr.size] = next;
135         curr.pos[curr.num-1] = curr.size;
136         ++curr.size;
137         s.push_back(curr);
138     }
139     
140 }