Skip to content

Commit

Permalink
Use clear to zero slice after go1.21
Browse files Browse the repository at this point in the history
  • Loading branch information
chen3feng committed Mar 6, 2024
1 parent 6793044 commit 6dade2e
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 25 deletions.
9 changes: 9 additions & 0 deletions check.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
go build

go test ./... -coverprofile=coverage.out

golint

gosec .

go tool cover -html=coverage.out
34 changes: 17 additions & 17 deletions generated_doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ Fill fills each element in slice a with new value v.
Complexity: O\(len\(a\)\).

<a name="FillPattern"></a>
## func [FillPattern](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L50>)
## func [FillPattern](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L42>)

```go
func FillPattern[T any](a []T, pattern []T)
Expand All @@ -444,13 +444,13 @@ FillPattern fills elements in slice a with specified pattern.
Complexity: O\(len\(a\)\).

<a name="FillZero"></a>
## func [FillZero](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L42>)
## func [FillZero](<https://github.com/chen3feng/stl4go/blob/master/transform_fillzero_clear.go#L9>)

```go
func FillZero[T any](a []T)
```

Fill fills each element in slice a with zero value.
FillZero fills each element in slice a with zero value.

Complexity: O\(len\(a\)\).

Expand Down Expand Up @@ -750,7 +750,7 @@ Range make a \[\]T filled with values in the \`\[first, last\)\` sequence. NOTE:
Complexity: O\(last\-first\).

<a name="Remove"></a>
## func [Remove](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L162>)
## func [Remove](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L154>)

```go
func Remove[T comparable](a []T, x T) []T
Expand All @@ -761,7 +761,7 @@ Remove remove the elements which equals to x from the input slice. return the pr
Complexity: O\(len\(a\)\).

<a name="RemoveCopy"></a>
## func [RemoveCopy](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L177>)
## func [RemoveCopy](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L169>)

```go
func RemoveCopy[T comparable](a []T, x T) []T
Expand All @@ -783,7 +783,7 @@ RemoveHeapFunc removes and returns the element at index i from the heap.
Complexity: is O\(log\(n\)\) where n = len\(\*heap\).

<a name="RemoveIf"></a>
## func [RemoveIf](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L191>)
## func [RemoveIf](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L183>)

```go
func RemoveIf[T any](a []T, cond func(T) bool) []T
Expand All @@ -794,7 +794,7 @@ RemoveIf remove each element which make cond\(x\) returns true from the input sl
Complexity: O\(len\(a\)\).

<a name="RemoveIfCopy"></a>
## func [RemoveIfCopy](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L206>)
## func [RemoveIfCopy](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L198>)

```go
func RemoveIfCopy[T any](a []T, cond func(T) bool) []T
Expand All @@ -816,7 +816,7 @@ RemoveMinHeap removes and returns the element at index i from the min heap.
Complexity: is O\(log\(n\)\) where n = len\(\*heap\).

<a name="Replace"></a>
## func [Replace](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L99>)
## func [Replace](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L91>)

```go
func Replace[T comparable](a []T, old, new T)
Expand All @@ -827,7 +827,7 @@ Replace replaces every element that equals to old with new.
Complexity: O\(len\(a\)\).

<a name="ReplaceIf"></a>
## func [ReplaceIf](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L110>)
## func [ReplaceIf](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L102>)

```go
func ReplaceIf[T any](a []T, pred func(v T) bool, new T)
Expand All @@ -838,7 +838,7 @@ ReplaceIf replaces every element that make preq returns true with new.
Complexity: O\(len\(a\)\).

<a name="Reverse"></a>
## func [Reverse](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L228>)
## func [Reverse](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L220>)

```go
func Reverse[T any](a []T)
Expand All @@ -849,7 +849,7 @@ Reverse reverses the order of the elements in the slice a.
Complexity: O\(len\(a\)\).

<a name="ReverseCopy"></a>
## func [ReverseCopy](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L237>)
## func [ReverseCopy](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L229>)

```go
func ReverseCopy[T any](a []T) []T
Expand All @@ -860,7 +860,7 @@ ReverseCopy returns a reversed copy of slice a.
Complexity: O\(len\(a\)\).

<a name="Shuffle"></a>
## func [Shuffle](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L219>)
## func [Shuffle](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L211>)

```go
func Shuffle[T any](a []T)
Expand Down Expand Up @@ -933,7 +933,7 @@ func SumAs[R, T Numeric](a []T) R
SumAs summarize all elements in a. returns the result as type R, this is useful when T is too small to hold the result. Complexity: O\(len\(a\)\).

<a name="Transform"></a>
## func [Transform](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L79>)
## func [Transform](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L71>)

```go
func Transform[T any](a []T, op func(T) T)
Expand All @@ -944,7 +944,7 @@ Transform applies the function op to each element in slice a and set it back to
Complexity: O\(len\(a\)\).

<a name="TransformCopy"></a>
## func [TransformCopy](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L88>)
## func [TransformCopy](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L80>)

```go
func TransformCopy[R any, T any](a []T, op func(T) R) []R
Expand All @@ -955,7 +955,7 @@ TransformCopy applies the function op to each element in slice a and return all
Complexity: O\(len\(a\)\).

<a name="TransformTo"></a>
## func [TransformTo](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L68>)
## func [TransformTo](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L60>)

```go
func TransformTo[R any, T any](a []T, op func(T) R, b []R) []R
Expand All @@ -966,7 +966,7 @@ TransformTo applies the function op to each element in slice a and fill it to sl
Complexity: O\(len\(a\)\).

<a name="Unique"></a>
## func [Unique](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L122>)
## func [Unique](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L114>)

```go
func Unique[T comparable](a []T) []T
Expand All @@ -977,7 +977,7 @@ Unique remove adjacent repeated elements from the input slice. return the proces
Complexity: O\(len\(a\)\).

<a name="UniqueCopy"></a>
## func [UniqueCopy](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L142>)
## func [UniqueCopy](<https://github.com/chen3feng/stl4go/blob/master/transform.go#L134>)

```go
func UniqueCopy[T comparable](a []T) []T
Expand Down
8 changes: 0 additions & 8 deletions transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,6 @@ func Fill[T any](a []T, v T) {
}
}

// FillZero fills each element in slice a with zero value.
//
// Complexity: O(len(a)).
func FillZero[T any](a []T) {
var zero T
Fill(a, zero)
}

// FillPattern fills elements in slice a with specified pattern.
//
// Complexity: O(len(a)).
Expand Down
11 changes: 11 additions & 0 deletions transform_fillzero_clear.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//go:build go1.21
// +build go1.21

package stl4go

// FillZero fills each element in slice a with zero value.
//
// Complexity: O(len(a)).
func FillZero[T any](a []T) {
clear(a)
}
12 changes: 12 additions & 0 deletions transform_fillzero_old.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//go:build !go1.21
// +build !go1.21

package stl4go

// FillZero fills each element in slice a with zero value.
//
// Complexity: O(len(a)).
func FillZero[T any](a []T) {
var zero T
Fill(a, zero)
}
3 changes: 3 additions & 0 deletions updatedoc.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@echo off

gomarkdoc -o generated_doc.md -e .

0 comments on commit 6dade2e

Please sign in to comment.