|
@ -4,85 +4,112 @@ |
|
|
package helper |
|
|
package helper |
|
|
|
|
|
|
|
|
import ( |
|
|
import ( |
|
|
"errors" |
|
|
|
|
|
"fmt" |
|
|
|
|
|
|
|
|
"errors" |
|
|
|
|
|
"fmt" |
|
|
|
|
|
"sort" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
// 求差集
|
|
|
// 求差集
|
|
|
func Diff(slice1, slice2 []string) []string { |
|
|
func Diff(slice1, slice2 []string) []string { |
|
|
m := make(map[string]int) |
|
|
|
|
|
nn := make([]string, 0) |
|
|
|
|
|
inter := Intersect(slice1, slice2) |
|
|
|
|
|
for _, v := range inter { |
|
|
|
|
|
m[v]++ |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
m := make(map[string]int) |
|
|
|
|
|
nn := make([]string, 0) |
|
|
|
|
|
inter := Intersect(slice1, slice2) |
|
|
|
|
|
for _, v := range inter { |
|
|
|
|
|
m[v]++ |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
for _, value := range slice1 { |
|
|
|
|
|
times, _ := m[value] |
|
|
|
|
|
if times == 0 { |
|
|
|
|
|
nn = append(nn, value) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return nn |
|
|
|
|
|
|
|
|
for _, value := range slice1 { |
|
|
|
|
|
times, _ := m[value] |
|
|
|
|
|
if times == 0 { |
|
|
|
|
|
nn = append(nn, value) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return nn |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 求交集
|
|
|
// 求交集
|
|
|
func Intersect(slice1, slice2 []string) []string { |
|
|
func Intersect(slice1, slice2 []string) []string { |
|
|
m := make(map[string]int) |
|
|
|
|
|
nn := make([]string, 0) |
|
|
|
|
|
for _, v := range slice1 { |
|
|
|
|
|
m[v]++ |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
m := make(map[string]int) |
|
|
|
|
|
nn := make([]string, 0) |
|
|
|
|
|
for _, v := range slice1 { |
|
|
|
|
|
m[v]++ |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
for _, v := range slice2 { |
|
|
|
|
|
times, _ := m[v] |
|
|
|
|
|
if times == 1 { |
|
|
|
|
|
nn = append(nn, v) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return nn |
|
|
|
|
|
|
|
|
for _, v := range slice2 { |
|
|
|
|
|
times, _ := m[v] |
|
|
|
|
|
if times == 1 { |
|
|
|
|
|
nn = append(nn, v) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return nn |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 求并集
|
|
|
// 求并集
|
|
|
func Union(slice1, slice2 []string) []string { |
|
|
func Union(slice1, slice2 []string) []string { |
|
|
m := make(map[string]int) |
|
|
|
|
|
for _, v := range slice1 { |
|
|
|
|
|
m[v]++ |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
m := make(map[string]int) |
|
|
|
|
|
for _, v := range slice1 { |
|
|
|
|
|
m[v]++ |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
for _, v := range slice2 { |
|
|
|
|
|
times, _ := m[v] |
|
|
|
|
|
if times == 0 { |
|
|
|
|
|
slice1 = append(slice1, v) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return slice1 |
|
|
|
|
|
|
|
|
for _, v := range slice2 { |
|
|
|
|
|
times, _ := m[v] |
|
|
|
|
|
if times == 0 { |
|
|
|
|
|
slice1 = append(slice1, v) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return slice1 |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func Unique(originals interface{}) (interface{}, error) { |
|
|
func Unique(originals interface{}) (interface{}, error) { |
|
|
temp := map[string]struct{}{} |
|
|
|
|
|
switch slice := originals.(type) { |
|
|
|
|
|
case []string: |
|
|
|
|
|
result := make([]string, 0, len(originals.([]string))) |
|
|
|
|
|
for _, item := range slice { |
|
|
|
|
|
key := fmt.Sprint(item) |
|
|
|
|
|
if _, ok := temp[key]; !ok { |
|
|
|
|
|
temp[key] = struct{}{} |
|
|
|
|
|
result = append(result, item) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return result, nil |
|
|
|
|
|
case []int64: |
|
|
|
|
|
result := make([]int64, 0, len(originals.([]int64))) |
|
|
|
|
|
for _, item := range slice { |
|
|
|
|
|
key := fmt.Sprint(item) |
|
|
|
|
|
if _, ok := temp[key]; !ok { |
|
|
|
|
|
temp[key] = struct{}{} |
|
|
|
|
|
result = append(result, item) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return result, nil |
|
|
|
|
|
default: |
|
|
|
|
|
return nil, errors.New(fmt.Sprintf("Unknown type: %T", slice)) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
temp := map[string]struct{}{} |
|
|
|
|
|
switch slice := originals.(type) { |
|
|
|
|
|
case []string: |
|
|
|
|
|
result := make([]string, 0, len(originals.([]string))) |
|
|
|
|
|
for _, item := range slice { |
|
|
|
|
|
key := fmt.Sprint(item) |
|
|
|
|
|
if _, ok := temp[key]; !ok { |
|
|
|
|
|
temp[key] = struct{}{} |
|
|
|
|
|
result = append(result, item) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return result, nil |
|
|
|
|
|
case []int64: |
|
|
|
|
|
result := make([]int64, 0, len(originals.([]int64))) |
|
|
|
|
|
for _, item := range slice { |
|
|
|
|
|
key := fmt.Sprint(item) |
|
|
|
|
|
if _, ok := temp[key]; !ok { |
|
|
|
|
|
temp[key] = struct{}{} |
|
|
|
|
|
result = append(result, item) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return result, nil |
|
|
|
|
|
default: |
|
|
|
|
|
return nil, errors.New(fmt.Sprintf("Unknown type: %T", slice)) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 将切片字符串(数字型)排序
|
|
|
|
|
|
func StringSliceSort(str []string, sort_type string) ([]string, error) { |
|
|
|
|
|
int_arr := []int{} |
|
|
|
|
|
string_arr := []string{} |
|
|
|
|
|
|
|
|
|
|
|
if len(str) < 1 { |
|
|
|
|
|
return []string{}, nil |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
for _, val := range str { |
|
|
|
|
|
int_arr = append(int_arr, ToInt(val)) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if sort_type == "desc" { |
|
|
|
|
|
sort.Sort(sort.Reverse(sort.IntSlice(int_arr))) |
|
|
|
|
|
}else{ |
|
|
|
|
|
sort.Ints(int_arr) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
for _, val_int := range int_arr { |
|
|
|
|
|
string_arr = append(string_arr, ToStr(val_int)) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return string_arr, nil |
|
|
} |
|
|
} |