Skip to content

A simple and generic priority queue implementation in Golang.

License

Notifications You must be signed in to change notification settings

alexanderbez/gopq

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gopq

GoDoc Build Status Go Report Card

A simple and generic priority queue implementation in Golang.

API

Any such type that implements the Heapable interface may be used in a PriorityQueue. The underlying element essentially must know how to give priority when compared to another element of the same type.

If the priority queue does not require modifications (i.e. updates or removals), the type implementing Heapable does not need to support indexing.

type myType struct {
	priority int
}

func (mt *myType) Index() (i int) { return }
func (mt *myType) SetIndex(_ int) {}

func (mt *myType) Priority(other interface{}) bool {
	if t, ok := other.(*myType); ok {
		return th.priority > t.priority
	}

	return false
}

pq := NewPriorityQueue()

pq.Push(&myType{priority: 1})
pq.Push(&myType{priority: 3})
pq.Push(&myType{priority: 2})

for pq.Size() != 0 {
    res, err := pq.Pop()

    // ...
}

Tests

$ go test -v ./...

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b feature/my-new-feature)
  3. Commit your changes (git commit -m 'Add some feature')
  4. Push to the branch (git push origin feature/my-new-feature)
  5. Create a new Pull Request

About

A simple and generic priority queue implementation in Golang.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages