-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprqueue.go
More file actions
94 lines (79 loc) · 1.6 KB
/
prqueue.go
File metadata and controls
94 lines (79 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Package prqueue implements a priority queue with generics and ability to send comparator-func as a constructor argument.
//
// For documentation expand section above.
package prqueue
import (
"container/heap"
"errors"
"fmt"
"sync"
)
var ErrEmptyQueue = errors.New("priority queue is empty")
type pqs[T any] struct {
mu sync.RWMutex
list []T
cmp func(a, b T) bool
}
func New[T any](cmp func(a, b T) bool, c ...int) (pq *pqs[T]) {
pq = &pqs[T]{cmp: cmp}
if len(c) != 0 {
pq.list = make([]T, 0, c[0])
} else {
pq.list = make([]T, 0)
}
return
}
func (pq *pqs[T]) Add(el T) {
pq.mu.Lock()
defer pq.mu.Unlock()
heap.Push(pq, el)
}
func (pq *pqs[T]) Poll() (el T, er error) {
pq.mu.Lock()
defer pq.mu.Unlock()
if len(pq.list) > 0 {
el = heap.Pop(pq).(T)
return
}
er = ErrEmptyQueue
return
}
func (pq *pqs[T]) Peek() (el T, er error) {
pq.mu.RLock()
defer pq.mu.RUnlock()
if len(pq.list) > 0 {
el = pq.list[0]
return
}
er = ErrEmptyQueue
return
}
func (pq *pqs[T]) IsEmpty() bool {
pq.mu.RLock()
defer pq.mu.RUnlock()
return len(pq.list) == 0
}
func (pq *pqs[T]) String() string {
return fmt.Sprintf("%v", pq.list)
}
//================================
func (pq *pqs[T]) Push(e any) {
pq.list = append(pq.list, e.(T))
}
func (pq *pqs[T]) Len() int {
return len(pq.list)
}
func (pq *pqs[T]) Less(i, j int) bool {
return pq.cmp(pq.list[i], pq.list[j])
}
func (pq *pqs[T]) Swap(i, j int) {
pq.list[i], pq.list[j] = pq.list[j], pq.list[i]
}
func (pq *pqs[T]) Pop() (e any) {
lidx := len(pq.list) - 1
e = pq.list[lidx]
var tmp T
pq.list[lidx] = tmp
pq.list = pq.list[:lidx]
return
}