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 []interface{})
- func (l *List) BackValue() (value interface{})
- func (l *List) Clear()
- func (l *List) Front() (e *Element)
- func (l *List) FrontAll() (values []interface{})
- func (l *List) FrontValue() (value interface{})
- func (l *List) InsertAfter(p *Element, v interface{}) (e *Element)
- func (l *List) InsertBefore(p *Element, v interface{}) (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 interface{})
- func (l *List) PopBackAll() []interface{}
- func (l *List) PopBacks(max int) (values []interface{})
- func (l *List) PopFront() (value interface{})
- func (l *List) PopFrontAll() []interface{}
- func (l *List) PopFronts(max int) (values []interface{})
- func (l *List) PushBack(v interface{}) (e *Element)
- func (l *List) PushBackList(other *List)
- func (l *List) PushBacks(values []interface{})
- func (l *List) PushFront(v interface{}) (e *Element)
- func (l *List) PushFrontList(other *List)
- func (l *List) PushFronts(values []interface{})
- func (l *List) RLockFunc(f func(list *list.List))
- func (l *List) Remove(e *Element) (value interface{})
- 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 interface{}) (err error)
Examples ¶
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/csrgxtu/gf/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.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 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.
func (*List) BackAll ¶
func (l *List) BackAll() (values []interface{})
BackAll copies and returns values of all elements from back of <l> as slice.
func (*List) BackValue ¶
func (l *List) BackValue() (value interface{})
BackValue returns value of the last element of <l> or nil if the list is empty.
func (*List) FrontAll ¶
func (l *List) FrontAll() (values []interface{})
FrontAll copies and returns values of all elements from front of <l> as slice.
func (*List) FrontValue ¶
func (l *List) FrontValue() (value interface{})
FrontValue returns value of the first element of <l> or nil if the list is empty.
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.
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.
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/csrgxtu/gf/container/garray" "github.com/csrgxtu/gf/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/csrgxtu/gf/container/garray" "github.com/csrgxtu/gf/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/csrgxtu/gf/frame/g" "github.com/csrgxtu/gf/container/glist" ) 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) LockFunc ¶
LockFunc locks writing with given callback function <f> within RWMutex.Lock.
Example ¶
package main import ( "container/list" "fmt" "github.com/csrgxtu/gf/container/garray" "github.com/csrgxtu/gf/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.
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.
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.
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.
func (*List) PopBack ¶
func (l *List) PopBack() (value interface{})
PopBack removes the element from back of <l> and returns the value of the element.
Example ¶
package main import ( "fmt" "github.com/csrgxtu/gf/frame/g" "github.com/csrgxtu/gf/container/glist" ) func main() { l := glist.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9}) fmt.Println(l.PopBack()) }
Output: 9
func (*List) PopBackAll ¶
func (l *List) PopBackAll() []interface{}
PopBackAll removes all elements from back of <l> and returns values of the removed elements as slice.
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/csrgxtu/gf/frame/g" "github.com/csrgxtu/gf/container/glist" ) func main() { l := glist.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9}) fmt.Println(l.PopBacks(2)) }
Output: [9 8]
func (*List) PopFront ¶
func (l *List) PopFront() (value interface{})
PopFront removes the element from front of <l> and returns the value of the element.
Example ¶
package main import ( "fmt" "github.com/csrgxtu/gf/frame/g" "github.com/csrgxtu/gf/container/glist" ) func main() { l := glist.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9}) fmt.Println(l.PopFront()) }
Output: 1
func (*List) PopFrontAll ¶
func (l *List) PopFrontAll() []interface{}
PopFrontAll removes all elements from front of <l> and returns values of the removed elements as slice.
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/csrgxtu/gf/frame/g" "github.com/csrgxtu/gf/container/glist" ) func main() { l := glist.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9}) fmt.Println(l.PopFronts(2)) }
Output: [1 2]
func (*List) PushBack ¶
PushBack inserts a new element <e> with value <v> at the back of list <l> and returns <e>.
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.
func (*List) PushBacks ¶
func (l *List) PushBacks(values []interface{})
PushBacks inserts multiple new elements with values <values> at the back of list <l>.
func (*List) PushFront ¶
PushFront inserts a new element <e> with value <v> at the front of list <l> and returns <e>.
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.
func (*List) PushFronts ¶
func (l *List) PushFronts(values []interface{})
PushFronts inserts multiple new elements with values <values> at the front of list <l>.
func (*List) RLockFunc ¶
RLockFunc locks reading with given callback function <f> within RWMutex.RLock.
Example ¶
package main import ( "container/list" "fmt" "github.com/csrgxtu/gf/container/garray" "github.com/csrgxtu/gf/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.
func (*List) Removes ¶
Removes removes multiple elements <es> from <l> if <es> are elements of list <l>.
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.