From 3df7711e74d0ad95033df209f5c2e9cab982553b Mon Sep 17 00:00:00 2001 From: mingzaily Date: Tue, 2 Nov 2021 14:31:34 +0800 Subject: [PATCH] complete example for glist --- container/glist/glist.go | 2 +- container/glist/glist_example_test.go | 160 ------ container/glist/glist_z_example_test.go | 689 ++++++++++++++++++++++++ 3 files changed, 690 insertions(+), 161 deletions(-) delete mode 100644 container/glist/glist_example_test.go create mode 100644 container/glist/glist_z_example_test.go diff --git a/container/glist/glist.go b/container/glist/glist.go index ee6baa3d7..03bfa261b 100644 --- a/container/glist/glist.go +++ b/container/glist/glist.go @@ -416,7 +416,7 @@ func (l *List) RemoveAll() { l.mu.Unlock() } -// See RemoveAll(). +// Clear is alias of RemoveAll. func (l *List) Clear() { l.RemoveAll() } diff --git a/container/glist/glist_example_test.go b/container/glist/glist_example_test.go deleted file mode 100644 index 3cd2880d3..000000000 --- a/container/glist/glist_example_test.go +++ /dev/null @@ -1,160 +0,0 @@ -// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. -// -// This Source Code Form is subject to the terms of the MIT License. -// If a copy of the MIT was not distributed with this file, -// You can obtain one at https://github.com/gogf/gf. - -package glist_test - -import ( - "container/list" - "fmt" - "github.com/gogf/gf/v2/container/garray" - "github.com/gogf/gf/v2/frame/g" - - "github.com/gogf/gf/v2/container/glist" -) - -func ExampleNew() { - n := 10 - l := glist.New() - for i := 0; i < n; i++ { - l.PushBack(i) - } - fmt.Println(l.Len()) - fmt.Println(l.FrontAll()) - fmt.Println(l.BackAll()) - for i := 0; i < n; i++ { - fmt.Print(l.PopFront()) - } - l.Clear() - fmt.Println() - fmt.Println(l.Len()) - - // Output: - // 10 - // [0 1 2 3 4 5 6 7 8 9] - // [9 8 7 6 5 4 3 2 1 0] - // 0123456789 - // 0 -} - -func ExampleList_RLockFunc() { - // concurrent-safe list. - l := glist.NewFrom(garray.NewArrayRange(1, 10, 1).Slice(), true) - // iterate reading from head. - l.RLockFunc(func(list *list.List) { - length := list.Len() - if length > 0 { - for i, e := 0, list.Front(); i < length; i, e = i+1, e.Next() { - fmt.Print(e.Value) - } - } - }) - fmt.Println() - // iterate reading from tail. - l.RLockFunc(func(list *list.List) { - length := list.Len() - if length > 0 { - for i, e := 0, list.Back(); i < length; i, e = i+1, e.Prev() { - fmt.Print(e.Value) - } - } - }) - - fmt.Println() - // Output: - // 12345678910 - // 10987654321 -} - -func ExampleList_IteratorAsc() { - // concurrent-safe list. - l := glist.NewFrom(garray.NewArrayRange(1, 10, 1).Slice(), true) - // iterate reading from head using IteratorAsc. - l.IteratorAsc(func(e *glist.Element) bool { - fmt.Print(e.Value) - return true - }) - - // Output: - // 12345678910 -} - -func ExampleList_IteratorDesc() { - // concurrent-safe list. - l := glist.NewFrom(garray.NewArrayRange(1, 10, 1).Slice(), true) - // iterate reading from tail using IteratorDesc. - l.IteratorDesc(func(e *glist.Element) bool { - fmt.Print(e.Value) - return true - }) - // Output: - // 10987654321 -} - -func ExampleList_LockFunc() { - // concurrent-safe list. - l := glist.NewFrom(garray.NewArrayRange(1, 10, 1).Slice(), true) - // iterate writing from head. - l.LockFunc(func(list *list.List) { - length := list.Len() - if length > 0 { - for i, e := 0, list.Front(); i < length; i, e = i+1, e.Next() { - if e.Value == 6 { - e.Value = "M" - break - } - } - } - }) - fmt.Println(l) - - // Output: - // [1,2,3,4,5,M,7,8,9,10] -} - -func ExampleList_PopBack() { - l := glist.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9}) - - fmt.Println(l.PopBack()) - - // Output: - // 9 -} -func ExampleList_PopBacks() { - l := glist.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9}) - - fmt.Println(l.PopBacks(2)) - - // Output: - // [9 8] -} - -func ExampleList_PopFront() { - l := glist.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9}) - - fmt.Println(l.PopFront()) - - // Output: - // 1 -} - -func ExampleList_PopFronts() { - l := glist.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9}) - - fmt.Println(l.PopFronts(2)) - - // Output: - // [1 2] -} - -func ExampleList_Join() { - var l glist.List - l.PushBacks(g.Slice{"a", "b", "c", "d"}) - - fmt.Println(l.Join(",")) - - // Output: - // a,b,c,d -} diff --git a/container/glist/glist_z_example_test.go b/container/glist/glist_z_example_test.go new file mode 100644 index 000000000..54126b760 --- /dev/null +++ b/container/glist/glist_z_example_test.go @@ -0,0 +1,689 @@ +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package glist_test + +import ( + "container/list" + "fmt" + "github.com/gogf/gf/v2/container/garray" + "github.com/gogf/gf/v2/frame/g" + + "github.com/gogf/gf/v2/container/glist" +) + +func ExampleNew() { + n := 10 + l := glist.New() + for i := 0; i < n; i++ { + l.PushBack(i) + } + + fmt.Println(l.Len()) + fmt.Println(l) + fmt.Println(l.FrontAll()) + fmt.Println(l.BackAll()) + + for i := 0; i < n; i++ { + fmt.Print(l.PopFront()) + } + + fmt.Println() + fmt.Println(l.Len()) + + // Output: + // 10 + // [0,1,2,3,4,5,6,7,8,9] + // [0 1 2 3 4 5 6 7 8 9] + // [9 8 7 6 5 4 3 2 1 0] + // 0123456789 + // 0 +} + +func ExampleNewFrom() { + n := 10 + l := glist.NewFrom(garray.NewArrayRange(1, 10, 1).Slice()) + + fmt.Println(l.Len()) + fmt.Println(l) + fmt.Println(l.FrontAll()) + fmt.Println(l.BackAll()) + + for i := 0; i < n; i++ { + fmt.Print(l.PopFront()) + } + + fmt.Println() + fmt.Println(l.Len()) + + // Output: + // 10 + // [1,2,3,4,5,6,7,8,9,10] + // [1 2 3 4 5 6 7 8 9 10] + // [10 9 8 7 6 5 4 3 2 1] + // 12345678910 + // 0 +} + +func ExampleList_PushFront() { + l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) + + fmt.Println(l.Len()) + fmt.Println(l) + + l.PushFront(0) + + fmt.Println(l.Len()) + fmt.Println(l) + + // Output: + // 5 + // [1,2,3,4,5] + // 6 + // [0,1,2,3,4,5] +} + +func ExampleList_PushBack() { + l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) + + fmt.Println(l.Len()) + fmt.Println(l) + + l.PushBack(6) + + fmt.Println(l.Len()) + fmt.Println(l) + + // Output: + // 5 + // [1,2,3,4,5] + // 6 + // [1,2,3,4,5,6] +} + +func ExampleList_PushFronts() { + l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) + + fmt.Println(l.Len()) + fmt.Println(l) + + l.PushFronts(g.Slice{0, -1, -2, -3, -4}) + + fmt.Println(l.Len()) + fmt.Println(l) + + // Output: + // 5 + // [1,2,3,4,5] + // 10 + // [-4,-3,-2,-1,0,1,2,3,4,5] +} + +func ExampleList_PushBacks() { + l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) + + fmt.Println(l.Len()) + fmt.Println(l) + + l.PushBacks(g.Slice{6, 7, 8, 9, 10}) + + fmt.Println(l.Len()) + fmt.Println(l) + + // Output: + // 5 + // [1,2,3,4,5] + // 10 + // [1,2,3,4,5,6,7,8,9,10] +} + +func ExampleList_PopBack() { + l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) + + fmt.Println(l.Len()) + fmt.Println(l) + fmt.Println(l.PopBack()) + fmt.Println(l.Len()) + fmt.Println(l) + + // Output: + // 5 + // [1,2,3,4,5] + // 5 + // 4 + // [1,2,3,4] +} + +func ExampleList_PopFront() { + l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) + + fmt.Println(l.Len()) + fmt.Println(l) + fmt.Println(l.PopFront()) + fmt.Println(l.Len()) + fmt.Println(l) + + // Output: + // 5 + // [1,2,3,4,5] + // 1 + // 4 + // [2,3,4,5] +} + +func ExampleList_PopBacks() { + l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) + + fmt.Println(l.Len()) + fmt.Println(l) + fmt.Println(l.PopBacks(2)) + fmt.Println(l.Len()) + fmt.Println(l) + + // Output: + // 5 + // [1,2,3,4,5] + // [5 4] + // 3 + // [1,2,3] +} + +func ExampleList_PopFronts() { + l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) + + fmt.Println(l.Len()) + fmt.Println(l) + fmt.Println(l.PopFronts(2)) + fmt.Println(l.Len()) + fmt.Println(l) + + // Output: + // 5 + // [1,2,3,4,5] + // [1 2] + // 3 + // [3,4,5] +} + +func ExampleList_PopBackAll() { + l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) + + fmt.Println(l.Len()) + fmt.Println(l) + fmt.Println(l.PopBackAll()) + fmt.Println(l.Len()) + + // Output: + // 5 + // [1,2,3,4,5] + // [5 4 3 2 1] + // 0 +} + +func ExampleList_PopFrontAll() { + l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) + + fmt.Println(l.Len()) + fmt.Println(l) + fmt.Println(l.PopFrontAll()) + fmt.Println(l.Len()) + + // Output: + // 5 + // [1,2,3,4,5] + // [1 2 3 4 5] + // 0 +} + +func ExampleList_FrontAll() { + l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) + + fmt.Println(l) + fmt.Println(l.FrontAll()) + + // Output: + // [1,2,3,4,5] + // [1 2 3 4 5] +} + +func ExampleList_BackAll() { + l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) + + fmt.Println(l) + fmt.Println(l.BackAll()) + + // Output: + // [1,2,3,4,5] + // [5 4 3 2 1] +} + +func ExampleList_FrontValue() { + l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) + + fmt.Println(l) + fmt.Println(l.FrontValue()) + + // Output: + // [1,2,3,4,5] + // 1 +} + +func ExampleList_BackValue() { + l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) + + fmt.Println(l) + fmt.Println(l.BackValue()) + + // Output: + // [1,2,3,4,5] + // 5 +} + +func ExampleList_Front() { + l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) + + fmt.Println(l.Front().Value) + fmt.Println(l) + + e := l.Front() + l.InsertBefore(e, 0) + l.InsertAfter(e, "a") + + fmt.Println(l) + + // Output: + // 1 + // [1,2,3,4,5] + // [0,1,a,2,3,4,5] +} + +func ExampleList_Back() { + l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) + + fmt.Println(l.Back().Value) + fmt.Println(l) + + e := l.Back() + l.InsertBefore(e, "a") + l.InsertAfter(e, 6) + + fmt.Println(l) + + // Output: + // 5 + // [1,2,3,4,5] + // [1,2,3,4,a,5,6] +} + +func ExampleList_Len() { + l := glist.NewFrom(g.Slice{1, 2, 3, 4, 5}) + + fmt.Println(l.Len()) + + // Output: + // 5 +} + +func ExampleList_Size() { + l := glist.NewFrom(g.Slice{1, 2, 3, 4, 5}) + + fmt.Println(l.Size()) + + // Output: + // 5 +} + +func ExampleList_MoveBefore() { + l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) + + fmt.Println(l.Size()) + fmt.Println(l) + + // element of `l` + e := l.PushBack(6) + fmt.Println(l.Size()) + fmt.Println(l) + + l.MoveBefore(e, l.Front()) + + fmt.Println(l.Size()) + fmt.Println(l) + + // not element of `l` + e = &glist.Element{Value: 7} + l.MoveBefore(e, l.Front()) + + fmt.Println(l.Size()) + fmt.Println(l) + + // Output: + // 5 + // [1,2,3,4,5] + // 6 + // [1,2,3,4,5,6] + // 6 + // [6,1,2,3,4,5] + // 6 + // [6,1,2,3,4,5] +} + +func ExampleList_MoveAfter() { + l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) + + fmt.Println(l.Size()) + fmt.Println(l) + + // element of `l` + e := l.PushFront(0) + fmt.Println(l.Size()) + fmt.Println(l) + + l.MoveAfter(e, l.Back()) + + fmt.Println(l.Size()) + fmt.Println(l) + + // not element of `l` + e = &glist.Element{Value: -1} + l.MoveAfter(e, l.Back()) + + fmt.Println(l.Size()) + fmt.Println(l) + + // Output: + // 5 + // [1,2,3,4,5] + // 6 + // [0,1,2,3,4,5] + // 6 + // [1,2,3,4,5,0] + // 6 + // [1,2,3,4,5,0] +} + +func ExampleList_MoveToFront() { + l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) + + fmt.Println(l.Size()) + fmt.Println(l) + + // element of `l` + l.MoveToFront(l.Back()) + + fmt.Println(l.Size()) + fmt.Println(l) + + // not element of `l` + e := &glist.Element{Value: 6} + l.MoveToFront(e) + + fmt.Println(l.Size()) + fmt.Println(l) + + // Output: + // 5 + // [1,2,3,4,5] + // 5 + // [5,1,2,3,4] + // 5 + // [5,1,2,3,4] +} + +func ExampleList_MoveToBack() { + l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) + + fmt.Println(l.Size()) + fmt.Println(l) + + // element of `l` + l.MoveToBack(l.Front()) + + fmt.Println(l.Size()) + fmt.Println(l) + + // not element of `l` + e := &glist.Element{Value: 0} + l.MoveToBack(e) + + fmt.Println(l.Size()) + fmt.Println(l) + + // Output: + // 5 + // [1,2,3,4,5] + // 5 + // [2,3,4,5,1] + // 5 + // [2,3,4,5,1] +} + +func ExampleList_PushBackList() { + l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) + + fmt.Println(l.Size()) + fmt.Println(l) + + other := glist.NewFrom(g.Slice{6, 7, 8, 9, 10}) + + fmt.Println(other.Size()) + fmt.Println(other) + + l.PushBackList(other) + + fmt.Println(l.Size()) + fmt.Println(l) + + // Output: + // 5 + // [1,2,3,4,5] + // 5 + // [6,7,8,9,10] + // 10 + // [1,2,3,4,5,6,7,8,9,10] +} + +func ExampleList_PushFrontList() { + l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) + + fmt.Println(l.Size()) + fmt.Println(l) + + other := glist.NewFrom(g.Slice{-4, -3, -2, -1, 0}) + + fmt.Println(other.Size()) + fmt.Println(other) + + l.PushFrontList(other) + + fmt.Println(l.Size()) + fmt.Println(l) + + // Output: + // 5 + // [1,2,3,4,5] + // 5 + // [-4,-3,-2,-1,0] + // 10 + // [-4,-3,-2,-1,0,1,2,3,4,5] +} + +func ExampleList_InsertAfter() { + l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) + + fmt.Println(l.Len()) + fmt.Println(l) + + l.InsertAfter(l.Front(), "a") + l.InsertAfter(l.Back(), "b") + + fmt.Println(l.Len()) + fmt.Println(l) + + // Output: + // 5 + // [1,2,3,4,5] + // 7 + // [1,a,2,3,4,5,b] +} + +func ExampleList_InsertBefore() { + l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) + + fmt.Println(l.Len()) + fmt.Println(l) + + l.InsertBefore(l.Front(), "a") + l.InsertBefore(l.Back(), "b") + + fmt.Println(l.Len()) + fmt.Println(l) + + // Output: + // 5 + // [1,2,3,4,5] + // 7 + // [a,1,2,3,4,b,5] +} + +func ExampleList_Remove() { + l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) + + fmt.Println(l.Len()) + fmt.Println(l) + + fmt.Println(l.Remove(l.Front())) + fmt.Println(l.Remove(l.Back())) + + fmt.Println(l.Len()) + fmt.Println(l) + + // Output: + // 5 + // [1,2,3,4,5] + // 1 + // 5 + // 3 + // [2,3,4] +} + +func ExampleList_Removes() { + l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) + + fmt.Println(l.Len()) + fmt.Println(l) + + l.Removes([]*glist.Element{l.Front(), l.Back()}) + + fmt.Println(l.Len()) + fmt.Println(l) + + // Output: + // 5 + // [1,2,3,4,5] + // 3 + // [2,3,4] +} + +func ExampleList_RemoveAll() { + l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) + + fmt.Println(l.Len()) + fmt.Println(l) + + l.RemoveAll() + + fmt.Println(l.Len()) + + // Output: + // 5 + // [1,2,3,4,5] + // 0 +} + +func ExampleList_RLockFunc() { + // concurrent-safe list. + l := glist.NewFrom(garray.NewArrayRange(1, 10, 1).Slice(), true) + // iterate reading from head. + l.RLockFunc(func(list *list.List) { + length := list.Len() + if length > 0 { + for i, e := 0, list.Front(); i < length; i, e = i+1, e.Next() { + fmt.Print(e.Value) + } + } + }) + fmt.Println() + // iterate reading from tail. + l.RLockFunc(func(list *list.List) { + length := list.Len() + if length > 0 { + for i, e := 0, list.Back(); i < length; i, e = i+1, e.Prev() { + fmt.Print(e.Value) + } + } + }) + + fmt.Println() + // Output: + // 12345678910 + // 10987654321 +} + +func ExampleList_IteratorAsc() { + // concurrent-safe list. + l := glist.NewFrom(garray.NewArrayRange(1, 10, 1).Slice(), true) + // iterate reading from head using IteratorAsc. + l.IteratorAsc(func(e *glist.Element) bool { + fmt.Print(e.Value) + return true + }) + + // Output: + // 12345678910 +} + +func ExampleList_IteratorDesc() { + // concurrent-safe list. + l := glist.NewFrom(garray.NewArrayRange(1, 10, 1).Slice(), true) + // iterate reading from tail using IteratorDesc. + l.IteratorDesc(func(e *glist.Element) bool { + fmt.Print(e.Value) + return true + }) + // Output: + // 10987654321 +} + +func ExampleList_LockFunc() { + // concurrent-safe list. + l := glist.NewFrom(garray.NewArrayRange(1, 10, 1).Slice(), true) + // iterate writing from head. + l.LockFunc(func(list *list.List) { + length := list.Len() + if length > 0 { + for i, e := 0, list.Front(); i < length; i, e = i+1, e.Next() { + if e.Value == 6 { + e.Value = "M" + break + } + } + } + }) + fmt.Println(l) + + // Output: + // [1,2,3,4,5,M,7,8,9,10] +} + +func ExampleList_Join() { + var l glist.List + l.PushBacks(g.Slice{"a", "b", "c", "d"}) + + fmt.Println(l.Join(",")) + + // Output: + // a,b,c,d +}