queue

package module
v1.11.0 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2024 License: MIT Imports: 1 Imported by: 0

README

arrayQueue、circularBuffer、priorityQueue

Go Reference Go Report Card

Features

  • The data structure implemented using generics
  • Higher performance
  • arrayQueue is scalable, automatic expanding like slice

Quick Start

priorityQueue
go get -u github.com/doraemonkeys/queue/priorityQueue
package main

import (
	"fmt"

	pq "github.com/doraemonkeys/queue/priorityQueue"
)

func main() {
	q := pq.New(func(a, b int) bool {
		return a < b
	})
	q.Push(1)
	q.Push(5)
	q.Push(3)

	q2 := q.ToTopK(q.Len())
	q2.Push(7)
	for !q2.IsEmpty() {
		fmt.Println(q2.Pop())
	}
	// Output:
	// 3
	// 5
	// 7
}
arrayQueue
go get -u github.com/doraemonkeys/queue/arrayQueue
package main

import (
	"fmt"

	aq "github.com/doraemonkeys/queue/arrayQueue"
)

func main() {
	que := aq.New[int]()
	que.Push(1)
	que.Push(2)
	que.Push(3)
	que.Pop()
	que.Push(99)
	fmt.Println(que.Front())
	fmt.Println(que.Back())
	it := que.Iterator()
	for it.Next() {
		fmt.Println(it.Value())
	}
	// Output:
	// 2
	// 99
	// 2
	// 3
	// 99
}
circularBuffer
go get -u github.com/doraemonkeys/queue/circularBuffer
package main

import (
	"fmt"

	cb "github.com/doraemonkeys/queue/circularBuffer"
)

func main() {
	c := cb.New[int](3)
	c.PushBack(1)
	c.PushBack(2)
	c.PushBack(3)
	// 1 2 3
	c.PushBack(4)
	// 2 3 4
	c.PushFront(5)
	// 5 2 3
	it := c.Iterator()
	for it.Next() {
		fmt.Println(it.Value())
	}
	// Output:
	// 5
	// 2
	// 3
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func TimeComparator added in v1.7.1

func TimeComparator(a, b time.Time) int

TimeComparator provides a basic comparison on time.Time

Types

type Comparator added in v1.7.1

type Comparator[T any] func(x, y T) int

Comparator is a function that compares two elements. Returns -1 if x < y, 0 if x == y, 1 if x > y.

type Iterator

type Iterator[T any] interface {
	Begin()
	End()
	Next() bool
	Prev() bool
	Index() int
	Value() T
}

type LessFn added in v1.6.0

type LessFn[T any] func(a, b T) bool

LessFn is a function that returns whether 'a' is less than 'b'. Returns true if 'a' is less than 'b'.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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