-
Notifications
You must be signed in to change notification settings - Fork 0
/
minterm.js
111 lines (92 loc) · 2.74 KB
/
minterm.js
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
/**
* A class to hold information about a minterm when using the Quine-McCluskey Algorithm
*/
module.exports = class Minterm {
/**
* Creates a new minterm object
*/
constructor(values, value) {
this.values = values;
this.value = value;
this.used = false;
this.values.sort(function(number1, number2) {return number1 > number2;});
}
/**
* Returns a String representation of the Minterm
*/
toString() {
let values = this.values.join(", ");
return `m(${values}) = ${this.value}`;
}
/**
* Determines if this Minterm object is equal to another object
*/
equals(minterm) {
if (!(minterm instanceof Minterm)) {
return false;
}
return (
this.value == minterm.value &&
this.values.length == minterm.values.length &&
this.values.every(function(u,i) {return u === minterm.values[i]})
);
}
/**
* Returns the values in this Minterm
*/
getValues() {
return this.values;
}
/**
* Returns the binary value of this Minterm
*/
getValue() {
return this.value;
}
/**
* Returns whether or not this Minterm has been used
*/
isUsed() {
return this.used;
}
/**
* Labels this Minterm as "used"
*/
use() {
this.used = true;
}
/**
* Combines 2 Minterms together if they can be combined
*/
combine(minterm) {
// Check if this value is this same; If so, do nothing
if (this.value == minterm.value) {
return null;
}
// Check if the values are the same; If so, do nothing
if (this.values.length == minterm.values.length &&
this.values.every(function(u,i) {return u === minterm.values[i]})) {
return null;
}
// Keep track of the difference between the minterms
let diff = 0;
let result = "";
// Iterate through the bits in this Minterm's value
for (const i in this.value) {
// Check if the current bit value differs from the minterm's bit value
if (this.value.charAt(i) != minterm.value.charAt(i)) {
diff += 1;
result += "-";
}
// There is no difference
else {
result += this.value.charAt(i);
}
// The difference has exceeded 1
if (diff > 1) {
return null;
}
}
return new Minterm(this.values.concat(minterm.values), result);
}
}