-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathst.go
67 lines (55 loc) · 1.09 KB
/
st.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
package algo
// Key is an interface of the key in ST
type Key interface {
compareTo(interface{}) int
}
// StringKey implements Key
type StringKey string
// CompareTo ...
func (k StringKey) compareTo(other interface{}) int {
if k < other.(StringKey) {
return -1
} else if k > other.(StringKey) {
return 1
}
return 0
}
// ST is symbol table implemented by go map[string]int
type ST map[string]int
// NewST ...
func NewST() ST {
return make(ST)
}
// Put add new key value pair into the st
func (st ST) Put(key string, val int) {
st[key] = val
}
// Get add new key value pair into the st
func (st ST) Get(key string) int {
return st[key]
}
// Delete ...
func (st ST) Delete(key string) {
delete(st, key)
}
// Contains ...
func (st ST) Contains(key string) bool {
_, ok := st[key]
return ok
}
// Size ...
func (st ST) Size() int {
return len(st)
}
// IsEmpty add new key value pair into the st
func (st ST) IsEmpty() bool {
return st.Size() == 0
}
// Keys ...
func (st ST) Keys() []string {
keys := make([]string, 0, len(st))
for k := range st {
keys = append(keys, k)
}
return keys
}