38 lines
680 B
Go
38 lines
680 B
Go
// Copyright (c) 2013 CloudFlare, Inc.
|
|
|
|
// This code is based on golang example from "container/heap" package.
|
|
|
|
package lrucache
|
|
|
|
type priorityQueue []*entry
|
|
|
|
func (pq priorityQueue) Len() int {
|
|
return len(pq)
|
|
}
|
|
|
|
func (pq priorityQueue) Less(i, j int) bool {
|
|
return pq[i].expire.Before(pq[j].expire)
|
|
}
|
|
|
|
func (pq priorityQueue) Swap(i, j int) {
|
|
pq[i], pq[j] = pq[j], pq[i]
|
|
pq[i].index = i
|
|
pq[j].index = j
|
|
}
|
|
|
|
func (pq *priorityQueue) Push(e interface{}) {
|
|
n := len(*pq)
|
|
item := e.(*entry)
|
|
item.index = n
|
|
*pq = append(*pq, item)
|
|
}
|
|
|
|
func (pq *priorityQueue) Pop() interface{} {
|
|
old := *pq
|
|
n := len(old)
|
|
item := old[n-1]
|
|
item.index = -1
|
|
*pq = old[0 : n-1]
|
|
return item
|
|
}
|