-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.h
More file actions
151 lines (127 loc) · 3.26 KB
/
node.h
File metadata and controls
151 lines (127 loc) · 3.26 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
#ifndef __NODE_H__
#define __NODE_H__
#include <string>
#include <iostream>
#include <vector>
using namespace std;
#include "truthTable.h"
// define node type
enum nodeType {PRIMARY_INPUT, PRIMARY_OUTPUT, INTERNAL, ZERO_NODE, ONE_NODE};
// define value type
class Circuit;
class Node
{
friend class Circuit;
private:
string name;
nodeType type;
vector<Node*> fanin;
TruthTable tt;
int val;
public:
// constructors
Node():type(INTERNAL) {}
Node(const string &nodeName):name(nodeName), type(INTERNAL) {}
// destructor
~Node() {};
// returns and sets the name of the node
string getName() { return name; }
int setName(const string &n) { name = n; return 0; }
// returns and sets the type of the node
nodeType getType() { return type; }
int setType(nodeType t) { type = t; return 0; }
// returns and sets value of the node
int getVal() { return val; }
int setVal(int in) { val = in; return 0; }
// returns the vector of fanin nodes
vector<Node*> getFanin() { return fanin; }
// adds a fanin node
int addFanin(Node* &inNode) { fanin.push_back(inNode); return 0; }
// returns the number of fanin nodes (variables)
unsigned getNumFanin() { return fanin.size(); }
// clear functions
int clearName() { name = ""; return 0; }
int clearFanin() { fanin.clear(); return 0; }
int clearTT() { tt.clear(); return 0; }
int clear() { name = ""; clearFanin(); clearTT(); return 0; }
// prints node information
int print()
{
cout << "Name: " << name << " [TYPE = ";
switch(type)
{
case PRIMARY_INPUT : cout << "PRIMARY_INPUT"; break;
case PRIMARY_OUTPUT: cout << "PRIMARY_OUTPUT"; break;
case INTERNAL : cout << "INTERNAL"; break;
case ZERO_NODE : cout << "ZERO_NODE"; break;
case ONE_NODE : cout << "ONE_NODE"; break;
}
cout << "]" << endl;
if (type == PRIMARY_OUTPUT || type == INTERNAL)
{
cout << "Fanin nodes: ";
for (unsigned i = 0; i < fanin.size(); ++i)
cout << fanin[i]->name << " ";
cout << endl;
tt.print();
}
return 0;
}
int getSitch() //0 = NOT, 1 = NOTNOT, 2 = AND, 3 = OR
{
int sitch = 0;
if (tt.getNumVars() > 1) sitch += 2;
if (sitch == 2) //either AND or OR
{
if (tt.getNumEntries() > 1) sitch++; //OR
}
else //either NOT or NOTNOT
{
if (tt.NOTNOT()) sitch++;
}
return sitch;
}
void cascade()
{
int sitch; //variable to be used for switch
vector<Node*> Fan = getFanin();
sitch = getSitch();
switch (sitch)
{
case 0: //NOT
if (Fan[0]->getVal() == 0)
setVal(1);
else
setVal(0);
break;
case 1: //NOTNOT
setVal(Fan[0]->getVal());
break;
case 2: //AND
setVal(1);
for (int i = 0; i < Fan.size(); i++)
{
if (Fan[i]->getVal() == 0) //if a 0 is found it evaluates to 0
{
setVal(0);
break;
}
}
break;
case 3: //OR
setVal(0);
for (int i = 0; i < Fan.size(); i++)
{
if (Fan[i]->getVal() == 1) //if a 1 is found, it evaluates to 1
{
setVal(1);
break;
}
}
break;
default: //BROKEN
break;
}
}
};
#endif