-
Notifications
You must be signed in to change notification settings - Fork 247
/
multimap.go
150 lines (120 loc) · 4.06 KB
/
multimap.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
package mapz
import (
"golang.org/x/exp/maps"
)
// ReadOnlyMultimap is a read-only multimap.
type ReadOnlyMultimap[T comparable, Q any] interface {
// Has returns true if the key is found in the map.
Has(key T) bool
// Get returns the values for the given key in the map and whether the key
// existed.
// If the key does not exist, an empty slice is returned.
Get(key T) ([]Q, bool)
// IsEmpty returns true if the map is currently empty.
IsEmpty() bool
// Len returns the length of the map, e.g. the number of *keys* present.
Len() int
// Keys returns the keys of the map.
Keys() []T
// Values returns all values in the map.
Values() []Q
}
// NewMultiMap initializes a new MultiMap.
func NewMultiMap[T comparable, Q any]() *MultiMap[T, Q] {
return &MultiMap[T, Q]{items: map[T][]Q{}}
}
// NewMultiMapWithCap initializes with the provided capacity for the top-level
// map.
func NewMultiMapWithCap[T comparable, Q any](capacity uint32) *MultiMap[T, Q] {
return &MultiMap[T, Q]{items: make(map[T][]Q, capacity)}
}
// MultiMap represents a map that can contain 1 or more values for each key.
type MultiMap[T comparable, Q any] struct {
items map[T][]Q
}
// Clear clears all entries in the map.
func (mm *MultiMap[T, Q]) Clear() {
mm.items = map[T][]Q{}
}
// Add inserts the value into the map at the given key.
//
// If there exists an existing value, then this value is appended
// *without comparison*. Put another way, a value can be added twice, if this
// method is called twice for the same value.
func (mm *MultiMap[T, Q]) Add(key T, item Q) {
if _, ok := mm.items[key]; !ok {
mm.items[key] = []Q{}
}
mm.items[key] = append(mm.items[key], item)
}
// RemoveKey removes the given key from the map.
func (mm *MultiMap[T, Q]) RemoveKey(key T) {
delete(mm.items, key)
}
// Has returns true if the key is found in the map.
func (mm *MultiMap[T, Q]) Has(key T) bool {
_, ok := mm.items[key]
return ok
}
// Get returns the values stored in the map for the provided key and whether
// the key existed.
//
// If the key does not exist, an empty slice is returned.
func (mm *MultiMap[T, Q]) Get(key T) ([]Q, bool) {
found, ok := mm.items[key]
if !ok {
return []Q{}, false
}
return found, true
}
// IsEmpty returns true if the map is currently empty.
func (mm *MultiMap[T, Q]) IsEmpty() bool { return len(mm.items) == 0 }
// Len returns the length of the map, e.g. the number of *keys* present.
func (mm *MultiMap[T, Q]) Len() int { return len(mm.items) }
// Keys returns the keys of the map.
func (mm *MultiMap[T, Q]) Keys() []T { return maps.Keys(mm.items) }
// Values returns all values in the map.
func (mm MultiMap[T, Q]) Values() []Q {
values := make([]Q, 0, len(mm.items)*2)
for _, valueSlice := range maps.Values(mm.items) {
values = append(values, valueSlice...)
}
return values
}
// AsReadOnly returns a read-only *copy* of the mulitmap.
func (mm *MultiMap[T, Q]) AsReadOnly() ReadOnlyMultimap[T, Q] {
return readOnlyMultimap[T, Q]{
maps.Clone(mm.items),
}
}
type readOnlyMultimap[T comparable, Q any] struct {
items map[T][]Q
}
// Has returns true if the key is found in the map.
func (mm readOnlyMultimap[T, Q]) Has(key T) bool {
_, ok := mm.items[key]
return ok
}
// Get returns the values for the given key in the map and whether the key existed. If the key
// does not exist, an empty slice is returned.
func (mm readOnlyMultimap[T, Q]) Get(key T) ([]Q, bool) {
found, ok := mm.items[key]
if !ok {
return []Q{}, false
}
return found, true
}
// IsEmpty returns true if the map is currently empty.
func (mm readOnlyMultimap[T, Q]) IsEmpty() bool { return len(mm.items) == 0 }
// Len returns the length of the map, e.g. the number of *keys* present.
func (mm readOnlyMultimap[T, Q]) Len() int { return len(mm.items) }
// Keys returns the keys of the map.
func (mm readOnlyMultimap[T, Q]) Keys() []T { return maps.Keys(mm.items) }
// Values returns all values in the map.
func (mm readOnlyMultimap[T, Q]) Values() []Q {
values := make([]Q, 0, len(mm.items)*2)
for _, valueSlice := range maps.Values(mm.items) {
values = append(values, valueSlice...)
}
return values
}