-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First steps in IdentityMap implementation
- Loading branch information
Ivan Sushkov
committed
May 31, 2024
1 parent
5e5dccc
commit 4d3d77d
Showing
7 changed files
with
390 additions
and
63 deletions.
There are no files selected for viewing
24 changes: 0 additions & 24 deletions
24
grade/internal/infrastructure/seedwork/identity/manageable.go
This file was deleted.
Oops, something went wrong.
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package cache | ||
|
||
import ( | ||
"container/list" | ||
) | ||
|
||
type Lru[T comparable] struct { | ||
items map[T]*list.Element | ||
order *list.List | ||
size uint | ||
} | ||
|
||
func NewLru[T comparable](size uint) Lru[T] { | ||
return Lru[T]{ | ||
items: make(map[T]*list.Element, size), | ||
order: list.New(), | ||
size: size, | ||
} | ||
} | ||
|
||
func (l *Lru[T]) Add(value T) { | ||
order := l.order.PushBack(&value) | ||
l.items[value] = order | ||
|
||
if (uint)(len(l.items)) > l.size { | ||
order = l.order.Front() | ||
l.order.Remove(order) | ||
|
||
delete(l.items, order.Value.(T)) | ||
} | ||
} | ||
|
||
func (l *Lru[T]) Touch(value T) { | ||
order := l.items[value] | ||
l.order.MoveToBack(order) | ||
} | ||
|
||
func (l *Lru[T]) Remove(value T) { | ||
order := l.items[value] | ||
|
||
delete(l.items, value) | ||
l.order.Remove(order) | ||
} | ||
|
||
func (l *Lru[T]) Clear() { | ||
l.order = list.New() | ||
l.items = make(map[T]*list.Element, l.size) | ||
} | ||
|
||
func (l *Lru[T]) SetSize(size uint) { | ||
l.size = size | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package cache | ||
|
||
import "testing" | ||
|
||
func TestLru(t *testing.T) { | ||
|
||
lru := NewLru[string](2) | ||
|
||
//lru.Add("1") | ||
//lru.Add("2") | ||
lru.Add("3") | ||
lru.Add("2") | ||
lru.Add("1") | ||
|
||
//lru.Touch("3") | ||
//lru.Add("3") | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package collections | ||
|
||
import ( | ||
"container/list" | ||
"errors" | ||
) | ||
|
||
var ( | ||
ErrKeyDoesNotContains = errors.New("") | ||
) | ||
|
||
type CachedMap[K comparable, V any] struct { | ||
m map[K]V | ||
} | ||
|
||
func NewCachedMap[K comparable, V any]() CachedMap[K, V] { | ||
return CachedMap[K, V]{ | ||
m: map[K]V{}, | ||
} | ||
} | ||
|
||
func (m CachedMap[K, V]) Add(key K, value V) { | ||
m.m[key] = value | ||
} | ||
|
||
func (m CachedMap[K, V]) Get(key K) (value V, err error) { | ||
if value, found := m.m[key]; found { | ||
return value, nil | ||
} | ||
|
||
return value, ErrKeyDoesNotContains | ||
} | ||
|
||
func (m CachedMap[K, V]) Remove(key K) { | ||
delete(m.m, key) | ||
} | ||
|
||
func (m CachedMap[K, V]) Has(key K) bool { | ||
_, found := m.m[key] | ||
return found | ||
} | ||
|
||
type item[K comparable, V any] struct { | ||
key K | ||
value V | ||
} | ||
|
||
type ReplacingMap[K comparable, V any] struct { | ||
items map[K]*list.Element | ||
order *list.List | ||
size uint | ||
} | ||
|
||
func NewReplacingMap[K comparable, V any](size uint) ReplacingMap[K, V] { | ||
return ReplacingMap[K, V]{ | ||
items: make(map[K]*list.Element, size), | ||
order: list.New(), | ||
size: size, | ||
} | ||
} | ||
|
||
func (m *ReplacingMap[K, V]) Add(key K, value V) { | ||
element := m.order.PushBack(item[K, V]{key, value}) | ||
m.items[key] = element | ||
|
||
if (uint)(len(m.items)) > m.size { | ||
element = m.order.Front() | ||
m.order.Remove(element) | ||
delete(m.items, element.Value.(item[K, V]).key) | ||
} | ||
} | ||
|
||
func (m *ReplacingMap[K, V]) Get(key K) (value V, err error) { | ||
if element, found := m.items[key]; found { | ||
return element.Value.(item[K, V]).value, nil | ||
} | ||
|
||
return value, ErrKeyDoesNotContains | ||
} | ||
|
||
func (m *ReplacingMap[K, V]) Touch(key K) { | ||
element := m.items[key] | ||
m.order.MoveToBack(element) | ||
} | ||
|
||
func (m *ReplacingMap[K, V]) Remove(key K) { | ||
element, found := m.items[key] | ||
if !found { | ||
return | ||
} | ||
|
||
delete(m.items, key) | ||
m.order.Remove(element) | ||
} | ||
|
||
func (m *ReplacingMap[K, V]) Has(key K) bool { | ||
_, found := m.items[key] | ||
return found | ||
} | ||
|
||
func (m *ReplacingMap[K, V]) SetSize(size uint) { | ||
m.size = size | ||
} |
Oops, something went wrong.