memoization

package
v2.0.0-alpha.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 12, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package memoization implement a simple memoization cache. It's designed to improve performance in textarea.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HInt

type HInt int

HInt is a type that implements the Hasher interface for integers.

func (HInt) Hash

func (h HInt) Hash() string

Hash is a method that returns the hash of the integer.

type HString

type HString string

HString is a type that implements the Hasher interface for strings.

func (HString) Hash

func (h HString) Hash() string

Hash is a method that returns the hash of the string.

type Hasher

type Hasher interface {
	Hash() string
}

Hasher is an interface that requires a Hash method. The Hash method is expected to return a string representation of the hash of the object.

type MemoCache

type MemoCache[H Hasher, T any] struct {
	// contains filtered or unexported fields
}

MemoCache is a struct that represents a cache with a set capacity. It uses an LRU (Least Recently Used) eviction policy. It is safe for concurrent use.

func NewMemoCache

func NewMemoCache[H Hasher, T any](capacity int) *MemoCache[H, T]

NewMemoCache is a function that creates a new MemoCache with a given capacity. It returns a pointer to the created MemoCache.

func (*MemoCache[H, T]) Capacity

func (m *MemoCache[H, T]) Capacity() int

Capacity is a method that returns the capacity of the MemoCache.

func (*MemoCache[H, T]) Get

func (m *MemoCache[H, T]) Get(h H) (T, bool)

Get is a method that returns the value associated with the given hashable item in the MemoCache. If there is no corresponding value, the method returns nil.

func (*MemoCache[H, T]) Set

func (m *MemoCache[H, T]) Set(h H, value T)

Set is a method that sets the value for the given hashable item in the MemoCache. If the cache is at capacity, it evicts the least recently used item before adding the new item.

func (*MemoCache[H, T]) Size

func (m *MemoCache[H, T]) Size() int

Size is a method that returns the current size of the MemoCache. It is the number of items currently stored in the cache.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL