Skip to content

Commit

Permalink
update doc and fix typo (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
chen3feng authored Aug 15, 2022
1 parent b28d649 commit 2c70dc0
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 28 deletions.
23 changes: 13 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,26 @@ Different containers support different methods. The following are the methods su
DList and SkipList support simple iterators.

```go
l := stl4go.NewDListOf(Range(1, 10000)...)
sum := 0
for i := 0; i < b.N; i++ {
for it := l.Iterate(); it.IsNotEnd(); it.MoveToNext() {
sum += it.Value()
}
l := stl4go.NewDListOf(Range(1, 10000)...)
sum := 0
for i := 0; i < b.N; i++ {
for it := l.Iterate(); it.IsNotEnd(); it.MoveToNext() {
sum += it.Value()
}
}
```

SkipList also supports interval iteration:
SkipList also supports range iteration:

```go
l := stl4go.NewDListOf(Range(1, 1000)...)
it := sl.FindRange(120, 350)
sl := stl4go.NewSkipList[int, int]()
for i := 0; i < 1000; i++ {
sl.Insert(i, 0)
}
it := sl.FindRange(120, 350)
```

Iterating over `it` yields numbers between 120 and 349.
Iterating over `it` only yields the keys between 120 and 349.

In many cases, it is more convenient to use the `ForEach` and `ForEachIf` methods provided by the container,
and the performance is often better:
Expand Down
21 changes: 12 additions & 9 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,26 @@ import "github.com/chen3feng/stl4go"
DList 和 SkipList 支持简单的迭代器。

```go
l := stl4go.NewDListOf(Range(1, 10000)...)
sum := 0
for i := 0; i < b.N; i++ {
for it := l.Iterate(); it.IsNotEnd(); it.MoveToNext() {
sum += it.Value()
}
l := stl4go.NewDListOf(Range(1, 10000)...)
sum := 0
for i := 0; i < b.N; i++ {
for it := l.Iterate(); it.IsNotEnd(); it.MoveToNext() {
sum += it.Value()
}
}
```

SkipList 还支持区间迭代:

```go
l := stl4go.NewDListOf(Range(1, 1000)...)
it := sl.FindRange(120, 350)
sl := stl4go.NewSkipList[int, int]()
for i := 0; i < 1000; i++ {
sl.Insert(i, 0)
}
it := sl.FindRange(120, 350)
```

`it` 迭代可以得到 120~349 之间的数。
`it` 迭代可以只会得到 120~349 之间的数。

更多时候,使用容器提供的 `ForEach``ForEachIf` 更方便,往往性能也更好一些:

Expand Down
6 changes: 3 additions & 3 deletions lookup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ func Test_MinN(t *testing.T) {
expectEq(t, MinN(2, 1, 3), 1)
expectEq(t, MinN(1, 1, 1), 1)
expectEq(t, MinN("hello", "world"), "hello")
expactPanic(t, func() { MinN(emptyInts...) })
expectPanic(t, func() { MinN(emptyInts...) })
}

func Test_MaxN(t *testing.T) {
expectEq(t, MaxN(1, 2), 2)
expectEq(t, MaxN(2, 1), 2)
expectEq(t, MaxN(2, 2), 2)
expectEq(t, MaxN("hello", "world"), "world")
expactPanic(t, func() { MaxN(emptyInts...) })
expectPanic(t, func() { MaxN(emptyInts...) })
}

func Test_MinMax(t *testing.T) {
Expand All @@ -45,7 +45,7 @@ func Test_MinMaxN(t *testing.T) {
min, max := MinMaxN(3, 4, 1, 2)
expectEq(t, min, 1)
expectEq(t, max, 4)
expactPanic(t, func() { MinMaxN(emptyInts...) })
expectPanic(t, func() { MinMaxN(emptyInts...) })
}

func Test_Find(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions skiplist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ func TestSkipList_newnode(t *testing.T) {
node := newSkipListNode(level, 1, 1)
expectEq(t, len(node.next), level)
}
expactPanic(t, func() { newSkipListNode(0, 1, 1) })
expactPanic(t, func() { newSkipListNode(skipListMaxLevel+1, 1, 1) })
expectPanic(t, func() { newSkipListNode(0, 1, 1) })
expectPanic(t, func() { newSkipListNode(skipListMaxLevel+1, 1, 1) })
}

func TestSkipList_Find(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion stack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,5 @@ func Test_Stack_Must(t *testing.T) {
s.Push(1)
v := s.MustPop()
expectEq(t, v, 1)
expactPanic(t, func() { s.MustPop() })
expectPanic(t, func() { s.MustPop() })
}
2 changes: 1 addition & 1 deletion test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func expectFalse(t *testing.T, actual bool) {
}
}

func expactPanic(t *testing.T, f func()) {
func expectPanic(t *testing.T, f func()) {
t.Helper()
defer func() {
t.Helper()
Expand Down
2 changes: 1 addition & 1 deletion transform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func Test_TransformTo(t *testing.T) {
for i, v := range a {
expectEq(t, b[i], strconv.Itoa(v))
}
expactPanic(t, func() {
expectPanic(t, func() {
c := make([]string, len(a)-1)
TransformTo(a, func(v int) string { return strconv.Itoa(v) }, c)
})
Expand Down
2 changes: 1 addition & 1 deletion vector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func Test_Vector_At_Set(t *testing.T) {
expectEq(t, v[0], 1)
v[0] = 2
expectEq(t, v[0], 2)
expactPanic(t, func() { v.Set(3, 2) })
expectPanic(t, func() { v.Set(3, 2) })
}

func Test_Vector_Insert(t *testing.T) {
Expand Down

0 comments on commit 2c70dc0

Please sign in to comment.