glist

package
v2.0.0-...-bbb9e3c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 7, 2023 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package glist provides most commonly used doubly linked list container which also supports concurrent-safe/unsafe switch feature.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Element

type Element = list.Element

Element the item type of the list.

type List

type List[T any] 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

func New[T any](safe ...bool) *List[T]

New creates and returns a new empty doubly linked list.

Example
package main

import (
	"fmt"

	"github.com/wowqhb/gf/v2/container/glist"
)

func main() {
	n := 10
	l := glist.New[int]()
	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

func NewFrom[T any](array []T, safe ...bool) *List[T]

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/wowqhb/gf/v2/container/garray"
	"github.com/wowqhb/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[T]) Back

func (l *List[T]) Back() (e *Element)

Back returns the last element of list `l` or nil if the list is empty.

Example
package main

import (
	"fmt"

	"github.com/wowqhb/gf/v2/container/garray"
	"github.com/wowqhb/gf/v2/container/glist"
	"github.com/wowqhb/gf/v2/util/gconv"
)

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, gconv.Int("a"))
	l.InsertAfter(e, 6)

	fmt.Println(l)

}
Output:

5
[1,2,3,4,5]
[1,2,3,4,a,5,6]

func (*List[T]) BackAll

func (l *List[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/wowqhb/gf/v2/container/garray"
	"github.com/wowqhb/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[T]) BackValue

func (l *List[T]) BackValue() (value T)

BackValue returns value of the last element of `l` or nil if the list is empty.

Example
package main

import (
	"fmt"

	"github.com/wowqhb/gf/v2/container/garray"
	"github.com/wowqhb/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[T]) Clear

func (l *List[T]) Clear()

Clear is alias of RemoveAll.

func (*List[T]) DeepCopy

func (l *List[T]) DeepCopy() *List[T]

DeepCopy implements interface for deep copy of current type.

func (*List[T]) Front

func (l *List[T]) Front() (e *Element)

Front returns the first element of list `l` or nil if the list is empty.

Example
package main

import (
	"fmt"

	"github.com/wowqhb/gf/v2/container/garray"
	"github.com/wowqhb/gf/v2/container/glist"
)

func main() {
	l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice())

	fmt.Println(l)

	// fmt.Println(l.Front().Value)
	// fmt.Println(l)

	// e := l.Front()
	// l.InsertBefore(e, 0)
	// l.InsertAfter(e, gconv.Int("a"))

}
Output:

1
[1,2,3,4,5]
[0,1,a,2,3,4,5]

func (*List[T]) FrontAll

func (l *List[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/wowqhb/gf/v2/container/garray"
	"github.com/wowqhb/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[T]) FrontValue

func (l *List[T]) FrontValue() (value T)

FrontValue returns value of the first element of `l` or nil if the list is empty.

Example
package main

import (
	"fmt"

	"github.com/wowqhb/gf/v2/container/garray"
	"github.com/wowqhb/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[T]) InsertAfter

func (l *List[T]) InsertAfter(p *Element, v T) (e *Element)

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/wowqhb/gf/v2/container/glist"
	"github.com/wowqhb/gf/v2/frame/g"
)

func main() {
	// l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice())
	l := glist.NewFrom(g.Slice{1, 2, 3, 4, 5})

	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[T]) InsertBefore

func (l *List[T]) InsertBefore(p *Element, v T) (e *Element)

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/wowqhb/gf/v2/container/glist"
	"github.com/wowqhb/gf/v2/frame/g"
)

func main() {
	// l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice())
	l := glist.NewFrom(g.Slice{1, 2, 3, 4, 5})

	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[T]) Iterator

func (l *List[T]) Iterator(f func(e *Element) bool)

Iterator is alias of IteratorAsc.

func (*List[T]) IteratorAsc

func (l *List[T]) IteratorAsc(f func(e *Element) bool)

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/wowqhb/gf/v2/container/garray"
	"github.com/wowqhb/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[T]) IteratorDesc

func (l *List[T]) IteratorDesc(f func(e *Element) bool)

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/wowqhb/gf/v2/container/garray"
	"github.com/wowqhb/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[T]) Join

func (l *List[T]) Join(glue string) string

Join joins list elements with a string `glue`.

Example
package main

import (
	"fmt"

	"github.com/wowqhb/gf/v2/container/glist"
)

func main() {
	var l glist.List[string]
	l.PushBacks([]string{"a", "b", "c", "d"})

	fmt.Println(l.Join(","))

}
Output:

a,b,c,d

func (*List[T]) Len

func (l *List[T]) Len() (length int)

Len returns the number of elements of list `l`. The complexity is O(1).

Example
package main

import (
	"fmt"

	"github.com/wowqhb/gf/v2/container/glist"
	"github.com/wowqhb/gf/v2/frame/g"
)

func main() {
	l := glist.NewFrom(g.Slice{1, 2, 3, 4, 5})

	fmt.Println(l.Len())

}
Output:

5

func (*List[T]) LockFunc

func (l *List[T]) LockFunc(f func(list *list.List))

LockFunc locks writing with given callback function `f` within RWMutex.Lock.

Example
package main

import (
	"container/list"
	"fmt"

	"github.com/wowqhb/gf/v2/container/garray"
	"github.com/wowqhb/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[T]) MarshalJSON

func (l *List[T]) MarshalJSON() ([]byte, error)

MarshalJSON implements the interface MarshalJSON for json.Marshal.

func (*List[T]) MoveAfter

func (l *List[T]) MoveAfter(e, p *Element)

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/wowqhb/gf/v2/container/garray"
	"github.com/wowqhb/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[T]) MoveBefore

func (l *List[T]) MoveBefore(e, p *Element)

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/wowqhb/gf/v2/container/garray"
	"github.com/wowqhb/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[T]) MoveToBack

func (l *List[T]) MoveToBack(e *Element)

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/wowqhb/gf/v2/container/garray"
	"github.com/wowqhb/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[T]) MoveToFront

func (l *List[T]) MoveToFront(e *Element)

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/wowqhb/gf/v2/container/garray"
	"github.com/wowqhb/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[T]) PopBack

func (l *List[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/wowqhb/gf/v2/container/garray"
	"github.com/wowqhb/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[T]) PopBackAll

func (l *List[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/wowqhb/gf/v2/container/garray"
	"github.com/wowqhb/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[T]) PopBacks

func (l *List[T]) PopBacks(max int) (values []T)

PopBacks removes `max` elements from back of `l` and returns values of the removed elements as slice.

Example
package main

import (
	"fmt"

	"github.com/wowqhb/gf/v2/container/garray"
	"github.com/wowqhb/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[T]) PopFront

func (l *List[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/wowqhb/gf/v2/container/garray"
	"github.com/wowqhb/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[T]) PopFrontAll

func (l *List[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/wowqhb/gf/v2/container/garray"
	"github.com/wowqhb/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[T]) PopFronts

func (l *List[T]) PopFronts(max int) (values []T)

PopFronts removes `max` elements from front of `l` and returns values of the removed elements as slice.

Example
package main

import (
	"fmt"

	"github.com/wowqhb/gf/v2/container/garray"
	"github.com/wowqhb/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[T]) PushBack

func (l *List[T]) PushBack(v T) (e *Element)

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/wowqhb/gf/v2/container/garray"
	"github.com/wowqhb/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[T]) PushBackList

func (l *List[T]) PushBackList(other *List[T])

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/wowqhb/gf/v2/container/garray"
	"github.com/wowqhb/gf/v2/container/glist"
	"github.com/wowqhb/gf/v2/frame/g"
)

func main() {
	l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice())
	// l := glist.New[any]()

	fmt.Println(l.Size())
	fmt.Println(l)

	other := glist.NewFrom(g.SliceInt{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[T]) PushBacks

func (l *List[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/wowqhb/gf/v2/container/garray"
	"github.com/wowqhb/gf/v2/container/glist"
)

func main() {
	l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice())

	fmt.Println(l.Len())
	fmt.Println(l)

	l.PushBacks([]int{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[T]) PushFront

func (l *List[T]) PushFront(v T) (e *Element)

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/wowqhb/gf/v2/container/garray"
	"github.com/wowqhb/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[T]) PushFrontList

func (l *List[T]) PushFrontList(other *List[T])

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/wowqhb/gf/v2/container/glist"
	"github.com/wowqhb/gf/v2/frame/g"
)

func main() {
	l := glist.NewFrom(g.Slice{1, 2, 3, 4, 5})

	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[T]) PushFronts

func (l *List[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/wowqhb/gf/v2/container/garray"
	"github.com/wowqhb/gf/v2/container/glist"
)

func main() {
	l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice())

	fmt.Println(l.Len())
	fmt.Println(l)

	l.PushFronts([]int{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[T]) RLockFunc

func (l *List[T]) RLockFunc(f func(list *list.List))

RLockFunc locks reading with given callback function `f` within RWMutex.RLock.

Example
package main

import (
	"container/list"
	"fmt"

	"github.com/wowqhb/gf/v2/container/garray"
	"github.com/wowqhb/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[T]) Remove

func (l *List[T]) Remove(e *Element) (value T)

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/wowqhb/gf/v2/container/garray"
	"github.com/wowqhb/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[T]) RemoveAll

func (l *List[T]) RemoveAll()

RemoveAll removes all elements from list `l`.

Example
package main

import (
	"fmt"

	"github.com/wowqhb/gf/v2/container/garray"
	"github.com/wowqhb/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[T]) Removes

func (l *List[T]) Removes(es []*Element)

Removes removes multiple elements `es` from `l` if `es` are elements of list `l`.

Example
package main

import (
	"fmt"

	"github.com/wowqhb/gf/v2/container/garray"
	"github.com/wowqhb/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[T]) Size

func (l *List[T]) Size() int

Size is alias of Len.

Example
package main

import (
	"fmt"

	"github.com/wowqhb/gf/v2/container/glist"
	"github.com/wowqhb/gf/v2/frame/g"
)

func main() {
	l := glist.NewFrom(g.Slice{1, 2, 3, 4, 5})

	fmt.Println(l.Size())

}
Output:

5

func (*List[T]) String

func (l *List[T]) String() string

String returns current list as a string.

func (*List[T]) UnmarshalJSON

func (l *List[T]) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.

func (*List[T]) UnmarshalValue

func (l *List[T]) UnmarshalValue(value interface{}) (err error)

UnmarshalValue is an interface implement which sets any type of value for list.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL