-
Notifications
You must be signed in to change notification settings - Fork 1
/
common_words.go
90 lines (82 loc) · 2.34 KB
/
common_words.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
package multiexp
// gcw inputs two positive integer a and b, calculates the most common words
// i.e. a = 11011111, b = 11100000, most common word(s) = 11000000
func gcw(a, b nat) (nat, nat, nat) {
aExtra := nat(nil).make(len(a))
bExtra := nat(nil).make(len(b))
var minWordLen int
if len(a) > len(b) {
minWordLen = len(b)
for i := minWordLen; i < len(a); i++ {
aExtra[i] = a[i]
}
} else {
minWordLen = len(a)
for i := minWordLen; i < len(b); i++ {
bExtra[i] = b[i]
}
}
commonWords := nat(nil).make(minWordLen)
for i := 0; i < minWordLen; i++ {
commonWords[i] = a[i] & b[i]
aExtra[i] = a[i] - commonWords[i]
bExtra[i] = b[i] - commonWords[i]
}
return aExtra, bExtra, commonWords
}
// fourfoldGCW inputs four positive integer a, b, c, d and calculates the greatest common words
// the last element in output is the common word slice
func fourfoldGCW(input [4]nat) [5]nat {
maxWordLen := 0
minWordLen := len(input[0])
for i := 0; i < 4; i++ {
if maxWordLen < len(input[i]) {
maxWordLen = len(input[i])
}
if minWordLen > len(input[i]) {
minWordLen = len(input[i])
}
}
var outputs [5]nat
for i := 0; i < 4; i++ {
outputs[i] = outputs[i].make(len(input[i]))
}
outputs[4] = outputs[4].make(minWordLen)
for i := 0; i < minWordLen; i++ {
outputs[4][i] = input[0][i] & input[1][i] & input[2][i] & input[3][i]
outputs[0][i] = input[0][i] - outputs[4][i]
outputs[1][i] = input[1][i] - outputs[4][i]
outputs[2][i] = input[2][i] - outputs[4][i]
outputs[3][i] = input[3][i] - outputs[4][i]
}
for i := 0; i < 4; i++ {
if len(outputs[i]) > minWordLen {
for j := minWordLen; j < len(outputs[i]); j++ {
outputs[i][j] = input[i][j]
}
}
}
return outputs
}
// threefoldGcb inputs three positive integer a, b, c and calculates the greatest common words
// the last element in output is the common word slice
func threefoldGCW(input [3]nat) nat {
maxWordLen := 0
minWordLen := len(input[0])
for i := 0; i < 3; i++ {
if maxWordLen < len(input[i]) {
maxWordLen = len(input[i])
}
if minWordLen > len(input[i]) {
minWordLen = len(input[i])
}
}
output := nat(nil).make(minWordLen)
for i := 0; i < minWordLen; i++ {
output[i] = input[0][i] & input[1][i] & input[2][i]
input[0][i] = input[0][i] - output[i]
input[1][i] = input[1][i] - output[i]
input[2][i] = input[2][i] - output[i]
}
return output
}