-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcuckoo.go
executable file
·100 lines (86 loc) · 1.92 KB
/
cuckoo.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
package btcec
import (
"encoding/gob"
"fmt"
"os"
)
const (
EMPTY uint8 = 0
MAXITER int = 500
)
type Key uint32
type Value uint32
type Op_Cuckoo struct {
Table_v [cuckoolen]Value
Table_k [cuckoolen]Key
// Hash_index []uint8
nentries int
maxsize Hash
}
func Op_NewCuckoo() *Op_Cuckoo {
c := &Op_Cuckoo{
nentries: Jmax,
maxsize: Hash(cuckoolen),
}
return c
}
func (c *Op_Cuckoo) Op_search(X []byte) (v Value, ok bool) {
for i := 0; i < 3; i++ {
start := i * 8
end := start + 4
x := BytesToUint32(X[end : end+4])
x_key := Key(x)
h := BytesToUint32(X[start:end]) % c.maxsize
if c.Table_k[h] == x_key {
return c.Table_v[h], true
}
}
return 0, false
}
func (c *Op_Cuckoo) Op_insert(data [Jmax][]byte) {
Hash_index := make([]uint8, c.maxsize)
for i := 0; i < c.nentries; i++ {
v := Value(i + 1)
old_hash_id := uint8(1)
j := 0
for ; j < MAXITER; j++ {
X := data[v-1]
start := (old_hash_id - 1) * 8
end := start + 4
x := BytesToUint32(X[end : end+4])
x_key := Key(x)
h := BytesToUint32(X[start:end]) % c.maxsize
hash_id_address := &Hash_index[h]
key_index_address := &c.Table_v[h]
key_address := &c.Table_k[h]
if *hash_id_address == EMPTY {
*hash_id_address = old_hash_id
*key_index_address = v
*key_address = x_key
break
} else {
v, *key_index_address = *key_index_address, v
old_hash_id, *hash_id_address = *hash_id_address, old_hash_id
x_key, *key_address = *key_address, x_key
old_hash_id = old_hash_id%3 + 1
}
}
if j == MAXITER-1 {
fmt.Println("insert failed, ", i)
}
}
}
func (c *Op_Cuckoo) Save(save_file_path string) {
// save
fmt.Println("start save file T1")
f, _ := os.Create(save_file_path)
defer f.Close()
enc := gob.NewEncoder(f)
enc.Encode(c)
}
func (c *Op_Cuckoo) Load(save_file_path string) {
T1_file, _ := os.Open(save_file_path)
defer T1_file.Close()
T1_dec := gob.NewDecoder(T1_file)
T1_dec.Decode(c)
}