-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable.go
95 lines (86 loc) · 1.75 KB
/
table.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
package morse_code
// Table 表示一个摩尔斯密码解密表
type Table map[rune]string
// CanEncode 判断这个字符使用这个摩尔斯密码表是否可以编码
func (x Table) CanEncode(character rune) bool {
_, exists := x[character]
return exists
}
// ToMorseSearchTree 将摩尔斯加密表转为适合解密使用的摩尔斯查询树
func (x Table) ToMorseSearchTree() (*MorseSearchTree, error) {
tree := NewMorseSearchTree()
for character, morseCode := range x {
err := tree.AddMapping(morseCode, character)
if err != nil {
return nil, err
}
}
return tree, nil
}
// DefaultMorseCodeTable 默认的摩尔斯密码表
var DefaultMorseCodeTable Table = map[rune]string{
// 英文字母
'A': ".-",
'B': "-...",
'C': "-.-.",
'D': "-..",
'E': ".",
'F': "..-.",
'G': "--.",
'H': "....",
'I': "..",
'J': ".---",
'K': "-.-",
'L': ".-..",
'M': "--",
'N': "-.",
'O': "---",
'P': ".--.",
'Q': "--.-",
'R': ".-.",
'S': "...",
'T': "-",
'U': "..-",
'V': "...-",
'W': ".--",
'X': "-..-",
'Y': "-.--",
'Z': "--..",
// 数字
'0': "-----",
'1': ".----",
'2': "..---",
'3': "...--",
'4': "....-",
'5': ".....",
'6': "-....",
'7': "--...",
'8': "---..",
'9': "----.",
// 标点符号
'.': ".-.-.-",
',': "--..--",
'?': "..--..",
'\'': ".----.",
'!': "-.-.--",
'/': "-..-.",
'(': "-.--.",
')': "-.--.-",
'&': ".-...",
':': "---...",
';': "-.-.-.",
'=': "-...-",
'+': ".-.-.",
'-': "-....-",
'_': "..--.-",
'"': ".-..-.",
'$': "...-..-",
'@': ".--.-.",
'¿': "..-.-",
'¡': "--...-",
}
// DefaultMorseSearchTree 默认的摩尔斯密码表对应的搜索树
var DefaultMorseSearchTree *MorseSearchTree
func init() {
DefaultMorseSearchTree, _ = DefaultMorseCodeTable.ToMorseSearchTree()
}