Skip to content

Commit 9f98378

Browse files
authored
Merge pull request #40 from chenyanchen/fix/priotiry_queue_swap
2 parents 0cefe16 + 20c3c20 commit 9f98378

File tree

2 files changed

+57
-17
lines changed

2 files changed

+57
-17
lines changed

policy/lfu/priotiry_queue.go renamed to policy/lfu/priority_queue.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,29 +40,29 @@ func newPriorityQueue[K comparable, V any](cap int) *priorityQueue[K, V] {
4040
// see example of priority queue: https://pkg.go.dev/container/heap
4141
var _ heap.Interface = (*priorityQueue[struct{}, interface{}])(nil)
4242

43-
func (l priorityQueue[K, V]) Len() int { return len(l) }
43+
func (q priorityQueue[K, V]) Len() int { return len(q) }
4444

45-
func (l priorityQueue[K, V]) Less(i, j int) bool {
46-
if l[i].referenceCount == l[j].referenceCount {
47-
return l[i].referencedAt.Before(l[j].referencedAt)
45+
func (q priorityQueue[K, V]) Less(i, j int) bool {
46+
if q[i].referenceCount == q[j].referenceCount {
47+
return q[i].referencedAt.Before(q[j].referencedAt)
4848
}
49-
return l[i].referenceCount < l[j].referenceCount
49+
return q[i].referenceCount < q[j].referenceCount
5050
}
5151

52-
func (l priorityQueue[K, V]) Swap(i, j int) {
53-
l[i], l[j] = l[j], l[i]
54-
l[i].index = i
55-
l[i].index = j
52+
func (q priorityQueue[K, V]) Swap(i, j int) {
53+
q[i], q[j] = q[j], q[i]
54+
q[i].index = i
55+
q[j].index = j
5656
}
5757

58-
func (l *priorityQueue[K, V]) Push(x interface{}) {
58+
func (q *priorityQueue[K, V]) Push(x interface{}) {
5959
entry := x.(*entry[K, V])
60-
entry.index = len(*l)
61-
*l = append(*l, entry)
60+
entry.index = len(*q)
61+
*q = append(*q, entry)
6262
}
6363

64-
func (l *priorityQueue[K, V]) Pop() interface{} {
65-
old := *l
64+
func (q *priorityQueue[K, V]) Pop() interface{} {
65+
old := *q
6666
n := len(old)
6767
entry := old[n-1]
6868
old[n-1] = nil // avoid memory leak
@@ -71,12 +71,12 @@ func (l *priorityQueue[K, V]) Pop() interface{} {
7171
for i := 0; i < len(new); i++ {
7272
new[i].index = i
7373
}
74-
*l = new
74+
*q = new
7575
return entry
7676
}
7777

78-
func (pq *priorityQueue[K, V]) update(e *entry[K, V], val V) {
78+
func (q *priorityQueue[K, V]) update(e *entry[K, V], val V) {
7979
e.val = val
8080
e.referenced()
81-
heap.Fix(pq, e.index)
81+
heap.Fix(q, e.index)
8282
}

policy/lfu/priority_queue_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package lfu
22

33
import (
44
"container/heap"
5+
"reflect"
56
"testing"
67
"time"
78
)
@@ -73,3 +74,42 @@ func TestPriorityQueue(t *testing.T) {
7374
t.Errorf("want %d, but got %d", want, got)
7475
}
7576
}
77+
78+
func Test_priorityQueue_Swap(t *testing.T) {
79+
type args struct {
80+
i int
81+
j int
82+
}
83+
type testCase[K comparable, V any] struct {
84+
name string
85+
q *priorityQueue[K, V]
86+
args args
87+
want *priorityQueue[K, V]
88+
}
89+
tests := []testCase[string, int]{
90+
{
91+
name: "swap case",
92+
q: func() *priorityQueue[string, int] {
93+
q := newPriorityQueue[string, int](10)
94+
q.Push(&entry[string, int]{index: 0})
95+
q.Push(&entry[string, int]{index: 1})
96+
return q
97+
}(),
98+
args: args{i: 0, j: 1},
99+
want: func() *priorityQueue[string, int] {
100+
q := newPriorityQueue[string, int](10)
101+
q.Push(&entry[string, int]{index: 1})
102+
q.Push(&entry[string, int]{index: 0})
103+
return q
104+
}(),
105+
},
106+
}
107+
for _, tt := range tests {
108+
t.Run(tt.name, func(t *testing.T) {
109+
tt.q.Swap(tt.args.i, tt.args.j)
110+
if !reflect.DeepEqual(tt.q, tt.want) {
111+
t.Errorf("want %v, got %v", tt.want, tt.q)
112+
}
113+
})
114+
}
115+
}

0 commit comments

Comments
 (0)