-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add version 2 with generics * Add another benchmark inspired by the original * Remove New in favor of variable initialization * Add golang version constraint in readme * Update README example
- Loading branch information
1 parent
0262637
commit b01cbdd
Showing
8 changed files
with
441 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,44 @@ | ||
## flatqueue-go [![Tests](https://github.com/invisiblefunnel/flatqueue-go/actions/workflows/go.yml/badge.svg)](https://github.com/invisiblefunnel/flatqueue-go/actions/workflows/go.yml) | ||
|
||
A Go port of the [flatqueue](https://github.com/mourner/flatqueue) priority queue library. Push items by identifier (`int`) and value (`float64`). | ||
A Go 1.18+ port of the [flatqueue](https://github.com/mourner/flatqueue) priority queue library using generics. | ||
|
||
`Peek`, `PeekValue`, and `Pop` will panic if called on an empty queue. You must check `Len` accordingly. | ||
|
||
```go | ||
q := flatqueue.New() | ||
package main | ||
|
||
for i, item := range items { | ||
q.Push(i, item.Value) | ||
import "github.com/invisiblefunnel/flatqueue-go/v2" | ||
|
||
type Item struct { | ||
Name string | ||
Value int | ||
} | ||
|
||
var ( | ||
id int | ||
value float64 | ||
) | ||
func main() { | ||
items := []Item{ | ||
{"X", 5}, | ||
{"Y", 2}, | ||
{"Z", 3}, | ||
} | ||
|
||
id = q.Peek() // top item index | ||
value = q.PeekValue() // top item value | ||
id = q.Pop() // remove and return the top item index | ||
var q flatqueue.FlatQueue[Item, int] | ||
|
||
// loop while queue is not empty | ||
for q.Len() > 0 { | ||
q.Pop() | ||
} | ||
``` | ||
for _, item := range items { | ||
q.Push(item, item.Value) | ||
} | ||
|
||
Specifying an initial capacity for the underlying slices may improve the performance of `Push` if you know, or can estimate, the maximum length of the queue. This does not limit the length of the queue. | ||
var ( | ||
item Item | ||
value int | ||
) | ||
|
||
```go | ||
q := flatqueue.NewWithCapacity(len(items)) | ||
item = q.Peek() // top item | ||
value = q.PeekValue() // top item value | ||
item = q.Pop() // remove and return the top item | ||
|
||
for i, item := range items { | ||
q.Push(i, item.Value) | ||
// loop while the queue is not empty | ||
for q.Len() > 0 { | ||
q.Pop() | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
## flatqueue-go [![Tests](https://github.com/invisiblefunnel/flatqueue-go/actions/workflows/go.yml/badge.svg)](https://github.com/invisiblefunnel/flatqueue-go/actions/workflows/go.yml) | ||
|
||
A Go port of the [flatqueue](https://github.com/mourner/flatqueue) priority queue library. Push items by identifier (`int`) and value (`float64`). | ||
|
||
`Peek`, `PeekValue`, and `Pop` will panic if called on an empty queue. You must check `Len` accordingly. | ||
|
||
```go | ||
q := flatqueue.New() | ||
|
||
for i, item := range items { | ||
q.Push(i, item.Value) | ||
} | ||
|
||
var ( | ||
id int | ||
value float64 | ||
) | ||
|
||
id = q.Peek() // top item index | ||
value = q.PeekValue() // top item value | ||
id = q.Pop() // remove and return the top item index | ||
|
||
// loop while queue is not empty | ||
for q.Len() > 0 { | ||
q.Pop() | ||
} | ||
``` | ||
|
||
Specifying an initial capacity for the underlying slices may improve the performance of `Push` if you know, or can estimate, the maximum length of the queue. This does not limit the length of the queue. | ||
|
||
```go | ||
q := flatqueue.NewWithCapacity(len(items)) | ||
|
||
for i, item := range items { | ||
q.Push(i, item.Value) | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package flatqueue | ||
|
||
import ( | ||
"golang.org/x/exp/constraints" | ||
) | ||
|
||
type FlatQueue[T any, V constraints.Ordered] struct { | ||
items []T | ||
values []V | ||
length int | ||
} | ||
|
||
func (q *FlatQueue[_, _]) Len() int { | ||
return q.length | ||
} | ||
|
||
func (q *FlatQueue[T, V]) Push(item T, value V) { | ||
pos := q.length | ||
q.length++ | ||
|
||
q.items = append(q.items, item) | ||
q.values = append(q.values, value) | ||
|
||
for pos > 0 { | ||
parent := (pos - 1) >> 1 | ||
parentValue := q.values[parent] | ||
|
||
if value > parentValue { | ||
break | ||
} | ||
|
||
q.items[pos] = q.items[parent] | ||
q.values[pos] = parentValue | ||
|
||
pos = parent | ||
} | ||
|
||
q.items[pos] = item | ||
q.values[pos] = value | ||
} | ||
|
||
func (q *FlatQueue[T, _]) Pop() T { | ||
top := q.items[0] | ||
q.length-- | ||
|
||
if q.length > 0 { | ||
id := q.items[q.length] | ||
value := q.values[q.length] | ||
|
||
q.items[0] = id | ||
q.values[0] = value | ||
|
||
halfLength := q.length >> 1 | ||
pos := 0 | ||
|
||
for pos < halfLength { | ||
left := (pos << 1) + 1 | ||
right := left + 1 | ||
|
||
bestIndex := q.items[left] | ||
bestValue := q.values[left] | ||
rightValue := q.values[right] | ||
|
||
if right < q.length && rightValue < bestValue { | ||
left = right | ||
bestIndex = q.items[right] | ||
bestValue = rightValue | ||
} | ||
|
||
if bestValue > value { | ||
break | ||
} | ||
|
||
q.items[pos] = bestIndex | ||
q.values[pos] = bestValue | ||
pos = left | ||
} | ||
|
||
q.items[pos] = id | ||
q.values[pos] = value | ||
} | ||
|
||
return top | ||
} | ||
|
||
func (q *FlatQueue[T, _]) Peek() T { | ||
return q.items[0] | ||
} | ||
|
||
func (q *FlatQueue[_, V]) PeekValue() V { | ||
return q.values[0] | ||
} |
Oops, something went wrong.