maps

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2022 License: MIT Imports: 4 Imported by: 2

README

go-maps

Provides a selection of hashmaps (or, "dictionaries") with features exceeding that of the default Go runtime hashmap.

Includes:

  • OrderedMap
  • LRUMap

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LRUMap

type LRUMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

LRU provides an ordered hashmap implementation that keeps elements ordered according to last recently used (hence, LRU).

func NewLRU

func NewLRU[K comparable, V any](len, cap int) *LRUMap[K, V]

NewLRU returns a new instance of LRUMap with given initializing length and maximum capacity.

func (*LRUMap[K, V]) Add

func (m *LRUMap[K, V]) Add(key K, value V) bool

Add will add the given key-value pair to the map, pushing them to the front of the map. Returns false if already exists. Evicts old at maximum capacity.

func (*LRUMap[K, V]) AddWithHook

func (m *LRUMap[K, V]) AddWithHook(key K, value V, evict func(K, V)) bool

AddWithHook performs .Add() but passing any evicted entry to given hook function.

func (*LRUMap[K, V]) Cap

func (m *LRUMap[K, V]) Cap() int

Cap returns the maximum capacity of this LRU map.

func (*LRUMap) Delete

func (m *LRUMap) Delete(key K) bool

Delete will delete given key from map, returns false if not found.

func (*LRUMap[K, V]) Format

func (m *LRUMap[K, V]) Format(state fmt.State, verb rune)

Format implements fmt.Formatter, allowing performant string formatting of map.

func (*LRUMap[K, V]) Get

func (m *LRUMap[K, V]) Get(key K) (V, bool)

Get will fetch value for given key from map, in the process pushing it to the front of the map. Returns false if not found.

func (*LRUMap) Has

func (m *LRUMap) Has(key K) bool

Has returns whether key exists in map.

func (*LRUMap[K, V]) Init

func (m *LRUMap[K, V]) Init(len, cap int)

Init will initialize this map with initial length and maximum capacity.

func (*LRUMap) Len

func (m *LRUMap) Len() int

Len returns the current length of the map.

func (*LRUMap) Range

func (m *LRUMap) Range(start, length int, fn func(int, K, V))

Range passes given function over the requested range of the map.

func (*LRUMap) RangeIf

func (m *LRUMap) RangeIf(start, length int, fn func(int, K, V) bool)

RangeIf passes given function over the requested range of the map. Returns early on 'fn' -> false.

func (*LRUMap[K, V]) Set

func (m *LRUMap[K, V]) Set(key K, value V)

Set will ensure that given key-value pair exists in the map, by either adding new or updating existing, pushing them to the front of the map. Evicts old at maximum capacity.

func (*LRUMap[K, V]) SetWithHook

func (m *LRUMap[K, V]) SetWithHook(key K, value V, evict func(K, V))

SetWithHook performs .Set() but passing any evicted entry to given hook function.

func (*LRUMap) Std

func (m *LRUMap) Std() map[K]V

Std returns a clone of map's data in the standard library equivalent map type.

func (*LRUMap) Truncate

func (m *LRUMap) Truncate(sz int, fn func(K, V))

Truncate will truncate the map from the back by given amount, passing dropped elements to given function.

type OrderedMap

type OrderedMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

OrderedMap provides a hashmap implementation that tracks the order in which keys are added.

func NewOrdered

func NewOrdered[K comparable, V any](len int) *OrderedMap[K, V]

NewOrdered returns a new instance of LRUMap with given initializing length and maximum capacity.

func (*OrderedMap[K, V]) Add

func (m *OrderedMap[K, V]) Add(key K, value V) bool

Add will add the given key-value pair to the map, returns false if already exists.

func (*OrderedMap) Delete

func (m *OrderedMap) Delete(key K) bool

Delete will delete given key from map, returns false if not found.

func (*OrderedMap[K, V]) Format

func (m *OrderedMap[K, V]) Format(state fmt.State, verb rune)

Format implements fmt.Formatter, allowing performant string formatting of map.

func (*OrderedMap[K, V]) Get

func (m *OrderedMap[K, V]) Get(key K) (V, bool)

Get will fetch value for given key from map. Returns false if not found.

func (*OrderedMap) Has

func (m *OrderedMap) Has(key K) bool

Has returns whether key exists in map.

func (*OrderedMap[K, V]) Index

func (m *OrderedMap[K, V]) Index(idx int) (K, V, bool)

Index returns the key-value pair at index from map. Returns false if index out of range.

func (*OrderedMap[K, V]) Init

func (m *OrderedMap[K, V]) Init(len int)

Init will initialize this map with initial length.

func (*OrderedMap) Len

func (m *OrderedMap) Len() int

Len returns the current length of the map.

func (*OrderedMap[K, V]) Pop

func (m *OrderedMap[K, V]) Pop(idx int) (K, V)

Pop will remove and return the key-value pair at index in the map. Panics if index out of range.

func (*OrderedMap[K, V]) Push

func (m *OrderedMap[K, V]) Push(idx int, key K, value V)

Push will insert the given key-value pair at index in the map. Panics if index out of range.

func (*OrderedMap) Range

func (m *OrderedMap) Range(start, length int, fn func(int, K, V))

Range passes given function over the requested range of the map.

func (*OrderedMap) RangeIf

func (m *OrderedMap) RangeIf(start, length int, fn func(int, K, V) bool)

RangeIf passes given function over the requested range of the map. Returns early on 'fn' -> false.

func (*OrderedMap[K, V]) Set

func (m *OrderedMap[K, V]) Set(key K, value V)

Set will ensure that given key-value pair exists in the map, by either adding new or updating existing.

func (*OrderedMap) Std

func (m *OrderedMap) Std() map[K]V

Std returns a clone of map's data in the standard library equivalent map type.

func (*OrderedMap) Truncate

func (m *OrderedMap) Truncate(sz int, fn func(K, V))

Truncate will truncate the map from the back by given amount, passing dropped elements to given function.

Jump to

Keyboard shortcuts

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