diff --git a/.github/workflows/codecov.yml b/.github/workflows/codecov.yml index 47b40b17..7a8bc8f4 100644 --- a/.github/workflows/codecov.yml +++ b/.github/workflows/codecov.yml @@ -1,13 +1,13 @@ -name: Test and coverage +name: test on: push: branches: - main - - v2 + # - v2 pull_request: branches: - main - - v2 + # - v2 jobs: build: runs-on: ubuntu-latest diff --git a/README.md b/README.md index 4519d38f..1081be9d 100644 --- a/README.md +++ b/README.md @@ -6,9 +6,10 @@
![Go version](https://img.shields.io/badge/go-%3E%3D1.16-9cf) -[![Release](https://img.shields.io/badge/release-1.1.9-green.svg)](https://github.com/duke-git/lancet/releases) +[![Release](https://img.shields.io/badge/release-1.1.10-green.svg)](https://github.com/duke-git/lancet/releases) [![GoDoc](https://godoc.org/github.com//duke-git/lancet?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet) [![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet)](https://goreportcard.com/report/github.com/duke-git/lancet) +[![test](https://github.com/duke-git/lancet/actions/workflows/codecov.yml/badge.svg?branch=main&event=push)](https://github.com/duke-git/lancet/actions/workflows/codecov.yml) [![codecov](https://codecov.io/gh/duke-git/lancet/branch/main/graph/badge.svg?token=FC48T1F078)](https://codecov.io/gh/duke-git/lancet) [![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/duke-git/lancet/blob/main/LICENSE) @@ -393,6 +394,7 @@ func main() { ```go func Contain[T comparable](slice []T, value T) bool //check if the value is in the slice or not +func ContainSubSlice[T comparable](slice, subslice []T) bool //check if the slice contain subslice or not func Chunk[T any](slice []T, size int) [][]T //creates an slice of elements split into groups the length of size. func ConvertSlice(originalSlice interface{}, newSliceType reflect.Type) interface{} //convert originalSlice to newSliceType func Difference[T comparable](slice1, slice2 []T) []T //creates an slice of whose element not included in the other given slice diff --git a/README_zh-CN.md b/README_zh-CN.md index 754870e0..fe77f622 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -6,9 +6,10 @@
![Go version](https://img.shields.io/badge/go-%3E%3D1.16-9cf) -[![Release](https://img.shields.io/badge/release-1.1.9-green.svg)](https://github.com/duke-git/lancet/releases) +[![Release](https://img.shields.io/badge/release-1.1.10-green.svg)](https://github.com/duke-git/lancet/releases) [![GoDoc](https://godoc.org/github.com//duke-git/lancet?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet) [![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet)](https://goreportcard.com/report/github.com/duke-git/lancet) +[![test](https://github.com/duke-git/lancet/actions/workflows/codecov.yml/badge.svg?branch=main&event=push)](https://github.com/duke-git/lancet/actions/workflows/codecov.yml) [![codecov](https://codecov.io/gh/duke-git/lancet/branch/main/graph/badge.svg?token=FC48T1F078)](https://codecov.io/gh/duke-git/lancet) [![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/duke-git/lancet/blob/main/LICENSE) @@ -394,6 +395,7 @@ func main() { ```go func Contain[T comparable](slice []T, value T) bool //判断slice是否包含value +func ContainSubSlice[T comparable](slice, subslice []T) bool //判断slice是否包含subslice func Chunk[T any](slice []T, size int) [][]T //均分slice func ConvertSlice(originalSlice interface{}, newSliceType reflect.Type) interface{} //将originalSlice转换为 newSliceType func Difference[T comparable](slice1, slice2 []T) []T //返回切片,其元素在slice1中,不在slice2中 diff --git a/slice/slice.go b/slice/slice.go index 03b5baec..34896a21 100644 --- a/slice/slice.go +++ b/slice/slice.go @@ -24,6 +24,21 @@ func Contain[T comparable](slice []T, value T) bool { return false } +// ContainSubSlice check if the slice contain subslice or not +func ContainSubSlice[T comparable](slice, subslice []T) bool { + unique := make(map[T]bool) + for _, v := range slice { + unique[v] = true + } + for _, v := range subslice { + if !unique[v] { + return false + } + } + + return true +} + // Chunk creates an slice of elements split into groups the length of size. func Chunk[T any](slice []T, size int) [][]T { var res [][]T diff --git a/slice/slice_test.go b/slice/slice_test.go index 3e60fefd..0f3bd95f 100644 --- a/slice/slice_test.go +++ b/slice/slice_test.go @@ -17,6 +17,14 @@ func TestContain(t *testing.T) { assert.Equal(true, Contain([]int{1, 2, 3}, 1)) } +func TestContainSubSlice(t *testing.T) { + assert := internal.NewAssert(t, "TestContainSubSlice") + assert.Equal(true, ContainSubSlice([]string{"a", "a", "b", "c"}, []string{"a", "a"})) + assert.Equal(false, ContainSubSlice([]string{"a", "a", "b", "c"}, []string{"a", "d"})) + assert.Equal(true, ContainSubSlice([]int{1, 2, 3}, []int{1})) + assert.Equal(false, ContainSubSlice([]int{1, 2, 3}, []int{0})) +} + func TestChunk(t *testing.T) { assert := internal.NewAssert(t, "TestChunk") diff --git a/slice/slice_util.go b/slice/slice_util.go index b3113f1e..6f94503b 100644 --- a/slice/slice_util.go +++ b/slice/slice_util.go @@ -5,6 +5,31 @@ import ( "reflect" ) +// func quickSort[T comparable](slice []T, low, high int) []T { +// if low < high { +// var p int +// slice, p = partitionForQuickSort(slice, low, high) +// slice = quickSort(slice, low, p-1) +// slice = quickSort(slice, p+1, high) +// } + +// return slice +// } + +// func partitionForQuickSort[T comparable](slice []T, low, high int) ([]T, int) { +// p := slice[high] +// i := low +// for j := low; j < high; j++ { +// //???, error: comparable don't support operator < +// if slice[j] < p { +// slice[i], slice[j] = slice[j], slice[i] +// i++ +// } +// } +// slice[i], slice[high] = slice[high], slice[i] +// return slice, i +// } + // sliceValue return the reflect value of a slice func sliceValue(slice interface{}) reflect.Value { v := reflect.ValueOf(slice)