Skip to content

Commit

Permalink
feat: add func ContainSubSlice
Browse files Browse the repository at this point in the history
  • Loading branch information
duke-git committed Jan 13, 2022
2 parents c6fc92a + e31fb28 commit d46d12f
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 5 deletions.
6 changes: 3 additions & 3 deletions .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
Expand Down
4 changes: 3 additions & 1 deletion README.md
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion README_zh-CN.md
Expand Up @@ -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)

Expand Down Expand Up @@ -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中
Expand Down
15 changes: 15 additions & 0 deletions slice/slice.go
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions slice/slice_test.go
Expand Up @@ -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")

Expand Down
25 changes: 25 additions & 0 deletions slice/slice_util.go
Expand Up @@ -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)
Expand Down

0 comments on commit d46d12f

Please sign in to comment.