-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes_map.go
78 lines (65 loc) · 2.26 KB
/
types_map.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
// Copyright 2022 streams Author. All Rights Reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package streams
import "reflect"
type Map0[K, V comparable] map[K]V
func NewMap[K, V comparable]() Map0[K, V] { return Map0[K, V]{} }
func NewMapValue[K, V comparable](k K, v V) Map0[K, V] { return Map0[K, V]{k: v} }
func NewMapValue2[K, V comparable](k1, k2 K, v1, v2 V) Map0[K, V] { return Map0[K, V]{k1: v1, k2: v2} }
func NewMapValue3[K, V comparable](k1, k2, k3 K, v1, v2, v3 V) Map0[K, V] {
return Map0[K, V]{k1: v1, k2: v2, k3: v3}
}
func (m Map0[K, V]) Len() int { return len(m) }
func (m Map0[K, V]) Put(k K, v V) Map0[K, V] { m[k] = v; return m }
func (m Map0[K, V]) PutAll(entries []*Entry[K, V]) Map0[K, V] {
for _, entry := range entries {
m.Put(entry.K, entry.V)
}
return m
}
func (m Map0[K, V]) Delete(k K) { delete(m, k) }
func (m Map0[K, V]) Clear() { m.Iterate(func(k K, _ V) { m.Delete(k) }) }
func (m Map0[K, V]) ContainsKey(k K) bool { _, have := m[k]; return have }
func (m Map0[K, V]) ContainsValue(v V) bool {
for _, v0 := range m {
if reflect.DeepEqual(v0, v) {
return true
}
}
return false
}
func (m Map0[K, V]) Iterate(f func(k K, v V)) {
for k, v := range m {
f(k, v)
}
}
func (m Map0[K, V]) Keys() []K {
ks := make([]K, 0)
for k := range m {
ks = append(ks, k)
}
return ks
}
func (m Map0[K, V]) Values() []V {
vs := make([]V, 0)
for _, v := range m {
vs = append(vs, v)
}
return vs
}
func (m Map0[K, V]) KeysReduce(sum K, reduceFunc func(k K, sum *K)) K {
m.Iterate(func(k K, _ V) { reduceFunc(k, &sum) })
return sum
}
func (m Map0[K, V]) ValuesReduce(sum V, reduceFunc func(v V, sum *V)) V {
m.Iterate(func(_ K, v V) { reduceFunc(v, &sum) })
return sum
}