Extract thirdparty/pq to go-ipfs-pq
This moves the `thirdparty/pq` package to https://github.com/ipfs/go-ipfs-pq . History has been retained. The new package has been gx'ed and published. Imports have been updated accordingly. License: MIT Signed-off-by: Hector Sanjuan <hector@protocol.ai>
Hector Sanjuan committed
Feb 15, 2018 at 22:53 UTC
9b6a63e768f3c8c3e5417fbd48fd3073090527c0
4 files changed
+7
-191
exchange/bitswap/decision/peer_request_queue.go
+1
-1
@@ -5,8 +5,8 @@ import (
5
"time"
6
7
wantlist "github.com/ipfs/go-ipfs/exchange/bitswap/wantlist"
8
- pq "github.com/ipfs/go-ipfs/thirdparty/pq"
8
9
+ pq "gx/ipfs/QmZUbTDJ39JpvtFCSubiWeUTQRvMA1tVE5RZCJrY4oeAsC/go-ipfs-pq"
10
peer "gx/ipfs/QmZoWKhxUmZ2seW4BzX6fJkNR8hh9PsGModr7q171yq2SS/go-libp2p-peer"
11
cid "gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid"
12
)
package.json
+6
@@ -546,6 +546,12 @@
546
"name": "go-ipfs-flags",
547
"version": "0.0.1"
548
},
549
+ {
550
+ "author": "hsanjuan",
551
+ "hash": "QmZUbTDJ39JpvtFCSubiWeUTQRvMA1tVE5RZCJrY4oeAsC",
552
+ "name": "go-ipfs-pq",
553
+ "version": "0.0.1"
554
+ },
555
{
556
"author": "hsanjuan",
557
"hash": "QmdQTPWduSeyveSxeCAte33M592isSW5Z979g81aJphrgn",
thirdparty/pq/container.go
deleted
-105
@@ -1,105 +0,0 @@
1
-package pq
2
-
3
-import "container/heap"
4
-
5
-// PQ is a basic priority queue.
6
-type PQ interface {
7
- // Push adds the ele
8
- Push(Elem)
9
- // Pop returns the highest priority Elem in PQ.
10
- Pop() Elem
11
- // Len returns the number of elements in the PQ.
12
- Len() int
13
- // Update `fixes` the PQ.
14
- Update(index int)
15
-
16
- // TODO explain why this interface should not be extended
17
- // It does not support Remove. This is because...
18
-}
19
-
20
-// Elem describes elements that can be added to the PQ. Clients must implement
21
-// this interface.
22
-type Elem interface {
23
- // SetIndex stores the int index.
24
- SetIndex(int)
25
- // Index returns the last given by SetIndex(int).
26
- Index() int
27
-}
28
-
29
-// ElemComparator returns true if pri(a) > pri(b)
30
-type ElemComparator func(a, b Elem) bool
31
-
32
-// New creates a PQ with a client-supplied comparator.
33
-func New(cmp ElemComparator) PQ {
34
- q := &wrapper{heapinterface{
35
- elems: make([]Elem, 0),
36
- cmp: cmp,
37
- }}
38
- heap.Init(&q.heapinterface)
39
- return q
40
-}
41
-
42
-// wrapper exists because we cannot re-define Push. We want to expose
43
-// Push(Elem) but heap.Interface requires Push(interface{})
44
-type wrapper struct {
45
- heapinterface
46
-}
47
-
48
-var _ PQ = &wrapper{}
49
-
50
-func (w *wrapper) Push(e Elem) {
51
- heap.Push(&w.heapinterface, e)
52
-}
53
-
54
-func (w *wrapper) Pop() Elem {
55
- return heap.Pop(&w.heapinterface).(Elem)
56
-}
57
-
58
-func (w *wrapper) Update(index int) {
59
- heap.Fix(&w.heapinterface, index)
60
-}
61
-
62
-// heapinterface handles dirty low-level details of managing the priority queue.
63
-type heapinterface struct {
64
- elems []Elem
65
- cmp ElemComparator
66
-}
67
-
68
-var _ heap.Interface = &heapinterface{}
69
-
70
-// public interface
71
-
72
-func (q *heapinterface) Len() int {
73
- return len(q.elems)
74
-}
75
-
76
-// Less delegates the decision to the comparator
77
-func (q *heapinterface) Less(i, j int) bool {
78
- return q.cmp(q.elems[i], q.elems[j])
79
-}
80
-
81
-// Swap swaps the elements with indexes i and j.
82
-func (q *heapinterface) Swap(i, j int) {
83
- q.elems[i], q.elems[j] = q.elems[j], q.elems[i]
84
- q.elems[i].SetIndex(i)
85
- q.elems[j].SetIndex(j)
86
-}
87
-
88
-// Note that Push and Pop in this interface are for package heap's
89
-// implementation to call. To add and remove things from the heap, wrap with
90
-// the pq struct to call heap.Push and heap.Pop.
91
-
92
-func (q *heapinterface) Push(x interface{}) { // where to put the elem?
93
- t := x.(Elem)
94
- t.SetIndex(len(q.elems))
95
- q.elems = append(q.elems, t)
96
-}
97
-
98
-func (q *heapinterface) Pop() interface{} {
99
- old := q.elems
100
- n := len(old)
101
- elem := old[n-1] // remove the last
102
- elem.SetIndex(-1) // for safety // FIXME why?
103
- q.elems = old[0 : n-1] // shrink
104
- return elem
105
-}
thirdparty/pq/container_test.go
deleted
-85
@@ -1,85 +0,0 @@
1
-package pq
2
-
3
-import (
4
- "sort"
5
- "testing"
6
-)
7
-
8
-type TestElem struct {
9
- Key string
10
- Priority int
11
- index int
12
-}
13
-
14
-func (e *TestElem) Index() int {
15
- return e.index
16
-}
17
-
18
-func (e *TestElem) SetIndex(i int) {
19
- e.index = i
20
-}
21
-
22
-var PriorityComparator = func(i, j Elem) bool {
23
- return i.(*TestElem).Priority > j.(*TestElem).Priority
24
-}
25
-
26
-func TestQueuesReturnTypeIsSameAsParameterToPush(t *testing.T) {
27
- q := New(PriorityComparator)
28
- expectedKey := "foo"
29
- elem := &TestElem{Key: expectedKey}
30
- q.Push(elem)
31
- switch v := q.Pop().(type) {
32
- case *TestElem:
33
- if v.Key != expectedKey {
34
- t.Fatal("the key doesn't match the pushed value")
35
- }
36
- default:
37
- t.Fatal("the queue is not casting values appropriately")
38
- }
39
-}
40
-
41
-func TestCorrectnessOfPop(t *testing.T) {
42
- q := New(PriorityComparator)
43
- tasks := []TestElem{
44
- {Key: "a", Priority: 9},
45
- {Key: "b", Priority: 4},
46
- {Key: "c", Priority: 3},
47
- {Key: "d", Priority: 0},
48
- {Key: "e", Priority: 6},
49
- }
50
- for _, e := range tasks {
51
- q.Push(&e)
52
- }
53
- var priorities []int
54
- for q.Len() > 0 {
55
- i := q.Pop().(*TestElem).Priority
56
- t.Log("popped %v", i)
57
- priorities = append(priorities, i)
58
- }
59
- if !sort.IntsAreSorted(priorities) {
60
- t.Fatal("the values were not returned in sorted order")
61
- }
62
-}
63
-
64
-func TestUpdate(t *testing.T) {
65
- t.Log(`
66
- Add 3 elements.
67
- Update the highest priority element to have the lowest priority and fix the queue.
68
- It should come out last.`)
69
- q := New(PriorityComparator)
70
- lowest := &TestElem{Key: "originallyLowest", Priority: 1}
71
- middle := &TestElem{Key: "originallyMiddle", Priority: 2}
72
- highest := &TestElem{Key: "toBeUpdated", Priority: 3}
73
- q.Push(middle)
74
- q.Push(highest)
75
- q.Push(lowest)
76
- if q.Pop().(*TestElem).Key != highest.Key {
77
- t.Fatal("popped element doesn't have the highest priority")
78
- }
79
- q.Push(highest) // re-add the popped element
80
- highest.Priority = 0 // update the PQ
81
- q.Update(highest.Index()) // fix the PQ
82
- if q.Pop().(*TestElem).Key != middle.Key {
83
- t.Fatal("middle element should now have the highest priority")
84
- }
85
-}