-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathmultimap.go
57 lines (51 loc) · 1.49 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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
package autodiscovery
// a multimap is a string/string map that can contain multiple values for a
// single key. Duplicate values are allowed (but not used in this package).
type multimap struct {
data map[string][]string
}
// newMultimap creates a new multimap
func newMultimap() multimap {
return multimap{data: map[string][]string{}}
}
// insert adds an item into a multimap
func (m *multimap) insert(k, v string) {
var slice []string
if existing, found := m.data[k]; found {
slice = existing
} else {
slice = []string{}
}
m.data[k] = append(slice, v)
}
// remove removes an item from a multimap
func (m *multimap) remove(k, v string) {
if values, found := m.data[k]; found {
for i, u := range values {
if u == v {
// remove index i from the slice
values[i] = values[len(values)-1]
values = values[:len(values)-1]
break
}
}
if len(values) > 0 {
m.data[k] = values
} else {
delete(m.data, k)
}
}
}
// get gets the set of items with the given key. The returned slice must not
// be modified, and is only valid until the next multimap operation. This
// will always return a valid slice, not nil.
func (m *multimap) get(k string) []string {
if values, found := m.data[k]; found {
return values
}
return []string{}
}