diff --git a/policy/lfu/priotiry_queue.go b/policy/lfu/priority_queue.go similarity index 65% rename from policy/lfu/priotiry_queue.go rename to policy/lfu/priority_queue.go index 5379fa4..575890c 100644 --- a/policy/lfu/priotiry_queue.go +++ b/policy/lfu/priority_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[i].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) } 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) + } + }) + } +}