ringdict

package module
v2.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2022 License: Unlicense Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RDict

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

RDict is a data structure that behaves both as a map and as a ring. It is defined by a capacity, just as a ring. As more key-value pairs are added to it, it fills up until the capacity is reached. At that point, the first key in insertion order gets overwritten.

It is not goroutine-safe.

func New

func New[K comparable, V any](capacity int) *RDict[K, V]

New is the constructor for the RDict. One needs to provide the capacity as sole input. Note that the type parameters for key and value must also be specified.

func (*RDict[K, V]) Cap

func (r *RDict[K, V]) Cap() int

Cap returns the capacity of the RDict.

func (*RDict[K, V]) Del

func (r *RDict[K, V]) Del(key K) bool

Del removes the key-value pair from the RDict. Returns true if the provided key was present in the RDict, false otherwise. Beware: it is O(N) in the worst case, being N the capacity of the RDict.

func (*RDict[K, V]) Do

func (r *RDict[K, V]) Do(f func(K, V))

Do is the API to iterate over all the RDict elements. The provided input function receives, for each element stored in RDict, the key as first argument and the corresponding value as second.

func (*RDict[K, V]) Get

func (r *RDict[K, V]) Get(key K) (V, bool)

Get is the getter for the RDict.

func (*RDict[K, V]) Len

func (r *RDict[K, V]) Len() int

Len returns the number of elements contained by the RDict.

func (*RDict[K, V]) Safe

func (r *RDict[K, V]) Safe() *SafeRDict[K, V]

Safe returns a new SafeRDict from a RDict.

func (*RDict[K, V]) Set

func (r *RDict[K, V]) Set(key K, value V)

Set is the setter for the RDict.

type SafeRDict

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

SafeRDict is the goroutine-safe version of RDict.

func NewSafe

func NewSafe[K comparable, V any](capacity int) *SafeRDict[K, V]

NewSafe is the constructor for the SafeRDict. One needs to provide the capacity as sole input. Note that the type parameters for key and value must also be specified.

func (*SafeRDict[K, V]) Cap

func (s *SafeRDict[K, V]) Cap() int

Cap returns the capacity of the SafeRDict. Does not lock.

func (*SafeRDict[K, V]) Del

func (s *SafeRDict[K, V]) Del(key K) bool

Del removes the key-value pair from the RDict. Returns true if the provided key was present in the RDict, false otherwise. Beware: it is O(N) in the worst case, being N the capacity of the SafeRDict. Locks RW.

func (*SafeRDict[K, V]) Do

func (s *SafeRDict[K, V]) Do(f func(K, V))

Do is the API to iterate over all the RDict elements. The provided input function receives, for each element stored in RDict, the key as first argument and the corresponding value as second. Locks R.

func (*SafeRDict[K, V]) Get

func (s *SafeRDict[K, V]) Get(key K) (V, bool)

Get is the setter for the SafeRDict. Locks R.

func (*SafeRDict[K, V]) Len

func (s *SafeRDict[K, V]) Len() int

Len returns the number of elements contained by the SafeRDict. Locks R.

func (*SafeRDict[K, V]) Set

func (s *SafeRDict[K, V]) Set(key K, value V)

Set is the setter for the SafeRDict. Locks RW.

func (*SafeRDict[K, V]) Unsafe

func (s *SafeRDict[K, V]) Unsafe() *RDict[K, V]

Unsafe returns a new RDict from a SafeRDict.

Jump to

Keyboard shortcuts

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