concurrent

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2022 License: BSD-3-Clause Imports: 1 Imported by: 3

Documentation

Overview

Package concurrent provides a handful of thread-safe generic types.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Map

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

Map is a concurrent map. It wraps the standard library's map with a mutex for concurrent access.

func NewMap

func NewMap[K comparable, V any]() *Map[K, V]

NewMap returns a new Map.

func (*Map[K, V]) Exists

func (m *Map[K, V]) Exists(k K) (exists bool)

Exists returns true if key exists in the map, false otherwise.

func (*Map[K, V]) Get

func (m *Map[K, V]) Get(k K) (v V, ok bool)

Get gets the value at key k, or the zero value if not set.

func (*Map[K, V]) Length

func (m *Map[K, V]) Length() int

Length returns the size of m.

func (*Map[K, V]) ReadFunc

func (m *Map[K, V]) ReadFunc(fn func(map[K]V))

ReadFunc runs fn with the mutex locked for reading. The raw map is passed to fn.

func (*Map[K, V]) Remove

func (m *Map[K, V]) Remove(k K) (exists bool)

Remove removes a key from the map. It returns true if the key existed in the map, false otherwise.

func (*Map[K, V]) Set

func (m *Map[K, V]) Set(k K, v V)

Set sets the key k to the value v.

func (*Map[K, V]) Values

func (m *Map[K, V]) Values() []V

Values returns all values in m. The returned slice is unordered.

func (*Map[K, V]) WriteFunc

func (m *Map[K, V]) WriteFunc(fn func(map[K]V))

WriteFunc runs fn with the mutex locked for writing. The raw map is passed to fn.

type OrderedSet

type OrderedSet[T comparable] struct {
	// contains filtered or unexported fields
}

OrderedSet has the same usage as Set, but is ordered.

func NewOrderedSet

func NewOrderedSet[T comparable](initial ...T) *OrderedSet[T]

NewOrderedSet returns a new OrderedSet.

func (*OrderedSet[T]) Add

func (s *OrderedSet[T]) Add(v T) bool

Add adds a value to the set. It returns true if the value doesn't already exist, false otherwise.

func (*OrderedSet[T]) Append

func (s *OrderedSet[T]) Append(values ...T)

Append adds a slice of values to the set.

func (*OrderedSet[T]) Exists

func (s *OrderedSet[T]) Exists(v T) (exists bool)

Exists returns true if v exists in the set, false otherwise.

func (*OrderedSet[T]) Length

func (s *OrderedSet[T]) Length() int

Length returns the length of the set.

func (*OrderedSet[T]) Remove

func (s *OrderedSet[T]) Remove(v T) (exists bool)

Remove removes a value from the set. It returns true if the value existed in the set, false otherwise.

func (*OrderedSet[T]) Values

func (s *OrderedSet[T]) Values() []T

Values returns all values in the set. The return slice is unordered.

type Set

type Set[T comparable] struct {
	// contains filtered or unexported fields
}

Set is a concurrent set. It is not ordered.

func NewSet

func NewSet[T comparable](initial ...T) *Set[T]

NewSet returns a new Set.

func (*Set[T]) Add

func (s *Set[T]) Add(v T) bool

Add adds a value to the set. It returns true if the value doesn't already exist, false otherwise.

func (*Set[T]) Append

func (s *Set[T]) Append(values ...T)

Append adds a slice of values to the set.

func (*Set[T]) Exists

func (s *Set[T]) Exists(v T) (exists bool)

Exists returns true if v exists in the set, false otherwise.

func (*Set[T]) Length

func (s *Set[T]) Length() int

Length returns the length of the set.

func (*Set[T]) Remove

func (s *Set[T]) Remove(v T) (exists bool)

Remove removes a value from the set. It returns true if the value existed in the set, false otherwise.

func (*Set[T]) Values

func (s *Set[T]) Values() []T

Values returns all values in the set. The return slice is unordered.

type Value

type Value[T any] struct {
	// contains filtered or unexported fields
}

Value wraps a single value. A zero Value is valid. The wrapped type should not be a pointer.

func (*Value[T]) Get

func (s *Value[T]) Get() T

func (*Value[T]) Set

func (s *Value[T]) Set(val T)

Jump to

Keyboard shortcuts

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