1
2
3
4
5 package a
6
7 type Pair[L, R any] struct {
8 L L
9 R R
10 }
11
12 func Two[L, R any](l L, r R) Pair[L, R] {
13 return Pair[L, R]{L: l, R: r}
14 }
15
16 type Map[K, V any] interface {
17 Put(K, V)
18 Len() int
19 Iterate(func(Pair[K, V]) bool)
20 }
21
22 type HashMap[K comparable, V any] struct {
23 m map[K]V
24 }
25
26 func NewHashMap[K comparable, V any](capacity int) HashMap[K, V] {
27 var m map[K]V
28 if capacity >= 1 {
29 m = make(map[K]V, capacity)
30 } else {
31 m = map[K]V{}
32 }
33
34 return HashMap[K, V]{m: m}
35 }
36
37 func (m HashMap[K, V]) Put(k K, v V) {
38 m.m[k] = v
39 }
40
41 func (m HashMap[K, V]) Len() int {
42 return len(m.m)
43 }
44
45 func (m HashMap[K, V]) Iterate(cb func(Pair[K, V]) bool) {
46 for k, v := range m.m {
47 if !cb(Two(k, v)) {
48 return
49 }
50 }
51 }
52
View as plain text