Skip to content

Commit

Permalink
Merge bcaed85 into afba102
Browse files Browse the repository at this point in the history
  • Loading branch information
calvinit committed Apr 18, 2024
2 parents afba102 + bcaed85 commit 14bf6e4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions generated_doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ Package stl4go is a generic container and algorithm library for go.
- [func \(v \*Vector\[T\]\) PopBack\(\) T](<#Vector[T].PopBack>)
- [func \(v \*Vector\[T\]\) PushBack\(x T\)](<#Vector[T].PushBack>)
- [func \(v \*Vector\[T\]\) Remove\(i int\)](<#Vector[T].Remove>)
- [func \(v \*Vector\[T\]\) RemoveIf\(cond func\(T\) bool\)](<#Vector[T].RemoveIf>)
- [func \(v \*Vector\[T\]\) RemoveLength\(i int, len int\)](<#Vector[T].RemoveLength>)
- [func \(v \*Vector\[T\]\) RemoveRange\(i, j int\)](<#Vector[T].RemoveRange>)
- [func \(v \*Vector\[T\]\) Reserve\(l int\)](<#Vector[T].Reserve>)
Expand Down
7 changes: 7 additions & 0 deletions vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,13 @@ func (v *Vector[T]) RemoveLength(i int, len int) {
v.RemoveRange(i, i+len)
}

// RemoveIf removes the elements which make cond(x) returns true from the vector.
func (v *Vector[T]) RemoveIf(cond func(T) bool) {
oldV := *v
*v = RemoveIf(*v, cond)
FillZero(oldV[v.Len():])
}

// ForEach iterate the container, apply each element to the cb callback function.
func (v Vector[T]) ForEach(cb func(val T)) {
for _, e := range v {
Expand Down
12 changes: 12 additions & 0 deletions vector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,18 @@ func Test_Vector_RemoveLength(t *testing.T) {
expectEq(t, oldV[3], 0)
}

func Test_Vector_RemoveIf(t *testing.T) {
v := VectorOf(1, 2, 3, 4)
oldV := v
v.RemoveIf(func(i int) bool { return i%2 == 0 })
expectEq(t, v.Len(), 2)
expectEq(t, v.Cap(), 4)
expectEq(t, v[0], 1)
expectEq(t, v[1], 3)
expectEq(t, oldV[2], 0)
expectEq(t, oldV[3], 0)
}

func Test_Vector_ForEach(t *testing.T) {
a := []int{1, 2, 3}
v := VectorOf(a...)
Expand Down

0 comments on commit 14bf6e4

Please sign in to comment.