threadsafe

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2024 License: MIT Imports: 4 Imported by: 1

README

Threadsafe

A Go package providing thread-safe implementations of arrays, slices, maps, stack & queue using generics and type constraints.

Installation

To install the package, use the following command:

go get github.com/hayageek/threadsafe

Usage

This package provides thread-safe implementations for arrays, slices, maps, stack & queue. Below are usage examples and API lists for each of these data types.

Thread-Safe Array

A thread-safe array with a fixed size.

APIs
  • NewArray(size int) *Array[T] - Creates a new thread-safe array with the given size.
  • (*Array[T]) Get(index int) (T, bool) - Retrieves the value at the given index.
  • (*Array[T]) Set(index int, value T) bool - Sets the value at the given index.
  • (*Array[T]) Append(value T) - Appends a value to the array.
  • (*Array[T]) Remove(index int) bool - Removes the element at the given index.
  • (*Array[T]) Contains(value T) bool - Checks if the array contains the specified value.
  • (*Array[T]) Clear() - Clears all elements from the array.
  • (*Array[T]) Insert(index int, value T) bool - Inserts a value at the specified index.
  • (*Array[T]) Copy() *Array[T] - Returns a copy of the array.
  • (*Array[T]) Values() []T - Returns a slice of all elements in the array.
  • (*Array[T]) Length() int - Returns the length of the array.
Example
package main

import (
    "fmt"
    "github.com/hayageek/threadsafe"
)

func main() {
    // Create a new thread-safe array with size 5
    arr := threadsafe.NewArray[int](5)

    // Set values in the array
    for i := 0; i < arr.Length(); i++ {
        arr.Set(i, i*10)
    }

    // Get values from the array
    for i := 0; i < arr.Length(); i++ {
        value, _ := arr.Get(i)
        fmt.Println(value)
    }
}
Thread-Safe Slice

A dynamically-sized, thread-safe slice.

APIs
  • NewSlice() *Slice[T] - Creates a new thread-safe slice.
  • (*Slice[T]) Append(value T) - Appends a value to the slice.
  • (*Slice[T]) Get(index int) (T, bool) - Retrieves the value at the given index.
  • (*Slice[T]) Set(index int, value T) bool - Sets the value at the given index.
  • (*Slice[T]) Remove(index int) bool - Removes the element at the given index.
  • (*Slice[T]) Contains(value T) bool - Checks if the slice contains the specified value.
  • (*Slice[T]) Clear() - Clears all elements from the slice.
  • (*Slice[T]) Insert(index int, value T) bool - Inserts a value at the specified index.
  • (*Slice[T]) Copy() *Slice[T] - Returns a copy of the slice.
  • (*Slice[T]) Values() []T - Returns a slice of all values present in the slice.
  • (*Slice[T]) Length() int - Returns the length of the slice.
Example
package main

import (
    "fmt"
    "github.com/hayageek/threadsafe"
)

func main() {
    // Create a new thread-safe slice
    slice := threadsafe.NewSlice[int]()

    // Append values to the slice
    for i := 0; i < 5; i++ {
        slice.Append(i * 10)
    }

    // Get values from the slice
    for i := 0; i < slice.Length(); i++ {
        value, _ := slice.Get(i)
        fmt.Println(value)
    }

    // Set values in the slice
    for i := 0; i < slice.Length(); i++ {
        slice.Set(i, i*20)
    }

    // Get updated values from the slice
    for i := 0; i < slice.Length(); i++ {
        value, _ := slice.Get(i)
        fmt.Println(value)
    }
}
Thread-Safe Map

A thread-safe map for storing key-value pairs.

APIs
  • NewMap() *Map[K, V] - Creates a new thread-safe map.
  • (*Map[K, V]) Get(key K) (V, bool) - Retrieves the value associated with the key.
  • (*Map[K, V]) Set(key K, value V) - Sets the value for the given key.
  • (*Map[K, V]) Delete(key K) - Deletes the value associated with the key.
  • (*Map[K, V]) Contains(key K) bool - Checks if the map contains the specified key.
  • (*Map[K, V]) Clear() - Clears all key-value pairs from the map.
  • (*Map[K, V]) Copy() *Map[K, V] - Returns a copy of the map.
  • (*Map[K, V]) Length() int - Returns the number of key-value pairs in the map.
  • (*Map[K, V]) Keys() []K - Returns a slice of all keys present in the map.
  • (*Map[K, V]) Values() []V - Returns a slice of all values present in the map.
Example
package main

import (
    "fmt"
    "github.com/hayageek/threadsafe"
)

func main() {
    // Create a new thread-safe map
    m := threadsafe.NewMap[string, int]()

    // Set values in the map
    m.Set("one", 1)
    m.Set("two", 2)
    m.Set("three", 3)

    // Get values from the map
    value, _ := m.Get("one")
    fmt.Println(value)

    value, _ = m.Get("two")
    fmt.Println(value)

    // Delete a key from the map
    m.Delete("two")

    // Check if a key exists
    _, ok := m.Get("two")
    if !ok {
        fmt.Println("Key 'two' not found")
    }

    // Get the length of the map
    length := m.Length()
    fmt.Println("Length:", length)

    // Get all keys from the map
    keys := m.Keys()
    fmt.Println("Keys:", keys)

    // Get all values from the map
    values := m.Values()
    fmt.Println("Values:", values)
}
Thread-Safe Stack

A thread-safe stack for safely adding and removing items.

APIs
  • NewStack() *Stack - Creates a new thread-safe stack.
  • (*Stack) Push(value interface{}) - Adds an element to the stack.
  • (*Stack) Pop() (interface{}, bool) - Removes and returns an element from the stack. Returns false if the stack is empty.
  • (*Stack) Peek() (interface{}, bool) - Returns the element at the top of the stack without removing it.
  • (*Stack) IsEmpty() bool - Checks if the stack is empty.
  • (*Stack) Clear() - Clears all elements from the stack.
  • (*Stack) Values() []interface{} - Returns a slice of all elements in the stack.
  • (*Stack) Len() int - Returns the number of elements in the stack.
Example
package main

import (
    "fmt"
    "github.com/hayageek/threadsafe"
)

func main() {
    stack := threadsafe.NewStack()

    stack.Push(10)
    stack.Push(20)
    stack.Push(30)

    fmt.Println("Stack length:", stack.Len())

    value, ok := stack.Pop()
    if ok {
        fmt.Println("Popped value:", value)
    } else {
        fmt.Println("Stack is empty")
    }

    fmt.Println("Stack length after pop:", stack.Len())
}
Thread-Safe Queue

A thread-safe queue for safely adding and removing items.

APIs
  • NewQueue() *Queue - Creates a new thread-safe queue.
  • (*Queue) Enqueue(value interface{}) - Adds an element to the queue.
  • (*Queue) Dequeue() (interface{}, bool) - Removes and returns an element from the queue. Returns false if the queue is empty.
  • (*Queue) Peek() (interface{}, bool) - Returns the element at the front of the queue without removing it.
  • (*Queue) IsEmpty() bool - Checks if the queue is empty.
  • (*Queue) Clear() - Clears all elements from the queue.
  • (*Queue) Values() []interface{} - Returns a slice of all elements in the queue.
  • (*Queue) Len() int - Returns the number of elements in the queue.
Queue Example
package main

import (
    "fmt"
    "github.com/hayageek/threadsafe"
)

