-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.go
executable file
·89 lines (82 loc) · 1.83 KB
/
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
79
80
81
82
83
84
85
86
87
88
89
package until
import (
"bytes"
"fmt"
"sort"
)
// MergeMap 合并两个map[int]int
func MergeMap(args ...map[int]int) map[int]int {
mergedMap := map[int]int{}
for _, m := range args {
for k, v := range m {
if _, exists := mergedMap[k]; exists {
mergedMap[k] += v
} else {
mergedMap[k] = v
}
}
}
return mergedMap
}
// GetMapMinValue 获取map[int]int中最小值, 并返回所有key的集合
// value约定是大于0的
func GetMapMinValue(m map[int]int) (int, []int) {
// value到keys的对应关系
keys := []int{}
// 最小值
minValue := -1
for k, v := range m {
if minValue == -1 || v < minValue {
minValue = v
keys = []int{k}
} else if v == minValue {
keys = append(keys, k)
}
}
return minValue, keys
}
// GetMapMaxValue 获取map[int]int中最大值, 并返回所有key的集合
// value约定是大于0的
func GetMapMaxValue(m map[int]int) (int, []int) {
// value到keys的对应关系
keys := []int{}
// 最大值
maxValue := -1
for k, v := range m {
if v > maxValue {
maxValue = v
keys = []int{k}
} else if v == maxValue {
keys = append(keys, k)
}
}
return maxValue, keys
}
// GetMapValues 获取map[int]int结构的所有value
// 返回结果去重
func GetMapValues(m map[int]int) []int {
values := make([]int, 0)
for _, v := range m {
values = append(values, v)
}
return values
}
func Urlencode(data map[string]interface{}, slice []string) string {
var buf bytes.Buffer
var post_string []string
for k, _ := range data {
str := fmt.Sprintf("%s", k)
if !InStringSlice(str, slice) {
post_string = append(post_string, fmt.Sprintf("%s", k))
}
}
sort.Strings(post_string)
for _, vel := range post_string {
buf.WriteString(vel)
buf.WriteByte('=')
buf.WriteString(fmt.Sprintf("%s", data[vel]))
buf.WriteByte('&')
}
s := buf.String()
return s[0 : len(s)-1]
}