queue

package
v1.2.123 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Example
package main

import (
	"fmt"

	"github.com/searKing/golang/go/exp/container/queue"
)

func main() {
	// Create a new queue and put some numbers in it.
	var l1 queue.Queue[int]
	l1.PushBack(1)
	l1.PushBack(2)
	l1.PushBack(3)
	l1.PushBack(4)

	var l2 queue.Queue[int]
	l2.PushBack(5)
	l2.PushBack(6)
	l2.PushBack(7)
	l2.PushBack(8)

	l1.PushBackSeq(l2.Values())

	var s []int
	if cleaned := l2.TrimFrontFunc(func(e int) bool {
		if e%2 == 1 {
			s = append(s, e)
			return true
		}
		return false
	}); cleaned {
		fmt.Printf("l2: clean leading: %v\n", s)
	}
	// Iterate through queue and print its contents.
	for i, e := range l1.All() {
		fmt.Printf("l1[%d]: %d\n", i, e)
	}
	var i int
	for e := range l2.Values() {
		fmt.Printf("l2[%d]: %d\n", i, e)
		i++
	}

}
Output:

l2: clean leading: [5]
l1[0]: 1
l1[1]: 2
l1[2]: 3
l1[3]: 4
l1[4]: 5
l1[5]: 6
l1[6]: 7
l1[7]: 8
l2[0]: 6
l2[1]: 7
l2[2]: 8

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Queue

type Queue[E any] struct {
	// contains filtered or unexported fields
}

A Queue is a queue of FIFO, not a deque.

func New

func New[E any](e ...E) *Queue[E]

New returns an initialized queue. Deprecated: Use var q Queue[E] instead.

func (*Queue[E]) All added in v1.2.121

func (q *Queue[E]) All() iter.Seq2[int, E]

All returns an iterator over index-value pairs in the queue in the usual order.

func (*Queue[E]) Do

func (q *Queue[E]) Do(f func(E))

Do calls function f on each element of the queue without removing it, in forward order. The behavior of Do is undefined if f changes *q. Deprecated: Use [Range] or [All] instead.

func (*Queue[E]) Front

func (q *Queue[E]) Front() E

Front returns the item at the front of the queue without removing it.

func (*Queue[E]) Len

func (q *Queue[E]) Len() int

Len returns the number of items in the queue.

func (*Queue[E]) Next

func (q *Queue[E]) Next() bool

Next reports whether there are more iterations to execute. Every call to PopFront, even the first one, must be preceded by a call to Next.

func (*Queue[E]) PopFront

func (q *Queue[E]) PopFront() E

PopFront removes and returns the item at the front of the queue.

func (*Queue[E]) PushBack

func (q *Queue[E]) PushBack(w E)

PushBack adds w to the back of the queue.

func (*Queue[E]) PushBackQueue

func (q *Queue[E]) PushBackQueue(other *Queue[E])

PushBackQueue inserts a copy of another queue at the back of queue l. Deprecated: Use [PushBackSeq] instead.

func (*Queue[E]) PushBackSeq added in v1.2.121

func (q *Queue[E]) PushBackSeq(seq iter.Seq[E])

PushBackSeq appends the values from seq to the queue.

func (*Queue[E]) Range added in v1.2.121

func (q *Queue[E]) Range(f func(e E) bool)

Range calls f sequentially for each value present in the queue[E] in forward order. If f returns false, range stops the iteration.

func (*Queue[E]) ShrinkToFit

func (q *Queue[E]) ShrinkToFit()

ShrinkToFit requests the removal of unused capacity.

func (*Queue[E]) TrimFrontFunc

func (q *Queue[E]) TrimFrontFunc(f func(e E) bool) (cleaned bool)

TrimFrontFunc pops all leading elem that satisfying f(c) from the head of the queue, reporting whether any were popped.

func (*Queue[E]) Values added in v1.2.121

func (q *Queue[E]) Values() iter.Seq[E]

Values returns an iterator that yields the queue elements in order.

Jump to

Keyboard shortcuts

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