func main() {
    queue := threadsafe.NewQueue()

    queue.Enqueue(10)
    queue.Enqueue(20)
    queue.Enqueue(30)

    fmt.Println("Queue length:", queue.Len())

    value, ok := queue.Dequeue()
    if ok {
        fmt.Println("Dequeued value:", value)
    } else {
        fmt.Println("Queue is empty")
    }

    fmt.Println("Queue length after dequeue:", queue.Len())
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Array

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

Array represents a thread-safe array. It uses a mutex to ensure that all operations are thread-safe.

func NewArray

func NewArray[T any](size int) *Array[T]

NewArray creates a new thread-safe array with a given size. Example:

arr := threadsafe.NewArray

func (*Array[T]) Append added in v1.0.0

func (a *Array[T]) Append(value T)

Append appends a value to the array. Example:

arr.Append(10)

func (*Array[T]) Clear added in v1.0.0

func (a *Array[T]) Clear()

Clear removes all elements from the array. Example:

arr.Clear()

func (*Array[T]) Contains added in v1.0.0

func (a *Array[T]) Contains(value T) bool

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

contains := arr.Contains(10)

func (*Array[T]) Copy added in v1.0.0

func (a *Array[T]) Copy() *Array[T]

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

copyArray := arr.Copy()

func (*Array[T]) Get

func (a *Array[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 := arr.Get(2)

func (*Array[T]) Insert added in v1.0.0

func (a *Array[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 := arr.Insert(2, 10)

func (*Array[T]) Length

func (a *Array[T]) Length() int

Length returns the length of the array. Example:

length := arr.Length()

func (*Array[T]) Remove added in v1.0.0

func (a *Array[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 := arr.Remove(2)

func (*Array[T]) Set

func (a *Array[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 := arr.Set(2, 100)

func (*Array[T]) Values added in v1.0.1

func (a *Array[T]) Values() []T

Values returns a slice of all elements in the array. Example:

values := arr.Values()

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 added in v1.0.0

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

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

m.Clear()

func (*Map[K, V]) Contains added in v1.0.0

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 added in v1.0.0

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 Queue added in v0.0.3

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

Queue is a thread-safe queue.

func NewQueue added in v0.0.3

func NewQueue() *Queue

NewQueue creates a new thread-safe queue.

func (*Queue) Clear added in v1.0.0

func (q *Queue) Clear()

Clear removes all elements from the queue. Example:

q.Clear()

func (*Queue) Dequeue added in v0.0.3

func (q *Queue) Dequeue() (interface{}, bool)

Dequeue removes and returns an element from the queue.

func (*Queue) Enqueue added in v0.0.3

func (q *Queue) Enqueue(value interface{})

Enqueue adds an element to the queue.

func (*Queue) IsEmpty added in v1.0.0

func (q *Queue) IsEmpty() bool

IsEmpty checks if the queue is empty. Example:

isEmpty := q.IsEmpty()

func (*Queue) Len added in v0.0.3

func (q *Queue) Len() int

Len returns the number of elements in the queue.

func (*Queue) Peek added in v1.0.0

func (q *Queue) Peek() (interface{}, bool)

Peek returns the element at the front of the queue without removing it. Example:

value, ok := q.Peek()

func (*Queue) Values added in v1.0.0

func (q *Queue) Values() []interface{}

Values returns a slice of all elements in the queue. Example:

values := q.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 added in v1.0.0

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

Clear removes all elements from the slice. Example:

slice.Clear()

func (*Slice[T]) Contains added in v1.0.0

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 added in v1.0.0

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 added in v1.0.0

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 added in v1.0.0

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 added in v1.0.0

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

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

values := slice.Values()

type Stack added in v0.0.3

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

Stack is a thread-safe stack.

func NewStack added in v0.0.3

func NewStack() *Stack

NewStack creates a new thread-safe stack.

func (*Stack) Clear added in v1.0.0

func (s *Stack) Clear()

Clear removes all elements from the stack. Example:

s.Clear()

func (*Stack) IsEmpty added in v1.0.0

func (s *Stack) IsEmpty() bool

IsEmpty checks if the stack is empty. Example:

isEmpty := s.IsEmpty()

func (*Stack) Len added in v0.0.3

func (s *Stack) Len() int

Len returns the number of elements in the stack.

func (*Stack) Peek added in v1.0.0

func (s *Stack) Peek() (interface{}, bool)

Peek returns the element at the top of the stack without removing it. Example:

value, ok := s.Peek()

func (*Stack) Pop added in v0.0.3

func (s *Stack) Pop() (interface{}, bool)

Pop removes and returns an element from the stack.

func (*Stack) Push added in v0.0.3

func (s *Stack) Push(value interface{})

Push adds an element to the stack.

func (*Stack) Values added in v1.0.0

func (s *Stack) Values() []interface{}

Values returns a slice of all elements in the stack. Example:

values := s.Values()

Jump to

Keyboard shortcuts

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