-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.h
284 lines (240 loc) · 6.17 KB
/
node.h
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
#pragma once
#include "gate.h"
#include "help.h"
#include <map>
class node;
class line
{
private:
string name;
uint sub;
bool isConst=false;
public:
node *n;
bool constVal;
line(string name, node *n,uint sub=0) : name(name), n(n), sub(sub) {}
void delayedConstruction(node *n,uint sub=0);
line(string name,bool constVal=0) : name(name), isConst(true), constVal(constVal) {}
bool get();
string getName() { return this->name; }
bool getIsEvaling();
void stru(uint tabNum=0);
};
class node
{
private:
blist getInputPar()
{
blist par;
for(line* i : inputLine)
{
if(i->getIsEvaling()) //检查是否有环形
{
gate* iTri=i->n->g;
if(iTri->getIsTri())
{
tri* t=(tri*)iTri;
par.push_back(t->getStat()[tri::Q]);
}
else
throw string("Illegal Ring Circuit");
}
else
par.push_back(i->get());
}
return par;
}
public:
vector<line*> inputLine; //与g的input个数相等。每个连接需指明插槽位
bool isEvaled=false;
bool isEvaling=false;
blist result;
gate *g;
node(gate* g) : g(g) {}
~node() { delete g; }
void addInputLine(line* l) { inputLine.push_back(l); }
blist eval()
{
isEvaling=true;
if(isEvaled==false)
{
this->result=g->calu(getInputPar());
this->isEvaled=true;
}
isEvaling=false;
return this->result;
}
void stru(uint tabNum=0)
{
cout<<g->getName()<<"-";
for(uint i=0;i<inputLine.size();i++)
{
inputLine[i]->stru(tabNum+1);
if(i!=inputLine.size()-1)
{
cout<<endl;
help::tab(tabNum+1);
}
}
if(tabNum==0)
cout<<endl;
}
};
class nodeManager
{
private:
list<node*> allNode;
list<line*> allLine;
vector<line*> allInput;
vector<line*> allOutput;
vector<node*> allTri;
string recuTriTrue(uint sub = 0)
{
string result;
tri* t=(tri*)(allTri[sub]->g);
t->setQ(0);
if(sub==allTri.size()-1) //产生一个所有触发器值的组合之后再run
result=run(true)+result;
else
result=recuTriTrue(sub+1)+result;
t->setQ(1);
if(sub==allTri.size()-1)
result=run(true)+result;
else
result=recuTriTrue(sub+1)+result;
return result;
}
void resetChunk()
{
for(node* i : allNode)
i->isEvaled=false;
}
void addTri(node* n) { allTri.push_back(n); }
public:
~nodeManager()
{
for(line* i : allLine)
delete i;
for(node* i : allNode)
delete i;
allNode.clear();
allLine.clear();
allInput.clear();
allOutput.clear();
allTri.clear();
}
void addNode(node* n)
{
allNode.push_back(n);
if(n->g->getIsTri())
this->addTri(n);
}
void addLine(line* n) { allLine.push_back(n); }
void addInputLine(line* n) { allInput.push_back(n); }
void addOutputLine(line* n) { allOutput.push_back(n); }
string gateNum()
{
string result;
result+="Gate:"+to_string(allNode.size())+"\n";
map<string,int> num;
for(node* i : allNode)
num[i->g->getName()]+=1;
for(auto i : num)
result+=i.first+":"+to_string(i.second)+"\n";
return result;
}
string trueTable(uint sub = 0, bool staRecu=false)
{
if(staRecu && this->allTri.empty())
return "There is no trigger in this circuit\n";
string result;
auto conti=[&]()
{
if(sub==allInput.size()-1)
{
if(staRecu)
result+=recuTriTrue();
else
result+=run();
}
else
result+=trueTable(sub+1,staRecu);
};
allInput[sub]->constVal=0;
conti();
allInput[sub]->constVal=1;
conti();
return result;
}
string run(bool outputSta=false)
{
string result;
resetChunk();
for(line* i : allInput)
result+="["+i->getName()+"]"+to_string(i->get())+" ";
if(outputSta)
{
result+=" || ";
for(uint i=0;i<allTri.size();i++)
{
tri* t=(tri*)(allTri[i]->g);
result+="["+to_string(i)+"]"+to_string(t->getQ())+" ";
}
}
result+=" -> ";
for(uint i=0;i<allOutput.size();i++)
result+="["+to_string(i)+"]"+to_string(allOutput[i]->get())+" ";
result+="\n";
return result;
}
void resetTri()
{
for(node* i : allTri)
{
tri* t=(tri*)(i->g);
t->setQ(0);
}
}
void stru()
{
for(line* i : allOutput)
i->stru();
}
void middleVar()
{
for(line* i : allLine)
cout<<"["<<i->getName()<<"]"<<i->get()<<"\t";
cout<<endl;
}
map<line*,int> getLineMultiplexing()
{
map<line*,int> result;
for(node* i : allNode)
{
for(line* j : i->inputLine)
result[j]++;
}
return result;
}
map<node*,int> getNodeMultiplexing()
{
map<node*,int> result;
for(line* i : allLine) //allLine记录的是输出
result[i->n]++; //看看它是从哪个元件连出来的,给那个元件加
for(node* i : allNode)
result[i]+=i->inputLine.size(); //inputLine记录的是输入,有几个输入加几
return result;
}
string multiplexing()
{
string strResult;
uint lineNum=0;
for(auto i : this->getLineMultiplexing())
{
strResult+="["+i.first->getName()+"]"+to_string(i.second)+"\t";
lineNum+=i.second;
}
strResult+="\nlineNum:"+to_string(lineNum)+"\n";
return strResult;
}
};