Browse Source

init

master
Your Name 5 years ago
commit
6546085403
  1. 8
      .idea/.gitignore
  2. 9
      .idea/LRUCache.iml
  3. 8
      .idea/modules.xml
  4. 6
      .idea/vcs.xml
  5. 3
      go.mod
  6. 42
      lru.go

8
.idea/.gitignore

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/

9
.idea/LRUCache.iml

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

8
.idea/modules.xml

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/LRUCache.iml" filepath="$PROJECT_DIR$/.idea/LRUCache.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

3
go.mod

@ -0,0 +1,3 @@
module github.com/doublepi123/LRUCache
go 1.14

42
lru.go

@ -0,0 +1,42 @@
package LRUCache
import "container/list"
type item struct {
key string
value string
pos *list.Element
}
type LRU struct {
ul list.List
data map[string]*item
Maxsize int
}
func (lru LRU) Get(key string) string {
i := lru.data[key]
if i != nil {
lru.ul.Remove(i.pos)
lru.ul.PushBack(i)
return i.value
} else {
return ""
}
}
func (lru LRU) Set(key string, value string) {
i := &item{key: key, value: value}
lru.data[key] = i
lru.ul.Remove(i.pos)
lru.ul.PushBack(i)
if lru.ul.Len() > lru.Maxsize {
lru.ul.Remove(lru.ul.Front())
}
i.pos = lru.ul.Back()
}
func (lru LRU) init() {
lru.data = make(map[string]*item)
lru.ul = list.List{}
}
Loading…
Cancel
Save