Skip to content

Commit

Permalink
add ForEachxxx to vector
Browse files Browse the repository at this point in the history
  • Loading branch information
chen3feng committed Sep 1, 2022
1 parent 94f6b8f commit af61348
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 2 deletions.
40 changes: 38 additions & 2 deletions generated_doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ Package stl4go is a generic container and algorithm library for go.
- [func (v *Vector[T]) At(i int) T](<#func-vectort-at>)
- [func (v *Vector[T]) Cap() int](<#func-vectort-cap>)
- [func (v *Vector[T]) Clear()](<#func-vectort-clear>)
- [func (v Vector[T]) ForEach(cb func(val T))](<#func-vectort-foreach>)
- [func (v Vector[T]) ForEachIf(cb func(val T) bool)](<#func-vectort-foreachif>)
- [func (v Vector[T]) ForEachMutable(cb func(val *T))](<#func-vectort-foreachmutable>)
- [func (v Vector[T]) ForEachMutableIf(cb func(val *T) bool)](<#func-vectort-foreachmutableif>)
- [func (v *Vector[T]) Insert(i int, x ...T)](<#func-vectort-insert>)
- [func (v *Vector[T]) IsEmpty() bool](<#func-vectort-isempty>)
- [func (v Vector[T]) Iterate() MutableIterator[T]](<#func-vectort-iterate>)
Expand Down Expand Up @@ -1734,6 +1738,38 @@ func (v *Vector[T]) Clear()

Clear erases all elements from the vector. After this call, Len\(\) returns zero. Leaves the Cap\(\) of the vector unchanged.

### func \(Vector\[T\]\) [ForEach](<https://github.com/chen3feng/stl4go/blob/master/vector.go#L126>)

```go
func (v Vector[T]) ForEach(cb func(val T))
```

ForEach iterate the list, apply each element to the cb callback function.

### func \(Vector\[T\]\) [ForEachIf](<https://github.com/chen3feng/stl4go/blob/master/vector.go#L133>)

```go
func (v Vector[T]) ForEachIf(cb func(val T) bool)
```

ForEachIf iterate the list, apply each element to the cb callback function, stop if cb returns false.

### func \(Vector\[T\]\) [ForEachMutable](<https://github.com/chen3feng/stl4go/blob/master/vector.go#L142>)

```go
func (v Vector[T]) ForEachMutable(cb func(val *T))
```

ForEachMutable iterate the list, apply pointer of each element to the cb callback function.

### func \(Vector\[T\]\) [ForEachMutableIf](<https://github.com/chen3feng/stl4go/blob/master/vector.go#L149>)

```go
func (v Vector[T]) ForEachMutableIf(cb func(val *T) bool)
```

ForEachMutableIf iterate the list, apply pointer of each element to the cb callback function, stop if cb returns false.

### func \(\*Vector\[T\]\) [Insert](<https://github.com/chen3feng/stl4go/blob/master/vector.go#L91>)

```go
Expand All @@ -1752,15 +1788,15 @@ func (v *Vector[T]) IsEmpty() bool

IsEmpty implements the Container interface.

### func \(Vector\[T\]\) [Iterate](<https://github.com/chen3feng/stl4go/blob/master/vector.go#L126>)
### func \(Vector\[T\]\) [Iterate](<https://github.com/chen3feng/stl4go/blob/master/vector.go#L158>)

```go
func (v Vector[T]) Iterate() MutableIterator[T]
```

Iterate returns an iterator to the whole vector.

### func \(Vector\[T\]\) [IterateRange](<https://github.com/chen3feng/stl4go/blob/master/vector.go#L131>)
### func \(Vector\[T\]\) [IterateRange](<https://github.com/chen3feng/stl4go/blob/master/vector.go#L163>)

```go
func (v Vector[T]) IterateRange(i, j int) MutableIterator[T]
Expand Down
32 changes: 32 additions & 0 deletions vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,38 @@ func (v *Vector[T]) RemoveLength(i int, len int) {
v.RemoveRange(i, i+len)
}

// ForEach iterate the list, apply each element to the cb callback function.
func (v Vector[T]) ForEach(cb func(val T)) {
for _, e := range v {
cb(e)
}
}

// ForEachIf iterate the list, apply each element to the cb callback function, stop if cb returns false.
func (v Vector[T]) ForEachIf(cb func(val T) bool) {
for _, e := range v {
if !cb(e) {
break
}
}
}

// ForEachMutable iterate the list, apply pointer of each element to the cb callback function.
func (v Vector[T]) ForEachMutable(cb func(val *T)) {
for i := range v {
cb(&v[i])
}
}

// ForEachMutableIf iterate the list, apply pointer of each element to the cb callback function, stop if cb returns false.
func (v Vector[T]) ForEachMutableIf(cb func(val *T) bool) {
for i := range v {
if !cb(&v[i]) {
break
}
}
}

// Iterate returns an iterator to the whole vector.
func (v Vector[T]) Iterate() MutableIterator[T] {
return &vectorIterator[T]{v, 0}
Expand Down
43 changes: 43 additions & 0 deletions vector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,49 @@ func Test_Vector_RemoveLength(t *testing.T) {
expectEq(t, v[1], 4)
}

func Test_Vector_ForEach(t *testing.T) {
a := []int{1, 2, 3}
v := MakeVectorOf(a...)
var b []int
v.ForEach(func(n int) {
b = append(b, n)
})
expectEq(t, len(b), 3)
expectTrue(t, Equal(a, b))
}

func Test_Vector_ForEachIf(t *testing.T) {
v := MakeVectorOf(1, 2, 3)
c := 0
v.ForEachIf(func(n int) bool {
c = n
return n != 2
})
expectEq(t, c, 2)
}

func Test_Vector_ForEachMutable(t *testing.T) {
a := []int{1, 2, 3}
v := MakeVectorOf(1, 2, 3)
v.ForEachMutable(func(n *int) {
*n = -*n
})
expectEq(t, v.Len(), 3)
for i := range v {
expectEq(t, a[i], -v[i])
}
}

func Test_Vector_ForEachMutableIf(t *testing.T) {
v := MakeVectorOf(1, 2, 3)
c := 0
v.ForEachMutableIf(func(n *int) bool {
c = *n
return *n != 2
})
expectEq(t, c, 2)
}

func Test_Vector_Iterate(t *testing.T) {
v := MakeVectorOf(1, 2, 3, 4)
i := 1
Expand Down

0 comments on commit af61348

Please sign in to comment.