Skip to content

🦄 LRU cache for Go @bilibili. (beats 100% submissions @ leetcode)

License

Notifications You must be signed in to change notification settings

vurtneyang/lrucache

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cache.go

license Go Report Card

Result at leetcode.com

Usage

	lc := lrucache.New(5)
	lc.Put(4, "1")
	lc.Put(1, "2")
	lc.Put(2, "3")
	lc.Put(3, "4")
	lc.Put(4, "5")

	fmt.Println("Range demo:")
	i := 1
	lc.Range(func(key, value interface{}) bool {
		fmt.Printf("[%d] %d => %s\r\n", i, key.(int), value.(string))
		i++
		return true
	})

	fmt.Println("Get demo:")
	if e, ok := lc.Get(1); ok {
		fmt.Printf("%d => %s\r\n", 1, e.(string))
	}

	fmt.Println("Reverse iteration after Get demo:")
	i = 1
	for e := lc.Back(); e != nil; e = e.Prev() {
		fmt.Printf("[%d] %d => %s\r\n", lc.Len()-i+1, e.Key.(int), e.Value.(string))
		i++
	}

	lc.Delete(4)
	fmt.Println("Iteration after Delete demo:")
	i = 1
	for e := lc.Front(); e != nil; e = e.Next() {
		fmt.Printf("[%d] %d => %s\r\n", i, e.Key.(int), e.Value.(string))
		i++
	}

Output:


Range demo:
[1] 4 => 5
[2] 3 => 4
[3] 2 => 3
[4] 1 => 2
Get demo:
1 => 2
Reverse iteration after Get demo:
[4] 2 => 3
[3] 3 => 4
[2] 4 => 5
[1] 1 => 2
Iteration after Delete demo:
[1] 1 => 2
[2] 3 => 4
[3] 2 => 3

About

🦄 LRU cache for Go @bilibili. (beats 100% submissions @ leetcode)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%