-
Notifications
You must be signed in to change notification settings - Fork 5
/
gmap.go
159 lines (139 loc) · 4.02 KB
/
gmap.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
* Copyright (c) 2022, nwillc@gmail.com
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
package container
import (
"github.com/nwillc/genfuncs"
"golang.org/x/exp/maps"
)
// GMap implements the Map interface.
var (
_ Map[int, int] = (GMap[int, int])(nil)
_ Sequence[string] = (GMap[int, string])(nil)
_ Iterator[string] = (*gmapIterator[int, string])(nil)
)
// GMap is a generic type employing the standard Go map and implementation Map.
type (
GMap[K comparable, V any] map[K]V
gmapIterator[K comparable, V any] struct {
gmap GMap[K, V]
iterator Iterator[K]
}
)
// All returns true if all values in GMap satisfy the predicate.
func (m GMap[K, V]) All(predicate genfuncs.Function[V, bool]) (ok bool) {
for _, v := range m {
if !predicate(v) {
return ok
}
}
ok = true
return ok
}
// Any returns true if any values in GMap satisfy the predicate.
func (m GMap[K, V]) Any(predicate genfuncs.Function[V, bool]) (ok bool) {
for _, v := range m {
if predicate(v) {
ok = true
return ok
}
}
return ok
}
// Contains returns true if the GMap contains the given key.
func (m GMap[K, V]) Contains(key K) (isTrue bool) {
_, isTrue = m[key]
return isTrue
}
// Delete an entry in the GMap.
func (m GMap[K, V]) Delete(key K) {
delete(m, key)
}
// Filter a GMap by a predicate, returning a new GMap that contains only values that satisfy the predicate.
func (m GMap[K, V]) Filter(predicate genfuncs.Function[V, bool]) (result GMap[K, V]) {
result = make(GMap[K, V])
for k, v := range m {
if !predicate(v) {
continue
}
result[k] = v
}
return result
}
// FilterKeys returns a new GMap that contains only values whose key satisfy the predicate.
func (m GMap[K, V]) FilterKeys(predicate genfuncs.Function[K, bool]) (result GMap[K, V]) {
result = make(GMap[K, V])
for k, v := range m {
if !predicate(k) {
continue
}
result[k] = v
}
return result
}
// ForEach performs the given action on each entry in the GMap.
func (m GMap[K, V]) ForEach(action func(k K, v V)) {
for k, v := range m {
action(k, v)
}
}
// Get returns an entry from the Map. The returned bool indicates if the key is in the Map.
func (m GMap[K, V]) Get(key K) (v V, ok bool) {
v, ok = m[key]
return v, ok
}
// GetOrElse returns the value at the given key if it exists or returns the result of defaultValue.
func (m GMap[K, V]) GetOrElse(k K, defaultValue func() V) (value V) {
ok := false
value, ok = m[k]
if ok {
return value
}
value = defaultValue()
return value
}
// Iterator creates an iterator for the values in the GMap.
func (m GMap[K, V]) Iterator() Iterator[V] {
return &gmapIterator[K, V]{gmap: m, iterator: m.Keys().Iterator()}
}
// Keys return a GSlice containing the keys of the GMap.
func (m GMap[K, V]) Keys() (keys GSlice[K]) {
keys = maps.Keys(m)
return keys
}
// Len is the number of elements in the GMap.
func (m GMap[K, V]) Len() (length int) {
length = len(m)
return length
}
// Put a key and value in the Map.
func (m GMap[K, V]) Put(key K, value V) {
m[key] = value
}
// Values returns a GSlice of all the values in the GMap.
func (m GMap[K, V]) Values() (values GSlice[V]) {
values = maps.Values(m)
return values
}
func (g gmapIterator[K, V]) HasNext() bool {
return g.iterator.HasNext()
}
func (g gmapIterator[K, V]) Next() V {
if !g.HasNext() {
panic(genfuncs.NoSuchElement)
}
k := g.iterator.Next()
return g.gmap[k]
}