-
Notifications
You must be signed in to change notification settings - Fork 1
/
algo.go
64 lines (53 loc) · 1.19 KB
/
algo.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
package algoexplore
import (
"encoding/json"
"fmt"
"io"
"sort"
"sync"
)
var (
algos = map[string]AlgoFactory{}
algosMutex sync.RWMutex
)
type AlgoPlugin interface {
Name() string
Init(inputLen int)
Step(d byte)
SerializeState() string
DeserializeState(state string) error
}
type AlgoFactory func() AlgoPlugin
func Register(algoFactory AlgoFactory) {
algosMutex.Lock()
defer algosMutex.Unlock()
if _, ok := algos[algoFactory().Name()]; ok {
panic(fmt.Sprintf("algo already registered: %s", algoFactory().Name()))
}
algos[algoFactory().Name()] = algoFactory
}
func GetAlgo(name string) (AlgoPlugin, error) {
algosMutex.RLock()
defer algosMutex.RUnlock()
m, ok := algos[name]
if !ok {
return nil, fmt.Errorf("algo not registered: %s", name)
}
return m(), nil
}
// Algos returns the names of all registered algorithms
func Algos() []string {
algosMutex.RLock()
defer algosMutex.RUnlock()
names := make([]string, 0, len(algos))
for name := range algos {
names = append(names, name)
}
sort.Strings(names)
return names
}
func StrictUnmarshalJSON(data *io.Reader, v interface{}) error {
dec := json.NewDecoder(*data)
dec.DisallowUnknownFields()
return dec.Decode(v)
}