Skip to content

An fast, thread-safe implementation of Priority Queue in Golang

License

Notifications You must be signed in to change notification settings

xybor-x/priority_queue

Repository files navigation

xybor founder Go Reference GitHub Repo stars GitHub top language GitHub go.mod Go version GitHub release (release name instead of tag name) Codacy Badge Codacy Badge Go Report

Introduction

A fast, thread-safe implementation of Priority Queue in Golang

Get started

pqueue := priorityqueue.Default[int]()

// Setup priority level.
pqueue.SetPriority(Urgent, 0)
pqueue.SetPriority(Necessary, 5)
pqueue.SetPriority(Lazy, 10)
pqueue.SetPriority(Background, 100)

// Specially for the Necessary level, after 500 miliseconds since the element is
// enqueued into the queue, its priority level will be raised up.
pqueue.SetAgingTimeSlice(Necessary, 500*time.Millisecond)

// Enqueue three elements to Lazy level.
pqueue.Enqueue(Lazy, 4, 5, 6)

// Enqueue an element to Urgent level.
pqueue.Enqueue(Urgent, 1)

// Enqueue two elements to Necessary level.
pqueue.Enqueue(Necessary, 2, 3)

// Use Dequeue() to get the first highest priority element.
v, err := pqueue.Dequeue()
assert.NoError(t, err)
assert.Equal(t, 1, v.To())

// Or use JustDequeue(), a shortcut of Dequeue().
assert.Equal(t, 2, pqueue.JustDequeue().To())
assert.Equal(t, 3, pqueue.JustDequeue().To())
assert.Equal(t, 4, pqueue.JustDequeue().To())
assert.Equal(t, 5, pqueue.JustDequeue().To())
assert.Equal(t, 6, pqueue.JustDequeue().To())
assert.Nil(t, pqueue.JustDequeue())

About

An fast, thread-safe implementation of Priority Queue in Golang

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages