1
0
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:
dudaodong
2022-01-13 11:52:21 +08:00
6 changed files with 57 additions and 5 deletions

View File

@@ -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

View File

@@ -6,9 +6,10 @@
<div align="center" style="text-align: center;">
![Go version](https://img.shields.io/badge/go-%3E%3D1.16<recommend>-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

View File

@@ -6,9 +6,10 @@
<div align="center" style="text-align: center;">
![Go version](https://img.shields.io/badge/go-%3E%3D1.16<recommend>-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中

View File

@@ -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

View File

@@ -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")

View File

@@ -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)