generated from mpppk/cli-template
/
iroha.go
210 lines (187 loc) · 5.54 KB
/
iroha.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package lib
import (
"context"
"fmt"
"math/bits"
"github.com/mpppk/iroha/storage"
"github.com/mpppk/iroha/ktkn"
"golang.org/x/sync/errgroup"
)
type Iroha struct {
katakanaBitsMap ktkn.KatakanaBitsMap
katakana *ktkn.Katakana
log *Log
depths *DepthOptions
storage storage.Storage
}
type DepthOptions struct {
MaxLog int
MinParallel int
MaxParallel int
MaxStorage int
}
func NewIroha(words []string, storage storage.Storage, options *DepthOptions) *Iroha {
km, _ := ktkn.NewKatakanaBitsMap()
return &Iroha{
katakanaBitsMap: km,
katakana: ktkn.NewKatakana(words),
depths: options,
storage: storage,
}
}
func (i *Iroha) PrintWordCountMap() {
i.katakana.PrintWordCountMap()
}
func (i *Iroha) PrintWordByKatakanaMap() {
i.katakana.PrintWordByKatakanaMap()
}
func (i *Iroha) Search() (rowIndicesList [][]int, err error) {
katakanaBitsAndWordsList := i.katakana.ListSortedKatakanaBitsAndWords()
i.log = NewLog(katakanaBitsAndWordsList, i.depths.MaxLog, i.depths.MinParallel)
wordsList, _, err := i.searchByBits([]int{}, katakanaBitsAndWordsList, ktkn.WordBits(0))
if err != nil {
return nil, err
}
for _, words := range wordsList {
var rowIndices []int
for _, word := range words {
rowIndices = append(rowIndices, int(word.Id))
}
rowIndicesList = append(rowIndicesList, rowIndices)
}
return
}
func (i *Iroha) f(word *ktkn.Word, usedIndices []int, katakanaBitsAndWords []*ktkn.KatakanaBitsAndWords, remainKatakanaBits ktkn.WordBits) ([][]*ktkn.Word, bool, error) {
var results [][]*ktkn.Word
if remainKatakanaBits.HasDuplicatedKatakana(word.Bits) {
return nil, false, nil
}
newRemainKatakanaBits := remainKatakanaBits.Merge(word.Bits)
newIrohaWordIdLists, ok, err := i.searchByBits(usedIndices, katakanaBitsAndWords[1:], newRemainKatakanaBits)
if err != nil {
return nil, false, err
}
if ok {
for _, newIrohaWordList := range newIrohaWordIdLists {
newIrohaWordList = append(newIrohaWordList, word)
results = append(results, newIrohaWordList)
}
}
return results, true, nil
}
func (i *Iroha) gf(word *ktkn.Word, usedIndices []int, katakanaBitsAndWords []*ktkn.KatakanaBitsAndWords, remainKatakanaBits ktkn.WordBits, wordListChan chan<- []*ktkn.Word) error {
// FIXME: check cache
wordLists, ok, err := i.f(word, usedIndices, katakanaBitsAndWords, remainKatakanaBits)
if err != nil {
return err
}
if ok {
for _, wordList := range wordLists {
wordListChan <- wordList
}
}
return nil
}
func (i *Iroha) searchByBits(usedIndices []int, katakanaBitsAndWords []*ktkn.KatakanaBitsAndWords, remainKatakanaBits ktkn.WordBits) ([][]*ktkn.Word, bool, error) {
ctx := context.Background()
remainKatakanaNum := bits.OnesCount64(uint64(remainKatakanaBits))
if remainKatakanaNum == int(ktkn.KatakanaLen) {
return [][]*ktkn.Word{{}}, true, nil
}
if len(katakanaBitsAndWords) == 0 {
return nil, false, nil
}
katakanaAndWordBits := katakanaBitsAndWords[0]
if len(katakanaAndWordBits.Words) == 0 {
return nil, false, nil
}
depth := int(ktkn.KatakanaLen) - len(katakanaBitsAndWords) - 1
if depth <= i.depths.MaxStorage {
if results, ok, err := i.storage.Get(ctx, usedIndices); err != nil {
return nil, false, err
} else if ok {
i.log.PrintProgressLog(depth, "cache used")
return results, true, nil
}
}
if depth == i.depths.MaxStorage {
if err := i.storage.Start(ctx, usedIndices); err != nil {
return nil, false, err
}
}
var irohaWordLists [][]*ktkn.Word
goroutineMode := depth >= i.depths.MinParallel && depth <= i.depths.MaxParallel
if goroutineMode {
eg := errgroup.Group{}
wordListChan := make(chan []*ktkn.Word, 1000000)
for index, word := range katakanaAndWordBits.Words {
gWord := word
newIndices := generateNewUsedIndices(usedIndices, index)
eg.Go(func() error {
if err := i.gf(gWord, newIndices, katakanaBitsAndWords, remainKatakanaBits, wordListChan); err != nil {
return err
}
return nil
})
}
errChan := make(chan error)
go func() {
if err := eg.Wait(); err != nil {
errChan <- err
return
}
close(wordListChan)
}()
L:
for {
select {
case wordList, ok := <-wordListChan:
if !ok {
break L
}
irohaWordLists = append(irohaWordLists, wordList)
case err, ok := <-errChan:
if !ok {
return nil, false, fmt.Errorf("unexpected error channel closing")
}
return nil, false, err
}
}
} else {
for index, word := range katakanaAndWordBits.Words {
newIndices := generateNewUsedIndices(usedIndices, index)
wordList, ok, err := i.f(word, newIndices, katakanaBitsAndWords, remainKatakanaBits)
if err != nil {
return nil, false, err
}
if ok {
irohaWordLists = append(irohaWordLists, wordList...)
}
}
}
// どれも入れない場合
if remainKatakanaBits.Has(katakanaAndWordBits.KatakanaBits) {
otherIrohaWordBitsLists, ok, err := i.searchByBits(append(usedIndices, -1), katakanaBitsAndWords[1:], remainKatakanaBits)
if err != nil {
return nil, false, err
}
if ok {
irohaWordLists = append(irohaWordLists, otherIrohaWordBitsLists...)
}
}
msg := ""
if depth <= i.depths.MaxStorage {
if err := i.storage.Set(ctx, usedIndices, irohaWordLists); err != nil {
return nil, false, err
}
msg += "cache saved"
}
i.log.PrintProgressLog(depth, msg)
return irohaWordLists, len(irohaWordLists) > 0, nil
}
func generateNewUsedIndices(usedIndices []int, newIndex int) []int {
newIndices := make([]int, len(usedIndices), len(usedIndices)+1)
copy(newIndices, usedIndices)
newIndices = append(newIndices, newIndex)
return newIndices
}