Documentation
¶
Overview ¶
Package glist provides most commonly used doubly linked list container which also supports concurrent-safe/unsafe switch feature.
Index ¶
- type Element
- type List
- func (l *List) Back() (e *Element)
- func (l *List) BackAll() (values []any)
- func (l *List) BackValue() (value any)
- func (l *List) Clear()
- func (l *List) DeepCopy() any
- func (l *List) Front() (e *Element)
- func (l *List) FrontAll() (values []any)
- func (l *List) FrontValue() (value any)
- func (l *List) InsertAfter(p *Element, v any) (e *Element)
- func (l *List) InsertBefore(p *Element, v any) (e *Element)
- func (l *List) Iterator(f func(e *Element) bool)
- func (l *List) IteratorAsc(f func(e *Element) bool)
- func (l *List) IteratorDesc(f func(e *Element) bool)
- func (l *List) Join(glue string) string
- func (l *List) Len() (length int)
- func (l *List) LockFunc(f func(list *list.List))
- func (l List) MarshalJSON() ([]byte, error)
- func (l *List) MoveAfter(e, p *Element)
- func (l *List) MoveBefore(e, p *Element)
- func (l *List) MoveToBack(e *Element)
- func (l *List) MoveToFront(e *Element)
- func (l *List) PopBack() (value any)
- func (l *List) PopBackAll() []any
- func (l *List) PopBacks(max int) (values []any)
- func (l *List) PopFront() (value any)
- func (l *List) PopFrontAll() []any
- func (l *List) PopFronts(max int) (values []any)
- func (l *List) PushBack(v any) (e *Element)
- func (l *List) PushBackList(other *List)
- func (l *List) PushBacks(values []any)
- func (l *List) PushFront(v any) (e *Element)
- func (l *List) PushFrontList(other *List)
- func (l *List) PushFronts(values []any)
- func (l *List) RLockFunc(f func(list *list.List))
- func (l *List) Remove(e *Element) (value any)
- func (l *List) RemoveAll()
- func (l *List) Removes(es []*Element)
- func (l *List) Size() int
- func (l *List) String() string
- func (l *List) UnmarshalJSON(b []byte) error
- func (l *List) UnmarshalValue(value any) (err error)
- type TElement
- type TList
- func (l *TList[T]) AppendList(nl *list.List)
- func (l *TList[T]) AssignList(nl *list.List) int
- func (l *TList[T]) Back() (e *TElement[T])
- func (l *TList[T]) BackAll() (values []T)
- func (l *TList[T]) BackValue() (value T)
- func (l *TList[T]) Clear()
- func (l *TList[T]) DeepCopy() any
- func (l *TList[T]) Front() (e *TElement[T])
- func (l *TList[T]) FrontAll() (values []T)
- func (l *TList[T]) FrontValue() (value T)
- func (l *TList[T]) InsertAfter(p *TElement[T], v T) (e *TElement[T])
- func (l *TList[T]) InsertBefore(p *TElement[T], v T) (e *TElement[T])
- func (l *TList[T]) Iterator(f func(e *TElement[T]) bool)
- func (l *TList[T]) IteratorAsc(f func(e *TElement[T]) bool)
- func (l *TList[T]) IteratorDesc(f func(e *TElement[T]) bool)
- func (l *TList[T]) Join(glue string) string
- func (l *TList[T]) Len() (length int)
- func (l *TList[T]) LockFunc(f func(list *list.List))
- func (l TList[T]) MarshalJSON() ([]byte, error)
- func (l *TList[T]) MoveAfter(e, p *TElement[T])
- func (l *TList[T]) MoveBefore(e, p *TElement[T])
- func (l *TList[T]) MoveToBack(e *TElement[T])
- func (l *TList[T]) MoveToFront(e *TElement[T])
- func (l *TList[T]) PopBack() (value T)
- func (l *TList[T]) PopBackAll() []T
- func (l *TList[T]) PopBacks(max int) (values []T)
- func (l *TList[T]) PopFront() (value T)
- func (l *TList[T]) PopFrontAll() []T
- func (l *TList[T]) PopFronts(max int) (values []T)
- func (l *TList[T]) PushBack(v T) (e *TElement[T])
- func (l *TList[T]) PushBackList(other *TList[T])
- func (l *TList[T]) PushBacks(values []T)
- func (l *TList[T]) PushFront(v T) (e *TElement[T])
- func (l *TList[T]) PushFrontList(other *TList[T])
- func (l *TList[T]) PushFronts(values []T)
- func (l *TList[T]) RLockFunc(f func(list *list.List))
- func (l *TList[T]) Remove(e *TElement[T]) (value T)
- func (l *TList[T]) RemoveAll()
- func (l *TList[T]) Removes(es []*TElement[T])
- func (l *TList[T]) Size() int
- func (l *TList[T]) String() string
- func (l *TList[T]) ToList() *list.List
- func (l *TList[T]) UnmarshalJSON(b []byte) error
- func (l *TList[T]) UnmarshalValue(value any) (err error)
Examples ¶
- List.Back
- List.BackAll
- List.BackValue
- List.Front
- List.FrontAll
- List.FrontValue
- List.InsertAfter
- List.InsertBefore
- List.IteratorAsc
- List.IteratorDesc
- List.Join
- List.Len
- List.LockFunc
- List.MoveAfter
- List.MoveBefore
- List.MoveToBack
- List.MoveToFront
- List.PopBack
- List.PopBackAll
- List.PopBacks
- List.PopFront
- List.PopFrontAll
- List.PopFronts
- List.PushBack
- List.PushBackList
- List.PushBacks
- List.PushFront
- List.PushFrontList
- List.PushFronts
- List.RLockFunc
- List.Remove
- List.RemoveAll
- List.Removes
- List.Size
- New
- NewFrom
- NewT
- NewTFrom
- TList.Back
- TList.BackAll
- TList.BackValue
- TList.Front
- TList.FrontAll
- TList.FrontValue
- TList.InsertAfter
- TList.InsertBefore
- TList.IteratorAsc
- TList.IteratorDesc
- TList.Join
- TList.Len
- TList.LockFunc
- TList.MoveAfter
- TList.MoveBefore
- TList.MoveToBack
- TList.MoveToFront
- TList.PopBack
- TList.PopBackAll
- TList.PopBacks
- TList.PopFront
- TList.PopFrontAll
- TList.PopFronts
- TList.PushBack
- TList.PushBackList
- TList.PushBacks
- TList.PushFront
- TList.PushFrontList
- TList.PushFronts
- TList.RLockFunc
- TList.Remove
- TList.RemoveAll
- TList.Removes
- TList.Size
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type List ¶
type List struct {
// contains filtered or unexported fields
}
List is a doubly linked list containing a concurrent-safe/unsafe switch. The switch should be set when its initialization and cannot be changed then.
func New ¶
New creates and returns a new empty doubly linked list.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
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 NewFrom ¶
NewFrom creates and returns a list from a copy of given slice `array`. The parameter `safe` is used to specify whether using list in concurrent-safety, which is false in default.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
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 (*List) Back ¶
Back returns the last element of list `l` or nil if the list is empty.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
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 (*List) BackAll ¶
BackAll copies and returns values of all elements from back of `l` as slice.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
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 (*List) BackValue ¶
BackValue returns value of the last element of `l` or nil if the list is empty.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice())
fmt.Println(l)
fmt.Println(l.BackValue())
}
Output: [1,2,3,4,5] 5
func (*List) DeepCopy ¶ added in v2.1.0
DeepCopy implements interface for deep copy of current type.
func (*List) Front ¶
Front returns the first element of list `l` or nil if the list is empty.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
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 (*List) FrontAll ¶
FrontAll copies and returns values of all elements from front of `l` as slice.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
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 (*List) FrontValue ¶
FrontValue returns value of the first element of `l` or nil if the list is empty.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice())
fmt.Println(l)
fmt.Println(l.FrontValue())
}
Output: [1,2,3,4,5] 1
func (*List) InsertAfter ¶
InsertAfter inserts a new element `e` with value `v` immediately after `p` and returns `e`. If `p` is not an element of `l`, the list is not modified. The `p` must not be nil.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
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 (*List) InsertBefore ¶
InsertBefore inserts a new element `e` with value `v` immediately before `p` and returns `e`. If `p` is not an element of `l`, the list is not modified. The `p` must not be nil.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
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 (*List) IteratorAsc ¶
IteratorAsc iterates the list readonly in ascending order with given callback function `f`. If `f` returns true, then it continues iterating; or false to stop.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
// 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 (*List) IteratorDesc ¶
IteratorDesc iterates the list readonly in descending order with given callback function `f`. If `f` returns true, then it continues iterating; or false to stop.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
// 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 (*List) Join ¶
Join joins list elements with a string `glue`.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/glist"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
var l glist.List
l.PushBacks(g.Slice{"a", "b", "c", "d"})
fmt.Println(l.Join(","))
}
Output: a,b,c,d
func (*List) Len ¶
Len returns the number of elements of list `l`. The complexity is O(1).
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/glist"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
l := glist.NewFrom(g.Slice{1, 2, 3, 4, 5})
fmt.Println(l.Len())
}
Output: 5
func (*List) LockFunc ¶
LockFunc locks writing with given callback function `f` within RWMutex.Lock.
Example ¶
package main
import (
"container/list"
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
// 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 (List) MarshalJSON ¶
MarshalJSON implements the interface MarshalJSON for json.Marshal.
func (*List) MoveAfter ¶
MoveAfter moves element `e` to its new position after `p`. If `e` or `p` is not an element of `l`, or `e` == `p`, the list is not modified. The element and `p` must not be nil.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
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 (*List) MoveBefore ¶
MoveBefore moves element `e` to its new position before `p`. If `e` or `p` is not an element of `l`, or `e` == `p`, the list is not modified. The element and `p` must not be nil.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
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 (*List) MoveToBack ¶
MoveToBack moves element `e` to the back of list `l`. If `e` is not an element of `l`, the list is not modified. The element must not be nil.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
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 (*List) MoveToFront ¶
MoveToFront moves element `e` to the front of list `l`. If `e` is not an element of `l`, the list is not modified. The element must not be nil.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
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 (*List) PopBack ¶
PopBack removes the element from back of `l` and returns the value of the element.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
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 (*List) PopBackAll ¶
PopBackAll removes all elements from back of `l` and returns values of the removed elements as slice.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
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 (*List) PopBacks ¶
PopBacks removes `max` elements from back of `l` and returns values of the removed elements as slice.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
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 (*List) PopFront ¶
PopFront removes the element from front of `l` and returns the value of the element.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
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 (*List) PopFrontAll ¶
PopFrontAll removes all elements from front of `l` and returns values of the removed elements as slice.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
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 (*List) PopFronts ¶
PopFronts removes `max` elements from front of `l` and returns values of the removed elements as slice.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
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 (*List) PushBack ¶
PushBack inserts a new element `e` with value `v` at the back of list `l` and returns `e`.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
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 (*List) PushBackList ¶
PushBackList inserts a copy of an other list at the back of list `l`. The lists `l` and `other` may be the same, but they must not be nil.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
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 (*List) PushBacks ¶
PushBacks inserts multiple new elements with values `values` at the back of list `l`.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
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 (*List) PushFront ¶
PushFront inserts a new element `e` with value `v` at the front of list `l` and returns `e`.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
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 (*List) PushFrontList ¶
PushFrontList inserts a copy of an other list at the front of list `l`. The lists `l` and `other` may be the same, but they must not be nil.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
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 (*List) PushFronts ¶
PushFronts inserts multiple new elements with values `values` at the front of list `l`.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
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 (*List) RLockFunc ¶
RLockFunc locks reading with given callback function `f` within RWMutex.RLock.
Example ¶
package main
import (
"container/list"
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
// 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 (*List) Remove ¶
Remove removes `e` from `l` if `e` is an element of list `l`. It returns the element value e.Value. The element must not be nil.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
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 (*List) RemoveAll ¶
func (l *List) RemoveAll()
RemoveAll removes all elements from list `l`.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
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 (*List) Removes ¶
Removes removes multiple elements `es` from `l` if `es` are elements of list `l`.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
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 (*List) Size ¶
Size is alias of Len.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/glist"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
l := glist.NewFrom(g.Slice{1, 2, 3, 4, 5})
fmt.Println(l.Size())
}
Output: 5
func (*List) UnmarshalJSON ¶
UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
func (*List) UnmarshalValue ¶
UnmarshalValue is an interface implement which sets any type of value for list.
type TElement ¶ added in v2.9.6
type TElement[T any] struct { // The value stored with this element. Value T // contains filtered or unexported fields }
TElement is an element of a linked list.
type TList ¶ added in v2.9.6
type TList[T any] struct { // contains filtered or unexported fields }
func NewT ¶ added in v2.9.6
NewT creates and returns a new empty doubly linked list.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
n := 10
l := glist.NewT[any]()
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 NewTFrom ¶ added in v2.9.6
NewTFrom creates and returns a list from a copy of given slice `array`. The parameter `safe` is used to specify whether using list in concurrent-safety, which is false in default.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
n := 10
l := glist.NewTFrom[any](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 (*TList[T]) AppendList ¶ added in v2.9.6
AppendList append list.List to the end
func (*TList[T]) AssignList ¶ added in v2.9.6
AssignList assigns list.List to now TList[T]. It will clear TList[T] first, and append the list.List. Note: Elements in nl that are not assignable to T are silently skipped. Returns the number of skipped (incompatible) elements.
func (*TList[T]) Back ¶ added in v2.9.6
Back returns the last element of list `l` or nil if the list is empty.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
l := glist.NewTFrom[any](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 (*TList[T]) BackAll ¶ added in v2.9.6
func (l *TList[T]) BackAll() (values []T)
BackAll copies and returns values of all elements from back of `l` as slice.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
l := glist.NewTFrom[any](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 (*TList[T]) BackValue ¶ added in v2.9.6
func (l *TList[T]) BackValue() (value T)
BackValue returns value of the last element of `l` or zero value of T if the list is empty.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
l := glist.NewTFrom[any](garray.NewArrayRange(1, 5, 1).Slice())
fmt.Println(l)
fmt.Println(l.BackValue())
}
Output: [1,2,3,4,5] 5
func (*TList[T]) DeepCopy ¶ added in v2.9.6
DeepCopy implements interface for deep copy of current type.
func (*TList[T]) Front ¶ added in v2.9.6
Front returns the first element of list `l` or nil if the list is empty.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
l := glist.NewTFrom[any](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 (*TList[T]) FrontAll ¶ added in v2.9.6
func (l *TList[T]) FrontAll() (values []T)
FrontAll copies and returns values of all elements from front of `l` as slice.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
l := glist.NewTFrom[any](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 (*TList[T]) FrontValue ¶ added in v2.9.6
func (l *TList[T]) FrontValue() (value T)
FrontValue returns value of the first element of `l` or zero value of T if the list is empty.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
l := glist.NewTFrom[any](garray.NewArrayRange(1, 5, 1).Slice())
fmt.Println(l)
fmt.Println(l.FrontValue())
}
Output: [1,2,3,4,5] 1
func (*TList[T]) InsertAfter ¶ added in v2.9.6
InsertAfter inserts a new element `e` with value `v` immediately after `p` and returns `e`. If `p` is not an element of `l`, the list is not modified. The `p` must not be nil.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
l := glist.NewTFrom[any](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 (*TList[T]) InsertBefore ¶ added in v2.9.6
InsertBefore inserts a new element `e` with value `v` immediately before `p` and returns `e`. If `p` is not an element of `l`, the list is not modified. The `p` must not be nil.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
l := glist.NewTFrom[any](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 (*TList[T]) IteratorAsc ¶ added in v2.9.6
IteratorAsc iterates the list readonly in ascending order with given callback function `f`. If `f` returns true, then it continues iterating; or false to stop.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
// concurrent-safe list.
l := glist.NewTFrom[any](garray.NewArrayRange(1, 10, 1).Slice(), true)
// iterate reading from head using IteratorAsc.
l.IteratorAsc(func(e *glist.TElement[any]) bool {
fmt.Print(e.Value)
return true
})
}
Output: 12345678910
func (*TList[T]) IteratorDesc ¶ added in v2.9.6
IteratorDesc iterates the list readonly in descending order with given callback function `f`. If `f` returns true, then it continues iterating; or false to stop.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
// concurrent-safe list.
l := glist.NewTFrom[any](garray.NewArrayRange(1, 10, 1).Slice(), true)
// iterate reading from tail using IteratorDesc.
l.IteratorDesc(func(e *glist.TElement[any]) bool {
fmt.Print(e.Value)
return true
})
}
Output: 10987654321
func (*TList[T]) Join ¶ added in v2.9.6
Join joins list elements with a string `glue`.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/glist"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
var l glist.TList[any]
l.PushBacks(g.Slice{"a", "b", "c", "d"})
fmt.Println(l.Join(","))
}
Output: a,b,c,d
func (*TList[T]) Len ¶ added in v2.9.6
Len returns the number of elements of list `l`. The complexity is O(1).
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/glist"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
l := glist.NewTFrom[any](g.Slice{1, 2, 3, 4, 5})
fmt.Println(l.Len())
}
Output: 5
func (*TList[T]) LockFunc ¶ added in v2.9.6
LockFunc locks writing with given callback function `f` within RWMutex.Lock.
Example ¶
package main
import (
"container/list"
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
// concurrent-safe list.
l := glist.NewTFrom[any](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 (TList[T]) MarshalJSON ¶ added in v2.9.6
MarshalJSON implements the interface MarshalJSON for json.Marshal.
func (*TList[T]) MoveAfter ¶ added in v2.9.6
MoveAfter moves element `e` to its new position after `p`. If `e` or `p` is not an element of `l`, or `e` == `p`, the list is not modified. The element and `p` must not be nil.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
l := glist.NewTFrom[any](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.TElement[any]{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 (*TList[T]) MoveBefore ¶ added in v2.9.6
MoveBefore moves element `e` to its new position before `p`. If `e` or `p` is not an element of `l`, or `e` == `p`, the list is not modified. The element and `p` must not be nil.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
l := glist.NewTFrom[any](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.TElement[any]{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 (*TList[T]) MoveToBack ¶ added in v2.9.6
MoveToBack moves element `e` to the back of list `l`. If `e` is not an element of `l`, the list is not modified. The element must not be nil.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
l := glist.NewTFrom[any](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.TElement[any]{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 (*TList[T]) MoveToFront ¶ added in v2.9.6
MoveToFront moves element `e` to the front of list `l`. If `e` is not an element of `l`, the list is not modified. The element must not be nil.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
l := glist.NewTFrom[any](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.TElement[any]{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 (*TList[T]) PopBack ¶ added in v2.9.6
func (l *TList[T]) PopBack() (value T)
PopBack removes the element from back of `l` and returns the value of the element.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
l := glist.NewTFrom[any](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 (*TList[T]) PopBackAll ¶ added in v2.9.6
func (l *TList[T]) PopBackAll() []T
PopBackAll removes all elements from back of `l` and returns values of the removed elements as slice.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
l := glist.NewTFrom[any](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 (*TList[T]) PopBacks ¶ added in v2.9.6
PopBacks removes `max` elements from back of `l` and returns values of the removed elements as slice.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
l := glist.NewTFrom[any](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 (*TList[T]) PopFront ¶ added in v2.9.6
func (l *TList[T]) PopFront() (value T)
PopFront removes the element from front of `l` and returns the value of the element.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
l := glist.NewTFrom[any](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 (*TList[T]) PopFrontAll ¶ added in v2.9.6
func (l *TList[T]) PopFrontAll() []T
PopFrontAll removes all elements from front of `l` and returns values of the removed elements as slice.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
l := glist.NewTFrom[any](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 (*TList[T]) PopFronts ¶ added in v2.9.6
PopFronts removes `max` elements from front of `l` and returns values of the removed elements as slice.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
l := glist.NewTFrom[any](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 (*TList[T]) PushBack ¶ added in v2.9.6
PushBack inserts a new element `e` with value `v` at the back of list `l` and returns `e`.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
l := glist.NewTFrom[any](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 (*TList[T]) PushBackList ¶ added in v2.9.6
PushBackList inserts a copy of an other list at the back of list `l`. The lists `l` and `other` may be the same, but they must not be nil.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
l := glist.NewTFrom[any](garray.NewArrayRange(1, 5, 1).Slice())
fmt.Println(l.Size())
fmt.Println(l)
other := glist.NewTFrom[any](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 (*TList[T]) PushBacks ¶ added in v2.9.6
func (l *TList[T]) PushBacks(values []T)
PushBacks inserts multiple new elements with values `values` at the back of list `l`.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
l := glist.NewTFrom[any](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 (*TList[T]) PushFront ¶ added in v2.9.6
PushFront inserts a new element `e` with value `v` at the front of list `l` and returns `e`.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
l := glist.NewTFrom[any](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 (*TList[T]) PushFrontList ¶ added in v2.9.6
PushFrontList inserts a copy of an other list at the front of list `l`. The lists `l` and `other` may be the same, but they must not be nil.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
l := glist.NewTFrom[any](garray.NewArrayRange(1, 5, 1).Slice())
fmt.Println(l.Size())
fmt.Println(l)
other := glist.NewTFrom[any](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 (*TList[T]) PushFronts ¶ added in v2.9.6
func (l *TList[T]) PushFronts(values []T)
PushFronts inserts multiple new elements with values `values` at the front of list `l`.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
l := glist.NewTFrom[any](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 (*TList[T]) RLockFunc ¶ added in v2.9.6
RLockFunc locks reading with given callback function `f` within RWMutex.RLock.
Example ¶
package main
import (
"container/list"
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
// concurrent-safe list.
l := glist.NewTFrom[any](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 (*TList[T]) Remove ¶ added in v2.9.6
Remove removes `e` from `l` if `e` is an element of list `l`. It returns the element value e.Value. The element must not be nil.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
l := glist.NewTFrom[any](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 (*TList[T]) RemoveAll ¶ added in v2.9.6
func (l *TList[T]) RemoveAll()
RemoveAll removes all elements from list `l`.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
l := glist.NewTFrom[any](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 (*TList[T]) Removes ¶ added in v2.9.6
Removes removes multiple elements `es` from `l` if `es` are elements of list `l`.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/glist"
)
func main() {
l := glist.NewTFrom[any](garray.NewArrayRange(1, 5, 1).Slice())
fmt.Println(l.Len())
fmt.Println(l)
l.Removes([]*glist.TElement[any]{l.Front(), l.Back()})
fmt.Println(l.Len())
fmt.Println(l)
}
Output: 5 [1,2,3,4,5] 3 [2,3,4]
func (*TList[T]) Size ¶ added in v2.9.6
Size is alias of Len.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/glist"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
l := glist.NewTFrom[any](g.Slice{1, 2, 3, 4, 5})
fmt.Println(l.Size())
}
Output: 5
func (*TList[T]) UnmarshalJSON ¶ added in v2.9.6
UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
func (*TList[T]) UnmarshalValue ¶ added in v2.9.6
UnmarshalValue is an interface implement which sets any type of value for list.