queue

package
v0.0.0-...-592a62f Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ChunkQueue

type ChunkQueue[T any] struct {
	// contains filtered or unexported fields
}

ChunkQueue is a generic, efficient, iterable and GC-friendly queue. Attention, it's not thread-safe.

func NewChunkQueue

func NewChunkQueue[T any]() *ChunkQueue[T]

NewChunkQueue creates a new ChunkQueue

func NewChunkQueueLeastCapacity

func NewChunkQueueLeastCapacity[T any](minCapacity int) *ChunkQueue[T]

NewChunkQueueLeastCapacity creates a ChunkQueue with an argument minCapacity. It requests that the queue capacity be at least minCapacity. And it's similar to the cap argument when making a slice using make([]T, len, cap)

func (*ChunkQueue[T]) Begin

func (q *ChunkQueue[T]) Begin() *ChunkQueueIterator[T]

Begin is an alias of First(), for convenient

func (*ChunkQueue[T]) Cap

func (q *ChunkQueue[T]) Cap() int

Cap returns the capacity of the queue. The queue can hold more elements than that number by automatic expansion

func (*ChunkQueue[T]) Clear

func (q *ChunkQueue[T]) Clear()

Clear clears the queue and shrinks the chunks array

func (*ChunkQueue[T]) Empty

func (q *ChunkQueue[T]) Empty() bool

Empty indicates whether the queue is empty

func (*ChunkQueue[T]) End

func (q *ChunkQueue[T]) End() *ChunkQueueIterator[T]

End returns a special iterator of the queue representing the end. The end iterator is not valid since it's not in the queue. Its predecessor is Last()

func (*ChunkQueue[T]) First

func (q *ChunkQueue[T]) First() *ChunkQueueIterator[T]

First returns the iterator of the first element. The iterator is valid for a non-empty queue, and invalid otherwise.

func (*ChunkQueue[T]) GetIterator

func (q *ChunkQueue[T]) GetIterator(idx int) *ChunkQueueIterator[T]

GetIterator returns an iterator of a given index. Nil for invalid indices

func (*ChunkQueue[T]) Head

func (q *ChunkQueue[T]) Head() (T, bool)

Head returns the value of the first element. This method is read-only

func (*ChunkQueue[T]) Last

func (q *ChunkQueue[T]) Last() *ChunkQueueIterator[T]

Last returns the iterator of the last element. The iterator is valid for a non-empty queue, and invalid otherwise.

func (*ChunkQueue[T]) Len

func (q *ChunkQueue[T]) Len() int

Len returns the number of elements in queue

func (*ChunkQueue[T]) Peek

func (q *ChunkQueue[T]) Peek(idx int) T

Peek returns the value of a given index. It does NOT support modifying the value

func (*ChunkQueue[T]) Pop

func (q *ChunkQueue[T]) Pop() (T, bool)

Pop dequeues an element from head. The second return value is true on success, and false if the queue is empty and no element can be popped

func (*ChunkQueue[T]) PopAll

func (q *ChunkQueue[T]) PopAll() []T

PopAll dequeues all elements in the queue

func (*ChunkQueue[T]) PopMany

func (q *ChunkQueue[T]) PopMany(n int) ([]T, bool)

PopMany dequeues n elements at a time. The second return value is true if n elements were popped out, and false otherwise.

func (*ChunkQueue[T]) Push

func (q *ChunkQueue[T]) Push(v T)

Push enqueues an element to tail

func (*ChunkQueue[T]) PushMany

func (q *ChunkQueue[T]) PushMany(vals ...T)

PushMany enqueues multiple elements at a time

func (*ChunkQueue[T]) Range

func (q *ChunkQueue[T]) Range(f func(e T) bool)

Range iterates the queue from head to the first element e that f(e) returns false, or to the end if f() is true for all elements.

func (*ChunkQueue[T]) RangeAndPop

func (q *ChunkQueue[T]) RangeAndPop(f func(e T) bool)

RangeAndPop iterate the queue from head, and pop the element til the first element e that f(e) is false, or all elements if f(e) is true for all elements. This method is more convenient than Peek and Pop

func (*ChunkQueue[T]) RangeWithIndex

func (q *ChunkQueue[T]) RangeWithIndex(f func(idx int, e T) bool)

RangeWithIndex iterates the queue with index from head. the first element e with index i that f(i, e) is false, or to tail if f() is true for all elements.

func (*ChunkQueue[T]) Replace

func (q *ChunkQueue[T]) Replace(idx int, val T)

Replace assigns a new value to a given index

func (*ChunkQueue[T]) Shrink

func (q *ChunkQueue[T]) Shrink()

Shrink shrinks the space of the chunks array

func (*ChunkQueue[T]) Tail

func (q *ChunkQueue[T]) Tail() (T, bool)

Tail returns the value of the last element. This method is only for reading the last element, not for modification

type ChunkQueueIterator

type ChunkQueueIterator[T any] struct {
	// contains filtered or unexported fields
}

ChunkQueueIterator is the iterator type of ChunkQueue. Iterating ChunkQueue by iterators is not thread-safe. Manipulating invalid iterators may incur panics. Don't use an iterator of an element that has already been dequeued. Instead, use with checks and in loop. E.g.

for it := someQueue.First(); it.Valid(); it.Next() {
		... // operations cannot pop element
}

Note: Begin() and First() are interchangeable for it := someQueue.Begin(); it.Valid(); { // forwards

		it.Next()
		q.Pop() // can pop element
}

for it := someQueue.Last(); it.Valid(); it.Next() { // backwards

		...
}

for it := someQueue.End(); it.Prev(); { // backwards

		...
}

func (*ChunkQueueIterator[T]) Index

func (it *ChunkQueueIterator[T]) Index() int

Index returns the index of a valid iterator, and -1 otherwise. Attention: The time complexity is O(N). Please avoid using this method

func (*ChunkQueueIterator[T]) Next

func (it *ChunkQueueIterator[T]) Next() bool

Next updates the current iterator to its next iterator. It returns true if the next iterator is still in queue, and false otherwise. Calling Next for an invalid iterator is meaningless, and using invalid iterators may panic.

func (*ChunkQueueIterator[T]) Prev

func (it *ChunkQueueIterator[T]) Prev() bool

Prev updates the current to its previous iterator. It returns true if the next iterator is in queue, and false otherwise. The Prev of an end iterator points to the last element of the queue if the queue is not empty. The return boolean value is useful for backwards iteration. E.g. `for it := someQueue.Last(); it.Valid(); it.Next() {...}` `for it := someQueue.End(); it.Prev; {...} `

func (*ChunkQueueIterator[T]) Set

func (it *ChunkQueueIterator[T]) Set(v T)

Set replaces the element of the valid iterator. Panic for invalid iterators

func (*ChunkQueueIterator[T]) Valid

func (it *ChunkQueueIterator[T]) Valid() bool

Valid indicates if the element of the iterator is in queue

func (*ChunkQueueIterator[T]) Value

func (it *ChunkQueueIterator[T]) Value() T

Value returns the element value of a valid iterator which is in queue. It's meaningless and may panic otherwise.

Jump to

Keyboard shortcuts

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