-
Notifications
You must be signed in to change notification settings - Fork 0
HW4 is completed #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
Open
ezhk
wants to merge
11
commits into
master
Choose a base branch
from
hw04_lru_cache
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
762083e
HW4 is completed
d1a9359
LRU logic released
d641487
Optimize update value of exist cache element
7f76dd1
remove not used ()
0e014c4
update key type
1a7eb50
make cache goroutine safely
4e08912
Clear() test cases added
fa9f4ca
update gorouting count
480174f
codereview patches
a73ffc0
clean by creating new empty list
742ed02
update comments
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,82 @@ | ||
| package hw04_lru_cache //nolint:golint,stylecheck | ||
|
|
||
| import ( | ||
| "sync" | ||
| ) | ||
|
|
||
| type Key string | ||
|
|
||
| type Cache interface { | ||
| // Place your code here | ||
| Set(key Key, value interface{}) bool | ||
| Get(key Key) (interface{}, bool) | ||
| Clear() | ||
| } | ||
|
|
||
| type lruCache struct { | ||
| // Place your code here: | ||
| // - capacity | ||
| // - queue | ||
| // - items | ||
| sync.Mutex | ||
| capacity int | ||
| Queue List | ||
| Items map[Key]*listItem | ||
| } | ||
|
|
||
| type cacheItem struct { | ||
| // Place your code here | ||
| cKey Key | ||
| cValue interface{} | ||
| } | ||
|
|
||
| func NewCache(capacity int) Cache { | ||
| return &lruCache{} | ||
| return &lruCache{ | ||
| capacity: capacity, | ||
| Queue: NewList(), | ||
| Items: make(map[Key]*listItem), | ||
| } | ||
| } | ||
|
|
||
| func newItem(key Key, value interface{}) *cacheItem { | ||
| return &cacheItem{ | ||
| cKey: key, | ||
| cValue: value, | ||
| } | ||
| } | ||
|
|
||
| func (c *lruCache) Set(key Key, value interface{}) bool { | ||
| c.Lock() | ||
| defer c.Unlock() | ||
|
|
||
| if item, ok := c.Items[key]; ok { | ||
| item.Value.(*cacheItem).cValue = value | ||
| c.Queue.MoveToFront(item) | ||
| return true | ||
| } | ||
|
|
||
| for c.Queue.Len() >= c.capacity { | ||
| latestItem := c.Queue.Back() | ||
|
|
||
| c.Queue.Remove(latestItem) | ||
| delete(c.Items, latestItem.Value.(*cacheItem).cKey) | ||
| } | ||
|
|
||
| item := newItem(key, value) | ||
| c.Items[key] = c.Queue.PushFront(item) | ||
| return false | ||
| } | ||
|
|
||
| func (c *lruCache) Get(key Key) (interface{}, bool) { | ||
| c.Lock() | ||
| defer c.Unlock() | ||
|
|
||
| if item, ok := c.Items[key]; ok { | ||
| c.Queue.MoveToFront(item) | ||
| return item.Value.(*cacheItem).cValue, true | ||
| } | ||
|
|
||
| return nil, false | ||
| } | ||
|
|
||
| func (c *lruCache) Clear() { | ||
| c.Lock() | ||
| defer c.Unlock() | ||
|
|
||
| c.Queue = NewList() | ||
| c.Items = make(map[Key]*listItem) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| module github.com/fixme_my_friend/hw04_lru_cache | ||
| module github.com/ezhk/golang-learning/hw04_lru_cache | ||
|
|
||
| go 1.14 | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,105 @@ | ||
| package hw04_lru_cache //nolint:golint,stylecheck | ||
|
|
||
| type List interface { | ||
| // Place your code here | ||
| Len() int // длина списка | ||
| Front() *listItem // первый Item | ||
| Back() *listItem // последний Item | ||
| PushFront(v interface{}) *listItem // добавить значение в начало | ||
| PushBack(v interface{}) *listItem // добавить значение в конец | ||
| Remove(i *listItem) // удалить элемент | ||
| MoveToFront(i *listItem) // переместить элемент в начало | ||
| } | ||
|
|
||
| type listItem struct { | ||
| // Place your code here | ||
| Value interface{} // значение | ||
| Next *listItem // следующий элемент | ||
| Prev *listItem // предыдущий элемент | ||
| } | ||
|
|
||
| type list struct { | ||
| // Place your code here | ||
| // Length stay for back compatibility when we cannot use map | ||
| Length int | ||
|
|
||
| First *listItem | ||
| Last *listItem | ||
| } | ||
|
|
||
| func NewList() List { | ||
| return &list{} | ||
| } | ||
|
|
||
| func (l *list) Len() int { | ||
| return l.Length | ||
| } | ||
|
|
||
| func (l *list) Front() *listItem { | ||
| return l.First | ||
| } | ||
|
|
||
| func (l *list) Back() *listItem { | ||
| return l.Last | ||
| } | ||
|
|
||
| func (l *list) PushFront(v interface{}) *listItem { | ||
| switch l.Len() { | ||
| case 0: | ||
| l.First = &listItem{Value: v} | ||
| l.Last = l.First | ||
| default: | ||
| value := &listItem{ | ||
| Value: v, | ||
| Next: l.First, | ||
| } | ||
| l.First.Prev = value | ||
| l.First = value | ||
| } | ||
|
|
||
| l.Length++ | ||
| return l.First | ||
| } | ||
|
|
||
| func (l *list) PushBack(v interface{}) *listItem { | ||
| switch l.Len() { | ||
| case 0: | ||
| l.Last = &listItem{Value: v} | ||
| l.First = l.Last | ||
| default: | ||
| value := &listItem{ | ||
| Value: v, | ||
| Prev: l.Last, | ||
| } | ||
| l.Last.Next = value | ||
| l.Last = value | ||
| } | ||
|
|
||
| l.Length++ | ||
| return l.Last | ||
| } | ||
|
|
||
| func (l *list) Remove(i *listItem) { | ||
| switch { | ||
| // element in the middle | ||
| case i.Prev != nil && i.Next != nil: | ||
| i.Prev.Next = i.Next | ||
| i.Next.Prev = i.Prev | ||
| // element in the right/end | ||
| case i.Prev != nil: | ||
| l.Last = i.Prev | ||
| i.Prev.Next = nil | ||
| // element in the left/begin | ||
| case i.Next != nil: | ||
| l.First = i.Next | ||
| i.Next.Prev = nil | ||
| // stay only one element | ||
| default: | ||
| l.First = nil | ||
| l.Last = nil | ||
| } | ||
|
|
||
| l.Length-- | ||
| } | ||
|
|
||
| func (l *list) MoveToFront(i *listItem) { | ||
| l.Remove(i) | ||
| l.PushFront(i.Value) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Почему для map тут создается новый экзепляр, а для списка удаляются элементы? Чем обусловлен разный подход к чистке?
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.
Да, неоднозначно получилось, поправил во втором коммите.
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.
GC корректно отловит существующий список ни к чему не привязанный?
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.
Да, раз ссылок на объект не осталось, то GC его приберет.