-
Notifications
You must be signed in to change notification settings - Fork 0
list #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
list #4
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,9 @@ | ||
| package hw04lrucache | ||
|
|
||
| import ( | ||
| "sync" | ||
| ) | ||
|
|
||
| type Key string | ||
|
|
||
| type Cache interface { | ||
|
|
@@ -9,8 +13,7 @@ type Cache interface { | |
| } | ||
|
|
||
| type lruCache struct { | ||
| Cache // Remove me after realization. | ||
|
|
||
| mu sync.Mutex | ||
| capacity int | ||
| queue List | ||
| items map[Key]*ListItem | ||
|
|
@@ -28,3 +31,57 @@ func NewCache(capacity int) Cache { | |
| items: make(map[Key]*ListItem, capacity), | ||
| } | ||
| } | ||
|
|
||
| func (cache lruCache) Set(key Key, value interface{}) bool { | ||
| cache.mu.Lock() | ||
| defer cache.mu.Unlock() | ||
|
|
||
| existedListItem, ok := cache.items[key] | ||
|
|
||
| if ok { | ||
| cache.queue.Remove(existedListItem) | ||
| } else { | ||
| length := cache.queue.Len() | ||
|
|
||
| if length == cache.capacity { | ||
| // remove the last item from the map and queue | ||
| lastItem := cache.queue.Back() | ||
| itemKey := lastItem.Value.(cacheItem).key | ||
| delete(cache.items, itemKey) | ||
| cache.queue.Remove(lastItem) | ||
| } | ||
| } | ||
|
|
||
| newListItem := cacheItem{key, value} | ||
| // add item to the front | ||
| cache.queue.PushFront(newListItem) | ||
| cache.items[key] = cache.queue.Front() | ||
|
|
||
| return ok | ||
| } | ||
|
|
||
| func (cache lruCache) Get(key Key) (interface{}, bool) { | ||
| cache.mu.Lock() | ||
| defer cache.mu.Unlock() | ||
|
|
||
| var result interface{} | ||
|
|
||
| value, ok := cache.items[key] | ||
|
|
||
| if ok { | ||
| cache.queue.MoveToFront(cache.items[key]) | ||
| result = value.Value.(cacheItem).value | ||
| } else { | ||
| result = nil | ||
| } | ||
|
|
||
| return result, ok | ||
| } | ||
|
|
||
| func (cache *lruCache) Clear() { | ||
| cache.mu.Lock() | ||
| defer cache.mu.Unlock() | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Отличное решение по очистке кеша за О(1)! |
||
| cache.queue = NewList() | ||
| cache.items = make(map[Key]*ListItem, cache.capacity) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,9 @@ | ||
| module github.com/fixme_my_friend/hw04_lru_cache | ||
| module hw04_lru_cache | ||
|
|
||
| go 1.16 | ||
|
|
||
| require ( | ||
| github.com/davecgh/go-spew v1.1.1 // indirect | ||
| github.com/stretchr/testify v1.7.0 | ||
| gopkg.in/yaml.v2 v2.4.0 // indirect | ||
| gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,13 @@ | ||
| github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= | ||
| github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
| github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
| github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
| github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
| github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
| github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
| github.com/stretchr/testify v1.5.0 h1:DMOzIV76tmoDNE9pX6RSN0aDtCYeCg5VueieJaAo1uw= | ||
| github.com/stretchr/testify v1.5.0/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= | ||
| github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= | ||
| github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
| gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
| gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
| gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= | ||
| gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= | ||
| gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= | ||
| gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= | ||
| gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= | ||
| gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= | ||
| gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,9 @@ | ||
| package hw04lrucache | ||
|
|
||
| import ( | ||
| "fmt" | ||
| ) | ||
|
|
||
| type List interface { | ||
| Len() int | ||
| Front() *ListItem | ||
|
|
@@ -8,6 +12,7 @@ type List interface { | |
| PushBack(v interface{}) *ListItem | ||
| Remove(i *ListItem) | ||
| MoveToFront(i *ListItem) | ||
| Print() | ||
| } | ||
|
|
||
| type ListItem struct { | ||
|
|
@@ -17,10 +22,123 @@ type ListItem struct { | |
| } | ||
|
|
||
| type list struct { | ||
| List // Remove me after realization. | ||
| // Place your code here. | ||
| firstNode *ListItem | ||
| lastNode *ListItem | ||
| len int | ||
| } | ||
|
|
||
| func NewList() List { | ||
| return new(list) | ||
| } | ||
|
|
||
| func (l list) Len() int { | ||
| return l.len | ||
| } | ||
|
|
||
| func (l list) Front() *ListItem { | ||
| if l.len > 0 { | ||
| return l.firstNode | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (l list) Back() *ListItem { | ||
| if l.len > 0 { | ||
| return l.lastNode | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (l *list) PushFront(v interface{}) *ListItem { | ||
| newItem := ListItem{v, nil, nil} | ||
|
|
||
| l.len++ | ||
|
|
||
| if l.len > 1 { | ||
| newItem.Next = l.Front() | ||
| l.firstNode.Prev = &newItem | ||
| } | ||
|
|
||
| l.firstNode = &newItem | ||
|
|
||
| if l.len == 1 { | ||
| l.lastNode = &newItem | ||
| } | ||
|
|
||
| return l.Front() | ||
| } | ||
|
|
||
| func (l *list) PushBack(v interface{}) *ListItem { | ||
| newItem := ListItem{v, nil, nil} | ||
|
|
||
| l.len++ | ||
|
|
||
| if l.len > 1 { | ||
| newItem.Prev = l.Back() | ||
| l.lastNode.Next = &newItem | ||
| } | ||
|
|
||
| l.lastNode = &newItem | ||
|
|
||
| if l.len == 1 { | ||
| l.firstNode = &newItem | ||
| } | ||
|
|
||
| return l.Back() | ||
| } | ||
|
|
||
| func (l *list) Remove(i *ListItem) { | ||
| l.len-- | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Инкрементить/декрементить счетчик лучше после добавления/удаления элемента, чтобы избежать ситуации, когда счетчик обновился, а изменений так и не произошло. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Для того, чтобы это было возможно реализовать, стоит завязать проверку условий ( |
||
|
|
||
| if l.Front() == i { | ||
| l.firstNode = i.Next | ||
| } | ||
|
|
||
| if l.Back() == i { | ||
| l.lastNode = i.Prev | ||
| } | ||
|
|
||
| if i.Prev != nil { | ||
| i.Prev.Next = i.Next | ||
| } | ||
|
|
||
| if i.Next != nil { | ||
| i.Next.Prev = i.Prev | ||
| } | ||
| } | ||
|
|
||
| func (l *list) MoveToFront(i *ListItem) { | ||
| if l.Front() == i { | ||
| return | ||
| } | ||
|
|
||
| if i.Prev != nil { | ||
| i.Prev.Next = i.Next | ||
| } | ||
|
|
||
| if i.Next != nil { | ||
| i.Next.Prev = i.Prev | ||
| } else { | ||
| l.lastNode = i.Prev | ||
| } | ||
|
|
||
| i.Prev = nil | ||
| i.Next = l.Front() | ||
| l.Front().Prev = i | ||
| l.firstNode = i | ||
| } | ||
|
|
||
| func (l list) Print() { | ||
| if l.firstNode == nil { | ||
| fmt.Println(nil) | ||
| return | ||
| } | ||
|
|
||
| i := l.Front() | ||
|
|
||
| for i != nil { | ||
| fmt.Println(i.Value) | ||
| i = i.Next | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
здесь можно немного упростить код и обойтись без введения дополнительной переменной result, просто вернув
nil, falseесли!ok