Skip to content

Commit

Permalink
泛型 array
Browse files Browse the repository at this point in the history
  • Loading branch information
李文龙 committed Mar 7, 2023
1 parent 4ab6641 commit cd11b12
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 38 deletions.
64 changes: 62 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,62 @@
# item
go array、map actions
# item #

go functions about array、map liked php array_values array_columns actions

go version must gte 1.18

[![Dongle Release](https://img.shields.io/github/release/lwl1989/item.svg)](https://github.com/lwl1989/item/releases)
[![Go doc](https://img.shields.io/badge/go.dev-reference-brightgreen?logo=go&logoColor=white&style=flat)](https://pkg.go.dev/github.com/lwl1909/item)
![License](https://img.shields.io/github/license/lwl1909/item)

### Installation

```go
// By github
import (
"github.com/lwl1989/item"
)
```

### Introduction

#### Compare interface

```go
// go comparable
type comparable interface{ comparable }
// item Compare
type ICompare interface {
Compare(ICompare) bool
GetCompareValue() interface{}
}
```

#### map

```go
MapKeys(mp map[string]T) []string
MapGetKey[T comparable](mp map[string]T, item T) (key string)
MapGetKeyWithCompare[T ICompare](mp map[string]T, item T) string
```

#### array

```go
InArray[T comparable](params []T, value T) bool
ArrayUnique[V comparable](params []V) []V
GetArrayIndex[T comparable](params []T, value T) int
InArrayCompare[T ICompare](params []T, value T) bool
ArrayMap(params []map[string]interface{}, key string) map[string]map[string]interface{}
ArrayMapCompare[T comparable](params []map[T]interface{}, key T) map[T]map[T]interface{}
ArrayMapCompareValue[K comparable,V any](params []map[K]V, key K) map[K]map[K]V
ArrayColumns[K comparable,V any](params []map[K]V, key K) []V
ArrayColumnValues[K comparable,V any](params []map[K]interface{}, key, VKey K) map[K]V
```

#### struct

```go
GetItemMap[K comparable, V any, Item any](values []Item, fieldK, fieldV string) map[K]V
```

### Usage and example
73 changes: 55 additions & 18 deletions array.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ func InArrayCompare[T ICompare](params []T, value T) bool {
}

func ArrayMap(params []map[string]interface{}, key string) map[string]map[string]interface{} {
if len(params) <1 {
if len(params) < 1 {
return nil
}
res := make(map[string]map[string]interface{})
for _,v := range params {
for _, v := range params {
_, ok := v[key]
if !ok {
continue
Expand All @@ -42,12 +42,12 @@ func ArrayMap(params []map[string]interface{}, key string) map[string]map[string
return res
}

func ArrayMapCompare[T comparable](params []map[T]interface{}, key T) map[T]map[T]interface{} {
if len(params) <1 {
func ArrayMapCompare[T comparable](params []map[T]interface{}, key T) map[T]map[T]interface{} {
if len(params) < 1 {
return nil
}
res := make(map[T]map[T]interface{})
for _,v := range params {
for _, v := range params {
_, ok := v[key]
if !ok {
continue
Expand All @@ -57,12 +57,12 @@ func ArrayMapCompare[T comparable](params []map[T]interface{}, key T) map[T]map
return res
}

func ArrayMapCompareValue[K comparable,V any](params []map[K]V, key K) map[K]map[K]V {
if len(params) <1 {
func ArrayMapCompareValue[K comparable, V any](params []map[K]V, key K) map[K]map[K]V {
if len(params) < 1 {
return nil
}
res := make(map[K]map[K]V)
for _,v := range params {
for _, v := range params {
_, ok := v[key]
if !ok {
continue
Expand All @@ -72,12 +72,12 @@ func ArrayMapCompareValue[K comparable,V any](params []map[K]V, key K) map[K]ma
return res
}

func ArrayColumns[K comparable,V any](params []map[K]V, key K) []V {
if len(params) <1 {
func ArrayColumns[K comparable, V any](params []map[K]V, key K) []V {
if len(params) < 1 {
return nil
}
res := make([]V, len(params))
for i,m := range params {
res := make([]V, len(params))
for i, m := range params {
v, ok := m[key]
if !ok {
// res[i]= *new(V) do not create it
Expand All @@ -88,25 +88,62 @@ func ArrayColumns[K comparable,V any](params []map[K]V, key K) []V {
return res
}

func ArrayColumnValues[K comparable,V any](params []map[K]interface{}, key, VKey K) map[K]V {
if len(params) <1 {
func ArrayColumnValues[K comparable, V any](params []map[K]interface{}, key, VKey K) map[K]V {
if len(params) < 1 {
return nil
}
res := make(map[K]V)
for _,m := range params {
for _, m := range params {
_, ok := m[key]
if !ok {
// res[i]= *new(V) do not create it
continue
}
v,ok := m[VKey]
v, ok := m[VKey]
if !ok {
continue
}
value,ok := v.(V)
value, ok := v.(V)
if ok {
res[key] = value
}
}
return res
}
}

func ArrayUnique[V comparable](params []V) []V {
mp := map[V]struct{}{}
res := make([]V, len(params))
var index int
for _, v := range params {
if _, ok := mp[v]; ok {
continue
}
mp[v] = struct{}{}
res[index] = v
index++
}
return res[:index]
}

func ArrayDiff[V comparable](params ...[]V) []V {
var all []V
mp := map[V]int8{}
for _, v := range params {
all = append(all, v...)
}
for _, v := range all {
if num, ok := mp[v]; ok {
mp[v] = num + 1
continue
}
mp[v] = 1
}
var res []V
for v, num := range mp {
if num == 1 {
res = append(res, v)
}
}
return res
}
22 changes: 15 additions & 7 deletions array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,25 @@ func TestInArrayCompare(t *testing.T) {
}

func TestArrayMapCompare(t *testing.T) {
fmt.Println( ArrayMapCompare[int]([]map[int]interface{}{
{3:"4",4:5},
{4:"3"},
fmt.Println(ArrayMapCompare[int]([]map[int]interface{}{
{3: "4", 4: 5},
{4: "3"},
}, 3))
fmt.Println( ArrayMapCompare[int]([]map[int]interface{}{
{4:"3"},
fmt.Println(ArrayMapCompare[int]([]map[int]interface{}{
{4: "3"},
}, 3))
}

func TestArrayMapCompareValue(t *testing.T) {
fmt.Println( ArrayMapCompareValue[string, ICompare]([]map[string]ICompare{
fmt.Println(ArrayMapCompareValue[string, ICompare]([]map[string]ICompare{
{"a": cp{value: "dasdas"}},
}, "a"))
}
}

func TestArrayUnique(t *testing.T) {
fmt.Println(ArrayUnique[string]([]string{"a", "aaa", "a", "bbb", "ccc", "a"}))
}

func TestArrayDiff(t *testing.T) {
fmt.Println(ArrayDiff[string]([]string{"a", "aaa", "a", "bbb", "ccc", "a", "ggg"}, []string{"a", "aaa", "a", "bbb", "ccc", "a", "fff"}))
}
19 changes: 8 additions & 11 deletions map.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,35 @@
package item

func MapKeys[T any](mp map[string]T) (keys []string){
func MapKeys[T any](mp map[string]T) (keys []string) {
if len(mp) == 0 {
return
}
for k,_:= range mp {
for k, _ := range mp {
keys = append(keys, k)
}
return keys
}

func MapGetKey[T comparable](mp map[string]T, item T) (key string){
if len(mp) == 0 {
func MapGetKey[T comparable](mp map[string]T, item T) string {
if len(mp) == 0 {
return ""
}
for k,v := range mp {
for k, v := range mp {
if v == item {
return k
}
}
return ""
}

func MapGetKeyWithCompare[T ICompare](mp map[string]T, item T) string {
if len(mp) == 0 {
func MapGetKeyWithCompare[T ICompare](mp map[string]T, item T) string {
if len(mp) == 0 {
return ""
}
for k,v := range mp {
for k, v := range mp {
if item.Compare(v) {
return k
}
}
return ""
}



0 comments on commit cd11b12

Please sign in to comment.