prlist

package module
v0.0.0-...-324bb83 Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2021 License: MIT Imports: 1 Imported by: 0

README

prlist

priority list, 优先级链表

Index

type Element 
func (e *Element) Prev() *Element
func (e *Element) Next() *Element 

type PrList 
func New() *PrList 
func (pl *PrList) Len() int 
func (pl *PrList) Front() *Element
func (pl *PrList) Back() *Element 
func (pl *PrList) Remove(e *Element) interface{} 
func (pl *PrList) Push(v interface{}, priority ...uint32) *Element
func (pl *PrList) PushList(other *PrList)
func (pl *PrList) Pop() interface{}

Usage

func TestNew(t *testing.T) {
	pl := New()

	// push
	pl.Push(1, 2)
	pl.Push(2, 4)
	pl.Push(3, 2)
	pl.Push(4)
	pl.Push(5, 5)
	e := pl.Push(6, 4)
	pl.Push(7, 5)
	pl.Push(8)
	
	// range
	for e := pl.Front(); e != nil; e = e.Next() {
		t.Log(e.Value)
	}
	
	fmt.Println()
	// remove
	t.Log(pl.Remove(e))
	pl.Push(9, 4)
	
	fmt.Println()
	// push list
	pl2 := New()
	pl2.PushList(pl)
	pl.PushList(pl)
	
	// pop
	for i := pl.Len(); i > 0; i-- {
		t.Log(pl.Pop())
	}
	fmt.Println()
	for i := pl2.Len(); i > 0; i-- {
		t.Log(pl2.Pop())
	}
}

// output
//    prlist_test.go:23: 5
//    prlist_test.go:23: 7
//    prlist_test.go:23: 2
//    prlist_test.go:23: 6
//    prlist_test.go:23: 1
//    prlist_test.go:23: 3
//    prlist_test.go:23: 4
//    prlist_test.go:23: 8
//
//    prlist_test.go:28: 6
//
//    prlist_test.go:39: 5
//    prlist_test.go:39: 7
//    prlist_test.go:39: 2
//    prlist_test.go:39: 9
//    prlist_test.go:39: 1
//    prlist_test.go:39: 3
//    prlist_test.go:39: 4
//    prlist_test.go:39: 8
//
//    prlist_test.go:43: 5
//    prlist_test.go:43: 7
//    prlist_test.go:43: 2
//    prlist_test.go:43: 9
//    prlist_test.go:43: 1
//    prlist_test.go:43: 3
//    prlist_test.go:43: 4
//    prlist_test.go:43: 8

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Element

type Element struct {
	// The value stored with this element.
	Value interface{}
	// contains filtered or unexported fields
}

Element is an element of a linked prlist.

func (*Element) Next

func (e *Element) Next() *Element

Next returns the next prlist element or nil.

func (*Element) Prev

func (e *Element) Prev() *Element

Prev returns the previous prlist element or nil.

type PrList

type PrList struct {
	// contains filtered or unexported fields
}

func New

func New() *PrList

New returns an initialized prlist.

func (*PrList) Back

func (pl *PrList) Back() *Element

Back returns the last element of prlist pl or nil if the prlist is empty.

func (*PrList) Front

func (pl *PrList) Front() *Element

Front returns the first element of prlist pl or nil if the prlist is empty.

func (*PrList) Len

func (pl *PrList) Len() int

Len returns the number of elements of prlist pl. The complexity is O(1).

func (*PrList) Pop

func (pl *PrList) Pop() interface{}

Pop returns the first element.Value of prlist pl and remove it.

func (*PrList) Push

func (pl *PrList) Push(v interface{}, priority ...uint32) *Element

Push insert v with priority and return a new element.

func (*PrList) PushList

func (pl *PrList) PushList(other *PrList)

PushList inserts a copy of another prlist.

func (*PrList) Remove

func (pl *PrList) Remove(e *Element) interface{}

Remove removes e from pl if e is an element of prlist pl. It returns the element value e.Value. The element must not be nil.

Jump to

Keyboard shortcuts

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