-
Notifications
You must be signed in to change notification settings - Fork 11
/
alphabet.go
60 lines (48 loc) · 1.33 KB
/
alphabet.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
package bits
/**
* Set alphabet of words
*/
import "strings"
//var allowedCharacters = "abcdeghijklmnoprstuvyāīūṁṃŋṇṅñṭḍḷ…'’° -"
var allowedCharacters = "abcdefghijklmnopqrstuvwxyz "
var mapCharToUint = getCharToUintMap(allowedCharacters)
var mapUintToChar = getUintToCharMap(mapCharToUint)
/**
* Write the data for each node, call getDataBits() to calculate how many bits
* for one node.
* 1 bit stores the "final" indicator. The other bits store one of the
* characters of the alphabet.
*/
var dataBits = getDataBits(allowedCharacters)
func SetAllowedCharacters(alphabet string) {
allowedCharacters = alphabet
mapCharToUint = getCharToUintMap(alphabet)
mapUintToChar = getUintToCharMap(mapCharToUint)
dataBits = getDataBits(alphabet)
}
func getCharToUintMap(alphabet string) map[string]uint {
result := map[string]uint{}
var i uint = 0
chars := strings.Split(alphabet, "")
for _, char := range chars {
result[char] = i
i++
}
return result
}
func getUintToCharMap(c2ui map[string]uint) map[uint]string {
result := map[uint]string{}
for k, v := range c2ui {
result[v] = k
}
return result
}
func getDataBits(alphabet string) uint {
numOfChars := len(strings.Split(alphabet, ""))
var i uint = 0
for (1 << i) < numOfChars {
i++
}
// one more bit for the "final" indicator
return (i + 1)
}