-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmaps.go
123 lines (92 loc) · 1.87 KB
/
maps.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package maps
//---
// generic interface{} Key() based Map:
//---
type Map map[interface{}]interface{}
type Pair struct {
Key interface{}
Value interface{}
}
type Keyer interface {
Key() interface{}
}
func key(k interface{}) interface{} {
if k, ok := k.(Keyer); ok {
return k.Key()
}
return k
}
func (m Map) Get(k interface{}) (interface{}, bool) {
v, Ok := m[key(k)]
return v.(Pair).Value, Ok
}
func (m Map) Delete(k interface{}) {
m[key(k)] = nil, false
}
func (m Map) Insert(k interface{}, v interface{}) {
m[key(k)] = Pair{k, v}
}
func (m Map) Len() int {
return len(m)
}
func (m Map) Do(f func(key interface{}, value interface{})) {
for _, p := range m {
f(p.(Pair).Key, p.(Pair).Value)
}
}
//---
// Specialized String() key based SMap:
//---
type Stringer interface {
String() string
}
type SMap map[string]Pair
func NewSMap() SMap {
return make(map[string]Pair)
}
func (m SMap) Insert(key Stringer, value interface{}) {
m[key.String()] = Pair{key, value}
}
func (m SMap) Do(f func(key interface{},value interface{})) {
for _, p := range m {
f(p.Key, p.Value)
}
}
func (m SMap) Get(key Stringer) (interface{}, bool) {
v, t := m[key.String()]
return v.Value, t
}
func (m SMap) Delete(key Stringer) {
m[key.String()] = Pair{nil, nil}, false
}
func (m SMap) Len() int {
return len(m)
}
//---
// Specialized Int() key based IMap :
//---
type Inter interface {
Int() int
}
type IMap map[int]Pair
func NewIMap() IMap {
return make(map[int]Pair)
}
func (m IMap) Insert(key Inter, value interface{}) {
m[key.Int()] = Pair{key, value}
}
func (m IMap) Do(f func(key interface{}, value interface{})) {
for _, p := range m {
f(p.Key, p.Value)
}
}
func (m IMap) Get(key Inter) (interface{}, bool) {
v, t := m[key.Int()]
return v.Value, t
}
func (m IMap) Delete(key Inter) {
m[key.Int()] = Pair{nil, nil}, false
}
func (m IMap) Len() int {
return len(m)
}