forked from etnz/boolgebra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
154 lines (134 loc) · 3.47 KB
/
types.go
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
package boolgebra
import (
"sort"
"strconv"
"strings"
)
type (
// expression is boolean algebra expression as a sum of prod of minterms.
// As such it is a slice of minterms. It must be considered as a set
//
// an empty expression is always false ( and this is the definition of false
//
expression []minterm
//minterm is a product of identifier or their negation. For instance
// mintern "AB'D" <=> "A or Not(B) or D" is coded as minterm{ "A":true, "B":false, "D":true}
//
// it is conventional that https://en.wikipedia.org/wiki/Empty_product the empty minterm is 1 the neutral for prod ( and for and too)
//
minterm map[string]bool
// Expr is the interface that all elements of a boolean algebra share
Expr interface {
String() string
// return the negation of receiver
Not() Expr
// Is return true if the Expr is literally equals to value
Is(val bool) bool
Terms() int
Term(i int) Expr
IDs() (ids map[string]struct{})
}
)
// String return the literal representation (using primary functions) of the current expression.
func (x expression) String() string {
if len(x) == 0 {
return "Lit(false)"
}
if len(x) == 1 {
return x[0].String()
}
var terms []string
for _, k := range x {
terms = append(terms, k.String())
}
if len(terms) > 3 {
return "Or(\n " + strings.Join(terms, ",\n ") + "\n)"
}
return "Or(" + strings.Join(terms, ", ") + ")"
}
//String return the literal representation (using primary functions) of the current minterm
func (m minterm) String() string {
if len(m) == 0 {
return "Lit(true)"
}
var terms []string
for k := range m {
terms = append(terms, k)
}
sort.Strings(terms)
for i, t := range terms {
if !m[t] {
terms[i] = "Not(" + strconv.Quote(t) + ")"
} else {
terms[i] = strconv.Quote(t)
}
}
if len(terms) == 1 {
return terms[0]
} else {
return "And(" + strings.Join(terms, ", ") + ")"
}
}
// NOT
func (x expression) Not() Expr {
factors := make([]Expr, 0, len(x))
for _, e := range x {
factors = append(factors, e.Not())
}
return And(factors...)
}
func (m minterm) Not() Expr {
res := make(expression, 0, len(m))
for k, v := range m {
res = append(res, minterm{string(k): !v})
}
return res
}
//Is return true if this expression is equals to val
func (x expression) Is(val bool) bool {
if val {
return len(x) == 1 && len(x[0]) == 0
} else {
return len(x) == 0
}
}
//Is return true if this expression is equals to val
func (m minterm) Is(val bool) bool {
return val && len(m) == 0
}
//Terms retuns the number of terms in this expression
func (x expression) Terms() int { return len(x) }
//Terms retuns the number of terms in this expression
func (m minterm) Terms() int { return 1 }
//Term retuns the ith terms. Panic if out of bounds ( negative, or >= Terms())
func (x expression) Term(i int) Expr {
if i < 0 || i >= x.Terms() {
panic("Term is not defined for this index value")
}
return x[i]
}
//Term retuns the ith terms. Panic if out of bounds ( negative, or >= Terms())
func (m minterm) Term(i int) Expr {
if i != 0 {
panic("Term is not defined for this index value")
}
return m
}
// IDs return the set of ID in this expression
func (x expression) IDs() (ids map[string]struct{}) {
ids = make(map[string]struct{})
for _, m := range x {
for k := range m {
ids[k] = struct{}{}
}
}
return
}
// IDs return the set of ID in this expression
func (m minterm) IDs() (ids map[string]struct{}) {
ids = make(map[string]struct{})
for k := range m {
ids[k] = struct{}{}
}
return
}