Skip to content

Commit

Permalink
add iterator to vector (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
chen3feng committed Aug 30, 2022
1 parent eb977b3 commit 94f6b8f
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 3 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Different containers support different methods. The following are the methods su

### Iterators

DList and SkipList support iterators.
Vector, DList and SkipList support iterators.

```go
// Iterator is the interface for container's iterator.
Expand Down Expand Up @@ -162,7 +162,8 @@ func TestSkipList_ForEachMutable(t *testing.T) {

### Algorithms

Due to the limitations of language, most algorithms only support Slice. The functions name of the algorithms ends with `If`, `Func`,
Due to the limitations of language, most algorithms only support Slice.
The functions name of the algorithms ends with `If` or `Func`,
indicating that a custom comparison function can be passed.

#### Generate
Expand Down
2 changes: 1 addition & 1 deletion README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import "github.com/chen3feng/stl4go"

### 迭代器

DList 和 SkipList 支持迭代器。
Vector、DList 和 SkipList 支持迭代器。

```go
// Iterator is the interface for container's iterator.
Expand Down
18 changes: 18 additions & 0 deletions generated_doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ Package stl4go is a generic container and algorithm library for go.
- [func (v *Vector[T]) Clear()](<#func-vectort-clear>)
- [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>)
- [func (v Vector[T]) IterateRange(i, j int) MutableIterator[T]](<#func-vectort-iteraterange>)
- [func (v *Vector[T]) Len() int](<#func-vectort-len>)
- [func (v *Vector[T]) PushBack(x T)](<#func-vectort-pushback>)
- [func (v *Vector[T]) Remove(i int)](<#func-vectort-remove>)
Expand Down Expand Up @@ -1750,6 +1752,22 @@ 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>)

```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>)

```go
func (v Vector[T]) IterateRange(i, j int) MutableIterator[T]
```

IterateRange returns an iterator to the range \[i, j\) of the vector.

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

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

// Iterate returns an iterator to the whole vector.
func (v Vector[T]) Iterate() MutableIterator[T] {
return &vectorIterator[T]{v, 0}
}

// IterateRange returns an iterator to the range [i, j) of the vector.
func (v Vector[T]) IterateRange(i, j int) MutableIterator[T] {
return &vectorIterator[T]{v[i:j], 0}
}

type vectorIterator[T any] struct {
v Vector[T]
i int
}

func (it vectorIterator[T]) Value() T {
return it.v[it.i]
}

func (it vectorIterator[T]) Pointer() *T {
return &it.v[it.i]
}

func (it vectorIterator[T]) IsNotEnd() bool {
return it.i < len(it.v)
}

func (it *vectorIterator[T]) MoveToNext() {
it.i++
}
22 changes: 22 additions & 0 deletions vector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,25 @@ func Test_Vector_RemoveLength(t *testing.T) {
expectEq(t, v[0], 1)
expectEq(t, v[1], 4)
}

func Test_Vector_Iterate(t *testing.T) {
v := MakeVectorOf(1, 2, 3, 4)
i := 1
for it := v.Iterate(); it.IsNotEnd(); it.MoveToNext() {
expectEq(t, it.Value(), i)
expectEq(t, *it.Pointer(), it.Value())
i++
}
expectEq(t, i, 5)
}

func Test_Vector_IterateRange(t *testing.T) {
v := MakeVectorOf(1, 2, 3, 4)
i := 2
for it := v.IterateRange(1, 3); it.IsNotEnd(); it.MoveToNext() {
expectEq(t, it.Value(), i)
expectEq(t, *it.Pointer(), it.Value())
i++
}
expectEq(t, i, 4)
}

0 comments on commit 94f6b8f

Please sign in to comment.