mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
feat: add func ContainSubSlice
This commit is contained in:
6
.github/workflows/codecov.yml
vendored
6
.github/workflows/codecov.yml
vendored
@@ -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
|
||||
|
||||
@@ -6,9 +6,10 @@
|
||||
<div align="center" style="text-align: center;">
|
||||
|
||||

|
||||
[](https://github.com/duke-git/lancet/releases)
|
||||
[](https://github.com/duke-git/lancet/releases)
|
||||
[](https://pkg.go.dev/github.com/duke-git/lancet)
|
||||
[](https://goreportcard.com/report/github.com/duke-git/lancet)
|
||||
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
||||
[](https://codecov.io/gh/duke-git/lancet)
|
||||
[](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
|
||||
|
||||
@@ -6,9 +6,10 @@
|
||||
<div align="center" style="text-align: center;">
|
||||
|
||||

|
||||
[](https://github.com/duke-git/lancet/releases)
|
||||
[](https://github.com/duke-git/lancet/releases)
|
||||
[](https://pkg.go.dev/github.com/duke-git/lancet)
|
||||
[](https://goreportcard.com/report/github.com/duke-git/lancet)
|
||||
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
||||
[](https://codecov.io/gh/duke-git/lancet)
|
||||
[](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中
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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")
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user