shrinkingmap

package
v0.0.0-...-15a9106 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2023 License: Apache-2.0, BSD-2-Clause Imports: 2 Imported by: 5

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(opts *Options)

Option is a function setting an Options option.

func WithShrinkingThresholdCount

func WithShrinkingThresholdCount(count int) Option

WithShrinkingThresholdCount defines the count of deletions that triggers shrinking of the map.

func WithShrinkingThresholdRatio

func WithShrinkingThresholdRatio(ratio float32) Option

WithShrinkingThresholdRatio defines the ratio between the amount of deleted keys and the current map's size before shrinking is triggered.

type Options

type Options struct {
	// contains filtered or unexported fields
}

Options define options for a ShrinkingMap.

type ShrinkingMap

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

ShrinkingMap provides a non concurrent-safe map that shrinks if certain conditions are met (AND condition). Default values are: - ShrinkingThresholdRatio: 10.0 (set to 0.0 to disable) - ShrinkingThresholdCount: 100 (set to 0 to disable).

func New

func New[K comparable, V any](opts ...Option) *ShrinkingMap[K, V]

New returns a new ShrinkingMap.

func (*ShrinkingMap[K, V]) AsMap

func (s *ShrinkingMap[K, V]) AsMap() (asMap map[K]V)

AsMap returns the shrinking map as a regular map.

func (*ShrinkingMap[K, V]) Clear

func (s *ShrinkingMap[K, V]) Clear()

Clear removes all the entries from the map.

func (*ShrinkingMap[K, V]) Compute

func (s *ShrinkingMap[K, V]) Compute(key K, updateFunc func(currentValue V, exists bool) V) (updatedValue V)

Compute computes the new value for a given key and stores it in the map.

func (*ShrinkingMap[K, V]) Delete

func (s *ShrinkingMap[K, V]) Delete(key K, optCondition ...func() bool) (deleted bool)

Delete removes the entry with the given key, and possibly shrinks the map if the shrinking conditions have been reached.

func (*ShrinkingMap[K, V]) DeleteAndReturn

func (s *ShrinkingMap[K, V]) DeleteAndReturn(key K) (value V, deleted bool)

DeleteAndReturn removes the entry with the given key, and returns the deleted value (if it existed).

func (*ShrinkingMap[K, V]) ForEach

func (s *ShrinkingMap[K, V]) ForEach(callback func(K, V) bool)

ForEach iterates through the map and calls the consumer for every element. Returning false from this function indicates to abort the iteration.

func (*ShrinkingMap[K, V]) ForEachKey

func (s *ShrinkingMap[K, V]) ForEachKey(callback func(K) bool)

ForEachKey iterates through the map and calls the consumer for every element. Returning false from this function indicates to abort the iteration.

func (*ShrinkingMap[K, V]) Get

func (s *ShrinkingMap[K, V]) Get(key K) (value V, exists bool)

Get returns the value mapped to the given key, and the boolean flag that indicated if the key exists.

func (*ShrinkingMap[K, V]) GetOrCreate

func (s *ShrinkingMap[K, V]) GetOrCreate(key K, defaultValueFunc func() V) (value V, created bool)

GetOrCreate returns the value mapped to the given key and the boolean flag that indicated if the values was created. If the value does not exist the passed func will be called and the provided value will be set.

func (*ShrinkingMap[K, V]) Has

func (s *ShrinkingMap[K, V]) Has(key K) (has bool)

Has returns if an entry with the given key exists.

func (*ShrinkingMap[K, V]) IsEmpty

func (s *ShrinkingMap[K, V]) IsEmpty() (empty bool)

IsEmpty returns if the map is empty.

func (*ShrinkingMap[K, V]) Keys

func (s *ShrinkingMap[K, V]) Keys() []K

Keys creates a slice of the map keys.

func (*ShrinkingMap[K, V]) Pop

func (s *ShrinkingMap[K, V]) Pop() (key K, value V, exists bool)

Pop removes the first element from the map and returns it.

func (*ShrinkingMap[K, V]) Set

func (s *ShrinkingMap[K, V]) Set(key K, value V) (wasCreated bool)

Set adds a key-value pair to the map. It returns true if the key was created.

func (*ShrinkingMap[K, V]) Shrink

func (s *ShrinkingMap[K, V]) Shrink()

Shrink shrinks the map.

func (*ShrinkingMap[K, V]) Size

func (s *ShrinkingMap[K, V]) Size() (size int)

Size returns the number of entries in the map.

func (*ShrinkingMap[K, V]) Values

func (s *ShrinkingMap[K, V]) Values() []V

Values creates a slice of the map values.

Jump to

Keyboard shortcuts

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