threadsafe

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2025 License: Apache-2.0, MIT Imports: 2 Imported by: 0

Documentation

Overview

* imported from https://github.com/hayageek/threadsafe * (c) Copyright (c) 2024 Ravishanker Kusuma - MIT License * See LICENSE for more details

* imported from https://github.com/hayageek/threadsafe * (c) Copyright (c) 2024 Ravishanker Kusuma - MIT License * See LICENSE for more details

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 represents a thread-safe map. It uses a mutex to ensure that all operations are thread-safe.

func NewMap

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

NewMap creates a new thread-safe map. Example:

m := threadsafe.NewMap[string, int]()

func (*Map[K, V]) Clear

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

Clear removes all key-value pairs from the map. Example:

m.Clear()

func (*Map[K, V]) Contains

func (m *Map[K, V]) Contains(key K) bool

Contains checks if the map contains the specified key. Example:

contains := m.Contains("key")

func (*Map[K, V]) Copy

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

Copy returns a new thread-safe map that is a copy of the current map. Example:

copyMap := m.Copy()

func (*Map[K, V]) Delete

func (m *Map[K, V]) Delete(key K)

Delete removes the value associated with the key. Example:

m.Delete("key")

func (*Map[K, V]) Get

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

Get retrieves the value associated with the key. It returns the value and a boolean indicating whether the key was found. Example:

value, ok := m.Get("key")

func (*Map[K, V]) Keys

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

Keys returns a slice of all keys present in the map. Example:

keys := m.Keys()

func (*Map[K, V]) Length

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

Length returns the number of key-value pairs in the map. Example:

length := m.Length()

func (*Map[K, V]) Set

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

Set sets the value for the given key. Example:

m.Set("key", 100)

func (*Map[K, V]) Values

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

Values returns a slice of all values present in the map. Example:

values := m.Values()

type Slice

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

Slice represents a thread-safe slice. It uses a mutex to ensure that all operations are thread-safe.

func NewSlice

func NewSlice[T any]() *Slice[T]

NewSlice creates a new thread-safe slice. Example:

slice := threadsafe.NewSlice[int]()

func (*Slice[T]) Append

func (s *Slice[T]) Append(value T)

Append appends a value to the slice. Example:

slice.Append(10)

func (*Slice[T]) Clear

func (s *Slice[T]) Clear()

Clear removes all elements from the slice. Example:

slice.Clear()

func (*Slice[T]) Contains

func (s *Slice[T]) Contains(value T) bool

Contains checks if the slice contains the specified value. Example:

contains := slice.Contains(10)

func (*Slice[T]) Copy

func (s *Slice[T]) Copy() *Slice[T]

Copy returns a new thread-safe slice that is a copy of the current slice. Example:

copySlice := slice.Copy()

func (*Slice[T]) Get

func (s *Slice[T]) Get(index int) (T, bool)

Get retrieves the value at the given index. It returns the value and a boolean indicating whether the index was valid. Example:

value, ok := slice.Get(2)

func (*Slice[T]) Insert

func (s *Slice[T]) Insert(index int, value T) bool

Insert inserts a value at the specified index. It returns a boolean indicating whether the operation was successful. Example:

ok := slice.Insert(2, 10)

func (*Slice[T]) Length

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

Length returns the length of the slice. Example:

length := slice.Length()

func (*Slice[T]) Remove

func (s *Slice[T]) Remove(index int) bool

Remove removes the element at the given index. It returns a boolean indicating whether the operation was successful. Example:

ok := slice.Remove(2)

func (*Slice[T]) Set

func (s *Slice[T]) Set(index int, value T) bool

Set sets the value at the given index. It returns a boolean indicating whether the operation was successful. Example:

ok := slice.Set(2, 100)

func (*Slice[T]) Values

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

Values returns a copy of the slice's data as a regular slice. Example:

values := slice.Values()

Jump to

Keyboard shortcuts

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