-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathkeyid.go
56 lines (48 loc) · 1.11 KB
/
keyid.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
// (c) 2014 Cergoo
// under terms of ISC license
// Package keyid it's key to id and id to key association implementation
package keyid
type (
// Tkeyid struct
Tkeyid struct {
keytoid map[string]uint
idtokey map[uint]string
}
)
// New constructor of a new Tkeyid
func New() *Tkeyid {
return &Tkeyid{keytoid: make(map[string]uint), idtokey: make(map[uint]string)}
}
// Set set cortege (key, id)
func (t *Tkeyid) Set(key string, id uint) {
t.idtokey[id] = key
t.keytoid[key] = id
}
// DelFromKey delete from key and return id
func (t *Tkeyid) DelFromKey(key string) (id uint, ok bool) {
id, ok = t.keytoid[key]
if ok {
delete(t.idtokey, id)
delete(t.keytoid, key)
}
return
}
// DelFromId delete from id and return key
func (t *Tkeyid) DelFromId(id uint) (key string, ok bool) {
key, ok = t.idtokey[id]
if ok {
delete(t.idtokey, id)
delete(t.keytoid, key)
}
return
}
// GetId get id from key
func (t *Tkeyid) GetId(key string) (id uint, ok bool) {
id, ok = t.keytoid[key]
return
}
// GetKey get key from id
func (t *Tkeyid) GetKey(id uint) (key string, ok bool) {
key, ok = t.idtokey[id]
return
}