Use the standard library deque

Drops a dependency
This commit is contained in:
Kovid Goyal 2022-09-22 23:57:33 +05:30
parent 49f5f25fb9
commit cb2389efa5
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 5 additions and 9 deletions

1
go.mod
View File

@ -4,7 +4,6 @@ go 1.19
require (
github.com/ALTree/bigfloat v0.0.0-20220102081255-38c8b72a9924
github.com/gammazero/deque v0.2.0
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/google/uuid v1.3.0
github.com/jamesruan/go-rfc1924 v0.0.0-20170108144916-2767ca7c638f

2
go.sum
View File

@ -1,8 +1,6 @@
github.com/ALTree/bigfloat v0.0.0-20220102081255-38c8b72a9924 h1:DG4UyTVIujioxwJc8Zj8Nabz1L1wTgQ/xNBSQDfdP3I=
github.com/ALTree/bigfloat v0.0.0-20220102081255-38c8b72a9924/go.mod h1:+NaH2gLeY6RPBPPQf4aRotPPStg+eXc8f9ZaE4vRfD4=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/gammazero/deque v0.2.0 h1:SkieyNB4bg2/uZZLxvya0Pq6diUlwx7m2TeT7GAIWaA=
github.com/gammazero/deque v0.2.0/go.mod h1:LFroj8x4cMYCukHJDbxFCkT+r9AndaJnFMuZDV34tuU=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=

View File

@ -3,10 +3,9 @@
package utils
import (
"container/list"
"fmt"
"sync"
"github.com/gammazero/deque"
)
var _ = fmt.Print
@ -15,11 +14,11 @@ type LRUCache[K comparable, V any] struct {
data map[K]V
lock sync.RWMutex
max_size int
lru deque.Deque[K]
lru *list.List
}
func NewLRUCache[K comparable, V any](max_size int) *LRUCache[K, V] {
ans := LRUCache[K, V]{data: map[K]V{}, max_size: max_size}
ans := LRUCache[K, V]{data: map[K]V{}, max_size: max_size, lru: list.New()}
return &ans
}
@ -36,8 +35,8 @@ func (self *LRUCache[K, V]) GetOrCreate(key K, create func(key K) (V, error)) (V
self.data[key] = ans
self.lru.PushFront(key)
if self.max_size > 0 && self.lru.Len() > self.max_size {
k := self.lru.PopBack()
delete(self.data, k)
k := self.lru.Remove(self.lru.Back())
delete(self.data, k.(K))
}
self.lock.Unlock()
}