Debian Codegraph @

msp/formatted.go

raw symbols SCIP
1 package msp
2
3 import (
4 "errors"
5 "fmt"
6 "strconv"
7 "strings"
8 )
9
10 type Formatted struct { // Represents threshold gate (also type of condition)
11 Min int
12 Conds []Condition
13 }
14
15 func StringToFormatted(f string) (out Formatted, err error) {
16 // Automaton. Modification of Dijkstra's Two-Stack Algorithm for parsing
17 // infix notation. Running time linear in the size of the predicate?
18 //
19 // Steps either to the next comma or the next unparenthesis.
20 // ( -> Push new queue onto staging stack
21 // value -> Push onto back of queue at top of staging stack.
22 // ) -> Pop queue off top of staging stack, build threshold gate,
23 // and push gate onto the back of the top queue.
24 //
25 // Staging stack is empty on initialization and should have exactly 1 built
26 // threshold gate at the end of the string.
27 if len(f) == 0 || f[0] != '(' || f[len(f)-1] != ')' {
28 return out, errors.New("Invalid string: Needs to begin and end with parentheses.")
29 }
30
31 getNext := func(f string) (string, string) { // f -> (next, rest)
32 f = strings.TrimSpace(f)
33
34 if f[0] == '(' {
35 return f[0:1], f[1:]
36 }
37
38 nextComma := strings.Index(f, ",")
39 if f[0] == ')' {
40 if nextComma == -1 {
41 return f[0:1], ""
42 }
43 return f[0:1], f[nextComma+1:]
44 } else if nextComma == -1 {
45 return f[0 : len(f)-1], f[len(f)-1:]
46 }
47
48 nextUnParen := strings.Index(f, ")")
49 if nextComma < nextUnParen {
50 return strings.TrimSpace(f[0:nextComma]), f[nextComma+1:]
51 }
52
53 return strings.TrimSpace(f[0:nextUnParen]), f[nextUnParen:]
54 }
55
56 staging := [][]Condition{}
57 indices := make(map[string]int, 0)
58
59 var nxt string
60 for len(f) > 0 {
61 nxt, f = getNext(f)
62
63 switch nxt {
64 case "(":
65 staging = append([][]Condition{[]Condition{}}, staging...)
66 case ")":
67 if len(staging) < 1 || len(staging[0]) < 1 { // Check 1
68 return out, errors.New("Invalid string: Illegal close parenthesis.")
69 }
70
71 top := staging[0] // Legal because of check 1.
72 staging = staging[1:]
73
74 var min int
75 minStr, ok := top[0].(Name) // Legal because of check 1.
76 if !ok {
77 return out, errors.New("Invalid string: First argument wasn't a threshold!")
78 }
79
80 min, err = strconv.Atoi(minStr.string)
81 if err != nil {
82 return
83 }
84
85 built := Formatted{
86 Min: min,
87 Conds: []Condition{},
88 }
89
90 for _, cond := range top[1:] {
91 built.Conds = append(built.Conds, cond)
92 }
93
94 if len(staging) == 0 { // Check 2
95 if len(f) == 0 {
96 return built, nil
97 }
98 return out, errors.New("Invalid string: Can't parse anymore, but there's still data. Too many closing parentheses or too few opening parentheses?")
99 }
100
101 staging[0] = append(staging[0], built) // Legal because of check 2.
102
103 default:
104 if len(staging) < 1 {
105 return out, errors.New("Invalid string: Name is not encapsulated!")
106 }
107
108 if _, there := indices[nxt]; !there {
109 indices[nxt] = 0
110 }
111
112 staging[0] = append(staging[0], Name{nxt, indices[nxt]}) // Legal because of check above.
113 indices[nxt]++
114 }
115 }
116
117 return out, errors.New("Invalid string: Not finished parsing, but out of data. Too many opening parentheses or too few closing parentheses?")
118 }
119
120 func (f Formatted) String() string {
121 out := fmt.Sprintf("(%v", f.Min)
122
123 for _, cond := range f.Conds {
124 switch cond := cond.(type) {
125 case Name:
126 out += fmt.Sprintf(", %v", cond.string)
127 case Formatted:
128 out += fmt.Sprintf(", %v", cond.String())
129 }
130 }
131
132 return out + ")"
133 }
134
135 func (f Formatted) Ok(db UserDatabase) bool {
136 // Goes through the smallest number of conditions possible to check if the
137 // threshold gate returns true. Sometimes requires recursing down to check
138 // nested threshold gates.
139 rest := f.Min
140
141 for _, cond := range f.Conds {
142 if cond.Ok(db) {
143 rest--
144 }
145
146 if rest == 0 {
147 return true
148 }
149 }
150
151 return false
152 }
153
154 func (f *Formatted) Compress() {
155 if f.Min == len(f.Conds) {
156 // AND Compression: (n, ..., (m, ...), ...) = (n + m, ...)
157 skip := 0
158 for i, cond := range f.Conds {
159 if skip > 0 {
160 skip--
161 continue
162 }
163
164 switch cond := cond.(type) {
165 case Formatted:
166 cond.Compress()
167 f.Conds[i] = cond
168
169 if cond.Min == len(cond.Conds) {
170 f.Min += cond.Min - 1
171 f.Conds = append(f.Conds[0:i],
172 append(cond.Conds, f.Conds[i+1:]...)...)
173 skip = len(cond.Conds) - 1
174 }
175 }
176 }
177 } else if f.Min == 1 {
178 // OR Compression: (1, ..., (1, ...), ...) = (1, ...)
179 skip := 0
180 for i, cond := range f.Conds {
181 if skip > 0 {
182 skip--
183 continue
184 }
185
186 switch cond := cond.(type) {
187 case Formatted:
188 cond.Compress()
189 f.Conds[i] = cond
190
191 if cond.Min == 1 {
192 f.Conds = append(f.Conds[0:i],
193 append(cond.Conds, f.Conds[i+1:]...)...)
194 skip = len(cond.Conds) - 1
195 }
196 }
197 }
198 }
199 }