|
| 1 | +package memoization |
| 2 | + |
| 3 | +import ( |
| 4 | + "container/list" |
| 5 | + "crypto/sha256" |
| 6 | + "fmt" |
| 7 | + "sync" |
| 8 | +) |
| 9 | + |
| 10 | +// Hasher is an interface that requires a Hash method. The Hash method is |
| 11 | +// expected to return a string representation of the hash of the object. |
| 12 | +type Hasher interface { |
| 13 | + Hash() string |
| 14 | +} |
| 15 | + |
| 16 | +// entry is a struct that holds a key-value pair. It is used as an element |
| 17 | +// in the evictionList of the MemoCache. |
| 18 | +type entry[T any] struct { |
| 19 | + key string |
| 20 | + value T |
| 21 | +} |
| 22 | + |
| 23 | +// MemoCache is a struct that represents a cache with a set capacity. It |
| 24 | +// uses an LRU (Least Recently Used) eviction policy. It is safe for |
| 25 | +// concurrent use. |
| 26 | +type MemoCache[H Hasher, T any] struct { |
| 27 | + capacity int |
| 28 | + mutex sync.Mutex |
| 29 | + cache map[string]*list.Element // The cache holding the results |
| 30 | + evictionList *list.List // A list to keep track of the order for LRU |
| 31 | + hashableItems map[string]T // This map keeps track of the original hashable items (optional) |
| 32 | +} |
| 33 | + |
| 34 | +// NewMemoCache is a function that creates a new MemoCache with a given |
| 35 | +// capacity. It returns a pointer to the created MemoCache. |
| 36 | +func NewMemoCache[H Hasher, T any](capacity int) *MemoCache[H, T] { |
| 37 | + return &MemoCache[H, T]{ |
| 38 | + capacity: capacity, |
| 39 | + cache: make(map[string]*list.Element), |
| 40 | + evictionList: list.New(), |
| 41 | + hashableItems: make(map[string]T), |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +// Capacity is a method that returns the capacity of the MemoCache. |
| 46 | +func (m *MemoCache[H, T]) Capacity() int { |
| 47 | + return m.capacity |
| 48 | +} |
| 49 | + |
| 50 | +// Size is a method that returns the current size of the MemoCache. It is |
| 51 | +// the number of items currently stored in the cache. |
| 52 | +func (m *MemoCache[H, T]) Size() int { |
| 53 | + m.mutex.Lock() |
| 54 | + defer m.mutex.Unlock() |
| 55 | + return m.evictionList.Len() |
| 56 | +} |
| 57 | + |
| 58 | +// Get is a method that returns the value associated with the given |
| 59 | +// hashable item in the MemoCache. If there is no corresponding value, the |
| 60 | +// method returns nil. |
| 61 | +func (m *MemoCache[H, T]) Get(h H) (T, bool) { |
| 62 | + m.mutex.Lock() |
| 63 | + defer m.mutex.Unlock() |
| 64 | + |
| 65 | + hashedKey := h.Hash() |
| 66 | + if element, found := m.cache[hashedKey]; found { |
| 67 | + m.evictionList.MoveToFront(element) |
| 68 | + return element.Value.(*entry[T]).value, true |
| 69 | + } |
| 70 | + var result T |
| 71 | + return result, false |
| 72 | +} |
| 73 | + |
| 74 | +// Set is a method that sets the value for the given hashable item in the |
| 75 | +// MemoCache. If the cache is at capacity, it evicts the least recently |
| 76 | +// used item before adding the new item. |
| 77 | +func (m *MemoCache[H, T]) Set(h H, value T) { |
| 78 | + m.mutex.Lock() |
| 79 | + defer m.mutex.Unlock() |
| 80 | + |
| 81 | + hashedKey := h.Hash() |
| 82 | + if element, found := m.cache[hashedKey]; found { |
| 83 | + m.evictionList.MoveToFront(element) |
| 84 | + element.Value.(*entry[T]).value = value |
| 85 | + return |
| 86 | + } |
| 87 | + |
| 88 | + // Check if the cache is at capacity |
| 89 | + if m.evictionList.Len() >= m.capacity { |
| 90 | + // Evict the least recently used item from the cache |
| 91 | + toEvict := m.evictionList.Back() |
| 92 | + if toEvict != nil { |
| 93 | + evictedEntry := m.evictionList.Remove(toEvict).(*entry[T]) |
| 94 | + delete(m.cache, evictedEntry.key) |
| 95 | + delete(m.hashableItems, evictedEntry.key) // if you're keeping track of original items |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + // Add the value to the cache and the evictionList |
| 100 | + newEntry := &entry[T]{ |
| 101 | + key: hashedKey, |
| 102 | + value: value, |
| 103 | + } |
| 104 | + element := m.evictionList.PushFront(newEntry) |
| 105 | + m.cache[hashedKey] = element |
| 106 | + m.hashableItems[hashedKey] = value // if you're keeping track of original items |
| 107 | +} |
| 108 | + |
| 109 | +// HString is a type that implements the Hasher interface for strings. |
| 110 | +type HString string |
| 111 | + |
| 112 | +// Hash is a method that returns the hash of the string. |
| 113 | +func (h HString) Hash() string { |
| 114 | + return fmt.Sprintf("%x", sha256.Sum256([]byte(h))) |
| 115 | +} |
| 116 | + |
| 117 | +// HInt is a type that implements the Hasher interface for integers. |
| 118 | +type HInt int |
| 119 | + |
| 120 | +// Hash is a method that returns the hash of the integer. |
| 121 | +func (h HInt) Hash() string { |
| 122 | + return fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprintf("%d", h)))) |
| 123 | +} |
0 commit comments