Skip to content

Commit

Permalink
Refactoring code
Browse files Browse the repository at this point in the history
  • Loading branch information
jimchen committed Aug 22, 2024
1 parent 8bd5b76 commit 5bd2c25
Show file tree
Hide file tree
Showing 15 changed files with 731 additions and 493 deletions.
50 changes: 36 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![Codecov Badge]][coverage status]
[![License Badge]][license]

An implementation of a LRU cache. The cache supports `Put`, `Get` `Peek` and `Pop` operations,
An implementation of a LRU cache. The cache supports `Push`, `Put`, `Get` `Peek` and `Pop` operations,
all of which are O(1). This package was heavily influenced by the [LRU Cache implementation in a Rust crate].

## Example
Expand All @@ -15,25 +15,47 @@ Below is a simple example of how to instantiate and use a LRU cache.
package main

import (
"fmt"
"github.com/lovelysunlight/lru-go"
"fmt"

"github.com/lovelysunlight/lru-go"
)

func main() {
cache := lru.New[string, string](2)
cache.Put("apple", "red")
cache.Put("banana", "yellow")
cache, _ := lru.New[string, string](2)
cache.Put("apple", "red")
cache.Put("banana", "yellow")

var (
r, v string
ok bool
)

r, ok = cache.Get("apple")
fmt.Printf("Get() found: %v, value: %q\n", ok, r)

r, ok = cache.Get("banana")
fmt.Printf("Get() found: %v, value: %q\n", ok, r)

r, ok = cache.Get("pear")
fmt.Printf("Get() found: %v, value: %q\n", ok, r)

r, ok = cache.Peek("apple")
fmt.Printf("Peek() found: %v, value: %q\n", ok, r)

r, ok = cache.Peek("banana")
fmt.Printf("Peek() found: %v, value: %q\n", ok, r)

r, ok = cache.Peek("pear")
fmt.Printf("Peek() found: %v, value: %q\n", ok, r)

fmt.Println(cache.get("apple").Unwrap()) // "red"
fmt.Println(cache.get("banana").Unwrap()) // "yellow"
fmt.Println(cache.get("pear").IsSome()) // false
r, ok = cache.Pop("banana")
fmt.Printf("Pop() found: %v, value: %q\n", ok, r)

fmt.Println(cache.put("banana", "foo").Unwrap()) // "yellow"
fmt.Println(cache.put("pear", "bar").IsSome()) // false
r, v, ok = cache.RemoveOldest()
fmt.Printf("RemoveOldest() found: %v, key: %q, value: %q\n", ok, r, v)

fmt.Println(cache.get("pear").Unwrap()) // "bar"
fmt.Println(cache.get("banana").Unwrap()) // "foo"
fmt.Println(cache.get("apple").IsSome()) // false
fmt.Printf("Len() = : %v\n", cache.Len())
fmt.Printf("Cap() = : %v\n", cache.Cap())
}
```

Expand Down
90 changes: 0 additions & 90 deletions entry.go

This file was deleted.

45 changes: 45 additions & 0 deletions example/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"fmt"

"github.com/lovelysunlight/lru-go"
)

func main() {
cache, _ := lru.New[string, string](2)
cache.Put("apple", "red")
cache.Put("banana", "yellow")

var (
r, v string
ok bool
)

r, ok = cache.Get("apple")
fmt.Printf("Get() found: %v, value: %q\n", ok, r)

r, ok = cache.Get("banana")
fmt.Printf("Get() found: %v, value: %q\n", ok, r)

r, ok = cache.Get("pear")
fmt.Printf("Get() found: %v, value: %q\n", ok, r)

r, ok = cache.Peek("apple")
fmt.Printf("Peek() found: %v, value: %q\n", ok, r)

r, ok = cache.Peek("banana")
fmt.Printf("Peek() found: %v, value: %q\n", ok, r)

r, ok = cache.Peek("pear")
fmt.Printf("Peek() found: %v, value: %q\n", ok, r)

r, ok = cache.Pop("banana")
fmt.Printf("Pop() found: %v, value: %q\n", ok, r)

r, v, ok = cache.RemoveOldest()
fmt.Printf("RemoveOldest() found: %v, key: %q, value: %q\n", ok, r, v)

fmt.Printf("Len() = : %v\n", cache.Len())
fmt.Printf("Cap() = : %v\n", cache.Cap())
}
16 changes: 10 additions & 6 deletions internal/hashmap/hashmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ type Map[K comparable, V any] struct {
inner map[K]V
}

func New[K comparable, V any]() Map[K, V] {
return Map[K, V]{inner: make(map[K]V)}
func New[K comparable, V any]() *Map[K, V] {
return &Map[K, V]{inner: make(map[K]V)}
}

func (m Map[K, V]) Get(k K) (V, bool) {
func (m *Map[K, V]) Get(k K) (V, bool) {
v, ok := m.inner[k]
return v, ok
}

func (m Map[K, V]) Set(k K, v V) {
func (m *Map[K, V]) Set(k K, v V) {
m.inner[k] = v
}

func (m Map[K, V]) Remove(k K) (V, bool) {
func (m *Map[K, V]) Remove(k K) (V, bool) {
if v, ok := m.Get(k); ok {
delete(m.inner, k)
return v, true
Expand All @@ -26,6 +26,10 @@ func (m Map[K, V]) Remove(k K) (V, bool) {
return v, false
}

func (m Map[K, V]) Len() int {
func (m *Map[K, V]) Clear() {
m.inner = make(map[K]V)
}

func (m *Map[K, V]) Len() int {
return len(m.inner)
}
11 changes: 11 additions & 0 deletions internal/hashmap/hashmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,14 @@ func TestMap_Remove(t *testing.T) {
assert.False(t, exists)
assert.EqualValues(t, "", got)
}

func TestMap_Clear(t *testing.T) {
m := New[string, string]()
m.Set("a", "aa")
m.Set("b", "bb")

assert.EqualValues(t, 2, m.Len())

m.Clear()
assert.EqualValues(t, 0, m.Len())
}
40 changes: 0 additions & 40 deletions internal/option/option.go

This file was deleted.

63 changes: 0 additions & 63 deletions internal/option/option_test.go

This file was deleted.

Loading

0 comments on commit 5bd2c25

Please sign in to comment.