x

package
v0.2.51 Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2023 License: Apache-2.0 Imports: 1 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetAndSet

func GetAndSet[T any](ptr *T, val T) (old T)

GetAndSet gets value to the *ptr and returns the old one, after setting new.

func SReverse

func SReverse[S ~[]E, E any](s S) S

SReverse reverse slice order. Doesn't clone slice. This is Fastest of these.

func SSReverse

func SSReverse[S ~[]E, E any](s S) S

SSReverse reverse slice order. Doesn't clone slice. This is a Sample.

func Swap

func Swap[T any](lhs, rhs *T) (nlhs T)

Swap two values, which must be given as ptr. Returns new lhs, aka rhs.

func Whom

func Whom[T any](b bool, yes, no T) T

Whom is exactly same as C/C++ ternary operator. In Go it's implemented with generics.

Types

type RWMap

type RWMap[M ~map[T]U, T comparable, U any] struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

RWMap is a type for a thread-safe Go map. It tries to be short and simple. Tip: It's useful to create a type alias (it allows it):

testersMap = map[int]testing.TB

Which shortens and makes easier to read its usage:

x.Tx(testers, func(m testersMap) {
    delete(m, goid())
})

func NewRWMap

func NewRWMap[M ~map[T]U, T comparable, U any](size ...int) *RWMap[M, T, U]

NewRWMap creates a new thread-safe map that's as simple as possible. The first version had only two functions Tx and Rx to allow interact with the map.

func (*RWMap[M, T, U]) Del

func (m *RWMap[M, T, U]) Del(key T) U

Del deletes a key value pair from the map.

func (*RWMap[M, T, U]) Get

func (m *RWMap[M, T, U]) Get(key T) U

func (*RWMap[M, T, U]) Rx

func (m *RWMap[M, T, U]) Rx(f func(m M))

func (*RWMap[M, T, U]) Set

func (m *RWMap[M, T, U]) Set(key T, val U) U

Set sets a key value pair to the map.

func (*RWMap[M, T, U]) Tx

func (m *RWMap[M, T, U]) Tx(f func(m M))

Tx executes a critical section during the function given as an argument. This critical section allows the map be updated. If you only need to read the map please use the Rx function that's for a read-only critical section.

type RWSlice

type RWSlice[S ~[]T, T any] struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

RWSlice is a type for a thread-safe Go slice. It tries to be short and simple. Tip: It's useful to create a type alias (it allows it):

type testersSlice = [int]testing.TB

Which shortens and makes easier to read its usage:

x.TxSlice(testers, func(s testersSlice) {
    s[goid()] = val
})

func NewRWSlice

func NewRWSlice[S ~[]T, T any](size ...int) *RWSlice[S, T]

NewRWSlice creates a new thread-safe slice that's as simple as possible. The first version had only two functions Tx and Rx to allow interact with the slice.

func (*RWSlice[S, T]) Add

func (s *RWSlice[S, T]) Add(val T) T

Add adds a value to the thread-safe slice.

func (*RWSlice[S, T]) Get

func (s *RWSlice[S, T]) Get(index int) T

Get reads thread-safely value from index index.

func (*RWSlice[S, T]) Rx

func (s *RWSlice[S, T]) Rx(f func(s S))

Rx executes thread-safe read transaction i.e. critical section for the RWSlice.

func (*RWSlice[S, T]) Set

func (s *RWSlice[S, T]) Set(index int, val T) T

Set sets a value to the slice index in a thread-safe way.

func (*RWSlice[S, T]) Tx

func (s *RWSlice[S, T]) Tx(f func(s S))

Tx executes a update transaction to the thread-safe slice.

Jump to

Keyboard shortcuts

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