This repository has been archived by the owner on Dec 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bidi.go
68 lines (53 loc) · 1.47 KB
/
bidi.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
package main
import (
"sync"
)
// BidiIntToStrDict is used to store mapping of integer to string and vice versa.
type BidiIntToStrDict struct {
int2str map[int]string
str2int map[string]int
sync.Mutex
}
// NewBidiIntToStrDict creates new BidiIntToStrDict object.
func NewBidiIntToStrDict() *BidiIntToStrDict {
obj := new(BidiIntToStrDict)
obj.int2str = make(map[int]string)
obj.str2int = make(map[string]int)
return obj
}
// Bind is used to define specified integer to string mapping and vice versa.
func (obj *BidiIntToStrDict) Bind(i int, s string) {
obj.Lock()
obj.int2str[i] = s
obj.str2int[s] = i
obj.Unlock()
}
// UnBind is used to undefine specified keys.
func (obj *BidiIntToStrDict) UnBind(keys ...interface{}) {
obj.Lock()
for i := range keys {
switch v := keys[i].(type) {
case int:
if k, ok := obj.GetString(v); ok {
delete(obj.str2int, k)
}
delete(obj.int2str, v)
case string:
if k, ok := obj.GetInteger(v); ok {
delete(obj.int2str, k)
}
delete(obj.str2int, v)
}
}
obj.Unlock()
}
// GetInteger returns integer value based on specified string key with bool that specifies successful lookup.
func (obj *BidiIntToStrDict) GetInteger(key string) (int, bool) {
val, ok := obj.str2int[key]
return val, ok
}
// GetString returns string value based on specified integer key with bool that specifies successful lookup.
func (obj *BidiIntToStrDict) GetString(key int) (string, bool) {
val, ok := obj.int2str[key]
return val, ok
}