Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/theodesp/go-shuffled-queue
A priority queue that shuffles elements with the same priority. Written in Go and inspired by https://github.com/mafintosh/shuffled-priority-queue.
https://github.com/theodesp/go-shuffled-queue
golang priority-queue queue random set
Last synced: about 1 month ago
JSON representation
A priority queue that shuffles elements with the same priority. Written in Go and inspired by https://github.com/mafintosh/shuffled-priority-queue.
- Host: GitHub
- URL: https://github.com/theodesp/go-shuffled-queue
- Owner: theodesp
- License: mit
- Created: 2017-05-30T16:57:30.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-10-09T09:33:20.000Z (about 6 years ago)
- Last Synced: 2024-10-03T07:21:59.017Z (about 2 months ago)
- Topics: golang, priority-queue, queue, random, set
- Language: Go
- Homepage:
- Size: 19.5 KB
- Stars: 6
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-shuffled-queue
A priority queue that shuffles elements with the same priority written in Go
and inspired by [shuffled-priority-queue](https://github.com/mafintosh/shuffled-priority-queue).## Install
`$ go get -u github.com/theodesp/go-shuffled-queue`
## Usage
```go
queue := shuffledQueue.NewSPQ()queue.Add("hello") // Default Priority is 0
queue.Add("world") // Default Priority is 0queue.AddWithPriority("welt", 1)
queue.AddWithPriority("verden", 2)
queue.AddWithPriority("verden", 3)fmt.Println(queue.Pop()) // returns "verden", true
fmt.Println(queue.Pop()) // returns "verden", true
fmt.Println(queue.Pop()) // returns "welt", true
fmt.Println(queue.Pop()) // returns "hello", true or "world", true
fmt.Println(queue.Pop()) // returns "hello", true or "world", true
fmt.Println(queue.Pop()) // returns nil, false```
## API
#### `queue := shuffledQueue.NewSPQ()`
Create a new queue.#### `value := queue.Add(value)`
Add a new value to the queue. Accepts single values. The value is returned for convenience. It also assigns it with a default priority.
#### `value := queue.AddWithPriority(value)`
Add a new PriorityQueueItem to the queue. The value is returned for convenience.
#### `value := queue.Remove(value)`
Remove a value from the queue.
#### `value := queue.Pop()`
Pop the value with the highest priority off the queue. If multiple values have the same priority a random one is popped.
If the queue is empty it will return a value of nil.#### `value := queue.Last()`
Same as Pop() but does not mutate the queue.
#### `value := queue.Shift()`
Same as Pop() but returns a value with the lowest priority.
#### `value := queue.First()`
Same as Shift() but does not mutate the queue.
## Licence
MIT @ 2017