From ffe4195a734ec5955b48e8a6532bc50e71f450fb Mon Sep 17 00:00:00 2001 From: Yanchen Date: Wed, 10 May 2023 14:45:43 +0800 Subject: [PATCH 1/4] Fix Swap function for lfu.priorityQueue. --- policy/lfu/priotiry_queue.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/policy/lfu/priotiry_queue.go b/policy/lfu/priotiry_queue.go index 5379fa4..e3bf2e7 100644 --- a/policy/lfu/priotiry_queue.go +++ b/policy/lfu/priotiry_queue.go @@ -52,7 +52,7 @@ func (l priorityQueue[K, V]) Less(i, j int) bool { func (l priorityQueue[K, V]) Swap(i, j int) { l[i], l[j] = l[j], l[i] l[i].index = i - l[i].index = j + l[j].index = j } func (l *priorityQueue[K, V]) Push(x interface{}) { From 8b76a00e7d527efb5203cd753305c046aaf6fb89 Mon Sep 17 00:00:00 2001 From: Yanchen Date: Wed, 10 May 2023 14:52:14 +0800 Subject: [PATCH 2/4] Rename all priorityQueue receivers as q, the first alphabet of queue. --- policy/lfu/priotiry_queue.go | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/policy/lfu/priotiry_queue.go b/policy/lfu/priotiry_queue.go index e3bf2e7..575890c 100644 --- a/policy/lfu/priotiry_queue.go +++ b/policy/lfu/priotiry_queue.go @@ -40,29 +40,29 @@ func newPriorityQueue[K comparable, V any](cap int) *priorityQueue[K, V] { // see example of priority queue: https://pkg.go.dev/container/heap var _ heap.Interface = (*priorityQueue[struct{}, interface{}])(nil) -func (l priorityQueue[K, V]) Len() int { return len(l) } +func (q priorityQueue[K, V]) Len() int { return len(q) } -func (l priorityQueue[K, V]) Less(i, j int) bool { - if l[i].referenceCount == l[j].referenceCount { - return l[i].referencedAt.Before(l[j].referencedAt) +func (q priorityQueue[K, V]) Less(i, j int) bool { + if q[i].referenceCount == q[j].referenceCount { + return q[i].referencedAt.Before(q[j].referencedAt) } - return l[i].referenceCount < l[j].referenceCount + return q[i].referenceCount < q[j].referenceCount } -func (l priorityQueue[K, V]) Swap(i, j int) { - l[i], l[j] = l[j], l[i] - l[i].index = i - l[j].index = j +func (q priorityQueue[K, V]) Swap(i, j int) { + q[i], q[j] = q[j], q[i] + q[i].index = i + q[j].index = j } -func (l *priorityQueue[K, V]) Push(x interface{}) { +func (q *priorityQueue[K, V]) Push(x interface{}) { entry := x.(*entry[K, V]) - entry.index = len(*l) - *l = append(*l, entry) + entry.index = len(*q) + *q = append(*q, entry) } -func (l *priorityQueue[K, V]) Pop() interface{} { - old := *l +func (q *priorityQueue[K, V]) Pop() interface{} { + old := *q n := len(old) entry := old[n-1] old[n-1] = nil // avoid memory leak @@ -71,12 +71,12 @@ func (l *priorityQueue[K, V]) Pop() interface{} { for i := 0; i < len(new); i++ { new[i].index = i } - *l = new + *q = new return entry } -func (pq *priorityQueue[K, V]) update(e *entry[K, V], val V) { +func (q *priorityQueue[K, V]) update(e *entry[K, V], val V) { e.val = val e.referenced() - heap.Fix(pq, e.index) + heap.Fix(q, e.index) } From a0eadf25d38ea3747c7cf94e96ca23400a7087cd Mon Sep 17 00:00:00 2001 From: Yanchen Date: Wed, 10 May 2023 15:04:25 +0800 Subject: [PATCH 3/4] Rename file priotiry_queue.go to priority_queue.go. --- policy/lfu/{priotiry_queue.go => priority_queue.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename policy/lfu/{priotiry_queue.go => priority_queue.go} (100%) diff --git a/policy/lfu/priotiry_queue.go b/policy/lfu/priority_queue.go similarity index 100% rename from policy/lfu/priotiry_queue.go rename to policy/lfu/priority_queue.go From 20c3c203191a3ccc4338894f62bf7a8b60739b8f Mon Sep 17 00:00:00 2001 From: Yanchen Date: Wed, 10 May 2023 15:04:46 +0800 Subject: [PATCH 4/4] Add unit tests for priorityQueue.Swap. --- policy/lfu/priority_queue_test.go | 40 +++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/policy/lfu/priority_queue_test.go b/policy/lfu/priority_queue_test.go index 2a245f1..358f479 100644 --- a/policy/lfu/priority_queue_test.go +++ b/policy/lfu/priority_queue_test.go @@ -2,6 +2,7 @@ package lfu import ( "container/heap" + "reflect" "testing" "time" ) @@ -73,3 +74,42 @@ func TestPriorityQueue(t *testing.T) { t.Errorf("want %d, but got %d", want, got) } } + +func Test_priorityQueue_Swap(t *testing.T) { + type args struct { + i int + j int + } + type testCase[K comparable, V any] struct { + name string + q *priorityQueue[K, V] + args args + want *priorityQueue[K, V] + } + tests := []testCase[string, int]{ + { + name: "swap case", + q: func() *priorityQueue[string, int] { + q := newPriorityQueue[string, int](10) + q.Push(&entry[string, int]{index: 0}) + q.Push(&entry[string, int]{index: 1}) + return q + }(), + args: args{i: 0, j: 1}, + want: func() *priorityQueue[string, int] { + q := newPriorityQueue[string, int](10) + q.Push(&entry[string, int]{index: 1}) + q.Push(&entry[string, int]{index: 0}) + return q + }(), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tt.q.Swap(tt.args.i, tt.args.j) + if !reflect.DeepEqual(tt.q, tt.want) { + t.Errorf("want %v, got %v", tt.want, tt.q) + } + }) + } +}