-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
89 lines (78 loc) · 1.75 KB
/
util.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
package disttopk
import (
"bytes"
"encoding/binary"
"encoding/gob"
"io"
"math"
)
func GetRoundedBits(m_est int) int {
m_log := math.Log2(float64(m_est))
m_log_rounded, frac := math.Modf(m_log)
if frac >= 0.5 {
m_log_rounded++
}
return int(m_log_rounded)
}
func GetValueFromBits(bits int) int {
m := (1 << (uint(bits)))
return m
}
func MakePowerOf2(m_est int) int {
m_log_rounded := GetRoundedBits(m_est)
return GetValueFromBits(m_log_rounded)
}
func IntKeyToByteKey(key int) []byte {
tmp := make([]byte, 16)
binary.PutUvarint(tmp, uint64(key))
return tmp
}
type Serializer interface {
Serialize(w io.Writer) error
Deserialize(r io.Reader) error
}
func SerializeObject(obj Serializer) ([]byte, error) {
buf := new(bytes.Buffer)
if err := obj.Serialize(buf); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func DeserializeObject(into Serializer, b []byte) error {
buf := bytes.NewReader(b)
return into.Deserialize(buf)
}
func GobBytesEncode(obj interface{}) ([]byte, error) {
buf := new(bytes.Buffer)
e := gob.NewEncoder(buf)
if err := e.Encode(obj); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func GobBytesDecode(into interface{}, b []byte) error {
buf := bytes.NewReader(b)
e := gob.NewDecoder(buf)
err := e.Decode(into)
return err
}
func SerializeIntAsU32(w io.Writer, v *int) error {
writev := uint32(*v)
return binary.Write(w, binary.BigEndian, &writev)
}
func DeserializeIntAsU32(r io.Reader, v *int) error {
readv := uint32(0)
err := binary.Read(r, binary.BigEndian, &readv)
if err != nil {
return err
}
*v = int(readv)
return nil
}
func MakeHashTables(ils []ItemList) []*HashTable {
hts := make([]*HashTable, 0, len(ils))
for _, v := range ils {
hts = append(hts, v.MakeHashTable())
}
return hts
}