-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
143 lines (125 loc) · 3.4 KB
/
main.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
package main
import (
"fmt"
"math/big"
"os"
"sync"
)
// ********************** cmdline UI functions ********************* //
func convert_next_value(current string) (string, string, bool, bool) {
var i int
for i=0; len(current) > i && current[i] >= '0' && current[i] <= '9'; i++ {}
nextval := current[0:i]
var newcurr string
var do_add bool
if i == len(current) {
newcurr = ""
do_add = true
} else {
if (current[i] != '+') && (current[i] != '-') {
return "", "", true, true
} else {
newcurr = current[i+1:]
do_add = true
if current[i] == '-' {
do_add = false
}
}
}
return nextval, newcurr, do_add, false
}
func convert_formula(formula string) (*big.Int) {
var current, next string = formula, ""
var do_add, do_add_next, err bool = true, true, false
var two, tmp, q = big.NewInt(2), big.NewInt(0), big.NewInt(0)
for ; len(current) > 0; {
tmp.SetUint64(0)
if (len(current) > 1) && (current[0:2] == "2^") {
current = current[2:]
if len(current) == 0 {
return nil
}
if current, next, do_add_next, err = convert_next_value(current); err {
return nil
}
tmp.SetString(current, 10)
tmp.Exp(two, tmp, nil)
} else {
if current, next, do_add_next, err = convert_next_value(current); err {
return nil
}
tmp.SetString(current, 10)
}
if do_add {
q.Add(q, tmp)
} else {
q.Sub(q, tmp)
}
current = next
do_add = do_add_next
}
return q
}
func usage() {
fmt.Printf("Usage: %s <formula>\n\n<formula> can be a decimal number or a formula like 2^255-19\n", os.Args[0])
}
type runT struct {
size int
seq []seqT
}
func main() {
// read in arguments
if len(os.Args) < 2 {
usage()
fmt.Println("\nYou must specify q > 4.")
os.Exit(-1)
}
var q = convert_formula(os.Args[1])
if q == nil {
usage()
fmt.Printf("\ncannot convert formula '%s'\n", os.Args[1])
os.Exit(-1)
}
// search in parallel
const swin = 2
const ewin = 11
var ch = make(chan runT, ewin - swin + 2)
var wg = sync.WaitGroup{}
// Yacobi LZ method
wg.Add(1)
go func (wg *sync.WaitGroup) {
ch <- runT{0, make_sequence(yacobi_lz(q))}
wg.Done()
}(&wg)
// Bergeron-Berstel-Brlek-Duboc
wg.Add(1)
go func (wg *sync.WaitGroup) {
ch <- runT{1, make_sequence(minchain(q))}
wg.Done()
}(&wg)
// Bos-Coster for various window sizes
for i := swin; i < ewin; i++ {
wg.Add(1)
go func (wg *sync.WaitGroup, i int) {
ch <- runT{i, make_sequence(bos_coster(q, i))}
wg.Done()
}(&wg, i)
}
// wait for all to be done
wg.Wait()
close(ch)
// find the best result
var win runT
var wlen, wsto = (1 << 30), (1 << 30)
for wx := range ch {
var xlen, xsto = seq_len(wx.seq), seq_storage(wx.seq)
show_run(&wx, xlen, xsto)
if xlen < wlen || (xlen == wlen && xsto < wsto) {
wlen, wsto = xlen, xsto
win = wx
}
}
// show the winner
print_sequence(win.seq)
show_run(&win, wlen, wsto)
}