pax_global_header00006660000000000000000000000064127235276310014522gustar00rootroot0000000000000052 comment=ab1073853bdeba13dfbd3bcd4330958e11bf0c2c receptor-0.0~git20160601.ab10738/000077500000000000000000000000001272352763100157125ustar00rootroot00000000000000receptor-0.0~git20160601.ab10738/.gitignore000066400000000000000000000004121272352763100176770ustar00rootroot00000000000000# Compiled Object files, Static and Dynamic libs (Shared Objects) *.o *.a *.so # Folders _obj _test # Architecture specific extensions/prefixes *.[568vq] [568vq].out *.cgo1.go *.cgo2.c _cgo_defun.c _cgo_gotypes.go _cgo_export.* _testmain.go *.exe *.test *.prof receptor-0.0~git20160601.ab10738/.travis.yml000066400000000000000000000016051272352763100200250ustar00rootroot00000000000000language: go sudo: false go: - tip - 1.6 before_install: - go get github.com/mattn/goveralls - go get golang.org/x/tools/cmd/cover script: - "$HOME/gopath/bin/goveralls -service=travis-ci" env: global: secure: gcKaprEPUAKU2qddYC9p2xzVPhjq2uY4fdTvKIt83wpVMnKApH6RRm+4foktosrNqmjXhGxEoyWuqzECwEpUlJAEfUZ3Viojp9BdSmtao+GdLddR4Ol2Jy7vufG3YiEsTorj7CGHmizPPOQcFnfdAswpl1zYY1QdVjjWy1s5hJNW0e3C77QtYpNjy5WETImxtLIBFSqhpRlm+t2224ZlEoZYwhouz43g7eN2I+efjDo+eP97Q3eoGIqa8pZIIlAxljxLUrgpL0+4n+6sl4maW7AXmYHRGljzPHaT//u5rN8iExW3mG48Gi8/yPJL1s0yicp50rxTi8+T2RzJnnpAqA0KNgi5SSP7Gv3CkhYRmsfCxrNhhmryVXiQpjXm9kHyxaz0AIrYOQbBprZP4nnLCgw8PgooZMDaksXLgNEcaKKs+DBR10fa/+Lm1ZaoAzSVcegxdJ92jhaSpPQYKws7ydFO7gjCxxuxeWVnp8jiBVr6ZZhx22L3Yl6KZRCvOZ6l7bsDQkFv4lL9OJB4CdbaeFoRMMPpS8hgQ5wsnKV6ZZMIeolJCDXuZaS51Hbfxnd7gvOO0fzEQsMMZxuFxINGmfgojKmJlzVeqGR5Vn3gkzE9jqIM767w6zNFct6oiZGyPsr1a9GRTy34dVMceEDidB12RB3kC74JZ+1bIlw7r9E= receptor-0.0~git20160601.ab10738/LICENSE000066400000000000000000000020671272352763100167240ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2016 Jupp Müller Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. receptor-0.0~git20160601.ab10738/README.md000066400000000000000000000012371272352763100171740ustar00rootroot00000000000000# go-priority-queue [![Build Status](https://travis-ci.org/jupp0r/go-priority-queue.svg?branch=master)](https://travis-ci.org/jupp0r/go-priority-queue) [![Coverage Status](https://coveralls.io/repos/github/jupp0r/go-priority-queue/badge.svg?branch=master)](https://coveralls.io/github/jupp0r/go-priority-queue?branch=master) [![Go Report Card](https://goreportcard.com/badge/github.com/jupp0r/go-priority-queue)](https://goreportcard.com/report/github.com/jupp0r/go-priority-queue) [![GoDoc](https://godoc.org/github.com/jupp0r/go-priority-queue?status.svg)](https://godoc.org/github.com/jupp0r/go-priority-queue) A priority queue implementation on top of container/heap receptor-0.0~git20160601.ab10738/priorty_queue.go000066400000000000000000000047211272352763100211610ustar00rootroot00000000000000// Package pq implements a priority queue data structure on top of container/heap. // As an addition to regular operations, it allows an update of an items priority, // allowing the queue to be used in graph search algorithms like Dijkstra's algorithm. // Computational complexities of operations are mainly determined by container/heap. // In addition, a map of items is maintained, allowing O(1) lookup needed for priority updates, // which themselves are O(log n). package pq import ( "container/heap" "errors" ) // PriorityQueue represents the queue type PriorityQueue struct { itemHeap *itemHeap lookup map[interface{}]*item } // New initializes an empty priority queue. func New() PriorityQueue { return PriorityQueue{ itemHeap: &itemHeap{}, lookup: make(map[interface{}]*item), } } // Len returns the number of elements in the queue. func (p *PriorityQueue) Len() int { return p.itemHeap.Len() } // Insert inserts a new element into the queue. No action is performed on duplicate elements. func (p *PriorityQueue) Insert(v interface{}, priority float64) { _, ok := p.lookup[v] if ok { return } newItem := &item{ value: v, priority: priority, } heap.Push(p.itemHeap, newItem) p.lookup[v] = newItem } // Pop removes the element with the highest priority from the queue and returns it. // In case of an empty queue, an error is returned. func (p *PriorityQueue) Pop() (interface{}, error) { if len(*p.itemHeap) == 0 { return nil, errors.New("empty queue") } item := heap.Pop(p.itemHeap).(*item) delete(p.lookup, item.value) return item.value, nil } // UpdatePriority changes the priority of a given item. // If the specified item is not present in the queue, no action is performed. func (p *PriorityQueue) UpdatePriority(x interface{}, newPriority float64) { item, ok := p.lookup[x] if !ok { return } item.priority = newPriority heap.Fix(p.itemHeap, item.index) } type itemHeap []*item type item struct { value interface{} priority float64 index int } func (ih *itemHeap) Len() int { return len(*ih) } func (ih *itemHeap) Less(i, j int) bool { return (*ih)[i].priority < (*ih)[j].priority } func (ih *itemHeap) Swap(i, j int) { (*ih)[i], (*ih)[j] = (*ih)[j], (*ih)[i] (*ih)[i].index = i (*ih)[j].index = j } func (ih *itemHeap) Push(x interface{}) { it := x.(*item) it.index = len(*ih) *ih = append(*ih, it) } func (ih *itemHeap) Pop() interface{} { old := *ih item := old[len(old)-1] *ih = old[0 : len(old)-1] return item } receptor-0.0~git20160601.ab10738/priorty_queue_test.go000066400000000000000000000033331272352763100222160ustar00rootroot00000000000000package pq import ( "sort" "testing" ) func TestPriorityQueue(t *testing.T) { pq := New() elements := []float64{5, 3, 7, 8, 6, 2, 9} for _, e := range elements { pq.Insert(e, e) } sort.Float64s(elements) for _, e := range elements { item, err := pq.Pop() if err != nil { t.Fatalf(err.Error()) } i := item.(float64) if e != i { t.Fatalf("expected %v, got %v", e, i) } } } func TestPriorityQueueUpdate(t *testing.T) { pq := New() pq.Insert("foo", 3) pq.Insert("bar", 4) pq.UpdatePriority("bar", 2) item, err := pq.Pop() if err != nil { t.Fatal(err.Error()) } if item.(string) != "bar" { t.Fatal("priority update failed") } } func TestPriorityQueueLen(t *testing.T) { pq := New() if pq.Len() != 0 { t.Fatal("empty queue should have length of 0") } pq.Insert("foo", 1) pq.Insert("bar", 1) if pq.Len() != 2 { t.Fatal("queue should have lenght of 2 after 2 inserts") } } func TestDoubleAddition(t *testing.T) { pq := New() pq.Insert("foo", 2) pq.Insert("bar", 3) pq.Insert("bar", 1) if pq.Len() != 2 { t.Fatal("queue should ignore inserting the same element twice") } item, _ := pq.Pop() if item.(string) != "foo" { t.Fatal("queue should ignore duplicate insert, not update existing item") } } func TestPopEmptyQueue(t *testing.T) { pq := New() _, err := pq.Pop() if err == nil { t.Fatal("should produce error when performing pop on empty queue") } } func TestUpdateNonExistingItem(t *testing.T) { pq := New() pq.Insert("foo", 4) pq.UpdatePriority("bar", 5) if pq.Len() != 1 { t.Fatal("update should not add items") } item, _ := pq.Pop() if item.(string) != "foo" { t.Fatalf("update should not overwrite item, expected \"foo\", got \"%v\"", item.(string)) } }