mirror of
https://github.com/duke-git/lancet.git
synced 2026-03-01 00:35:28 +08:00
feat(set, doc): add ToSlice,ToSortedSlice method,fix doc (#189)
This commit is contained in:
@@ -4,19 +4,21 @@
|
|||||||
// Package datastructure contains some data structure. Set is a data container, like slice, but element of set is not duplicate.
|
// Package datastructure contains some data structure. Set is a data container, like slice, but element of set is not duplicate.
|
||||||
package datastructure
|
package datastructure
|
||||||
|
|
||||||
|
import "sort"
|
||||||
|
|
||||||
// Set is a data container, like slice, but element of set is not duplicate.
|
// Set is a data container, like slice, but element of set is not duplicate.
|
||||||
type Set[T comparable] map[T]struct{}
|
type Set[T comparable] map[T]struct{}
|
||||||
|
|
||||||
// New create a instance of set from given values.
|
// New create a instance of set from given values.
|
||||||
func New[T comparable](items ...T) Set[T] {
|
func New[T comparable](items ...T) Set[T] {
|
||||||
set := make(Set[T])
|
set := make(Set[T], len(items))
|
||||||
set.Add(items...)
|
set.Add(items...)
|
||||||
return set
|
return set
|
||||||
}
|
}
|
||||||
|
|
||||||
// FromSlice create a set from given slice.
|
// FromSlice create a set from given slice.
|
||||||
func FromSlice[T comparable](items []T) Set[T] {
|
func FromSlice[T comparable](items []T) Set[T] {
|
||||||
set := make(Set[T])
|
set := make(Set[T], len(items))
|
||||||
for _, item := range items {
|
for _, item := range items {
|
||||||
set.Add(item)
|
set.Add(item)
|
||||||
}
|
}
|
||||||
@@ -77,8 +79,7 @@ func (s Set[T]) ContainAll(other Set[T]) bool {
|
|||||||
|
|
||||||
// Clone return a copy of set
|
// Clone return a copy of set
|
||||||
func (s Set[T]) Clone() Set[T] {
|
func (s Set[T]) Clone() Set[T] {
|
||||||
set := New[T]()
|
set := FromSlice(s.ToSlice())
|
||||||
set.Add(s.Values()...)
|
|
||||||
return set
|
return set
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -116,14 +117,11 @@ func (s Set[T]) Size() int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Values return all values of set
|
// Values return all values of set
|
||||||
|
// Deprecated: Values function is deprecated and will be removed in future versions. Please use ToSlice() function instead.
|
||||||
|
//
|
||||||
|
// The ToSlice() function provides the same functionality as Values and returns a slice containing all values of the set.
|
||||||
func (s Set[T]) Values() []T {
|
func (s Set[T]) Values() []T {
|
||||||
result := make([]T, 0, len(s))
|
return s.ToSlice()
|
||||||
|
|
||||||
s.Iterate(func(value T) {
|
|
||||||
result = append(result, value)
|
|
||||||
})
|
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Union creates a new set contain all element of set s and other
|
// Union creates a new set contain all element of set s and other
|
||||||
@@ -163,7 +161,7 @@ func (s Set[T]) SymmetricDifference(other Set[T]) Set[T] {
|
|||||||
return set
|
return set
|
||||||
}
|
}
|
||||||
|
|
||||||
// Minus creates an set of whose element in origin set but not in compared set
|
// Minus creates a set of whose element in origin set but not in compared set
|
||||||
func (s Set[T]) Minus(comparedSet Set[T]) Set[T] {
|
func (s Set[T]) Minus(comparedSet Set[T]) Set[T] {
|
||||||
set := New[T]()
|
set := New[T]()
|
||||||
|
|
||||||
@@ -197,3 +195,25 @@ func (s Set[T]) Pop() (v T, ok bool) {
|
|||||||
|
|
||||||
return v, false
|
return v, false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ToSlice returns a slice containing all values of the set.
|
||||||
|
func (s Set[T]) ToSlice() []T {
|
||||||
|
if s.IsEmpty() {
|
||||||
|
return []T{}
|
||||||
|
}
|
||||||
|
result := make([]T, 0, s.Size())
|
||||||
|
s.Iterate(func(value T) {
|
||||||
|
result = append(result, value)
|
||||||
|
})
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// ToSortedSlice returns a sorted slice containing all values of the set.
|
||||||
|
func (s Set[T]) ToSortedSlice(less func(v1, v2 T) bool) []T {
|
||||||
|
result := s.ToSlice()
|
||||||
|
sort.Slice(result, func(i, j int) bool {
|
||||||
|
return less(result[i], result[j])
|
||||||
|
})
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package datastructure
|
package datastructure
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"reflect"
|
||||||
|
"sort"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/duke-git/lancet/v2/internal"
|
"github.com/duke-git/lancet/v2/internal"
|
||||||
@@ -260,3 +262,65 @@ func TestEachWithBreak(t *testing.T) {
|
|||||||
// assert.Equal(3, val)
|
// assert.Equal(3, val)
|
||||||
// assert.Equal(true, ok)
|
// assert.Equal(true, ok)
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
func TestSet_ToSlice(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
assert := internal.NewAssert(t, "TestSet_ToSlice")
|
||||||
|
|
||||||
|
set1 := FromSlice([]int{6, 3, 1, 5, 6, 7, 1})
|
||||||
|
set2 := FromSlice([]float64{-2.65, 4.25, 4.25 - 3.14, 0})
|
||||||
|
set3 := New[string]()
|
||||||
|
|
||||||
|
slice1 := set1.ToSlice()
|
||||||
|
slice2 := set2.ToSlice()
|
||||||
|
slice3 := set3.ToSlice()
|
||||||
|
|
||||||
|
sort.Ints(slice1)
|
||||||
|
sort.Float64s(slice2)
|
||||||
|
|
||||||
|
assert.Equal(5, len(slice1))
|
||||||
|
assert.Equal(4, len(slice2))
|
||||||
|
assert.Equal(0, len(slice3))
|
||||||
|
|
||||||
|
assert.Equal(true, reflect.DeepEqual(slice1, []int{1, 3, 5, 6, 7}))
|
||||||
|
assert.Equal(true, reflect.DeepEqual(slice2, []float64{-2.65, 0, 1.11, 4.25}))
|
||||||
|
assert.Equal("[]string", reflect.TypeOf(slice3).String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSet_ToSortedSlice(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
assert := internal.NewAssert(t, "TestSet_ToSortedSlice")
|
||||||
|
|
||||||
|
set1 := FromSlice([]int{6, 3, 1, 5, 6, 7, 1})
|
||||||
|
set2 := FromSlice([]float64{-2.65, 4.25, 4.25 - 3.14, 0})
|
||||||
|
|
||||||
|
type Person struct {
|
||||||
|
Name string
|
||||||
|
Age int
|
||||||
|
}
|
||||||
|
set3 := FromSlice([]Person{{"Tom", 20}, {"Jerry", 18}, {"Spike", 25}})
|
||||||
|
|
||||||
|
slice1 := set1.ToSortedSlice(func(v1, v2 int) bool {
|
||||||
|
return v1 < v2
|
||||||
|
})
|
||||||
|
slice2 := set2.ToSortedSlice(func(v1, v2 float64) bool {
|
||||||
|
return v2 < v1
|
||||||
|
})
|
||||||
|
slice3 := set3.ToSortedSlice(func(v1, v2 Person) bool {
|
||||||
|
return v1.Age < v2.Age
|
||||||
|
})
|
||||||
|
|
||||||
|
assert.Equal(5, len(slice1))
|
||||||
|
assert.Equal(4, len(slice2))
|
||||||
|
assert.Equal(3, len(slice3))
|
||||||
|
|
||||||
|
assert.Equal(true, reflect.DeepEqual(slice1, []int{1, 3, 5, 6, 7}))
|
||||||
|
assert.Equal(true, reflect.DeepEqual(slice2, []float64{4.25, 1.11, 0, -2.65}))
|
||||||
|
assert.Equal(true, reflect.DeepEqual(slice3, []Person{
|
||||||
|
{"Jerry", 18},
|
||||||
|
{"Tom", 20},
|
||||||
|
{"Spike", 25},
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|||||||
@@ -40,6 +40,9 @@ import (
|
|||||||
- [Intersection](#Intersection)
|
- [Intersection](#Intersection)
|
||||||
- [SymmetricDifference](#SymmetricDifference)
|
- [SymmetricDifference](#SymmetricDifference)
|
||||||
- [Minus](#Minus)
|
- [Minus](#Minus)
|
||||||
|
- [Pop](#Pop)
|
||||||
|
- [ToSlice](#ToSlice)
|
||||||
|
- [ToSortedSlice](#ToSortedSlice)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -52,7 +55,7 @@ import (
|
|||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
type Set[T comparable] map[T]bool
|
type Set[T comparable] map[T]struct{}
|
||||||
func New[T comparable](items ...T) Set[T]
|
func New[T comparable](items ...T) Set[T]
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -98,9 +101,10 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### <span id="Values">Values</span>
|
### <span id="Values">Values<sup>deprecated</sup></span>
|
||||||
|
|
||||||
<p>获取集合中所有元素的切片</p>
|
<p>获取集合中所有元素的切片<br>
|
||||||
|
<a href='#ToSlice'>ToSlice()</a> 方法提供与 Values 方法相同的功能</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
@@ -647,3 +651,58 @@ func main() {
|
|||||||
fmt.Println(ok) // true
|
fmt.Println(ok) // true
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="ToSlice">ToSlice</span>
|
||||||
|
|
||||||
|
<p>以切片的形式返回集合中所有的元素(无序)</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (s Set[T]) ToSlice() (v T, ok bool)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func main() {
|
||||||
|
s := set.New(1, 2, 3, 4, 5)
|
||||||
|
|
||||||
|
val := s.ToSlice()
|
||||||
|
fmt.Println(val) // [2 3 4 5 1]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="ToSortedSlice">ToSortedSlice</span>
|
||||||
|
|
||||||
|
<p>以切片的形式返回集合中所有的元素(按给定的规则排序)</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (s Set[T]) ToSortedSlice() (v T, ok bool)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func main() {
|
||||||
|
s1 := set.New(1, 2, 3, 4, 5)
|
||||||
|
type Person struct {
|
||||||
|
Name string
|
||||||
|
Age int
|
||||||
|
}
|
||||||
|
s2 := FromSlice([]Person{{"Tom", 20}, {"Jerry", 18}, {"Spike", 25}})
|
||||||
|
|
||||||
|
res1 := s1.ToSortedSlice(func(v1, v2 int) bool {
|
||||||
|
return v1 < v2
|
||||||
|
})
|
||||||
|
|
||||||
|
res2 := s2.ToSortedSlice(func(v1, v2 Person) bool {
|
||||||
|
return v1.Age < v2.Age
|
||||||
|
})
|
||||||
|
|
||||||
|
fmt.Println(res1) // [1 2 3 4 5]
|
||||||
|
fmt.Println(res2) // [{Jerry 18} {Tom 20} {Spike 25}]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -41,6 +41,9 @@ import (
|
|||||||
- [Intersection](#Intersection)
|
- [Intersection](#Intersection)
|
||||||
- [SymmetricDifference](#SymmetricDifference)
|
- [SymmetricDifference](#SymmetricDifference)
|
||||||
- [Minus](#Minus)
|
- [Minus](#Minus)
|
||||||
|
- [Pop](#Pop)
|
||||||
|
- [ToSlice](#ToSlice)
|
||||||
|
- [ToSortedSlice](#ToSortedSlice)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -53,7 +56,7 @@ import (
|
|||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
type Set[T comparable] map[T]bool
|
type Set[T comparable] map[T]struct{}
|
||||||
func New[T comparable](items ...T) Set[T]
|
func New[T comparable](items ...T) Set[T]
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -99,9 +102,10 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### <span id="Values">Values</span>
|
### <span id="Values">Values<sup>deprecated</sup></span>
|
||||||
|
|
||||||
<p>Return slice of all set data</p>
|
<p>Return slice of all set data.<br>
|
||||||
|
The <a href='#ToSlice'>ToSlice()</a> function provides the same functionality as Values and returns a slice containing all values of the set.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
@@ -648,3 +652,58 @@ func main() {
|
|||||||
fmt.Println(ok) // true
|
fmt.Println(ok) // true
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="ToSlice">ToSlice</span>
|
||||||
|
|
||||||
|
<p>returns a slice containing all values of the set.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (s Set[T]) ToSlice() (v T, ok bool)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func main() {
|
||||||
|
s := set.New(1, 2, 3, 4, 5)
|
||||||
|
|
||||||
|
val := s.ToSlice()
|
||||||
|
fmt.Println(val) // [2 3 4 5 1]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="ToSortedSlice">ToSortedSlice</span>
|
||||||
|
|
||||||
|
<p>returns a sorted slice containing all values of the set</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (s Set[T]) ToSortedSlice() (v T, ok bool)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func main() {
|
||||||
|
s1 := set.New(1, 2, 3, 4, 5)
|
||||||
|
type Person struct {
|
||||||
|
Name string
|
||||||
|
Age int
|
||||||
|
}
|
||||||
|
s2 := FromSlice([]Person{{"Tom", 20}, {"Jerry", 18}, {"Spike", 25}})
|
||||||
|
|
||||||
|
res1 := s1.ToSortedSlice(func(v1, v2 int) bool {
|
||||||
|
return v1 < v2
|
||||||
|
})
|
||||||
|
|
||||||
|
res2 := s2.ToSortedSlice(func(v1, v2 Person) bool {
|
||||||
|
return v1.Age < v2.Age
|
||||||
|
})
|
||||||
|
|
||||||
|
fmt.Println(res1) // [1 2 3 4 5]
|
||||||
|
fmt.Println(res2) // [{Jerry 18} {Tom 20} {Spike 25}]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user