forked from go/golangs_learn
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
167 lines
3.0 KiB
167 lines
3.0 KiB
4 years ago
|
/**
|
||
|
该工作目录设置处理关于map集合计算功能
|
||
|
*/
|
||
|
package final_common
|
||
|
|
||
|
import "fmt"
|
||
|
|
||
|
// 并集(保留项) -- 遍历S2
|
||
|
func UnionF(s1, s2 map[string]interface{}) map[string]interface{} {
|
||
|
for k, v := range s2 {
|
||
|
if _, ok := s1[k]; ok {
|
||
|
continue
|
||
|
} else {
|
||
|
s1[k] = v
|
||
|
}
|
||
|
}
|
||
|
return s1
|
||
|
}
|
||
|
|
||
|
// 并集(覆盖项) -- 遍历S2
|
||
|
func UnionL(s1, s2 map[string]interface{}) map[string]interface{} {
|
||
|
for k, v := range s2 {
|
||
|
s1[k] = v
|
||
|
}
|
||
|
return s1
|
||
|
}
|
||
|
|
||
|
// 差集 -- 遍历S2
|
||
|
func Minus(s1, s2 map[string]interface{}) map[string]interface{} {
|
||
|
for k, _ := range s2 {
|
||
|
delete(s1, k)
|
||
|
}
|
||
|
return s1
|
||
|
}
|
||
|
|
||
|
// 交集 -- 遍历S1
|
||
|
func Intersect(s1, s2 map[string]interface{}) map[string]interface{} {
|
||
|
for k, _ := range s1 {
|
||
|
if _, ok := s2[k]; ok {
|
||
|
continue
|
||
|
} else {
|
||
|
delete(s1, k)
|
||
|
}
|
||
|
}
|
||
|
return s1
|
||
|
}
|
||
|
|
||
|
// 补集 -- 遍历S2
|
||
|
func Complement(s1, s2 map[string]interface{}) map[string]interface{} {
|
||
|
for k, _ := range s2 {
|
||
|
if _, ok := s1[k]; ok {
|
||
|
delete(s2, k)
|
||
|
} else {
|
||
|
continue
|
||
|
}
|
||
|
}
|
||
|
return s2
|
||
|
}
|
||
|
|
||
|
//========================== 指定map(map) ==========================
|
||
|
|
||
|
// 并集(保留项) -- 遍历S2
|
||
|
func UnionFMap(s1, s2 map[string]map[string]string) map[string]map[string]string {
|
||
|
for k, v := range s2 { // 第一层
|
||
|
if m, ok := s1[k]; ok {
|
||
|
for mk, mv := range v { // 第二层
|
||
|
if _, ok := m[mk]; ok {
|
||
|
continue
|
||
|
} else {
|
||
|
m[mk] = mv
|
||
|
}
|
||
|
}
|
||
|
} else {
|
||
|
s1[k] = v
|
||
|
}
|
||
|
}
|
||
|
return s1
|
||
|
}
|
||
|
|
||
|
// 并集(覆盖项) -- 遍历S2
|
||
|
func UnionLMap(s1, s2 map[string]map[string]string) map[string]map[string]string {
|
||
|
for k, v := range s2 { // 第一层
|
||
|
if m, ok := s1[k]; ok {
|
||
|
for mk, mv := range v { // 第二层
|
||
|
if _, ok := m[mk]; ok {
|
||
|
continue
|
||
|
} else {
|
||
|
m[mk] = mv
|
||
|
}
|
||
|
}
|
||
|
} else {
|
||
|
s1[k] = v
|
||
|
}
|
||
|
}
|
||
|
return s1
|
||
|
}
|
||
|
|
||
|
// 差集 -- 遍历S2
|
||
|
func MinusMap(s1, s2 map[string]map[string]string) map[string]map[string]string {
|
||
|
a:
|
||
|
for k, v := range s2 { // 第一层
|
||
|
if m, ok := s1[k]; ok {
|
||
|
for mk, _ := range v { // 第二层
|
||
|
if _, ok := m[mk]; ok {
|
||
|
continue
|
||
|
} else {
|
||
|
break a
|
||
|
}
|
||
|
}
|
||
|
delete(s1, k)
|
||
|
} else {
|
||
|
break a
|
||
|
}
|
||
|
}
|
||
|
return s1
|
||
|
}
|
||
|
|
||
|
// 交集 -- 遍历S1
|
||
|
func IntersectMap(s1, s2 map[string]map[string]string) map[string]map[string]string {
|
||
|
for k, v := range s1 { // 第一层
|
||
|
if m, ok := s2[k]; ok {
|
||
|
for mk, _ := range v { // 第二层
|
||
|
if _, ok := m[mk]; ok {
|
||
|
continue
|
||
|
} else {
|
||
|
delete(s1, k)
|
||
|
}
|
||
|
}
|
||
|
} else {
|
||
|
delete(s1, k)
|
||
|
}
|
||
|
}
|
||
|
return s1
|
||
|
}
|
||
|
|
||
|
// 补集 -- 遍历S2
|
||
|
func ComplementMap(s1, s2 map[string]map[string]string) map[string]map[string]string {
|
||
|
a:
|
||
|
for k, v := range s2 { // 第一层
|
||
|
if m, ok := s1[k]; ok {
|
||
|
delete(s2, k)
|
||
|
} else {
|
||
|
for mk, _ := range v { // 第二层
|
||
|
if _, ok := m[mk]; ok {
|
||
|
continue
|
||
|
} else {
|
||
|
break a
|
||
|
}
|
||
|
}
|
||
|
delete(s2, k)
|
||
|
}
|
||
|
}
|
||
|
return s2
|
||
|
}
|
||
|
|
||
|
//========================== 打印map(map) ==========================
|
||
|
|
||
|
// 打印Map集合
|
||
|
func PrintMap(f map[string]map[string]string) {
|
||
|
for key, value := range f {
|
||
|
fmt.Println("首层:", key)
|
||
|
for key, value := range value {
|
||
|
fmt.Println("次层:", key, value)
|
||
|
}
|
||
|
}
|
||
|
}
|